Mercurial > hg > Members > Moririn
view src/parallel_execution/examples/boundedBuffer/BoundedBuffer.cbc @ 492:9333486471b9
Add boundedBuffer example
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 30 Dec 2017 22:03:33 +0900 |
parents | |
children | 82f0c49750f1 |
line wrap: on
line source
#include "../../context.h" #interface Queue.h #interface Semaphore.h Queue* createBoundedBuffer(struct Context* context, int size) { struct Queue* queue = new Queue(); struct BoundedBuffer* boundedBuffer = new BoundedBuffer(); boundedBuffer->top = new Element(); boundedBuffer->top->next = NULL; boundedBuffer->last = boundedBuffer->top; boundedBuffer->fullCount = createSemaphoreImpl(context, 0); boundedBuffer->emptyCount = createSemaphoreImpl(context, size); boundedBuffer->lock = createSemaphoreImpl(context, 1); // binary semaphore queue->queue = (union Data*)boundedBuffer; queue->take = C_takeBoundedBuffer; queue->put = C_putBoundedBuffer; queue->isEmpty = C_isEmptyBoundedBuffer; queue->clear = C_clearBoundedBuffer; return queue; } __code putBoudnedBuffer(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { struct Semaphore sem = boundedBuffer->emptyCount; goto sem->p(putBoudnedBuffer1); } __code putBoudnedBuffer1(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { struct Semaphore sem = boundedBuffer->lock; goto sem->p(putBoudnedBuffer2); } __code putBoudnedBuffer2(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { struct Element* element = new Element(); element->data = data; element->next = NULL; struct Element* last = queue->last; last->next = element; struct Semaphore sem = boundedBuffer->lock; goto sem->v(putBoudnedBuffer3); } __code putBoudnedBuffer3(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { struct Semaphore sem = boundedBuffer->fullCount; goto sem->v(putBoudnedBuffer4); } __code putBoudnedBuffer4(struct BoundedBuffer* boundedBuffer, union Data* data, __code next(union Data* data, ...)) { goto next(data, ...); } __code takeBoudnedBuffer(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { struct Semaphore sem = boundedBuffer->fullCount; goto sem->p(takeBoudnedBuffer1); } __code takeBoudnedBuffer1(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { struct Semaphore sem = boundedBuffer->lock; goto sem->p(takeBoudnedBuffer2); } __code takeBoudnedBuffer2(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { struct Element* top = queue->top; struct Element* nextElement = top->next; data = nextElement->data; queue->top = nextElement; struct Semaphore sem = boundedBuffer->lock; goto sem->v(takeBoudnedBuffer3); } __code takeBoudnedBuffer3(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { struct Semaphore sem = boundedBuffer->emptyCount; goto sem->v(takeBoudnedBuffer4); } __code takeBoudnedBuffer4(struct BoundedBuffer* boundedBuffer, __code next(union Data* data, ...)) { goto next(data, ...); }