roo_threads
API Documentation for roo_threads
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1#pragma once
2
4#include "roo_time.h"
5
6#ifdef ROO_THREADS_USE_CPPSTD
7
8#define ROO_THREADS_ATTRIBUTES_SUPPORT_PRIORITY 0
9#define ROO_THREADS_ATTRIBUTES_SUPPORT_NAME 0
10#define ROO_THREADS_ATTRIBUTES_SUPPORT_STACK_SIZE 0
11
12#include <thread>
13
14namespace roo_threads {
15
16/// @brief Backend namespace using C++ standard library threading primitives.
17namespace cppstd {
18
19/// @ingroup roo_threads_api_thread
20/// @brief C++ standard library backend implementation of `roo::thread`.
21/// @copydoc roo_threads::doc::thread
22using thread = std::thread;
23
24namespace this_thread {
25
26/// @ingroup roo_threads_api_thread
27/// @copydoc roo_threads::doc::this_thread::get_id
28inline thread::id get_id() noexcept { return std::this_thread::get_id(); }
29
30/// @ingroup roo_threads_api_thread
31/// @copydoc roo_threads::doc::this_thread::yield
32inline void yield() noexcept { std::this_thread::yield(); }
33
34/// @ingroup roo_threads_api_thread
35/// @copydoc roo_threads::doc::this_thread::sleep_for
36/// @note Wait duration is clamped to a safe upper bound to avoid overflow.
37inline void sleep_for(const roo_time::Duration& duration) {
38 static constexpr auto kMaxSafeWaitDelay =
39 std::chrono::microseconds(10LL * 24 * 3600 * 1000000);
40 auto delay = std::chrono::microseconds(duration.inMicros());
41 if (delay > kMaxSafeWaitDelay) {
42 // Protecting against overflow, e.g. wait_for(roo_time::Duration::Max()):
43 // never wait for longer than 10 days; spuriously wake up if needed.
44 // Using safely low max duration of 10 days, as ESP32 seems to overflow at
45 // about 24 days.
46 delay = kMaxSafeWaitDelay;
47 }
48 std::this_thread::sleep_for(delay);
49}
50
51/// @ingroup roo_threads_api_thread
52/// @copydoc roo_threads::doc::this_thread::sleep_until
53inline void sleep_until(const roo_time::Uptime& when) {
54 sleep_for(when - roo_time::Uptime::Now());
55}
56
57} // namespace this_thread
58
59} // namespace cppstd
60} // namespace roo_threads
61
62#endif
thread::id get_id() noexcept
Returns identifier of the current thread.
void yield() noexcept
Hints the scheduler to run another thread.
void sleep_until(const roo_time::Uptime &when)
Blocks the current thread until the specified time point.
void sleep_for(const roo_time::Duration &duration)
Blocks the current thread for at least the given duration.