6 static unsigned int *node_nums;
8 static unsigned int new_node()
10 return node_nums[get_thread_num()];
13 static void reclaim(unsigned int node)
15 node_nums[get_thread_num()] = node;
18 void init_queue(queue_t *q, int num_threads)
25 node_nums = malloc(num_threads * sizeof(*node_nums));
26 for (i = 0; i < num_threads; i++)
29 /* initialize queue */
30 head = MAKE_POINTER(1, 0);
31 tail = MAKE_POINTER(1, 0);
32 next = MAKE_POINTER(0, 0); // (NULL, 0)
34 atomic_init(&q->nodes[0].next, 0); // assumed inititalized in original example
36 atomic_store(&q->head, head);
37 atomic_store(&q->tail, tail);
38 atomic_store(&q->nodes[1].next, next);
40 /* initialize avail list */
41 for (i = 2; i < MAX_NODES; i++) {
42 next = MAKE_POINTER(i + 1, 0);
43 atomic_store(&q->nodes[i].next, next);
46 next = MAKE_POINTER(0, 0); // (NULL, 0)
47 atomic_store(&q->nodes[MAX_NODES].next, next);
50 void enqueue(queue_t *q, unsigned int val)
59 q->nodes[node].value = val;
60 tmp = atomic_load(&q->nodes[node].next);
61 set_ptr(&tmp, 0); // NULL
62 atomic_store(&q->nodes[node].next, tmp);
65 tail = atomic_load(&q->tail);
66 next = atomic_load(&q->nodes[get_ptr(tail)].next);
67 if (tail == atomic_load(&q->tail)) {
68 if (get_ptr(next) == 0) { // == NULL
69 pointer value = MAKE_POINTER(node, get_count(next) + 1);
70 success = atomic_compare_exchange_weak(&q->nodes[get_ptr(tail)].next,
74 unsigned int ptr = get_ptr(atomic_load(&q->nodes[get_ptr(tail)].next));
75 pointer value = MAKE_POINTER(ptr,
77 atomic_compare_exchange_strong(&q->tail,
83 atomic_compare_exchange_strong(&q->tail,
85 MAKE_POINTER(node, get_count(tail) + 1));
88 unsigned int dequeue(queue_t *q)
97 head = atomic_load(&q->head);
98 tail = atomic_load(&q->tail);
99 next = atomic_load(&q->nodes[get_ptr(head)].next);
100 if (atomic_load(&q->head) == head) {
101 if (get_ptr(head) == get_ptr(tail)) {
102 if (get_ptr(next) == 0) { // NULL
105 atomic_compare_exchange_strong(&q->tail,
107 MAKE_POINTER(get_ptr(next), get_count(tail) + 1));
110 value = q->nodes[get_ptr(next)].value;
111 success = atomic_compare_exchange_weak(&q->head,
113 MAKE_POINTER(get_ptr(next), get_count(head) + 1));
119 reclaim(get_ptr(head));