Skip to content

Commit

Permalink
LibC: Remove a bunch of weak pthread_* symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
timschumi authored and bgianfo committed Jul 20, 2022
1 parent cf4b7e3 commit 224ac1a
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 137 deletions.
11 changes: 0 additions & 11 deletions Userland/Libraries/LibC/bits/pthread_integration.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,10 @@ void __pthread_fork_atfork_register_prepare(void (*)(void));
void __pthread_fork_atfork_register_parent(void (*)(void));
void __pthread_fork_atfork_register_child(void (*)(void));

int __pthread_mutex_init(pthread_mutex_t*, pthread_mutexattr_t const*);
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*);

int __pthread_key_create(pthread_key_t*, KeyDestructor);
int __pthread_key_delete(pthread_key_t);
void* __pthread_getspecific(pthread_key_t);
int __pthread_setspecific(pthread_key_t, void const*);

int __pthread_self(void);

void __pthread_key_destroy_for_current_thread(void);

#define __PTHREAD_MUTEX_NORMAL 0
Expand Down
3 changes: 2 additions & 1 deletion Userland/Libraries/LibC/bits/stdio_file_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <LibC/bits/FILE.h>
#include <LibC/bits/pthread_integration.h>
#include <LibC/bits/wchar.h>
#include <pthread.h>
#include <sys/types.h>

#pragma once
Expand All @@ -21,7 +22,7 @@ struct FILE {
, m_mode(mode)
{
pthread_mutexattr_t attr = { __PTHREAD_MUTEX_RECURSIVE };
__pthread_mutex_init(&m_mutex, &attr);
pthread_mutex_init(&m_mutex, &attr);
}
~FILE();

Expand Down
17 changes: 9 additions & 8 deletions Userland/Libraries/LibC/cxxabi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <LibC/bits/pthread_integration.h>
#include <LibC/mallocdefs.h>
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -80,13 +81,13 @@ void __begin_atexit_locking()

int __cxa_atexit(AtExitFunction exit_function, void* parameter, void* dso_handle)
{
__pthread_mutex_lock(&atexit_mutex);
pthread_mutex_lock(&atexit_mutex);

// allocate initial atexit region
if (!atexit_entries) {
atexit_entries = (AtExitEntry*)mmap(nullptr, atexit_region_bytes(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
if (atexit_entries == MAP_FAILED) {
__pthread_mutex_unlock(&atexit_mutex);
pthread_mutex_unlock(&atexit_mutex);
perror("__cxa_atexit mmap");
_exit(1);
}
Expand All @@ -100,7 +101,7 @@ int __cxa_atexit(AtExitFunction exit_function, void* parameter, void* dso_handle

auto* new_atexit_entries = (AtExitEntry*)mmap(nullptr, new_atexit_region_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
if (new_atexit_entries == MAP_FAILED) {
__pthread_mutex_unlock(&atexit_mutex);
pthread_mutex_unlock(&atexit_mutex);
perror("__cxa_atexit mmap (new size)");
return -1;
}
Expand All @@ -118,7 +119,7 @@ int __cxa_atexit(AtExitFunction exit_function, void* parameter, void* dso_handle
atexit_entries[atexit_entry_count++] = { exit_function, parameter, dso_handle };
lock_atexit_handlers();

__pthread_mutex_unlock(&atexit_mutex);
pthread_mutex_unlock(&atexit_mutex);

return 0;
}
Expand All @@ -132,7 +133,7 @@ void __cxa_finalize(void* dso_handle)
// Multiple calls to __cxa_finalize shall not result in calling termination function entries multiple times;
// the implementation may either remove entries or mark them finished.

__pthread_mutex_lock(&atexit_mutex);
pthread_mutex_lock(&atexit_mutex);

if (atexit_entry_count > atexit_called_entries->size())
atexit_called_entries->grow(atexit_entry_count, false);
Expand All @@ -147,13 +148,13 @@ void __cxa_finalize(void* dso_handle)
if (needs_calling) {
dbgln_if(GLOBAL_DTORS_DEBUG, "__cxa_finalize: calling entry[{}] {:p}({:p}) dso: {:p}", entry_index, exit_entry.method, exit_entry.parameter, exit_entry.dso_handle);
atexit_called_entries->set(entry_index, true);
__pthread_mutex_unlock(&atexit_mutex);
pthread_mutex_unlock(&atexit_mutex);
exit_entry.method(exit_entry.parameter);
__pthread_mutex_lock(&atexit_mutex);
pthread_mutex_lock(&atexit_mutex);
}
}

__pthread_mutex_unlock(&atexit_mutex);
pthread_mutex_unlock(&atexit_mutex);
}

__attribute__((noreturn)) void __cxa_pure_virtual()
Expand Down
54 changes: 0 additions & 54 deletions Userland/Libraries/LibC/pthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ static int create_thread(pthread_t* thread, void* (*entry)(void*), void* argumen
__RETURN_PTHREAD_ERROR(rc);
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_self.html
int pthread_self()
{
return __pthread_self();
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html
int pthread_create(pthread_t* thread, pthread_attr_t* attributes, void* (*start_routine)(void*), void* argument_to_start_routine)
{
Expand Down Expand Up @@ -206,36 +200,12 @@ int pthread_sigmask(int how, sigset_t const* set, sigset_t* old_set)
return 0;
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_init.html
int pthread_mutex_init(pthread_mutex_t* mutex, pthread_mutexattr_t const* attributes)
{
return __pthread_mutex_init(mutex, attributes);
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_destroy.html
int pthread_mutex_destroy(pthread_mutex_t*)
{
return 0;
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_lock.html
int pthread_mutex_lock(pthread_mutex_t* mutex)
{
return __pthread_mutex_lock(mutex);
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_trylock.html
int pthread_mutex_trylock(pthread_mutex_t* mutex)
{
return __pthread_mutex_trylock(mutex);
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_unlock.html
int pthread_mutex_unlock(pthread_mutex_t* mutex)
{
return __pthread_mutex_unlock(mutex);
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutexattr_init.html
int pthread_mutexattr_init(pthread_mutexattr_t* attr)
{
Expand Down Expand Up @@ -518,30 +488,6 @@ int pthread_cancel(pthread_t)
TODO();
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html
int pthread_key_create(pthread_key_t* key, KeyDestructor destructor)
{
return __pthread_key_create(key, destructor);
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_delete.html
int pthread_key_delete(pthread_key_t key)
{
return __pthread_key_delete(key);
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_getspecific.html
void* pthread_getspecific(pthread_key_t key)
{
return __pthread_getspecific(key);
}

// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_setspecific.html
int pthread_setspecific(pthread_key_t key, void const* value)
{
return __pthread_setspecific(key, value);
}

int pthread_setname_np(pthread_t thread, char const* name)
{
if (!name)
Expand Down
62 changes: 29 additions & 33 deletions Userland/Libraries/LibC/pthread_integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -31,73 +32,73 @@ 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)
{
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)
{
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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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--;
Expand All @@ -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")));
}
Loading

0 comments on commit 224ac1a

Please sign in to comment.