9 #include "ms-queue_simple.h"
12 void init_queue(queue_t *q, int num_threads);
13 void enqueue(queue_t *q, unsigned int val);
14 bool dequeue(queue_t *q, unsigned int *retVal);
16 void init_queue(queue_t *q, int num_threads) {
17 struct node *newnode=malloc(sizeof (struct node));
18 atomic_store(&newnode->value, 0);
20 atomic_store(&newnode->next, (uintptr_t) NULL);
21 /* initialize queue */
22 atomic_store(&q->head, newnode);
24 atomic_store(&q->tail, newnode);
27 void enqueue(queue_t *q, unsigned int val) {
29 struct node * node_ptr = malloc(sizeof(struct node));
30 atomic_store(&node_ptr->value, val);
32 atomic_store(&node_ptr->next, NULL);
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, unsigned 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)) {
81 static void e(void *param)
84 for(i=0;i<PROBLEMSIZE;i++)
88 static void d(void *param) {
90 for(i=0;i<PROBLEMSIZE;i++) {
91 dequeue(&queue, &val);
95 int user_main(int argc, char **argv)
97 // MODEL_ASSERT(queue);
99 init_queue(&queue, 2);
102 thrd_create(&t1, e, NULL);
103 thrd_create(&t2, d, NULL);