diff options
Diffstat (limited to 'samples/microkernel/apps/philosophers/src')
-rw-r--r-- | samples/microkernel/apps/philosophers/src/phil_fiber.c | 56 | ||||
-rw-r--r-- | samples/microkernel/apps/philosophers/src/phil_task.c | 31 |
2 files changed, 43 insertions, 44 deletions
diff --git a/samples/microkernel/apps/philosophers/src/phil_fiber.c b/samples/microkernel/apps/philosophers/src/phil_fiber.c index 7d1fed633..1380766f5 100644 --- a/samples/microkernel/apps/philosophers/src/phil_fiber.c +++ b/samples/microkernel/apps/philosophers/src/phil_fiber.c @@ -71,30 +71,28 @@ kmutex_t forks[] = {forkMutex0, forkMutex1, forkMutex2, forkMutex3, forkMutex4, * * myPrint - print a philosophers state * +* @param id Philosopher ID. +* @param str EATING or THINKING. +* * RETURNS: N/A */ -static void myPrint - ( - int id, /* philosopher ID */ - char *str /* EATING or THINKING */ - ) - { +static void myPrint(int id, char *str) +{ PRINTF("\x1b[%d;%dHPhilosopher %d %s\n", id + 1, 1, id, str); - } +} /******************************************************************************* * * myDelay - wait for a number of ticks to elapse * +* @param ticks Number of ticks to delay. +* * RETURNS: N/A */ -static void myDelay - ( - int ticks /* # of ticks to delay */ - ) - { +static void myDelay(int ticks) +{ #ifdef CONFIG_MICROKERNEL task_sleep(ticks); #else @@ -104,7 +102,7 @@ static void myDelay nano_fiber_timer_start(&timer, ticks); nano_fiber_timer_wait(&timer); #endif - } +} /******************************************************************************* * @@ -117,7 +115,7 @@ static void myDelay */ void philEntry(void) - { +{ #ifdef CONFIG_NANOKERNEL struct nano_sem *f1; /* fork #1 */ struct nano_sem *f2; /* fork #2 */ @@ -125,33 +123,33 @@ void philEntry(void) kmutex_t f1; /* fork #1 */ kmutex_t f2; /* fork #2 */ #endif - static int myId; /* next philosopher ID */ - int pri = irq_lock(); /* interrupt lock level */ - int id = myId++; /* current philosopher ID */ + static int myId; /* next philosopher ID */ + int pri = irq_lock(); /* interrupt lock level */ + int id = myId++; /* current philosopher ID */ irq_unlock(pri); - /* always take the lowest fork first */ + /* always take the lowest fork first */ if ((id+1) != N_PHILOSOPHERS) { - f1 = FORK(id); - f2 = FORK(id + 1); + f1 = FORK(id); + f2 = FORK(id + 1); } else { - f1 = FORK(0); - f2 = FORK(id); + f1 = FORK(0); + f2 = FORK(id); } while (1) { - TAKE(f1); - TAKE(f2); + TAKE(f1); + TAKE(f2); PRINT(id, "EATING "); - RANDDELAY(id); + RANDDELAY(id); - GIVE(f2); - GIVE(f1); + GIVE(f2); + GIVE(f1); PRINT(id, "THINKING"); - RANDDELAY(id); - } + RANDDELAY(id); } +} diff --git a/samples/microkernel/apps/philosophers/src/phil_task.c b/samples/microkernel/apps/philosophers/src/phil_task.c index b21a0fc18..7ae6eba82 100644 --- a/samples/microkernel/apps/philosophers/src/phil_task.c +++ b/samples/microkernel/apps/philosophers/src/phil_task.c @@ -74,27 +74,28 @@ struct nano_sem forks[N_PHILOSOPHERS]; */ int main(void) - { +{ int i; PRINTF(DEMO_DESCRIPTION, "fibers", "nanokernel"); for (i = 0; i < N_PHILOSOPHERS; i++) { - nano_sem_init(&forks[i]); - nano_task_sem_give(&forks[i]); + nano_sem_init(&forks[i]); + nano_task_sem_give(&forks[i]); } - /* create philosopher fibers */ - for (i = 0; i < N_PHILOSOPHERS; i++) + /* create philosopher fibers */ + for (i = 0; i < N_PHILOSOPHERS; i++) { task_fiber_start(&philStack[i][0], STSIZE, - (nano_fiber_entry_t) philEntry, 0, 0, 6, 0); + (nano_fiber_entry_t) philEntry, 0, 0, 6, 0); + } - /* wait forever */ + /* wait forever */ while (1) { - extern void nano_cpu_idle(void); - nano_cpu_idle(); - } + extern void nano_cpu_idle(void); + nano_cpu_idle(); } +} #else /******************************************************************************* @@ -105,14 +106,14 @@ int main(void) */ void philDemo(void) - { +{ PRINTF(DEMO_DESCRIPTION, "tasks", "microkernel"); - task_group_start(PHI); + task_group_start(PHI); - /* wait forever */ + /* wait forever */ while (1) { - task_sleep(10000); - } + task_sleep(10000); } +} #endif |