示例#1
0
文件: Main.c 项目: thisiscam/P
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);
}
int pingpong_main(int argc, char *argv[])
{
    char* arg = NULL;
    if (argc >= 1)
    {
        arg = argv[1];
        if (arg != NULL)
        {
            // make a copy, because nuttxshell will delete this memory as soon as we return
            if (strcmp(arg, "stop") == 0 && ContainerProcess != NULL)
            {
                PrtStopProcess(ContainerProcess);
                ContainerProcess = NULL;
                return 0;
            }
            else
            {
                int len = strlen(arg);
                if (len > 0)
                {
                    char* copy = malloc(len+1);
                    strcpy(copy, arg);
                    arg = copy;
                } else {
                    arg = NULL;
                }
            }
        }
    }

    if (ContainerProcess != NULL)
    {
        int32_t elapsed = clock_systimer() - start;
        int32_t milliseconds = elapsed / (TICK_PER_SEC / 1000);
        printf("PingPong is running, so far %d steps in %d ms\n", steps, milliseconds);
        return 0;
    }

    // start the pingpong thread from the worker thread and not from
    // this thread.  This way we allow NuttxShell to continue processing
    // input (since NuttxShell has a waitpid() call waiting for this task to complete
    // so if we start the pingpong thread then this task will not be considered complete
    // even if this method returns because it still has a child thread that is alive
    // running the state machine.
    steps = 0;
    work_queue(LPWORK, &_work, (worker_t)StartPingPong, arg, 1);
    return 0;
}
示例#3
0
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
}
示例#4
0
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;
}