Skip to content

Commit

Permalink
libphoenix: fix locking in pthread_create
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdebek committed Oct 18, 2023
1 parent 367c8ee commit 95e4b20
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pthread/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,20 +260,23 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
ctx->cancelstate = PTHREAD_CANCEL_ENABLE;
*thread = (pthread_t)ctx;

mutexLock(pthread_common.pthread_list_lock);

int err = beginthreadex(start_point, attrs->priority, stack,
attrs->stacksize, (void *)ctx, &ctx->id);

if (err != 0) {
munmap(stack, attrs->stacksize);
pthread_ctx_put(ctx);
_pthread_ctx_put(ctx);
thread = NULL;
}
else if (ctx->detached == PTHREAD_CREATE_JOINABLE) {
mutexLock(pthread_common.pthread_list_lock);
if (pthread_common.pthread_list != NULL && pthread_common.pthread_list->id == 0)
pthread_common.pthread_list = NULL;

LIST_ADD(&pthread_common.pthread_list, ctx);
else {
if (ctx->detached == PTHREAD_CREATE_JOINABLE) {
if (pthread_common.pthread_list != NULL && pthread_common.pthread_list->id == 0) {
pthread_common.pthread_list = NULL;
}
LIST_ADD(&pthread_common.pthread_list, ctx);
}
mutexUnlock(pthread_common.pthread_list_lock);
}

Expand Down

0 comments on commit 95e4b20

Please sign in to comment.