summaryrefslogtreecommitdiffstats
path: root/lock_free_queue.c
blob: 217fb0b4356d225241c34e2772417951a7cef2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * File     : lock_free_queue.c
 * Author   : Jérémy Zurcher  <jeremy@asynk.ch>
 * Date     : 01/11/09 
 * License  : stolen from http://www.cs.rochester.edu/~scott/papers/1996_PODC_queues.pdf
 */

#include "stdlib.h"
#include "cas.h"
#include "lock_free_queue.h"
#ifdef DEBUG
#include "stdio.h"
#endif

void init( lfq_t *q ) {
    node_t *node = (node_t*)malloc(sizeof(node_t));
    node->next.split.ptr = NULL;
    node->next.split.count = 0;
    q->head.split.ptr = q->tail.split.ptr = node;
    q->head.split.count = q->tail.split.count = 0;
}

void enqueue( lfq_t *q, void *data ) {
    node_t *node;
    pointer_t tail;
    pointer_t next;
    pointer_t tmp;

    node = (node_t*)malloc(sizeof(node_t));
    node->data = data;
    node->next.split.ptr = NULL;
    /* node->next.split.count = 0; */
    for(;;) {
        tail.concat = q->tail.concat;                   /* copy tail pointer */
        next.concat = tail.split.ptr->next.concat;      /* copy next pointer */
        if (tail.concat == q->tail.concat) {            /* tail is still consistent */
            if (next.split.ptr == NULL) {               /* next is still the last node */
                /* if tail->next is the same as next, link node at the end of the list */
                tmp.split.ptr = node;
                tmp.split.count = next.split.count+1;
                if ( compare_and_swap( &tail.split.ptr->next.concat, next.concat, tmp.concat ) ) break;
            } else {
                /* try to swing tail to the next node, if q-> tail is still tail => next is ok */
                tmp.split.ptr = next.split.ptr;
                tmp.split.count = tail.split.count+1;
                compare_and_swap( &q->tail.concat, tail.concat, tmp.concat );
            }
        }
    }
    /* try to swing tail to the next node, may have been done by a concurrent push */
    tmp.split.ptr = node;
    tmp.split.count = tail.split.count+1;
    compare_and_swap( &q->tail.concat, tail.concat, tmp.concat );
}

void* dequeue( lfq_t *q ) {
    void *data;
    pointer_t head;
    pointer_t tail;
    node_t *next;
    pointer_t tmp;
    
    for(;;) {
        head.concat = q->head.concat;
        tail.concat = q->tail.concat;
        next = (node_t *)head.split.ptr;
        if (head.concat == q->head.concat) {
            if (head.split.ptr == tail.split.ptr) {     /* still consistent */
                if (next->next.split.ptr == NULL) {     /* queue empty */
                    return NULL;
                }
                /* new node has been linked, but tail is behind, should advance it */
                tmp.split.ptr = next->next.split.ptr;
                tmp.split.count = tail.split.count+1;
                compare_and_swap( &q->tail.concat, tail.concat, tmp.concat );
            } else {
                data = next->data;
                /* try to swing head to the next node */
                tmp.split.ptr = next->next.split.ptr;
                tmp.split.count = head.split.count+1;
                if( compare_and_swap( &q->head.concat, head.concat, tmp.concat ) ) break;
            }
        }
    }
    free((void*)head.split.ptr);
    return data;
}