view src/parallel_execution/SingleLinkedQueue.cbc @ 361:a2c01ab30ea2

Add synchronized queue of paper
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Wed, 28 Jun 2017 05:48:39 +0900
parents 2c2e4e597eb0
children 394e38952c80
line wrap: on
line source

#include "../context.h"
#include <stdio.h>

Queue* createSingleLinkedQueue(struct Context* context) {
    struct Queue* queue = new Queue();
    struct SingleLinkedQueue* singleLinkedQueue = new 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 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 SingleLinkedQueue* queue, __code next(...)) {
    queue->top = NULL;
    goto next(...);
}

__code putSingleLinkedQueue(struct SingleLinkedQueue* queue, union Data* data, __code next(...)) {
    Element* element = new Element();
    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 next(...);
}

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

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