28 #ifndef CMSIS_PLUS_RTOS_OS_THREAD_H_ 29 #define CMSIS_PLUS_RTOS_OS_THREAD_H_ 33 #if defined(__cplusplus) 39 #if !defined(__ARM_EABI__) 52 using _func_args_t =
void*;
118 exit (
void* exit_ptr =
nullptr);
221 #pragma GCC diagnostic push 222 #pragma GCC diagnostic ignored "-Wpadded" 278 static constexpr uint32_t range = 4;
300 lowest = (2 << range),
304 below_normal = (4 << range),
309 normal = (6 << range),
311 above_normal = (8 << range),
313 high = (10 << range),
315 realtime = (12 << range),
320 highest = (((13 + 1) << range) - 1),
325 isr = (((14 + 1) << range) - 1),
330 error = (((15 + 1) << range) - 1)
426 static const element_t magic = os::rtos::port::stack::magic;
446 operator= (
const stack&) =
delete;
448 operator= (
stack&&) =
delete;
537 check_bottom_magic (
void);
547 check_top_magic (
void);
584 min_size (std::size_t size_bytes);
601 default_size (std::size_t size_bytes);
616 std::size_t size_bytes_;
618 static std::size_t min_size_bytes_;
619 static std::size_t default_size_bytes_;
655 operator= (
const context&) =
delete;
657 operator= (
context&&) =
delete;
710 #if !defined(OS_USE_RTOS_PORT_SCHEDULER) 714 friend port::stack::element_t*
742 #if !defined(OS_USE_RTOS_PORT_SCHEDULER) 747 port::thread_context_t port_;
762 #pragma GCC diagnostic push 763 #pragma GCC diagnostic ignored "-Wpadded" 788 attributes (
const attributes&) =
default;
789 attributes (attributes&&) =
default;
791 operator= (
const attributes&) =
default;
793 operator= (attributes&&) =
default;
798 ~attributes () =
default;
818 void* th_stack_address =
nullptr;
831 std::size_t th_stack_size_bytes = 0;
852 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) \ 853 || defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) 907 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) 921 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) 945 rtos::scheduler::internal_switch_threads (
void);
947 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) 951 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) 963 #pragma GCC diagnostic pop 1013 thread (
const char* name);
1029 operator= (
const thread&) =
delete;
1031 operator= (
thread&&) =
delete;
1098 join (
void** exit_ptr =
nullptr);
1140 priority_inherited (
void);
1145 set_cancel_state (
int,
int*);
1147 set_cancel_type (
int,
int*);
1150 get_sched_param (
int*,
struct sched_param*);
1152 set_sched_param (
int,
const struct sched_param*);
1174 interrupt (
bool interrupt =
true);
1201 function_args (
void)
const;
1203 #if defined(OS_INCLUDE_RTOS_CUSTOM_THREAD_USER_STORAGE) || defined(__DOXYGEN__) 1211 os_thread_user_storage_t*
1212 user_storage (
void);
1228 #if defined(OS_INCLUDE_RTOS_THREAD_PUBLIC_FLAGS_CLEAR) 1255 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) \ 1256 || defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) 1315 scheduler::internal_link_node (
1322 scheduler::internal_unlink_node (
1327 scheduler::internal_switch_threads (
void);
1335 friend port::stack::element_t*
1381 std::size_t stack_size_bytes);
1391 internal_suspend_ (
void);
1400 internal_exit_ (
void* exit_ptr =
nullptr);
1515 internal_destroy_ (
void);
1524 internal_relink_running_ (
void);
1533 internal_check_stack_ (
void);
1560 void* func_result_ =
nullptr;
1563 thread* parent_ =
nullptr;
1574 threads_list children_
1583 thread* joiner_ =
nullptr;
1594 clock* clock_ =
nullptr;
1599 const void* allocator_ =
nullptr;
1603 std::size_t allocated_stack_size_elements_ = 0;
1606 std::size_t
volatile acquired_mutexes_ = 0;
1616 state_t volatile state_ = state::undefined;
1631 priority_t volatile prio_assigned_ = priority::none;
1632 priority_t volatile prio_inherited_ = priority::none;
1634 bool volatile interrupted_ =
false;
1638 #if defined(OS_INCLUDE_RTOS_CUSTOM_THREAD_USER_STORAGE) || defined(__DOXYGEN__) 1639 os_thread_user_storage_t user_storage_;
1642 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) 1651 #if defined(OS_USE_RTOS_PORT_SCHEDULER) 1653 os_thread_port_data_t port_;
1676 template<
typename Allocator = memory::allocator<
void*>>
1752 internal_destroy_ (
void)
override;
1771 template<std::
size_t N = port::stack::default_size_bytes>
1779 static const std::size_t stack_size_bytes = N;
1837 stack::allocation_element_t stack_[(stack_size_bytes
1838 +
sizeof(stack::allocation_element_t) - 1)
1839 /
sizeof(stack::allocation_element_t)];
1847 #pragma GCC diagnostic pop 2053 __attribute__ ((always_inline))
2088 bottom_address_ =
nullptr;
2100 assert (size_bytes >= min_size_bytes_);
2101 bottom_address_ = address;
2102 size_bytes_ = size_bytes;
2113 return bottom_address_;
2124 return bottom_address_ + (size_bytes_ /
sizeof(
element_t));
2146 return *bottom () == stack::magic;
2157 return *top () == stack::magic;
2168 return min_size_bytes_;
2179 std::size_t tmp = min_size_bytes_;
2180 min_size_bytes_ = size_bytes;
2192 return default_size_bytes_;
2203 assert (size_bytes != 0);
2204 assert (size_bytes >= min_size_bytes_);
2206 std::size_t tmp = default_size_bytes_;
2207 default_size_bytes_ = size_bytes;
2237 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) 2256 return context_switches_;
2261 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) 2295 return this == &rhs;
2328 return interrupted_;
2331 #if defined(OS_INCLUDE_RTOS_CUSTOM_THREAD_USER_STORAGE) || defined(__DOXYGEN__) 2350 inline os_thread_user_storage_t*
2353 return &user_storage_;
2358 #if !defined(OS_USE_RTOS_PORT_SCHEDULER) 2365 thread::internal_relink_running_ (
void)
2367 if (state_ == state::running)
2372 if (crt_node.
next () ==
nullptr)
2374 rtos::scheduler::ready_threads_list_.link (crt_node);
2380 assert (
stack ().check_bottom_magic ());
2398 return context_.stack_;
2401 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) 2416 #if defined(OS_INCLUDE_RTOS_THREAD_PUBLIC_FLAGS_CLEAR) 2421 return internal_flags_clear_ (mask, oflags);
2469 template<
typename Allocator>
2475 {
nullptr,
function, args, attr, allocator }
2521 template<
typename Allocator>
2528 #if defined(OS_TRACE_RTOS_THREAD) 2531 if (attr.th_stack_address !=
nullptr 2534 internal_construct_ (
function, args, attr,
nullptr, 0);
2542 allocated_stack_size_elements_ = (attr.th_stack_size_bytes
2555 allocated_stack_address_ =
2557 allocated_stack_size_elements_));
2559 assert (allocated_stack_address_ !=
nullptr);
2561 internal_construct_ (
2565 allocated_stack_address_,
2566 allocated_stack_size_elements_
2575 template<
typename Allocator>
2579 #if defined(OS_TRACE_RTOS_THREAD) 2580 trace::printf (
"thread_allocated::%s() @%p %s\n", __func__,
this,
2584 if (allocated_stack_address_ !=
nullptr)
2586 internal_check_stack_ ();
2588 typedef typename std::allocator_traits<allocator_type>::pointer pointer;
2591 reinterpret_cast<pointer> (allocated_stack_address_),
2592 allocated_stack_size_elements_);
2594 allocated_stack_address_ =
nullptr;
2597 thread::internal_destroy_ ();
2617 template<
typename Allocator>
2620 #if defined(OS_TRACE_RTOS_THREAD) 2673 template<std::
size_t N>
2678 {
nullptr,
function, args, attr }
2729 template<std::
size_t N>
2736 #if defined(OS_TRACE_RTOS_THREAD) 2753 template<std::
size_t N>
2756 #if defined(OS_TRACE_RTOS_THREAD)
static const attributes initializer
Default thread initialiser.
void os_rtos_idle_actions(void)
static std::size_t default_size(void)
Get the default stack size.
bool check_top_magic(void)
Check if top magic word is still there.
void * function_args(void) const
Get the thread function arguments.
class thread::stack * stack(void)
Get the interrupts stack.
void start(void)
Start the RTOS scheduler.
virtual ~thread_allocated() override
Destruct the thread object instance.
void yield(void)
Yield execution to the next ready thread.
void exit(void *exit_ptr=nullptr)
Terminate the current running thread.
os_thread_user_storage_t * user_storage(void)
Get the user storage.
void deallocate(value_type *addr, std::size_t elements) noexcept
Deallocate the number of memory blocks of type value_type.
Priority ordered list of threads.
Ask for flags to be cleared after read.
Double linked list node, with thread reference.
uint8_t state_t
Type of variables holding thread states.
int * __errno(void)
Implementation of the library __errno() function.
stack::element_t * top(void)
Get the top stack address.
void clear(void)
Clear the stack pointer and size.
Base class for attributes.
std::size_t size(void)
Get the stack size.
Template of a POSIX compliant thread with local stack.
Double linked list node, with time stamp and thread.
void set(stack::element_t *address, std::size_t size_bytes)
Set the stack address and size.
os::rtos::port::stack::element_t element_t
Type of a stack element.
stack::element_t * switch_stacks(stack::element_t *sp)
result_t flags_try_wait(flags::mask_t mask, flags::mask_t *oflags=nullptr, flags::mode_t mode=flags::mode::all|flags::mode::clear)
Try to wait for thread event flags.
bool operator==(thread::id x, thread::id y) noexcept
stack::element_t * bottom(void)
Get the stack lowest reserved address.
virtual ~thread_inclusive()
Destruct the thread object instance.
constexpr attributes()
Construct a thread attributes object instance.
Internal event flags implementation.
rtos::statistics::duration_t cpu_cycles(void)
Get the thread execution time.
os::rtos::port::stack::allocation_element_t allocation_element_t
Type of a stack allocation element.
const char * name(void) const
Get object name.
Base class for named system objects.
uint8_t priority_t
Type of variables holding thread priorities.
context()
Construct a thread context object instance.
Priority ordered list of threads waiting too run.
A namespace for functions applying to the current thread.
POSIX compliant thread, using the default RTOS allocator.
port::clock::duration_t duration_t
Type of variables holding clock durations.
The core of a double linked list, pointers to next, previous.
POSIX compliant condition variable.
stack()
Construct a thread stack object instance.
thread_allocated(func_t function, func_args_t args, const attributes &attr=initializer, const allocator_type &allocator=allocator_type())
Construct a thread object instance.
Return when all flags are set.
rtos::statistics::counter_t context_switches(void)
Get the total number of context switches.
state_t state(void) const
Get thread scheduler state.
class thread::statistics & statistics(void)
flags::mask_t flags_get(flags::mask_t mask, flags::mode_t mode=flags::mode::all|flags::mode::clear)
Get/clear thread event flags.
int printf(const char *format,...)
Write a formatted string to the trace device.
class thread::stack & stack(void)
Get the thread context stack.
Template of a POSIX compliant thread with allocator.
static const std::size_t stack_size_bytes
Local constant based on template definition.
thread::stack & stack(void)
Get the associated stack.
void suspend(void)
Suspend the current running thread to wait for an event.
thread_inclusive(func_t function, func_args_t args, const attributes &attr=initializer)
Construct a thread object instance.
bool check_bottom_magic(void)
Check if bottom magic word is still there.
rtos::statistics::counter_t context_switches(void)
Get the number of thread context switches.
T value_type
Type of elements to be allocated.
Unordered list of threads.
static_double_list_links * next(void) const
static std::size_t min_size(void)
Get the min stack size.
result_t flags_clear(flags::mask_t mask, flags::mask_t *oflags=nullptr)
Clear thread event flags.
result_t flags_timed_wait(flags::mask_t mask, clock::duration_t timeout, flags::mask_t *oflags=nullptr, flags::mode_t mode=flags::mode::all|flags::mode::clear)
Timed wait for thread event flags.
uint64_t counter_t
Type of variables holding context switches counters.
Circular double linked list of nodes.
Ordered list of time stamp nodes.
void *(*)(func_args_t args) func_t
Type of thread function.
thread & thread(void)
Get the current running thread.
uint32_t mask_t
Type of variables holding flags masks.
List of children threads.
uint32_t mode_t
Type of variables holding flags modes.
result_t flags_wait(flags::mask_t mask, flags::mask_t *oflags=nullptr, flags::mode_t mode=flags::mode::all|flags::mode::clear)
Wait for thread event flags.
uint32_t result_t
Type of values returned by RTOS functions.
bool operator==(const thread &rhs) const
Compare threads.
allocator_stateless_default_resource< T > allocator
Type of allocator used by the system objects. Must be stateless.
Standard allocator based on the RTOS system default memory manager.
_func_args_t func_args_t
Type of thread function arguments.
bool interrupted(void)
Check if interrupted.
uint64_t duration_t
Type of variables holding durations in CPU cycles.
int kill(pid_t pid, int sig)
rtos::statistics::duration_t cpu_cycles(void)
Get the total duration of all threads.