From 5536f3c27768111cb3d31adc17ed6320178b36b4 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Mon, 5 Jul 2021 14:55:57 +0300 Subject: [PATCH] LibC: Add __pthread_mutex_lock_pessimistic_np() This is a private function that locks the lock much like the regular pthread_mutex_lock(), but causes the corresponding unlock operation to always assume there may be other waiters. This is useful in case some waiters are made to wait on the mutex's futex directly, without going through pthread_mutex_lock(). This is going to be used by the condition variable implementation in the next commit. --- .../Libraries/LibC/bits/pthread_integration.h | 1 + Userland/Libraries/LibC/pthread_integration.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Userland/Libraries/LibC/bits/pthread_integration.h b/Userland/Libraries/LibC/bits/pthread_integration.h index b74462afd2d..1d37ee2d441 100644 --- a/Userland/Libraries/LibC/bits/pthread_integration.h +++ b/Userland/Libraries/LibC/bits/pthread_integration.h @@ -21,6 +21,7 @@ void __pthread_fork_atfork_register_child(void (*)(void)); int __pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*); int __pthread_mutex_lock(pthread_mutex_t*); int __pthread_mutex_trylock(pthread_mutex_t*); +int __pthread_mutex_lock_pessimistic_np(pthread_mutex_t*); int __pthread_mutex_unlock(pthread_mutex_t*); typedef void (*KeyDestructor)(void*); diff --git a/Userland/Libraries/LibC/pthread_integration.cpp b/Userland/Libraries/LibC/pthread_integration.cpp index ebe7b232cad..e2f10cdda0e 100644 --- a/Userland/Libraries/LibC/pthread_integration.cpp +++ b/Userland/Libraries/LibC/pthread_integration.cpp @@ -167,6 +167,22 @@ int __pthread_mutex_lock(pthread_mutex_t* mutex) int pthread_mutex_lock(pthread_mutex_t*) __attribute__((weak, alias("__pthread_mutex_lock"))); +int __pthread_mutex_lock_pessimistic_np(pthread_mutex_t* mutex) +{ + // Same as pthread_mutex_lock(), but always set MUTEX_LOCKED_NEED_TO_WAKE, + // and also don't bother checking for already owning the mutex recursively, + // because we know we don't. Used in the condition variable implementation. + u32 value = AK::atomic_exchange(&mutex->lock, MUTEX_LOCKED_NEED_TO_WAKE, AK::memory_order_acquire); + while (value != MUTEX_UNLOCKED) { + futex_wait(&mutex->lock, value, nullptr, 0); + value = AK::atomic_exchange(&mutex->lock, MUTEX_LOCKED_NEED_TO_WAKE, AK::memory_order_acquire); + } + + AK::atomic_store(&mutex->owner, __pthread_self(), AK::memory_order_relaxed); + mutex->level = 0; + return 0; +} + int __pthread_mutex_unlock(pthread_mutex_t* mutex) { if (mutex->type == __PTHREAD_MUTEX_RECURSIVE && mutex->level > 0) {