view src/parallel_execution/SynchronizedQueue.cbc @ 314:1839586f5b41

pthread CUDA test
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 15 Feb 2017 12:34:19 +0900
parents f1b0cc555b6e
children a2c01ab30ea2
line wrap: on
line source

#include "../context.h"

#include <stdio.h>

Queue* createSynchronizedQueue(struct Context* context) {
    struct Queue* queue = new Queue();
    struct SynchronizedQueue* synchronizedQueue = new SynchronizedQueue();
    synchronizedQueue->top  = NULL;
    synchronizedQueue->last = NULL;
    synchronizedQueue ->queueCount = createSemaphoreImpl(context, 0);
    queue->queue = (union Data*)synchronizedQueue;
    queue->take  = C_takeSynchronizedQueue;
    queue->put  = C_putSynchronizedQueue;
    queue->isEmpty = C_isEmptySynchronizedQueue;
    queue->clear = C_clearSynchronizedQueue;
    return queue;
}

__code clearSynchronizedQueue(struct SynchronizedQueue* queue, __code next(...)) {
    struct Element* top = queue->top;
    if (__sync_bool_compare_and_swap(&queue->top, top, NULL)) {
        goto next(...);
    } else {
        goto meta(context, C_clearSynchronizedQueue);
    }
}

__code putSynchronizedQueue(struct SynchronizedQueue* queue, union Data* data, __code next(...)) {
    Element* element = new Element();
    element->next = NULL;
    element->data = data;
    if (queue->top) {
        Element* last = queue->last;
        if (__sync_bool_compare_and_swap(&queue->last, last, element)) {
            last->next = element;
        } else {
            goto meta(context, C_putSynchronizedQueue);
        }
    } else {
        if (__sync_bool_compare_and_swap(&queue->top, NULL, element)) {
            queue->last = element;
        } else {
            goto meta(context, C_putSynchronizedQueue);
        }
    }
    goto meta(context, C_putSynchronizedQueue1);
}

__code putSynchronizedQueue1(struct SynchronizedQueue* queue, struct Semaphore* semaphore, __code next(...)) {
    semaphore->semaphore = (union Data*)queue->queueCount;
    semaphore->next = next;
    goto meta(context, queue->queueCount->v);
}

__code takeSynchronizedQueue(struct SynchronizedQueue* queue, struct Semaphore* semaphore) {
    semaphore->semaphore = (union Data*)queue->queueCount;
    semaphore->next = C_takeSynchronizedQueue1;
    goto meta(context, queue->queueCount->p);
}

__code takeSynchronizedQueue1(struct SynchronizedQueue* queue, __code next(union Data* data, ...)) {
    struct Element* top = queue->top;
    if (__sync_bool_compare_and_swap(&queue->top, top, top->next)) {
        data = top->data;
    } else {
        goto meta(context, C_takeSynchronizedQueue);
    }
    goto next(data, ...);
}

__code isEmptySynchronizedQueue(struct SynchronizedQueue* queue, __code next(...), __code whenEmpty(...)) {
    if (queue->top)
        goto next(...);
    else
        goto whenEmpty(...);
}