summaryrefslogtreecommitdiff
path: root/zen/thread.h
diff options
context:
space:
mode:
Diffstat (limited to 'zen/thread.h')
-rw-r--r--zen/thread.h27
1 files changed, 26 insertions, 1 deletions
diff --git a/zen/thread.h b/zen/thread.h
index fd9dc76d..ac94da6a 100644
--- a/zen/thread.h
+++ b/zen/thread.h
@@ -65,6 +65,9 @@ void interruptibleSleep(const std::chrono::duration<Rep, Period>& relTime); //th
#ifdef ZEN_WIN
void setCurrentThreadName(const char* threadName);
#endif
+
+std::uint64_t getThreadId(); //simple integer thread id, unlike boost::thread::id: https://svn.boost.org/trac/boost/ticket/5754
+
//------------------------------------------------------------------------------------------
/*
@@ -335,7 +338,7 @@ private:
}
std::atomic<bool> interrupted{ false }; //std:atomic is uninitialized by default!!!
- //"The default constructor is trivial: no initialization takes place other than zero initialization of static and thread-local objects."
+ //"The default constructor is trivial: no initialization takes place other than zero initialization of static and thread-local objects."
std::condition_variable* activeCondition = nullptr;
std::mutex lockConditionPtr; //serialize pointer access (only!)
@@ -447,6 +450,28 @@ void setCurrentThreadName(const char* threadName)
__except (EXCEPTION_EXECUTE_HANDLER) {}
}
#endif
+
+
+inline
+std::uint64_t getThreadId()
+{
+#ifdef ZEN_WIN
+ static_assert(sizeof(std::uint64_t) >= sizeof(DWORD), "");
+ return ::GetCurrentThreadId(); //no-fail
+
+#elif defined ZEN_LINUX
+ //obviously "gettid()" is not available on Ubuntu/Debian/Suse => use the OpenSSL approach:
+ static_assert(sizeof(std::uint64_t) >= sizeof(void*), "");
+ return reinterpret_cast<std::uint64_t>(static_cast<void*>(&errno));
+
+#elif defined ZEN_MAC
+ uint64_t tid = 0;
+ const int rv = ::pthread_threadid_np(nullptr, &tid); //yeah, theoretically no-fail, too :> http://opensource.apple.com//source/Libc/Libc-583/pthreads/pthread.c
+ assert(rv == 0);
+ (void)rv;
+ return tid;
+#endif
+}
}
#endif //THREAD_H_7896323423432235246427
bgstack15