X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/95d328f9ca4dd6ccd2065c6c84aca83d43001804..dff9e15c44ab6340d27215957c56fa72fad246a2:/examples/gras/synchro/philosopher.c diff --git a/examples/gras/synchro/philosopher.c b/examples/gras/synchro/philosopher.c index a19c3c9311..528989242f 100644 --- a/examples/gras/synchro/philosopher.c +++ b/examples/gras/synchro/philosopher.c @@ -10,7 +10,7 @@ #include "gras.h" #include "xbt/synchro.h" -XBT_LOG_NEW_DEFAULT_CATEGORY(philo,"Logs of this example"); +XBT_LOG_NEW_DEFAULT_CATEGORY(philo, "Logs of this example"); /** Philosopher logic **/ @@ -24,34 +24,39 @@ xbt_cond_t *forks; #define EATING 1 int *state; -int *id; /* to pass a pointer to the threads without race condition */ - -static void pickup(int id, int lunch) { - INFO2("Thread %d gets hungry (lunch #%d)",id,lunch); - xbt_mutex_acquire(mutex); - while (state[(id + (philosopher_amount-1))%philosopher_amount] == EATING || - state[(id + 1)%philosopher_amount] == EATING) - { - xbt_cond_wait(forks[id], mutex); - } - - state[id] = EATING; - xbt_assert1(state[(id + (philosopher_amount-1))%philosopher_amount] == THINKING && - state[(id + 1)%philosopher_amount] == THINKING , - "Philosopher %d eats at the same time that one of its neighbors!!!",id); - - xbt_mutex_release(mutex); - INFO1("Thread %d eats",id); +int *id; /* to pass a pointer to the threads without race condition */ + +static void pickup(int id, int lunch) +{ + INFO2("Thread %d gets hungry (lunch #%d)", id, lunch); + xbt_mutex_acquire(mutex); + while (state[(id + (philosopher_amount - 1)) % philosopher_amount] == EATING + || state[(id + 1) % philosopher_amount] == EATING) { + xbt_cond_wait(forks[id], mutex); + } + + state[id] = EATING; + xbt_assert1(state[(id + (philosopher_amount - 1)) % philosopher_amount] == + THINKING + && state[(id + 1) % philosopher_amount] == THINKING, + "Philosopher %d eats at the same time that one of its neighbors!!!", + id); + + xbt_mutex_release(mutex); + INFO1("Thread %d eats", id); } -static void putdown(int id) { - INFO1("Thread %d is full",id); - xbt_mutex_acquire(mutex); - state[id] = THINKING; - xbt_cond_signal(forks[(id+(philosopher_amount-1))%philosopher_amount]); - xbt_cond_signal(forks[(id+1)%philosopher_amount]); - - xbt_mutex_release(mutex); - INFO1("Thread %d thinks",id); + +static void putdown(int id) +{ + INFO1("Thread %d is full", id); + xbt_mutex_acquire(mutex); + state[id] = THINKING; + xbt_cond_signal(forks + [(id + (philosopher_amount - 1)) % philosopher_amount]); + xbt_cond_signal(forks[(id + 1) % philosopher_amount]); + + xbt_mutex_release(mutex); + INFO1("Thread %d thinks", id); } /* @@ -64,74 +69,80 @@ int running_threads; xbt_mutex_t dead_end; /* Code ran by each thread */ -static void philo_thread(void *arg) { - int id = *(int*)arg; - int i; - - for (i=0; i=2,"This program expects one argument (the amount of philosophers)"); + + gras_init(&argc, argv); + xbt_assert0(argc >= 2, + "This program expects one argument (the amount of philosophers)"); /* initializations of the philosopher mecanisms */ philosopher_amount = atoi(argv[1]); - state = xbt_new0(int,philosopher_amount); - id = xbt_new0(int,philosopher_amount); - forks = xbt_new(xbt_cond_t,philosopher_amount); - philosophers = xbt_new(xbt_thread_t,philosopher_amount); - + state = xbt_new0(int, philosopher_amount); + id = xbt_new0(int, philosopher_amount); + forks = xbt_new(xbt_cond_t, philosopher_amount); + philosophers = xbt_new(xbt_thread_t, philosopher_amount); + mutex = xbt_mutex_init(); - for (i=0; i