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=malloc(sizeof (struct node));
19 store_32(&newnode->value, 0);
21 store_64(&newnode->next, (uintptr_t) NULL);
22 /* initialize queue */
23 store_64(&q->head, (uintptr_t) newnode);
25 store_64(&q->tail, (uintptr_t) newnode);
28 void enqueue(queue_t *q, unsigned int val) {
30 struct node * node_ptr = malloc(sizeof(struct node));
31 store_32(&node_ptr->value, val);
33 store_64(&node_ptr->next, (uintptr_t) NULL);
36 tail = (struct node *) load_64(&q->tail);
37 struct node ** tail_ptr_next= & tail->next;
39 struct node * next = (struct node *) load_64(tail_ptr_next);
41 struct node * qtail = (struct node *) load_64(&q->tail);
42 int tqt = (tail == qtail);
48 struct node * valread = (struct node *) rmw_64(CAS, tail_ptr_next, (uintptr_t) next, (uintptr_t) node_ptr);
49 int vn = (valread == next);
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);
63 rmw_64(CAS, &q->tail, (uintptr_t) tail, (uintptr_t) node_ptr);
66 bool dequeue(queue_t *q, unsigned int *retVal) {
68 struct node * head = (struct node *) load_64(&q->head);
69 struct node * tail = (struct node *) load_64(&q->tail);
70 struct node * next = (struct node *) load_64(&head->next);
72 int t = ((struct node *) load_64(&q->head)) == head;
74 int ht = (head == tail);
82 rmw_64(CAS, &q->tail, (uintptr_t) tail, (uintptr_t) next);
85 int nv = load_32(&next->value);
87 struct node *old_head = (struct node *) rmw_64(CAS, &q->head,
90 int ohh = (old_head == head);
106 static void e(void *p) {
112 static void d(void *p) {
116 dequeue(&queue, &val);
119 int user_main(int argc, char **argv)
121 init_queue(&queue, 2);
124 thrd_create(&t1, e, NULL);
125 thrd_create(&t2, d, NULL);