Details on lock-free buffer management » History » Version 3

Eric Flumerfelt, 10/16/2018 08:38 AM

1 1 Eric Flumerfelt
h1. Details on lock-free buffer management
2 1 Eric Flumerfelt
3 1 Eric Flumerfelt
The _artdaq_ shared memory interface uses several flags in the buffer descriptor structure to achieve self-contained, lock-free operation. 
4 1 Eric Flumerfelt
5 1 Eric Flumerfelt
# std::atomic<BufferSemaphoreFlags> sem
6 1 Eric Flumerfelt
** The state of the buffer. One of Empty, Writing, Full, or Reading.
7 1 Eric Flumerfelt
# std::atomic<int16_t> sem_id
8 1 Eric Flumerfelt
** The current owner of the buffer. Each instance of SharedMemoryManager takes an ID number (from ShmStruct::std::atomic<int> next_id), and uses this number to indicate that it is the current owner of the buffer. Buffers without owners have sem_id set to -1.
9 1 Eric Flumerfelt
# std::atomic<uint64_t> last_touch_time
10 1 Eric Flumerfelt
** The last time (microseconds, system clock) the buffer was "touched". Buffers should be touched whenever a modifying operation occurs, such as writes or reads, or before the buffer transitions from an inactive state (Empty, Full) to an active state (Writing, Reading).
11 2 Eric Flumerfelt
12 2 Eric Flumerfelt
* The checkBuffer_ function is used to determine if the buffer's sem and sem_id are currently set to the expected values. No read or write operations should be performed without calling this function.
13 2 Eric Flumerfelt
* Buffer touch time should only be updated by the owner of the buffer, except the case of unowned (sem_id == -1) buffers which can be updated by anyone.
14 2 Eric Flumerfelt
** The logic for resetting buffers from an active state to a passive state is in the ResetBuffer function. Additionally, when buffer read operations do not empty the buffer, ResetBuffer ensures that old broadcasts are removed from the shared memory once they have timed out.
15 3 Eric Flumerfelt
16 3 Eric Flumerfelt
!buffer state diagram.png!