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_SINGLETHREADED
7
8namespace roo_threads {
9
10/// @brief Backend namespace for single-threaded fallback implementations.
11namespace singlethreaded {
12
13/// @ingroup roo_threads_api_thread
14/// @brief Single-threaded backend implementation of `roo::thread`.
15/// @copydoc roo_threads::doc::thread
16class thread {
17 public:
18 /// @brief Single-threaded backend thread identifier.
19 /// @copydoc roo_threads::doc::thread::id
20 class id {
21 public:
22 /// @copydoc roo_threads::doc::thread::id::id
23 id() : id_(0) {}
24 id(int id) : id_(id) {}
25 /// @copydoc roo_threads::doc::thread::id::operator==
26 bool operator==(const id& other) const { return id_ == other.id_; }
27
28 private:
29 friend class thread;
30 int id_;
31 };
32
33 /// @copydoc roo_threads::doc::thread::thread() noexcept
34 thread() noexcept = default;
35
36 thread(const thread&) = delete;
37
38 /// @copydoc roo_threads::doc::thread::thread(thread&&)
39 thread(thread&& other) noexcept { swap(other); }
40
41 // Left intentionally unimplemented, as there is nothing we can do on a
42 // single-threaded platform.
43 /// @copydoc roo_threads::doc::thread::thread(Callable&&,Args&&...)
44 /// @note Not available in single-threaded mode.
45 template <typename Callable, typename... Args>
46 explicit thread(Callable&& callable, Args&&... args);
47
48 /// @copydoc roo_threads::doc::thread::~thread
49 ~thread() = default;
50
51 thread& operator=(const thread&) = delete;
52
53 /// @copydoc roo_threads::doc::thread::operator=(thread&&)
54 thread& operator=(thread&& other) noexcept = default;
55
56 /// @copydoc roo_threads::doc::thread::swap
57 void swap(thread& other) noexcept { std::swap(id_, other.id_); }
58
59 /// @copydoc roo_threads::doc::thread::joinable
60 bool joinable() const noexcept { return !(id_ == id()); }
61
62 /// @copydoc roo_threads::doc::thread::join
63 /// @note Not available in single-threaded mode.
64 void join(); // Left unimplemented.
65
66 /// @copydoc roo_threads::doc::thread::detach
67 /// @note Not available in single-threaded mode.
68 void detach(); // Left unimplemented.
69
70 /// @copydoc roo_threads::doc::thread::get_id
71 thread::id get_id() const noexcept { return id_; }
72
73 private:
74 id id_;
75};
76
77namespace this_thread {
78
79/// @ingroup roo_threads_api_thread
80/// @copydoc roo_threads::doc::this_thread::get_id
81inline thread::id get_id() noexcept { return thread::id(1); }
82
83/// @ingroup roo_threads_api_thread
84/// @copydoc roo_threads::doc::this_thread::yield
85/// @note Not available in single-threaded mode.
86void yield() noexcept; // Left unimplemented.
87
88/// @ingroup roo_threads_api_thread
89/// @copydoc roo_threads::doc::this_thread::sleep_for
90inline void sleep_for(const roo_time::Duration& duration) {
91 if (duration.inMicros() <= 0) return;
92 roo_time::Delay(duration);
93}
94
95/// @ingroup roo_threads_api_thread
96/// @copydoc roo_threads::doc::this_thread::sleep_until
97inline void sleep_until(const roo_time::Uptime& when) {
98 return sleep_for(when - roo_time::Uptime::Now());
99}
100
101} // namespace this_thread
102
103} // namespace singlethreaded
104} // namespace roo_threads
105
106#endif // ROO_THREADS_SINGLETHREADED
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.