format and fix some minor issues

This commit is contained in:
Mylloon 2024-04-23 12:47:35 +02:00
parent dc4f0c466b
commit ce6cfc7434
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 76 additions and 66 deletions

View file

@ -14,9 +14,6 @@ struct scheduler {
/* Indicateur de changement d'état */ /* Indicateur de changement d'état */
pthread_cond_t cond; pthread_cond_t cond;
/* Taille de la pile */
int qlen;
/* Mutex qui protège la structure */ /* Mutex qui protège la structure */
pthread_mutex_t mutex; pthread_mutex_t mutex;
@ -26,6 +23,9 @@ struct scheduler {
/* Nombre de threads en attente */ /* Nombre de threads en attente */
int nthsleep; int nthsleep;
/* Taille de la pile */
int qlen;
/* Pile de tâches */ /* Pile de tâches */
struct task_info *tasks; struct task_info *tasks;
@ -33,15 +33,14 @@ struct scheduler {
int top; int top;
}; };
/* Ordonnanceur partagé */
static struct scheduler sched;
/* Lance une tâche de la pile */ /* Lance une tâche de la pile */
void *sched_worker(void *); void *sched_worker(void *);
int int
sched_init(int nthreads, int qlen, taskfunc f, void *closure) sched_init(int nthreads, int qlen, taskfunc f, void *closure)
{ {
static struct scheduler sched;
if(qlen <= 0) { if(qlen <= 0) {
fprintf(stderr, "qlen must be greater than 0\n"); fprintf(stderr, "qlen must be greater than 0\n");
return -1; return -1;
@ -56,6 +55,8 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
} }
sched.nthreads = nthreads; sched.nthreads = nthreads;
sched.nthsleep = 0;
if(pthread_mutex_init(&sched.mutex, NULL) != 0) { if(pthread_mutex_init(&sched.mutex, NULL) != 0) {
fprintf(stderr, "Can't init mutex\n"); fprintf(stderr, "Can't init mutex\n");
return -1; return -1;
@ -68,7 +69,7 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
sched.top = -1; sched.top = -1;
if((sched.tasks = malloc(qlen * sizeof(struct task_info))) == NULL) { if((sched.tasks = malloc(qlen * sizeof(struct task_info))) == NULL) {
fprintf(stderr, "Can't allocate memory for stack\n"); perror("Stack");
return -1; return -1;
} }
@ -110,6 +111,9 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
free(sched.tasks); free(sched.tasks);
pthread_mutex_destroy(&sched.mutex);
pthread_cond_destroy(&sched.cond);
return 1; return 1;
} }
@ -125,7 +129,8 @@ sched_spawn(taskfunc f, void *closure, struct scheduler *s)
return -1; return -1;
} }
s->tasks[++s->top] = (struct task_info){closure, f}; s->top++;
s->tasks[s->top] = (struct task_info){closure, f};
pthread_cond_signal(&s->cond); pthread_cond_signal(&s->cond);
pthread_mutex_unlock(&s->mutex); pthread_mutex_unlock(&s->mutex);
@ -138,6 +143,7 @@ sched_worker(void *arg)
{ {
struct scheduler *s = (struct scheduler *)arg; struct scheduler *s = (struct scheduler *)arg;
struct task_info task;
while(1) { while(1) {
pthread_mutex_lock(&s->mutex); pthread_mutex_lock(&s->mutex);
@ -166,13 +172,12 @@ sched_worker(void *arg)
s->tasks[random_index] = s->tasks[s->top]; s->tasks[random_index] = s->tasks[s->top];
s->tasks[s->top] = echange; s->tasks[s->top] = echange;
taskfunc f = s->tasks[s->top].f; task = s->tasks[s->top];
void *closure = s->tasks[s->top].closure;
s->top--; s->top--;
pthread_mutex_unlock(&s->mutex); pthread_mutex_unlock(&s->mutex);
// Exécute la tâche // Exécute la tâche
f(closure, s); task.f(task.closure, s);
} }
return NULL; return NULL;

View file

@ -14,9 +14,6 @@ struct scheduler {
/* Indicateur de changement d'état */ /* Indicateur de changement d'état */
pthread_cond_t cond; pthread_cond_t cond;
/* Taille de la pile */
int qlen;
/* Mutex qui protège la structure */ /* Mutex qui protège la structure */
pthread_mutex_t mutex; pthread_mutex_t mutex;
@ -26,6 +23,9 @@ struct scheduler {
/* Nombre de threads en attente */ /* Nombre de threads en attente */
int nthsleep; int nthsleep;
/* Taille de la pile */
int qlen;
/* Pile de tâches */ /* Pile de tâches */
struct task_info *tasks; struct task_info *tasks;
@ -33,15 +33,14 @@ struct scheduler {
int top; int top;
}; };
/* Ordonnanceur partagé */
static struct scheduler sched;
/* Lance une tâche de la pile */ /* Lance une tâche de la pile */
void *sched_worker(void *); void *sched_worker(void *);
int int
sched_init(int nthreads, int qlen, taskfunc f, void *closure) sched_init(int nthreads, int qlen, taskfunc f, void *closure)
{ {
static struct scheduler sched;
if(qlen <= 0) { if(qlen <= 0) {
fprintf(stderr, "qlen must be greater than 0\n"); fprintf(stderr, "qlen must be greater than 0\n");
return -1; return -1;
@ -56,6 +55,8 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
} }
sched.nthreads = nthreads; sched.nthreads = nthreads;
sched.nthsleep = 0;
if(pthread_mutex_init(&sched.mutex, NULL) != 0) { if(pthread_mutex_init(&sched.mutex, NULL) != 0) {
fprintf(stderr, "Can't init mutex\n"); fprintf(stderr, "Can't init mutex\n");
return -1; return -1;
@ -68,7 +69,7 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
sched.top = -1; sched.top = -1;
if((sched.tasks = malloc(qlen * sizeof(struct task_info))) == NULL) { if((sched.tasks = malloc(qlen * sizeof(struct task_info))) == NULL) {
fprintf(stderr, "Can't allocate memory for stack\n"); perror("Stack");
return -1; return -1;
} }
@ -107,6 +108,9 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
free(sched.tasks); free(sched.tasks);
pthread_mutex_destroy(&sched.mutex);
pthread_cond_destroy(&sched.cond);
return 1; return 1;
} }
@ -122,7 +126,8 @@ sched_spawn(taskfunc f, void *closure, struct scheduler *s)
return -1; return -1;
} }
s->tasks[++s->top] = (struct task_info){closure, f}; s->top++;
s->tasks[s->top] = (struct task_info){closure, f};
pthread_cond_signal(&s->cond); pthread_cond_signal(&s->cond);
pthread_mutex_unlock(&s->mutex); pthread_mutex_unlock(&s->mutex);
@ -135,6 +140,7 @@ sched_worker(void *arg)
{ {
struct scheduler *s = (struct scheduler *)arg; struct scheduler *s = (struct scheduler *)arg;
struct task_info task;
while(1) { while(1) {
pthread_mutex_lock(&s->mutex); pthread_mutex_lock(&s->mutex);
@ -157,13 +163,12 @@ sched_worker(void *arg)
} }
// Extrait la tâche de la pile // Extrait la tâche de la pile
taskfunc f = s->tasks[s->top].f; task = s->tasks[s->top];
void *closure = s->tasks[s->top].closure;
s->top--; s->top--;
pthread_mutex_unlock(&s->mutex); pthread_mutex_unlock(&s->mutex);
// Exécute la tâche // Exécute la tâche
f(closure, s); task.f(task.closure, s);
} }
return NULL; return NULL;

View file

@ -12,8 +12,8 @@ struct task_info {
}; };
struct scheduler { struct scheduler {
/* Taille des piles */ /* Dernier élément du deck (premier ajouter) */
int qlen; int *bottom;
/* Variable de conditions pour reveillé les threads au besoin */ /* Variable de conditions pour reveillé les threads au besoin */
pthread_cond_t cond; pthread_cond_t cond;
@ -24,29 +24,27 @@ struct scheduler {
/* Nombre de threads instanciés */ /* Nombre de threads instanciés */
int nthreads; int nthreads;
/* Compteur des threads dormants */
int nthsleep;
/* Taille des piles */
int qlen;
/* Piles de tâches */ /* Piles de tâches */
struct task_info **tasks; struct task_info **tasks;
/* Liste des threads */ /* Liste des threads */
pthread_t *threads; pthread_t *threads;
/* Compteur des threads dormants */ /* Premier élément du deck (dernier ajouter) */
int nthsleep;
/* Deck permettant de récupérer aussi bien le premier élément ajouté
* que le dernier */
int *top; int *top;
int *bottom;
}; };
/* Ordonnanceur partagé */
static struct scheduler sched;
/* Lance une tâche de la pile */ /* Lance une tâche de la pile */
void *sched_worker(void *); void *sched_worker(void *);
/* Nettoie les opérations effectuées par l'initialisation de l'ordonnanceur */ /* Nettoie les opérations effectuées par l'initialisation de l'ordonnanceur */
int sched_init_cleanup(int); int sched_init_cleanup(struct scheduler, int);
/* Récupère l'index du thread courant */ /* Récupère l'index du thread courant */
int current_thread(struct scheduler *); int current_thread(struct scheduler *);
@ -54,6 +52,8 @@ int current_thread(struct scheduler *);
int int
sched_init(int nthreads, int qlen, taskfunc f, void *closure) sched_init(int nthreads, int qlen, taskfunc f, void *closure)
{ {
static struct scheduler sched;
sched.tasks = NULL; sched.tasks = NULL;
sched.threads = NULL; sched.threads = NULL;
sched.top = NULL; sched.top = NULL;
@ -78,23 +78,23 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
// Initialisation du mutex // Initialisation du mutex
if(pthread_mutex_init(&sched.mutex, NULL) != 0) { if(pthread_mutex_init(&sched.mutex, NULL) != 0) {
fprintf(stderr, "Can't init mutex\n"); fprintf(stderr, "Can't init mutex\n");
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
// Initialisation variable de condition // Initialisation variable de condition
if(pthread_cond_init(&sched.cond, NULL) != 0) { if(pthread_cond_init(&sched.cond, NULL) != 0) {
fprintf(stderr, "Can't init varcond\n"); fprintf(stderr, "Can't init varcond\n");
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
// Initialisation du curseur suivant l'état de la pile de chaque processus // Initialisation du curseur suivant l'état de la pile de chaque processus
if(!(sched.top = malloc(nthreads * sizeof(int)))) { if(!(sched.top = malloc(nthreads * sizeof(int)))) {
perror("Cursor top stack"); perror("Cursor top stack");
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
if(!(sched.bottom = malloc(nthreads * sizeof(int)))) { if(!(sched.bottom = malloc(nthreads * sizeof(int)))) {
perror("Cursor bottom stack"); perror("Cursor bottom stack");
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
for(int i = 0; i < nthreads; ++i) { for(int i = 0; i < nthreads; ++i) {
sched.top[i] = 0; sched.top[i] = 0;
@ -103,13 +103,13 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
// Allocation mémoire pour la pile de chaque processus // Allocation mémoire pour la pile de chaque processus
if(!(sched.tasks = malloc(nthreads * sizeof(struct task_info *)))) { if(!(sched.tasks = malloc(nthreads * sizeof(struct task_info *)))) {
perror("Stack list"); perror("Deck list");
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
for(int i = 0; i < nthreads; ++i) { for(int i = 0; i < nthreads; ++i) {
if(!(sched.tasks[i] = malloc(qlen * sizeof(struct task_info)))) { if(!(sched.tasks[i] = malloc(qlen * sizeof(struct task_info)))) {
fprintf(stderr, "Stack for thread %d: %s\n", i, strerror(errno)); fprintf(stderr, "Deck for thread %d: %s\n", i, strerror(errno));
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
} }
@ -119,13 +119,13 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
// Créer les threads // Créer les threads
if(!(sched.threads = malloc(nthreads * sizeof(pthread_t)))) { if(!(sched.threads = malloc(nthreads * sizeof(pthread_t)))) {
perror("Threads"); perror("Threads");
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
// Ajoute la tâche initiale // Ajoute la tâche initiale
if(sched_spawn(f, closure, &sched) < 0) { if(sched_spawn(f, closure, &sched) < 0) {
fprintf(stderr, "Can't create the initial task\n"); fprintf(stderr, "Can't create the initial task\n");
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
// Démarre les threads // Démarre les threads
@ -144,7 +144,7 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
pthread_mutex_lock(&sched.mutex); pthread_mutex_lock(&sched.mutex);
@ -155,45 +155,45 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
for(int i = 0; i < nthreads; ++i) { for(int i = 0; i < nthreads; ++i) {
if((pthread_join(sched.threads[i], NULL) != 0)) { if((pthread_join(sched.threads[i], NULL) != 0)) {
fprintf(stderr, "Can't wait the thread %d\n", i); fprintf(stderr, "Can't wait the thread %d\n", i);
return sched_init_cleanup(-1); return sched_init_cleanup(sched, -1);
} }
} }
return sched_init_cleanup(1); return sched_init_cleanup(sched, 1);
} }
int int
sched_init_cleanup(int ret_code) sched_init_cleanup(struct scheduler s, int ret_code)
{ {
pthread_mutex_destroy(&sched.mutex); pthread_mutex_destroy(&s.mutex);
pthread_cond_destroy(&sched.cond); pthread_cond_destroy(&s.cond);
if(sched.tasks) { if(s.tasks) {
for(int i = 0; i < sched.nthreads; ++i) { for(int i = 0; i < s.nthreads; ++i) {
if(sched.tasks[i]) { if(s.tasks[i]) {
free(sched.tasks[i]); free(s.tasks[i]);
sched.tasks[i] = NULL; s.tasks[i] = NULL;
} }
} }
free(sched.tasks); free(s.tasks);
sched.tasks = NULL; s.tasks = NULL;
} }
if(sched.threads) { if(s.threads) {
free(sched.threads); free(s.threads);
sched.threads = NULL; s.threads = NULL;
} }
if(sched.top) { if(s.top) {
free(sched.top); free(s.top);
sched.top = NULL; s.top = NULL;
} }
if(sched.bottom) { if(s.bottom) {
free(sched.bottom); free(s.bottom);
sched.bottom = NULL; s.bottom = NULL;
} }
return ret_code; return ret_code;