From a0592912c362dd268c341746b2614f02fe00142b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 13 Apr 2020 10:17:11 +0200 Subject: [PATCH] LibC: Simplify the gettid() cache by just clearing the cache in fork() It's hilarious how much better this is. Thanks to Sergey for suggesting it! :^) --- Libraries/LibC/unistd.cpp | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/Libraries/LibC/unistd.cpp b/Libraries/LibC/unistd.cpp index 679817a937..d1891c146a 100644 --- a/Libraries/LibC/unistd.cpp +++ b/Libraries/LibC/unistd.cpp @@ -46,6 +46,8 @@ extern "C" { +static __thread int s_cached_tid = 0; + int chown(const char* pathname, uid_t uid, gid_t gid) { if (!pathname) { @@ -66,6 +68,8 @@ int fchown(int fd, uid_t uid, gid_t gid) pid_t fork() { int rc = syscall(SC_fork); + if (rc == 0) + s_cached_tid = 0; __RETURN_WITH_ERRNO(rc, rc, -1); } @@ -548,27 +552,11 @@ int ftruncate(int fd, off_t length) __RETURN_WITH_ERRNO(rc, rc, -1); } -struct ThreadInfoCache { - int tid { 0 }; -}; - -__thread ThreadInfoCache* thread_info_cache; - int gettid() { - if (!thread_info_cache) { - thread_info_cache = (ThreadInfoCache*)mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); - ASSERT(thread_info_cache != MAP_FAILED); - if (minherit(thread_info_cache, PAGE_SIZE, MAP_INHERIT_ZERO) < 0) { - perror("minherit(MAP_INHERIT_ZERO)"); - ASSERT_NOT_REACHED(); - } - } - - if (thread_info_cache->tid == 0) - thread_info_cache->tid = syscall(SC_gettid); - - return thread_info_cache->tid; + if (!s_cached_tid) + s_cached_tid = syscall(SC_gettid); + return s_cached_tid; } int donate(int tid)