linuxsampler 2.3.1
LinuxSampler::SynchronizedConfig< T > Class Template Reference

Thread-safe management of configuration data, where the data is updated by a single non real time thread and read by a number of real time threads. More...

#include <SynchronizedConfig.h>

Inheritance diagram for LinuxSampler::SynchronizedConfig< T >:
LinuxSampler::BackBuffer< T >

Classes

class  Reader
 

Public Member Functions

 SynchronizedConfig ()
 
TGetConfigForUpdate ()
 Gets the configuration object for use by the non real time thread.
 
const TGetUnsafeUpdateConfig () const
 Get the data on update side for read-only access.
 
TSwitchConfig ()
 Atomically switch the newly updated configuration object with the one used by the real time thread, then wait for the real time thread to finish working with the old object before returning the old object.
 

Detailed Description

template<class T>
class LinuxSampler::SynchronizedConfig< T >

Thread-safe management of configuration data, where the data is updated by a single non real time thread and read by a number of real time threads.

The synchronization is achieved by using two instances of the configuration data. The non real time thread gets access to the instance not currently in use by the real time threads by calling GetConfigForUpdate(). After the data is updated, the non real time thread must call SwitchConfig() and redo the update on the other instance. SwitchConfig() blocks until it is safe to modify the other instance.

The real time threads need one Reader object each to access the configuration data. This object must be created outside the real time thread. The Lock() function returns a reference to the data to be read, and Unlock() must be called when finished reading the data. (Neither Lock nor Unlock will block the real time thread, or use any system calls.)

Note that the non real time side isn't safe for concurrent access, so if there are multiple non real time threads that update the configuration data, a mutex has to be used.

Implementation note: the memory order parameters and fences are very carefully chosen to make the code fast but still safe for memory access reordering made by the CPU.

Definition at line 60 of file SynchronizedConfig.h.

Constructor & Destructor Documentation

◆ SynchronizedConfig()

template<class T >
LinuxSampler::SynchronizedConfig< T >::SynchronizedConfig ( )

Definition at line 153 of file SynchronizedConfig.h.

Member Function Documentation

◆ GetConfigForUpdate()

template<class T >
T & LinuxSampler::SynchronizedConfig< T >::GetConfigForUpdate ( )

Gets the configuration object for use by the non real time thread.

The object returned is not in use by the real time thread, so it can safely be updated. After the update is done, the non real time thread must call SwitchConfig() and the same update must be done again.

Returns
a reference to the configuration object to be updated by the non real time thread

Definition at line 158 of file SynchronizedConfig.h.

◆ GetUnsafeUpdateConfig()

template<class T >
const T & LinuxSampler::SynchronizedConfig< T >::GetUnsafeUpdateConfig ( ) const
inline

Get the data on update side for read-only access.

Definition at line 128 of file SynchronizedConfig.h.

◆ SwitchConfig()

template<class T >
T & LinuxSampler::SynchronizedConfig< T >::SwitchConfig ( )

Atomically switch the newly updated configuration object with the one used by the real time thread, then wait for the real time thread to finish working with the old object before returning the old object.

SwitchConfig() must be called by the non real time thread after an update has been done, and the object returned must be updated in the same way as the first.

Returns
a reference to the configuration object to be updated by the non real time thread

Definition at line 162 of file SynchronizedConfig.h.

References LinuxSampler::atomic_thread_fence(), LinuxSampler::memory_order_acquire, LinuxSampler::memory_order_release, and LinuxSampler::memory_order_seq_cst.


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