store threads list into the sched struct

This commit is contained in:
Mylloon 2024-04-21 16:36:35 +02:00
parent 8a2af26283
commit 98193d6500
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -29,6 +29,9 @@ struct scheduler {
/* Piles de tâches */ /* Piles de tâches */
struct task_info **tasks; struct task_info **tasks;
/* Liste des threads */
pthread_t *threads;
/* Positions actuelle dans la pile */ /* Positions actuelle dans la pile */
int *top; int *top;
}; };
@ -51,6 +54,7 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
sched.cond = NULL; sched.cond = NULL;
sched.mutex = NULL; sched.mutex = NULL;
sched.tasks = NULL; sched.tasks = NULL;
sched.threads = NULL;
sched.top = NULL; sched.top = NULL;
if(qlen <= 0) { if(qlen <= 0) {
@ -114,15 +118,18 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
} }
} }
pthread_t threads[nthreads]; if(!(sched.threads = malloc(sched.nthreads * sizeof(pthread_t *)))) {
perror("Threads");
return sched_init_cleanup(-1);
}
for(int i = 0; i < nthreads; ++i) { for(int i = 0; i < nthreads; ++i) {
if(pthread_create(&threads[i], NULL, sched_worker, &sched) != 0) { if(pthread_create(&sched.threads[i], NULL, sched_worker, &sched) != 0) {
fprintf(stderr, "Can't create the thread %d\n", i); fprintf(stderr, "Can't create the thread %d\n", i);
if(i > 0) { if(i > 0) {
fprintf(stderr, ", cancelling already created threads...\n"); fprintf(stderr, ", cancelling already created threads...\n");
for(int j = 0; j < i; ++j) { for(int j = 0; j < i; ++j) {
if(pthread_cancel(threads[j]) != 0) { if(pthread_cancel(sched.threads[j]) != 0) {
fprintf(stderr, "Can't cancel the thread %d\n", j); fprintf(stderr, "Can't cancel the thread %d\n", j);
} }
} }
@ -140,7 +147,7 @@ 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(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(-1);
} }
@ -178,6 +185,11 @@ sched_init_cleanup(int ret_code)
sched.tasks = NULL; sched.tasks = NULL;
} }
if(sched.threads) {
free(sched.threads);
sched.threads = NULL;
}
if(sched.top) { if(sched.top) {
free(sched.top); free(sched.top);
sched.top = NULL; sched.top = NULL;
@ -221,7 +233,7 @@ sched_spawn_core(taskfunc f, void *closure, struct scheduler *s, int core)
void * void *
sched_worker(void *arg) sched_worker(void *arg)
{ {
// TODO: Récupère le processus courand (ID = index tableau schedulers) // TODO: Récupère le processus courant (ID = index tableau schedulers)
int curr_th = 0; int curr_th = 0;
struct scheduler *s = (struct scheduler *)arg; struct scheduler *s = (struct scheduler *)arg;