void firsttask(void *arg) { POSTASK_t t; UVAR_t i; posAtomicSet(&counter, 0); /* creating the flag */ flagset = posFlagCreate(); if (flagset == NULL) { nosPrint("Failed to create a set of flags!\n"); return; } /* creating semaphore object with set one default value */ semaphore = posSemaCreate(1); if (semaphore == NULL){ nosPrint("Failed to create a semaphore!\n"); return; } t = nosTaskCreate(task2, /* ptr to function: task2 that is executed */ NULL, /* optional argument, not used here */ 1, /* priority of the first task */ 0, /* stack size for the first task, 0 = default */ "task2"); /* task name */ if (t == NULL) { nosPrint("Failed to start second task!\n"); } t = nosTaskCreate(task3, /* ptr to function: task3 that is executed */ NULL, /* optional argument, not used here */ 1, /* priority of the first task */ 0, /* stack size for the first task, 0 = default */ "task3"); /* task name */ if (t == NULL) { nosPrint("Failed to start third task!\n"); } /* first flag status change */ posFlagSet(flagset, 1); /* task1 handled */ task1(arg); }
/* This is the first function that is called in the multitasking context. * (See file ex_init4.c for how to setup pico]OS). */ void firsttask(void *arg) { POSTASK_t t; posAtomicSet(&counter, 0); /* Create a semaphore, initialize to 2. * You may vary the initialization count between * 1 and 3 and observe the output of this program. * * The initialization semaphore count limits the number * of tasks that are allowed to access a shared resource * at the same time. */ semaphore = posSemaCreate(2); if (semaphore == NULL) { nosPrint("Failed to create a semaphore!\n"); return; } /* start a second task */ t = nosTaskCreate(task2, /* pointer to new task-function */ NULL, /* optional argument for the task-function */ 2, /* priority level of the new task */ 0, /* stack size (0 = default size) */ "task2"); /* optional name of the second task */ if (t == NULL) { nosPrint("Failed to start second task!\n"); } /* start a second task */ t = nosTaskCreate(task3, /* pointer to new task-function */ NULL, /* optional argument for the task-function */ 3, /* priority level of the new task */ 0, /* stack size (0 = default size) */ "task3"); /* optional name of the third task */ if (t == NULL) { nosPrint("Failed to start third task!\n"); } /* continue execution in function task1 */ task1(arg); }
/* This function is executed by the first task that is started * by pico]OS ( see the nosInit()-call in main(), file ex_init4.c ). */ void firsttask(void *arg) { NOSTASK_t t; /* Avoid compiler warning. "arg" is the "NULL" in the nosInit()-call */ (void) arg; nosPrint("First task started\n"); /* start a second task */ t = nosTaskCreate(secondtask, /* pointer to new task-function */ "Task 2", /* optional argument for the task-function */ 2, /* priority level of the new task */ 0, /* stack size (0 = default size) */ "task2"); /* optional name of the second task */ if (t == NULL) { nosPrint("Failed to start second task!\n"); } for(;;) { /* print string: "Task 1" */ nosPrint("Task 1\n"); /* sleep (=do nothing) for one second */ nosTaskSleep(MS(1000)); } }
void tcpServerThread(void* arg) { int sockd, sockd2; socklen_t addrlen; struct sockaddr_in myAddr, peerAddr; int status; sockd = socket(AF_INET, SOCK_STREAM, 0); if (sockd == -1) { nosPrintf("Socket creation error\n"); return; } /* * Bind socket to telnet port so we can test this easily. */ myAddr.sin_family = AF_INET; myAddr.sin_addr.s_addr = INADDR_ANY; myAddr.sin_port = htons(24); status = bind(sockd, (struct sockaddr*)&myAddr, sizeof(myAddr)); if (status == -1) { nosPrintf("Binding error\n"); return; } status = listen(sockd, 5); if (status == -1) { nosPrintf("Listening error\n"); return; } nosPrintf("socket server listening\n"); for(;;) { /* * Wait for new connection. */ addrlen = sizeof(peerAddr); sockd2 = accept(sockd, (struct sockaddr*)&peerAddr, &addrlen); if (sockd2 == -1) { nosPrintf("Wrong connection\n"); continue; } /* * Create thread to serve connection. */ if (nosTaskCreate(tcpClientThread, (void*)(long)sockd2, 5, 512, "socktest") == NULL) close(sockd2); } }
/* This is the first function that is called in the multitasking context. * (See file ex_init4.c for how to setup pico]OS). */ void firsttask(void *arg) { POSTASK_t t; VAR_t status; (void) arg; /* create a semaphore, initialize to 0 */ event = posSemaCreate(0); if (event == NULL) { nosPrint("Failed to create a semaphore!\n"); return; } /* start a second task */ t = nosTaskCreate(task2, /* pointer to new task-function */ event, /* optional argument for the task-function */ 2, /* priority level of the new task */ 0, /* stack size (0 = default size) */ "task2"); /* optional name of the second task */ if (t == NULL) { nosPrint("Failed to start second task!\n"); return; } /* install software interrupt handler (no. 4) */ status = posSoftIntSetHandler(4, softisr); if (status != E_OK) { nosPrint("Failed to install software interrupt handler!\n"); return; } /* Signal the interrupt every second. * Usually, the software interrupt would be raised * asynchronously through eg. a hardware isr handler. */ for(;;) { /* raise the software interrupt no. 4 */ posSoftInt(4, 0); /* wait 1 second */ posTaskSleep(MS(1000)); } }
/* * Create thread with arg. */ wwd_result_t host_rtos_create_thread_with_arg(host_thread_type_t* thread, void (*entryFunction)(uint32_t), const char* name, void* stack, uint32_t stackSize, uint32_t priority, uint32_t arg) { P_ASSERT("Cannot use pre-allocated thread stack.", stack == NULL); *thread = nosTaskCreate((POSTASKFUNC_t)entryFunction, (void*)arg, priority, stackSize, name); if (*thread == NULL) return WWD_THREAD_CREATE_FAILED; return WWD_SUCCESS; }
/* This is the first function that is called in the multitasking context. * (See file ex_init4.c for how to setup pico]OS). */ void firsttask(void *arg) { POSTASK_t t; /* Note: You may uncomment the source below by setting the #if to * zero and then observe the output of this program. * This is a nice example of how synchronization works. */ #if 1 /* create a mutex */ mutex = posMutexCreate(); if (mutex == NULL) { nosPrint("Failed to create a mutex!\n"); return; } #endif /* start a second task */ t = nosTaskCreate(task2, /* pointer to new task-function */ NULL, /* optional argument for the task-function */ 2, /* priority level of the new task */ 0, /* stack size (0 = default size) */ "task2"); /* optional name of the second task */ if (t == NULL) { nosPrint("Failed to start second task!\n"); } /* continue execution in function task1 */ task1(arg); }
void potatoStart() { nosTaskCreate(potatoTask, NULL, 2, 4000, "PotatoBus"); }
/* * Thread creation, use Pico]OS nano layer directly. */ sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio) { return nosTaskCreate(thread, arg, prio, stacksize, name); }