comparison src/parallel_execution/examples/boundedBuffer/BoundedBuffer.cbc @ 492:9333486471b9

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