# HG changeset patch # User Tatsuki IHA # Date 1486300527 -32400 # Node ID fd470e0904037ca5cf2450aee3e7d8c277fdab15 # Parent bc17237bc8cfa58c65a73a8719d18b2d7dd4a305 Add sempahore diff -r bc17237bc8cf -r fd470e090403 src/parallel_execution/CMakeLists.txt --- 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( diff -r bc17237bc8cf -r fd470e090403 src/parallel_execution/Queue.cbc --- 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; diff -r bc17237bc8cf -r fd470e090403 src/parallel_execution/Semaphore.cbc --- /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{ + union Data* semaphore; + __code p(Impl* semaphore, __code next(...)); + __code v(Impl* semaphore, __code next(...)); + __code next(...); +} Semaphore; + diff -r bc17237bc8cf -r fd470e090403 src/parallel_execution/SemaphoreImpl.cbc --- /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(...); +} + diff -r bc17237bc8cf -r fd470e090403 src/parallel_execution/context.h --- 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;