summaryrefslogtreecommitdiff
path: root/rts/Task.h
diff options
context:
space:
mode:
authorDuncan Coutts <duncan@well-typed.com>2012-07-03 19:18:46 +0100
committerMikolaj Konarski <mikolaj@well-typed.com>2012-07-07 00:28:19 +0200
commit647ae1cfbb5ea3e2d3b1541c2bc12ea5db321134 (patch)
tree9f40458a21ee74661b95ae43c0c61bc5e3a8e08f /rts/Task.h
parent01386d383fa535a16ccf6117adaffdd38af703ca (diff)
downloadhaskell-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/Task.h')
-rw-r--r--rts/Task.h39
1 files changed, 38 insertions, 1 deletions
diff --git a/rts/Task.h b/rts/Task.h
index ab47a07fc3..7c9c86e40b 100644
--- a/rts/Task.h
+++ b/rts/Task.h
@@ -159,7 +159,6 @@ isBoundTask (Task *task)
return (task->incall->tso != NULL);
}
-
// Linked list of all tasks.
//
extern Task *all_tasks;
@@ -275,6 +274,44 @@ setMyTask (Task *task)
#endif
}
+// Tasks are identified by their OS thread ID, which can be serialised
+// to StgWord64, as defined below.
+typedef StgWord64 TaskId;
+
+// Get a unique serialisable representation for a task id.
+//
+// It's only unique within the process. For example if they are emitted in a
+// log file then it is suitable to work out which log entries are releated.
+//
+// This is needed because OSThreadId is an opaque type
+// and in practice on some platforms it is a pointer type.
+//
+#if defined(THREADED_RTS)
+INLINE_HEADER TaskId serialiseTaskId (OSThreadId taskID) {
+#if defined(freebsd_HOST_OS) || defined(darwin_HOST_OS)
+ // Here OSThreadId is a pthread_t and pthread_t is a pointer, but within
+ // the process we can still use that pointer value as a unique id.
+ return (TaskId) taskID
+#else
+ // On Windows, Linux and others it's an integral type to start with.
+ return taskID;
+#endif
+}
+#endif
+
+//
+// Get a serialisable Id for the Task's OS thread
+// Needed mainly for logging since the OSThreadId is an opaque type
+INLINE_HEADER TaskId
+serialisableTaskId (Task *task STG_UNUSED)
+{
+#if defined(THREADED_RTS)
+ return serialiseTaskId(task->id);
+#else
+ return 1;
+#endif
+}
+
#include "EndPrivate.h"
#endif /* TASK_H */