The Open Master Hearing Aid (openMHA)  openMHA
Open community platform for hearing aid algorithm research
MHAPlugin::cfg_node_t< runtime_cfg_t > Class Template Reference

A node class for storing MHA plugin runtime configurations as a singly linked list, where the nodes store besides the usual "next" and "data" pointers also a flag that indicates whether this node can be deleted. More...

Public Member Functions

 cfg_node_t (runtime_cfg_t *runtime_cfg)
 Constructor for a singly linked list node. More...
 
 ~cfg_node_t ()
 Destructor of the singly linked list node. More...
 

Public Attributes

std::atomic< cfg_node_t< runtime_cfg_t > * > next
 A pointer to the next node in the singly linked list. More...
 
std::atomic< bool > not_in_use
 Initially this data member is set to false by the constructor. More...
 
runtime_cfg_t * data
 A native pointer to the runtime configuration managed by this node. More...
 

Detailed Description

template<class runtime_cfg_t>
class MHAPlugin::cfg_node_t< runtime_cfg_t >

A node class for storing MHA plugin runtime configurations as a singly linked list, where the nodes store besides the usual "next" and "data" pointers also a flag that indicates whether this node can be deleted.

The singly linked list is designed for a single producer thread and a single consumer thread, where the producer is also responsible for destroying objects when they are no longer needed because the consumer is the signal processing thread that cannot afford memory allocation or deallocation operations.

Constructor & Destructor Documentation

◆ cfg_node_t()

template<class runtime_cfg_t >
MHAPlugin::cfg_node_t< runtime_cfg_t >::cfg_node_t ( runtime_cfg_t *  runtime_cfg)
explicit

Constructor for a singly linked list node.

Parameters
runtime_cfgPointer to a runtime configuration object that was just created on the heap. The newly constructed cfg_node_t object takes over object ownership of the pointed-to runtime configuration and will call delete on it in its destructor.

◆ ~cfg_node_t()

template<class runtime_cfg_t >
MHAPlugin::cfg_node_t< runtime_cfg_t >::~cfg_node_t

Destructor of the singly linked list node.

Will also delete the pointed-to data object (the runtime configuration)

Member Data Documentation

◆ next

template<class runtime_cfg_t >
std::atomic<cfg_node_t<runtime_cfg_t>*> MHAPlugin::cfg_node_t< runtime_cfg_t >::next

A pointer to the next node in the singly linked list.

On construction, this will be a NULL pointer. New objects can be appended to the singly linked list by writing the address to the next node into this data member. Since this pointer is std::atomic, writing to it is a release operation which means all threads seeing the new value of the next pointer will also see all other writes to memory that the thread doing this write has performed, which includes anything the constructor of the new runtime config has written and the assignment of the address of the new runtime config to the data pointer of the next node. This prevents making half-constructed runtime configuration objects visible to the consumer thread.

◆ not_in_use

template<class runtime_cfg_t >
std::atomic<bool> MHAPlugin::cfg_node_t< runtime_cfg_t >::not_in_use

Initially this data member is set to false by the constructor.

It is set to true by the consumer thread when it no longer needs the run time configuration pointed to by this node's data member. This bool is atomic because this node and the runtime object it points to can be deleted by the configuration thread as soon as value true is stored in this bool, which happens right after the processing thread acquires a pointer to the next node in the singly linked list. The atomic ensures all threads agree on this "right after" ordering.

◆ data

template<class runtime_cfg_t >
runtime_cfg_t* MHAPlugin::cfg_node_t< runtime_cfg_t >::data

A native pointer to the runtime configuration managed by this node.

The runtime configuration lives on the heap and is owned by this node. It is deleted in this node's destructor. The runtime configuration object must be created by client code with operator new before ownership is transferred to this node by passing a pointer to it as the constructor's parameter. This pointer does not need to be atomic, memory access ordering is ensured by the atomic next pointer and placing accesses to "data" in the correct places relative to accesses to "next".


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