1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00

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! :^)
This commit is contained in:
Andreas Kling 2020-04-13 10:17:11 +02:00
parent bbd592cb6c
commit a0592912c3

View file

@ -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)