From 1ed393f253d6d6ceba407196ade66fc2e156ba0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Tue, 23 Apr 2024 17:46:44 +0200 Subject: [PATCH] feat: add mutex --- src/panoramix.c | 63 ++++++++++++++++++++++++------------------------- src/panoramix.h | 2 ++ 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/panoramix.c b/src/panoramix.c index 4dd5112..f080f9d 100644 --- a/src/panoramix.c +++ b/src/panoramix.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -53,36 +54,20 @@ static bool parse_args(int argc, char **argv, gaule_t *gaule) static void *run_villager(villager_t *villager) { - DEBUG("[%lu]\tRunning villager", villager->id); printf("Villager %lu: Going into battle!\n", villager->id); - printf("Villager %lu: I'm going to sleep now.\n", villager->id); - DEBUG("[%lu]\tExit villager", villager->id); - return villager; -} - -static int gaulois_land(gaule_t *gaule) -{ - if (pthread_mutex_init(&gaule->mutex, NULL) != 0) { - perror("mutex"); - return Error; + while (villager->fights < villager->gaule->nb_fights) { + pthread_mutex_lock(&villager->gaule->mutex); + villager->fights++; + /* villager->gaule->pot_size--; */ + printf( + "Villager %lu: I need a drink... I see %lu servings left.\n", + villager->id, villager->gaule->pot_size); + pthread_mutex_unlock(&villager->gaule->mutex); } - for (size_t i = 0; i < gaule->nb_villagers; i++) { - villager_t *vil = gaule->villagers + i; - *vil = (villager_t){.id = i, .gaule = gaule}; - - if (pthread_create( - &vil->thread, NULL, (void *(*)(void *)) & run_villager, vil)) - return perror("pthread_create"), Error; - } - - for (size_t i = 0; i < gaule->nb_villagers; i++) - if (pthread_join(gaule->villagers[i].thread, NULL)) - return perror("pthread_join"), Error; - - pthread_mutex_destroy(&gaule->mutex); - return Valid; + printf("Villager %lu: I'm going to sleep now.\n", villager->id); + return villager; } int panoramix(int argc, char **argv) @@ -93,13 +78,27 @@ int panoramix(int argc, char **argv) return Error; gaule.villagers = malloc(gaule.nb_villagers * sizeof *gaule.villagers); - if (gaule.villagers == NULL) { - perror("malloc"); - return Error; - } + if (gaule.villagers == NULL) + return perror("malloc"), Error; memset(gaule.villagers, 0, gaule.nb_villagers * sizeof *gaule.villagers); - int ret = gaulois_land(&gaule); + // Initalize mutex and villagers + if (pthread_mutex_init(&gaule.mutex, NULL) != 0) + return perror("mutex"), Error; + pthread_mutex_lock(&gaule.mutex); + for (size_t i = 0; i < gaule.nb_villagers; i++) { + villager_t *vil = gaule.villagers + i; + *vil = (villager_t){.id = i, .gaule = &gaule}; + if (pthread_create( + &vil->thread, NULL, (void *(*)(void *)) & run_villager, vil)) + return perror("pthread_create"), Error; + } + pthread_mutex_unlock(&gaule.mutex); + for (size_t i = 0; i < gaule.nb_villagers; i++) + if (pthread_join(gaule.villagers[i].thread, NULL)) + return perror("pthread_join"), Error; + + pthread_mutex_destroy(&gaule.mutex); free(gaule.villagers); - return ret; + return Valid; } diff --git a/src/panoramix.h b/src/panoramix.h index 72a40a3..320c6b1 100644 --- a/src/panoramix.h +++ b/src/panoramix.h @@ -16,6 +16,8 @@ typedef struct { size_t id; pthread_t thread; gaule_t *gaule; + + unsigned long fights; } villager_t; typedef struct gaule_s {