Support for Mac OS and BSD

This commit is contained in:
Alexey Milovidov 2020-02-02 23:05:47 +03:00
parent d11f3cb78f
commit 55b83dbcee

View File

@ -1,13 +1,26 @@
#include <unistd.h>
#include <syscall.h>
#include <common/getThreadId.h> #include <common/getThreadId.h>
#if OS_LINUX
#include <unistd.h>
#include <syscall.h>
#else
#include <pthread.h>
#include <stdexcept>
#endif
static thread_local uint64_t current_tid = 0; static thread_local uint64_t current_tid = 0;
uint64_t getThreadId() uint64_t getThreadId()
{ {
if (!current_tid) if (!current_tid)
{
#if OS_LINUX
current_tid = syscall(SYS_gettid); /// This call is always successful. - man gettid current_tid = syscall(SYS_gettid); /// This call is always successful. - man gettid
#else
if (0 != pthread_threadid_np(nullptr, &current_tid)
throw std::logic_error("pthread_threadid_np returned error");
#endif
}
return current_tid; return current_tid;
} }