summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-04-13 10:17:11 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-13 10:27:14 +0200
commita0592912c362dd268c341746b2614f02fe00142b (patch)
tree1269183670f33297025cc6bb7a6d8f365b45f17d
parentbbd592cb6c449dd0b96b8e6a8103126d698f8262 (diff)
downloadserenity-a0592912c362dd268c341746b2614f02fe00142b.zip
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! :^)
-rw-r--r--Libraries/LibC/unistd.cpp26
1 files 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)