annotate BlockingQueue.c @ 48:1ea30ca7093c

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