Mercurial > hg > GearsTemplate
comparison src/parallel_execution/examples/boundedBuffer/BoundedBuffer.cbc @ 493:82f0c49750f1
Add codeGear for boundedBuffer example
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 31 Dec 2017 01:36:18 +0900 |
parents | 9333486471b9 |
children | d8b2036c6942 |
comparison
equal
deleted
inserted
replaced
492:9333486471b9 | 493:82f0c49750f1 |
---|---|
1 #include "../../context.h" | 1 #include "../../../context.h" |
2 #interface Queue.h | 2 #interface "Queue.h" |
3 #interface Semaphore.h | 3 #interface "Semaphore.h" |
4 | 4 |
5 Queue* createBoundedBuffer(struct Context* context, int size) { | 5 Queue* createBoundedBuffer(struct Context* context, int size) { |
6 struct Queue* queue = new Queue(); | 6 struct Queue* queue = new Queue(); |
7 struct BoundedBuffer* boundedBuffer = new BoundedBuffer(); | 7 struct BoundedBuffer* boundedBuffer = new BoundedBuffer(); |
8 boundedBuffer->top = new Element(); | 8 boundedBuffer->top = new Element(); |
12 boundedBuffer->emptyCount = createSemaphoreImpl(context, size); | 12 boundedBuffer->emptyCount = createSemaphoreImpl(context, size); |
13 boundedBuffer->lock = createSemaphoreImpl(context, 1); // binary semaphore | 13 boundedBuffer->lock = createSemaphoreImpl(context, 1); // binary semaphore |
14 queue->queue = (union Data*)boundedBuffer; | 14 queue->queue = (union Data*)boundedBuffer; |
15 queue->take = C_takeBoundedBuffer; | 15 queue->take = C_takeBoundedBuffer; |
16 queue->put = C_putBoundedBuffer; | 16 queue->put = C_putBoundedBuffer; |
17 queue->isEmpty = C_isEmptyBoundedBuffer; | 17 // queue->isEmpty = C_isEmptyBoundedBuffer; |
18 queue->clear = C_clearBoundedBuffer; | 18 // queue->clear = C_clearBoundedBuffer; |
19 return queue; | 19 return queue; |
20 } | 20 } |
21 | 21 |
22 __code putBoudnedBuffer(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { | 22 __code putBoundedBuffer(struct BoundedBuffer* queue, union Data* data, __code next(...)) { |
23 struct Semaphore sem = boundedBuffer->emptyCount; | 23 struct Semaphore* sem = queue->emptyCount; |
24 goto sem->p(putBoudnedBuffer1); | 24 goto sem->p(putBoundedBuffer1); |
25 } | 25 } |
26 | 26 |
27 __code putBoudnedBuffer1(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { | 27 __code putBoundedBuffer1(struct BoundedBuffer* queue, union Data* data, __code next(...)) { |
28 struct Semaphore sem = boundedBuffer->lock; | 28 struct Semaphore* sem = queue->lock; |
29 goto sem->p(putBoudnedBuffer2); | 29 goto sem->p(putBoundedBuffer2); |
30 } | 30 } |
31 | 31 |
32 __code putBoudnedBuffer2(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { | 32 __code putBoundedBuffer2(struct BoundedBuffer* queue, union Data* data, __code next(...)) { |
33 struct Element* element = new Element(); | 33 struct Element* element = new Element(); |
34 element->data = data; | 34 element->data = data; |
35 element->next = NULL; | 35 element->next = NULL; |
36 struct Element* last = queue->last; | 36 struct Element* last = queue->last; |
37 last->next = element; | 37 last->next = element; |
38 struct Semaphore sem = boundedBuffer->lock; | 38 struct Semaphore* sem = queue->lock; |
39 goto sem->v(putBoudnedBuffer3); | 39 goto sem->v(putBoundedBuffer3); |
40 } | 40 } |
41 | 41 |
42 __code putBoudnedBuffer3(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { | 42 __code putBoundedBuffer3(struct BoundedBuffer* queue, union Data* data, __code next(...)) { |
43 struct Semaphore sem = boundedBuffer->fullCount; | 43 struct Semaphore* sem = queue->fullCount; |
44 goto sem->v(putBoudnedBuffer4); | 44 goto sem->v(putBoundedBuffer4); |
45 } | 45 } |
46 | 46 |
47 __code putBoudnedBuffer4(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { | 47 __code putBoundedBuffer4(struct BoundedBuffer* queue, union Data* data, __code next(...)) { |
48 goto next(data, ...); | 48 goto next(data, ...); |
49 } | 49 } |
50 __code takeBoudnedBuffer(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { | 50 __code takeBoundedBuffer(struct BoundedBuffer* queue, __code next(union Data* data, ...)) { |
51 struct Semaphore sem = boundedBuffer->fullCount; | 51 struct Semaphore* sem = queue->fullCount; |
52 goto sem->p(takeBoudnedBuffer1); | 52 goto sem->p(takeBoundedBuffer1); |
53 } | 53 } |
54 | 54 |
55 __code takeBoudnedBuffer1(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { | 55 __code takeBoundedBuffer1(struct BoundedBuffer* queue, __code next(union Data* data, ...)) { |
56 struct Semaphore sem = boundedBuffer->lock; | 56 struct Semaphore* sem = queue->lock; |
57 goto sem->p(takeBoudnedBuffer2); | 57 goto sem->p(takeBoundedBuffer2); |
58 } | 58 } |
59 | 59 |
60 __code takeBoudnedBuffer2(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { | 60 __code takeBoundedBuffer2(struct BoundedBuffer* queue, __code next(union Data* data, ...)) { |
61 struct Element* top = queue->top; | 61 struct Element* top = queue->top; |
62 struct Element* nextElement = top->next; | 62 struct Element* nextElement = top->next; |
63 data = nextElement->data; | 63 data = nextElement->data; |
64 queue->top = nextElement; | 64 queue->top = nextElement; |
65 struct Semaphore sem = boundedBuffer->lock; | 65 struct Semaphore* sem = queue->lock; |
66 goto sem->v(takeBoudnedBuffer3); | 66 goto sem->v(takeBoundedBuffer3); |
67 } | 67 } |
68 | 68 |
69 __code takeBoudnedBuffer3(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { | 69 __code takeBoundedBuffer3(struct BoundedBuffer* queue, __code next(union Data* data, ...)) { |
70 struct Semaphore sem = boundedBuffer->emptyCount; | 70 struct Semaphore* sem = queue->emptyCount; |
71 goto sem->v(takeBoudnedBuffer4); | 71 goto sem->v(takeBoundedBuffer4); |
72 } | 72 } |
73 | 73 |
74 __code takeBoudnedBuffer4(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { | 74 __code takeBoundedBuffer4(struct BoundedBuffer* queue, __code next(union Data* data, ...)) { |
75 goto next(data, ...); | 75 goto next(data, ...); |
76 } | 76 } |