MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::this_thread Namespace Reference

Functions

Duration sleep (Duration duration, Clock clock=Clock::Realtime) noexcept
int sleepUntil (Timepoint timepoint, Clock clock=Clock::Realtime) noexcept
void yield () noexcept
void yieldProcessor () noexcept

Function Documentation

◆ sleep()

MXL_EXPORT Duration mxl::lib::this_thread::sleep ( Duration duration,
Clock clock = Clock::Realtime )
noexcept

Sleep for the specified amount of time to pass on the specified clock.

Parameters
[in]durationthe amount of time to sleep for,
[in]clockthe clock the specified duration refers to.
Returns
The remaining time of the originally specified duration that was not spent sleeping because the sleep was interrupted for any reason.

If the remaining time is non-zero the condition that caused the sleep to abort can be obtained by inspecting errno.

Definition at line 33 of file Thread.cpp.

34 {
35 auto const sleepTime = asTimeSpec(duration);
36 std::timespec remainder;
37
38 auto const req = &sleepTime;
39 auto const rem = &remainder;
40
41#if defined(__linux__)
42 auto const result = ::clock_nanosleep(detail::clockToId(clock), 0, req, rem);
43 errno = result;
44#elif defined(__APPLE__)
45 auto const result = ::nanosleep(req, rem);
46#else
47# pragma GCC error No this_thread::sleep() implementation available for the current platform.
48#endif
49 // Please be aware that nanosleep() and clock_nanosleep() have
50 // different domains for their return value, but in either case
51 // a value of 0 means the call completed successfully.
52 return (result == 0) ? Duration{} : asDuration(remainder);
53 }
constexpr clockid_t clockToId(Clock clock) noexcept
constexpr std::timespec asTimeSpec(Timepoint timepoint) noexcept
Definition Timing.hpp:241
constexpr Duration asDuration(std::timespec const &duration) noexcept
Definition Timing.hpp:385

◆ sleepUntil()

MXL_EXPORT int mxl::lib::this_thread::sleepUntil ( Timepoint timepoint,
Clock clock = Clock::Realtime )
noexcept

Sleep until the specified point in time is reached by the specified clock.

Parameters
[in]timepointthe point in time, until which to sleep.
[in]clockthe clock the specified point in time refers to.
Returns
0 on success, or an error number if the sleep failed to complete for any reason.

Definition at line 56 of file Thread.cpp.

57 {
58#if defined(__linux__)
59 auto const sleepTime = asTimeSpec(timepoint);
60 auto const result = ::clock_nanosleep(detail::clockToId(clock), TIMER_ABSTIME, &sleepTime, nullptr);
61 return result;
62#elif defined(__APPLE__)
63
64 auto const now = currentTime(clock);
65 if (timepoint > now)
66 {
67 auto const sleepTime = asTimeSpec(timepoint - now);
68 auto const result = ::nanosleep(&sleepTime, nullptr);
69 if (result)
70 {
71 return errno;
72 }
73 }
74 return 0;
75#else
76# pragma GCC error No this_thread::sleepUntil() implementation available for the current platform.
77#endif
78 }
auto const now
auto const currentTime
Definition test_time.cpp:58

◆ yield()

void mxl::lib::this_thread::yield ( )
noexcept

Yield the current thread time slice to other runnable threads.

Definition at line 14 of file Thread.cpp.

15 {
16 ::sched_yield();
17 }

◆ yieldProcessor()

void mxl::lib::this_thread::yieldProcessor ( )
noexcept

Perform a CPU level yield operation to briefly relieve pressure on shared resources, such as the memory bus during busy loops.

Definition at line 19 of file Thread.cpp.

20 {
21#if defined(__i386__) || defined(__x86_64__)
22 __builtin_ia32_pause();
23#elif defined(__ARM_ARCH) && (__ARM_ARCH >= 7)
24 asm volatile("dmb ish" ::: "memory");
25#else
26 // At least emulate the side effect of a compiler
27 // read/write barrier.
28 asm volatile("" ::: "memory")
29#endif
30 }