comparison BlockingQueue.c @ 32:a74011c0b78b

fixed expansion of queue, and added numInPrivQ and freePrivQ
author Some Random Person <seanhalle@yahoo.com>
date Wed, 14 Mar 2012 22:48:50 -0700
parents bd38feb38c80
children b66352de717e d01d48b023ca
comparison
equal deleted inserted replaced
14:2a0e716cb48e 15:69a5b2c0de8f
9 #include <stdio.h> 9 #include <stdio.h>
10 #include <errno.h> 10 #include <errno.h>
11 #include <pthread.h> 11 #include <pthread.h>
12 #include <stdlib.h> 12 #include <stdlib.h>
13 #include <sched.h> 13 #include <sched.h>
14 #include <string.h>
14 15
15 #include "BlockingQueue.h" 16 #include "BlockingQueue.h"
16 17
17 #define INC(x) (++x == 1024) ? (x) = 0 : (x) 18 #define INC(x) (++x == 1024) ? (x) = 0 : (x)
18 19
36 CASQueueStruc* retQ; 37 CASQueueStruc* retQ;
37 retQ = (CASQueueStruc *) VMS_WL__malloc( sizeof( CASQueueStruc ) ); 38 retQ = (CASQueueStruc *) VMS_WL__malloc( sizeof( CASQueueStruc ) );
38 39
39 retQ->insertLock = UNLOCKED; 40 retQ->insertLock = UNLOCKED;
40 retQ->extractLock= UNLOCKED; 41 retQ->extractLock= UNLOCKED;
41 //TODO: check got pointer syntax right 42
42 retQ->extractPos = &(retQ->startOfData[0]); //side by side == empty 43 retQ->extractPos = (volatile void**)&(retQ->startOfData[0]); //side by side == empty
43 retQ->insertPos = &(retQ->startOfData[1]); // so start pos's have to be 44 retQ->insertPos = (volatile void**)&(retQ->startOfData[1]); // so start pos's have to be
44 retQ->endOfData = &(retQ->startOfData[1023]); 45 retQ->endOfData = &(retQ->startOfData[1023]);
45 46
46 return retQ; 47 return retQ;
47 } 48 }
48 49
62 gotLock = 63 gotLock =
63 __sync_bool_compare_and_swap( &(Q->extractLock), UNLOCKED, LOCKED ); 64 __sync_bool_compare_and_swap( &(Q->extractLock), UNLOCKED, LOCKED );
64 //NOTE: checked assy, and it does lock correctly.. 65 //NOTE: checked assy, and it does lock correctly..
65 if( gotLock ) 66 if( gotLock )
66 { 67 {
67 void **insertPos = Q->insertPos; 68 void **insertPos = (void **)Q->insertPos;
68 void **extractPos = Q->extractPos; 69 void **extractPos = (void **)Q->extractPos;
69 70
70 //if not empty -- extract just below insert when empty 71 //if not empty -- extract just below insert when empty
71 if( insertPos - extractPos != 1 && 72 if( insertPos - extractPos != 1 &&
72 !(extractPos == endOfData && insertPos == startOfData)) 73 !(extractPos == endOfData && insertPos == startOfData))
73 { //move before read 74 { //move before read
75 { Q->extractPos = startOfData; //can't overrun then fix it 'cause 76 { Q->extractPos = startOfData; //can't overrun then fix it 'cause
76 } // other thread might read bad pos 77 } // other thread might read bad pos
77 else 78 else
78 { Q->extractPos++; 79 { Q->extractPos++;
79 } 80 }
80 out = *(Q->extractPos); 81 out = (void *) *(Q->extractPos);
81 Q->extractLock = UNLOCKED; 82 Q->extractLock = UNLOCKED;
82 return out; 83 return out;
83 } 84 }
84 else //Q is empty 85 else //Q is empty
85 { Q->extractLock = UNLOCKED;//empty, so release lock for others 86 { Q->extractLock = UNLOCKED;//empty, so release lock for others
107 // gotLock is FALSE 108 // gotLock is FALSE
108 gotLock = 109 gotLock =
109 __sync_bool_compare_and_swap( &(Q->insertLock), UNLOCKED, LOCKED ); 110 __sync_bool_compare_and_swap( &(Q->insertLock), UNLOCKED, LOCKED );
110 if( gotLock ) 111 if( gotLock )
111 { 112 {
112 void **insertPos = Q->insertPos; 113 void **insertPos = (void **)Q->insertPos;
113 void **extractPos = Q->extractPos; 114 void **extractPos = (void **)Q->extractPos;
114 115
115 //check if room to insert.. can't use a count variable 116 //check if room to insert.. can't use a count variable
116 // 'cause both insertor Thd and extractor Thd would write it 117 // 'cause both insertor Thd and extractor Thd would write it
117 if( extractPos - insertPos != 1 && 118 if( extractPos - insertPos != 1 &&
118 !(insertPos == endOfData && extractPos == startOfData)) 119 !(insertPos == endOfData && extractPos == startOfData))