This repository has been archived on 2024-05-05. You can view files and clone it, but cannot push or open issues or pull requests.
work-stealing-scheduler/src/sched.c

116 lines
2.5 KiB
C
Raw Normal View History

#include "../includes/sched.h"
2024-03-09 18:47:40 +01:00
2024-04-18 17:41:41 +02:00
#include <errno.h>
2024-03-15 12:43:44 +01:00
#include <stdio.h>
2024-04-18 17:41:41 +02:00
static struct scheduler sched;
/* Lance une tâche de la pile */
void *worker_routine(void *arg) {
struct scheduler *s = (struct scheduler *)arg;
while (1) {
pthread_mutex_lock(&s->mutex);
2024-04-19 13:48:50 +02:00
if (s->exit == 1) {
2024-04-19 12:57:21 +02:00
pthread_mutex_unlock(&s->mutex);
2024-04-19 13:48:50 +02:00
printf("finalement..rien a faire, ciao\n");
2024-04-19 12:57:21 +02:00
break;
}
// S'il on a rien à faire
2024-04-18 17:41:41 +02:00
if (s->top == -1) {
2024-04-19 13:48:50 +02:00
// printf("attend..\n");
pthread_cond_wait(&s->cond, &s->mutex);
2024-04-18 17:41:41 +02:00
pthread_mutex_unlock(&s->mutex);
2024-04-19 13:48:50 +02:00
// printf("reveillé!\n");
continue;
2024-04-18 17:41:41 +02:00
}
2024-04-19 13:48:50 +02:00
// printf("lancement tâche #%d\n", s->top);
2024-04-18 17:41:41 +02:00
// Extrait la tâche de la pile
taskfunc f = s->tasks[s->top].f;
void *closure = s->tasks[s->top].closure;
2024-04-18 17:41:41 +02:00
s->top--;
pthread_mutex_unlock(&s->mutex);
// Exécute la tâche
f(closure, s);
2024-04-19 13:48:50 +02:00
// Signale s'il n'y a plus rien à faire
if (s->top == -1) {
printf("va falloir partir\n");
pthread_mutex_lock(&s->mutex);
s->exit = 1;
pthread_cond_broadcast(&s->cond);
pthread_mutex_unlock(&s->mutex);
}
2024-04-18 17:41:41 +02:00
}
return NULL;
2024-03-09 18:47:40 +01:00
}
2024-04-18 17:41:41 +02:00
int sched_init(int nthreads, int qlen, taskfunc f, void *closure) {
if (nthreads == 0) {
nthreads = sched_default_threads();
}
// TODO : Actuellement on n'utilises pas qlen
2024-04-18 17:41:41 +02:00
// => On utilise une pile de taille fixe
(void)qlen;
sched.top = -1;
2024-04-19 13:48:50 +02:00
sched.exit = 0;
2024-03-15 12:43:44 +01:00
2024-04-18 17:41:41 +02:00
if (pthread_mutex_init(&sched.mutex, NULL) != 0) {
fprintf(stderr, "Can't init mutex\n");
2024-03-15 12:43:44 +01:00
return -1;
}
2024-04-18 17:41:41 +02:00
if (pthread_cond_init(&sched.cond, NULL) != 0) {
fprintf(stderr, "Can't init condition variable\n");
2024-03-15 12:43:44 +01:00
return -1;
}
2024-04-18 17:41:41 +02:00
pthread_t threads[nthreads];
for (int i = 0; i < nthreads; ++i) {
if (pthread_create(&threads[i], NULL, worker_routine, &sched) != 0) {
fprintf(stderr, "Can't create threads\n");
return -1;
}
}
if (sched_spawn(f, closure, &sched) != 0) {
fprintf(stderr, "Can't create the initial task\n");
2024-04-18 17:41:41 +02:00
return -1;
}
for (int i = 0; i < nthreads; ++i) {
if ((pthread_join(threads[i], NULL) != 0)) {
fprintf(stderr, "Can't wait the thread %d\n", i);
return -1;
}
}
return 1;
}
int sched_spawn(taskfunc f, void *closure, struct scheduler *s) {
pthread_mutex_lock(&s->mutex);
if (s->top + 1 >= MAX_TASKS) {
pthread_mutex_unlock(&s->mutex);
errno = EAGAIN;
fprintf(stderr, "Stack is full\n");
2024-04-18 17:41:41 +02:00
return -1;
}
s->tasks[++s->top] = (taskinfo){f, closure};
2024-04-18 17:41:41 +02:00
pthread_cond_signal(&s->cond);
2024-04-19 12:57:21 +02:00
pthread_mutex_unlock(&s->mutex);
2024-04-18 17:41:41 +02:00
2024-03-15 12:43:44 +01:00
return 0;
2024-03-09 18:47:40 +01:00
}