1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:04:59 +00:00

LibC: Remove a bunch of weak pthread_* symbols

This commit is contained in:
Tim Schumacher 2022-07-19 17:23:18 +02:00 committed by Brian Gianforcaro
parent cf4b7e343a
commit 224ac1a307
8 changed files with 67 additions and 137 deletions

View file

@ -10,6 +10,7 @@
#include <AK/Vector.h>
#include <bits/pthread_integration.h>
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <serenity.h>
#include <unistd.h>
@ -31,10 +32,10 @@ void __pthread_fork_prepare(void)
if (!g_did_touch_atfork.load())
return;
__pthread_mutex_lock(&g_atfork_list_mutex);
pthread_mutex_lock(&g_atfork_list_mutex);
for (auto entry : g_atfork_prepare_list.get())
entry();
__pthread_mutex_unlock(&g_atfork_list_mutex);
pthread_mutex_unlock(&g_atfork_list_mutex);
}
void __pthread_fork_child(void)
@ -42,10 +43,10 @@ void __pthread_fork_child(void)
if (!g_did_touch_atfork.load())
return;
__pthread_mutex_lock(&g_atfork_list_mutex);
pthread_mutex_lock(&g_atfork_list_mutex);
for (auto entry : g_atfork_child_list.get())
entry();
__pthread_mutex_unlock(&g_atfork_list_mutex);
pthread_mutex_unlock(&g_atfork_list_mutex);
}
void __pthread_fork_parent(void)
@ -53,51 +54,51 @@ void __pthread_fork_parent(void)
if (!g_did_touch_atfork.load())
return;
__pthread_mutex_lock(&g_atfork_list_mutex);
pthread_mutex_lock(&g_atfork_list_mutex);
for (auto entry : g_atfork_parent_list.get())
entry();
__pthread_mutex_unlock(&g_atfork_list_mutex);
pthread_mutex_unlock(&g_atfork_list_mutex);
}
void __pthread_fork_atfork_register_prepare(void (*func)(void))
{
g_did_touch_atfork.store(true);
__pthread_mutex_lock(&g_atfork_list_mutex);
pthread_mutex_lock(&g_atfork_list_mutex);
g_atfork_prepare_list->append(func);
__pthread_mutex_unlock(&g_atfork_list_mutex);
pthread_mutex_unlock(&g_atfork_list_mutex);
}
void __pthread_fork_atfork_register_parent(void (*func)(void))
{
g_did_touch_atfork.store(true);
__pthread_mutex_lock(&g_atfork_list_mutex);
pthread_mutex_lock(&g_atfork_list_mutex);
g_atfork_parent_list->append(func);
__pthread_mutex_unlock(&g_atfork_list_mutex);
pthread_mutex_unlock(&g_atfork_list_mutex);
}
void __pthread_fork_atfork_register_child(void (*func)(void))
{
g_did_touch_atfork.store(true);
__pthread_mutex_lock(&g_atfork_list_mutex);
pthread_mutex_lock(&g_atfork_list_mutex);
g_atfork_child_list->append(func);
__pthread_mutex_unlock(&g_atfork_list_mutex);
pthread_mutex_unlock(&g_atfork_list_mutex);
}
int __pthread_self()
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_self.html
int pthread_self()
{
return gettid();
}
int pthread_self() __attribute__((weak, alias("__pthread_self")));
static constexpr u32 MUTEX_UNLOCKED = 0;
static constexpr u32 MUTEX_LOCKED_NO_NEED_TO_WAKE = 1;
static constexpr u32 MUTEX_LOCKED_NEED_TO_WAKE = 2;
int __pthread_mutex_init(pthread_mutex_t* mutex, pthread_mutexattr_t const* attributes)
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_init.html
int pthread_mutex_init(pthread_mutex_t* mutex, pthread_mutexattr_t const* attributes)
{
mutex->lock = 0;
mutex->owner = 0;
@ -106,21 +107,20 @@ int __pthread_mutex_init(pthread_mutex_t* mutex, pthread_mutexattr_t const* attr
return 0;
}
int pthread_mutex_init(pthread_mutex_t*, pthread_mutexattr_t const*) __attribute__((weak, alias("__pthread_mutex_init")));
int __pthread_mutex_trylock(pthread_mutex_t* mutex)
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_trylock.html
int pthread_mutex_trylock(pthread_mutex_t* mutex)
{
u32 expected = MUTEX_UNLOCKED;
bool exchanged = AK::atomic_compare_exchange_strong(&mutex->lock, expected, MUTEX_LOCKED_NO_NEED_TO_WAKE, AK::memory_order_acquire);
if (exchanged) [[likely]] {
if (mutex->type == __PTHREAD_MUTEX_RECURSIVE)
AK::atomic_store(&mutex->owner, __pthread_self(), AK::memory_order_relaxed);
AK::atomic_store(&mutex->owner, pthread_self(), AK::memory_order_relaxed);
mutex->level = 0;
return 0;
} else if (mutex->type == __PTHREAD_MUTEX_RECURSIVE) {
pthread_t owner = AK::atomic_load(&mutex->owner, AK::memory_order_relaxed);
if (owner == __pthread_self()) {
if (owner == pthread_self()) {
// We already own the mutex!
mutex->level++;
return 0;
@ -129,21 +129,20 @@ int __pthread_mutex_trylock(pthread_mutex_t* mutex)
return EBUSY;
}
int pthread_mutex_trylock(pthread_mutex_t* mutex) __attribute__((weak, alias("__pthread_mutex_trylock")));
int __pthread_mutex_lock(pthread_mutex_t* mutex)
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_lock.html
int pthread_mutex_lock(pthread_mutex_t* mutex)
{
// Fast path: attempt to claim the mutex without waiting.
u32 value = MUTEX_UNLOCKED;
bool exchanged = AK::atomic_compare_exchange_strong(&mutex->lock, value, MUTEX_LOCKED_NO_NEED_TO_WAKE, AK::memory_order_acquire);
if (exchanged) [[likely]] {
if (mutex->type == __PTHREAD_MUTEX_RECURSIVE)
AK::atomic_store(&mutex->owner, __pthread_self(), AK::memory_order_relaxed);
AK::atomic_store(&mutex->owner, pthread_self(), AK::memory_order_relaxed);
mutex->level = 0;
return 0;
} else if (mutex->type == __PTHREAD_MUTEX_RECURSIVE) {
pthread_t owner = AK::atomic_load(&mutex->owner, AK::memory_order_relaxed);
if (owner == __pthread_self()) {
if (owner == pthread_self()) {
// We already own the mutex!
mutex->level++;
return 0;
@ -161,13 +160,11 @@ int __pthread_mutex_lock(pthread_mutex_t* mutex)
}
if (mutex->type == __PTHREAD_MUTEX_RECURSIVE)
AK::atomic_store(&mutex->owner, __pthread_self(), AK::memory_order_relaxed);
AK::atomic_store(&mutex->owner, pthread_self(), AK::memory_order_relaxed);
mutex->level = 0;
return 0;
}
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,
@ -180,12 +177,13 @@ int __pthread_mutex_lock_pessimistic_np(pthread_mutex_t* mutex)
}
if (mutex->type == __PTHREAD_MUTEX_RECURSIVE)
AK::atomic_store(&mutex->owner, __pthread_self(), AK::memory_order_relaxed);
AK::atomic_store(&mutex->owner, pthread_self(), AK::memory_order_relaxed);
mutex->level = 0;
return 0;
}
int __pthread_mutex_unlock(pthread_mutex_t* mutex)
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_unlock.html
int pthread_mutex_unlock(pthread_mutex_t* mutex)
{
if (mutex->type == __PTHREAD_MUTEX_RECURSIVE && mutex->level > 0) {
mutex->level--;
@ -203,6 +201,4 @@ int __pthread_mutex_unlock(pthread_mutex_t* mutex)
return 0;
}
int pthread_mutex_unlock(pthread_mutex_t*) __attribute__((weak, alias("__pthread_mutex_unlock")));
}