Go to the documentation of this file.
24 #ifndef RIOT_THREAD_HPP
25 #define RIOT_THREAD_HPP
38 #include <type_traits>
63 thread_data() : ref_count{2}, joining_thread{thread_uninitialized} {
67 std::atomic<unsigned> ref_count;
69 std::array<char, stack_size> stack;
83 if (--ptr->ref_count == 0) {
96 template <
class T,
class Traits>
97 friend std::basic_ostream<T, Traits>&
operator<<(std::basic_ostream
106 inline thread_id() noexcept : m_handle{thread_uninitialized} {}
116 return m_handle == other.m_handle;
122 return !(m_handle == other.m_handle);
128 return m_handle < other.m_handle;
134 return !(m_handle > other.m_handle);
140 return m_handle > other.m_handle;
146 return !(m_handle < other.m_handle);
156 template <
class T,
class Traits>
157 inline std::basic_ostream<T, Traits>&
operator<<(std::basic_ostream
160 return out <<
id.m_handle;
163 namespace this_thread {
177 void sleep_for(
const std::chrono::nanoseconds& ns);
182 template <
class Rep,
class Period>
183 void sleep_for(
const std::chrono::duration<Rep, Period>& sleep_duration) {
184 using namespace std::chrono;
185 if (sleep_duration > std::chrono::duration<Rep, Period>::zero()) {
186 constexpr std::chrono::duration<long double> max = nanoseconds::max();
188 if (sleep_duration < max) {
189 ns = duration_cast<nanoseconds>(sleep_duration);
190 if (ns < sleep_duration) {
194 ns = nanoseconds::max();
235 inline thread() noexcept : m_handle{thread_uninitialized} {}
241 template <
class F,
class... Args>
242 explicit thread(F&& f, Args&&... args);
247 thread(
const thread&) =
delete;
253 t.m_handle = thread_uninitialized;
254 std::swap(m_data, t.m_data);
262 thread& operator=(
const thread&) =
delete;
267 thread& operator=(thread&&) noexcept;
274 std::swap(m_data, t.m_data);
275 std::swap(m_handle, t.m_handle);
283 return m_handle != thread_uninitialized;
310 static unsigned hardware_concurrency() noexcept;
325 template <class Tuple>
326 void* thread_proxy(
void* vp) {
328 std::unique_ptr<Tuple> p(
static_cast<Tuple*
>(vp));
329 auto tmp = std::get<0>(*p);
330 std::unique_ptr<thread_data, thread_data_deleter> data{tmp};
332 auto indices = detail::get_indices<std::tuple_size<Tuple>::value, 2>();
334 detail::apply_args(std::get<1>(*p), indices, *p);
339 if (data->joining_thread != thread_uninitialized) {
349 template <
class F,
class... Args>
353 using func_and_args = tuple
354 <
thread_data*,
typename decay<F>::type,
typename decay<Args>::type...>;
355 unique_ptr<func_and_args> p(
356 new func_and_args(m_data.get(), forward<F>(f), forward<Args>(args)...));
359 &thread_proxy<func_and_args>, p.get(),
"riot_cpp_thread");
363 throw std::system_error(
364 std::make_error_code(std::errc::resource_unavailable_try_again),
365 "Failed to create thread.");
370 if (m_handle != thread_uninitialized) {
373 m_handle = other.m_handle;
374 other.m_handle = thread_uninitialized;
375 std::swap(m_data, other.m_data);
383 #endif // RIOT_THREAD_HPP
void operator()(thread_data *ptr)
Called by the deleter of a thread object to manage the lifetime of the thread internal management dat...
int16_t kernel_pid_t
Unique process identifier.
native_handle_type native_handle() noexcept
Returns the native handle to a thread.
int thread_wakeup(kernel_pid_t pid)
Wakes up a sleeping thread.
std::basic_ostream< T, Traits > & operator<<(std::basic_ostream< T, Traits > &out, thread_id id)
Enable printing of thread ids using output streams.
thread_id() noexcept
Creates a uninitialized thread id.
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.
This deleter prevents our thread data from being destroyed if the thread object is destroyed before t...
implementation of thread::id
void sleep_for(const std::chrono::nanoseconds &ns)
Puts the current thread to sleep.
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.
static kernel_pid_t thread_getpid(void)
Returns the process ID of the currently running thread.
void yield() noexcept
Yield the currently running thread.
NORETURN void sched_task_exit(void)
Removes thread from scheduler and set status to STATUS_STOPPED.
void swap(unique_lock< Mutex > &lhs, unique_lock< Mutex > &rhs) noexcept
Swaps two mutexes.
C++11 compliant implementation of unique lock.
void thread_yield(void)
Lets current thread yield.
C++11 chrono drop in replacement that adds the function now based on xtimer/timex.
time_point now()
Returns the current time saved in a time point.
thread(thread &&t) noexcept
Move constructor.
id get_id() const noexcept
Returns the id of a thread.
A time point for timed wait, as clocks from the standard are not available on RIOT.
bool operator>=(thread_id other) noexcept
Comparison operator for thread ids.
C++11 condition variable drop in replacement.
thread & operator=(const thread &)=delete
Disallow copy assignment operator.
#define THREAD_STACKSIZE_MAIN
Size of the main task's stack in bytes.
bool operator>(thread_id other) noexcept
Comparison operator for thread ids.
C++11 compliant implementation of mutex, uses the time point implemented in our chrono replacement in...
Holds context data for the thread.
bool operator!=(thread_id other) noexcept
Comparison operator for thread ids.
bool joinable() const noexcept
Query if the thread is joinable.
cv_status wait_until(unique_lock< mutex > &lock, const time_point &timeout_time)
Block until woken up through the condition variable or a specified point in time is reached.
thread() noexcept
Per default, an uninitialized thread is created.
void sleep_until(const riot::time_point &sleep_time)
Puts the current thread to sleep.
C++11 compliant implementation of thread, however uses the time point from out chrono header instead ...
C++11 compliant implementation of condition variable, uses the time point implemented in our chrono r...
thread_id get_id() noexcept
Access the id of the currently running thread.
#define THREAD_PRIORITY_MAIN
Priority of the main thread.
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 mutex drop in replacement.
kernel_pid_t native_handle_type
The native handle type is the kernel_pid_t of RIOT.