Project

General

Profile

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

Eric Flumerfelt, 10/16/2018 10:00 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 6 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. Buffers may be touched when in any mode. The only negative side effect of a buffer being touched out-of-turn is that it may take longer for a timeout to occur.
14 1 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 6 Eric Flumerfelt
16 6 Eric Flumerfelt
h2. Operations on buffers
17 6 Eric Flumerfelt
18 6 Eric Flumerfelt
h3. Acquire a buffer for reading/writing
19 6 Eric Flumerfelt
20 7 Eric Flumerfelt
# Check @sem_id == -1@
21 6 Eric Flumerfelt
# Check sem for corresponding inactive state (Empty for writing, Full for reading)
22 6 Eric Flumerfelt
# Touch buffer
23 6 Eric Flumerfelt
# Update sem_id
24 7 Eric Flumerfelt
# Check @sem_id == manager_id_@
25 6 Eric Flumerfelt
# Update sem
26 6 Eric Flumerfelt
# Check sem
27 7 Eric Flumerfelt
# Check @sem_id == manager_id_@
28 6 Eric Flumerfelt
# Touch buffer
29 6 Eric Flumerfelt
30 6 Eric Flumerfelt
h3. Read/Write from buffer
31 6 Eric Flumerfelt
32 7 Eric Flumerfelt
# Check @sem_id == manager_id_@
33 7 Eric Flumerfelt
# Check @sem == Reading/Writing@
34 6 Eric Flumerfelt
# Touch buffer
35 1 Eric Flumerfelt
# Read from/Write to buffer
36 1 Eric Flumerfelt
# Touch buffer
37 1 Eric Flumerfelt
38 1 Eric Flumerfelt
h3. Check buffer for timeout in non-broadcast mode
39 1 Eric Flumerfelt
40 7 Eric Flumerfelt
# Read last_touch_time and calculate delta from current time
41 8 Eric Flumerfelt
# IF @delta < timeout@ OR @sem == Empty@ *return false*
42 8 Eric Flumerfelt
# IF @sem_id == manager_id_@ AND @sem == Writing@ *return true* <-- This should never happen!
43 7 Eric Flumerfelt
# IF @sem_id != manager_id_@ AND @sem == Reading@ THEN
44 7 Eric Flumerfelt
## Re-calculate delta
45 8 Eric Flumerfelt
## IF @delta < timeout@ *return false*
46 8 Eric Flumerfelt
## *return true*
47 8 Eric Flumerfelt
# *return false*
48 7 Eric Flumerfelt
49 1 Eric Flumerfelt
h3. Check buffer for timeout in broadcast mode
50 7 Eric Flumerfelt
51 7 Eric Flumerfelt
# Read last_touch_time and calculate delta from current time
52 8 Eric Flumerfelt
# IF @delta < timeout@ OR @sem == Empty@ *return false*
53 8 Eric Flumerfelt
# IF @sem_id == manager_id_@ AND @sem == Writing@ *return true* <-- This should never happen!
54 7 Eric Flumerfelt
# IF @sem == Full@ AND (@manager_id_ == 0@ OR buffer already seen) THEN
55 7 Eric Flumerfelt
## Reset buffer to Empty state
56 8 Eric Flumerfelt
## *return true*
57 7 Eric Flumerfelt
# IF @sem_id != manager_id_@ AND @sem == Reading@ THEN
58 7 Eric Flumerfelt
## Re-calculate delta
59 8 Eric Flumerfelt
## IF @delta < timeout@ *return false*
60 7 Eric Flumerfelt
## Reset buffer to Full state
61 8 Eric Flumerfelt
## *return true*
62 8 Eric Flumerfelt
# *return false*
63 3 Eric Flumerfelt
64 5 Eric Flumerfelt
!buffer_state_diagram.png!