Skip to content

Commit

Permalink
fix: exit villager when it can't refill
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed May 2, 2024
1 parent 8365a08 commit 3c4f110
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/panoramix.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,35 @@ static bool parse_args(int argc, char **argv, gaule_t *gaule)
return true;
}

static bool villager_glou_glou(villager_t *villager)
{
printf("Villager %lu: I need a drink... I see %lu servings left.\n",
villager->id, villager->gaule->pot);
if (villager->gaule->pot == 0) {
if (!villager->gaule->druid_awake)
return false;
printf("Villager %lu: Hey Pano wake up! We need more potion.\n",
villager->id);
sem_post(&villager->gaule->sem_druid);
sem_wait(&villager->gaule->sem_villagers);
if (!villager->gaule->druid_awake)
return false;
}
villager->gaule->pot -= 1;
return true;
}

static void *run_villager(villager_t *villager)
{
printf("Villager %lu: Going into battle!\n", villager->id);
while (villager->fights < villager->gaule->nb_fights) {
pthread_mutex_lock(&villager->gaule->mutex);
printf("Villager %lu: I need a drink... I see %lu servings left.\n",
villager->id, villager->gaule->pot);
if (villager->gaule->pot == 0) {
printf("Villager %lu: Hey Pano wake up! We need more potion.\n",
villager->id);
sem_post(&villager->gaule->sem_druid);
sem_wait(&villager->gaule->sem_villagers);
if (!villager_glou_glou(villager)) {
pthread_mutex_unlock(&villager->gaule->mutex);
return NULL;
}
villager->gaule->pot--;
pthread_mutex_unlock(&villager->gaule->mutex);
villager->fights++;
villager->fights += 1;
printf("Villager %lu: Take that roman scum! Only %lu left.\n",
villager->id, villager->gaule->nb_fights - villager->fights);
}
Expand All @@ -74,7 +87,9 @@ static void *run_drouid(gaule_t *gaule)
sem_wait(&gaule->sem_druid);
if (gaule->refills >= gaule->nb_refills) {
printf("Druid: I'm out of viscum. I'm going back to... zZz\n");
break;
gaule->druid_awake = false;
sem_post(&gaule->sem_villagers);
return NULL;
}
gaule->pot = gaule->pot_size;
gaule->refills++;
Expand Down Expand Up @@ -126,6 +141,7 @@ int panoramix(int argc, char **argv)

if (!parse_args(argc, argv, &gaule) || summon_gaule(&gaule) == NULL)
return Error;
gaule.druid_awake = true;
for (size_t i = 0; i < gaule.nb_villagers; i++) {
vil = gaule.villagers + i;
*vil = (villager_t){.id = i, .gaule = &gaule};
Expand Down
2 changes: 2 additions & 0 deletions src/panoramix.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <pthread.h>
#include <semaphore.h>
#include <stdbool.h>
#include <unistd.h>

typedef struct gaule_s gaule_t;
Expand All @@ -31,6 +32,7 @@ typedef struct gaule_s {
pthread_mutex_t mutex;
villager_t *villagers;
pthread_t tdruid;
bool druid_awake;

sem_t sem_druid;
sem_t sem_villagers;
Expand Down

0 comments on commit 3c4f110

Please sign in to comment.