Embedded Artistry Framework
Embedded Systems C++ Framework
Classes | Enumerations | Functions

Virtual OS implementation for POSIX systems. More...

Collaboration diagram for POSIX OS Types:

Classes

class  os::posix::posixOSFactory_impl
 Implementation of the POSIX OS Factory For API documentation, see embvm::embvm::VirtualOSFactory. More...
 
class  os::posix::EventFlag
 POSIX Event Flag Implementation. More...
 
class  os::posix::MessageQueue< TType >
 Posix Message Queue implementation for Linux. More...
 
class  os::posix::Mutex
 POSIX Mutex Implementation. More...
 
class  os::posix::Semaphore
 Create a POSIX semaphore (OSX variant) More...
 
class  os::posix::Thread
 Create a POSIX thread. More...
 

Enumerations

enum  os::posix::pthread_priority : uint8_t {
  os::posix::pthread_priority::panic = 99, os::posix::pthread_priority::interrupt = 99, os::posix::pthread_priority::realtime = 99, os::posix::pthread_priority::veryHigh = 50,
  os::posix::pthread_priority::high = 40, os::posix::pthread_priority::aboveNormal = 30, os::posix::pthread_priority::normal = 20, os::posix::pthread_priority::belowNormal = 10,
  os::posix::pthread_priority::low = 5, os::posix::pthread_priority::lowest = 1, os::posix::pthread_priority::idle = 0
}
 PThread priority values. More...
 

Functions

constexpr posix::pthread_priority os::posix::convert_to_pthread_priority (embvm::thread::priority p) noexcept
 Convert between framework thread priorities and PThread priorities. More...
 

Detailed Description

Virtual OS implementation for POSIX systems.


Class Documentation

◆ os::posix::posixOSFactory_impl

class os::posix::posixOSFactory_impl

Implementation of the POSIX OS Factory For API documentation, see embvm::embvm::VirtualOSFactory.

Public Member Functions

 posixOSFactory_impl ()=default
 
 ~posixOSFactory_impl ()=default
 

Static Public Member Functions

static embvm::VirtualConditionVariablecreateConditionVariable_impl () noexcept
 
static embvm::VirtualThreadcreateThread_impl (std::string_view name, embvm::thread::func_t f, embvm::thread::input_t input, embvm::thread::priority p, size_t stack_size, void *stack_ptr) noexcept
 
static embvm::VirtualMutexcreateMutex_impl (embvm::mutex::type type=embvm::mutex::type::defaultType, embvm::mutex::mode mode=embvm::mutex::mode::defaultMode) noexcept
 
static embvm::VirtualSemaphorecreateSemaphore_impl (embvm::semaphore::mode mode, embvm::semaphore::count_t ceiling, embvm::semaphore::count_t initial_count) noexcept
 
template<typename TType >
static embvm::VirtualMessageQueue< TType > * createMessageQueue_impl (size_t queue_length) noexcept
 
static embvm::VirtualEventFlagcreateEventFlag_impl () noexcept
 
static void destroy_impl (embvm::VirtualThread *item) noexcept
 
static void destroy_impl (embvm::VirtualMutex *item) noexcept
 
static void destroy_impl (embvm::VirtualSemaphore *item) noexcept
 
static void destroy_impl (embvm::VirtualEventFlag *item) noexcept
 
static void destroy_impl (embvm::VirtualConditionVariable *item) noexcept
 

Constructor & Destructor Documentation

◆ posixOSFactory_impl()

os::posix::posixOSFactory_impl::posixOSFactory_impl ( )
default

◆ ~posixOSFactory_impl()

os::posix::posixOSFactory_impl::~posixOSFactory_impl ( )
default

Member Function Documentation

◆ createConditionVariable_impl()

static embvm::VirtualConditionVariable* os::posix::posixOSFactory_impl::createConditionVariable_impl ( )
inlinestaticnoexcept

◆ createEventFlag_impl()

static embvm::VirtualEventFlag* os::posix::posixOSFactory_impl::createEventFlag_impl ( )
inlinestaticnoexcept

◆ createMessageQueue_impl()

template<typename TType >
static embvm::VirtualMessageQueue<TType>* os::posix::posixOSFactory_impl::createMessageQueue_impl ( size_t  queue_length)
inlinestaticnoexcept

◆ createMutex_impl()

static embvm::VirtualMutex* os::posix::posixOSFactory_impl::createMutex_impl ( embvm::mutex::type  type = embvm::mutex::type::defaultType,
embvm::mutex::mode  mode = embvm::mutex::mode::defaultMode 
)
inlinestaticnoexcept

References type.

◆ createSemaphore_impl()

static embvm::VirtualSemaphore* os::posix::posixOSFactory_impl::createSemaphore_impl ( embvm::semaphore::mode  mode,
embvm::semaphore::count_t  ceiling,
embvm::semaphore::count_t  initial_count 
)
inlinestaticnoexcept

◆ createThread_impl()

static embvm::VirtualThread* os::posix::posixOSFactory_impl::createThread_impl ( std::string_view  name,
embvm::thread::func_t  f,
embvm::thread::input_t  input,
embvm::thread::priority  p,
size_t  stack_size,
void *  stack_ptr 
)
inlinestaticnoexcept

References p.

◆ destroy_impl() [1/5]

static void os::posix::posixOSFactory_impl::destroy_impl ( embvm::VirtualThread item)
inlinestaticnoexcept

◆ destroy_impl() [2/5]

static void os::posix::posixOSFactory_impl::destroy_impl ( embvm::VirtualMutex item)
inlinestaticnoexcept

◆ destroy_impl() [3/5]

static void os::posix::posixOSFactory_impl::destroy_impl ( embvm::VirtualSemaphore item)
inlinestaticnoexcept

◆ destroy_impl() [4/5]

static void os::posix::posixOSFactory_impl::destroy_impl ( embvm::VirtualEventFlag item)
inlinestaticnoexcept

◆ destroy_impl() [5/5]

static void os::posix::posixOSFactory_impl::destroy_impl ( embvm::VirtualConditionVariable item)
inlinestaticnoexcept

◆ os::posix::EventFlag

class os::posix::EventFlag

POSIX Event Flag Implementation.

Inheritance diagram for os::posix::EventFlag:
Inheritance graph

Public Member Functions

 EventFlag ()=default
 
 ~EventFlag () noexcept
 Default destructor which cleans up the event flag group. More...
 
embvm::eventflag::flag_t get (embvm::eventflag::flag_t bits_wait, embvm::eventflag::option opt=embvm::eventflag::option::OR, bool clearOnExit=true, const embvm::os_timeout_t &timeout=embvm::OS_WAIT_FOREVER) noexcept final
 Get (or wait for) event flags. More...
 
void set (embvm::eventflag::flag_t bits) noexcept final
 Set event flags. More...
 
void setFromISR (embvm::eventflag::flag_t bits) noexcept final
 Set event flags from an ISR. More...
 
void clear () noexcept final
 Clear all event flag bits. More...
 
embvm::eventflag::handle_t native_handle () const noexcept final
 Get the native handle for the event flags group object. More...
 

Private Member Functions

embvm::eventflag::flag_t checkAndClearFlags (embvm::eventflag::flag_t bits_wait, embvm::eventflag::option opt, bool clearOnExit) noexcept
 Helper function which evaluates flags and returns the matching value. More...
 

Private Attributes

std::atomic< embvm::eventflag::flag_tflags_ = 0
 The storage type for the event flag group's flags. More...
 
posix::ConditionVariable cv_
 Condition variable which is used to wake threads waiting on this group. More...
 
posix::Mutex mutex_
 Mutex which protects the flags. More...
 

Constructor & Destructor Documentation

◆ EventFlag()

os::posix::EventFlag::EventFlag ( )
default

◆ ~EventFlag()

EventFlag::~EventFlag ( )
noexcept

Default destructor which cleans up the event flag group.

Member Function Documentation

◆ checkAndClearFlags()

embvm::eventflag::flag_t os::posix::EventFlag::checkAndClearFlags ( embvm::eventflag::flag_t  bits_wait,
embvm::eventflag::option  opt,
bool  clearOnExit 
)
inlineprivatenoexcept

Helper function which evaluates flags and returns the matching value.

Parameters
bits_waitThe bits the caller is waiting for, specified as a bitmask
optThe comparison option (AND or OR)
clearOnExitIf true, matching flags will be cleared from flag_ when the function exits
Returns
the resulting event flag bits, compared to bits_wait and with the selected option (opt).
Postcondition
If clearOnExit is true:
  • All flags set in bits_wait will be cleared from flag_ if AND option is used.
  • Flags set in both btis_wait and flags_ will be cleared from flag_ if OR option is used.

References flags_, and embvm::eventflag::OR.

Referenced by get().

◆ clear()

void os::posix::EventFlag::clear ( )
inlinefinalvirtualnoexcept

Clear all event flag bits.

Postcondition
Event flag group value is reset to '0'.

Implements embvm::VirtualEventFlag.

References flags_, os::posix::Mutex::lock(), mutex_, and os::posix::Mutex::unlock().

◆ get()

embvm::eventflag::flag_t os::posix::EventFlag::get ( embvm::eventflag::flag_t  bits_wait,
embvm::eventflag::option  opt = embvm::eventflag::option::OR,
bool  clearOnExit = true,
const embvm::os_timeout_t timeout = embvm::OS_WAIT_FOREVER 
)
inlinefinalvirtualnoexcept

Get (or wait for) event flags.

Get the event flag bits. If the required event flags are not set, this function will block the calling thread until the flags are set or the timeout expires.

To see all bits currently set, set bits_wait to ~0 and timeout_t to the minimal value (timeout_t(1)).

Parameters
bits_waitA bitmask of bits to wait for in the event flags group.
optThe wait option, which is combined with bits_wait to control the behavior of get(). embvm::eventflag::option::OR will return once any of the bits specified in bits_wait are set in the event flag group. embvm::eventflag::option::AND will only return once ALL of the bits specified in bits_wait are set in the event flag group.
clearOnExitIf true, all event flags that were specified in bits_wait and set in the event flag group will be set to 0. If false, event flag bits will not be modified.
timeoutSpecify a timeout for the get operation. If OS_WAIT_FOREVER is supplied, this call will block until the event flags are set. Otherwise, the function will timeout after the elapsed time if flags are not set.
Returns
0 if timeout, or flags that were currently set according to bits_wait

Implements embvm::VirtualEventFlag.

References checkAndClearFlags(), cv_, os::posix::Mutex::lock(), mutex_, os::posix::Mutex::unlock(), and os::posix::ConditionVariable::wait().

◆ native_handle()

embvm::eventflag::handle_t os::posix::EventFlag::native_handle ( ) const
inlinefinalvirtualnoexcept

Get the native handle for the event flags group object.

Returns
the native handle for this event flags object. The handle will always return the handle_t and it must be cast to the native handle type by the user.

Implements embvm::VirtualEventFlag.

◆ set()

void os::posix::EventFlag::set ( embvm::eventflag::flag_t  bits)
inlinefinalvirtualnoexcept

Set event flags.

Set event flags from a threaded context. Threads waiting on event flags (via get()) will be notified.

Parameters
bitsA bitmask representing the bits to set in the event flag group.

Implements embvm::VirtualEventFlag.

References os::posix::ConditionVariable::broadcast(), cv_, flags_, os::posix::Mutex::lock(), mutex_, and os::posix::Mutex::unlock().

◆ setFromISR()

void os::posix::EventFlag::setFromISR ( embvm::eventflag::flag_t  bits)
inlinefinalvirtualnoexcept

Set event flags from an ISR.

Set event flags from an interrupt context. This call must not use a lock. Threads waiting on event flags (via get()) will be notified.

Parameters
bitsA bitmask representing the bits to set in the event flag group.

Implements embvm::VirtualEventFlag.

References os::posix::ConditionVariable::broadcast(), cv_, flags_, os::posix::Mutex::lock(), mutex_, and os::posix::Mutex::unlock().

Member Data Documentation

◆ cv_

posix::ConditionVariable os::posix::EventFlag::cv_
private

Condition variable which is used to wake threads waiting on this group.

Referenced by get(), set(), and setFromISR().

◆ flags_

std::atomic<embvm::eventflag::flag_t> os::posix::EventFlag::flags_ = 0
private

The storage type for the event flag group's flags.

Referenced by checkAndClearFlags(), clear(), set(), and setFromISR().

◆ mutex_

posix::Mutex os::posix::EventFlag::mutex_
private

Mutex which protects the flags.

Referenced by clear(), get(), set(), and setFromISR().

◆ os::posix::MessageQueue

class os::posix::MessageQueue

template<typename TType>
class os::posix::MessageQueue< TType >

Posix Message Queue implementation for Linux.

Template Parameters
TTypeThe type of data to be stored in the message queue
Inheritance diagram for os::posix::MessageQueue< TType >:
Inheritance graph

Public Member Functions

 MessageQueue (std::string_view name, size_t queue_length) noexcept
 Construct a message queue with name and length. More...
 
 ~MessageQueue () noexcept
 Default destructor. More...
 
bool push (TType val, embvm::os_timeout_t timeout=embvm::OS_WAIT_FOREVER) noexcept final
 Post a message to the queue. More...
 
TType pop (embvm::os_timeout_t timeout=embvm::OS_WAIT_FOREVER) noexcept final
 Retrieve a message from the queue. More...
 
size_t size () const noexcept final
 Get the current size of the message queue. More...
 
void reset () noexcept final
 Reset the message queue to an empty state. More...
 
bool empty () const noexcept final
 Check if the message queue is empty. More...
 
bool full () const noexcept final
 Check if the message queue is full. More...
 
virtual embvm::msgqueue::handle_t native_handle () const noexcept=0
 Get the native handle for the message queue object. More...
 

Private Attributes

std::string_view name_
 The name of this message queue instance. More...
 
mqd_t handle_
 The message queue instance handle. More...
 

Constructor & Destructor Documentation

◆ MessageQueue()

template<typename TType >
os::posix::MessageQueue< TType >::MessageQueue ( std::string_view  name,
size_t  queue_length 
)
inlineexplicitnoexcept

Construct a message queue with name and length.

Parameters
nameThe name associated with the message queue.
Note
A std::string input must remain valid for the lifetime of this object, since std::string_view is used to store the name.
Parameters
queue_lengthThe maximum size of the message queue.

References assert, os::posix::MessageQueue< TType >::handle_, and os::posix::MessageQueue< TType >::name_.

◆ ~MessageQueue()

template<typename TType >
os::posix::MessageQueue< TType >::~MessageQueue ( )
inlinenoexcept

Default destructor.

References os::posix::MessageQueue< TType >::handle_.

Member Function Documentation

◆ empty()

template<typename TType >
bool os::posix::MessageQueue< TType >::empty ( ) const
inlinefinalvirtualnoexcept

Check if the message queue is empty.

Returns
true if the queue is empty, false otherwise.

Implements embvm::VirtualMessageQueue< TType >.

References assert, os::posix::MessageQueue< TType >::handle_, and r.

◆ full()

template<typename TType >
bool os::posix::MessageQueue< TType >::full ( ) const
inlinefinalvirtualnoexcept

Check if the message queue is full.

Returns
true if the queue is full, false otherwise.

Implements embvm::VirtualMessageQueue< TType >.

References assert, os::posix::MessageQueue< TType >::handle_, and r.

◆ native_handle()

template<typename TType >
virtual embvm::msgqueue::handle_t embvm::VirtualMessageQueue< TType >::native_handle ( ) const
pure virtualnoexceptinherited

Get the native handle for the message queue object.

Returns
the native handle for this message queue object. The handle will always return the handle_t and it must be cast to the native handle type by the user.

Implemented in os::freertos::MessageQueue< TType >.

◆ pop()

template<typename TType >
TType os::posix::MessageQueue< TType >::pop ( embvm::os_timeout_t  timeout = embvm::OS_WAIT_FOREVER)
inlinefinalvirtualnoexcept

Retrieve a message from the queue.

Retrieve a message from the queue. This function blocks the calling thread until a message is received (if OS_WAIT_FOREVER is specified as a timeout), or until the timeout expires.

Parameters
timeoutThe time to wait for a new message. A value of OS_WAIT_FOREVER indicates that the thread should block until a new message is posted.
Returns
an optional value. Use has_value() member function to determine if the value is valid. A valid value is only returned on success.

Implements embvm::VirtualMessageQueue< TType >.

References assert, and os::posix::MessageQueue< TType >::handle_.

◆ push()

template<typename TType >
bool os::posix::MessageQueue< TType >::push ( TType  val,
embvm::os_timeout_t  timeout = embvm::OS_WAIT_FOREVER 
)
inlinefinalvirtualnoexcept

Post a message to the queue.

Post a message to the queue. This function blocks the calling thread until there is an available slot (if OS_WAIT_FOREVER is specified as a timeout), or until the timeout expires.

Posting a message to the queue will notify all threads waiting on a new message (via pop()).

Parameters
valThe data value to post to the queue.
timeoutThe time to wait for a new message. A value of OS_WAIT_FOREVER indicates that the thread should block until a new message is posted.
Returns
true if the message was successfully posted, false otherwise.

Implements embvm::VirtualMessageQueue< TType >.

◆ reset()

template<typename TType >
void os::posix::MessageQueue< TType >::reset ( )
inlinefinalvirtualnoexcept

Reset the message queue to an empty state.

Postcondition
The message queue is emptied, with size 0.

Implements embvm::VirtualMessageQueue< TType >.

References os::posix::MessageQueue< TType >::handle_.

◆ size()

template<typename TType >
size_t os::posix::MessageQueue< TType >::size ( ) const
inlinefinalvirtualnoexcept

Get the current size of the message queue.

Size represents the number of elements stored, rather than the maximum size (aka capacity).

Returns
the current number of elements stored in the message queue.

Implements embvm::VirtualMessageQueue< TType >.

References assert, os::posix::MessageQueue< TType >::handle_, and r.

Member Data Documentation

◆ handle_

◆ name_

template<typename TType >
std::string_view os::posix::MessageQueue< TType >::name_
private

The name of this message queue instance.

Referenced by os::posix::MessageQueue< TType >::MessageQueue().

◆ os::posix::Mutex

class os::posix::Mutex

POSIX Mutex Implementation.

Useful reference documentation:

Inheritance diagram for os::posix::Mutex:
Inheritance graph

Public Member Functions

 Mutex (embvm::mutex::type type=embvm::mutex::type::defaultType, embvm::mutex::mode mode=embvm::mutex::mode::defaultMode) noexcept
 Construct a POSIX mutex. More...
 
 ~Mutex () noexcept
 Default destructor. More...
 
void lock () noexcept final
 Lock the mutex. More...
 
void unlock () noexcept final
 Unlock the mutex. More...
 
bool trylock () noexcept final
 Try to lock the mutex. More...
 
embvm::mutex::handle_t native_handle () const noexcept final
 

Private Attributes

pthread_mutex_t handle_
 The pthread handle associated with this mutex;. More...
 

Constructor & Destructor Documentation

◆ Mutex()

os::posix::Mutex::Mutex ( embvm::mutex::type  type = embvm::mutex::type::defaultType,
embvm::mutex::mode  mode = embvm::mutex::mode::defaultMode 
)
inlineexplicitnoexcept

Construct a POSIX mutex.

Parameters
typeThe mutex type to create (normal, recursive)
modeThe mutex poperating mode, which controls priority inheritance behaviors.

References assert, handle_, embvm::mutex::none, embvm::mutex::normal, embvm::mutex::priorityInherit, embvm::mutex::protect, r, embvm::mutex::recursive, and type.

◆ ~Mutex()

Mutex::~Mutex ( )
noexcept

Default destructor.

References assert, handle_, and r.

Member Function Documentation

◆ lock()

void os::posix::Mutex::lock ( )
inlinefinalvirtualnoexcept

Lock the mutex.

Attempts to lock the mutex. If successful, the function returns with the calling thread owning the lock. If the lock is already owned, the calling thread will block until the lock is available.

Postcondition
The mutex is locked and owned by the calling thread.

Implements embvm::VirtualMutex.

References assert, handle_, and r.

Referenced by os::posix::EventFlag::clear(), os::posix::EventFlag::get(), os::posix::EventFlag::set(), and os::posix::EventFlag::setFromISR().

◆ native_handle()

embvm::mutex::handle_t os::posix::Mutex::native_handle ( ) const
inlinefinalvirtualnoexcept

Implements embvm::VirtualMutex.

References handle_.

◆ trylock()

bool os::posix::Mutex::trylock ( )
inlinefinalvirtualnoexcept

Try to lock the mutex.

Try to lock the mutex, but don't block the thread if the mutex is already owned.

Postcondition
If the mutex is unowned, the mutex will be locked and owned by the calling thread.
Returns
true if the mutex is successfully locked, false if another thread owns the mutex.

Implements embvm::VirtualMutex.

References assert, EBUSY, handle_, and r.

◆ unlock()

void os::posix::Mutex::unlock ( )
inlinefinalvirtualnoexcept

Unlock the mutex.

Precondition
The mutex is locked and owned by the calling thread.
Postcondition
The mutex is unlocked.

Implements embvm::VirtualMutex.

References assert, handle_, and r.

Referenced by os::posix::EventFlag::clear(), os::posix::EventFlag::get(), os::posix::EventFlag::set(), and os::posix::EventFlag::setFromISR().

Member Data Documentation

◆ handle_

pthread_mutex_t os::posix::Mutex::handle_
private

The pthread handle associated with this mutex;.

Referenced by lock(), Mutex(), native_handle(), trylock(), unlock(), and ~Mutex().

◆ os::posix::Semaphore

class os::posix::Semaphore

Create a POSIX semaphore (OSX variant)

Useful reference documenation:

Inheritance diagram for os::posix::Semaphore:
Inheritance graph

Public Member Functions

 Semaphore (embvm::semaphore::mode mode=embvm::semaphore::mode::counting, embvm::semaphore::count_t ceiling=1, embvm::semaphore::count_t initial_count=-1) noexcept
 
 ~Semaphore ()
 
void give () noexcept final
 Increment count Return a resource to the semaphore, incrementing the count. More...
 
void giveFromISR () noexcept final
 Increment count from an ISR Return a resource to the semaphore, incrementing the count. More...
 
bool take (embvm::os_timeout_t timeout=embvm::OS_WAIT_FOREVER) noexcept final
 
embvm::semaphore::count_t count () noexcept final
 
embvm::semaphore::handle_t native_handle () const noexcept final
 
virtual bool take (const embvm::os_timeout_t &timeout=embvm::OS_WAIT_FOREVER) noexcept=0
 Decrement count Take a resource from the semaphore, decrementing the count. More...
 
virtual embvm::semaphore::count_t count () const noexcept=0
 Check the count of the semaphore. More...
 

Private Attributes

sem_t handle_
 

Constructor & Destructor Documentation

◆ Semaphore()

os::posix::Semaphore::Semaphore ( embvm::semaphore::mode  mode = embvm::semaphore::mode::counting,
embvm::semaphore::count_t  ceiling = 1,
embvm::semaphore::count_t  initial_count = -1 
)
inlineexplicitnoexcept

◆ ~Semaphore()

Semaphore::~Semaphore ( )
noexcept

References assert, handle_, and r.

Member Function Documentation

◆ count() [1/2]

virtual embvm::semaphore::count_t embvm::VirtualSemaphore::count ( ) const
pure virtualnoexceptinherited

Check the count of the semaphore.

Returns
The current semaphore count.

Implemented in os::freertos::Semaphore.

◆ count() [2/2]

embvm::semaphore::count_t os::posix::Semaphore::count ( )
inlinefinalnoexcept

References assert, handle_, and r.

◆ give()

void os::posix::Semaphore::give ( )
inlinefinalvirtualnoexcept

Increment count Return a resource to the semaphore, incrementing the count.

Implements embvm::VirtualSemaphore.

References assert, handle_, and r.

◆ giveFromISR()

void os::posix::Semaphore::giveFromISR ( )
inlinefinalvirtualnoexcept

Increment count from an ISR Return a resource to the semaphore, incrementing the count.

This function variant is safe to call from an interrupt routine. No locking can be used inside this variant.

Implements embvm::VirtualSemaphore.

References assert, handle_, and r.

◆ native_handle()

embvm::semaphore::handle_t os::posix::Semaphore::native_handle ( ) const
inlinefinalvirtualnoexcept

Implements embvm::VirtualSemaphore.

References handle_.

◆ take() [1/2]

virtual bool embvm::VirtualSemaphore::take ( const embvm::os_timeout_t timeout = embvm::OS_WAIT_FOREVER)
pure virtualnoexceptinherited

Decrement count Take a resource from the semaphore, decrementing the count.

Parameters
timeoutThe time to wait for a semaphore resource. A value of WAIT_FOREVER indicates that the thread should block until the semaphore can be acquired.
Returns
true if take was successful, false if a timeout occurred.

Implemented in os::freertos::Semaphore.

◆ take() [2/2]

bool os::posix::Semaphore::take ( embvm::os_timeout_t  timeout = embvm::OS_WAIT_FOREVER)
inlinefinalnoexcept

References assert, handle_, and r.

Referenced by Semaphore().

Member Data Documentation

◆ handle_

sem_t os::posix::Semaphore::handle_
private

◆ os::posix::Thread

class os::posix::Thread

Create a POSIX thread.

Inheritance diagram for os::posix::Thread:
Inheritance graph

Public Member Functions

 Thread (std::string_view name, embvm::thread::func_t func, embvm::thread::input_t arg, embvm::thread::priority p=embvm::thread::priority::normal, size_t stack_size=PTHREAD_STACK_MIN, void *stack_ptr=nullptr) noexcept
 Construct a POSIX thread. More...
 
 ~Thread () noexcept
 Default destructor, cleans up thread on deletion. More...
 
void start () noexcept final
 Start the thread, if it is not auto-started. More...
 
void terminate () noexcept final
 Terminate a running thread. More...
 
void join () noexcept final
 Join a running thread. More...
 
int detach () noexcept
 
std::string_view name () const noexcept final
 View the thread's name. More...
 
embvm::thread::state state () const noexcept final
 Get the thread state. More...
 
embvm::thread::handle_t native_handle () const noexcept final
 Get the native handle for the thread. More...
 

Private Member Functions

void * thread_func () noexcept
 Thread handling wrapper to support thread canceling. More...
 

Private Attributes

std::string_view name_
 THe name of this thread instance. More...
 
pthread_t handle_
 The pthread handle corresponding to this thread instance. More...
 
embvm::thread::input_t arg_
 Thread input arguments. More...
 
embvm::thread::func_t func_
 The functor which will be invokd by the thread. More...
 
std::atomic< embvm::thread::statestate_ = embvm::thread::state::ready
 

Constructor & Destructor Documentation

◆ Thread()

os::posix::Thread::Thread ( std::string_view  name,
embvm::thread::func_t  func,
embvm::thread::input_t  arg,
embvm::thread::priority  p = embvm::thread::priority::normal,
size_t  stack_size = PTHREAD_STACK_MIN,
void *  stack_ptr = nullptr 
)
inlineexplicitnoexcept

Construct a POSIX thread.

Parameters
nameThe name associated with the mutex.
Note
A std::string input must remain valid for the lifetime of this object, since std::string_view is used to store the name.
Parameters
funcThe thread function to execute; can be any functor type.
argThe thread's optional input argument. This value is passed to the thread when it is created.
pThe thread priority setting.
stack_sizeThe thread stack size.
stack_ptrThe thread stack pointer. If stack_ptr is nullptr, then memory will be allocated by the pthread library.

References assert, BOUNCE, os::posix::convert_to_pthread_priority(), handle_, embutil::is_aligned(), NULL, p, PTHREAD_STACK_MIN, r, embvm::thread::ready, state_, and thread_func().

◆ ~Thread()

Thread::~Thread ( )
noexcept

Default destructor, cleans up thread on deletion.

References embvm::thread::completed, join(), state_, terminate(), and embvm::thread::terminated.

Member Function Documentation

◆ detach()

int os::posix::Thread::detach ( )
inlinenoexcept

References handle_.

◆ join()

void os::posix::Thread::join ( )
inlinefinalvirtualnoexcept

Join a running thread.

Joins a running thread, blocking the calling thread until the joined thread completes.

Precondition
The thread has not exited.
Postcondition
The thread has exited.

Implements embvm::VirtualThread.

References assert, EDEADLK, EINVAL, handle_, and r.

Referenced by ~Thread().

◆ name()

std::string_view os::posix::Thread::name ( ) const
inlinefinalvirtualnoexcept

View the thread's name.

Storage for the thread name is implementation defined. For example, the name may be stored in the RTOS memory, or it may require internal object storage.

Returns
The thread object's name.

Implements embvm::VirtualThread.

References name_.

◆ native_handle()

embvm::thread::handle_t os::posix::Thread::native_handle ( ) const
inlinefinalvirtualnoexcept

Get the native handle for the thread.

If custom extensions or behaviors are needed, the thread handle can be retrieved and manipulated directly.

Returns
the native thread object handle as a void*. The user must cast appropriately.

Implements embvm::VirtualThread.

References handle_.

◆ start()

void os::posix::Thread::start ( )
inlinefinalvirtualnoexcept

Start the thread, if it is not auto-started.

Implements embvm::VirtualThread.

◆ state()

embvm::thread::state os::posix::Thread::state ( ) const
inlinefinalvirtualnoexcept

Get the thread state.

Returns
The state of the thread.

Implements embvm::VirtualThread.

References state_.

◆ terminate()

void os::posix::Thread::terminate ( )
inlinefinalvirtualnoexcept

Terminate a running thread.

Precondition
The thread is running.
Postcondition
The thread is aborted.

Implements embvm::VirtualThread.

References assert, handle_, r, state_, and embvm::thread::terminated.

Referenced by ~Thread().

◆ thread_func()

void* os::posix::Thread::thread_func ( )
inlineprivatenoexcept

Thread handling wrapper to support thread canceling.

We wrap the thread to enable canceling, and so we can gracefully tell if the thread has completed successfully

References arg_, assert, embvm::thread::completed, embvm::thread::executing, func_, r, and state_.

Referenced by Thread().

Member Data Documentation

◆ arg_

embvm::thread::input_t os::posix::Thread::arg_
private

Thread input arguments.

Referenced by thread_func().

◆ func_

embvm::thread::func_t os::posix::Thread::func_
private

The functor which will be invokd by the thread.

Referenced by thread_func().

◆ handle_

pthread_t os::posix::Thread::handle_
private

The pthread handle corresponding to this thread instance.

Referenced by detach(), join(), native_handle(), terminate(), and Thread().

◆ name_

std::string_view os::posix::Thread::name_
private

THe name of this thread instance.

Referenced by name().

◆ state_

std::atomic<embvm::thread::state> os::posix::Thread::state_ = embvm::thread::state::ready
private

Enumeration Type Documentation

◆ pthread_priority

PThread priority values.

Note
For PThreads: higher number = higher priority
Enumerator
panic 
interrupt 
realtime 
veryHigh 
high 
aboveNormal 
normal 
belowNormal 
low 
lowest 
idle 

Function Documentation

◆ convert_to_pthread_priority()

constexpr posix::pthread_priority os::posix::convert_to_pthread_priority ( embvm::thread::priority  p)
noexcept