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

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().
This commit is contained in:
Andreas Kling 2020-07-21 18:45:06 +02:00
parent 15753e9633
commit b820e4626c

View file

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