Skip to content

Commit

Permalink
feat: init threads
Browse files Browse the repository at this point in the history
  • Loading branch information
drawbu committed Apr 22, 2024
1 parent bc0b634 commit 4589541
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
47 changes: 40 additions & 7 deletions src/panoramix.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "debug.h"
Expand Down Expand Up @@ -46,18 +47,50 @@ static bool parse_args(int argc, char **argv, gaule_t *gaule)
return true;
}

static void *run_villager(villager_t *villager)
{
DEBUG("Running villager [%lu]", villager->id);
return villager;
}

static int gaulois_land(gaule_t *gaule)
{
villager_t *vil = NULL;

if (pthread_mutex_init(&gaule->mutex, NULL) != 0) {
perror("mutex");
return Error;
}
for (size_t i = 0; i < gaule->nb_villagers; i++) {
vil = gaule->villagers + i;
*vil = (villager_t){
.id = i,
.gaule = gaule,
};
if (pthread_create(
&vil->thread, NULL, (void *(*)(void *)) & run_villager, vil)) {
perror("pthread_create");
return Error;
}
pthread_join(vil->thread, NULL);
}
pthread_mutex_destroy(&gaule->mutex);
return Valid;
}

int panoramix(int argc, char **argv)
{
gaule_t pano = {0};
gaule_t gaule = {0};

if (!parse_args(argc, argv, &pano))
if (!parse_args(argc, argv, &gaule))
return Error;
pano.villagers = malloc(pano.nb_villagers * sizeof *pano.villagers);
if (pano.villagers == NULL) {
gaule.villagers = malloc(gaule.nb_villagers * sizeof *gaule.villagers);
if (gaule.villagers == NULL) {
perror("malloc");
return Error;
}

free(pano.villagers);
return Valid;
memset(gaule.villagers, 0, gaule.nb_villagers * sizeof *gaule.villagers);
int ret = gaulois_land(&gaule);
free(gaule.villagers);
return ret;
}
7 changes: 6 additions & 1 deletion src/panoramix.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
#pragma once

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

typedef struct gaule_s gaule_t;

typedef struct {
size_t id;
pthread_t thread;
gaule_t *gaule;
} villager_t;

typedef struct {
typedef struct gaule_s {
// Those variables MUST NOT be moved in the structure
unsigned long nb_villagers;
unsigned long pot_size;
Expand Down

0 comments on commit 4589541

Please sign in to comment.