4 #include "model-assert.h"
8 #define relaxed memory_order_relaxed
9 #define release memory_order_release
10 #define acquire memory_order_acquire
12 #define MAX_FREELIST 4 /* Each thread can own up to MAX_FREELIST free nodes */
13 #define INITIAL_FREE 2 /* Each thread starts with INITIAL_FREE free nodes */
15 static unsigned int (*free_lists)[MAX_FREELIST];
17 /* Search this thread's free list for a "new" node */
18 static unsigned int new_node()
21 int t = get_thread_num();
22 for (i = 0; i < MAX_FREELIST; i++) {
23 unsigned int node = load_32(&free_lists[t][i]);
25 store_32(&free_lists[t][i], 0);
29 /* free_list is empty? */
34 /* Place this node index back on this thread's free list */
35 static void reclaim(unsigned int node)
38 int t = get_thread_num();
40 /* Don't reclaim NULL node */
43 for (i = 0; i < MAX_FREELIST; i++) {
44 /* Should never race with our own thread here */
45 unsigned int idx = load_32(&free_lists[t][i]);
47 /* Found empty spot in free list */
49 store_32(&free_lists[t][i], node);
53 /* free list is full? */
57 void init_queue(queue_t *q, int num_threads)
61 /* Initialize each thread's free list with INITIAL_FREE NULL "pointers" */
62 free_lists = malloc(num_threads * sizeof(*free_lists));
63 for (i = 0; i < num_threads; i++) {
64 for (j = 0; j < INITIAL_FREE; j++) {
65 free_lists[i][j] = 2 + i * MAX_FREELIST + j;
66 atomic_init(&q->nodes[free_lists[i][j]].next, MAKE_POINTER(0, 0));
70 /* initialize queue */
71 atomic_init(&q->head, MAKE_POINTER(1, 0));
72 atomic_init(&q->tail, MAKE_POINTER(1, 0));
73 atomic_init(&q->nodes[1].next, MAKE_POINTER(0, 0));
76 void enqueue(queue_t *q, unsigned int val)
85 store_32(&q->nodes[node].value, val);
86 tmp = atomic_load_explicit(&q->nodes[node].next, relaxed);
87 set_ptr(&tmp, 0); // NULL
88 atomic_store_explicit(&q->nodes[node].next, tmp, relaxed);
91 tail = atomic_load_explicit(&q->tail, acquire);
92 next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire);
93 if (tail == atomic_load_explicit(&q->tail, relaxed)) {
94 if (get_ptr(next) == 0) { // == NULL
95 pointer value = MAKE_POINTER(node, get_count(next) + 1);
96 success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next,
97 &next, value, release, release);
100 unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire));
101 pointer value = MAKE_POINTER(ptr,
102 get_count(tail) + 1);
103 atomic_compare_exchange_strong_explicit(&q->tail,
110 atomic_compare_exchange_strong_explicit(&q->tail,
112 MAKE_POINTER(node, get_count(tail) + 1),
116 unsigned int dequeue(queue_t *q)
125 head = atomic_load_explicit(&q->head, acquire);
126 tail = atomic_load_explicit(&q->tail, relaxed);
127 next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, acquire);
128 if (atomic_load_explicit(&q->head, relaxed) == head) {
129 if (get_ptr(head) == get_ptr(tail)) {
130 if (get_ptr(next) == 0) { // NULL
133 atomic_compare_exchange_strong_explicit(&q->tail,
135 MAKE_POINTER(get_ptr(next), get_count(tail) + 1),
139 value = load_32(&q->nodes[get_ptr(next)].value);
140 success = atomic_compare_exchange_strong_explicit(&q->head,
142 MAKE_POINTER(get_ptr(next), get_count(head) + 1),
149 reclaim(get_ptr(head));