allocate multiples stacks

This commit is contained in:
Mylloon 2024-04-21 15:00:45 +02:00
parent bfeb0df269
commit 7d064ecf5f
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -3,6 +3,7 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
struct task_info { struct task_info {
void *closure; void *closure;
@ -26,7 +27,7 @@ struct scheduler {
int nthsleep; int nthsleep;
/* Pile de tâches */ /* Pile de tâches */
struct task_info *tasks; struct task_info **tasks;
/* Position actuelle dans la pile */ /* Position actuelle dans la pile */
int *top; int *top;
@ -45,7 +46,9 @@ int
sched_init(int nthreads, int qlen, taskfunc f, void *closure) sched_init(int nthreads, int qlen, taskfunc f, void *closure)
{ {
sched.cond = NULL; sched.cond = NULL;
sched.mutex = NULL;
sched.tasks = NULL; sched.tasks = NULL;
sched.top = NULL;
if(qlen <= 0) { if(qlen <= 0) {
fprintf(stderr, "qlen must be greater than 0\n"); fprintf(stderr, "qlen must be greater than 0\n");
@ -96,10 +99,17 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
sched.top[i] = -1; sched.top[i] = -1;
} }
if((sched.tasks = malloc(qlen * sizeof(struct task_info))) == NULL) { // Allocation mémoire pour la pile de chaque processus
perror("Stack"); if(!(sched.tasks = malloc(sched.nthreads * sizeof(struct task_info *)))) {
perror("Stack list");
return sched_init_cleanup(-1); return sched_init_cleanup(-1);
} }
for(int i = 0; i < sched.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);
}
}
pthread_t threads[nthreads]; pthread_t threads[nthreads];
for(int i = 0; i < nthreads; ++i) { for(int i = 0; i < nthreads; ++i) {
@ -154,6 +164,12 @@ sched_init_cleanup(int ret_code)
} }
if(sched.tasks) { if(sched.tasks) {
for(int i = 0; i < sched.nthreads; ++i) {
if(sched.tasks[i]) {
free(sched.tasks[i]);
sched.tasks[i] = NULL;
}
}
free(sched.tasks); free(sched.tasks);
sched.tasks = NULL; sched.tasks = NULL;
@ -179,7 +195,7 @@ sched_spawn(taskfunc f, void *closure, struct scheduler *s)
return -1; return -1;
} }
s->tasks[++s->top[0]] = (struct task_info){closure, f}; s->tasks[0][++s->top[0]] = (struct task_info){closure, f};
pthread_cond_signal(&s->cond[0]); pthread_cond_signal(&s->cond[0]);
pthread_mutex_unlock(&s->mutex[0]); pthread_mutex_unlock(&s->mutex[0]);
@ -214,8 +230,8 @@ sched_worker(void *arg)
} }
// Extrait la tâche de la pile // Extrait la tâche de la pile
taskfunc f = s->tasks[s->top[0]].f; taskfunc f = s->tasks[0][s->top[0]].f;
void *closure = s->tasks[s->top[0]].closure; void *closure = s->tasks[0][s->top[0]].closure;
s->top[0]--; s->top[0]--;
pthread_mutex_unlock(&s->mutex[0]); pthread_mutex_unlock(&s->mutex[0]);