void send_task(void* pdata) { INT8U return_code = OS_NO_ERR; INT32U msg = 0; OS_Q_DATA queue_data; while (1) { return_code = OSQQuery(msgqueue, &queue_data); alt_ucosii_check_return_code(return_code); if(queue_data.OSNMsgs < MSG_QUEUE_SIZE) /*Check the number of messages*/ { /*in the message queue*/ return_code = OSQPostOpt(msgqueue, (void *)&msg, OS_POST_OPT_BROADCAST); alt_ucosii_check_return_code(return_code); msg++; number_of_messages_sent++; } else { OSTimeDlyHMSM(0, 0, 1, 0); } } }
/*The next two task compete for a shared resource via a semaphore. The name of * the task that owns the semaphore is copied into the global variable * sem_owner_task_name[]. */ void getsem_task1(void* pdata) { INT8U return_code = OS_NO_ERR; while (1) { OSSemPend(shared_resource_sem, 0, &return_code); alt_ucosii_check_return_code(return_code); strcpy(&sem_owner_task_name[0], "getsem_task1"); getsem_task1_got_sem++; OSSemPost(shared_resource_sem); OSTimeDlyHMSM(0, 0, 0, 100); } }
/* The following task is used to initialize the operating system data structures * and to create the task. The task deletes itself as it is not * needed after initialization is complete. The convention of creating a task * that is used to initialize the reset of the application is used by Labrosse. * The main purpose for doing this is to ensure that stack checking will * initialize correctly if enabled. See MicroC/OS-II The Real-Time Kernal text * book for details. */ void initialize_task(void* pdata) { INT8U return_code = OS_NO_ERR; /*create os data structures */ initOSDataStructs(); /* create the tasks */ initCreateTasks(); /*This task is deleted because there is no need for it to run again */ return_code = OSTaskDel(OS_PRIO_SELF); alt_ucosii_check_return_code(return_code); while (1); }
int main (int argc, char* argv[], char* envp[]) { INT8U return_code = OS_NO_ERR; return_code = OSTaskCreateExt(initialize_task, NULL, (void *)&initialize_task_stk[TASK_STACKSIZE], INITIALIZE_TASK_PRIORITY, INITIALIZE_TASK_PRIORITY, initialize_task_stk, TASK_STACKSIZE, NULL, 0); alt_ucosii_check_return_code(return_code); OSStart(); return 0; }
// Initializes the needed semaphores, and sets the left and right pointers void initPhilosophers() { INT8U return_code = OS_NO_ERR; char names[6] = {'N', 'E', 'S', 'W', '1', '2'}; // Initialize semaphores int i; for (i=0; i<NUM_PHILOSOPHERS; i++) { utensil[i] = OSSemCreate(1); } // Initialize philosophers for (i=0; i<NUM_PHILOSOPHERS; i++) { philosophers[i].left = utensil[i]; philosophers[i].right = utensil[(i+1)%NUM_PHILOSOPHERS]; philosophers[i].state = 0; philosophers[i].count = 0; philosophers[i].name = names[i]; return_code = OSTaskCreate(philosopherLoop, &philosophers[i], (void*)&stack[i][TASK_STACKSIZE-1], i); alt_ucosii_check_return_code(return_code); } }
int initCreateTasks(void) { INT8U return_code = OS_NO_ERR; return_code = OSTaskCreateExt(getsem_task1, NULL, (void *)&getsem_task1_stk[TASK_STACKSIZE], GETSEM_TASK1_PRIORITY, GETSEM_TASK1_PRIORITY, getsem_task1_stk, TASK_STACKSIZE, NULL, 0); alt_ucosii_check_return_code(return_code); return_code = OSTaskCreateExt(getsem_task2, NULL, (void *)&getsem_task2_stk[TASK_STACKSIZE], GETSEM_TASK2_PRIORITY, GETSEM_TASK2_PRIORITY, getsem_task2_stk, TASK_STACKSIZE, NULL, 0); alt_ucosii_check_return_code(return_code); return_code = OSTaskCreateExt(receive_task1, NULL, (void *)&receive_task1_stk[TASK_STACKSIZE], RECEIVE_TASK1_PRIORITY, RECEIVE_TASK1_PRIORITY, receive_task1_stk, TASK_STACKSIZE, NULL, 0); alt_ucosii_check_return_code(return_code); return_code = OSTaskCreateExt(receive_task2, NULL, (void *)&receive_task2_stk[TASK_STACKSIZE], RECEIVE_TASK2_PRIORITY, RECEIVE_TASK2_PRIORITY, receive_task2_stk, TASK_STACKSIZE, NULL, 0); alt_ucosii_check_return_code(return_code); return_code = OSTaskCreateExt(send_task, NULL, (void *)&send_task_stk[TASK_STACKSIZE], SEND_TASK_PRIORITY, SEND_TASK_PRIORITY, send_task_stk, TASK_STACKSIZE, NULL, 0); alt_ucosii_check_return_code(return_code); return_code = OSTaskCreateExt(print_status_task, NULL, (void *)&print_status_task_stk[TASK_STACKSIZE], PRINT_STATUS_TASK_PRIORITY, PRINT_STATUS_TASK_PRIORITY, print_status_task_stk, TASK_STACKSIZE, NULL, 0); alt_ucosii_check_return_code(return_code); return 0; }