mirror of
https://github.com/RGBCube/serenity
synced 2025-07-19 06:37:38 +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:
parent
bbd592cb6c
commit
a0592912c3
1 changed files with 7 additions and 19 deletions
|
@ -46,6 +46,8 @@
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
|
static __thread int s_cached_tid = 0;
|
||||||
|
|
||||||
int chown(const char* pathname, uid_t uid, gid_t gid)
|
int chown(const char* pathname, uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
if (!pathname) {
|
if (!pathname) {
|
||||||
|
@ -66,6 +68,8 @@ int fchown(int fd, uid_t uid, gid_t gid)
|
||||||
pid_t fork()
|
pid_t fork()
|
||||||
{
|
{
|
||||||
int rc = syscall(SC_fork);
|
int rc = syscall(SC_fork);
|
||||||
|
if (rc == 0)
|
||||||
|
s_cached_tid = 0;
|
||||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -548,27 +552,11 @@ int ftruncate(int fd, off_t length)
|
||||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ThreadInfoCache {
|
|
||||||
int tid { 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
__thread ThreadInfoCache* thread_info_cache;
|
|
||||||
|
|
||||||
int gettid()
|
int gettid()
|
||||||
{
|
{
|
||||||
if (!thread_info_cache) {
|
if (!s_cached_tid)
|
||||||
thread_info_cache = (ThreadInfoCache*)mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
s_cached_tid = syscall(SC_gettid);
|
||||||
ASSERT(thread_info_cache != MAP_FAILED);
|
return s_cached_tid;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int donate(int tid)
|
int donate(int tid)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue