fesa-core
5.0.1
|
#include <BufferSynchronizer.h>
Public Member Functions | |
BufferSynchronizer (DataManager &dataManager, boost::optional< int > priority) | |
void | newReader () |
void | releaseReader () |
uint16_t | getReaderIndex () const |
uint32_t | triggerSynchronization () |
uint32_t | getSynchronizationCount () const |
void | waitForSynchronization (uint32_t synchronizationId) const |
void | newWriter () |
void | releaseWriter () |
std::size_t | getSharedFieldsSize (bool multiProcess) const |
void | mapSharedFields (char *memory, bool multiProcess, bool first) |
Public Member Functions inherited from fesa::Thread | |
Thread () | |
Constructor. More... | |
virtual | ~Thread () |
Destructor. | |
void | setDetachState (int32_t state) |
Modify the detach state of the thread. More... | |
void | start (bool blockingMode, const std::string &threadName, bool permanent=true) |
virtual void | stop () |
Stop the thread. | |
int | join (void **status) |
bool | isRunning () const |
Check if the thread is running. More... | |
pthread_t | getID () const |
Get the process ID. More... | |
int32_t | getPriority () const |
Get the priority of the thread. More... | |
cpu_set_t | getAffinity () const |
Get the affinity of the thread. | |
void | setPriority (int32_t priority) |
Set the priority of the thread. More... | |
void | setAffinity (cpu_set_t affinity) |
Set the affinity of the thread. More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from fesa::Thread | |
static void | AdjustProcessScheduling () |
The process priority can only be adjusted in the main thread. It needs to be adjusted to the maximal thread priority since it sets the limit of the thread priorities of this process. More... | |
static std::string | getThreadName (pthread_t threadId) |
Get the name of the thread by its id. More... | |
static std::string | getCurrentThreadName () |
static void | registerThreadIdName (pthread_t threadId, const std::string &threadName) |
Associate a thread id with a name. More... | |
static void | registerThreadId (pthread_t threadId, Thread *thread) |
Associate a thread id with a name. More... | |
static Thread * | current () |
Returns the Thread object associated with the current thread. | |
static Thread * | getById (pthread_t id) |
Returns the Thread object associated with the given id. Throws FesaException if the id is unknown. More... | |
static const std::set< const Thread * > | getThreads () |
Protected Attributes inherited from fesa::Thread | |
bool | isRunning_ |
true if the thread is running | |
bool | hasFinished_ |
true if the thread has finished. | |
bool | joinHasFinished_ |
if true, a call to pthread_join has completed and a further pthread_cancel / pthread_join call is unsafe | |
bool | permanent_ |
pthread_t | tid_ |
Id of this thread. | |
BufferSynchronizer ensures data integrity between readers and writers. Readers never block; writers may block. There is a reference buffer modifiable by the writers. There are two buffers accessible by the readers ("reader buffers"). When a reader starts reading, it is registered on one of the two reader buffers. As long as a reader is registered on a buffer, this buffer cannot be modified. When an update is triggered, the reference buffer is copied to one of the two reader buffers. If none of the reader buffers can be modified, update is blocked until a reader buffer becomes modifiable. Writers can still modify the reference buffer while the update is blocked; they will only be blocked during the actual copy of the data from the reference buffer to one of the reader buffers.
fesa::BufferSynchronizer::BufferSynchronizer | ( | DataManager & | dataManager, |
boost::optional< int > | priority | ||
) |
Constructor.
dataManager | reference to the DataManager associated to this synchronizer |
priority | optional priority for the BufferSynchronizer thread |
uint16_t fesa::BufferSynchronizer::getReaderIndex | ( | ) | const |
Returns the index of the buffer assigned to this reader. Can only be called after newReader() has been called, and before calling releaseReader().
std::size_t fesa::BufferSynchronizer::getSharedFieldsSize | ( | bool | multiProcess | ) | const |
Returns the size of the shared fields of this object.
multiProcess | whether the shared fields will be access by several processes or not. |
uint32_t fesa::BufferSynchronizer::getSynchronizationCount | ( | ) | const |
Returns the number of synchronizations done. Can be used to check if values were updated since the last call to triggerSynchronization();
void fesa::BufferSynchronizer::mapSharedFields | ( | char * | memory, |
bool | multiProcess, | ||
bool | first | ||
) |
Maps the shared fields of this object, starting from the given address.
memory | pointer to a shared memory segment large enough to hold all the shared fields of this object. |
multiProcess | whether the shared fields will be access by several processes or not. |
first | whether this is the first process to map the fields or not. |
void fesa::BufferSynchronizer::newReader | ( | ) |
This method must be called once by a thread before it can access values. It increments the reference count of the current reader buffer.
void fesa::BufferSynchronizer::newWriter | ( | ) |
May be called by a writer before it modifies the reference buffer. If any writer modifies the reference buffer without calling this method first, then data integrity must be ensured at the DataManager level when synchronization happens.
void fesa::BufferSynchronizer::releaseReader | ( | ) |
This method must be called once by a thread when it has finished reading values. It decrements the reference count of the buffer this thread is registered on.
void fesa::BufferSynchronizer::releaseWriter | ( | ) |
To be called by a writer when it has finished modifying the reference buffer.
uint32_t fesa::BufferSynchronizer::triggerSynchronization | ( | ) |
Triggers a synchronization of the values. Can be called from any thread. This call is asynchronous; the actual synchronization happens as soon as a reader buffer is available. Must be called with the writer mutex UNLOCKED. Values are copied from the reference buffer.
void fesa::BufferSynchronizer::waitForSynchronization | ( | uint32_t | synchronizationId | ) | const |
Waits for synchronization with the given id to happen. Returns immediately if it has already happened.
id | the id of the synchronization to wait for |