mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-05 15:21:43 +00:00
31 lines
691 B
C++
31 lines
691 B
C++
#include <common/getThreadId.h>
|
|
|
|
#if defined(OS_LINUX)
|
|
#include <unistd.h>
|
|
#include <syscall.h>
|
|
#elif defined(OS_FREEBSD)
|
|
#include <pthread_np.h>
|
|
#else
|
|
#include <pthread.h>
|
|
#include <stdexcept>
|
|
#endif
|
|
|
|
|
|
static thread_local uint64_t current_tid = 0;
|
|
uint64_t getThreadId()
|
|
{
|
|
if (!current_tid)
|
|
{
|
|
#if defined(OS_LINUX)
|
|
current_tid = syscall(SYS_gettid); /// This call is always successful. - man gettid
|
|
#elif defined(OS_FREEBSD)
|
|
current_tid = pthread_getthreadid_np();
|
|
#else
|
|
if (0 != pthread_threadid_np(nullptr, ¤t_tid))
|
|
throw std::logic_error("pthread_threadid_np returned error");
|
|
#endif
|
|
}
|
|
|
|
return current_tid;
|
|
}
|