annotate BlockingQueue.c @ 23:c3049c8bd0fe

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