summaryrefslogtreecommitdiff
path: root/Libraries/LibC
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-07-21 18:45:06 +0200
committerAndreas Kling <kling@serenityos.org>2020-07-21 19:08:01 +0200
commitb820e4626c1a1bde87684807273a7464f160311d (patch)
tree08050a2a8813a8c60b84b3571ef8efae32b50d3c /Libraries/LibC
parent15753e9633be5f157df89423f399e9732a85ce37 (diff)
downloadserenity-b820e4626c1a1bde87684807273a7464f160311d.zip
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().
Diffstat (limited to 'Libraries/LibC')
-rw-r--r--Libraries/LibC/unistd.cpp9
1 files 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()