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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
286
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
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
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 Semaphore* createSemaphoreImpl(struct Context* context, int n) {
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 struct Semaphore* semaphore = new Semaphore();
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 struct SemaphoreImpl* semaphoreImpl = new SemaphoreImpl();
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 semaphore->semaphore = (union Data*)semaphoreImpl;
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 semaphoreImpl->value = n;
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 pthread_mutex_init(&semaphoreImpl->mutex, NULL);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 pthread_cond_init(&semaphoreImpl->cond, NULL);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 semaphore->p = C_pOperationSemaphoreImpl;
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 semaphore->v = C_vOperationSemaphoreImpl;
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 return semaphore;
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 }
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 __code pOperationSemaphoreImpl(struct SemaphoreImpl* semaphore, __code next(...)) {
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 pthread_mutex_lock(&semaphore->mutex);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 goto meta(context, C_pOperationSemaphoreImpl1);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 }
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 __code pOperationSemaphoreImpl1(struct SemaphoreImpl* semaphore, __code next(...)) {
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 if(semaphore->value == 0) {
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 pthread_cond_wait(&semaphore->cond, &semaphore->mutex);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 goto meta(context, C_pOperationSemaphoreImpl1);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 }
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 semaphore->value--;
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 pthread_mutex_unlock(&semaphore->mutex);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 goto next(...);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 }
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 __code vOperationSemaphoreImpl(struct SemaphoreImpl* semaphore, __code next(...)) {
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 pthread_mutex_lock(&semaphore->mutex);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 semaphore->value++;
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 pthread_cond_signal(&semaphore->cond);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 pthread_mutex_unlock(&semaphore->mutex);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 goto next(...);
fd470e090403 Add sempahore
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 }