VR-Vantage
Loading...
Searching...
No Matches
locklessQueue.inl
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2014 MAK Technologies, Inc.
3** All rights reserved.
4*******************************************************************************/
5
8
9
10#ifndef DtLocklessQueue_INL_
11 #error This file may only be included by DtLocklessQueue.h
12#endif
13
14#include <vlutil/vlProcessControl.h>
15
16namespace makRadarFx
17{
18template <class T>
20 : myReadHead(-1)
21 , myWriteHead(0)
22{
23 myElements.resize(size);
24}
26template <class T>
32template <class T>
35 int tempWriteHead = (myWriteHead + 1) % myElements.size();
36 //even though the write will succeed, the write head will
37 //not be able to advance if the read head is there,
38 //so pushing would block until the read had is advanced
39 return !(tempWriteHead == myReadHead);
40}
42template <class T>
43void DtLocklessQueue<T>::push(T newElement)
44{
45 //write the element at the reserved write spot
46 myElements[myWriteHead] = newElement;
47
48 //find the next available spot
49 int tempWriteHead = (myWriteHead + 1) % myElements.size();
50
51 //wait for read head to move forward so that we don't go past it
52 while (tempWriteHead == myReadHead)
53 {
54 DtSleep(0.001);
55 }
56
57 //actually increment the write head
58 myWriteHead = tempWriteHead;
59
60 //set the read head on the valid data so that we don't get into a state
61 //where the write head wraps around and overwrites unread data
62 //this will prevent that and cause pushes in that case to block, protecting the
63 //data in the queue until some stuff gets popped off.
64 if (myReadHead == -1)
65 {
66 myReadHead = 0;
67 }
68}
69
70template <class T>
72{
73 //uninitialized, nothing to read, so sleep
74 while (myReadHead == -1)
75 {
76 DtSleep(0.001);
77 }
78
79 //verify that there is something to read. Can't read if the write head is over
80 //this spot, since that means it hasn't written anything yet
81 while (myReadHead == myWriteHead)
82 {
83 DtSleep(0.001);
84 }
85
86 //get the data
87 T temp = myElements[myReadHead];
88
89 //it's safe to read the next value, so increment the read head and get the data
90 myReadHead = (myReadHead + 1) % myElements.size();
91
92 return temp;
93}
94
95template <class T>
97{
98 //get the values at this point in time, they could change during this function processing
99 int wh = myWriteHead;
100 int rh = myReadHead;
101
102 if (rh == -1)
103 {
104 return 0;
105 }
106
107 if (wh >= rh) //the write head is in front of the read head, and hasn't circled around the buffer
108 {
109 return wh - rh;
110 }
111 else
112 {
113 // add the buffer size to the wh value so we can figure how far apart they are
114 return (wh + myElements.size()) - rh;
115 }
116}
117}
int myWriteHead
Definition locklessQueue.h:57
std::vector< T > myElements
Definition locklessQueue.h:55
T pop()
Remove an element from the queue.
Definition locklessQueue.inl:71
DtLocklessQueue()
Default CTOR (unimplemented).
int myReadHead
Definition locklessQueue.h:56
bool okToPush()
Definition locklessQueue.inl:33
int count()
Get the current element count.
Definition locklessQueue.inl:96
~DtLocklessQueue()
DTOR.
Definition locklessQueue.inl:27
void push(T newElement)
Add an element on the queue.
Definition locklessQueue.inl:43
Definition radarFxConnector.h:28