diff options
author | Duncan Coutts <duncan@well-typed.com> | 2012-07-03 19:18:46 +0100 |
---|---|---|
committer | Mikolaj Konarski <mikolaj@well-typed.com> | 2012-07-07 00:28:19 +0200 |
commit | 647ae1cfbb5ea3e2d3b1541c2bc12ea5db321134 (patch) | |
tree | 9f40458a21ee74661b95ae43c0c61bc5e3a8e08f /rts/posix | |
parent | 01386d383fa535a16ccf6117adaffdd38af703ca (diff) | |
download | haskell-647ae1cfbb5ea3e2d3b1541c2bc12ea5db321134.tar.gz |
New functions to get kernel thread Id + serialisable task Id
On most platforms the userspace thread type (e.g. pthread_t) and kernel
thread id are different. Normally we don't care about kernel thread Ids,
but some system tools for tracing/profiling etc report kernel ids.
For example Solaris and OSX's DTrace and Linux's perf tool report kernel
thread ids. To be able to match these up with RTS's OSThread we need a
way to get at the kernel thread, so we add a new function for to do just
that (the implementation is system-dependent).
Additionally, strictly speaking the OSThreadId type, used as task ids,
is not a serialisable representation. On unix OSThreadId is a typedef for
pthread_t, but pthread_t is not guaranteed to be a numeric type.
Indeed on some systems pthread_t is a pointer and in principle it
could be a structure type. So we add another new function to get a
serialisable representation of an OSThreadId. This is only for use
in log files. We use the function to serialise an id of a task,
with the extra feature that it works in non-threaded builds
by always returning 1.
Diffstat (limited to 'rts/posix')
-rw-r--r-- | rts/posix/OSThreads.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index c29454809f..5fd09982de 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -23,6 +23,12 @@ #include "Rts.h" +#if defined(linux_HOST_OS) +#include <unistd.h> +#include <sys/types.h> +#include <sys/syscall.h> +#endif + #if defined(THREADED_RTS) #include "RtsUtils.h" #include "Task.h" @@ -312,4 +318,24 @@ nat getNumberOfProcessors (void) return 1; } +#endif /* defined(THREADED_RTS) */ + +KernelThreadId kernelThreadId (void) +{ +#if defined(linux_HOST_OS) + pid_t tid = syscall(SYS_gettid); // no really, see man gettid + return tid; + +#elif defined(freebsd_HOST_OS) + return pthread_getthreadid_np(); + +#elif defined(darwin_HOST_OS) + uint64_t ktid; + pthread_threadid_np(NULL, &ktid); + return ktid; + +#else + // unsupported + return 0; #endif +} |