8 #include "ms-queue-simple.h"
9 #include "libinterface.h"
11 void init_queue(queue_t *q, int num_threads);
12 void enqueue(queue_t *q, unsigned int val);
13 bool dequeue(queue_t *q, unsigned int *retVal);
17 void init_queue(queue_t *q, int num_threads) {
18 struct node *newnode=(struct node *)malloc(sizeof (struct node));
20 store_32(&newnode->value, 0);
23 store_64(&newnode->next, (uintptr_t) NULL);
24 /* initialize queue */
26 store_64(&q->head, (uintptr_t) newnode);
29 store_64(&q->tail, (uintptr_t) newnode);
32 void enqueue(queue_t *q, unsigned int val) {
34 struct node * node_ptr; node_ptr = (struct node *)malloc(sizeof(struct node));
36 store_32(&node_ptr->value, val);
39 store_64(&node_ptr->next, (uintptr_t) NULL);
43 tail = (struct node *) load_64(&q->tail);
45 struct node * next = (struct node *) load_64(&tail->next);
47 struct node * qtail = (struct node *) load_64(&q->tail);
53 struct node * valread = (struct node *) rmw_64(CAS, &tail->next, (uintptr_t) next, (uintptr_t) node_ptr);
55 if (next == valread) {
60 struct node * new_tailptr = (struct node *)load_64(&tail->next);
61 rmw_64(CAS, &q->tail, (uintptr_t) tail, (uintptr_t) new_tailptr);
68 rmw_64(CAS, &q->tail, (uintptr_t) tail, (uintptr_t) node_ptr);
71 bool dequeue(queue_t *q, unsigned int *retVal) {
73 struct node * head = (struct node *) load_64(&q->head);
74 struct node * tail = (struct node *) load_64(&q->tail);
75 struct node * next = (struct node *) load_64(&head->next);
77 struct node * headr = (struct node *) load_64(&q->head);
86 rmw_64(CAS, &q->tail, (uintptr_t) tail, (uintptr_t) next);
89 *retVal = load_32(&next->value);
90 struct node *old_head = (struct node *) rmw_64(CAS, &q->head,
94 if (head == old_head) {
109 static void e(void *p) {
111 for(i=0;i<PROBLEMSIZE;i++) {
116 static void d(void *p) {
119 for(i=0;i<PROBLEMSIZE;i++) {
121 r = dequeue(&queue, &val);
125 int user_main(int argc, char **argv)
127 init_queue(&queue, 2);
130 thrd_create(&t1, e, NULL);
131 thrd_create(&t2, d, NULL);