From b820e4626c1a1bde87684807273a7464f160311d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 21 Jul 2020 18:45:06 +0200 Subject: [PATCH] LibC: Add a cache for getpid() This works the same as gettid(). No sense in making a syscall to the kernel every time you ask for the PID since it won't change. Just like gettid(), the cache is invalidated on fork(). --- Libraries/LibC/unistd.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Libraries/LibC/unistd.cpp b/Libraries/LibC/unistd.cpp index 30929d47c1..9ff0605957 100644 --- a/Libraries/LibC/unistd.cpp +++ b/Libraries/LibC/unistd.cpp @@ -47,6 +47,7 @@ extern "C" { static __thread int s_cached_tid = 0; +static __thread int s_cached_pid = 0; int chown(const char* pathname, uid_t uid, gid_t gid) { @@ -68,8 +69,10 @@ int fchown(int fd, uid_t uid, gid_t gid) pid_t fork() { int rc = syscall(SC_fork); - if (rc == 0) + if (rc == 0) { s_cached_tid = 0; + s_cached_pid = 0; + } __RETURN_WITH_ERRNO(rc, rc, -1); } @@ -199,7 +202,9 @@ gid_t getgid() pid_t getpid() { - return syscall(SC_getpid); + if (!s_cached_pid) + s_cached_pid = syscall(SC_getpid); + return s_cached_pid; } pid_t getppid()