From 4a95343936b736ffc07daff969bf80c71dfba905 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 3 Aug 2025 19:33:01 +0300 Subject: [PATCH] LibC: Make _get_uthread a macro This allows nice inlining :) --- userspace/libraries/LibC/errno.cpp | 2 -- userspace/libraries/LibC/include/pthread.h | 14 ++++++++++++++ userspace/libraries/LibC/pthread.cpp | 11 ----------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/userspace/libraries/LibC/errno.cpp b/userspace/libraries/LibC/errno.cpp index 6f2a668f..707732c9 100644 --- a/userspace/libraries/LibC/errno.cpp +++ b/userspace/libraries/LibC/errno.cpp @@ -1,8 +1,6 @@ #include #include -extern uthread* _get_uthread(); - int* __errno_location() { return &_get_uthread()->errno_; diff --git a/userspace/libraries/LibC/include/pthread.h b/userspace/libraries/LibC/include/pthread.h index d8ecd745..ae33592e 100644 --- a/userspace/libraries/LibC/include/pthread.h +++ b/userspace/libraries/LibC/include/pthread.h @@ -87,6 +87,20 @@ struct uthread #define _PTHREAD_ATFORK_CHILD 2 void _pthread_call_atfork(int state); +#if defined(__x86_64__) +#define _get_uthread() ({ \ + struct uthread* __tmp; \ + asm volatile("movq %%fs:0, %0" : "=r"(__tmp)); \ + __tmp; \ + }) +#elif defined(__i686__) +#define _get_uthread() ({ \ + struct uthread* __tmp; \ + asm volatile("movl %%gs:0, %0" : "=r"(__tmp)); \ + __tmp; \ + }) +#endif + int pthread_atfork(void (*prepare)(void), void (*parent)(void), void(*child)(void)); int pthread_attr_destroy(pthread_attr_t* attr); int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* detachstate); diff --git a/userspace/libraries/LibC/pthread.cpp b/userspace/libraries/LibC/pthread.cpp index 45d7e278..18411eaa 100644 --- a/userspace/libraries/LibC/pthread.cpp +++ b/userspace/libraries/LibC/pthread.cpp @@ -54,17 +54,6 @@ extern "C" void _pthread_trampoline_cpp(void* arg) ASSERT_NOT_REACHED(); } -uthread* _get_uthread() -{ - uthread* result; -#if ARCH(x86_64) - asm volatile("movq %%fs:0, %0" : "=r"(result)); -#elif ARCH(i686) - asm volatile("movl %%gs:0, %0" : "=r"(result)); -#endif - return result; -} - static void free_uthread(uthread* uthread) { if (uthread->dtv[0] == 0)