HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
thread.hpp
1// Copyright Take Vos 2019-2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "required.hpp"
8#include "os_detect.hpp"
9#include "hires_utc_clock.hpp"
10#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
11#include <intrin.h>
12#endif
13#include <thread>
14#include <string_view>
15#include <functional>
16#include <atomic>
17#include <chrono>
18
19namespace tt {
20
27void set_thread_name(std::string_view name);
28
29bool is_main_thread();
30
31void run_from_main_loop(std::function<void()> f);
32
33#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
34using thread_id = uint32_t;
35#else
36using thread_id = uint64_t;
39inline thread_local thread_id current_thread_id_dummy = 0;
40#endif
41
42
43
44[[nodiscard]] inline thread_id current_thread_id() noexcept
45{
46#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
47 // Thread IDs on Win32 are guaranteed to be not zero.
48 constexpr uint64_t NT_TIB_CurrentThreadID = 0x48;
49 return __readgsdword(NT_TIB_CurrentThreadID);
50#else
51 // Addresses can not be zero.
52 return reinterpret_cast<uint64_t>(&current_thread_id_dummy);
53#endif
54}
55
56}
57