From 161cb89e87bf0890849e205e98bff56bb0c99898 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 14 Sep 2019 11:37:58 +0200 Subject: [PATCH] LibC: Make gettid() cache the thread ID in thread-local-storage This makes gettid() very cheap, compared to the cost of having to do a syscall every time. --- Libraries/LibC/unistd.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Libraries/LibC/unistd.cpp b/Libraries/LibC/unistd.cpp index 6881d2e33b..831f9c99ad 100644 --- a/Libraries/LibC/unistd.cpp +++ b/Libraries/LibC/unistd.cpp @@ -538,10 +538,13 @@ int ftruncate(int fd, off_t length) __RETURN_WITH_ERRNO(rc, rc, -1); } +__thread int t_cached_tid = -1; + int gettid() { - int rc = syscall(SC_gettid); - __RETURN_WITH_ERRNO(rc, rc, -1); + if (t_cached_tid == -1) + t_cached_tid = syscall(SC_gettid); + return t_cached_tid; } int donate(int tid)