9 #include "ms-queue_simple.h"
12 void init_queue(queue_t *q, int num_threads);
13 void enqueue(queue_t *q, int val);
14 bool dequeue(queue_t *q, int *retVal);
16 void init_queue(queue_t *q, int num_threads) {
17 struct node *newnode=(struct node *) malloc(sizeof (struct node));
18 atomic_store_explicit(&newnode->value, 0, std::memory_order_release);
20 newnode->next.store(NULL, std::memory_order_release);
21 /* initialize queue */
22 q->head.store(newnode, std::memory_order_release);
24 q->tail.store( newnode, std::memory_order_release);
27 void enqueue(queue_t *q, int val) {
29 struct node * node_ptr = (struct node *) malloc(sizeof(struct node));
30 atomic_store_explicit(&node_ptr->value, val, std::memory_order_release);
32 node_ptr->next.store(NULL, std::memory_order_release);
35 tail = (struct node *) atomic_load(&q->tail);
37 struct node * next = (struct node *) atomic_load( &tail->next);
39 struct node * qtail = (struct node *) atomic_load(&q->tail);
43 if (atomic_compare_exchange_strong( & tail->next,& next, node_ptr))
47 struct node * new_tailptr = (struct node *)atomic_load( &tail->next);
48 atomic_compare_exchange_strong(&q->tail, & tail, new_tailptr);
51 atomic_compare_exchange_strong(&q->tail, & tail, node_ptr);
54 bool dequeue(queue_t *q, int *retVal) {
56 struct node * head = (struct node *) atomic_load(&q->head);
57 struct node * tail = (struct node *) atomic_load(&q->tail);
58 struct node * next = (struct node *) atomic_load(&head->next);
60 int t = ((struct node *) atomic_load(&q->head)) == head;
67 atomic_compare_exchange_strong(&q->tail, & tail, next);
70 *retVal = atomic_load(&next->value);
71 if (atomic_compare_exchange_strong(&q->head,& head, next)) {
83 problemsize enqueue(&queue, 1);
87 void * d(void *param) {
89 problemsize dequeue(&queue, &val);
93 int main(int argc, char **argv)
95 // MODEL_ASSERT(queue);
97 init_queue(&queue, 2);
100 pthread_create(&t1, 0, e, NULL);
101 pthread_create(&t2, 0, d, NULL);