PRT_VALUE *P_FUN__SENDRELIABLE_IMPL(PRT_MACHINEINST *context, PRT_UINT32 funIndex, PRT_VALUE *value) { PRT_VALUE* target = PrtTupleGet(value, 0); while (PRT_FALSE == PrtDistSend(target, PrtTupleGet(value, 1), PrtTupleGet(value, 2))); return PrtMkNullValue(); }
void PrtPop( _Inout_ PRT_MACHINEINST_PRIV *context ) { context->lastOperation = PopStatement; PrtFreeValue(context->currentTrigger); PrtFreeValue(context->currentPayload); context->currentTrigger = PrtMkEventValue(PRT_SPECIAL_EVENT_NULL); context->currentPayload = PrtMkNullValue(); // Actual pop happens in PrtPopState; the exit function must be executed first. // context->{currentTrigger,currentPayload} are set on for the benefit of the exit function. }
int main(int argc, char *argv[]) { PRT_PROCESS *process; PRT_GUID processGuid; PRT_VALUE *payload; processGuid.data1 = 1; processGuid.data2 = 0; processGuid.data3 = 0; processGuid.data4 = 0; process = PrtStartProcess(processGuid, &P_GEND_PROGRAM, ErrorHandler, Log); payload = PrtMkNullValue(); PrtMkMachine(process, P_MACHINE_TestMachine, payload); PrtFreeValue(payload); PrtStopProcess(process); }
PRT_BOOLEAN PrtDequeueEvent( _Inout_ PRT_MACHINEINST_PRIV *context, _Inout_ PRT_FUNSTACK_INFO *frame ) { PRT_UINT32 queueLength; PRT_EVENTQUEUE *queue; PRT_UINT32* deferPacked; PRT_UINT32 i, head; queue = &context->eventQueue; queueLength = queue->eventsSize; deferPacked = PrtGetDeferredPacked(context, context->currentState); head = queue->headIndex; PRT_DBG_ASSERT(queue->size <= queueLength, "Check Failed"); PRT_DBG_ASSERT(queue->size >= 0, "Check Failed"); PRT_DBG_ASSERT(queue->headIndex >= 0, "Check Failed"); PRT_DBG_ASSERT(queue->tailIndex >= 0, "Check Failed"); // // Find the element to dequeue // for (i = 0; i < queue->size; i++) { PRT_UINT32 index = (head + i) % queueLength; PRT_EVENT e = queue->events[index]; PRT_UINT32 triggerIndex = PrtPrimGetEvent(e.trigger); if (context->receive == NULL) { if (!PrtIsEventDeferred(triggerIndex, context->currentDeferredSetCompact)) { PrtFreeValue(context->currentTrigger); PrtFreeValue(context->currentPayload); context->currentTrigger = e.trigger; context->currentPayload = e.payload; break; } } else { if (PrtIsEventReceivable(context, triggerIndex)) { PrtFreeValue(context->currentTrigger); PrtFreeValue(context->currentPayload); context->currentTrigger = e.trigger; context->currentPayload = e.payload; for (PRT_UINT32 i = 0; i < context->receive->nCases; i++) { PRT_CASEDECL *rcase = &context->receive->cases[i]; if (triggerIndex == rcase->triggerEventIndex) { frame->rcase = rcase; PrtPushNewEventHandlerFrame(context, rcase->funIndex, frame->locals); break; } } context->receive = NULL; break; } } } // // Check if not found // if (i == queue->size) { if (context->receive == NULL) { if (PrtStateHasDefaultTransitionOrAction(context)) { PrtFreeValue(context->currentTrigger); PrtFreeValue(context->currentPayload); context->currentTrigger = PrtMkEventValue(PRT_SPECIAL_EVENT_NULL); context->currentPayload = PrtMkNullValue(); return PRT_TRUE; } else { return PRT_FALSE; } } else { PRT_BOOLEAN hasDefaultCase = (context->process->program->eventSets[context->receive->caseSetIndex].packedEvents[0] & 0x1) == 1; if (hasDefaultCase) { PrtFreeValue(context->currentTrigger); PrtFreeValue(context->currentPayload); context->currentTrigger = PrtMkEventValue(PRT_SPECIAL_EVENT_NULL); context->currentPayload = PrtMkNullValue(); for (PRT_UINT32 i = 0; i < context->receive->nCases; i++) { PRT_CASEDECL *rcase = &context->receive->cases[i]; if (PRT_SPECIAL_EVENT_NULL == rcase->triggerEventIndex) { frame->rcase = rcase; PrtPushNewEventHandlerFrame(context, rcase->funIndex, frame->locals); break; } } context->receive = NULL; return PRT_TRUE; } else { return PRT_FALSE; } } } // // Collapse the event queue on the removed event // by moving the previous elements forward. // for (; i > 0; i--) { INT index = (head + i) % queueLength; INT prev = (index - 1 + queueLength) % queueLength; queue->events[index] = queue->events[prev]; } // // Adjust the queue size // queue->headIndex = (queue->headIndex + 1) % queueLength; queue->size--; PRT_DBG_ASSERT(queue->size <= queueLength, "Check Failed"); PRT_DBG_ASSERT(queue->size >= 0, "Check Failed"); PRT_DBG_ASSERT(queue->headIndex >= 0, "Check Failed"); PRT_DBG_ASSERT(queue->tailIndex >= 0, "Check Failed"); // //Log // PrtLog(PRT_STEP_DEQUEUE, context); return PRT_TRUE; }
int main(int argc, char *argv[]) { //The commandline arguments FirstArgument = argv[0]; if (argc != 5) { PrintUsage(); return; } int createMain = atoi(argv[2]); PrtAssert(createMain == 0 || createMain == 1, "CreateMain should be either 0 or 1"); int processId = atoi(argv[3]); PrtAssert(processId >= 0, "Process Id should be positive"); int nodeId = atoi(argv[4]); PRT_DBG_START_MEM_BALANCED_REGION { //Initialize the cluster configuration. PrtDistClusterConfigInitialize(argv[1]); SetCurrentDirectory(ClusterConfiguration.LocalFolder); PRT_GUID processGuid; processGuid.data1 = processId; processGuid.data2 = nodeId; //nodeId processGuid.data3 = 0; processGuid.data4 = 0; ContainerProcess = PrtStartProcess(processGuid, &P_GEND_PROGRAM, PrtDistSMExceptionHandler, PrtDistSMLogHandler); HANDLE listener = NULL; PRT_INT32 portNumber = atoi(ClusterConfiguration.ContainerPortStart) + processId; listener = CreateThread(NULL, 0, PrtDistCreateRPCServerForEnqueueAndWait, &portNumber, 0, NULL); if (listener == NULL) { PrtDistLog("Error Creating RPC server in PrtDistStartNodeManagerMachine"); } else { DWORD status; //Sleep(3000); //check if the thread is all ok GetExitCodeThread(listener, &status); if (status != STILL_ACTIVE) PrtDistLog("ERROR : Thread terminated"); } if (createMain) { //create main machine PRT_VALUE* payload = PrtMkNullValue(); PrtMkMachine(ContainerProcess, _P_MACHINE_MAIN, payload); PrtFreeValue(payload); } else { //create container machine PrtDistLog("Creating Container Machine"); PRT_VALUE* payload = PrtMkNullValue(); PrtMkMachine(ContainerProcess, P_MACHINE_Container, payload); PrtFreeValue(payload); } //after performing all operations block and wait WaitForSingleObject(listener, INFINITE); PrtStopProcess(ContainerProcess); } PRT_DBG_END_MEM_BALANCED_REGION }
int main(int argc, char *argv[]) { #ifndef PRT_PLAT_WINUSER init_ros("test_motion_planner", &argc, argv); #endif if (!ParseCommandLine(argc, argv)) { PrintUsage(); return 1; } const char* motion_planner_delta_s = getenv("MOTION_PLANNER_DELTA"); if(motion_planner_delta_s) { Delta = atoi(motion_planner_delta_s); printf("Using MOTION_PLANNER_DELTA = %d\n", Delta); } printf("Press any key to start simulation\n"); getchar(); PRT_DBG_START_MEM_BALANCED_REGION { PRT_PROCESS *process; PRT_GUID processGuid; PRT_VALUE *payload; //Initialize the workspace WORKSPACE_INFO = ParseWorkspaceConfig(workspaceConfig); #ifdef USE_DIJKSTRA_PRECOMPUTATION WS_LocationsList ends; ends.size = WORKSPACE_INFO->starts.size + WORKSPACE_INFO->ends.size; ends.locations = malloc(sizeof(WS_Coord) * ends.size); int count = 0; for(int i=0; i < WORKSPACE_INFO->starts.size; i++) { ends.locations[count++] = WORKSPACE_INFO->starts.locations[i]; } for(int i=0; i < WORKSPACE_INFO->ends.size; i++) { ends.locations[count++] = WORKSPACE_INFO->ends.locations[i]; } PreComputeObstacleDistanceH(WORKSPACE_INFO->dimension, WORKSPACE_INFO->obstacles, ends); #endif processGuid.data1 = 1; processGuid.data2 = 0; processGuid.data3 = 0; processGuid.data4 = 0; process = PrtStartProcess(processGuid, &P_GEND_PROGRAM, ErrorHandler, Log); if (cooperative) { PrtSetSchedulingPolicy(process, PRT_SCHEDULINGPOLICY_COOPERATIVE); } if (parg == NULL) { payload = PrtMkNullValue(); } else { int i = atoi(parg); payload = PrtMkIntValue(i); } PrtUpdateAssertFn(MyAssert); PrtMkMachine(process, P_MACHINE_Main, payload); if (cooperative) { // test some multithreading across state machines. #if defined(PRT_PLAT_WINUSER) HANDLE* threadsArr = (HANDLE*)PrtMalloc(threads*sizeof(HANDLE)); for (int i = 0; i < threads; i++) { DWORD threadId; threadsArr[i] = CreateThread(NULL, 16000, (LPTHREAD_START_ROUTINE)RunToIdle, process, 0, &threadId); } WaitForMultipleObjects(threads, threadsArr, TRUE, INFINITE); PrtFree(threadsArr); #elif defined(PRT_PLAT_LINUXUSER) typedef void *(*start_routine) (void *); pthread_t tid[threads]; for (int i = 0; i < threads; i++) { pthread_create(&tid[i], NULL, (start_routine)RunToIdle, (void*)process); } for (int i = 0; i < threads; i++) { pthread_join(tid[i], NULL); } #else #error Invalid Platform #endif } PrtFreeValue(payload); PrtStopProcess(process); } PRT_DBG_END_MEM_BALANCED_REGION //_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG); //_CrtDumpMemoryLeaks(); }
static void *machine_thread(void *arg) { int maxSteps = 0; printf("PingPong starting...\n"); lastCpuTime = 0; PrintHeader(); if (arg != NULL) { char* ptr = (char*)arg; maxSteps = atoi(ptr); } PRT_GUID processGuid; processGuid.data1 = 1; processGuid.data2 = 1; processGuid.data3 = 0; processGuid.data4 = 0; ContainerProcess = PrtStartProcess(processGuid, &P_GEND_PROGRAM, ErrorHandler, LogHandler); PrtSetSchedulingPolicy(ContainerProcess, PRT_SCHEDULINGPOLICY_COOPERATIVE); //create main machine PRT_VALUE* payload = PrtMkNullValue(); PrtMkMachine(ContainerProcess, _P_MACHINE_MAIN, payload); PrtFreeValue(payload); start = clock_systimer(); intervalStart = start; steps = 0; intervalSteps = 0; // push the state machines while yielding the CPU to other higher priority tasks. PRT_PROCESS_PRIV* privateProcess = (PRT_PROCESS_PRIV*)ContainerProcess; while (privateProcess->terminating == PRT_FALSE && (maxSteps == 0 || steps < maxSteps)) { PRT_STEP_RESULT result = PrtStepProcess(ContainerProcess); switch (result) { case PRT_STEP_TERMINATING: break; case PRT_STEP_IDLE: PrtWaitForWork(ContainerProcess); break; case PRT_STEP_MORE: PrtYieldThread(); break; } CountSteps(); } int32_t elapsed = clock_systimer() - start; int32_t milliseconds = elapsed / (TICK_PER_SEC / 1000); printf("PingPong ran %d steps in %d ms\n", steps, milliseconds); if (maxSteps != 0) { // cleanup the memory for the process PrtStopProcess(ContainerProcess); ContainerProcess = NULL; } return NULL; }