From ce6cfc74344254a3150b2fc08ed96fd394de74dd Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 23 Apr 2024 12:47:35 +0200 Subject: [PATCH] format and fix some minor issues --- src/sched-random.c | 27 ++++++++------ src/sched-stack.c | 27 ++++++++------ src/sched-ws.c | 88 +++++++++++++++++++++++----------------------- 3 files changed, 76 insertions(+), 66 deletions(-) diff --git a/src/sched-random.c b/src/sched-random.c index 47f7e9f..b30ec68 100644 --- a/src/sched-random.c +++ b/src/sched-random.c @@ -14,9 +14,6 @@ struct scheduler { /* Indicateur de changement d'état */ pthread_cond_t cond; - /* Taille de la pile */ - int qlen; - /* Mutex qui protège la structure */ pthread_mutex_t mutex; @@ -26,6 +23,9 @@ struct scheduler { /* Nombre de threads en attente */ int nthsleep; + /* Taille de la pile */ + int qlen; + /* Pile de tâches */ struct task_info *tasks; @@ -33,15 +33,14 @@ struct scheduler { int top; }; -/* Ordonnanceur partagé */ -static struct scheduler sched; - /* Lance une tâche de la pile */ void *sched_worker(void *); int sched_init(int nthreads, int qlen, taskfunc f, void *closure) { + static struct scheduler sched; + if(qlen <= 0) { fprintf(stderr, "qlen must be greater than 0\n"); return -1; @@ -56,6 +55,8 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) } sched.nthreads = nthreads; + sched.nthsleep = 0; + if(pthread_mutex_init(&sched.mutex, NULL) != 0) { fprintf(stderr, "Can't init mutex\n"); return -1; @@ -68,7 +69,7 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) sched.top = -1; if((sched.tasks = malloc(qlen * sizeof(struct task_info))) == NULL) { - fprintf(stderr, "Can't allocate memory for stack\n"); + perror("Stack"); return -1; } @@ -110,6 +111,9 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) free(sched.tasks); + pthread_mutex_destroy(&sched.mutex); + pthread_cond_destroy(&sched.cond); + return 1; } @@ -125,7 +129,8 @@ sched_spawn(taskfunc f, void *closure, struct scheduler *s) 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_mutex_unlock(&s->mutex); @@ -138,6 +143,7 @@ sched_worker(void *arg) { struct scheduler *s = (struct scheduler *)arg; + struct task_info task; while(1) { pthread_mutex_lock(&s->mutex); @@ -166,13 +172,12 @@ sched_worker(void *arg) s->tasks[random_index] = s->tasks[s->top]; s->tasks[s->top] = echange; - taskfunc f = s->tasks[s->top].f; - void *closure = s->tasks[s->top].closure; + task = s->tasks[s->top]; s->top--; pthread_mutex_unlock(&s->mutex); // Exécute la tâche - f(closure, s); + task.f(task.closure, s); } return NULL; diff --git a/src/sched-stack.c b/src/sched-stack.c index 1ce3319..614d6cb 100644 --- a/src/sched-stack.c +++ b/src/sched-stack.c @@ -14,9 +14,6 @@ struct scheduler { /* Indicateur de changement d'état */ pthread_cond_t cond; - /* Taille de la pile */ - int qlen; - /* Mutex qui protège la structure */ pthread_mutex_t mutex; @@ -26,6 +23,9 @@ struct scheduler { /* Nombre de threads en attente */ int nthsleep; + /* Taille de la pile */ + int qlen; + /* Pile de tâches */ struct task_info *tasks; @@ -33,15 +33,14 @@ struct scheduler { int top; }; -/* Ordonnanceur partagé */ -static struct scheduler sched; - /* Lance une tâche de la pile */ void *sched_worker(void *); int sched_init(int nthreads, int qlen, taskfunc f, void *closure) { + static struct scheduler sched; + if(qlen <= 0) { fprintf(stderr, "qlen must be greater than 0\n"); return -1; @@ -56,6 +55,8 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) } sched.nthreads = nthreads; + sched.nthsleep = 0; + if(pthread_mutex_init(&sched.mutex, NULL) != 0) { fprintf(stderr, "Can't init mutex\n"); return -1; @@ -68,7 +69,7 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) sched.top = -1; if((sched.tasks = malloc(qlen * sizeof(struct task_info))) == NULL) { - fprintf(stderr, "Can't allocate memory for stack\n"); + perror("Stack"); return -1; } @@ -107,6 +108,9 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) free(sched.tasks); + pthread_mutex_destroy(&sched.mutex); + pthread_cond_destroy(&sched.cond); + return 1; } @@ -122,7 +126,8 @@ sched_spawn(taskfunc f, void *closure, struct scheduler *s) 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_mutex_unlock(&s->mutex); @@ -135,6 +140,7 @@ sched_worker(void *arg) { struct scheduler *s = (struct scheduler *)arg; + struct task_info task; while(1) { pthread_mutex_lock(&s->mutex); @@ -157,13 +163,12 @@ sched_worker(void *arg) } // Extrait la tâche de la pile - taskfunc f = s->tasks[s->top].f; - void *closure = s->tasks[s->top].closure; + task = s->tasks[s->top]; s->top--; pthread_mutex_unlock(&s->mutex); // Exécute la tâche - f(closure, s); + task.f(task.closure, s); } return NULL; diff --git a/src/sched-ws.c b/src/sched-ws.c index 6bc5c84..d6e6fca 100644 --- a/src/sched-ws.c +++ b/src/sched-ws.c @@ -12,8 +12,8 @@ struct task_info { }; struct scheduler { - /* Taille des piles */ - int qlen; + /* Dernier élément du deck (premier ajouter) */ + int *bottom; /* Variable de conditions pour reveillé les threads au besoin */ pthread_cond_t cond; @@ -24,29 +24,27 @@ struct scheduler { /* Nombre de threads instanciés */ int nthreads; + /* Compteur des threads dormants */ + int nthsleep; + + /* Taille des piles */ + int qlen; + /* Piles de tâches */ struct task_info **tasks; /* Liste des threads */ pthread_t *threads; - /* Compteur des threads dormants */ - int nthsleep; - - /* Deck permettant de récupérer aussi bien le premier élément ajouté - * que le dernier */ + /* Premier élément du deck (dernier ajouter) */ int *top; - int *bottom; }; -/* Ordonnanceur partagé */ -static struct scheduler sched; - /* Lance une tâche de la pile */ void *sched_worker(void *); /* 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 */ int current_thread(struct scheduler *); @@ -54,6 +52,8 @@ int current_thread(struct scheduler *); int sched_init(int nthreads, int qlen, taskfunc f, void *closure) { + static struct scheduler sched; + sched.tasks = NULL; sched.threads = NULL; sched.top = NULL; @@ -78,23 +78,23 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) // Initialisation du mutex if(pthread_mutex_init(&sched.mutex, NULL) != 0) { fprintf(stderr, "Can't init mutex\n"); - return sched_init_cleanup(-1); + return sched_init_cleanup(sched, -1); } // Initialisation variable de condition if(pthread_cond_init(&sched.cond, NULL) != 0) { 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 if(!(sched.top = malloc(nthreads * sizeof(int)))) { perror("Cursor top stack"); - return sched_init_cleanup(-1); + return sched_init_cleanup(sched, -1); } if(!(sched.bottom = malloc(nthreads * sizeof(int)))) { perror("Cursor bottom stack"); - return sched_init_cleanup(-1); + return sched_init_cleanup(sched, -1); } for(int i = 0; i < nthreads; ++i) { 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 if(!(sched.tasks = malloc(nthreads * sizeof(struct task_info *)))) { - perror("Stack list"); - return sched_init_cleanup(-1); + perror("Deck list"); + return sched_init_cleanup(sched, -1); } for(int i = 0; i < nthreads; ++i) { if(!(sched.tasks[i] = malloc(qlen * sizeof(struct task_info)))) { - fprintf(stderr, "Stack for thread %d: %s\n", i, strerror(errno)); - return sched_init_cleanup(-1); + fprintf(stderr, "Deck for thread %d: %s\n", i, strerror(errno)); + return sched_init_cleanup(sched, -1); } } @@ -119,13 +119,13 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) // Créer les threads if(!(sched.threads = malloc(nthreads * sizeof(pthread_t)))) { perror("Threads"); - return sched_init_cleanup(-1); + return sched_init_cleanup(sched, -1); } // Ajoute la tâche initiale if(sched_spawn(f, closure, &sched) < 0) { fprintf(stderr, "Can't create the initial task\n"); - return sched_init_cleanup(-1); + return sched_init_cleanup(sched, -1); } // Démarre les threads @@ -144,7 +144,7 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure) fprintf(stderr, "\n"); } - return sched_init_cleanup(-1); + return sched_init_cleanup(sched, -1); } 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) { if((pthread_join(sched.threads[i], NULL) != 0)) { 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 -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) { - for(int i = 0; i < sched.nthreads; ++i) { - if(sched.tasks[i]) { - free(sched.tasks[i]); - sched.tasks[i] = NULL; + if(s.tasks) { + for(int i = 0; i < s.nthreads; ++i) { + if(s.tasks[i]) { + free(s.tasks[i]); + s.tasks[i] = NULL; } } - free(sched.tasks); - sched.tasks = NULL; + free(s.tasks); + s.tasks = NULL; } - if(sched.threads) { - free(sched.threads); - sched.threads = NULL; + if(s.threads) { + free(s.threads); + s.threads = NULL; } - if(sched.top) { - free(sched.top); - sched.top = NULL; + if(s.top) { + free(s.top); + s.top = NULL; } - if(sched.bottom) { - free(sched.bottom); - sched.bottom = NULL; + if(s.bottom) { + free(s.bottom); + s.bottom = NULL; } return ret_code;