8#if UNRAVEL_PLATFORM_WINDOWS && (UNRAVEL_COMPILER_MSVC || UNRAVEL_COMPILER_CLANG)
9#define WIN32_LEAN_AND_MEAN
23typedef struct tagTHREADNAME_INFO
36 info.szName = threadName;
37 info.dwThreadID = dwThreadID;
40 static const DWORD MS_VC_EXCEPTION = 0x406D1388;
44#pragma warning(disable : 6320 6322)
47 RaiseException(MS_VC_EXCEPTION, 0,
sizeof(info) /
sizeof(ULONG_PTR), (ULONG_PTR*)&info);
49 __except(EXCEPTION_EXECUTE_HANDLER)
57 DWORD threadId = ::GetCurrentThreadId();
62inline auto get_boot_max_mhz() -> DWORD
64 using get_max_proc_freq_fn = DWORD(WINAPI*)(DWORD, BYTE);
65 static const get_max_proc_freq_fn fn = []() -> get_max_proc_freq_fn
67 return reinterpret_cast<get_max_proc_freq_fn
>(
68 GetProcAddress(GetModuleHandleW(L
"kernel32.dll"),
"GetMaximumProcessorFrequency"));
72 const DWORD
m = fn(0, 0);
91 if(QueryThreadCycleTime(GetCurrentThread(), &cycles))
93 static const double ns_per_cycle = []() ->
double
95 const DWORD mhz = get_boot_max_mhz();
96 const double hz =
static_cast<double>(mhz) * 1e6;
99 return static_cast<int64_t
>(
static_cast<double>(cycles) * ns_per_cycle);
102 FILETIME creation, exit, kernel, user;
103 if(GetThreadTimes(GetCurrentThread(), &creation, &exit, &kernel, &user))
105 auto filetime_to_ns = [](
const FILETIME& ft) -> int64_t
108 li.LowPart = ft.dwLowDateTime;
109 li.HighPart = ft.dwHighDateTime;
110 return static_cast<int64_t
>(li.QuadPart) * 100;
112 return filetime_to_ns(kernel) + filetime_to_ns(user);
117#elif UNRAVEL_PLATFORM_LINUX
124 pthread_setname_np(pthread_self(), threadName);
129 struct timespec ts{};
130 if(clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0)
132 return static_cast<int64_t
>(ts.tv_sec) * 1'000'000'000LL +
static_cast<int64_t
>(ts.tv_nsec);
137#elif UNRAVEL_PLATFORM_OSX
144 pthread_setname_np(threadName);
149 struct timespec ts{};
150 if(clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0)
152 return static_cast<int64_t
>(ts.tv_sec) * 1'000'000'000LL +
static_cast<int64_t
>(ts.tv_nsec);