increase max tasks and create a special structure who hold task_info - also fix an infinite loop (introducing a new one ahah)
This commit is contained in:
parent
d566f26d90
commit
8afda87953
2 changed files with 25 additions and 18 deletions
|
@ -3,25 +3,29 @@
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define MAX_TASKS 1024
|
#define MAX_TASKS 81920
|
||||||
|
|
||||||
struct scheduler;
|
struct scheduler;
|
||||||
|
|
||||||
typedef void (*taskfunc)(void *, struct scheduler *);
|
typedef void (*taskfunc)(void *, struct scheduler *);
|
||||||
|
|
||||||
|
typedef struct task_info {
|
||||||
|
taskfunc f;
|
||||||
|
void *closure;
|
||||||
|
} taskinfo;
|
||||||
|
|
||||||
struct scheduler {
|
struct scheduler {
|
||||||
/* Mutex qui protège la pile */
|
/* Mutex qui protège la structure */
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
|
||||||
/* Indicateur de changement d'état de la pile */
|
/* Indicateur de changement d'état */
|
||||||
pthread_cond_t cond;
|
pthread_cond_t cond;
|
||||||
|
|
||||||
/* Position actuelle dans la pile */
|
/* Position actuelle dans la pile */
|
||||||
int top;
|
int top;
|
||||||
|
|
||||||
/* Tâches */
|
/* Tâches */
|
||||||
taskfunc tasks[MAX_TASKS];
|
taskinfo tasks[MAX_TASKS];
|
||||||
void *closures[MAX_TASKS];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline int sched_default_threads(void) {
|
static inline int sched_default_threads(void) {
|
||||||
|
|
29
src/sched.c
29
src/sched.c
|
@ -10,27 +10,30 @@ void *worker_routine(void *arg) {
|
||||||
struct scheduler *s = (struct scheduler *)arg;
|
struct scheduler *s = (struct scheduler *)arg;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// Attente d'un changement d'état
|
|
||||||
pthread_cond_wait(&s->cond, &s->mutex);
|
|
||||||
|
|
||||||
pthread_mutex_lock(&s->mutex);
|
pthread_mutex_lock(&s->mutex);
|
||||||
|
|
||||||
|
// S'il n'y a plus de tâches à exécuter
|
||||||
if (s->top == -1) {
|
if (s->top == -1) {
|
||||||
// Il n'y a plus de tâches à exécuter
|
// printf("rien a faire, on attend\n");
|
||||||
|
pthread_cond_wait(&s->cond, &s->mutex);
|
||||||
pthread_mutex_unlock(&s->mutex);
|
pthread_mutex_unlock(&s->mutex);
|
||||||
break;
|
continue;
|
||||||
}
|
}
|
||||||
|
// printf("on a un truc a faire!! (top: %d)\n", s->top);
|
||||||
|
|
||||||
// Extrait la tâche de la pile
|
// Extrait la tâche de la pile
|
||||||
taskfunc f = s->tasks[s->top];
|
taskfunc f = s->tasks[s->top].f;
|
||||||
void *closure = s->closures[s->top];
|
void *closure = s->tasks[s->top].closure;
|
||||||
s->top--;
|
s->top--;
|
||||||
|
|
||||||
pthread_mutex_unlock(&s->mutex);
|
pthread_mutex_unlock(&s->mutex);
|
||||||
|
|
||||||
// Exécute la tâche
|
// Exécute la tâche
|
||||||
|
// printf("lance la tache\n");
|
||||||
f(closure, s);
|
f(closure, s);
|
||||||
|
|
||||||
// Signale que la tâche est terminée
|
// Signale que la tâche est terminée
|
||||||
|
// printf("tache terminée\n");
|
||||||
pthread_cond_signal(&s->cond);
|
pthread_cond_signal(&s->cond);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +45,7 @@ int sched_init(int nthreads, int qlen, taskfunc f, void *closure) {
|
||||||
nthreads = sched_default_threads();
|
nthreads = sched_default_threads();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actuellement on n'utilises pas qlen
|
// TODO : Actuellement on n'utilises pas qlen
|
||||||
// => On utilise une pile de taille fixe
|
// => On utilise une pile de taille fixe
|
||||||
(void)qlen;
|
(void)qlen;
|
||||||
|
|
||||||
|
@ -67,7 +70,7 @@ int sched_init(int nthreads, int qlen, taskfunc f, void *closure) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sched_spawn(f, closure, &sched) != 0) {
|
if (sched_spawn(f, closure, &sched) != 0) {
|
||||||
fprintf(stderr, "Can't create a new task\n");
|
fprintf(stderr, "Can't create the initial task\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,20 +85,20 @@ int sched_init(int nthreads, int qlen, taskfunc f, void *closure) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int sched_spawn(taskfunc f, void *closure, struct scheduler *s) {
|
int sched_spawn(taskfunc f, void *closure, struct scheduler *s) {
|
||||||
|
// printf("attend spawn\n");
|
||||||
pthread_mutex_lock(&s->mutex);
|
pthread_mutex_lock(&s->mutex);
|
||||||
|
|
||||||
if (s->top + 1 >= MAX_TASKS) {
|
if (s->top + 1 >= MAX_TASKS) {
|
||||||
pthread_mutex_unlock(&s->mutex);
|
pthread_mutex_unlock(&s->mutex);
|
||||||
errno = EAGAIN;
|
errno = EAGAIN;
|
||||||
fprintf(stderr, "Stack full\n");
|
fprintf(stderr, "Stack is full\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->top++;
|
s->tasks[++s->top] = (taskinfo){f, closure};
|
||||||
s->tasks[s->top] = f;
|
|
||||||
s->closures[s->top] = closure;
|
|
||||||
|
|
||||||
pthread_mutex_unlock(&s->mutex);
|
pthread_mutex_unlock(&s->mutex);
|
||||||
|
|
||||||
pthread_cond_signal(&s->cond);
|
pthread_cond_signal(&s->cond);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Reference in a new issue