changeset 286:fd470e090403

Add sempahore
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Sun, 05 Feb 2017 22:15:27 +0900
parents bc17237bc8cf
children 6b099d73949c
files src/parallel_execution/CMakeLists.txt src/parallel_execution/Queue.cbc src/parallel_execution/Semaphore.cbc src/parallel_execution/SemaphoreImpl.cbc src/parallel_execution/context.h
diffstat 5 files changed, 57 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/CMakeLists.txt	Sun Feb 05 20:22:55 2017 +0900
+++ b/src/parallel_execution/CMakeLists.txt	Sun Feb 05 22:15:27 2017 +0900
@@ -40,7 +40,7 @@
   TARGET
       twice
   SOURCES 
-    main.cbc RedBlackTree.cbc compare.c SingleLinkedStack.cbc CPUWorker.cbc time.cbc twice.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc
+      main.cbc RedBlackTree.cbc compare.c SingleLinkedStack.cbc CPUWorker.cbc time.cbc twice.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc SemaphoreImpl.cbc
 )
 
 GearsCommand(
--- a/src/parallel_execution/Queue.cbc	Sun Feb 05 20:22:55 2017 +0900
+++ b/src/parallel_execution/Queue.cbc	Sun Feb 05 22:15:27 2017 +0900
@@ -7,5 +7,5 @@
         __code take(Impl* queue, __code next(union Data*, ...));
         __code isEmpty(Impl* queue, __code next(...), __code whenEmpty(...));
         __code next(...);
-} Stack;
+} Queue;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/Semaphore.cbc	Sun Feb 05 22:15:27 2017 +0900
@@ -0,0 +1,7 @@
+typedef struct Semaphore<Impl>{
+        union Data* semaphore;
+        __code p(Impl* semaphore, __code next(...)); 
+        __code v(Impl* semaphore, __code next(...)); 
+        __code next(...);
+} Semaphore;
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/SemaphoreImpl.cbc	Sun Feb 05 22:15:27 2017 +0900
@@ -0,0 +1,37 @@
+#include "../context.h"
+
+Semaphore* createSemaphoreImpl(struct Context* context, int n) {
+    struct Semaphore* semaphore = new Semaphore();
+    struct SemaphoreImpl* semaphoreImpl = new SemaphoreImpl();
+    semaphore->semaphore = (union Data*)semaphoreImpl;
+    semaphoreImpl->value =  n;
+    pthread_mutex_init(&semaphoreImpl->mutex, NULL);
+    pthread_cond_init(&semaphoreImpl->cond, NULL);
+    semaphore->p = C_pOperationSemaphoreImpl;
+    semaphore->v = C_vOperationSemaphoreImpl;
+    return semaphore;
+}
+
+__code pOperationSemaphoreImpl(struct SemaphoreImpl* semaphore, __code next(...)) {
+    pthread_mutex_lock(&semaphore->mutex);
+    goto meta(context, C_pOperationSemaphoreImpl1);
+}
+
+__code pOperationSemaphoreImpl1(struct SemaphoreImpl* semaphore, __code next(...)) {
+    if(semaphore->value == 0) {
+        pthread_cond_wait(&semaphore->cond, &semaphore->mutex);
+        goto meta(context, C_pOperationSemaphoreImpl1);
+    }
+    semaphore->value--;
+    pthread_mutex_unlock(&semaphore->mutex);
+    goto next(...);
+}
+
+__code vOperationSemaphoreImpl(struct SemaphoreImpl* semaphore, __code next(...)) {
+    pthread_mutex_lock(&semaphore->mutex);
+    semaphore->value++;
+    pthread_cond_signal(&semaphore->cond);
+    pthread_mutex_unlock(&semaphore->mutex);
+    goto next(...);
+}
+
--- a/src/parallel_execution/context.h	Sun Feb 05 20:22:55 2017 +0900
+++ b/src/parallel_execution/context.h	Sun Feb 05 22:15:27 2017 +0900
@@ -249,6 +249,17 @@
             Black,
         } color;
     } Node;
+    struct Semaphore {
+        union Data* semaphore;
+        enum Code p;
+        enum Code v;
+        enum Code next;
+    } Semaphore;
+    struct SemaphoreImpl {
+        int value;
+        pthread_mutex_t mutex;
+        pthread_cond_t cond;
+    } SemaphoreImpl;
     struct Allocate {
         enum Code next;
         long size;