top!finded the bug
This commit is contained in:
parent
4481d3d25d
commit
bfeb0df269
1 changed files with 20 additions and 8 deletions
28
src/sched.c
28
src/sched.c
|
@ -29,7 +29,7 @@ struct scheduler {
|
||||||
struct task_info *tasks;
|
struct task_info *tasks;
|
||||||
|
|
||||||
/* Position actuelle dans la pile */
|
/* Position actuelle dans la pile */
|
||||||
int top;
|
int *top;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Ordonnanceur partagé */
|
/* Ordonnanceur partagé */
|
||||||
|
@ -87,7 +87,14 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sched.top = -1;
|
// Initialisation du curseur suivant l'état de la pile de chaque processus
|
||||||
|
if(!(sched.top = malloc(sched.nthreads * sizeof(int)))) {
|
||||||
|
perror("Cursor top stack\n");
|
||||||
|
return sched_init_cleanup(-1);
|
||||||
|
}
|
||||||
|
for(int i = 0; i < sched.nthreads; ++i) {
|
||||||
|
sched.top[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
if((sched.tasks = malloc(qlen * sizeof(struct task_info))) == NULL) {
|
if((sched.tasks = malloc(qlen * sizeof(struct task_info))) == NULL) {
|
||||||
perror("Stack");
|
perror("Stack");
|
||||||
|
@ -152,6 +159,11 @@ sched_init_cleanup(int ret_code)
|
||||||
sched.tasks = NULL;
|
sched.tasks = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(sched.top) {
|
||||||
|
free(sched.top);
|
||||||
|
sched.top = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return ret_code;
|
return ret_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,14 +172,14 @@ sched_spawn(taskfunc f, void *closure, struct scheduler *s)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&s->mutex[0]);
|
pthread_mutex_lock(&s->mutex[0]);
|
||||||
|
|
||||||
if(s->top + 1 >= s->qlen) {
|
if(s->top[0] + 1 >= s->qlen) {
|
||||||
pthread_mutex_unlock(&s->mutex[0]);
|
pthread_mutex_unlock(&s->mutex[0]);
|
||||||
errno = EAGAIN;
|
errno = EAGAIN;
|
||||||
fprintf(stderr, "Stack is full\n");
|
fprintf(stderr, "Stack is full\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->tasks[++s->top] = (struct task_info){closure, f};
|
s->tasks[++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]);
|
||||||
|
@ -184,7 +196,7 @@ sched_worker(void *arg)
|
||||||
pthread_mutex_lock(&s->mutex[0]);
|
pthread_mutex_lock(&s->mutex[0]);
|
||||||
|
|
||||||
// S'il on a rien à faire
|
// S'il on a rien à faire
|
||||||
if(s->top == -1) {
|
if(s->top[0] == -1) {
|
||||||
s->nthsleep++;
|
s->nthsleep++;
|
||||||
if(s->nthsleep == s->nthreads) {
|
if(s->nthsleep == s->nthreads) {
|
||||||
// Signal a tout les threads que il n'y a plus rien à faire
|
// Signal a tout les threads que il n'y a plus rien à faire
|
||||||
|
@ -202,9 +214,9 @@ sched_worker(void *arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extrait la tâche de la pile
|
// Extrait la tâche de la pile
|
||||||
taskfunc f = s->tasks[s->top].f;
|
taskfunc f = s->tasks[s->top[0]].f;
|
||||||
void *closure = s->tasks[s->top].closure;
|
void *closure = s->tasks[s->top[0]].closure;
|
||||||
s->top--;
|
s->top[0]--;
|
||||||
pthread_mutex_unlock(&s->mutex[0]);
|
pthread_mutex_unlock(&s->mutex[0]);
|
||||||
|
|
||||||
// Exécute la tâche
|
// Exécute la tâche
|
||||||
|
|
Reference in a new issue