Mercurial > hg > Papers > 2024 > matac-master
view Paper/src/SynchronizedQueue.cbc @ 46:9baf70df56fa
copy algo
author | matac42 <matac@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 29 Jan 2024 13:41:22 +0900 |
parents | c8151a82f313 |
children |
line wrap: on
line source
#include "../context.h" #interface "Queue.h" #interface "Atomic.h" #include <stdio.h> Queue* createSynchronizedQueue(struct Context* context) { struct Queue* queue = new Queue(); struct SynchronizedQueue* synchronizedQueue = new SynchronizedQueue(); synchronizedQueue->top = new Element(); // allocate a free node synchronizedQueue->top->next = NULL; synchronizedQueue->last = synchronizedQueue->top; synchronizedQueue->atomic = createAtomicReference(context); queue->queue = (union Data*)synchronizedQueue; queue->take = C_takeSynchronizedQueue; queue->put = C_putSynchronizedQueue; queue->isEmpty = C_isEmptySynchronizedQueue; queue->clear = C_clearSynchronizedQueue; return queue; } __code putSynchronizedQueue(struct SynchronizedQueue* queue, union Data* data, __code next(...)) { Element* element = new Element(); element->data = data; element->next = NULL; Element* last = queue->last; Element* nextElement = last->next; if (last != queue->last) { goto putSynchronizedQueue(); } if (nextElement == NULL) { struct Atomic* atomic = queue->atomic; goto atomic->checkAndSet(&last->next, nextElement, element, next(...), putSynchronizedQueue); } else { struct Atomic* atomic = queue->atomic; goto atomic->checkAndSet(&queue->last, last, nextElement, putSynchronizedQueue, putSynchronizedQueue); } }