From 46e2d665e9c3a46ed34a5cc1dbaa18213f8dcc4b Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 11 Jul 2026 01:34:05 +0300 Subject: [PATCH] LibC: Make pthread_once block with a futex instead of yielding --- .../libraries/LibC/include/bits/types/pthread_types.h | 2 +- userspace/libraries/LibC/pthread.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/userspace/libraries/LibC/include/bits/types/pthread_types.h b/userspace/libraries/LibC/include/bits/types/pthread_types.h index c509dfca..73bff614 100644 --- a/userspace/libraries/LibC/include/bits/types/pthread_types.h +++ b/userspace/libraries/LibC/include/bits/types/pthread_types.h @@ -13,7 +13,7 @@ __BEGIN_DECLS #include -typedef int pthread_once_t; +typedef uint32_t pthread_once_t; typedef pthread_t pthread_spinlock_t; diff --git a/userspace/libraries/LibC/pthread.cpp b/userspace/libraries/LibC/pthread.cpp index d87c8ea7..091934cc 100644 --- a/userspace/libraries/LibC/pthread.cpp +++ b/userspace/libraries/LibC/pthread.cpp @@ -591,10 +591,14 @@ int pthread_once(pthread_once_t* once_control, void (*init_routine)(void)) { init_routine(); BAN::atomic_store(*once_control, 2); + futex(FUTEX_WAKE_PRIVATE, once_control, -1, nullptr); + } + else + { + while (BAN::atomic_load(*once_control) == 1) + futex(FUTEX_WAIT_PRIVATE, once_control, 1, nullptr); } - while (BAN::atomic_load(*once_control) != 2) - sched_yield(); return 0; }