diff --git a/Userland/Libraries/LibC/bits/pthread_integration.h b/Userland/Libraries/LibC/bits/pthread_integration.h index 4327734379..39af9e4d14 100644 --- a/Userland/Libraries/LibC/bits/pthread_integration.h +++ b/Userland/Libraries/LibC/bits/pthread_integration.h @@ -19,6 +19,7 @@ void __pthread_fork_atfork_register_parent(void (*)(void)); void __pthread_fork_atfork_register_child(void (*)(void)); int __pthread_mutex_lock(pthread_mutex_t*); +int __pthread_mutex_trylock(pthread_mutex_t*); int __pthread_mutex_unlock(pthread_mutex_t*); int __pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*); diff --git a/Userland/Libraries/LibC/pthread_integration.cpp b/Userland/Libraries/LibC/pthread_integration.cpp index 8b1ae53298..55677d5b3d 100644 --- a/Userland/Libraries/LibC/pthread_integration.cpp +++ b/Userland/Libraries/LibC/pthread_integration.cpp @@ -125,6 +125,24 @@ int __pthread_mutex_unlock(pthread_mutex_t* mutex) int pthread_mutex_unlock(pthread_mutex_t*) __attribute__((weak, alias("__pthread_mutex_unlock"))); +int __pthread_mutex_trylock(pthread_mutex_t* mutex) +{ + auto& atomic = reinterpret_cast&>(mutex->lock); + u32 expected = false; + if (!atomic.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) { + if (mutex->type == __PTHREAD_MUTEX_RECURSIVE && mutex->owner == pthread_self()) { + mutex->level++; + return 0; + } + return EBUSY; + } + mutex->owner = pthread_self(); + mutex->level = 0; + return 0; +} + +int pthread_mutex_trylock(pthread_mutex_t* mutex) __attribute__((weak, alias("__pthread_mutex_trylock"))); + int __pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attributes) { mutex->lock = 0; diff --git a/Userland/Libraries/LibPthread/pthread.cpp b/Userland/Libraries/LibPthread/pthread.cpp index ad508fd7c5..6e95517df3 100644 --- a/Userland/Libraries/LibPthread/pthread.cpp +++ b/Userland/Libraries/LibPthread/pthread.cpp @@ -167,18 +167,7 @@ int pthread_mutex_lock(pthread_mutex_t* mutex) int pthread_mutex_trylock(pthread_mutex_t* mutex) { - auto& atomic = reinterpret_cast&>(mutex->lock); - u32 expected = false; - if (!atomic.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) { - if (mutex->type == PTHREAD_MUTEX_RECURSIVE && mutex->owner == pthread_self()) { - mutex->level++; - return 0; - } - return EBUSY; - } - mutex->owner = pthread_self(); - mutex->level = 0; - return 0; + return __pthread_mutex_trylock(mutex); } int pthread_mutex_unlock(pthread_mutex_t* mutex)