view src/parallel_execution/queue.c @ 217:c34e6aa10967

Fix DataGear access name
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Mon, 09 Jan 2017 01:18:13 +0900
parents 25db17f32ac2
children
line wrap: on
line source

#include "queue.h"
#include "origin_cs.h"
#include <stdio.h>

union Data* createSingleLinkedQueue(struct Context* context) {
    struct Queue* queue = &ALLOCATE(context, Queue)->Queue;
    struct SingleLinkedQueue* singleLinkedQueue = &ALLOCATE(context, SingleLinkedQueue)->SingleLinkedQueue;
    queue->queue = (union Data*)singleLinkedQueue;
    singleLinkedQueue->top  = NULL;
    singleLinkedQueue->last = NULL;
    queue->take  = C_takeSingleLinkedQueue;
    queue->put  = C_putSingleLinkedQueue;
    queue->isEmpty = C_isEmptySingleLinkedQueue;
    queue->clear = C_clearSingleLinkedQueue;
    return (union Data*)(queue);
}

void printQueue1(union Data* data) {
    struct Node* node = &data->Element.data->Node;
    if (node == NULL) {
        printf("NULL");
    } else {
        printf("key = %d ,", node->key);
        printQueue1((union Data*)data->Element.next);
    }
}

void printQueue(union Data* data) {
    printQueue1(data);
    printf("\n");
}

__code clearSingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue,enum Code next) {
    queue->top = NULL;
    goto meta(context, next);
}

__code clearSingleLinkedQueue_stub(struct Context* context) {
    goto clearSingleLinkedQueue(context, (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, context->data[D_Queue]->Queue.next);
}

__code putSingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue, struct Element* element, union Data* data, enum Code next) {
    element->next = NULL;
    element->data = data;
    if (queue->last) {
        Element* last = queue->last;
        last->next = element;
        queue->last = element;
    } else {
        queue->top = element;
        queue->last = element;
    } 
    goto meta(context, next);
}

__code putSingleLinkedQueue_stub(struct Context* context) {
    struct Element* element = &ALLOCATE(context, Element)->Element;
    goto putSingleLinkedQueue(context,
                               (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue,
                               element,
                               context->data[D_Queue]->Queue.data,
                               context->data[D_Queue]->Queue.next);
}

__code takeSingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue, union Data** data, enum Code next) {
    if (queue->top) {
        *data = queue->top->data;
        queue->top = queue->top->next;
    } else {
        *data = NULL;
    }
    goto meta(context, next);
}

__code takeSingleLinkedQueue_stub(struct Context* context) {
    goto takeSingleLinkedQueue(context,
                               (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue,
                               &context->data[D_Queue]->Queue.data,
                               context->data[D_Queue]->Queue.next);
}

__code isEmptySingleLinkedQueue(struct Context* context, struct SingleLinkedQueue* queue, enum Code next,enum Code whenEmpty) {
    if (queue->top)
        goto meta(context, next);
    else
        goto meta(context, whenEmpty);
}

__code isEmptySingleLinkedQueue_stub(struct Context* context) {
    goto isEmptySingleLinkedQueue(context,
                               (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue,
                               context->data[D_Queue]->Queue.next,
                               context->data[D_Queue]->Queue.whenEmpty);
}

union Data* createSynchronizedQueue(struct Context* context) {
    struct Queue* queue = &ALLOCATE(context, Queue)->Queue;
    struct SingleLinkedQueue* singleLinkedQueue = &ALLOCATE(context, SingleLinkedQueue)->SingleLinkedQueue;
    queue->queue = (union Data*)singleLinkedQueue;
    singleLinkedQueue->top  = NULL;
    singleLinkedQueue->last = NULL;
    queue->take  = C_takeSynchronizedQueue;
    queue->put  = C_putSynchronizedQueue;
    queue->isEmpty = C_isEmptySynchronizedQueue;
    queue->clear = C_clearSynchronizedQueue;
    return (union Data*)(queue);
}

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

__code clearSynchronizedQueue_stub(struct Context* context) {
    goto clearSingleLinkedQueue(context, (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue, context->data[D_Queue]->Queue.next);
}

__code putSynchronizedQueue(struct Context* context, struct SingleLinkedQueue* queue, struct Element* element, union Data* data, enum Code next) {
    element->next = NULL;
    element->data = data;
    if (queue->last) {
        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, next);
}

__code putSynchronizedQueue_stub(struct Context* context) {
    struct Element* element = &ALLOCATE(context, Element)->Element;
    goto putSynchronizedQueue(context,
            (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue,
            element,
            context->data[D_Queue]->Queue.data,
            context->data[D_Queue]->Queue.next);
}

__code takeSynchronizedQueue(struct Context* context, struct SingleLinkedQueue* queue, union Data** data, enum Code next) {
    if (queue->top) {
        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);
        }
    } else {
        *data = NULL;
    }
    goto meta(context, next);
}

__code takeSynchronizedQueue_stub(struct Context* context) {
    goto takeSynchronizedQueue(context,
            (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue,
            &context->data[D_Queue]->Queue.data,
            context->data[D_Queue]->Queue.next);
}

__code isEmptySynchronizedQueue(struct Context* context, struct SingleLinkedQueue* queue, enum Code next,enum Code whenEmpty) {
    if (queue->top)
        goto meta(context, next);
    else
        goto meta(context, whenEmpty);
}

__code isEmptySynchronizedQueue_stub(struct Context* context) {
    goto isEmptySynchronizedQueue(context,
            (struct SingleLinkedQueue *)context->data[D_Queue]->Queue.queue->Queue.queue,
            context->data[D_Queue]->Queue.next,
            context->data[D_Queue]->Queue.whenEmpty);
}