| rev |
line source |
|
Me@19
|
1 /*
|
|
Me@19
|
2 * Copyright 2009 OpenSourceStewardshipFoundation.org
|
|
Me@19
|
3 * Licensed under GNU General Public License version 2
|
|
Me@19
|
4 *
|
|
Me@19
|
5 * Author: seanhalle@yahoo.com
|
|
Me@19
|
6 */
|
|
Me@19
|
7
|
|
Me@19
|
8
|
|
Me@19
|
9 #include <stdio.h>
|
|
Me@19
|
10 #include <errno.h>
|
|
Me@19
|
11 #include <pthread.h>
|
|
Me@19
|
12 #include <stdlib.h>
|
|
Me@19
|
13 #include <sched.h>
|
|
seanhalle@29
|
14 #include <string.h>
|
|
Me@19
|
15
|
|
Me@19
|
16 #include "BlockingQueue.h"
|
|
Me@19
|
17
|
|
Me@19
|
18 #define INC(x) (++x == 1024) ? (x) = 0 : (x)
|
|
Me@19
|
19
|
|
Me@19
|
20 #define SPINLOCK_TRIES 100000
|
|
Me@19
|
21
|
|
Me@19
|
22
|
|
Me@19
|
23
|
|
Me@19
|
24 //===========================================================================
|
|
Me@19
|
25 // multi reader multi writer fast Q via CAS
|
|
Me@19
|
26 #ifndef _GNU_SOURCE
|
|
Me@19
|
27 #define _GNU_SOURCE
|
|
Me@19
|
28
|
|
Me@19
|
29 /*This is a blocking queue, but it uses CAS instr plus yield() when empty
|
|
Me@19
|
30 * or full
|
|
Me@19
|
31 *It uses CAS because it's meant to have more than one reader and more than
|
|
Me@19
|
32 * one writer.
|
|
Me@19
|
33 */
|
|
Me@19
|
34
|
|
Me@19
|
35 CASQueueStruc* makeCASQ()
|
|
Me@19
|
36 {
|
|
Me@19
|
37 CASQueueStruc* retQ;
|
|
seanhalle@27
|
38 retQ = (CASQueueStruc *) VMS_WL__malloc( sizeof( CASQueueStruc ) );
|
|
Me@19
|
39
|
|
Me@19
|
40 retQ->insertLock = UNLOCKED;
|
|
Me@19
|
41 retQ->extractLock= UNLOCKED;
|
|
seanhalle@29
|
42
|
|
seanhalle@29
|
43 retQ->extractPos = (volatile void**)&(retQ->startOfData[0]); //side by side == empty
|
|
seanhalle@29
|
44 retQ->insertPos = (volatile void**)&(retQ->startOfData[1]); // so start pos's have to be
|
|
Me@19
|
45 retQ->endOfData = &(retQ->startOfData[1023]);
|
|
Me@19
|
46
|
|
Me@19
|
47 return retQ;
|
|
Me@19
|
48 }
|
|
Me@19
|
49
|
|
Me@19
|
50
|
|
Me@19
|
51 void* readCASQ( CASQueueStruc* Q )
|
|
Me@19
|
52 { void *out = 0;
|
|
Me@19
|
53 int tries = 0;
|
|
Me@19
|
54 void **startOfData = Q->startOfData;
|
|
Me@19
|
55 void **endOfData = Q->endOfData;
|
|
Me@19
|
56
|
|
Me@19
|
57 int gotLock = FALSE;
|
|
Me@19
|
58
|
|
Me@19
|
59 while( TRUE )
|
|
Me@19
|
60 { //this intrinsic returns true if the lock held "UNLOCKED", in which
|
|
Me@19
|
61 // case it now holds "LOCKED" -- if it already held "LOCKED", then
|
|
Me@19
|
62 // gotLock is FALSE
|
|
Me@19
|
63 gotLock =
|
|
Me@19
|
64 __sync_bool_compare_and_swap( &(Q->extractLock), UNLOCKED, LOCKED );
|
|
Me@19
|
65 //NOTE: checked assy, and it does lock correctly..
|
|
Me@19
|
66 if( gotLock )
|
|
Me@19
|
67 {
|
|
seanhalle@29
|
68 void **insertPos = (void **)Q->insertPos;
|
|
seanhalle@29
|
69 void **extractPos = (void **)Q->extractPos;
|
|
Me@19
|
70
|
|
Me@19
|
71 //if not empty -- extract just below insert when empty
|
|
Me@19
|
72 if( insertPos - extractPos != 1 &&
|
|
Me@19
|
73 !(extractPos == endOfData && insertPos == startOfData))
|
|
Me@19
|
74 { //move before read
|
|
Me@19
|
75 if( extractPos == endOfData ) //write new pos exactly once, correctly
|
|
Me@19
|
76 { Q->extractPos = startOfData; //can't overrun then fix it 'cause
|
|
Me@19
|
77 } // other thread might read bad pos
|
|
Me@19
|
78 else
|
|
Me@19
|
79 { Q->extractPos++;
|
|
Me@19
|
80 }
|
|
seanhalle@29
|
81 out = (void *) *(Q->extractPos);
|
|
Me@19
|
82 Q->extractLock = UNLOCKED;
|
|
Me@19
|
83 return out;
|
|
Me@19
|
84 }
|
|
Me@19
|
85 else //Q is empty
|
|
Me@19
|
86 { Q->extractLock = UNLOCKED;//empty, so release lock for others
|
|
Me@19
|
87 }
|
|
Me@19
|
88 }
|
|
Me@19
|
89 //Q is busy or empty
|
|
Me@19
|
90 tries++;
|
|
Me@19
|
91 if( tries > SPINLOCK_TRIES ) pthread_yield(); //not reliable
|
|
Me@19
|
92 }
|
|
Me@19
|
93 }
|
|
Me@19
|
94
|
|
Me@19
|
95 void writeCASQ( void * in, CASQueueStruc* Q )
|
|
Me@19
|
96 {
|
|
Me@19
|
97 int tries = 0;
|
|
Me@19
|
98 //TODO: need to make Q volatile? Want to do this Q in assembly!
|
|
Me@19
|
99 //Have no idea what GCC's going to do to this code
|
|
Me@19
|
100 void **startOfData = Q->startOfData;
|
|
Me@19
|
101 void **endOfData = Q->endOfData;
|
|
Me@19
|
102
|
|
Me@19
|
103 int gotLock = FALSE;
|
|
Me@19
|
104
|
|
Me@19
|
105 while( TRUE )
|
|
Me@19
|
106 { //this intrinsic returns true if the lock held "UNLOCKED", in which
|
|
Me@19
|
107 // case it now holds "LOCKED" -- if it already held "LOCKED", then
|
|
Me@19
|
108 // gotLock is FALSE
|
|
Me@19
|
109 gotLock =
|
|
Me@19
|
110 __sync_bool_compare_and_swap( &(Q->insertLock), UNLOCKED, LOCKED );
|
|
Me@19
|
111 if( gotLock )
|
|
Me@19
|
112 {
|
|
seanhalle@29
|
113 void **insertPos = (void **)Q->insertPos;
|
|
seanhalle@29
|
114 void **extractPos = (void **)Q->extractPos;
|
|
Me@19
|
115
|
|
Me@19
|
116 //check if room to insert.. can't use a count variable
|
|
Me@19
|
117 // 'cause both insertor Thd and extractor Thd would write it
|
|
Me@19
|
118 if( extractPos - insertPos != 1 &&
|
|
Me@19
|
119 !(insertPos == endOfData && extractPos == startOfData))
|
|
Me@19
|
120 { *(Q->insertPos) = in; //insert before move
|
|
Me@19
|
121 if( insertPos == endOfData )
|
|
Me@19
|
122 { Q->insertPos = startOfData;
|
|
Me@19
|
123 }
|
|
Me@19
|
124 else
|
|
Me@19
|
125 { Q->insertPos++;
|
|
Me@19
|
126 }
|
|
Me@19
|
127 Q->insertLock = UNLOCKED;
|
|
Me@19
|
128 return;
|
|
Me@19
|
129 }
|
|
Me@19
|
130 else //Q is full
|
|
Me@19
|
131 { Q->insertLock = UNLOCKED;//full, so release lock for others
|
|
Me@19
|
132 }
|
|
Me@19
|
133 }
|
|
Me@19
|
134 tries++;
|
|
Me@19
|
135 if( tries > SPINLOCK_TRIES ) pthread_yield(); //not reliable
|
|
Me@19
|
136 }
|
|
Me@19
|
137 }
|
|
Me@19
|
138
|
|
Me@19
|
139 #endif //_GNU_SOURCE
|
|
Me@19
|
140
|
|
Me@19
|
141
|
|
Me@19
|
142 //===========================================================================
|
|
Me@19
|
143 //Single reader single writer super fast Q.. no atomic instrs..
|
|
Me@19
|
144
|
|
Me@19
|
145
|
|
Me@19
|
146 /*This is a blocking queue, but it uses no atomic instructions, just does
|
|
Me@19
|
147 * yield() when empty or full
|
|
Me@19
|
148 *
|
|
Me@19
|
149 *It doesn't need any atomic instructions because only a single thread
|
|
Me@19
|
150 * extracts and only a single thread inserts, and it has no locations that
|
|
Me@19
|
151 * are written by both. It writes before moving and moves before reading,
|
|
Me@19
|
152 * and never lets write position and read position be the same, so dis-
|
|
Me@19
|
153 * synchrony can only ever cause an unnecessary call to yield(), never a
|
|
Me@19
|
154 * wrong value (by monotonicity of movement of pointers, plus single writer
|
|
Me@19
|
155 * to pointers, plus sequence of write before change pointer, plus
|
|
Me@19
|
156 * assumptions that if thread A semantically writes X before Y, then thread
|
|
Me@19
|
157 * B will see the writes in that order.)
|
|
Me@19
|
158 */
|
|
Me@19
|
159
|
|
Me@19
|
160 SRSWQueueStruc* makeSRSWQ()
|
|
Me@19
|
161 {
|
|
Me@19
|
162 SRSWQueueStruc* retQ;
|
|
seanhalle@27
|
163 retQ = (SRSWQueueStruc *) VMS_WL__malloc( sizeof( SRSWQueueStruc ) );
|
|
Me@19
|
164 memset( retQ->startOfData, 0, 1024 * sizeof(void *) );
|
|
Me@19
|
165
|
|
Me@19
|
166 retQ->extractPos = &(retQ->startOfData[0]); //side by side == empty
|
|
Me@19
|
167 retQ->insertPos = &(retQ->startOfData[1]); // so start pos's have to be
|
|
Me@19
|
168 retQ->endOfData = &(retQ->startOfData[1023]);
|
|
Me@19
|
169
|
|
Me@19
|
170 return retQ;
|
|
Me@19
|
171 }
|
|
Me@19
|
172
|
|
Me@19
|
173 void
|
|
Me@19
|
174 freeSRSWQ( SRSWQueueStruc* Q )
|
|
Me@19
|
175 {
|
|
Me@22
|
176 VMS_int__free( Q );
|
|
Me@19
|
177 }
|
|
Me@19
|
178
|
|
Me@19
|
179 void* readSRSWQ( SRSWQueueStruc* Q )
|
|
Me@19
|
180 { void *out = 0;
|
|
Me@19
|
181 int tries = 0;
|
|
Me@19
|
182
|
|
Me@19
|
183 while( TRUE )
|
|
Me@19
|
184 {
|
|
Me@19
|
185 if( Q->insertPos - Q->extractPos != 1 &&
|
|
Me@19
|
186 !(Q->extractPos == Q->endOfData && Q->insertPos == Q->startOfData))
|
|
Me@19
|
187 { if( Q->extractPos >= Q->endOfData ) Q->extractPos = Q->startOfData;
|
|
Me@19
|
188 else Q->extractPos++; //move before read
|
|
Me@19
|
189 out = *(Q->extractPos);
|
|
Me@19
|
190 return out;
|
|
Me@19
|
191 }
|
|
Me@19
|
192 //Q is empty
|
|
Me@19
|
193 tries++;
|
|
Me@19
|
194 if( tries > SPINLOCK_TRIES ) pthread_yield();
|
|
Me@19
|
195 }
|
|
Me@19
|
196 }
|
|
Me@19
|
197
|
|
Me@19
|
198
|
|
Me@19
|
199 void* readSRSWQ_NonBlocking( SRSWQueueStruc* Q )
|
|
Me@19
|
200 { void *out = 0;
|
|
Me@19
|
201 int tries = 0;
|
|
Me@19
|
202
|
|
Me@19
|
203 while( TRUE )
|
|
Me@19
|
204 {
|
|
Me@19
|
205 if( Q->insertPos - Q->extractPos != 1 &&
|
|
Me@19
|
206 !(Q->extractPos == Q->endOfData && Q->insertPos == Q->startOfData))
|
|
Me@19
|
207 { Q->extractPos++; //move before read
|
|
Me@19
|
208 if( Q->extractPos > Q->endOfData ) Q->extractPos = Q->startOfData;
|
|
Me@19
|
209 out = *(Q->extractPos);
|
|
Me@19
|
210 return out;
|
|
Me@19
|
211 }
|
|
Me@19
|
212 //Q is empty
|
|
Me@19
|
213 tries++;
|
|
Me@19
|
214 if( tries > 10 ) return NULL; //long enough for writer to finish
|
|
Me@19
|
215 }
|
|
Me@19
|
216 }
|
|
Me@19
|
217
|
|
Me@19
|
218
|
|
Me@19
|
219 void writeSRSWQ( void * in, SRSWQueueStruc* Q )
|
|
Me@19
|
220 {
|
|
Me@19
|
221 int tries = 0;
|
|
Me@19
|
222
|
|
Me@19
|
223 while( TRUE )
|
|
Me@19
|
224 {
|
|
Me@19
|
225 if( Q->extractPos - Q->insertPos != 1 &&
|
|
Me@19
|
226 !(Q->insertPos == Q->endOfData && Q->extractPos == Q->startOfData))
|
|
Me@19
|
227 { *(Q->insertPos) = in; //insert before move
|
|
Me@19
|
228 if( Q->insertPos >= Q->endOfData ) Q->insertPos = Q->startOfData;
|
|
Me@19
|
229 else Q->insertPos++;
|
|
Me@19
|
230 return;
|
|
Me@19
|
231 }
|
|
Me@19
|
232 //Q is full
|
|
Me@19
|
233 tries++;
|
|
Me@19
|
234 if( tries > SPINLOCK_TRIES ) pthread_yield();
|
|
Me@19
|
235 }
|
|
Me@19
|
236 }
|
|
Me@19
|
237
|
|
Me@19
|
238
|
|
Me@19
|
239
|
|
Me@19
|
240 //===========================================================================
|
|
Me@19
|
241 //Single reader Multiple writer super fast Q.. no atomic instrs..
|
|
Me@19
|
242
|
|
Me@19
|
243
|
|
Me@19
|
244 /*This is a blocking queue, but it uses no atomic instructions, just does
|
|
Me@19
|
245 * yield() when empty or full
|
|
Me@19
|
246 *
|
|
Me@19
|
247 *It doesn't need any atomic instructions because only a single thread
|
|
Me@19
|
248 * extracts and only a single thread inserts, and it has no locations that
|
|
Me@19
|
249 * are written by both. It writes before moving and moves before reading,
|
|
Me@19
|
250 * and never lets write position and read position be the same, so dis-
|
|
Me@19
|
251 * synchrony can only ever cause an unnecessary call to yield(), never a
|
|
Me@19
|
252 * wrong value (by monotonicity of movement of pointers, plus single writer
|
|
Me@19
|
253 * to pointers, plus sequence of write before change pointer, plus
|
|
Me@19
|
254 * assumptions that if thread A semantically writes X before Y, then thread
|
|
Me@19
|
255 * B will see the writes in that order.)
|
|
Me@19
|
256 *
|
|
Me@19
|
257 *The multi-writer version is implemented as a hierarchy. Each writer has
|
|
Me@19
|
258 * its own single-reader single-writer queue. The reader simply does a
|
|
Me@19
|
259 * round-robin harvesting from them.
|
|
Me@19
|
260 *
|
|
Me@19
|
261 *A writer must first register itself with the queue, and receives an ID back
|
|
Me@19
|
262 * It then uses that ID on each write operation.
|
|
Me@19
|
263 *
|
|
Me@19
|
264 *The implementation is:
|
|
Me@19
|
265 *Physically:
|
|
Me@19
|
266 * -] the SRMWQueueStruc holds an array of SRSWQueueStruc s
|
|
Me@19
|
267 * -] it also has read-pointer to the last queue a write was taken from.
|
|
Me@19
|
268 *
|
|
Me@19
|
269 *Action-Patterns:
|
|
Me@19
|
270 * -] To add a writer
|
|
Me@19
|
271 * --]] writer-thread calls addWriterToQ(), remember the ID it returns
|
|
Me@19
|
272 * --]] internally addWriterToQ does:
|
|
Me@19
|
273 * ---]]] if needs more room, makes a larger writer-array
|
|
Me@19
|
274 * ---]]] copies the old writer-array into the new
|
|
Me@19
|
275 * ---]]] makes a new SRSW queue an puts it into the array
|
|
Me@19
|
276 * ---]]] returns the index to the new SRSW queue as the ID
|
|
Me@19
|
277 * -] To write
|
|
Me@19
|
278 * --]] writer thread calls writeSRMWQ, passing the Q struc and its writer-ID
|
|
Me@19
|
279 * --]] this call may block, via repeated yield() calls
|
|
Me@19
|
280 * --]] internally, writeSRMWQ does:
|
|
Me@19
|
281 * ---]]] uses the writerID as index to get the SRSW queue for that writer
|
|
Me@19
|
282 * ---]]] performs writeQ on that queue (may block via repeated yield calls)
|
|
Me@19
|
283 * -] To Read
|
|
Me@19
|
284 * --]] reader calls readSRMWQ, passing the Q struc
|
|
Me@19
|
285 * --]] this call may block, via repeated yield() calls
|
|
Me@19
|
286 * --]] internally, readSRMWQ does:
|
|
Me@19
|
287 * ---]]] gets saved index of last SRSW queue read from
|
|
Me@19
|
288 * ---]]] increments index and gets indexed queue
|
|
Me@19
|
289 * ---]]] does a non-blocking read of that queue
|
|
Me@19
|
290 * ---]]] if gets something, saves index and returns that value
|
|
Me@19
|
291 * ---]]] if gets null, then goes to next queue
|
|
Me@19
|
292 * ---]]] if got null from all the queues then does yield() then tries again
|
|
Me@19
|
293 *
|
|
Me@19
|
294 *Note: "0" is used as the value null, so SRSW queues must only contain
|
|
Me@19
|
295 * pointers, and cannot use 0 as a valid pointer value.
|
|
Me@19
|
296 *
|
|
Me@19
|
297 */
|
|
Me@19
|
298
|
|
Me@19
|
299 SRMWQueueStruc* makeSRMWQ()
|
|
Me@19
|
300 { SRMWQueueStruc* retQ;
|
|
Me@19
|
301
|
|
seanhalle@27
|
302 retQ = (SRMWQueueStruc *) VMS_WL__malloc( sizeof( SRMWQueueStruc ) );
|
|
Me@19
|
303
|
|
Me@19
|
304 retQ->numInternalQs = 0;
|
|
Me@19
|
305 retQ->internalQsSz = 10;
|
|
seanhalle@27
|
306 retQ->internalQs = VMS_WL__malloc( retQ->internalQsSz * sizeof(SRSWQueueStruc *));
|
|
Me@19
|
307
|
|
Me@19
|
308 retQ->lastQReadFrom = 0;
|
|
Me@19
|
309
|
|
Me@19
|
310 return retQ;
|
|
Me@19
|
311 }
|
|
Me@19
|
312
|
|
Me@19
|
313 /* ---]]] if needs more room, makes a larger writer-array
|
|
Me@19
|
314 * ---]]] copies the old writer-array into the new
|
|
Me@19
|
315 * ---]]] makes a new SRSW queue an puts it into the array
|
|
Me@19
|
316 * ---]]] returns the index to the new SRSW queue as the ID
|
|
Me@19
|
317 *
|
|
Me@19
|
318 *NOTE: assuming all adds are completed before any writes or reads are
|
|
Me@19
|
319 * performed.. otherwise, this needs to be re-done carefully, probably with
|
|
Me@19
|
320 * a lock.
|
|
Me@19
|
321 */
|
|
Me@19
|
322 int addWriterToSRMWQ( SRMWQueueStruc* Q )
|
|
Me@19
|
323 { int oldSz, i;
|
|
Me@19
|
324 SRSWQueueStruc * *oldArray;
|
|
Me@19
|
325
|
|
Me@19
|
326 (Q->numInternalQs)++;
|
|
Me@19
|
327 if( Q->numInternalQs >= Q->internalQsSz )
|
|
Me@19
|
328 { //full, so make bigger
|
|
Me@19
|
329 oldSz = Q->internalQsSz;
|
|
Me@19
|
330 oldArray = Q->internalQs;
|
|
Me@19
|
331 Q->internalQsSz *= 2;
|
|
seanhalle@27
|
332 Q->internalQs = VMS_WL__malloc( Q->internalQsSz * sizeof(SRSWQueueStruc *));
|
|
Me@19
|
333 for( i = 0; i < oldSz; i++ )
|
|
Me@19
|
334 { Q->internalQs[i] = oldArray[i];
|
|
Me@19
|
335 }
|
|
Me@22
|
336 VMS_int__free( oldArray );
|
|
Me@19
|
337 }
|
|
Me@19
|
338 Q->internalQs[ Q->numInternalQs - 1 ] = makeSRSWQ();
|
|
Me@19
|
339 return Q->numInternalQs - 1;
|
|
Me@19
|
340 }
|
|
Me@19
|
341
|
|
Me@19
|
342
|
|
Me@19
|
343 /* ---]]] gets saved index of last SRSW queue read-from
|
|
Me@19
|
344 * ---]]] increments index and gets indexed queue
|
|
Me@19
|
345 * ---]]] does a non-blocking read of that queue
|
|
Me@19
|
346 * ---]]] if gets something, saves index and returns that value
|
|
Me@19
|
347 * ---]]] if gets null, then goes to next queue
|
|
Me@19
|
348 * ---]]] if got null from all the queues then does yield() then tries again
|
|
Me@19
|
349 */
|
|
Me@19
|
350 void* readSRMWQ( SRMWQueueStruc* Q )
|
|
Me@19
|
351 { SRSWQueueStruc *readQ;
|
|
Me@19
|
352 void *readValue = 0;
|
|
Me@19
|
353 int tries = 0;
|
|
Me@19
|
354 int QToReadFrom = 0;
|
|
Me@19
|
355
|
|
Me@19
|
356 QToReadFrom = Q->lastQReadFrom;
|
|
Me@19
|
357
|
|
Me@19
|
358 while( TRUE )
|
|
Me@19
|
359 { QToReadFrom++;
|
|
Me@19
|
360 if( QToReadFrom >= Q->numInternalQs ) QToReadFrom = 0;
|
|
Me@19
|
361 readQ = Q->internalQs[ QToReadFrom ];
|
|
Me@19
|
362 readValue = readSRSWQ_NonBlocking( readQ );
|
|
Me@19
|
363
|
|
Me@19
|
364 if( readValue != 0 ) //got a value, return it
|
|
Me@19
|
365 { Q->lastQReadFrom = QToReadFrom;
|
|
Me@19
|
366 return readValue;
|
|
Me@19
|
367 }
|
|
Me@19
|
368 else //SRSW Q just read is empty
|
|
Me@19
|
369 { //check if all queues have been tried
|
|
Me@19
|
370 if( QToReadFrom == Q->lastQReadFrom ) //all the queues tried & empty
|
|
Me@19
|
371 { tries++; //give a writer a chance to finish before yield
|
|
Me@19
|
372 if( tries > SPINLOCK_TRIES ) pthread_yield();
|
|
Me@19
|
373 }
|
|
Me@19
|
374 }
|
|
Me@19
|
375 }
|
|
Me@19
|
376 }
|
|
Me@19
|
377
|
|
Me@19
|
378
|
|
Me@19
|
379 /*
|
|
Me@19
|
380 * ---]]] uses the writerID as index to get the SRSW queue for that writer
|
|
Me@19
|
381 * ---]]] performs writeQ on that queue (may block via repeated yield calls)
|
|
Me@19
|
382 */
|
|
Me@19
|
383 void writeSRMWQ( void * in, SRMWQueueStruc* Q, int writerID )
|
|
Me@19
|
384 {
|
|
Me@19
|
385 if( in == 0 ) printf( "error, wrote 0 to SRMW Q" );//TODO: throw an error
|
|
Me@19
|
386
|
|
Me@19
|
387 writeSRSWQ( in, Q->internalQs[ writerID ] );
|
|
Me@19
|
388 }
|