9 #include "ms-queue-simple.h"
10 #include "libinterface.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);
18 void init_queue(queue_t *q, int num_threads) {
19 struct node *newnode=malloc(sizeof (struct node));
20 store_32(&newnode->value, 0);
22 store_64(&newnode->next, (uintptr_t) NULL);
23 /* initialize queue */
24 store_64(&q->head, (uintptr_t) newnode);
26 store_64(&q->tail, (uintptr_t) newnode);
29 void enqueue(queue_t *q, unsigned int val) {
31 struct node * node_ptr = malloc(sizeof(struct node));
33 store_32(&node_ptr->value, val);
36 store_64(&node_ptr->next, (uintptr_t) NULL);
40 tail = (struct node *) load_64(&q->tail);
41 struct node ** tail_ptr_next= & tail->next;
43 struct node * next = (struct node *) load_64(tail_ptr_next);
45 struct node * qtail = (struct node *) load_64(&q->tail);
51 struct node * valread = (struct node *) rmw_64(CAS, tail_ptr_next, (uintptr_t) next, (uintptr_t) node_ptr);
52 if (next == valread) {
56 struct node * new_tailptr = (struct node *)load_64( tail_ptr_next);
57 rmw_64(CAS, &q->tail, (uintptr_t) tail, (uintptr_t) new_tailptr);
64 rmw_64(CAS, &q->tail, (uintptr_t) tail, (uintptr_t) node_ptr);
67 bool dequeue(queue_t *q, unsigned int *retVal) {
69 struct node * head = (struct node *) load_64(&q->head);
70 struct node * tail = (struct node *) load_64(&q->tail);
71 struct node * next = (struct node *) load_64(&head->next);
73 struct node * headr = (struct node *) load_64(&q->head);
84 rmw_64(CAS, &q->tail, (uintptr_t) tail, (uintptr_t) next);
87 void * nextval= &(next->value);
88 *retVal = load_32(&next->value);
89 struct node *old_head = (struct node *) rmw_64(CAS, &q->head,
92 if (head == old_head) {
108 static void e(void *p) {
114 static void d(void *p) {
117 for(i=0;i<PROBLEMSIZE;i++)
118 dequeue(&queue, &val);
121 int user_main(int argc, char **argv)
123 init_queue(&queue, 2);
126 thrd_create(&t1, e, NULL);
127 thrd_create(&t2, d, NULL);