Skip to content

Commit

Permalink
pthread_once_control: replace wait queue with list head
Browse files Browse the repository at this point in the history
  • Loading branch information
shengwen-tw committed Nov 28, 2023
1 parent fdd4b4d commit 4acbf6a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/kernel/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct thread_attr {
};

struct thread_once {
struct list_head wq;
struct list_head wait_list;
bool finished;
};

Expand Down
10 changes: 6 additions & 4 deletions kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ static void pthread_once_cleanup_handler(void)
/* Wake up all waiting threads and mark once variable as complete */
struct thread_once *once_control = running_thread->once_control;
once_control->finished = true;
wake_up_all(&once_control->wq);
wake_up_all(&once_control->wait_list);
}

static int sys_pthread_once(pthread_once_t *_once_control,
Expand All @@ -2174,12 +2174,14 @@ static int sys_pthread_once(pthread_once_t *_once_control,
if (once_control->finished)
return 0;

if (once_control->wq.next == NULL || once_control->wq.prev == NULL) {
if (once_control->wait_list.next == NULL ||
once_control->wait_list.prev == NULL) {
/* The first time to execute pthread_once() */
init_waitqueue_head(&once_control->wq);
INIT_LIST_HEAD(&once_control->wait_list);
} else {
/* pthread_once() is already called */
prepare_to_wait(&once_control->wq, &running_thread->list, THREAD_WAIT);
prepare_to_wait(&once_control->wait_list, &running_thread->list,
THREAD_WAIT);
return 0;
}

Expand Down

0 comments on commit 4acbf6a

Please sign in to comment.