Mercurial > hg > Members > Moririn
annotate src/parallel_execution/SynchronizedQueue.cbc @ 313:4addbc7469ee
fix
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 15 Feb 2017 11:36:10 +0900 |
parents | f1b0cc555b6e |
children | a2c01ab30ea2 |
rev | line source |
---|---|
280 | 1 #include "../context.h" |
2 | |
276 | 3 #include <stdio.h> |
4 | |
5 Queue* createSynchronizedQueue(struct Context* context) { | |
6 struct Queue* queue = new Queue(); | |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
7 struct SynchronizedQueue* synchronizedQueue = new SynchronizedQueue(); |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
8 synchronizedQueue->top = NULL; |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
9 synchronizedQueue->last = NULL; |
287
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
10 synchronizedQueue ->queueCount = createSemaphoreImpl(context, 0); |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
11 queue->queue = (union Data*)synchronizedQueue; |
276 | 12 queue->take = C_takeSynchronizedQueue; |
13 queue->put = C_putSynchronizedQueue; | |
14 queue->isEmpty = C_isEmptySynchronizedQueue; | |
15 queue->clear = C_clearSynchronizedQueue; | |
16 return queue; | |
17 } | |
18 | |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
19 __code clearSynchronizedQueue(struct SynchronizedQueue* queue, __code next(...)) { |
276 | 20 struct Element* top = queue->top; |
21 if (__sync_bool_compare_and_swap(&queue->top, top, NULL)) { | |
22 goto next(...); | |
23 } else { | |
24 goto meta(context, C_clearSynchronizedQueue); | |
25 } | |
26 } | |
27 | |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
28 __code putSynchronizedQueue(struct SynchronizedQueue* queue, union Data* data, __code next(...)) { |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
29 Element* element = new Element(); |
276 | 30 element->next = NULL; |
31 element->data = data; | |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
32 if (queue->top) { |
276 | 33 Element* last = queue->last; |
34 if (__sync_bool_compare_and_swap(&queue->last, last, element)) { | |
35 last->next = element; | |
36 } else { | |
37 goto meta(context, C_putSynchronizedQueue); | |
38 } | |
39 } else { | |
40 if (__sync_bool_compare_and_swap(&queue->top, NULL, element)) { | |
41 queue->last = element; | |
42 } else { | |
43 goto meta(context, C_putSynchronizedQueue); | |
44 } | |
45 } | |
287
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
46 goto meta(context, C_putSynchronizedQueue1); |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
47 } |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
48 |
288 | 49 __code putSynchronizedQueue1(struct SynchronizedQueue* queue, struct Semaphore* semaphore, __code next(...)) { |
287
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
50 semaphore->semaphore = (union Data*)queue->queueCount; |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
51 semaphore->next = next; |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
52 goto meta(context, queue->queueCount->v); |
276 | 53 } |
54 | |
287
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
55 __code takeSynchronizedQueue(struct SynchronizedQueue* queue, struct Semaphore* semaphore) { |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
56 semaphore->semaphore = (union Data*)queue->queueCount; |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
57 semaphore->next = C_takeSynchronizedQueue1; |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
58 goto meta(context, queue->queueCount->p); |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
59 } |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
60 |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
61 __code takeSynchronizedQueue1(struct SynchronizedQueue* queue, __code next(union Data* data, ...)) { |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
62 struct Element* top = queue->top; |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
63 if (__sync_bool_compare_and_swap(&queue->top, top, top->next)) { |
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
282
diff
changeset
|
64 data = top->data; |
276 | 65 } else { |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
66 goto meta(context, C_takeSynchronizedQueue); |
276 | 67 } |
68 goto next(data, ...); | |
69 } | |
70 | |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
71 __code isEmptySynchronizedQueue(struct SynchronizedQueue* queue, __code next(...), __code whenEmpty(...)) { |
276 | 72 if (queue->top) |
73 goto next(...); | |
74 else | |
75 goto whenEmpty(...); | |
76 } |