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
|
__code putSynchronizedQueue(struct SynchronizedQueue* queue, union Data* data, __code next(...)) {
Element* element = new Element();
element->data = data;
element->next = NULL;
Element* last = queue->last;
Element* nextElement = last->next;
if (last != queue->last) {
goto putSynchronizedQueue();
}
if (nextElement == NULL) {
struct Atomic* atomic = queue->atomic;
goto atomic->checkAndSet(&last->next, nextElement, element, next(...), putSynchronizedQueue);
} else {
struct Atomic* atomic = queue->atomic;
goto atomic->checkAndSet(&queue->last, last, nextElement, putSynchronizedQueue, putSynchronizedQueue);
}
}
__code takeSynchronizedQueue(struct SynchronizedQueue* queue, __code next(union Data* data, ...)) {
struct Element* top = queue->top;
struct Element* last = queue->last;
struct Element* nextElement = top->next;
if (top != queue->top) {
goto takeSynchronizedQueue();
}
if (top == last) {
if (nextElement != NULL) {
struct Atomic* atomic = queue->atomic;
goto atomic->checkAndSet(&queue->last, last, nextElement, takeSynchronizedQueue, takeSynchronizedQueue);
}
} else {
struct Atomic* atomic = queue->atomic;
goto atomic->checkAndSet(&queue->top, top, nextElement, takeSynchronizedQueue1, takeSynchronizedQueue);
}
goto takeSynchronizedQueue();
}
|