diff options
author | Leandro A. F. Pereira <leandro.pereira@microsoft.com> | 2019-05-22 08:17:26 -0700 |
---|---|---|
committer | Jan Vorlicek <janvorli@microsoft.com> | 2019-05-22 08:17:26 -0700 |
commit | 41239feb757e7a5b5f0b710b810010df016d4391 (patch) | |
tree | 4b91dd12cd0075baa2c496359b68ab46d51c6465 /src | |
parent | aa66f12b58dc11857db5796ff49bd46dd242dd82 (diff) | |
download | coreclr-41239feb757e7a5b5f0b710b810010df016d4391.tar.gz coreclr-41239feb757e7a5b5f0b710b810010df016d4391.tar.bz2 coreclr-41239feb757e7a5b5f0b710b810010df016d4391.zip |
Cache current thread ID in TLS (#24699)
While looking at the strace of `corerun` built with logging and
debugging information, I was amazed at the number of gettid() calls it
was making. While system calls are cheap, they're still not free;
cache this number in the thread local storage area. Adds a branch, but
it's just a comparison with 0, so it's fine in comparison.
Diffstat (limited to 'src')
-rw-r--r-- | src/pal/src/include/pal/thread.hpp | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/pal/src/include/pal/thread.hpp b/src/pal/src/include/pal/thread.hpp index dd48c23e49..6227b721ce 100644 --- a/src/pal/src/include/pal/thread.hpp +++ b/src/pal/src/include/pal/thread.hpp @@ -828,25 +828,32 @@ Abstract: --*/ #if defined(__linux__) -#define THREADSilentGetCurrentThreadId() (SIZE_T)syscall(SYS_gettid) +#define PlatformGetCurrentThreadId() (SIZE_T)syscall(SYS_gettid) #elif defined(__APPLE__) -inline SIZE_T THREADSilentGetCurrentThreadId() { +inline SIZE_T PlatformGetCurrentThreadId() { uint64_t tid; pthread_threadid_np(pthread_self(), &tid); return (SIZE_T)tid; } #elif defined(__FreeBSD__) #include <sys/thr.h> -inline SIZE_T THREADSilentGetCurrentThreadId() { +inline SIZE_T PlatformGetCurrentThreadId() { long tid; thr_self(&tid); return (SIZE_T)tid; } #elif defined(__NetBSD__) #include <lwp.h> -#define THREADSilentGetCurrentThreadId() (SIZE_T)_lwp_self() +#define PlatformGetCurrentThreadId() (SIZE_T)_lwp_self() #else -#define THREADSilentGetCurrentThreadId() (SIZE_T)pthread_self() +#define PlatformGetCurrentThreadId() (SIZE_T)pthread_self() #endif +inline SIZE_T THREADSilentGetCurrentThreadId() { + static __thread SIZE_T tid; + if (!tid) + tid = PlatformGetCurrentThreadId(); + return tid; +} + #endif // _PAL_THREAD_HPP_ |