2020-11-24 15:41:18 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <brynet/base/Platform.hpp>
|
|
|
|
|
|
|
|
#ifdef BRYNET_PLATFORM_WINDOWS
|
2021-04-30 16:10:14 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2020-11-24 15:41:18 +01:00
|
|
|
#include <Windows.h>
|
2021-04-30 16:10:14 +02:00
|
|
|
#include <winsock2.h>
|
2020-11-24 15:41:18 +01:00
|
|
|
#elif defined BRYNET_PLATFORM_LINUX
|
2021-04-30 16:10:14 +02:00
|
|
|
#include <linux/unistd.h>
|
2020-11-24 15:41:18 +01:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2021-04-30 16:10:14 +02:00
|
|
|
#elif defined BRYNET_PLATFORM_DARWIN
|
2020-11-24 15:41:18 +01:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
2021-04-30 16:10:14 +02:00
|
|
|
#include <unistd.h>
|
2020-11-24 15:41:18 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace brynet { namespace net { namespace current_thread {
|
|
|
|
|
|
|
|
#ifdef BRYNET_PLATFORM_WINDOWS
|
2021-04-30 16:10:14 +02:00
|
|
|
using THREAD_ID_TYPE = DWORD;
|
2020-11-24 15:41:18 +01:00
|
|
|
#elif defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
|
2021-04-30 16:10:14 +02:00
|
|
|
using THREAD_ID_TYPE = int;
|
2020-11-24 15:41:18 +01:00
|
|
|
#endif
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
static THREAD_ID_TYPE& tid()
|
|
|
|
{
|
2020-11-24 15:41:18 +01:00
|
|
|
#ifdef BRYNET_PLATFORM_WINDOWS
|
2021-04-30 16:10:14 +02:00
|
|
|
static __declspec(thread) THREAD_ID_TYPE cachedTid = 0;
|
2020-11-24 15:41:18 +01:00
|
|
|
#elif defined BRYNET_PLATFORM_LINUX || defined BRYNET_PLATFORM_DARWIN
|
2021-04-30 16:10:14 +02:00
|
|
|
static __thread THREAD_ID_TYPE cachedTid = 0;
|
2020-11-24 15:41:18 +01:00
|
|
|
#endif
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
if (cachedTid == 0)
|
|
|
|
{
|
2020-11-24 15:41:18 +01:00
|
|
|
#ifdef BRYNET_PLATFORM_WINDOWS
|
2021-04-30 16:10:14 +02:00
|
|
|
cachedTid = GetCurrentThreadId();
|
2020-11-24 15:41:18 +01:00
|
|
|
#elif defined BRYNET_PLATFORM_LINUX
|
2021-04-30 16:10:14 +02:00
|
|
|
cachedTid = static_cast<pid_t>(::syscall(SYS_gettid));
|
2020-11-24 15:41:18 +01:00
|
|
|
#elif defined BRYNET_PLATFORM_DARWIN
|
2021-04-30 16:10:14 +02:00
|
|
|
// warning: 'syscall' is deprecated:
|
|
|
|
// first deprecated in macOS 10.12 - syscall(2) is unsupported;
|
|
|
|
// please switch to a supported interface.
|
|
|
|
uint64_t tid64;
|
|
|
|
pthread_threadid_np(NULL, &tid64);
|
|
|
|
cachedTid = (pid_t) tid64;
|
2020-11-24 15:41:18 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
return cachedTid;
|
|
|
|
}
|
|
|
|
|
|
|
|
}}}// namespace brynet::net::current_thread
|