Skip to content

Instantly share code, notes, and snippets.

@matteobertozzi
Created May 20, 2012 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matteobertozzi/2758693 to your computer and use it in GitHub Desktop.
Save matteobertozzi/2758693 to your computer and use it in GitHub Desktop.
Atomic Queue (DWCAS)
/* gcc atomic-queue.c -mcx16 */
/*
Copyright (c) 2011, Matteo Bertozzi
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct atomic_pointer {
uint64_t pointer;
uint64_t serial;
} atomic_pointer_t;
#define atomic_pointer_get(ptr, type) ((type *)((ptr)->pointer))
#define atomic_pointer_equals(a, b) \
((a)->pointer == (b)->pointer && (a)->serial == (b)->serial)
#define atomic_pointer_set(ptr, value) \
do { \
(ptr)->pointer = (uint64_t)value; \
(ptr)->serial = 0; \
} while (0)
#define atomic_pointer_copy(ptr, refptr) \
do { \
(ptr)->pointer = (refptr)->pointer; \
(ptr)->serial = (refptr)->serial; \
} while (0)
#define atomic_pointer_cas(ptr, old, new) \
__sync_bool_compare_and_swap((__uint128_t *)(ptr), \
*((__uint128_t *)(old)), \
*((__uint128_t *)(new)))
typedef struct atomic_qnode {
atomic_pointer_t next;
int data;
} atomic_qnode_t;
typedef struct atomic_queue {
atomic_pointer_t head;
atomic_pointer_t tail;
} atomic_queue_t;
int atomic_queue_open (atomic_queue_t *queue) {
atomic_qnode_t *node;
if ((node = (atomic_qnode_t *) malloc(sizeof(atomic_qnode_t))) == NULL)
return(1);
atomic_pointer_set(&(node->next), NULL);
atomic_pointer_set(&(queue->head), node);
atomic_pointer_set(&(queue->tail), node);
return(0);
}
int atomic_queue_push (atomic_queue_t *queue, int value) {
atomic_pointer_t pnode;
atomic_pointer_t tail;
atomic_pointer_t next;
atomic_qnode_t *tnode;
atomic_qnode_t *node;
if ((node = (atomic_qnode_t *) malloc(sizeof(atomic_qnode_t))) == NULL)
return(-1);
atomic_pointer_set(&(node->next), NULL);
node->data = value;
atomic_pointer_set(&pnode, node);
while (1) {
atomic_pointer_copy(&tail, &(queue->tail));
tnode = atomic_pointer_get(&tail, atomic_qnode_t);
atomic_pointer_copy(&next, &(tnode->next));
if (atomic_pointer_equals(&(queue->tail), &tail)) {
if (next.pointer == 0) {
pnode.serial = next.serial + 1;
tnode = atomic_pointer_get(&tail, atomic_qnode_t);
if (atomic_pointer_cas(&(tnode->next), &next, &pnode))
break;
} else {
next.serial = tail.serial + 1;
atomic_pointer_cas(&(queue->tail), &tail, &next);
}
}
}
pnode.serial = tail.serial + 1;
atomic_pointer_cas(&(queue->tail), &tail, &pnode);
return(0);
}
int atomic_queue_pop (atomic_queue_t *queue) {
atomic_pointer_t head;
atomic_pointer_t tail;
atomic_pointer_t next;
atomic_qnode_t *node;
int value = -1;
while (1) {
atomic_pointer_copy(&head, &(queue->head));
atomic_pointer_copy(&tail, &(queue->tail));
node = atomic_pointer_get(&head, atomic_qnode_t);
atomic_pointer_copy(&next, &(node->next));
if (atomic_pointer_equals(&(queue->head), &head)) {
if (head.pointer == tail.pointer) {
if (next.pointer == 0)
return(-1);
next.serial = tail.serial + 1;
atomic_pointer_cas(&(queue->tail), &tail, &next);
} else {
node = atomic_pointer_get(&next, atomic_qnode_t);
value = node->data;
next.serial = head.serial + 1;
if (atomic_pointer_cas(&(queue->head), &head, &next))
break;
}
}
}
node = atomic_pointer_get(&head, atomic_qnode_t);
free(node);
return(value);
}
void atomic_queue_print (atomic_queue_t *queue) {
unsigned int count = 0;
atomic_qnode_t *node;
printf("Queue\n");
node = atomic_pointer_get(&(queue->head), atomic_qnode_t);
printf(" - Node Dummy %d\n", node->data);
node = atomic_pointer_get(&(node->next), atomic_qnode_t);
while (node != NULL) {
printf(" - Node %d Value %d\n", count, node->data);
count++;
node = atomic_pointer_get(&(node->next), atomic_qnode_t);
}
}
int main (int argc, char **argv) {
atomic_queue_t queue;
int i, v;
atomic_queue_open(&queue);
for (i = 0; i < 10; ++i)
atomic_queue_push(&queue, i);
atomic_queue_print(&queue);
for (i = 0; i < 10; ++i) {
v = atomic_queue_pop(&queue);
printf("POP %d\n", v);
atomic_queue_print(&queue);
}
v = atomic_queue_pop(&queue);
printf("POP %d\n", v);
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment