Mercurial > hg > Gears > GearsAgda
view src/parallel_execution/SemaphoreImpl.cbc @ 479:b8b412a7670a
Fix segmentation fault if multithread
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 28 Dec 2017 19:51:06 +0900 |
parents | ac244346c85d |
children |
line wrap: on
line source
#include "../context.h" #interface "semaphore.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(...); }