4#ifndef GREM_CORE_SYSTEM_THREAD_HPP
5#define GREM_CORE_SYSTEM_THREAD_HPP
17#ifdef GREM_USE_MULTITHREADING
22#elif defined(__linux__)
28#ifndef GREM_USE_MULTITHREADING
36struct InvalidNativeThreadHandle {};
38template <
typename Thread>
39struct native_thread_handle {
40 using type = InvalidNativeThreadHandle;
43template <
typename Thread>
55#ifdef GREM_USE_MULTITHREADING
56 return std::this_thread::get_id();
64#ifdef GREM_USE_MULTITHREADING
65 ThreadID(std::thread::id value) noexcept
73#ifdef GREM_USE_MULTITHREADING
74 return value != std::thread::id{};
80#ifdef GREM_USE_MULTITHREADING
86 template <
typename CharT,
typename Traits>
87 friend std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& stream,
ThreadID id) {
88#ifdef GREM_USE_MULTITHREADING
89 return stream <<
id.value;
98 friend std::hash<ThreadID>;
100#ifdef GREM_USE_MULTITHREADING
101 std::thread::id value{};
107#ifdef GREM_USE_MULTITHREADING
114#ifdef GREM_USE_MULTITHREADING
115 return std::thread::hardware_concurrency();
124 template <typename F, typename... Args>
126#ifdef GREM_USE_MULTITHREADING
127 : value(std::forward<F>(f), std::forward<Args>(args)...)
130#ifndef GREM_USE_MULTITHREADING
141#ifdef GREM_USE_MULTITHREADING
142 return value.joinable();
149#ifdef GREM_USE_MULTITHREADING
150 return value.get_id();
157#ifdef GREM_USE_MULTITHREADING
158 if constexpr (
requires { value.native_handle(); }) {
159 return value.native_handle();
169#ifdef GREM_USE_MULTITHREADING
175#ifdef GREM_USE_MULTITHREADING
181#ifdef GREM_USE_MULTITHREADING
182 value.swap(other.value);
191#ifdef GREM_USE_MULTITHREADING
197#ifdef GREM_USE_MULTITHREADING
198 std::this_thread::yield();
201#elif defined(__linux__)
206template <
typename Rep,
typename Period>
208#ifdef GREM_USE_MULTITHREADING
209 std::this_thread::sleep_for(duration);
214 const TimePoint endTime = Clock::now() + duration_cast<Duration>(duration);
217 if (milliseconds > 0) {
221 while (currentTime < endTime) {
222 currentTime = Clock::now();
224#elif defined(__linux__)
228 struct timespec ts = {};
230 using TimespecNanoseconds =
DurationBase<
decltype(ts.tv_nsec),
Ratio<1, 1'000'000'000>>;
232 const TimespecNanoseconds nanoseconds = duration_cast<TimespecNanoseconds>(duration - seconds);
233 ts.tv_sec = seconds.count();
234 ts.tv_nsec = nanoseconds.count();
235 struct timespec rem = {};
236 while (nanosleep(&ts, &rem) != 0) {
237 if (errno == EINTR) {
242 const TimePoint endTime = currentTime + duration_cast<Duration>(duration);
243 while (currentTime < endTime) {
244 currentTime = Clock::now();
251 const TimePoint endTime = currentTime + duration_cast<Duration>(duration);
252 while (currentTime < endTime) {
253 currentTime = Clock::now();
258template <
typename Clock,
typename Duration>
260#ifdef GREM_USE_MULTITHREADING
261 std::this_thread::sleep_until(time);
262#elif defined(_WIN32) || defined(__linux__)
263 typename Clock::time_point currentTime = Clock::now();
264 if constexpr (Clock::is_steady) {
265 if (currentTime < time) {
269 while (currentTime < time) {
271 currentTime = Clock::now();
275 typename Clock::time_point currentTime = Clock::now();
276 while (currentTime < time) {
277 currentTime = Clock::now();
285struct std::hash<
grem::ThreadID> {
287#ifdef GREM_USE_MULTITHREADING
288 return hasher(
id.value);
295#ifdef GREM_USE_MULTITHREADING
297 std::hash<std::thread::id> hasher;
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
auto operator<=>(const ThreadID &) const =default
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &stream, ThreadID id)
Definition Thread.hpp:87
bool operator==(const ThreadID &) const =default
ThreadID() noexcept=default
static GREM_ALWAYS_INLINE ThreadID getCurrent() noexcept
Definition Thread.hpp:54
Definition Thread.hpp:105
GREM_ALWAYS_INLINE friend void swap(Thread &a, Thread &b) noexcept
Definition Thread.hpp:186
GREM_ALWAYS_INLINE void join()
Definition Thread.hpp:168
Thread(Thread &&) noexcept=default
GREM_ALWAYS_INLINE void swap(Thread &other) noexcept
Definition Thread.hpp:180
Thread() noexcept=default
GREM_ALWAYS_INLINE void detach()
Definition Thread.hpp:174
GREM_ALWAYS_INLINE native_handle_type native_handle()
Definition Thread.hpp:156
GREM_ALWAYS_INLINE ThreadID get_id() const noexcept
Definition Thread.hpp:148
static GREM_ALWAYS_INLINE unsigned int hardware_concurrency() noexcept
Definition Thread.hpp:113
detail::InvalidNativeThreadHandle native_handle_type
Definition Thread.hpp:110
GREM_ALWAYS_INLINE bool joinable() const noexcept
Definition Thread.hpp:140
GREM_ALWAYS_INLINE void sleepFor(const DurationBase< Rep, Period > &duration) noexcept
Definition Thread.hpp:207
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL floor(vec< N, T > a)
Definition math.hpp:1421
Clock::time_point TimePoint
Definition Clock.hpp:26
std::ratio< Num, Den > Ratio
Definition time.hpp:19
std::chrono::time_point< Clock, Duration > TimePointBase
Definition time.hpp:42
std::chrono::duration< Rep, Period > DurationBase
Definition time.hpp:39
void unreachable()
Definition assertions.hpp:125
GREM_ALWAYS_INLINE void sleepUntil(const TimePointBase< Clock, Duration > &time) noexcept
Definition Thread.hpp:259
GREM_ALWAYS_INLINE void yield() noexcept
Definition Thread.hpp:196
std::size_t operator()(const grem::ThreadID &id) const
Definition Thread.hpp:286