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 */
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;

View file

@ -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;

View file

@ -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;