µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
thread Class Reference

Standard thread. More...

#include <thread_internal.h>

Classes

class  id
 Thread unique id. More...
 

Public Types

using native_handle_type = os::rtos::thread *
 

Public Member Functions

 thread () noexcept=default
 
template<typename Callable_T , typename... Args_T>
 thread (Callable_T &&f, Args_T &&... args)
 
 thread (const thread &)=delete
 
template<typename F , typename... Args>
 thread (F &&f, Args &&... args)
 
 thread (thread &&t) noexcept
 
 ~thread ()
 
void detach (void)
 
id get_id (void) const noexcept
 
void join (void)
 
bool joinable (void) const noexcept
 
native_handle_type native_handle ()
 
threadoperator= (const thread &)=delete
 
threadoperator= (thread &&t) noexcept
 
void swap (thread &t) noexcept
 

Static Public Member Functions

static unsigned hardware_concurrency (void) noexcept
 

Private Types

using function_object_deleter_t = void(*)(void *)
 

Private Member Functions

void delete_system_thread (void)
 

Static Private Member Functions

template<typename F_T >
static void delete_function_object (const void *func_obj)
 
template<typename F_T >
static void run_function_object (const void *func_object)
 

Private Attributes

function_object_deleter_t function_object_deleter_ = nullptr
 
id id_
 

Detailed Description

Definition at line 39 of file thread_internal.h.

Member Typedef Documentation

◆ function_object_deleter_t

using thread::function_object_deleter_t = void (*) (void*)
private

Definition at line 146 of file thread_internal.h.

◆ native_handle_type

Definition at line 42 of file thread_internal.h.

Constructor & Destructor Documentation

◆ thread() [1/5]

thread::thread ( )
defaultnoexcept

◆ thread() [2/5]

template<typename F , typename... Args>
thread::thread ( F &&  f,
Args &&...  args 
)
explicit

◆ ~thread()

thread::~thread ( )

Definition at line 51 of file thread-cpp.h.

52{
53 os::trace::printf ("%s() @%p\n", __func__, this);
54 if (joinable ())
55 {
56 os::trace::printf ("%s() @%p attempt to destruct a running thread\n",
57 __func__, this);
58 std::abort (); // in ISO it is std::terminate()
59 }
60
62}
void delete_system_thread(void)
Definition thread-cpp.h:35
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59

References delete_system_thread(), joinable(), and os::trace::printf().

◆ thread() [3/5]

thread::thread ( const thread )
delete

◆ thread() [4/5]

thread::thread ( thread &&  t)
noexcept

Definition at line 16 of file thread-cpp.h.

17{
18 swap (t);
19}
void swap(thread &x, thread &y) noexcept

References swap().

◆ thread() [5/5]

template<typename Callable_T , typename... Args_T>
thread::thread ( Callable_T &&  f,
Args_T &&...  args 
)

Definition at line 344 of file thread_internal.h.

345{
346 // static_assert(std::is_same<Attr_T, os::rtos::thread::attr_t>::value,
347 // "first param must be thread_attr_t*");
348
349 os::trace::printf ("%s() @%p\n", __PRETTY_FUNCTION__, this);
350
351 using Function_object = decltype (std::bind (
352 std::forward<Callable_T> (f), std::forward<Args_T> (args)...));
353
354 // Dynamic allocation! The size depends on the number of arguments.
355 // This creates a small problem, since both running the function
356 // and deleting the object requires the type. It is passes as
357 // template functions.
358 Function_object* funct_obj = new Function_object (std::bind (
359 std::forward<Callable_T> (f), std::forward<Args_T> (args)...));
360
361 // The function to start the thread is a custom proxy that
362 // knows how to get the variadic arguments.
363#pragma GCC diagnostic push
364#if defined(__clang__)
365#pragma clang diagnostic ignored "-Wcast-function-type"
366#elif defined(__GNUC__)
367#pragma GCC diagnostic ignored "-Wcast-function-type"
368#endif
369 id_ = id{ new os::rtos::thread (
370 reinterpret_cast<os::rtos::thread::func_t> (
371 &run_function_object<Function_object>),
372 reinterpret_cast<os::rtos::thread::func_args_t> (funct_obj)) };
373#pragma GCC diagnostic pop
374
375#pragma GCC diagnostic push
376#if defined(__clang__)
377#pragma clang diagnostic ignored "-Wcast-function-type-strict"
378#endif
379 // The deleter, to be used during destruction.
381 &delete_function_object<Function_object>);
382#pragma GCC diagnostic pop
383}
POSIX compliant thread, using the default RTOS allocator.
Definition os-thread.h:251
void *(*)(func_args_t args) func_t
Type of thread function.
Definition os-thread.h:423
_func_args_t func_args_t
Type of thread function arguments.
Definition os-thread.h:414
void(*)(void *) function_object_deleter_t
function_object_deleter_t function_object_deleter_

Member Function Documentation

◆ delete_function_object()

template<typename F_T >
void thread::delete_function_object ( const void *  func_obj)
staticprivate

Definition at line 325 of file thread_internal.h.

326{
327 os::trace::printf ("%s()\n", __PRETTY_FUNCTION__);
328
329 using Function_object = F_T;
330 const Function_object* f = static_cast<const Function_object*> (func_obj);
331
332 // The delete now has the knowledge required to
333 // correctly delete the object (i.e. the object size).
334 delete f;
335}

◆ delete_system_thread()

void thread::delete_system_thread ( void  )
private

Definition at line 35 of file thread-cpp.h.

36{
37 if (id_ != id ())
38 {
39 void* args = id_.native_thread_->function_args ();
40 if (args != nullptr && function_object_deleter_ != nullptr)
41 {
42 // Manually delete the function object used to store arguments.
44 }
45
46 // Manually delete the system thread.
47 delete id_.native_thread_;
48 }
49}
void * function_args(void) const
Get the thread function arguments.
Definition os-thread.h:2364
native_handle_type native_thread_

References os::rtos::thread::function_args(), function_object_deleter_, id_, and thread::id::native_thread_.

Referenced by ~thread(), and join().

◆ detach()

void thread::detach ( void  )

Definition at line 91 of file thread-cpp.h.

92{
93 os::trace::printf ("%s() @%p\n", __func__, this);
94 if (id_ != id ())
95 {
97 }
98
99 // The detached thread will continue to run, but we'll not have
100 // access to it from here, not even to delete it.
101 // TODO: arrange to delete it at exit()?
102
103 id_ = id ();
104 os::trace::printf ("%s() @%p detached\n", __func__, this);
105}
result_t detach(void)
Detach a thread.

References os::rtos::thread::detach(), id_, thread::id::native_thread_, and os::trace::printf().

◆ get_id()

thread::id thread::get_id ( void  ) const
inlinenoexcept

Definition at line 295 of file thread_internal.h.

296{
297 return id_;
298}

◆ hardware_concurrency()

unsigned thread::hardware_concurrency ( void  )
inlinestaticnoexcept

Definition at line 307 of file thread_internal.h.

308{
309 return 1;
310}

◆ join()

void thread::join ( void  )

Definition at line 80 of file thread-cpp.h.

81{
82 os::trace::printf ("%s() @%p\n", __func__, this);
83
85
86 id_ = id ();
87 os::trace::printf ("%s() @%p joined\n", __func__, this);
88}

References delete_system_thread(), id_, and os::trace::printf().

◆ joinable()

bool thread::joinable ( void  ) const
noexcept

Definition at line 74 of file thread-cpp.h.

75{
76 return !(id_ == thread::id ());
77}
Thread unique id.

References id_.

Referenced by ~thread().

◆ native_handle()

thread::native_handle_type thread::native_handle ( )
inline

Definition at line 301 of file thread_internal.h.

302{
303 return id_.native_thread_;
304}

◆ operator=() [1/2]

thread & thread::operator= ( const thread )
delete

◆ operator=() [2/2]

thread & thread::operator= ( thread &&  t)
noexcept

Definition at line 22 of file thread-cpp.h.

23{
24 if (joinable ())
25 {
26 os::trace::printf ("%s() @%p attempt to assign a running thread\n",
27 __func__, this);
28 std::abort (); // in ISO it is std::terminate()
29 }
30 swap (t);
31 return *this;
32}

References os::trace::printf(), and swap().

◆ run_function_object()

template<typename F_T >
void thread::run_function_object ( const void *  func_object)
staticprivate

Definition at line 314 of file thread_internal.h.

315{
316 os::trace::printf ("%s()\n", __PRETTY_FUNCTION__);
317
318 using Function_object = F_T;
319 const Function_object* f = static_cast<const Function_object*> (func_obj);
320 (*f) ();
321}

◆ swap()

void thread::swap ( thread t)
noexcept

Definition at line 67 of file thread-cpp.h.

68{
69 std::swap (id_, t.id_);
70 std::swap (function_object_deleter_, t.function_object_deleter_);
71}

Member Data Documentation

◆ function_object_deleter_

function_object_deleter_t thread::function_object_deleter_ = nullptr
private

Definition at line 147 of file thread_internal.h.

Referenced by delete_system_thread().

◆ id_

id thread::id_
private

Definition at line 144 of file thread_internal.h.

Referenced by delete_system_thread(), detach(), join(), and joinable().


The documentation for this class was generated from the following files: