Debian Codegraph @

RSPiX/Src/ORANGE/CDT/QUEUE.H

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////cxx . . $ `<file>/RSPiX/Src/ORANGE/CDT/QUEUE.H`/
2 //
3 // Copyright 2016 RWS Inc, All Rights Reserved
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of version 2 of the GNU General Public License as published by
7 // the Free Software Foundation
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 //
18 //
19 // queue.h
20 //
21 // History:
22 // 06/12/94 MR Added this comment block and made things safe
23 // for interrupt-driven usage.
24 //
25 // 06/14/94 MR Added function to remove the item at the tail of
26 // the queue.
27 //
28 // 06/15/94 MR Cleaned up the overall documentation to explain
29 // how interrupt-driven usage works with the queue.
30 //
31 // 06/13/95 JMI Converted to a template in attempt to make this class
32 // more useful. This class used to be defined in a .CPP
33 // but that proves too cumbersome for templates.
34 //
35 // 07/25/96 JMI PeekTail had a bug where it was returning the next after
36 // the tail.
37 //
38 // 10/30/96 JMI Changed:
39 // Old label: New label:
40 // ========= =========
41 // CQueue RQueue
42 //
43 //////////////////////////////////////////////////////////////////////////////
44 //
45 // This object implements a generic queue of void pointers.
46 //
47 // The queue GENERALLY works properly even if it is being accessed by both
48 // interrupt-driven and normal (non-interrupt-driven) code at the same time.
49 //
50 // A typical application would be for normal code to add items to the queue
51 // and for interrupt-driven code to remove those items and process them.
52 // Alternately, the interrupt code could add the items and the normal code
53 // could remove them. Either way, this queue basically works as expected.
54 //
55 // However, there are a few specific functions which could yield innacurate
56 // results or even cause major problems in such an environment! Each function
57 // is documented as to whether it will work or what the potential problems are.
58 //
59 // For example, let's say normal code is adding items to the queue and
60 // interrupt-driven code is removing them. Now let's say that the normal code
61 // calls NumItems() to see how many items are in the queue. If an interrupt
62 // were to occur right after that call, then the number that it returned
63 // would no longer be valid. This is, of course, a typical problem in an
64 // interrupt-driven environment. On the other hand, NumItems() really does
65 // have a specific problem! If an interrupt were to occur while NumItems()
66 // was calculating the number of items in the queue, and the queue happened
67 // to be in a certain state with regards to the head and tail "wrapping
68 // around" past the end, then the calculated number of items could be
69 // completely wrong, and could even be returned as a negative number!
70 //
71 // A work-around for this is to call NumItems() repeatedly until it returns
72 // the same value twice in a row. This value will be a valid number,
73 // although it still only refers to the state that the queue was in at
74 // that time! This solution assumes that interrupts are not occuring
75 // relentlessly at an extremely high rate. In other words, if the value
76 // returned by two successive calls is different, then it is due to an
77 // interrupt having occurred sometime during the course of those two calls.
78 // As long as interrupts are occuring at a reasonable rate, we can be
79 // reasonably sure that another won't occur in the time it takes to perform
80 // one or two more calls to NumItems(). Once again, these are typical
81 // problems and solutions in interrupt-driven environments.
82 //
83 // The head and tail indices are declared as volatile so that the compiler
84 // will not optimize the usage of those variables. This is important because
85 // you wouldn't want the compiler to put those variables into registers and
86 // make changes to them and then later write them back, because interrupt-
87 // driven code could have changed the same variable in the meantime! There
88 // currently aren't any situations where this would matter in this code, but
89 // it seems safer to make them volatile....just in case.
90 //
91 //////////////////////////////////////////////////////////////////////////////
92 #ifndef QUEUE_H
93 #define QUEUE_Hcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:93:9`!
94
95 // Size of our queue.
96 #define QSIZEcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:96:9`! (sizeof(m_aptQ) / sizeof(m_aptQ[0]))
97
98 // When given a queue index, these macro return the "next" or "previous" index,
99 // taking warp-around into account. Note that the given index must NEVER be
100 // modified, even temporarily, because that could cause bugs when the queue is
101 // used by interrupt-driven and normal code at the same time.
102 #define QNEXTcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:102:9`!(i) (((i)+(short)1) >= QSIZEcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:96:9`! ? (short)0 : ((i)+(short)1))
103 #define QPREVcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:103:9`!(i) (((i)-(short)1) >= 0 ? ((i)-(short)1) : (QSIZEcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:96:9`!-(short)1))
104
105 template <class T, int16_t sSize> class RQueuecxx . . $ RQueue#
106 {
107 public:
108 // Default constructor. You must call SetSize() before queue can be used.
109 // NOTICE: With mixed usage of normal and interrupt-driven code, this would
110 // typically be called from the normal code. It could be called from
111 // interrupt, but it seems sort of weird.
112 RQueuecxx . . $ RQueue#`RQueue<T, sSize>`(49f6e7a06ebc5aa8).(void) { Resetcxx . . $ RQueue#Reset(49f6e7a06ebc5aa8).(); }
113
114 // Destructor.
115 // NOTICE: With mixed usage of normal and interrupt-driven code, this would
116 // typically be called from the normal code. It could be called from
117 // interrupt, but it seems sort of weird.
118 ~cxx . . $ RQueue#`~RQueue<T, sSize>`(49f6e7a06ebc5aa8).RQueue(void) { }
119
120 // Reset the queue, thereby losing track of everything that may be in it.
121 // NOTICE: No problems with mixed usage of normal and interrupt-driven code!
122 void Resetcxx . . $ RQueue#Reset(49f6e7a06ebc5aa8).(void)
123 {
124 // Head and tail both start at 0
125 m_sHeadcxx . . $ RQueue#m_sHead. = 0;
126 m_sTailcxx . . $ RQueue#m_sTail. = 0;
127 }
128
129 // Enqueue the indicated item (0 = success, non-zero = didn't fit)
130 // NOTICE: No problems with mixed usage of normal and interrupt-driven code!
131 int16_t EnQcxx . . $ RQueue#EnQ(b0b7078a86b84ddc).(T* pT)
132 {
133 // Make sure it isn't already full
134 if (IsNotFullcxx . . $ RQueue#IsNotFull(4c3706f7850cf040).())
135 {
136 // Add value at tail of queue, then increment tail (NOTE: Must be
137 // done in this order to allow interrupt-driven usage.)
138 m_aptQcxx . . $ RQueue#m_aptQ.[m_sTailcxx . . $ RQueue#m_sTail.] = pT;
139 m_sTailcxx . . $ RQueue#m_sTail. = QNEXTcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:102:9`!(m_sTail);
140 return 0; // success!
141 }
142 else
143 return 1; // failure!
144 }
145
146
147 // Dequeue the next item (NULL = empty)
148 // NOTICE: No problems with mixed usage of normal and interrupt-driven code!
149 T* DeQcxx . . $ RQueue#DeQ(155dc7f103cdb9db).(void)
150 {
151 // Check if there's anything to dequeue
152 if (IsNotEmptycxx . . $ RQueue#IsNotEmpty(4c3706f7850cf040).())
153 {
154 // Get value at head of queue, then increment head (NOTE: Must be
155 // done in this order to allow interrupt-driven usage.)
156 T* ptTemp = m_aptQcxx . . $ RQueue#m_aptQ.[m_sHeadcxx . . $ RQueue#m_sHead.];
157 m_sHeadcxx . . $ RQueue#m_sHead. = QNEXTcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:102:9`!(m_sHead);
158 return ptTemp;
159 }
160 else
161 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
162 }
163
164 // Remove the item at the tail of the queue (NULL = empty).
165 // WARNING: NEVER call this under mixed usage of normal and interrupt-driven
166 // code! It would only be safe if (1) called from normal code when interrupts
167 // are disabled or (2) from interrupt code when it is certain that normal code
168 // is not accessing the queue.
169 T* RemoveTailcxx . . $ RQueue#RemoveTail(155dc7f103cdb9db).(void)
170 {
171 // Check if there's anything to remove
172 if (IsNotEmptycxx . . $ RQueue#IsNotEmpty(4c3706f7850cf040).())
173 {
174 // Decrement tail, then get the item that was at the tail.
175 m_sTailcxx . . $ RQueue#m_sTail. = QPREVcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:103:9`!(m_sTail);
176 return m_aptQcxx . . $ RQueue#m_aptQ.[m_sTailcxx . . $ RQueue#m_sTail.];
177 }
178 else
179 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
180 }
181
182 // Check if queue is empty (returns TRUE if it's empty)
183 // NOTICE: No problems with mixed usage of normal and interrupt-driven code!
184 int16_t IsEmptycxx . . $ RQueue#IsEmpty(4c3706f7850cf040).(void)
185 { return (int16_t)(m_sHeadcxx . . $ RQueue#m_sHead. == m_sTailcxx . . $ RQueue#m_sTail.); }
186
187 // Check if queue is not empty (returns TRUE if it's not empty)
188 // NOTICE: No problems with mixed usage of normal and interrupt-driven code!
189 int16_t IsNotEmptycxx . . $ RQueue#IsNotEmpty(4c3706f7850cf040).(void)
190 { return (int16_t)(m_sHeadcxx . . $ RQueue#m_sHead. != m_sTailcxx . . $ RQueue#m_sTail.); }
191
192 // Check if queue is full (returns TRUE if it's full)
193 // NOTICE: No problems with mixed usage of normal and interrupt-driven code!
194 int16_t IsFullcxx . . $ RQueue#IsFull(4c3706f7850cf040).(void)
195 { return (int16_t)(QNEXTcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:102:9`!(m_sTail) == m_sHeadcxx . . $ RQueue#m_sHead.); }
196
197 // Check if queue is not full (returns TRUE if it's not full)
198 // NOTICE: No problems with mixed usage of normal and interrupt-driven code!
199 int16_t IsNotFullcxx . . $ RQueue#IsNotFull(4c3706f7850cf040).(void)
200 { return (int16_t)(QNEXTcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:102:9`!(m_sTail) != m_sHeadcxx . . $ RQueue#m_sHead.); }
201
202 // Get number of items currently in the queue
203 // WARNING: With mixed usage of normal and interrupt-driven code, this could
204 // yield innacurate results! In such cases you can call this function repeatedly
205 // until it returns the same result two times in a row. This works fine as long
206 // as interrupts aren't occuring relentlessly at an extremely high rate!
207 int16_t NumItemscxx . . $ RQueue#NumItems(4c3706f7850cf040).(void)
208 { return m_sTailcxx . . $ RQueue#m_sTail. >= m_sHeadcxx . . $ RQueue#m_sHead. ? m_sTailcxx . . $ RQueue#m_sTail. - m_sHeadcxx . . $ RQueue#m_sHead. : (m_sTailcxx . . $ RQueue#m_sTail. + QSIZEcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:96:9`!) - m_sHeadcxx . . $ RQueue#m_sHead.; };
209
210 // "Peek" at head item in the queue
211 // WARNING: Could yield inaccurate results if EnQ() or DeQ() are interrupt-driven!
212 T* Peekcxx . . $ RQueue#Peek(155dc7f103cdb9db).(void) { return (IsNotEmptycxx . . $ RQueue#IsNotEmpty(4c3706f7850cf040).() ? m_aptQcxx . . $ RQueue#m_aptQ.[m_sHeadcxx . . $ RQueue#m_sHead.] : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); }
213
214 // "Peek" at tail item in the queue.
215 // WARNING: Could yield inaccurate results if EnQ() or DeQ() are interrupt-driven!
216 T* PeekTailcxx . . $ RQueue#PeekTail(155dc7f103cdb9db).(void) { return (IsNotEmptycxx . . $ RQueue#IsNotEmpty(4c3706f7850cf040).() ? m_aptQcxx . . $ RQueue#m_aptQ.[QPREVcxx . . $ `RSPiX/Src/ORANGE/CDT/QUEUE.H:103:9`!(m_sTail)] : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); }
217
218 protected:
219 volatile int16_t m_sHeadcxx . . $ RQueue#m_sHead.; // volatile since it could change on interrupt!
220 volatile int16_t m_sTailcxx . . $ RQueue#m_sTail.; // volatile since it could change on interrupt!
221 T* m_aptQcxx . . $ RQueue#m_aptQ.[sSize + 1];
222 };
223
224 #endif // QUEUE_H