µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
os::rtos::thread_inclusive< N > Class Template Reference

Template of a POSIX compliant thread with local stack. More...

#include <cmsis-plus/rtos/os.h>

+ Inheritance diagram for os::rtos::thread_inclusive< N >:

Public Types

using allocator_type = memory::allocator< stack::allocation_element_t >
 Default RTOS allocator.
 
using func_args_t = _func_args_t
 Type of thread function arguments.
 
using func_t = void *(*)(func_args_t args)
 Type of thread function.
 
using priority_t = uint8_t
 Type of variables holding thread priorities.
 
using state_t = uint8_t
 Type of variables holding thread states.
 

Public Member Functions

Constructors & Destructor
 thread_inclusive (func_t function, func_args_t args, const attributes &attr=initializer)
 Construct a thread object instance.
 
 thread_inclusive (const char *name, func_t function, func_args_t args, const attributes &attr=initializer)
 Construct a named thread object instance.
 
virtual ~thread_inclusive () override
 Destruct the thread object instance.
 
Operators
bool operator== (const thread &rhs) const
 Compare threads.
 
Public Member Functions
result_t cancel (void)
 Cancel thread execution.
 
result_t detach (void)
 Detach a thread.
 
result_t join (void **exit_ptr=nullptr)
 Wait for thread termination.
 
result_t priority (priority_t prio)
 Set the assigned scheduling priority.
 
priority_t priority (void)
 Get the current scheduling priority.
 
result_t priority_inherited (priority_t prio)
 Set the inherited scheduling priority.
 
priority_t priority_inherited (void)
 Get the inherited scheduling priority.
 
bool interrupted (void)
 Check if interrupted.
 
bool interrupt (bool interrupt=true)
 Set the interrupt flag, possibly interrupting the thread.
 
state_t state (void) const
 Get thread scheduler state.
 
void resume (void)
 Resume the thread.
 
void * function_args (void) const
 Get the thread function arguments.
 
os_thread_user_storage_t * user_storage (void)
 Get the user storage.
 
result_t flags_raise (flags::mask_t mask, flags::mask_t *oflags=nullptr)
 Raise thread event flags.
 
result_t kill (void)
 Force thread termination.
 
thread::stackstack (void)
 Get the thread context stack.
 
thread::statisticsstatistics (void)
 
Public Member Functions
const char * name (void) const
 Get object name.
 

Static Public Member Functions

static bool is_constructed (const thread &thread)
 Check if the thread is constructed.
 
Operators
static void * operator new (std::size_t bytes)
 Allocate space for a new object instance using the RTOS system allocator.
 
static void * operator new (std::size_t bytes, void *ptr)
 Emplace a new object instance.
 
static void * operator new[] (std::size_t bytes)
 Allocate space for an array of new object instances using the RTOS system allocator.
 
static void * operator new[] (std::size_t bytes, void *ptr)
 Emplace an array of new object instances.
 
static void operator delete (void *ptr, std::size_t bytes)
 Deallocate the dynamically allocated object instance. using the RTOS system allocator.
 
static void operator delete[] (void *ptr, std::size_t bytes)
 Deallocate the dynamically allocated array of object. instances using the RTOS system allocator.
 

Public Attributes

int errno_ = 0
 

Static Public Attributes

static const attributes initializer
 Default thread initialiser.
 
static const std::size_t stack_size_bytes = N
 Local constant based on template definition.
 

Detailed Description

template<std::size_t N = port::stack::default_size_bytes>
class os::rtos::thread_inclusive< N >
Template Parameters
NSize of statically allocated stack in bytes.

Definition at line 1824 of file os-thread.h.

Member Typedef Documentation

◆ allocator_type

using os::rtos::thread::allocator_type = memory::allocator<stack::allocation_element_t>
inherited

Definition at line 1018 of file os-thread.h.

◆ func_args_t

using os::rtos::thread::func_args_t = _func_args_t
inherited

Useful to cast other similar types to silence possible compiler warnings.

Definition at line 414 of file os-thread.h.

◆ func_t

using os::rtos::thread::func_t = void* (*)(func_args_t args)
inherited

Useful to cast other similar types to silence possible compiler warnings.

Definition at line 423 of file os-thread.h.

◆ state_t

using os::rtos::thread::state_t = uint8_t
inherited

Definition at line 357 of file os-thread.h.

Constructor & Destructor Documentation

◆ thread_inclusive() [1/2]

template<std::size_t N>
os::rtos::thread_inclusive< N >::thread_inclusive ( func_t  function,
func_args_t  args,
const attributes attr = initializer 
)
inline
Parameters
[in]functionPointer to thread function.
[in]argsPointer to thread function arguments.
[in]attrReference to attributes.

This constructor shall initialise a thread object with attributes referenced by attr. If the attributes specified by attr are modified later, the thread attributes shall not be affected. Upon successful initialisation, the state of the thread object shall become initialised, and the thread is added to the ready list.

Only the thread object itself may be used for running the function. It is not allowed to make copies of thread objects.

In cases where default thread attributes are appropriate, the variable thread::initializer can be used to initialise threads. The effect shall be equivalent to creating a thread object with the default constructor.

The thread is created to execute function with args as its sole argument. If the function returns, the effect shall be as if there was an implicit call to exit() using the return value of function as the exit code. Note that the thread in which main() was originally invoked differs from this. When it returns from main(), the effect shall be as if there was an implicit call to exit() using the return value of main() as the exit code.

The storage shall be statically allocated inside the thread object instance.

Note
These objects are better instantiated as global static objects. When instantiated on the thread stack, the stack should be sized accordingly.

Implemented as a wrapper over the parent constructor, automatically passing the stack size and address.

POSIX compatibility
Inspired by pthread_create() from <pthread.h> (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 2736 of file os-thread.h.

2739 : thread_inclusive<N>{ nullptr, function, args, attr }
2740 {
2741 }

◆ thread_inclusive() [2/2]

template<std::size_t N>
os::rtos::thread_inclusive< N >::thread_inclusive ( const char *  name,
func_t  function,
func_args_t  args,
const attributes attr = initializer 
)
Parameters
[in]namePointer to name.
[in]functionPointer to thread function.
[in]argsPointer to thread function arguments.
[in]attrReference to attributes.

This constructor shall initialise a named thread object with attributes referenced by attr. If the attributes specified by attr are modified later, the thread attributes shall not be affected. Upon successful initialisation, the state of the thread object shall become initialised, and the thread is added to the ready list.

Only the thread object itself may be used for running the function. It is not allowed to make copies of thread objects.

In cases where default thread attributes are appropriate, the variable thread::initializer can be used to initialise threads. The effect shall be equivalent to creating a thread object with the default constructor.

The thread is created to execute function with args as its sole argument. If the function returns, the effect shall be as if there was an implicit call to exit() using the return value of function as the exit code. Note that the thread in which main() was originally invoked differs from this. When it returns from main(), the effect shall be as if there was an implicit call to exit() using the return value of main() as the exit code.

The storage shall be statically allocated inside the thread object instance.

Note
These objects are better instantiated as global static objects. When instantiated on the thread stack, the stack should be sized accordingly.

Implemented as a wrapper over the parent constructor, automatically passing the stack size and address.

POSIX compatibility
Inspired by pthread_create() from <pthread.h> (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 2793 of file os-thread.h.

2796 : thread{ name }
2797 {
2798#if defined(OS_TRACE_RTOS_THREAD)
2799 trace::printf ("%s @%p %s\n", __func__, this, this->name ());
2800#endif
2801 internal_construct_ (function, args, attr, &stack_, stack_size_bytes);
2802 }
const char * name(void) const
Get object name.
Definition os-decls.h:753
static const std::size_t stack_size_bytes
Local constant based on template definition.
Definition os-thread.h:1830
Standard thread.
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59

References os::trace::printf(), and os::rtos::thread_inclusive< N >::stack_size_bytes.

◆ ~thread_inclusive()

template<std::size_t N>
os::rtos::thread_inclusive< N >::~thread_inclusive
overridevirtual

This destructor shall destroy the thread object; the object becomes, in effect, uninitialised. An implementation may cause the destructor to set the object to an invalid value.

POSIX compatibility
No POSIX similar functionality identified.
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 2816 of file os-thread.h.

2817 {
2818#if defined(OS_TRACE_RTOS_THREAD)
2819 trace::printf ("%s @%p %s\n", __func__, this, name ());
2820#endif
2821 }

References os::trace::printf().

Member Function Documentation

◆ cancel()

result_t thread::cancel ( void  )
inherited
Parameters
None.
Return values
result::okThe cancel request was sent to the thread.
EPERMCannot be invoked from an Interrupt Service Routines.

The cancel() function shall not return an error code of EINTR. If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an ESRCH error. error number is returned.

Todo:
Implement it properly (thread interruption is not yet fully implemented).
POSIX compatibility
Inspired by pthread_cancel() from <pthread.h> (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 995 of file os-thread.cpp.

996 {
997#if defined(OS_TRACE_RTOS_THREAD)
998 trace::printf ("%s() @%p %s\n", __func__, this, name ());
999#endif
1000
1001 // Don't call this from interrupt handlers.
1003
1004 // TODO: implement according to POSIX specs.
1005 return result::ok;
1006 }
bool in_handler_mode(void)
Check if the CPU is in handler mode.
Definition os-sched.h:1101
@ ok
Function completed; no errors or events occurred.
Definition os-decls.h:179
#define os_assert_err(__e, __er)
Assert or return an error.
Definition os-decls.h:1101

References os::rtos::interrupts::in_handler_mode(), os::rtos::internal::object_named::name(), os::rtos::result::ok, os_assert_err, and os::trace::printf().

◆ detach()

result_t thread::detach ( void  )
inherited
Parameters
None.
Return values
result::okThe thread was detached.
EPERMCannot be invoked from an Interrupt Service Routines.

Indicate to the implementation that storage for the thread thread can be reclaimed when that thread terminates. If thread has not terminated, detach() shall not cause it to terminate. The behaviour is undefined if the value specified by the thread argument to detach() does not refer to a joinable thread.

POSIX compatibility
Inspired by pthread_detach() from <pthread.h> (IEEE Std 1003.1, 2013 Edition).

The detach() function shall not return an error code of EINTR.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 883 of file os-thread.cpp.

884 {
885#if defined(OS_TRACE_RTOS_THREAD)
886 trace::printf ("%s() @%p %s\n", __func__, this, name ());
887#endif
888
889 // Don't call this from interrupt handlers.
891
892#if defined(OS_USE_RTOS_PORT_SCHEDULER)
893
894 result_t res = port::thread::detach (this);
895 if (res != result::ok)
896 {
897 return res;
898 }
899
900#else
901
902 // TODO: implement
903
904#endif
905
906 return result::ok;
907 }
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:95

References os::rtos::interrupts::in_handler_mode(), os::rtos::internal::object_named::name(), os::rtos::result::ok, os_assert_err, and os::trace::printf().

Referenced by thread::detach().

◆ flags_raise()

result_t thread::flags_raise ( flags::mask_t  mask,
flags::mask_t oflags = nullptr 
)
inherited
Parameters
[in]maskThe OR-ed flags to raise.
[out]oflagsOptional pointer where to store the previous flags; may be nullptr.
Return values
result::okThe flags were raised.
EINVALThe mask is zero.
EPERMCannot be invoked from an Interrupt Service Routines.

Set more bits in the thread current event flags mask. Use OR at bit-mask level. Wake-up the thread to evaluate the event flags.

Note
Can be invoked from Interrupt Service Routines.

Definition at line 1313 of file os-thread.cpp.

1314 {
1315#if defined(OS_TRACE_RTOS_THREAD_FLAGS)
1316 trace::printf ("%s(0x%X) @%p %s <0x%X\n", __func__, mask, this, name (),
1317 event_flags_.mask ());
1318#endif
1319
1320 result_t res = event_flags_.raise (mask, oflags);
1321
1322 this->resume ();
1323
1324#if defined(OS_TRACE_RTOS_THREAD_FLAGS)
1325 trace::printf ("%s(0x%X) @%p %s >0x%X\n", __func__, mask, this, name (),
1326 event_flags_.mask ());
1327#endif
1328
1329 return res;
1330 }
void resume(void)
Resume the thread.

References os::rtos::internal::object_named::name(), os::trace::printf(), and os::rtos::thread::resume().

◆ function_args()

void * thread::function_args ( void  ) const
inlineinherited
Parameters
None.
Returns
Pointer to arguments.
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 2364 of file os-thread.h.

2365 {
2366 return func_args_;
2367 }

Referenced by thread::delete_system_thread().

◆ interrupt()

bool thread::interrupt ( bool  interrupt = true)
inherited
Parameters
[in]interruptFlag.
Returns
The previous value of the interrupt flag.

If the interrupt flag is true, threads waiting for an event are notified immediately (actually as soon as the thread priority allows it to run).

After the thread detects the interrupted condition, it must clear the interrupted flag.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 1020 of file os-thread.cpp.

1021 {
1022#if defined(OS_TRACE_RTOS_THREAD)
1023 trace::printf ("%s() @%p %s\n", __func__, this, name ());
1024#endif
1025
1026 bool tmp = interrupted_;
1027 interrupted_ = interrupt;
1028
1029 resume ();
1030 return tmp;
1031 }
bool interrupt(bool interrupt=true)
Set the interrupt flag, possibly interrupting the thread.

References os::rtos::thread::interrupt(), os::rtos::internal::object_named::name(), os::trace::printf(), and os::rtos::thread::resume().

Referenced by os::rtos::thread::interrupt().

◆ interrupted()

bool thread::interrupted ( void  )
inlineinherited
Parameters
None.
Return values
trueThe thread was interrupted.
falseThe thread was not interrupted.
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 2373 of file os-thread.h.

2374 {
2375 return interrupted_;
2376 }

Referenced by os::rtos::mutex::lock(), os::rtos::message_queue::receive(), os::rtos::message_queue::send(), os::rtos::mutex::timed_lock(), os::rtos::message_queue::timed_receive(), os::rtos::message_queue::timed_send(), os::rtos::semaphore::timed_wait(), os::rtos::event_flags::timed_wait(), os::rtos::event_flags::wait(), and os::rtos::semaphore::wait().

◆ is_constructed()

bool thread::is_constructed ( const thread thread)
staticinherited
Parameters
[in]threadReference to thread object instance.
Returns
true if the thread was constructed and not yet destructed.

Check the thread status to determine if the thread is already in a constructed state, which means it was constructed and not yet destructed. This is useful for threads constructed via the C API or in C++ via placement new, to avoid constructing them when already constructed.

Note
For this function to be accurate before the first invocation, it is necessary for the thread to start with the memory cleared, for example via a memset().
Todo:
Consider adding a separate member with the magic, for improved reliability.
Note
Can be invoked from Interrupt Service Routines.

Definition at line 307 of file os-thread.cpp.

308 {
309 return ((thread.state_ == state::ready || thread.state_ == state::running
310 || thread.state_ == state::suspended
311 || thread.state_ == state::terminated));
312 }
@ running
Has the CPU and runs.
Definition os-thread.h:386
@ terminated
No longer usable, but resources not yet released.
Definition os-thread.h:394
@ ready
Present in the READY list and competing for CPU.
Definition os-thread.h:382
@ suspended
Not present in the READY list, waiting for an event.
Definition os-thread.h:390

References os::rtos::thread::state::ready, os::rtos::thread::state::running, os::rtos::thread::state::suspended, and os::rtos::thread::state::terminated.

Referenced by os_thread_is_constructed().

◆ join()

result_t thread::join ( void **  exit_ptr = nullptr)
inherited
Parameters
[in]exit_ptrPointer to thread exit value. (Optional, may be nullptr).
Return values
result::okThe thread was terminated.
EPERMCannot be invoked from an Interrupt Service Routines.

Suspend execution of the calling thread until the target thread terminates, unless the target thread has already terminated. On return from a successful join() call with a non-NULL exit_ptr argument, the value passed to exit() by the terminating thread shall be made available in the location referenced by exit_ptr. When a join() returns successfully, the target thread has been terminated. The results of multiple simultaneous calls to join() specifying the same target thread are undefined. If the thread calling join() is cancelled, then the target thread shall not be detached.

POSIX compatibility
Inspired by pthread_join() from <pthread.h> (IEEE Std 1003.1, 2013 Edition).

The join() function may fail if: [EDEADLK] A deadlock was detected.

The join() function shall not return an error code of [EINTR].

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 941 of file os-thread.cpp.

942 {
943#if defined(OS_TRACE_RTOS_THREAD)
944 trace::printf ("%s() @%p %s\n", __func__, this, name ());
945#endif
946
947 // Don't call this from interrupt handlers.
949 // Don't call this from critical regions.
950 os_assert_err (!scheduler::locked (), EPERM);
951
952 // Fail if current thread
953 assert (this != this_thread::_thread ());
954
955 while (state_ != state::destroyed)
956 {
957 joiner_ = this_thread::_thread ();
958 this_thread::_thread ()->internal_suspend_ ();
959 }
960
961#if defined(OS_TRACE_RTOS_THREAD)
962 trace::printf ("%s() @%p %s joined\n", __func__, this, name ());
963#endif
964
965 if (exit_ptr != nullptr)
966 {
967 *exit_ptr = func_result_;
968 }
969
970 return result::ok;
971 }
bool locked(void)
Check if the scheduler is locked.
Definition os-sched.h:858
@ destroyed
Terminated and resources (like stack) released.
Definition os-thread.h:398

References os::rtos::thread::state::destroyed, os::rtos::interrupts::in_handler_mode(), os::rtos::scheduler::locked(), os::rtos::internal::object_named::name(), os::rtos::result::ok, os_assert_err, and os::trace::printf().

Referenced by os_thread_join().

◆ kill()

result_t thread::kill ( void  )
inherited
Parameters
None.
Return values
result::okThe tread was terminated.
POSIX compatibility
Inspired by pthread_kill() from <pthread.h> (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 1231 of file os-thread.cpp.

1232 {
1233#if defined(OS_TRACE_RTOS_THREAD)
1234 trace::printf ("%s() @%p %s\n", __func__, this, name ());
1235#endif
1236
1237 // Don't call this from interrupt handlers.
1239
1240 {
1241 // ----- Enter critical section ---------------------------------------
1242 scheduler::critical_section scs;
1243
1244 if (state_ == state::destroyed)
1245 {
1246#if defined(OS_TRACE_RTOS_THREAD)
1247 trace::printf ("%s() @%p %s already gone\n", __func__, this,
1248 name ());
1249#endif
1250 return result::ok; // Already exited itself
1251 }
1252
1253 {
1254 // ----- Enter critical section -------------------------------------
1255 interrupts::critical_section ics;
1256
1257 // Remove thread from the funeral list and kill it here.
1258 ready_node_.unlink ();
1259
1260 // If the thread is waiting on an event, remove it from the list.
1261 if (waiting_node_ != nullptr)
1262 {
1263 waiting_node_->unlink ();
1264 }
1265
1266 // If the thread is waiting on a timeout, remove it from the list.
1267 if (clock_node_ != nullptr)
1268 {
1269 clock_node_->unlink ();
1270 }
1271
1272 child_links_.unlink ();
1273 // ----- Exit critical section --------------------------------------
1274 }
1275
1276 // The must be no more children threads alive.
1277 assert (children_.empty ());
1278 parent_ = nullptr;
1279
1280#if defined(OS_USE_RTOS_PORT_SCHEDULER)
1281
1282 port::thread::destroy_other (this);
1283
1284#endif
1285
1286 func_result_ = nullptr;
1287
1288 func_ = nullptr;
1289 func_args_ = nullptr;
1290
1291 internal_destroy_ ();
1292
1293 // There must be no mutexes locked by this thread.
1294 // Must have been cleaned before.
1295 assert (mutexes_.empty ());
1296 assert (acquired_mutexes_ == 0);
1297
1298 // ----- Exit critical section ----------------------------------------
1299 }
1300
1301 return result::ok;
1302 }

References os::rtos::thread::state::destroyed, os::rtos::interrupts::in_handler_mode(), os::rtos::internal::object_named::name(), os::rtos::result::ok, os_assert_err, and os::trace::printf().

Referenced by os::rtos::thread::~thread(), and os_thread_kill().

◆ name()

const char * os::rtos::internal::object_named::name ( void  ) const
inlineinherited
Parameters
None.
Returns
A null terminated string.

All objects return a non-null string; anonymous objects return "-".

Note
Can be invoked from Interrupt Service Routines.

Definition at line 753 of file os-decls.h.

754 {
755 return name_;
756 }

Referenced by os::memory::lifo::lifo(), os::memory::malloc_memory_resource::malloc_memory_resource(), os::rtos::message_queue_typed< T, Allocator >::message_queue_typed(), os::memory::block_pool::~block_pool(), os::rtos::event_flags::~event_flags(), os::memory::first_fit_top::~first_fit_top(), os::memory::lifo::~lifo(), os::memory::malloc_memory_resource::~malloc_memory_resource(), os::rtos::memory_pool::~memory_pool(), os::rtos::message_queue::~message_queue(), os::rtos::mutex::~mutex(), os::rtos::semaphore::~semaphore(), os::rtos::thread::~thread(), os::rtos::timer::~timer(), os::rtos::memory_pool::alloc(), os::rtos::thread::cancel(), os::rtos::event_flags::clear(), os::rtos::mutex::consistent(), os::rtos::thread::detach(), os::memory::new_delete_memory_resource::do_allocate(), os::memory::block_pool::do_allocate(), os::memory::first_fit_top::do_allocate(), os::memory::lifo::do_allocate(), os::memory::malloc_memory_resource::do_allocate(), os::rtos::thread::flags_raise(), os::rtos::memory_pool::free(), os::rtos::event_flags::get(), os::rtos::thread::interrupt(), os::rtos::thread::join(), os::rtos::thread::kill(), os::rtos::internal::terminated_threads_list::link(), os::rtos::mutex::lock(), os::rtos::memory::memory_resource::out_of_memory_handler(), os::rtos::semaphore::post(), os::rtos::mutex::prio_ceiling(), os::rtos::mutex::prio_ceiling(), os::rtos::thread::priority(), os::rtos::thread::priority_inherited(), os::rtos::event_flags::raise(), os::rtos::message_queue::receive(), os::rtos::memory_pool::reset(), os::rtos::message_queue::reset(), os::rtos::mutex::reset(), os::rtos::semaphore::reset(), os::rtos::thread::resume(), os::rtos::message_queue::send(), os::rtos::clock::sleep_for(), os::rtos::timer::start(), os::rtos::timer::stop(), os::rtos::memory_pool::timed_alloc(), os::rtos::mutex::timed_lock(), os::rtos::message_queue::timed_receive(), os::rtos::message_queue::timed_send(), os::rtos::semaphore::timed_wait(), os::rtos::event_flags::timed_wait(), os::rtos::memory::memory_resource::trace_print_statistics(), os::rtos::memory_pool::try_alloc(), os::rtos::mutex::try_lock(), os::rtos::message_queue::try_receive(), os::rtos::message_queue::try_send(), os::rtos::event_flags::try_wait(), os::rtos::semaphore::try_wait(), os::rtos::internal::ready_threads_list::unlink_head(), os::rtos::mutex::unlock(), os::rtos::event_flags::wait(), os::rtos::semaphore::wait(), and os::rtos::event_flags::waiting().

◆ operator delete()

void os::rtos::internal::object_named_system::operator delete ( void *  ptr,
std::size_t  bytes 
)
inlinestaticinherited
Parameters
ptrPointer to object.
bytesNumber of bytes to deallocate.
Returns
Nothing.

The deallocation function (3.7.4.2) called by a delete-expression to render the value of ptr invalid.

ptr shall be a null pointer or its value shall be a value returned by an earlier call to the (possibly replaced) operator new() which has not been invalidated by an intervening call to operator delete(void*).

If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the earlier call to operator new.

The storage is deallocated using the RTOS system allocator.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 120 of file os-inlines.h.

121 {
122 assert (!interrupts::in_handler_mode ());
123
124 rtos::memory::allocator<char> ().deallocate (static_cast<char*> (ptr),
125 bytes);
126 }

References os::rtos::memory::allocator_stateless_default_resource< T >::deallocate(), and os::rtos::interrupts::in_handler_mode().

◆ operator delete[]()

void os::rtos::internal::object_named_system::operator delete[] ( void *  ptr,
std::size_t  bytes 
)
inlinestaticinherited
Parameters
ptrPointer to array of objects.
bytesNumber of bytes to deallocate.
Returns
Nothing.

The deallocation function (3.7.4.2) called by the array form of a delete-expression to render the value of ptr invalid.

If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the earlier call to operator new.

The storage is deallocated using the RTOS system allocator.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 141 of file os-inlines.h.

142 {
143 // Forward array deallocation to single element deallocation.
144 operator delete (ptr, bytes);
145 }

◆ operator new() [1/2]

void * os::rtos::internal::object_named_system::operator new ( std::size_t  bytes)
inlinestaticinherited
Parameters
bytesNumber of bytes to allocate.
Returns
Pointer to allocated object.

The allocation function (3.7.4.1) called by a new-expression (5.3.4) to allocate a storage of size bytes suitably aligned to represent any object of that size. Return a non-null pointer to suitably aligned storage (3.7.4).

The storage is allocated using the RTOS system allocator.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 43 of file os-inlines.h.

44 {
45 assert (!interrupts::in_handler_mode ());
46
47 return rtos::memory::allocator<char> ().allocate (bytes);
48 }

References os::rtos::memory::allocator_stateless_default_resource< T >::allocate(), and os::rtos::interrupts::in_handler_mode().

◆ operator new() [2/2]

void * os::rtos::internal::object_named_system::operator new ( std::size_t  bytes,
void *  ptr 
)
inlinestaticinherited
Parameters
bytesNumber of bytes to emplace.
ptrPointer to location to emplace the object.
Returns
Pointer to emplaced object.

The allocation function (3.7.4.1) called by a placement new-expression to allocate a storage of size bytes suitably aligned to represent any object of that size. Return a non-null pointer to suitably aligned storage (3.7.4).

The storage is allocated using the RTOS system allocator.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 80 of file os-inlines.h.

81 {
82 return ptr;
83 }

◆ operator new[]() [1/2]

void * os::rtos::internal::object_named_system::operator new[] ( std::size_t  bytes)
inlinestaticinherited
Parameters
bytesNumber of bytes to allocate.
Returns
Pointer to allocated array.

The allocation function (3.7.4.1) called by the array form of a new-expression (5.3.4) to allocate a storage of size bytes suitably aligned to represent any array object of that size or smaller.

The storage is allocated using the RTOS system allocator.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 61 of file os-inlines.h.

62 {
63 // Forward array allocation to single element allocation.
64 return operator new (bytes);
65 }

◆ operator new[]() [2/2]

void * os::rtos::internal::object_named_system::operator new[] ( std::size_t  bytes,
void *  ptr 
)
inlinestaticinherited
Parameters
bytesNumber of bytes to emplace.
ptrPointer to location to emplace the object.
Returns
Pointer to emplaced array.

The allocation function (3.7.4.1) called by the array form of a placement new-expression to allocate a storage of size bytes suitably aligned to represent any array object of that size or smaller.

The storage is allocated using the RTOS system allocator.

Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 97 of file os-inlines.h.

98 {
99 // Forward array allocation to single element allocation.
100 return operator new (bytes, ptr);
101 }

◆ operator==()

bool thread::operator== ( const thread rhs) const
inlineinherited
Return values
trueThe given thread is the same as this thread.
falseThe threads are different.

Identical threads should have the same memory address.

Compatible with POSIX pthread_equal(). http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_equal.html

Definition at line 2346 of file os-thread.h.

2347 {
2348 return this == &rhs;
2349 }

◆ priority() [1/2]

result_t thread::priority ( priority_t  prio)
inherited
Parameters
[in]prioNew priority.
Return values
result::okThe priority was set.
EPERMCannot be invoked from an Interrupt Service Routines.
EINVALThe value of prio is invalid for the scheduling policy of the specified thread.

Set the scheduling priority for the thread to the value given by prio.

If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an ESRCH error.

The priority() function shall not return an error code of EINTR.

POSIX compatibility
Inspired by pthread_setschedprio() from <pthread.h> (IEEE Std 1003.1, 2013 Edition).
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 734 of file os-thread.cpp.

735 {
736#if defined(OS_TRACE_RTOS_THREAD)
737 trace::printf ("%s(%u) @%p %s\n", __func__, prio, this, name ());
738#endif
739
740 // Don't call this from interrupt handlers.
742 // Check the priority, it is not in the allowed range.
743 os_assert_err (prio < priority::error, EINVAL);
744 os_assert_err (prio != priority::none, EINVAL);
745
746 if (prio_assigned_ == prio)
747 {
748 // Optimise, if priority did not change.
749 return result::ok;
750 }
751
752 prio_assigned_ = prio;
753
754 result_t res = result::ok;
755
756#if defined(OS_USE_RTOS_PORT_SCHEDULER)
757
758 // The port must perform a context switch.
759 res = port::thread::priority (this, prio);
760
761#else
762
763 if (state_ == state::ready)
764 {
765 // ----- Enter critical section -------------------------------------
766 interrupts::critical_section ics;
767
768 // Remove from initial location and reinsert according
769 // to new priority.
770 ready_node_.unlink ();
771 scheduler::ready_threads_list_.link (ready_node_);
772 // ----- Exit critical section --------------------------------------
773 }
774
775 // Mandatory, the priority might have been raised, the
776 // task must be scheduled to run.
778
779#endif
780
781 return res;
782 }
void yield(void)
Yield execution to the next ready thread.

References os::rtos::thread::priority::error, os::rtos::interrupts::in_handler_mode(), os::rtos::internal::object_named::name(), os::rtos::thread::priority::none, os::rtos::result::ok, os_assert_err, os::trace::printf(), os::rtos::thread::state::ready, and os::rtos::this_thread::yield().

Referenced by os::rtos::internal::ready_threads_list::link(), and os::rtos::internal::waiting_threads_list::link().

◆ priority() [2/2]

thread::priority_t thread::priority ( void  )
inherited
Parameters
None.
Returns
The thread priority.
POSIX compatibility
Extension to standard, no POSIX similar functionality identified.
Note
Can be invoked from Interrupt Service Routines.

Definition at line 678 of file os-thread.cpp.

679 {
680 // trace::printf ("%s() @%p %s\n", __func__, this, name ());
681
682 if (prio_inherited_ == priority::none)
683 {
684 // The common case is to have no inherited priority;
685 // return the assigned one.
686 return prio_assigned_;
687 }
688 else
689 {
690 // Return the maximum between inherited and assigned.
691 return (prio_inherited_ >= prio_assigned_) ? prio_inherited_
692 : prio_assigned_;
693 }
694 }

References os::rtos::thread::priority::none.

◆ priority_inherited() [1/2]

result_t thread::priority_inherited ( priority_t  prio)
inherited
Parameters
[in]prioNew priority.
Return values
result::okThe priority was set.
EPERMCannot be invoked from an Interrupt Service Routines.
EINVALThe value of prio is invalid for the scheduling policy of the specified thread.

Set the scheduling inherited priority for the thread to the value given by prio.

If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an ESRCH error.

The priority_inherited() function shall not return an error code of EINTR.

POSIX compatibility
Extension to standard, no POSIX similar functionality identified.
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 802 of file os-thread.cpp.

803 {
804#if defined(OS_TRACE_RTOS_THREAD)
805 trace::printf ("%s(%u) @%p %s\n", __func__, prio, this, name ());
806#endif
807
808 // Don't call this from interrupt handlers.
810 // Check the priority, it is not in the allowed range.
811 os_assert_err (prio < priority::error, EINVAL);
812
813 // Warning: do not check for `priority::none`, since
814 // `mutex::unlock()` sets it when the list of mutexes owned
815 // by a thread is empty.
816
817 if (prio == prio_inherited_)
818 {
819 // Optimise, if priority did not change.
820 return result::ok;
821 }
822
823 prio_inherited_ = prio;
824
825 if (prio_inherited_ < prio_assigned_)
826 {
827 // Optimise, no need to reschedule.
828 return result::ok;
829 }
830
831 result_t res = result::ok;
832
833#if defined(OS_USE_RTOS_PORT_SCHEDULER)
834
835 // The port must perform a context switch.
836 res = port::thread::priority (this, prio);
837
838#else
839
840 if (state_ == state::ready)
841 {
842 // ----- Enter critical section -------------------------------------
843 interrupts::critical_section ics;
844
845 // Remove from initial location and reinsert according
846 // to new priority.
847 ready_node_.unlink ();
848 scheduler::ready_threads_list_.link (ready_node_);
849 // ----- Exit critical section --------------------------------------
850 }
851
852 // Mandatory, the priority might have been raised, the
853 // task must be scheduled to run.
855
856#endif
857
858 return res;
859 }

References os::rtos::thread::priority::error, os::rtos::interrupts::in_handler_mode(), os::rtos::internal::object_named::name(), os::rtos::result::ok, os_assert_err, os::trace::printf(), os::rtos::thread::state::ready, and os::rtos::this_thread::yield().

◆ priority_inherited() [2/2]

thread::priority_t thread::priority_inherited ( void  )
inherited
Parameters
None.
Returns
The thread inherited priority. May be priority::none.
POSIX compatibility
Extension to standard, no POSIX similar functionality identified.
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 703 of file os-thread.cpp.

704 {
705 // Don't call this from interrupt handlers.
707
708 return prio_inherited_;
709 }

References os::rtos::thread::priority::error, os::rtos::interrupts::in_handler_mode(), and os_assert_err.

◆ resume()

void thread::resume ( void  )
inherited
Parameters
None.
Returns
Nothing.

Internal, used in the implementation of synchronisation objects.

POSIX compatibility
Extension to standard, no POSIX similar functionality identified.
Note
Can be invoked from Interrupt Service Routines.

Definition at line 630 of file os-thread.cpp.

631 {
632#if defined(OS_TRACE_RTOS_THREAD_CONTEXT)
633 trace::printf ("%s() @%p %s %u\n", __func__, this, name (),
634 prio_assigned_);
635#endif
636
637#if defined(OS_USE_RTOS_PORT_SCHEDULER)
638
639 {
640 // ----- Enter critical section ---------------------------------------
641 interrupts::critical_section ics;
642
643 state_ = state::ready;
644 port::thread::resume (this);
645 // ----- Exit critical section ----------------------------------------
646 }
647
648#else
649
650 // Don't call this from high priority interrupts.
651 assert (port::interrupts::is_priority_valid ());
652
653 {
654 // ----- Enter critical section ---------------------------------------
655 interrupts::critical_section ics;
656
657 // If the thread is not already in the ready list, enqueue it.
658 if (ready_node_.next () == nullptr)
659 {
660 scheduler::ready_threads_list_.link (ready_node_);
661 // state::ready set in above link().
662 }
663 // ----- Exit critical section ----------------------------------------
664 }
665
667
668#endif
669 }

References os::rtos::internal::object_named::name(), os::trace::printf(), os::rtos::thread::state::ready, and os::rtos::port::scheduler::reschedule().

Referenced by os::rtos::internal::timeout_thread_node::action(), os::rtos::thread::flags_raise(), os::rtos::thread::interrupt(), and os::rtos::internal::waiting_threads_list::resume_one().

◆ stack()

class thread::stack & thread::stack ( void  )
inlineinherited
Parameters
None.
Returns
A reference to the context stack object instance.
Note
Can be invoked from Interrupt Service Routines.

Definition at line 2451 of file os-thread.h.

2452 {
2453 return context_.stack_;
2454 }

Referenced by os_terminate_goodbye().

◆ state()

thread::state_t thread::state ( void  ) const
inlineinherited
Parameters
None.
Returns
Thread scheduler state.
Note
Can be invoked from Interrupt Service Routines.

Definition at line 2355 of file os-thread.h.

2356 {
2357 return state_;
2358 }

Referenced by os::rtos::internal::timeout_thread_node::action(), and os::rtos::internal::waiting_threads_list::resume_one().

◆ statistics()

class thread::statistics & thread::statistics ( void  )
inlineinherited
Warning
Cannot be invoked from Interrupt Service Routines.

Definition at line 2462 of file os-thread.h.

2463 {
2464 return statistics_;
2465 }

◆ user_storage()

os_thread_user_storage_t * thread::user_storage ( void  )
inlineinherited
Parameters
None.
Returns
The address of the thread user storage.

The user storage is a custom structure defined in <os-app-config.h>, which is added to each and every thread storage. Applications can store here any data.

Inspired by (actually a generalisation of) µC-OS III task user registers and FreeRTOS thread local storage, which proved useful when implementing CMSIS+ over FreeRTOS.

Note
Available only when OS_INCLUDE_RTOS_CUSTOM_THREAD_USER_STORAGE is defined.
Can be invoked from Interrupt Service Routines.

Definition at line 2398 of file os-thread.h.

2399 {
2400 return &user_storage_;
2401 }

Member Data Documentation

◆ errno_

int os::rtos::thread::errno_ = 0
inherited

Definition at line 255 of file os-thread.h.

Referenced by os::rtos::this_thread::__errno().

◆ initializer

const thread::attributes thread::initializer
staticinherited

This variable is used by the default constructor.

Definition at line 1011 of file os-thread.h.

◆ stack_size_bytes

template<std::size_t N = port::stack::default_size_bytes>
const std::size_t os::rtos::thread_inclusive< N >::stack_size_bytes = N
static

Definition at line 1830 of file os-thread.h.

Referenced by os::rtos::thread_inclusive< N >::thread_inclusive().


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