24#ifndef RIOT_THREAD_HPP
25#define RIOT_THREAD_HPP
56 std::atomic<unsigned> ref_count;
58 std::array<char, THREAD_STACKSIZE_MAIN> stack;
72 if (--ptr->ref_count == 0) {
85 template <
class T,
class Traits>
86 friend std::basic_ostream<T, Traits>&
operator<<(std::basic_ostream
105 return m_handle == other.m_handle;
111 return !(m_handle == other.m_handle);
117 return m_handle < other.m_handle;
123 return !(m_handle > other.m_handle);
129 return m_handle > other.m_handle;
135 return !(m_handle < other.m_handle);
145template <
class T,
class Traits>
146inline std::basic_ostream<T, Traits>& operator<<(std::basic_ostream
149 return out <<
id.m_handle;
152namespace this_thread {
157inline thread_id get_id() noexcept {
return thread_id{
thread_getpid()}; }
166void sleep_for(
const std::chrono::microseconds& us);
171template <
class Rep,
class Period>
172void sleep_for(
const std::chrono::duration<Rep, Period>& sleep_duration) {
173 using namespace std::chrono;
174 if (sleep_duration > sleep_duration.zero()) {
175 constexpr duration<long double> max = microseconds::max();
177 if (sleep_duration < max) {
178 us = duration_cast<microseconds>(sleep_duration);
179 if (us.count() == 0) {
181 us = microseconds(1);
183 if (us < sleep_duration) {
187 us = microseconds::max();
199 condition_variable cv;
200 unique_lock<mutex> lk(mtx);
202 cv.wait_until(lk, sleep_time);
234 template <
class F,
class... Args>
235 explicit thread(F&& f, Args&&... args);
247 std::swap(m_data, t.m_data);
267 std::swap(m_data, t.m_data);
268 std::swap(m_handle, t.m_handle);
318template <class Tuple>
319void* thread_proxy(
void* vp) {
321 std::unique_ptr<Tuple> p(
static_cast<Tuple*
>(vp));
322 auto tmp = std::get<0>(*p);
323 std::unique_ptr<thread_data, thread_data_deleter> data{tmp};
325 auto indices = detail::get_indices<std::tuple_size<Tuple>::value, 2>();
327 detail::apply_args(std::get<1>(*p), indices, *p);
342template <
class F,
class... Args>
345 using func_and_args = tuple
346 <
thread_data*,
typename decay<F>::type,
typename decay<Args>::type...>;
347 unique_ptr<func_and_args> p(
348 new func_and_args(m_data.get(), std::forward<F>(f), std::forward<Args>(args)...));
351 &thread_proxy<func_and_args>, p.get(),
"riot_cpp_thread");
355 throw std::system_error(
356 std::make_error_code(std::errc::resource_unavailable_try_again),
357 "Failed to create thread.");
365 m_handle = other.m_handle;
367 std::swap(m_data, other.m_data);
C++11 chrono drop in replacement that adds the function now based on ztimer/timex.
implementation of thread::id
bool operator>(thread_id other) noexcept
Comparison operator for thread ids.
thread_id() noexcept
Creates a uninitialized thread id.
bool operator!=(thread_id other) noexcept
Comparison operator for thread ids.
friend std::basic_ostream< T, Traits > & operator<<(std::basic_ostream< T, Traits > &out, thread_id id)
Enable printing of thread ids using output streams.
bool operator>=(thread_id other) noexcept
Comparison operator for thread ids.
bool operator<(thread_id other) noexcept
Comparison operator for thread ids.
bool operator==(thread_id other) noexcept
Comparison operator for thread ids.
bool operator<=(thread_id other) noexcept
Comparison operator for thread ids.
thread_id(kernel_pid_t handle)
Create a thread id from a native handle.
C++11 compliant implementation of thread, however uses the time point from out chrono header instead ...
id get_id() const noexcept
Returns the id of a thread.
bool joinable() const noexcept
Query if the thread is joinable.
thread(const thread &)=delete
Disallow copy constructor.
static unsigned hardware_concurrency() noexcept
Returns the number of concurrent threads supported by the underlying hardware.
native_handle_type native_handle() noexcept
Returns the native handle to a thread.
thread & operator=(const thread &)=delete
Disallow copy assignment operator.
kernel_pid_t native_handle_type
The native handle type is the kernel_pid_t of RIOT.
thread() noexcept
Per default, an uninitialized thread is created.
void join()
Block until the thread finishes.
void swap(thread &t) noexcept
Swap threads.
void detach()
Detaches a thread from its handle and allows it to execute independently.
thread(thread &&t) noexcept
Move constructor.
A time point for timed wait, as clocks from the standard are not available on RIOT.
C++11 condition variable drop in replacement.
int16_t kernel_pid_t
Unique process identifier.
NORETURN void sched_task_exit(void)
Removes thread from scheduler and set status to STATUS_STOPPED.
#define KERNEL_PID_UNDEF
Canonical identifier for an invalid PID.
kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, int flags, thread_task_func_t task_func, void *arg, const char *name)
Creates a new thread.
void thread_yield(void)
Lets current thread yield.
int thread_wakeup(kernel_pid_t pid)
Wakes up a sleeping thread.
static kernel_pid_t thread_getpid(void)
Returns the process ID of the currently running thread.
C++11 mutex drop in replacement.
time_point now()
Returns the current time saved in a time point.
void swap(unique_lock< Mutex > &lhs, unique_lock< Mutex > &rhs) noexcept
Swaps two mutexes.
This deleter prevents our thread data from being destroyed if the thread object is destroyed before t...
void operator()(thread_data *ptr)
Called by the deleter of a thread object to manage the lifetime of the thread internal management dat...
Holds context data for the thread.
#define THREAD_PRIORITY_MAIN
Priority of the main thread.