Mercurial > hg > GearsTemplate
annotate src/parallel_execution/examples/boundedBuffer/BoundedBuffer.cbc @ 507:a7127917c736
SemaphoreImpl use spinlock
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 03 Jan 2018 17:34:14 +0900 |
parents | 2e7ea81e5943 |
children | bc1616e1e172 |
rev | line source |
---|---|
493
82f0c49750f1
Add codeGear for boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
492
diff
changeset
|
1 #include "../../../context.h" |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
2 #interface "Buffer.h" |
493
82f0c49750f1
Add codeGear for boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
492
diff
changeset
|
3 #interface "Semaphore.h" |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
5 Buffer* createBoundedBuffer(struct Context* context, int size) { |
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
6 struct Buffer* buffer = new Buffer(); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 struct BoundedBuffer* boundedBuffer = new BoundedBuffer(); |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 boundedBuffer->top = new Element(); |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 boundedBuffer->top->next = NULL; |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 boundedBuffer->last = boundedBuffer->top; |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 boundedBuffer->fullCount = createSemaphoreImpl(context, 0); |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 boundedBuffer->emptyCount = createSemaphoreImpl(context, size); |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 boundedBuffer->lock = createSemaphoreImpl(context, 1); // binary semaphore |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
14 buffer->buffer = (union Data*)boundedBuffer; |
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
15 buffer->take = C_takeBoundedBuffer; |
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
16 buffer->put = C_putBoundedBuffer; |
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
17 return buffer; |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 } |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
20 __code putBoundedBuffer(struct BoundedBuffer* buffer, union Data* data, __code next(...)) { |
507
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
21 struct Semaphore* semaphore = buffer->emptyCount; |
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
22 goto semaphore->p(putBoundedBuffer1); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 } |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
25 __code putBoundedBuffer1(struct BoundedBuffer* buffer, union Data* data, __code next(...)) { |
507
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
26 struct Semaphore* semaphore = buffer->lock; |
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
27 goto semaphore->p(putBoundedBuffer2); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 } |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
30 __code putBoundedBuffer2(struct BoundedBuffer* buffer, union Data* data, __code next(...)) { |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 struct Element* element = new Element(); |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 element->data = data; |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 element->next = NULL; |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
34 struct Element* last = buffer->last; |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 last->next = element; |
495
2e7ea81e5943
Work BoundedBuffer if singlethread
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
494
diff
changeset
|
36 buffer->last = element; |
507
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
37 struct Semaphore* semaphore = buffer->lock; |
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
38 goto semaphore->v(putBoundedBuffer3); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 } |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
41 __code putBoundedBuffer3(struct BoundedBuffer* buffer, union Data* data, __code next(...)) { |
507
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
42 struct Semaphore* semaphore = buffer->fullCount; |
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
43 goto semaphore->v(putBoundedBuffer4); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 } |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
46 __code putBoundedBuffer4(struct BoundedBuffer* buffer, union Data* data, __code next(...)) { |
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
47 goto next(...); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 } |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
49 __code takeBoundedBuffer(struct BoundedBuffer* buffer, __code next(union Data* data, ...)) { |
507
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
50 struct Semaphore* semaphore = buffer->fullCount; |
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
51 goto semaphore->p(takeBoundedBuffer1); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 } |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
54 __code takeBoundedBuffer1(struct BoundedBuffer* buffer, __code next(union Data* data, ...)) { |
507
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
55 struct Semaphore* semaphore = buffer->lock; |
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
56 goto semaphore->p(takeBoundedBuffer2); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 } |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
59 __code takeBoundedBuffer2(struct BoundedBuffer* buffer, __code next(union Data* data, ...)) { |
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
60 struct Element* top = buffer->top; |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 struct Element* nextElement = top->next; |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 data = nextElement->data; |
495
2e7ea81e5943
Work BoundedBuffer if singlethread
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
494
diff
changeset
|
63 *O_data =data; |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
64 buffer->top = nextElement; |
507
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
65 struct Semaphore* semaphore = buffer->lock; |
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
66 goto semaphore->v(takeBoundedBuffer3); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 } |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
69 __code takeBoundedBuffer3(struct BoundedBuffer* buffer, __code next(union Data* data, ...)) { |
507
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
70 struct Semaphore* semaphore = buffer->emptyCount; |
a7127917c736
SemaphoreImpl use spinlock
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
495
diff
changeset
|
71 goto semaphore->v(takeBoundedBuffer4); |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 } |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 |
494
d8b2036c6942
BoundedBuffer implments Buffer interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
493
diff
changeset
|
74 __code takeBoundedBuffer4(struct BoundedBuffer* buffer, __code next(union Data* data, ...)) { |
492
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 goto next(data, ...); |
9333486471b9
Add boundedBuffer example
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 } |