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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 276
diff changeset
1 #include "../context.h"
2c2e4e597eb0 generate no compile errors
mir3636
parents: 276
diff changeset
2
276
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();
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
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
12 queue->take = C_takeSynchronizedQueue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
13 queue->put = C_putSynchronizedQueue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
14 queue->isEmpty = C_isEmptySynchronizedQueue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
15 queue->clear = C_clearSynchronizedQueue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
16 return queue;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
17 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
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
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
20 struct Element* top = queue->top;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
21 if (__sync_bool_compare_and_swap(&queue->top, top, NULL)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
22 goto next(...);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
23 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
24 goto meta(context, C_clearSynchronizedQueue);
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
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
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
30 element->next = NULL;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
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
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
33 Element* last = queue->last;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
34 if (__sync_bool_compare_and_swap(&queue->last, last, element)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
35 last->next = element;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
36 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
37 goto meta(context, C_putSynchronizedQueue);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
38 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
39 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
40 if (__sync_bool_compare_and_swap(&queue->top, NULL, element)) {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
41 queue->last = element;
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
42 } else {
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
43 goto meta(context, C_putSynchronizedQueue);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
44 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
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
f1b0cc555b6e Add odgCommit
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 287
diff changeset
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
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
53 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
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
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
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
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
67 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
68 goto next(data, ...);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
69 }
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
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
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
72 if (queue->top)
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
73 goto next(...);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
74 else
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
75 goto whenEmpty(...);
27bc962020de add SynchronizedQueue.cbc
mir3636
parents:
diff changeset
76 }