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 82f6687
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/panoramix.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,33 @@ 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->refills >= villager->gaule->nb_refills)
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);
}
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 Down

0 comments on commit 82f6687

Please sign in to comment.