| 1 | /* Non-Blocking Concurrent Queue Algorithm from Michael and Scott
|
|---|
| 2 | * https://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html.
|
|---|
| 3 | * Originally from "Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms", PODC96.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | #include <civlc.cvh>
|
|---|
| 7 | #include <stdio.h>
|
|---|
| 8 | #include <stdbool.h>
|
|---|
| 9 | #include <stdlib.h>
|
|---|
| 10 | #include <assert.h>
|
|---|
| 11 |
|
|---|
| 12 | typedef struct pointer_t pointer_t;
|
|---|
| 13 | typedef struct queue_t queue_t;
|
|---|
| 14 | typedef struct node_t node_t;
|
|---|
| 15 | typedef struct freeList freeList;
|
|---|
| 16 |
|
|---|
| 17 | struct node_t;
|
|---|
| 18 |
|
|---|
| 19 | struct pointer_t {
|
|---|
| 20 | node_t* ptr;
|
|---|
| 21 | int count;
|
|---|
| 22 | //int flag == 1; //set a flag for free status ("1" means not free, "0" means free)
|
|---|
| 23 | };
|
|---|
| 24 |
|
|---|
| 25 | struct node_t {
|
|---|
| 26 | int value;
|
|---|
| 27 | pointer_t next;
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | struct queue_t {
|
|---|
| 31 | pointer_t Head;
|
|---|
| 32 | pointer_t Tail;
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | struct freeList{
|
|---|
| 36 | pointer_t *node;
|
|---|
| 37 | freeList *next;
|
|---|
| 38 | };
|
|---|
| 39 |
|
|---|
| 40 | freeList list;
|
|---|
| 41 |
|
|---|
| 42 | void initialize(queue_t *Q) {
|
|---|
| 43 | node_t *node = (node_t*)malloc(sizeof(node_t)); // Allocate a free node
|
|---|
| 44 |
|
|---|
| 45 | list.node = NULL;
|
|---|
| 46 | list.next = NULL;
|
|---|
| 47 | node->next.ptr = NULL; // Make it the only node in the linked list
|
|---|
| 48 | node->next.count = 0; // Initialize counter
|
|---|
| 49 | Q->Head.ptr = Q->Tail.ptr = node; // Both Head and Tail point to it
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | void setFree(pointer_t freeNode){ //put the node to a special-use free list and not the partner to malloc
|
|---|
| 53 |
|
|---|
| 54 | //freeNode.flag = 0;
|
|---|
| 55 | freeList *temp = (freeList*)malloc(sizeof(freeList));
|
|---|
| 56 | temp->node = &freeNode;
|
|---|
| 57 | temp->next = list.next;
|
|---|
| 58 | list.next = temp;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | void deallocate(freeList *list){ // partner to malloc
|
|---|
| 62 | freeList *q;
|
|---|
| 63 |
|
|---|
| 64 | while(list!=NULL){
|
|---|
| 65 | q = list->next;
|
|---|
| 66 | free(list->node->ptr);
|
|---|
| 67 | free(list);
|
|---|
| 68 | list = q;
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | _Bool equal(pointer_t p1, pointer_t p2){ //define equal() method to compare two pointers
|
|---|
| 73 | return (p1.ptr == p2.ptr) && (p1.count == p2.count);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | _Bool CAS(pointer_t *dest, pointer_t oldval, pointer_t newval) //define CAS() method
|
|---|
| 77 | {
|
|---|
| 78 | $atomic {
|
|---|
| 79 | if (equal(*dest, oldval)) {
|
|---|
| 80 | *dest = newval;
|
|---|
| 81 | return true;
|
|---|
| 82 | }
|
|---|
| 83 | return false;
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void enqueue(queue_t *Q, int value) {
|
|---|
| 88 | pointer_t tail, next;
|
|---|
| 89 | node_t *node = (node_t*)malloc(sizeof(node_t)); // Allocate a new node from the free list
|
|---|
| 90 |
|
|---|
| 91 | node->value = value; // Copy enqueued value into node
|
|---|
| 92 | node->next.ptr = NULL; // Set next pointer of node to NULL
|
|---|
| 93 |
|
|---|
| 94 | while (true){ // Keep trying until Enqueue is done
|
|---|
| 95 | tail = Q->Tail; // Read Tail.ptr and Tail.count together
|
|---|
| 96 | next = tail.ptr->next; // Read next ptr and count fields together
|
|---|
| 97 | if (equal(tail, Q->Tail)) // Are tail and next consistent?
|
|---|
| 98 | // Was Tail pointing to the last node?
|
|---|
| 99 | if (next.ptr == NULL){
|
|---|
| 100 | // Try to link node at the end of the linked list
|
|---|
| 101 | if (CAS(&tail.ptr->next, next, (pointer_t){ node, next.count + 1 }))
|
|---|
| 102 | break; // **Enqueue is done. Exit loop
|
|---|
| 103 | }
|
|---|
| 104 | else{ // Tail was not pointing to the last node
|
|---|
| 105 | // Try to swing Tail to the next node
|
|---|
| 106 | CAS(&Q->Tail, tail, (pointer_t){ next.ptr, tail.count + 1 });
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | // Enqueue is done. Try to swing Tail to the inserted node
|
|---|
| 110 | CAS(&Q->Tail, tail, (pointer_t){ node, tail.count + 1 });
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | _Bool dequeue(queue_t *Q, int *pvalue) { //boolean type
|
|---|
| 114 | pointer_t head, tail, next;
|
|---|
| 115 |
|
|---|
| 116 | while (true){ // Keep trying until Dequeue is done
|
|---|
| 117 | head = Q->Head; // Read Head
|
|---|
| 118 | tail = Q->Tail; // Read Tail
|
|---|
| 119 | next = head.ptr->next; // Read Head.ptr->next
|
|---|
| 120 | if (equal(head, Q->Head)) // Are head, tail, and next consistent?
|
|---|
| 121 | if (head.ptr == tail.ptr){ // Is queue empty or Tail falling behind?
|
|---|
| 122 | if (next.ptr == NULL) // Is queue empty?
|
|---|
| 123 | return false; // Queue is empty, couldn't dequeue
|
|---|
| 124 | // Tail is falling behind. Try to advance it
|
|---|
| 125 | CAS(&Q->Tail, tail, (pointer_t){ next.ptr, tail.count + 1 });
|
|---|
| 126 | }
|
|---|
| 127 | else{
|
|---|
| 128 | // Read value before CAS
|
|---|
| 129 | // Otherwise, another dequeue might free the next node
|
|---|
| 130 | *pvalue = next.ptr->value;
|
|---|
| 131 | if (CAS(&Q->Head, head, (pointer_t){ next.ptr, head.count + 1 }))
|
|---|
| 132 | break;// **Dequeue is done. Exit loop
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | setFree(head); // It is safe now to free the old node
|
|---|
| 136 | return true; // Queue was not empty, dequeue succeeded
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | void test1() { // Test enqueue & dequeue
|
|---|
| 140 | int d;
|
|---|
| 141 | queue_t sq;
|
|---|
| 142 |
|
|---|
| 143 | initialize(&sq);
|
|---|
| 144 | for (int i = 0; i < 10; i++) {
|
|---|
| 145 | enqueue(&sq, i);
|
|---|
| 146 | }
|
|---|
| 147 | for (int i = 0; i < 10; i++) {
|
|---|
| 148 | _Bool result = dequeue(&sq, &d);
|
|---|
| 149 |
|
|---|
| 150 | assert(result);
|
|---|
| 151 | assert(d == i);
|
|---|
| 152 | }
|
|---|
| 153 | free(sq.Head.ptr);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | void test2() { //Test the concurrency
|
|---|
| 157 | queue_t sq;
|
|---|
| 158 | int array[3];
|
|---|
| 159 | void thread(int val) {enqueue(&sq, val);dequeue(&sq, &array[val-1]);}
|
|---|
| 160 |
|
|---|
| 161 | initialize(&sq);
|
|---|
| 162 |
|
|---|
| 163 | $parfor(int i: 1 .. 3)
|
|---|
| 164 | thread(i);
|
|---|
| 165 |
|
|---|
| 166 | for(int j=0; j<3; j++)
|
|---|
| 167 | printf("array[%d] = %d\t",j, array[j]);
|
|---|
| 168 | printf("\n");
|
|---|
| 169 |
|
|---|
| 170 | assert((array[0]==1&&array[1]==2&&array[2]==3)||(array[0]==1&&array[1]==3&&array[2]==2)||
|
|---|
| 171 | (array[0]==2&&array[1]==3&&array[2]==1)||(array[0]==2&&array[1]==1&&array[2]==3)||
|
|---|
| 172 | (array[0]==3&&array[1]==1&&array[2]==2)||(array[0]==3&&array[1]==2&&array[2]==1));
|
|---|
| 173 |
|
|---|
| 174 | deallocate(&list);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | void test3() { //Test the concurrency
|
|---|
| 178 | queue_t sq;
|
|---|
| 179 | int array[3];
|
|---|
| 180 | void threadEn(int val) {enqueue(&sq, val);}
|
|---|
| 181 | void threadDe(int val) {dequeue(&sq, &array[val-1]);}
|
|---|
| 182 | int x, y, z;
|
|---|
| 183 |
|
|---|
| 184 | initialize(&sq);
|
|---|
| 185 |
|
|---|
| 186 | $parfor(int i: 1 .. 3)
|
|---|
| 187 | threadEn(i);
|
|---|
| 188 |
|
|---|
| 189 | $parfor(int i: 1 .. 3)
|
|---|
| 190 | threadDe(i);
|
|---|
| 191 |
|
|---|
| 192 | for(int j=0; j<3; j++)
|
|---|
| 193 | printf("array[%d] = %d\t",j, array[j]);
|
|---|
| 194 | printf("\n");
|
|---|
| 195 |
|
|---|
| 196 | free(sq.Head.ptr);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | #define N 2
|
|---|
| 200 | #define T 2
|
|---|
| 201 | void test4() { //Test 3
|
|---|
| 202 | int RESULT[T][N];
|
|---|
| 203 | int A[N];
|
|---|
| 204 | queue_t sq;
|
|---|
| 205 | printf("This is test3!\n");
|
|---|
| 206 |
|
|---|
| 207 | void thread(int tid){
|
|---|
| 208 | for(int i=0; i<N; i++)
|
|---|
| 209 | enqueue(&sq, A[i]+tid*N);
|
|---|
| 210 | for(int i=0; i<N; i++){
|
|---|
| 211 | dequeue(&sq, &(RESULT[tid][i]));
|
|---|
| 212 | printf("%d\n",RESULT[tid][i]);
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | for(int i=0; i<N; i++)
|
|---|
| 217 | A[i] = i+1;
|
|---|
| 218 | for(int i=0; i<T; i++)
|
|---|
| 219 | for(int j=0; j<N; j++)
|
|---|
| 220 | RESULT[i][j] = 0;
|
|---|
| 221 | initialize(&sq);
|
|---|
| 222 | $parfor(int i: 0 .. T-1)
|
|---|
| 223 | thread(i);
|
|---|
| 224 |
|
|---|
| 225 | //assert(!(RESULT[][]));
|
|---|
| 226 | free(sq.Head.ptr);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | void test5(){
|
|---|
| 230 | queue_t sq;
|
|---|
| 231 | void thread(int val) {enqueue(&sq, val);}
|
|---|
| 232 | int x, y, z;
|
|---|
| 233 |
|
|---|
| 234 | initialize(&sq);
|
|---|
| 235 |
|
|---|
| 236 | $parfor(int i:1 .. 3)
|
|---|
| 237 | thread(i);
|
|---|
| 238 |
|
|---|
| 239 | dequeue(&sq, &x);
|
|---|
| 240 | dequeue(&sq, &y);
|
|---|
| 241 | dequeue(&sq, &z);
|
|---|
| 242 |
|
|---|
| 243 | assert((x==1&&y==2&&z==3)||(x==1&&y==3&&z==2)||
|
|---|
| 244 | (x==2&&y==3&&z==1)||(x==2&&y==1&&z==3)||
|
|---|
| 245 | (x==3&&y==1&&z==2)||(x==3&&y==2&&z==1));
|
|---|
| 246 |
|
|---|
| 247 | free(sq.Head.ptr);
|
|---|
| 248 |
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | void main() {
|
|---|
| 252 | //test1();
|
|---|
| 253 | test2();
|
|---|
| 254 | //test3()
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|