Jelajahi Sumber

LibPthread: Move the pthread and semaphore implementation to LibC

This additionally adds some compatibility code to redirect linking
attempts for LibPthread to LibC instead.
Tim Schumacher 3 tahun lalu
induk
melakukan
2f3b9c49a5

TEMPAT SAMPAH
Toolchain/Stubs/i686clang/libc.so


TEMPAT SAMPAH
Toolchain/Stubs/i686clang/libpthread.so


TEMPAT SAMPAH
Toolchain/Stubs/x86_64clang/libc.so


TEMPAT SAMPAH
Toolchain/Stubs/x86_64clang/libpthread.so


+ 3 - 0
Userland/DynamicLoader/CMakeLists.txt

@@ -25,6 +25,9 @@ if (ENABLE_UNDEFINED_SANITIZER)
     set(LOADER_SOURCES ${LOADER_SOURCES} ../Libraries/LibSanitizer/UBSanitizer.cpp)
 endif()
 
+# pthread requires thread local storage, which DynamicLoader does not have.
+list(FILTER LIBC_SOURCES1 EXCLUDE REGEX ".*/LibC/pthread\\.cpp")
+
 add_definitions(-D_DYNAMIC_LOADER)
 
 set(SOURCES ${LOADER_SOURCES} ${AK_SOURCES} ${ELF_SOURCES} ${LIBC_SOURCES1} ${LIBC_SOURCES2} ${LIBC_SOURCES3} ${LIBSYSTEM_SOURCES})

+ 4 - 1
Userland/Libraries/LibC/CMakeLists.txt

@@ -27,8 +27,10 @@ set(LIBC_SOURCES
     netdb.cpp
     poll.cpp
     priority.cpp
-    pthread_forward.cpp
+    pthread.cpp
+    pthread_cond.cpp
     pthread_integration.cpp
+    pthread_once.cpp
     pthread_tls.cpp
     pty.cpp
     pwd.cpp
@@ -38,6 +40,7 @@ set(LIBC_SOURCES
     scanf.cpp
     sched.cpp
     search.cpp
+    semaphore.cpp
     serenity.cpp
     shadow.cpp
     signal.cpp

+ 0 - 29
Userland/Libraries/LibC/bits/pthread_forward.h

@@ -1,29 +0,0 @@
-/*
- * Copyright (c) 2021, Gunnar Beutner <gunnar@beutner.name>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <pthread.h>
-
-struct PthreadFunctions {
-    int (*pthread_mutex_trylock)(pthread_mutex_t* mutex);
-    int (*pthread_mutex_destroy)(pthread_mutex_t*);
-
-    int (*pthread_mutexattr_init)(pthread_mutexattr_t*);
-    int (*pthread_mutexattr_settype)(pthread_mutexattr_t*, int);
-    int (*pthread_mutexattr_destroy)(pthread_mutexattr_t*);
-
-    int (*pthread_once)(pthread_once_t*, void (*)(void));
-
-    int (*pthread_cond_broadcast)(pthread_cond_t*);
-    int (*pthread_cond_init)(pthread_cond_t*, pthread_condattr_t const*);
-    int (*pthread_cond_signal)(pthread_cond_t*);
-    int (*pthread_cond_wait)(pthread_cond_t*, pthread_mutex_t*);
-    int (*pthread_cond_destroy)(pthread_cond_t*);
-    int (*pthread_cond_timedwait)(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
-};
-
-void __init_pthread_forward(PthreadFunctions);

+ 0 - 0
Userland/Libraries/LibPthread/pthread.cpp → Userland/Libraries/LibC/pthread.cpp


+ 0 - 0
Userland/Libraries/LibPthread/pthread.h → Userland/Libraries/LibC/pthread.h


+ 0 - 0
Userland/Libraries/LibPthread/pthread_cond.cpp → Userland/Libraries/LibC/pthread_cond.cpp


+ 0 - 87
Userland/Libraries/LibC/pthread_forward.cpp

@@ -1,87 +0,0 @@
-/*
- * Copyright (c) 2021, Gunnar Beutner <gunnar@beutner.name>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <AK/Assertions.h>
-#include <LibC/bits/pthread_forward.h>
-
-static PthreadFunctions s_pthread_functions;
-
-void __init_pthread_forward(PthreadFunctions funcs)
-{
-    s_pthread_functions = funcs;
-}
-
-int pthread_mutex_trylock(pthread_mutex_t* mutex)
-{
-    VERIFY(s_pthread_functions.pthread_mutex_trylock);
-    return s_pthread_functions.pthread_mutex_trylock(mutex);
-}
-
-int pthread_mutex_destroy(pthread_mutex_t* mutex)
-{
-    VERIFY(s_pthread_functions.pthread_mutex_destroy);
-    return s_pthread_functions.pthread_mutex_destroy(mutex);
-}
-
-int pthread_mutexattr_init(pthread_mutexattr_t* attr)
-{
-    VERIFY(s_pthread_functions.pthread_mutexattr_init);
-    return s_pthread_functions.pthread_mutexattr_init(attr);
-}
-
-int pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type)
-{
-    VERIFY(s_pthread_functions.pthread_mutexattr_settype);
-    return s_pthread_functions.pthread_mutexattr_settype(attr, type);
-}
-
-int pthread_mutexattr_destroy(pthread_mutexattr_t* attr)
-{
-    VERIFY(s_pthread_functions.pthread_mutexattr_destroy);
-    return s_pthread_functions.pthread_mutexattr_destroy(attr);
-}
-
-int pthread_once(pthread_once_t* self, void (*callback)(void))
-{
-    VERIFY(s_pthread_functions.pthread_once);
-    return s_pthread_functions.pthread_once(self, callback);
-}
-
-int pthread_cond_broadcast(pthread_cond_t* cond)
-{
-    VERIFY(s_pthread_functions.pthread_cond_broadcast);
-    return s_pthread_functions.pthread_cond_broadcast(cond);
-}
-
-int pthread_cond_init(pthread_cond_t* cond, pthread_condattr_t const* attr)
-{
-    VERIFY(s_pthread_functions.pthread_cond_init);
-    return s_pthread_functions.pthread_cond_init(cond, attr);
-}
-
-int pthread_cond_signal(pthread_cond_t* cond)
-{
-    VERIFY(s_pthread_functions.pthread_cond_signal);
-    return s_pthread_functions.pthread_cond_signal(cond);
-}
-
-int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
-{
-    VERIFY(s_pthread_functions.pthread_cond_wait);
-    return s_pthread_functions.pthread_cond_wait(cond, mutex);
-}
-
-int pthread_cond_destroy(pthread_cond_t* cond)
-{
-    VERIFY(s_pthread_functions.pthread_cond_destroy);
-    return s_pthread_functions.pthread_cond_destroy(cond);
-}
-
-int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
-{
-    VERIFY(s_pthread_functions.pthread_cond_timedwait);
-    return s_pthread_functions.pthread_cond_timedwait(cond, mutex, abstime);
-}

+ 0 - 0
Userland/Libraries/LibPthread/pthread_once.cpp → Userland/Libraries/LibC/pthread_once.cpp


+ 0 - 0
Userland/Libraries/LibPthread/semaphore.cpp → Userland/Libraries/LibC/semaphore.cpp


+ 0 - 0
Userland/Libraries/LibPthread/semaphore.h → Userland/Libraries/LibC/semaphore.h


+ 4 - 11
Userland/Libraries/LibPthread/CMakeLists.txt

@@ -1,11 +1,4 @@
-set(SOURCES
-    forward.cpp
-    pthread.cpp
-    pthread_cond.cpp
-    pthread_once.cpp
-    semaphore.cpp
-)
-
-serenity_libc(LibPthread pthread)
-target_link_libraries(LibPthread LibC LibSystem)
-target_include_directories(LibPthread PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
+# Provide a dummy target and a linker script that tells everything to link against LibC instead.
+add_library(LibPthread INTERFACE)
+target_link_libraries(LibPthread INTERFACE LibC)
+file(WRITE "${CMAKE_STAGING_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libpthread.so" "INPUT(libc.so)")

+ 0 - 31
Userland/Libraries/LibPthread/forward.cpp

@@ -1,31 +0,0 @@
-/*
- * Copyright (c) 2021, Gunnar Beutner <gunnar@beutner.name>
- * Copyright (c) 2022, the SerenityOS developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibC/bits/pthread_forward.h>
-
-static constexpr PthreadFunctions s_functions = {
-    .pthread_mutex_trylock = pthread_mutex_trylock,
-    .pthread_mutex_destroy = pthread_mutex_destroy,
-
-    .pthread_mutexattr_init = pthread_mutexattr_init,
-    .pthread_mutexattr_settype = pthread_mutexattr_settype,
-    .pthread_mutexattr_destroy = pthread_mutexattr_destroy,
-
-    .pthread_once = pthread_once,
-
-    .pthread_cond_broadcast = pthread_cond_broadcast,
-    .pthread_cond_init = pthread_cond_init,
-    .pthread_cond_signal = pthread_cond_signal,
-    .pthread_cond_wait = pthread_cond_wait,
-    .pthread_cond_destroy = pthread_cond_destroy,
-    .pthread_cond_timedwait = pthread_cond_timedwait,
-};
-
-[[gnu::constructor]] static void forward_pthread_functions()
-{
-    __init_pthread_forward(s_functions);
-}