| 1 | #ifndef _QUEUE_NON_BLOCKING_
|
|---|
| 2 | #define _QUEUE_NON_BLOCKING_
|
|---|
| 3 |
|
|---|
| 4 | #include <stdbool.h>
|
|---|
| 5 | #include <stdlib.h>
|
|---|
| 6 | #include "queue.h"
|
|---|
| 7 |
|
|---|
| 8 | typedef struct pointer_t pointer_t;
|
|---|
| 9 | //typedef struct queue_t queue_t;
|
|---|
| 10 | typedef struct node_t node_t;
|
|---|
| 11 | typedef struct freeList freeList;
|
|---|
| 12 |
|
|---|
| 13 | struct node_t;
|
|---|
| 14 |
|
|---|
| 15 | struct pointer_t {
|
|---|
| 16 | node_t* ptr;
|
|---|
| 17 | int count;
|
|---|
| 18 | };
|
|---|
| 19 |
|
|---|
| 20 | struct node_t {
|
|---|
| 21 | int value;
|
|---|
| 22 | pointer_t next;
|
|---|
| 23 | };
|
|---|
| 24 |
|
|---|
| 25 | struct queue_t {
|
|---|
| 26 | pointer_t Head;
|
|---|
| 27 | pointer_t Tail;
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | struct freeList{
|
|---|
| 31 | node_t *node;
|
|---|
| 32 | freeList *next;
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | freeList* list; //declare global list
|
|---|
| 36 |
|
|---|
| 37 | void initialize(queue_t *Q) {
|
|---|
| 38 | node_t *node = (node_t*)malloc(sizeof(node_t)); // Allocate a free node
|
|---|
| 39 |
|
|---|
| 40 | list=(freeList*)malloc(sizeof(freeList));
|
|---|
| 41 | list->node = NULL; //initialize list
|
|---|
| 42 | list->next = NULL;
|
|---|
| 43 | node->next.ptr = NULL; // Make it the only node in the linked list
|
|---|
| 44 | node->next.count = 0; // Initialize counter
|
|---|
| 45 | Q->Head.ptr = Q->Tail.ptr = node; // Both Head and Tail point to it
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | void setFree(node_t* freeNode){ //put the node to a special-use free list and not the partner to malloc
|
|---|
| 49 | $atomic{
|
|---|
| 50 | freeList *temp = (freeList*)malloc(sizeof(freeList));
|
|---|
| 51 |
|
|---|
| 52 | temp->node = freeNode;
|
|---|
| 53 | temp->next = list->next;
|
|---|
| 54 | list->next = temp;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void deallocate(freeList *list){ // partner to malloc
|
|---|
| 59 | freeList *q;
|
|---|
| 60 |
|
|---|
| 61 | while(list != NULL){
|
|---|
| 62 | q = list->next;
|
|---|
| 63 | free(list->node);
|
|---|
| 64 | free(list);
|
|---|
| 65 | list = q;
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | _Bool equal(pointer_t p1, pointer_t p2){ //define equal() method to compare two pointers
|
|---|
| 70 | return (p1.ptr == p2.ptr) && (p1.count == p2.count);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | _Bool CAS(pointer_t *dest, pointer_t oldval, pointer_t newval){ //define CAS() method
|
|---|
| 74 | $atomic {
|
|---|
| 75 | if (equal(*dest, oldval)) {
|
|---|
| 76 | *dest = newval;
|
|---|
| 77 | return true;
|
|---|
| 78 | }
|
|---|
| 79 | return false;
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | void enqueue(queue_t *Q, int value) {
|
|---|
| 84 | pointer_t tail, next;
|
|---|
| 85 | node_t *node = (node_t*)malloc(sizeof(node_t)); // Allocate a new node from the free list
|
|---|
| 86 |
|
|---|
| 87 | node->value = value; // Copy enqueued value into node
|
|---|
| 88 | node->next.ptr = NULL; // Set next pointer of node to NULL
|
|---|
| 89 |
|
|---|
| 90 | while (true){ // Keep trying until Enqueue is done
|
|---|
| 91 | tail = Q->Tail; // Read Tail.ptr and Tail.count together
|
|---|
| 92 | next = tail.ptr->next; // Read next ptr and count fields together
|
|---|
| 93 | if (equal(tail, Q->Tail)) // Are tail and next consistent?
|
|---|
| 94 | // Was Tail pointing to the last node?
|
|---|
| 95 | if (next.ptr == NULL){
|
|---|
| 96 | // Try to link node at the end of the linked list
|
|---|
| 97 | if (CAS(&tail.ptr->next, next, (pointer_t){ node, next.count + 1 }))
|
|---|
| 98 | break; // **Enqueue is done. Exit loop
|
|---|
| 99 | }
|
|---|
| 100 | else{ // Tail was not pointing to the last node
|
|---|
| 101 | // Try to swing Tail to the next node
|
|---|
| 102 | CAS(&Q->Tail, tail, (pointer_t){ next.ptr, tail.count + 1 });
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | // Enqueue is done. Try to swing Tail to the inserted node
|
|---|
| 106 | CAS(&Q->Tail, tail, (pointer_t){ node, tail.count + 1 });
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | _Bool dequeue(queue_t *Q, int *pvalue) { //boolean type
|
|---|
| 110 | pointer_t head, tail, next;
|
|---|
| 111 |
|
|---|
| 112 | while (true){ // Keep trying until Dequeue is done
|
|---|
| 113 | head = Q->Head; // Read Head
|
|---|
| 114 | tail = Q->Tail; // Read Tail
|
|---|
| 115 | next = head.ptr->next; // Read Head.ptr->next
|
|---|
| 116 | if (equal(head, Q->Head)) // Are head, tail, and next consistent?
|
|---|
| 117 | if (head.ptr == tail.ptr){ // Is queue empty or Tail falling behind?
|
|---|
| 118 | if (next.ptr == NULL) // Is queue empty?
|
|---|
| 119 | return false; // Queue is empty, couldn't dequeue
|
|---|
| 120 | // Tail is falling behind. Try to advance it
|
|---|
| 121 | CAS(&Q->Tail, tail, (pointer_t){ next.ptr, tail.count + 1 });
|
|---|
| 122 | }
|
|---|
| 123 | else{
|
|---|
| 124 | // Read value before CAS
|
|---|
| 125 | // Otherwise, another dequeue might free the next node
|
|---|
| 126 | *pvalue = next.ptr->value;
|
|---|
| 127 | if (CAS(&Q->Head, head, (pointer_t){ next.ptr, head.count + 1 }))
|
|---|
| 128 | break;// **Dequeue is done. Exit loop
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | setFree(head.ptr); // It is safe now to "free" the old node
|
|---|
| 132 | return true; // Queue was not empty, dequeue succeeded
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | void freequeue(queue_t q){
|
|---|
| 136 | deallocate(list);
|
|---|
| 137 | free(q.Head.ptr);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | #endif
|
|---|