annotate src/parallel_execution/SynchronizedQueue.cbc @ 276:27bc962020de

add SynchronizedQueue.cbc
author mir3636
date Wed, 01 Feb 2017 21:29:21 +0900
parents
children 2c2e4e597eb0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
276
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
1 #include "../queue.h"
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
2 #include "../origin_cs.h"
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
3 #include <stdio.h>
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
4
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
5 Queue* createSynchronizedQueue(struct Context* context) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
6 struct Queue* queue = new Queue();
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
7 struct SingleLinkedQueue* singleLinkedQueue = new SingleLinkedQueue();
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
8 queue->queue = (union Data*)singleLinkedQueue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
9 singleLinkedQueue->top = NULL;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
10 singleLinkedQueue->last = NULL;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
11 queue->take = C_takeSynchronizedQueue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
12 queue->put = C_putSynchronizedQueue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
13 queue->isEmpty = C_isEmptySynchronizedQueue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
14 queue->clear = C_clearSynchronizedQueue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
15 return queue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
16 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
17
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
18 __code clearSynchronizedQueue(struct SingleLinkedQueue* queue, __code next(...)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
19 struct Element* top = queue->top;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
20 if (__sync_bool_compare_and_swap(&queue->top, top, NULL)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
21 goto next(...);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
22 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
23 goto meta(context, C_clearSynchronizedQueue);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
24 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
25 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
26
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
27 __code putSynchronizedQueue(struct SingleLinkedQueue* queue, struct Element* element, union Data* data, __code next(...)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
28 element->next = NULL;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
29 element->data = data;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
30 if (queue->last) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
31 Element* last = queue->last;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
32 if (__sync_bool_compare_and_swap(&queue->last, last, element)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
33 last->next = element;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
34 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
35 goto meta(context, C_putSynchronizedQueue);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
36 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
37 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
38 if (__sync_bool_compare_and_swap(&queue->top, NULL, element)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
39 queue->last = element;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
40 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
41 goto meta(context, C_putSynchronizedQueue);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
42 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
43 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
44 goto next(...);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
45 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
46
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
47 __code takeSynchronizedQueue(struct SingleLinkedQueue* queue, __code next(union Data* data, ...)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
48 if (queue->top) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
49 struct Element* top = queue->top;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
50 if (__sync_bool_compare_and_swap(&queue->top, top, top->next)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
51 *data = top->data;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
52 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
53 goto meta(context, C_takeSynchronizedQueue);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
54 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
55 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
56 *data = NULL;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
57 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
58 goto next(data, ...);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
59 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
60
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
61 __code isEmptySynchronizedQueue(struct SingleLinkedQueue* queue, __code next(...), __code whenEmpty(...)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
62 if (queue->top)
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
63 goto next(...);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
64 else
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
65 goto whenEmpty(...);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
66 }