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 /* Place this node index back on this thread's free list */
37 static void reclaim(unsigned int node)
40 int t = get_thread_num();
42 /* Don't reclaim NULL node */
45 for (i = 0; i < MAX_FREELIST; i++) {
46 /* Should never race with our own thread here */
47 unsigned int idx = load_32(&free_lists[t][i]);
49 /* Found empty spot in free list */
51 store_32(&free_lists[t][i], node);
55 /* free list is full? */
59 void init_queue(queue_t *q, int num_threads)
63 /* Initialize each thread's free list with INITIAL_FREE pointers */
64 /* The actual nodes are initialized with poison indexes */
65 free_lists = malloc(num_threads * sizeof(*free_lists));
66 for (i = 0; i < num_threads; i++) {
67 for (j = 0; j < INITIAL_FREE; j++) {
68 free_lists[i][j] = 2 + i * MAX_FREELIST + j;
69 atomic_init(&q->nodes[free_lists[i][j]].next, MAKE_POINTER(POISON_IDX, 0));
73 /* initialize queue */
74 atomic_init(&q->head, MAKE_POINTER(1, 0));
75 atomic_init(&q->tail, MAKE_POINTER(1, 0));
76 atomic_init(&q->nodes[1].next, MAKE_POINTER(0, 0));
82 typedef struct tag_elem {
86 tag_elem(Tag _id, unsigned int _data) {
92 spec_queue<tag_elem_t> __queue;
95 # Only check the happens-before relationship according to the id of the
96 # commit_point_set. For commit_point_set that has same ID, A -> B means
97 # B happens after the previous A.
105 @Commit_point_set: Enqueue_Success_Point
106 @ID: __tag.getCurAndInc()
108 # __ID__ is an internal macro that refers to the id of the current
111 __queue.enqueue(tag_elem_t(__ID__, val));
114 void enqueue(queue_t *q, unsigned int val)
123 store_32(&q->nodes[node].value, val);
124 tmp = atomic_load_explicit(&q->nodes[node].next, relaxed);
125 set_ptr(&tmp, 0); // NULL
126 atomic_store_explicit(&q->nodes[node].next, tmp, relaxed);
129 tail = atomic_load_explicit(&q->tail, acquire);
130 next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire);
131 if (tail == atomic_load_explicit(&q->tail, relaxed)) {
133 /* Check for uninitialized 'next' */
134 MODEL_ASSERT(get_ptr(next) != POISON_IDX);
136 if (get_ptr(next) == 0) { // == NULL
137 pointer value = MAKE_POINTER(node, get_count(next) + 1);
138 success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next,
139 &next, value, release, release);
142 unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire));
143 pointer value = MAKE_POINTER(ptr,
144 get_count(tail) + 1);
145 int commit_success = 0;
146 commit_success = atomic_compare_exchange_strong_explicit(&q->tail,
147 &tail, value, release, release);
150 @Commit_point_define_check: __ATOMIC_RET__ == true
151 @Label: Enqueue_Success_Point
158 atomic_compare_exchange_strong_explicit(&q->tail,
160 MAKE_POINTER(node, get_count(tail) + 1),
167 @Commit_point_set: Dequeue_Success_Point
168 @ID: __queue.peak().tag
171 unsigned int _Old_Val = __queue.dequeue().data;
176 unsigned int dequeue(queue_t *q)
185 head = atomic_load_explicit(&q->head, acquire);
186 tail = atomic_load_explicit(&q->tail, relaxed);
187 next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, acquire);
188 if (atomic_load_explicit(&q->head, relaxed) == head) {
189 if (get_ptr(head) == get_ptr(tail)) {
191 /* Check for uninitialized 'next' */
192 MODEL_ASSERT(get_ptr(next) != POISON_IDX);
194 if (get_ptr(next) == 0) { // NULL
197 atomic_compare_exchange_strong_explicit(&q->tail,
199 MAKE_POINTER(get_ptr(next), get_count(tail) + 1),
203 value = load_32(&q->nodes[get_ptr(next)].value);
204 success = atomic_compare_exchange_strong_explicit(&q->head,
206 MAKE_POINTER(get_ptr(next), get_count(head) + 1),
210 @Commit_point_define_check: __ATOMIC_RET__ == true
211 @Label: Dequeue_Success_Point
219 reclaim(get_ptr(head));