fix thread never found, reducing performance

This commit is contained in:
Mylloon 2024-04-23 10:55:03 +02:00
parent 2674883a24
commit f42777db6c
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -146,7 +146,10 @@ sched_init(int nthreads, int qlen, taskfunc f, void *closure)
return sched_init_cleanup(-1); return sched_init_cleanup(-1);
} }
pthread_mutex_lock(&sched.mutex);
sched.nthreads++; sched.nthreads++;
pthread_mutex_unlock(&sched.mutex);
} }
for(int i = 0; i < nthreads; ++i) { for(int i = 0; i < nthreads; ++i) {
@ -200,11 +203,15 @@ int
current_thread(struct scheduler *s) current_thread(struct scheduler *s)
{ {
pthread_t current = pthread_self(); pthread_t current = pthread_self();
pthread_mutex_lock(&s->mutex);
for(int i = 0; i < s->nthreads; i++) { for(int i = 0; i < s->nthreads; i++) {
if(pthread_equal(s->threads[i], current)) { if(pthread_equal(s->threads[i], current)) {
pthread_mutex_unlock(&s->mutex);
return i; return i;
} }
} }
pthread_mutex_unlock(&s->mutex);
return -1; return -1;
} }