/* * ======== mutex.c ======== * This example shows the use of two tasks and one semaphore to perform * mutual exclusive data access. */ #include #include #include #include #include #include #include #include #include Void task1(UArg arg0, UArg arg1); Void task2(UArg arg0, UArg arg1); Int resource = 0; Semaphore_Handle sem; Task_Handle tsk1; Task_Handle tsk2; Int finishCount = 0; Int myTimerCount = 0; Void gapFillTimerIsr(UArg arg) { myTimerCount++; } /* * ======== main ======== */ Void main() { Task_Params taskParams; Timer_Handle gapFillTimerHandle; Error_Block eb; Timer_Params timerParams; Error_init(&eb); Timer_Params_init(&timerParams); timerParams.period = 200; /* 2 ms */ timerParams.periodType = Timer_PeriodType_MICROSECS; timerParams.startMode = Timer_StartMode_AUTO; gapFillTimerHandle = Timer_create(Timer_ANY, gapFillTimerIsr, &timerParams, &eb); if (gapFillTimerHandle == NULL) { System_abort("Doppler timer create failed"); } /* Create a Semaphore object to be use as a resource lock */ sem = Semaphore_create(1, NULL, NULL); /* Create two tasks that share a resource*/ Task_Params_init(&taskParams); taskParams.priority = 1; tsk1 = Task_create (task1, &taskParams, NULL); Task_Params_init(&taskParams); taskParams.priority = 2; tsk2 = Task_create (task2, &taskParams, NULL); BIOS_start(); } /* * ======== task1 ======== */ Void task1(UArg arg0, UArg arg1) { UInt32 time; for (;;) { System_printf("Running task1 function\n"); if (Semaphore_getCount(sem) == 0) { System_printf("Sem blocked in task1\n"); } /* Get access to resource */ Semaphore_pend(sem, BIOS_WAIT_FOREVER); /* do work by waiting for 2 system ticks to pass */ time = Clock_getTicks(); while (Clock_getTicks() <= (time + 1)) { ; } /* do work on locked resource */ resource += 1; /* unlock resource */ Semaphore_post(sem); Task_sleep(10); } } /* * ======== task2 ======== */ Void task2(UArg arg0, UArg arg1) { for (;;) { System_printf("Running task2 function\n"); if (Semaphore_getCount(sem) == 0) { System_printf("Sem blocked in task2\n"); } /* Get access to resource */ Semaphore_pend(sem, BIOS_WAIT_FOREVER); /* do work on locked resource */ resource += 1; /* unlock resource */ Semaphore_post(sem); Task_sleep(10); finishCount++; if (finishCount == 5) { System_printf("Calling BIOS_exit from task2\n"); BIOS_exit(0); } } }