roo_threads
API Documentation for roo_threads
Loading...
Searching...
No Matches
thread_api.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5#include "roo_time.h"
6
7namespace roo_threads {
8namespace doc {
9
10/// @ingroup roo_threads_api_thread
11/// @brief Canonical documentation-only thread API.
12///
13/// This type exists only to provide a single source of truth for API docs
14/// across backend-specific implementations selected via compile-time macros.
15class thread {
16 public:
17 /// @brief Opaque thread identifier.
18 class id;
19
20 /// @brief Optional thread attributes used by backend-specific constructors.
21 class attributes {
22 public:
23 /// @brief Creates default thread attributes.
25
26 /// @brief Returns requested stack size in bytes.
27 uint32_t stack_size() const;
28
29 /// @brief Returns requested scheduler priority.
30 uint16_t priority() const;
31
32 /// @brief Returns whether the created thread is joinable.
33 bool joinable() const;
34
35 /// @brief Returns thread name, or `nullptr` if not set.
36 const char* name() const;
37
38 /// @brief Sets requested stack size in bytes.
39 /// @param stack_size stack size in bytes.
40 void set_stack_size(uint32_t stack_size);
41
42 /// @brief Sets requested scheduler priority.
43 /// @param priority scheduler priority.
44 void set_priority(uint16_t priority);
45
46 /// @brief Sets thread name.
47 /// @param name zero-terminated name string.
48 void set_name(const char* name);
49 };
50
51 /// @brief Constructs a non-joinable empty thread handle.
52 thread() noexcept;
53
54 thread(const thread&) = delete;
55
56 /// @brief Move-constructs a thread handle.
57 /// @param other source thread handle.
58 thread(thread&& other) noexcept;
59
60 /// @brief Starts a new thread using default attributes.
61 /// @tparam Callable callable type.
62 /// @tparam Args argument types.
63 /// @param callable callable object.
64 /// @param args arguments passed to callable.
65 template <typename Callable, typename... Args>
66 explicit thread(Callable&& callable, Args&&... args);
67
68 /// @brief Starts a new thread using explicit attributes.
69 /// @tparam Callable callable type.
70 /// @tparam Args argument types.
71 /// @param attrs backend-specific thread attributes.
72 /// @param callable callable object.
73 /// @param args arguments passed to callable.
74 template <typename Callable, typename... Args>
75 explicit thread(const attributes& attrs, Callable&& callable, Args&&... args);
76
77 /// @brief Destroys the thread handle.
78 ///
79 /// Backend-specific behavior applies if the thread is still joinable.
81
82 thread& operator=(const thread&) = delete;
83
84 /// @brief Move-assigns a thread handle.
85 /// @param other source thread handle.
86 /// @return reference to this object.
87 thread& operator=(thread&& other) noexcept;
88
89 /// @brief Swaps two thread handles.
90 /// @param other thread handle to swap with.
91 void swap(thread& other) noexcept;
92
93 /// @brief Returns true if this handle represents an active thread.
94 bool joinable() const noexcept;
95
96 /// @brief Waits for the represented thread to complete.
97 void join();
98
99 /// @brief Detaches the represented thread from this handle.
100 void detach();
101
102 /// @brief Returns identifier of the represented thread.
103 id get_id() const noexcept;
104};
105
106namespace this_thread {
107
108/// @ingroup roo_threads_api_thread
109/// @brief Returns identifier of the current thread.
111
112/// @ingroup roo_threads_api_thread
113/// @brief Hints the scheduler to run another thread.
114void yield() noexcept;
115
116/// @ingroup roo_threads_api_thread
117/// @brief Blocks the current thread for at least the given duration.
118/// @param duration sleep interval.
119void sleep_for(const roo_time::Duration& duration);
120
121/// @ingroup roo_threads_api_thread
122/// @brief Blocks the current thread until the specified time point.
123/// @param when wake-up time.
124void sleep_until(const roo_time::Uptime& when);
125
126} // namespace this_thread
127
128class thread::id {
129 public:
130 /// @brief Constructs an invalid thread identifier.
131 id();
132
133 /// @brief Equality comparison.
134 bool operator==(const id& other) const;
135
136 /// @brief Inequality comparison.
137 bool operator!=(const id& other) const;
138
139 /// @brief Strict weak ordering comparison.
140 bool operator<(const id& other) const;
141
142 /// @brief Less-than-or-equal comparison.
143 bool operator<=(const id& other) const;
144
145 /// @brief Greater-than comparison.
146 bool operator>(const id& other) const;
147
148 /// @brief Greater-than-or-equal comparison.
149 bool operator>=(const id& other) const;
150};
151
152} // namespace doc
153} // namespace roo_threads
Optional thread attributes used by backend-specific constructors.
Definition thread_api.h:21
void set_priority(uint16_t priority)
Sets requested scheduler priority.
void set_name(const char *name)
Sets thread name.
void set_stack_size(uint32_t stack_size)
Sets requested stack size in bytes.
uint16_t priority() const
Returns requested scheduler priority.
uint32_t stack_size() const
Returns requested stack size in bytes.
const char * name() const
Returns thread name, or nullptr if not set.
bool joinable() const
Returns whether the created thread is joinable.
attributes()
Creates default thread attributes.
bool operator==(const id &other) const
Equality comparison.
bool operator>(const id &other) const
Greater-than comparison.
bool operator>=(const id &other) const
Greater-than-or-equal comparison.
bool operator!=(const id &other) const
Inequality comparison.
id()
Constructs an invalid thread identifier.
bool operator<=(const id &other) const
Less-than-or-equal comparison.
bool operator<(const id &other) const
Strict weak ordering comparison.
Canonical documentation-only thread API.
Definition thread_api.h:15
void join()
Waits for the represented thread to complete.
id get_id() const noexcept
Returns identifier of the represented thread.
void swap(thread &other) noexcept
Swaps two thread handles.
bool joinable() const noexcept
Returns true if this handle represents an active thread.
void detach()
Detaches the represented thread from this handle.
thread() noexcept
Constructs a non-joinable empty thread handle.