Mercurial > hg > GearsTemplate
changeset 276:27bc962020de
add SynchronizedQueue.cbc
author | mir3636 |
---|---|
date | Wed, 01 Feb 2017 21:29:21 +0900 |
parents | 06dab015a54d |
children | 9d671e63df74 |
files | src/parallel_execution/SynchronizedQueue.cbc src/parallel_execution/Tree.cbc |
diffstat | 2 files changed, 75 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/SynchronizedQueue.cbc Wed Feb 01 21:29:21 2017 +0900 @@ -0,0 +1,66 @@ +#include "../queue.h" +#include "../origin_cs.h" +#include <stdio.h> + +Queue* createSynchronizedQueue(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_takeSynchronizedQueue; + queue->put = C_putSynchronizedQueue; + queue->isEmpty = C_isEmptySynchronizedQueue; + queue->clear = C_clearSynchronizedQueue; + return queue; +} + +__code clearSynchronizedQueue(struct SingleLinkedQueue* 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 SingleLinkedQueue* queue, struct Element* element, union Data* data, __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 next(...); +} + +__code takeSynchronizedQueue(struct SingleLinkedQueue* queue, __code next(union Data* data, ...)) { + 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 next(data, ...); +} + +__code isEmptySynchronizedQueue(struct SingleLinkedQueue* queue, __code next(...), __code whenEmpty(...)) { + if (queue->top) + goto next(...); + else + goto whenEmpty(...); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/Tree.cbc Wed Feb 01 21:29:21 2017 +0900 @@ -0,0 +1,9 @@ +typedef struct Tree<Impl{ + union Data* tree; + struct Node* node; + __code putRedBlackTree(Impl* traverse, struct Node* node, struct Node* root, struct Node* newNode); + __code getRedBlackTree(Impl* traverse, __code next(...)); + // __code removeRedBlackTree(); + // __code clearRedBlackTree(); + __code next(...); +} Tree;