Embedded Artistry Framework
Embedded Systems C++ Framework
Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond > Class Template Reference

#include <active_object.hpp>

Inheritance diagram for embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >:
Inheritance graph

Public Member Functions

size_t queuedCount () const noexcept
 Get the number of operations in the queue. More...
 
bool enqueue (TStorageType t) noexcept
 Add an operation to the queue. More...
 
void shutdown () noexcept
 Shutdown the active object. More...
 

Protected Member Functions

 ~activeObject () noexcept
 Default destructor. More...
 

Private Types

using TQueueType = typename std::conditional<(TQueueSize==0), std::queue< TStorageType >, etl::queue< TStorageType, TQueueSize > >::type
 Queue type definition. More...
 

Private Member Functions

void thread_handler () noexcept
 Active object thread function. More...
 

Private Attributes

TQueueType op_queue_
 Queue storage instance. More...
 
TLock lock_
 Active object lock instance. More...
 
TCond cv_
 Active object condition variable instance. More...
 
std::atomic< boolshutdown_ = false
 Flag indicating that the active object should shutdown. More...
 
std::thread thread_ = std::thread(&activeObject::thread_handler, this)
 Active object thread declaration. More...
 

Member Typedef Documentation

◆ TQueueType

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
using embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::TQueueType = typename std::conditional<(TQueueSize == 0), std::queue<TStorageType>, etl::queue<TStorageType, TQueueSize> >::type
private

Queue type definition.

The queue is statically allocated when TQueueSize > 0, and dynamically allocated when TQueueSize == 0.

Constructor & Destructor Documentation

◆ ~activeObject()

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::~activeObject ( )
inlineprotectednoexcept

Default destructor.

When the active object is destroyed, the thread is shutdown to prevent any race conditions during destruction.

Member Function Documentation

◆ enqueue()

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
bool embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::enqueue ( TStorageType  t)
inlinenoexcept

Add an operation to the queue.

This function can be called directly, or by functions internal to the activeObject. For example, the "transfer" API enqueues transfer operations.

This function is marked noexcept because we want the program to terminate if an exception results from this call. For example, the underlying queue may throw on push.

Parameters
tThe operation data object to enqueue for later processing.

Referenced by embvm::i2c::activeMaster< 128 >::transfer_(), and embvm::spi::activeMaster< TQueueSize, TLock, TCond >::transfer_().

◆ queuedCount()

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
size_t embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::queuedCount ( ) const
inlinenoexcept

Get the number of operations in the queue.

Returns
the number of queued operations.

◆ shutdown()

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
void embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::shutdown ( )
inlinenoexcept

Shutdown the active object.

Call this function in the parent class destructor to stop the AO thread operation before destroying the parent class. This will prevent memory access problems due to race conidtions during destruction.

This function is marked noexcept because we want the program to terminate if an exception results from this call.

The shutdown process is permanent and cannot be reversed without recreating the object.

Referenced by embutil::activeObject< aardvarkI2CMaster, std::pair< embvm::i2c::op_t, const embvm::i2c::master::cb_t > >::~activeObject().

◆ thread_handler()

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
void embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::thread_handler ( )
inlineprivatenoexcept

Active object thread function.

This function monitors the queue and sleeps until new operations are added (or shutdown_ is set). Once a new item is added to the queue, the thread wakes, pops the op off the queue, and calls the derived class's process_() function.

This function is marked noexcept because we want the program to terminate if an exception results from this call. The underlying queue may throw on pop, for instance.

Member Data Documentation

◆ cv_

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
TCond embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::cv_
private

◆ lock_

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
TLock embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::lock_
private

◆ op_queue_

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
TQueueType embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::op_queue_
private

◆ shutdown_

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
std::atomic<bool> embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::shutdown_ = false
private

◆ thread_

template<class TDerivedClass, typename TStorageType, size_t TQueueSize = 0, typename TLock = std::mutex, typename TCond = std::condition_variable>
std::thread embutil::activeObject< TDerivedClass, TStorageType, TQueueSize, TLock, TCond >::thread_ = std::thread(&activeObject::thread_handler, this)
private

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