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 #define POISON_IDX 0x666
17 static unsigned int (*free_lists)[MAX_FREELIST];
19 /* Search this thread's free list for a "new" node */
20 static unsigned int new_node()
23 int t = get_thread_num();
24 for (i = 0; i < MAX_FREELIST; i++) {
25 unsigned int node = load_32(&free_lists[t][i]);
27 store_32(&free_lists[t][i], 0);
31 /* free_list is empty? */
36 /* Simulate the fact that when a node got recycled, it will get assigned to the
37 * same queue or for other usage */
38 void simulateRecycledNodeUpdate(queue_t *q, unsigned int node) {
39 atomic_store_explicit(&q->nodes[node].next, -1, memory_order_release);
43 /* Place this node index back on this thread's free list */
44 static void reclaim(unsigned int node)
47 int t = get_thread_num();
49 /* Don't reclaim NULL node */
52 for (i = 0; i < MAX_FREELIST; i++) {
53 /* Should never race with our own thread here */
54 unsigned int idx = load_32(&free_lists[t][i]);
56 /* Found empty spot in free list */
58 store_32(&free_lists[t][i], node);
62 /* free list is full? */
66 void init_queue(queue_t *q, int num_threads)
70 /* Initialize each thread's free list with INITIAL_FREE pointers */
71 /* The actual nodes are initialized with poison indexes */
72 free_lists = malloc(num_threads * sizeof(*free_lists));
73 for (i = 0; i < num_threads; i++) {
74 for (j = 0; j < INITIAL_FREE; j++) {
75 free_lists[i][j] = 2 + i * MAX_FREELIST + j;
76 atomic_init(&q->nodes[free_lists[i][j]].next, MAKE_POINTER(POISON_IDX, 0));
80 /* initialize queue */
81 atomic_init(&q->head, MAKE_POINTER(1, 0));
82 atomic_init(&q->tail, MAKE_POINTER(1, 0));
83 atomic_init(&q->nodes[1].next, MAKE_POINTER(0, 0));
86 void enqueue(queue_t *q, unsigned int val, bool yield)
95 store_32(&q->nodes[node].value, val);
96 tmp = atomic_load_explicit(&q->nodes[node].next, relaxed);
97 set_ptr(&tmp, 0); // NULL
98 atomic_store_explicit(&q->nodes[node].next, tmp, relaxed);
101 tail = atomic_load_explicit(&q->tail, acquire);
102 next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire);
103 if (tail == atomic_load_explicit(&q->tail, relaxed)) {
105 /* Check for uninitialized 'next' */
106 MODEL_ASSERT(get_ptr(next) != POISON_IDX);
108 if (get_ptr(next) == 0) { // == NULL
109 pointer value = MAKE_POINTER(node, get_count(next) + 1);
110 success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next,
111 &next, value, release, release);
114 unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire));
115 pointer value = MAKE_POINTER(ptr,
116 get_count(tail) + 1);
117 atomic_compare_exchange_strong_explicit(&q->tail,
124 atomic_compare_exchange_strong_explicit(&q->tail,
126 MAKE_POINTER(node, get_count(tail) + 1),
130 bool dequeue(queue_t *q, unsigned int *retVal, unsigned int *reclaimNode)
138 head = atomic_load_explicit(&q->head, acquire);
139 tail = atomic_load_explicit(&q->tail, relaxed);
140 next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, acquire);
141 if (atomic_load_explicit(&q->head, relaxed) == head) {
142 if (get_ptr(head) == get_ptr(tail)) {
144 /* Check for uninitialized 'next' */
145 MODEL_ASSERT(get_ptr(next) != POISON_IDX);
147 if (get_ptr(next) == 0) { // NULL
148 return false; // NULL
150 atomic_compare_exchange_strong_explicit(&q->tail,
152 MAKE_POINTER(get_ptr(next), get_count(tail) + 1),
156 *retVal = load_32(&q->nodes[get_ptr(next)].value);
157 success = atomic_compare_exchange_strong_explicit(&q->head,
159 MAKE_POINTER(get_ptr(next), get_count(head) + 1),
166 reclaimNode = get_ptr(head);
167 reclaim(get_ptr(head));