Mercurial > hg > Members > Moririn
annotate src/parallel_execution/SemaphoreImpl.cbc @ 462:8d7e5d48cad3
Running CPU examples
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 20 Dec 2017 22:05:08 +0900 |
parents | b07078bd1f2c |
children | ac244346c85d |
rev | line source |
---|---|
286 | 1 #include "../context.h" |
462
8d7e5d48cad3
Running CPU examples
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
353
diff
changeset
|
2 #include "semaphore.h" |
286 | 3 |
4 Semaphore* createSemaphoreImpl(struct Context* context, int n) { | |
5 struct Semaphore* semaphore = new Semaphore(); | |
6 struct SemaphoreImpl* semaphoreImpl = new SemaphoreImpl(); | |
7 semaphore->semaphore = (union Data*)semaphoreImpl; | |
8 semaphoreImpl->value = n; | |
9 pthread_mutex_init(&semaphoreImpl->mutex, NULL); | |
10 pthread_cond_init(&semaphoreImpl->cond, NULL); | |
11 semaphore->p = C_pOperationSemaphoreImpl; | |
12 semaphore->v = C_vOperationSemaphoreImpl; | |
13 return semaphore; | |
14 } | |
15 | |
16 __code pOperationSemaphoreImpl(struct SemaphoreImpl* semaphore, __code next(...)) { | |
17 pthread_mutex_lock(&semaphore->mutex); | |
18 goto meta(context, C_pOperationSemaphoreImpl1); | |
19 } | |
20 | |
21 __code pOperationSemaphoreImpl1(struct SemaphoreImpl* semaphore, __code next(...)) { | |
22 if(semaphore->value == 0) { | |
23 pthread_cond_wait(&semaphore->cond, &semaphore->mutex); | |
24 goto meta(context, C_pOperationSemaphoreImpl1); | |
25 } | |
26 semaphore->value--; | |
27 pthread_mutex_unlock(&semaphore->mutex); | |
28 goto next(...); | |
29 } | |
30 | |
31 __code vOperationSemaphoreImpl(struct SemaphoreImpl* semaphore, __code next(...)) { | |
32 pthread_mutex_lock(&semaphore->mutex); | |
33 semaphore->value++; | |
34 pthread_cond_signal(&semaphore->cond); | |
35 pthread_mutex_unlock(&semaphore->mutex); | |
36 goto next(...); | |
37 } |