view src/parallel_execution/perlTests/BoundedBuffer.c @ 707:f967c9a372a4

remove context->before at testfile
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 24 Aug 2020 11:53:50 +0900
parents d05e327cbc50
children 793b21a8ea12
line wrap: on
line source

#include "../../../context.h"

Buffer* createBoundedBuffer(struct Context* context, int size) {
    struct Buffer* buffer = &ALLOCATE(context, Buffer)->Buffer;
    struct BoundedBuffer* boundedBuffer = &ALLOCATE(context, BoundedBuffer)->BoundedBuffer;
    boundedBuffer->top = &ALLOCATE(context, Element)->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
    buffer->buffer = (union Data*)boundedBuffer;
    buffer->take = C_takeBoundedBuffer;
    buffer->put = C_putBoundedBuffer;
    return buffer;
}

__code putBoundedBuffer(struct Context *context,struct BoundedBuffer* buffer, union Data* data, enum Code next) {
    struct Semaphore* semaphore = buffer->emptyCount;
    Gearef(context, Semaphore)->semaphore = (union Data*) semaphore;
    Gearef(context, Semaphore)->next = C_putBoundedBuffer1;
    goto meta(context, semaphore->p);
}

__code putBoundedBuffer_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	Data* data = Gearef(context, Buffer)->data;
	enum Code next = Gearef(context, Buffer)->next;
	goto putBoundedBuffer(context, buffer, data, next);
}

__code putBoundedBuffer1(struct Context *context,struct BoundedBuffer* buffer, union Data* data, enum Code next) {
    struct Semaphore* semaphore = buffer->lock;
    Gearef(context, Semaphore)->semaphore = (union Data*) semaphore;
    Gearef(context, Semaphore)->next = C_putBoundedBuffer2;
    goto meta(context, semaphore->p);
}

__code putBoundedBuffer1_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	Data* data = Gearef(context, Buffer)->data;
	enum Code next = Gearef(context, Buffer)->next;
	goto putBoundedBuffer1(context, buffer, data, next);
}

__code putBoundedBuffer2(struct Context *context,struct BoundedBuffer* buffer, union Data* data, enum Code next) {
    struct Element* element = &ALLOCATE(context, Element)->Element;
    element->data = data;
    element->next = NULL;
    struct Element* last = buffer->last;
    last->next = element;
    buffer->last = element;
    struct Semaphore* semaphore = buffer->lock;
    Gearef(context, Semaphore)->semaphore = (union Data*) semaphore;
    Gearef(context, Semaphore)->next = C_putBoundedBuffer3;
    goto meta(context, semaphore->v);
}

__code putBoundedBuffer2_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	Data* data = Gearef(context, Buffer)->data;
	enum Code next = Gearef(context, Buffer)->next;
	goto putBoundedBuffer2(context, buffer, data, next);
}

__code putBoundedBuffer3(struct Context *context,struct BoundedBuffer* buffer, union Data* data, enum Code next) {
    struct Semaphore* semaphore = buffer->fullCount;
    Gearef(context, Semaphore)->semaphore = (union Data*) semaphore;
    Gearef(context, Semaphore)->next = C_putBoundedBuffer4;
    goto meta(context, semaphore->v);
}

__code putBoundedBuffer3_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	Data* data = Gearef(context, Buffer)->data;
	enum Code next = Gearef(context, Buffer)->next;
	goto putBoundedBuffer3(context, buffer, data, next);
}

__code putBoundedBuffer4(struct Context *context,struct BoundedBuffer* buffer, union Data* data, enum Code next) {
    goto meta(context, next);
}

__code putBoundedBuffer4_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	Data* data = Gearef(context, Buffer)->data;
	enum Code next = Gearef(context, Buffer)->next;
	goto putBoundedBuffer4(context, buffer, data, next);
}

__code takeBoundedBuffer(struct Context *context,struct BoundedBuffer* buffer, enum Code next,union Data **O_data) {
	Data* data  __attribute__((unused))  = *O_data;
    struct Semaphore* semaphore = buffer->fullCount;
    Gearef(context, Semaphore)->semaphore = (union Data*) semaphore;
    Gearef(context, Semaphore)->next = C_takeBoundedBuffer1;
    goto meta(context, semaphore->p);
}

__code takeBoundedBuffer_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	enum Code next = Gearef(context, Buffer)->next;
	Data** O_data = &Gearef(context, Buffer)->data;
	goto takeBoundedBuffer(context, buffer, next, O_data);
}

__code takeBoundedBuffer1(struct Context *context,struct BoundedBuffer* buffer, enum Code next,union Data **O_data) {
	Data* data  __attribute__((unused))  = *O_data;
    struct Semaphore* semaphore = buffer->lock;
    Gearef(context, Semaphore)->semaphore = (union Data*) semaphore;
    Gearef(context, Semaphore)->next = C_takeBoundedBuffer2;
    goto meta(context, semaphore->p);
}

__code takeBoundedBuffer1_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	enum Code next = Gearef(context, Buffer)->next;
	Data** O_data = &Gearef(context, Buffer)->data;
	goto takeBoundedBuffer1(context, buffer, next, O_data);
}

__code takeBoundedBuffer2(struct Context *context,struct BoundedBuffer* buffer, enum Code next,union Data **O_data) {
	Data* data  __attribute__((unused))  = *O_data;
    struct Element* top = buffer->top;
    struct Element* nextElement = top->next;
    data = nextElement->data;
    *O_data =data;
    buffer->top = nextElement;
    struct Semaphore* semaphore = buffer->lock;
    Gearef(context, Semaphore)->semaphore = (union Data*) semaphore;
    Gearef(context, Semaphore)->next = C_takeBoundedBuffer3;
    goto meta(context, semaphore->v);
}

__code takeBoundedBuffer2_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	enum Code next = Gearef(context, Buffer)->next;
	Data** O_data = &Gearef(context, Buffer)->data;
	goto takeBoundedBuffer2(context, buffer, next, O_data);
}

__code takeBoundedBuffer3(struct Context *context,struct BoundedBuffer* buffer, enum Code next,union Data **O_data) {
	Data* data  __attribute__((unused))  = *O_data;
    struct Semaphore* semaphore = buffer->emptyCount;
    Gearef(context, Semaphore)->semaphore = (union Data*) semaphore;
    Gearef(context, Semaphore)->next = C_takeBoundedBuffer4;
    goto meta(context, semaphore->v);
}

__code takeBoundedBuffer3_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	enum Code next = Gearef(context, Buffer)->next;
	Data** O_data = &Gearef(context, Buffer)->data;
	goto takeBoundedBuffer3(context, buffer, next, O_data);
}

__code takeBoundedBuffer4(struct Context *context,struct BoundedBuffer* buffer, enum Code next,union Data **O_data) {
	Data* data  __attribute__((unused))  = *O_data;
	*O_data = data;
    goto meta(context, next);
}
__code takeBoundedBuffer4_stub(struct Context* context) {
	BoundedBuffer* buffer = (BoundedBuffer*)GearImpl(context, Buffer, buffer);
	enum Code next = Gearef(context, Buffer)->next;
	Data** O_data = &Gearef(context, Buffer)->data;
	goto takeBoundedBuffer4(context, buffer, next, O_data);
}