| [ddafa46] | 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 | * The free in the algorithm (setFree method in Dequeue in this code) is meant to represent a function putting the
|
|---|
| 6 | * node back on to a locally-maintained special-use free list and not the partner to malloc.
|
|---|
| 7 | * http://blog.shealevy.com/2015/04/23/use-after-free-bug-in-maged-m-michael-and-michael-l-scotts-non-blocking-concurrent-queue-algorithm/#up1
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #include <civlc.cvh>
|
|---|
| 11 | #include <stdio.h>
|
|---|
| 12 | #include <stdbool.h>
|
|---|
| 13 | #include <stdlib.h>
|
|---|
| 14 | #include <assert.h>
|
|---|
| 15 |
|
|---|
| 16 | typedef struct pointer_t pointer_t;
|
|---|
| 17 | typedef struct queue_t queue_t;
|
|---|
| 18 | typedef struct node_t node_t;
|
|---|
| 19 | typedef struct freeList freeList;
|
|---|
| 20 |
|
|---|
| 21 | struct node_t;
|
|---|
| 22 |
|
|---|
| 23 | struct pointer_t {
|
|---|
| 24 | node_t* ptr;
|
|---|
| 25 | int count;
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | struct node_t {
|
|---|
| 29 | int value;
|
|---|
| 30 | pointer_t next;
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | struct queue_t {
|
|---|
| 34 | pointer_t Head;
|
|---|
| 35 | pointer_t Tail;
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | struct freeList{
|
|---|
| 39 | node_t *node;
|
|---|
| 40 | freeList *next;
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | freeList* list; //declare global list
|
|---|
| 44 |
|
|---|
| 45 | void initialize(queue_t *Q) {
|
|---|
| 46 | node_t *node = (node_t*)malloc(sizeof(node_t)); // Allocate a free node
|
|---|
| 47 |
|
|---|
| 48 | list=(freeList*)malloc(sizeof(freeList));
|
|---|
| 49 | list->node = NULL; //initialize list
|
|---|
| 50 | list->next = NULL;
|
|---|
| 51 | node->next.ptr = NULL; // Make it the only node in the linked list
|
|---|
| 52 | node->next.count = 0; // Initialize counter
|
|---|
| 53 | Q->Head.ptr = Q->Tail.ptr = node; // Both Head and Tail point to it
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | void setFree(node_t* freeNode){ //put the node to a special-use free list and not the partner to malloc
|
|---|
| 57 | $atomic{
|
|---|
| 58 | freeList *temp = (freeList*)malloc(sizeof(freeList));
|
|---|
| 59 |
|
|---|
| 60 | temp->node = freeNode;
|
|---|
| 61 | temp->next = list->next;
|
|---|
| 62 | list->next = temp;
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | void deallocate(freeList *list){ // partner to malloc
|
|---|
| 67 | freeList *q;
|
|---|
| 68 |
|
|---|
| 69 | while(list != NULL){
|
|---|
| 70 | q = list->next;
|
|---|
| 71 | free(list->node);
|
|---|
| 72 | free(list);
|
|---|
| 73 | list = q;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | _Bool equal(pointer_t p1, pointer_t p2){ //define equal() method to compare two pointers
|
|---|
| 78 | return (p1.ptr == p2.ptr) && (p1.count == p2.count);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | _Bool CAS(pointer_t *dest, pointer_t oldval, pointer_t newval){ //define CAS() method
|
|---|
| 82 | $atomic {
|
|---|
| 83 | if (equal(*dest, oldval)) {
|
|---|
| 84 | *dest = newval;
|
|---|
| 85 | return true;
|
|---|
| 86 | }
|
|---|
| 87 | return false;
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void enqueue(queue_t *Q, int value) {
|
|---|
| 92 | pointer_t tail, next;
|
|---|
| 93 | node_t *node = (node_t*)malloc(sizeof(node_t)); // Allocate a new node from the free list
|
|---|
| 94 |
|
|---|
| 95 | node->value = value; // Copy enqueued value into node
|
|---|
| 96 | node->next.ptr = NULL; // Set next pointer of node to NULL
|
|---|
| 97 |
|
|---|
| 98 | while (true){ // Keep trying until Enqueue is done
|
|---|
| 99 | tail = Q->Tail; // Read Tail.ptr and Tail.count together
|
|---|
| 100 | next = tail.ptr->next; // Read next ptr and count fields together
|
|---|
| 101 | if (equal(tail, Q->Tail)) // Are tail and next consistent?
|
|---|
| 102 | // Was Tail pointing to the last node?
|
|---|
| 103 | if (next.ptr == NULL){
|
|---|
| 104 | // Try to link node at the end of the linked list
|
|---|
| 105 | if (CAS(&tail.ptr->next, next, (pointer_t){ node, next.count + 1 }))
|
|---|
| 106 | break; // **Enqueue is done. Exit loop
|
|---|
| 107 | }
|
|---|
| 108 | else{ // Tail was not pointing to the last node
|
|---|
| 109 | // Try to swing Tail to the next node
|
|---|
| 110 | CAS(&Q->Tail, tail, (pointer_t){ next.ptr, tail.count + 1 });
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | // Enqueue is done. Try to swing Tail to the inserted node
|
|---|
| 114 | CAS(&Q->Tail, tail, (pointer_t){ node, tail.count + 1 });
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | _Bool dequeue(queue_t *Q, int *pvalue) { //boolean type
|
|---|
| 118 | pointer_t head, tail, next;
|
|---|
| 119 |
|
|---|
| 120 | while (true){ // Keep trying until Dequeue is done
|
|---|
| 121 | head = Q->Head; // Read Head
|
|---|
| 122 | tail = Q->Tail; // Read Tail
|
|---|
| 123 | next = head.ptr->next; // Read Head.ptr->next
|
|---|
| 124 | if (equal(head, Q->Head)) // Are head, tail, and next consistent?
|
|---|
| 125 | if (head.ptr == tail.ptr){ // Is queue empty or Tail falling behind?
|
|---|
| 126 | if (next.ptr == NULL) // Is queue empty?
|
|---|
| 127 | return false; // Queue is empty, couldn't dequeue
|
|---|
| 128 | // Tail is falling behind. Try to advance it
|
|---|
| 129 | CAS(&Q->Tail, tail, (pointer_t){ next.ptr, tail.count + 1 });
|
|---|
| 130 | }
|
|---|
| 131 | else{
|
|---|
| 132 | // Read value before CAS
|
|---|
| 133 | // Otherwise, another dequeue might free the next node
|
|---|
| 134 | *pvalue = next.ptr->value;
|
|---|
| 135 | if (CAS(&Q->Head, head, (pointer_t){ next.ptr, head.count + 1 }))
|
|---|
| 136 | break;// **Dequeue is done. Exit loop
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | setFree(head.ptr); // It is safe now to "free" the old node
|
|---|
| 140 | return true; // Queue was not empty, dequeue succeeded
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | void test1() { // Test enqueue & dequeue
|
|---|
| 144 | int d;
|
|---|
| 145 | queue_t sq;
|
|---|
| 146 |
|
|---|
| 147 | initialize(&sq);
|
|---|
| 148 | for (int i = 0; i < 10; i++) {
|
|---|
| 149 | enqueue(&sq, i);
|
|---|
| 150 | }
|
|---|
| 151 | for (int i = 0; i < 10; i++) {
|
|---|
| 152 | _Bool result = dequeue(&sq, &d);
|
|---|
| 153 |
|
|---|
| 154 | assert(result);
|
|---|
| 155 | assert(d == i);
|
|---|
| 156 | }
|
|---|
| [fe98629] | 157 |
|
|---|
| 158 | deallocate(list);
|
|---|
| [ddafa46] | 159 | free(sq.Head.ptr);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | #define N 2
|
|---|
| 163 |
|
|---|
| 164 | void test2() { //Test the concurrency
|
|---|
| 165 | queue_t sq;
|
|---|
| 166 | int array[N];
|
|---|
| 167 | void thread(int val) {enqueue(&sq, val);dequeue(&sq, &array[val-1]);}
|
|---|
| 168 |
|
|---|
| 169 | initialize(&sq);
|
|---|
| 170 |
|
|---|
| 171 | $parfor(int i: 1 .. N)
|
|---|
| 172 | thread(i);
|
|---|
| 173 |
|
|---|
| 174 | for(int j=0; j<N; j++)
|
|---|
| 175 | printf("array[%d] = %d\t",j, array[j]);
|
|---|
| 176 | printf("\n");
|
|---|
| 177 |
|
|---|
| 178 | deallocate(list);
|
|---|
| 179 | free(sq.Head.ptr);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | void main() {
|
|---|
| 183 | //test1();
|
|---|
| 184 | test2();
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|