X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=ms-queue%2Fmy_queue.c;h=ef3555296b1933cfb0f1b8d34d203de6089ea4d6;hb=ff4cd01eb602228cbd4091539c3f9754cb946dda;hp=1f8a4461e87e7688d1ca73c32b2b4503b1b0955b;hpb=7df8191c93d3664a3b1ea2dfcd0e88de2cfcc7c9;p=model-checker-benchmarks.git diff --git a/ms-queue/my_queue.c b/ms-queue/my_queue.c index 1f8a446..ef35552 100644 --- a/ms-queue/my_queue.c +++ b/ms-queue/my_queue.c @@ -1,130 +1,162 @@ -#include "main.h" +#include +#include +#include "librace.h" +#include "model-assert.h" -extern unsigned pid; -extern unsigned iterations; -extern unsigned initial_nodes; -extern private_t private; -extern shared_mem_t* smp; +#include "my_queue.h" -void init_private() -{ - private.node = 2 + initial_nodes + pid; - private.value = 1 + initial_nodes + (pid * iterations); +#define relaxed memory_order_relaxed +#define release memory_order_release +#define acquire memory_order_acquire -} +#define MAX_FREELIST 4 /* Each thread can own up to MAX_FREELIST free nodes */ +#define INITIAL_FREE 2 /* Each thread starts with INITIAL_FREE free nodes */ -void init_memory() -{ -} +#define POISON_IDX 0x666 + +static unsigned int (*free_lists)[MAX_FREELIST]; -static unsigned new_node() +/* Search this thread's free list for a "new" node */ +static unsigned int new_node() { - return private.node; + int i; + int t = get_thread_num(); + for (i = 0; i < MAX_FREELIST; i++) { + unsigned int node = load_32(&free_lists[t][i]); + if (node) { + store_32(&free_lists[t][i], 0); + return node; + } + } + /* free_list is empty? */ + MODEL_ASSERT(0); + return 0; } -static void reclaim(unsigned node) +/* Place this node index back on this thread's free list */ +static void reclaim(unsigned int node) { - private.node = node; + int i; + int t = get_thread_num(); + + /* Don't reclaim NULL node */ + MODEL_ASSERT(node); + + for (i = 0; i < MAX_FREELIST; i++) { + /* Should never race with our own thread here */ + unsigned int idx = load_32(&free_lists[t][i]); + + /* Found empty spot in free list */ + if (idx == 0) { + store_32(&free_lists[t][i], node); + return; + } + } + /* free list is full? */ + MODEL_ASSERT(0); } -void init_queue() +void init_queue(queue_t *q, int num_threads) { - unsigned i; + int i, j; - /* initialize queue */ - smp->head.sep.ptr = 1; - smp->head.sep.count = 0; - smp->tail.sep.ptr = 1; - smp->tail.sep.count = 0; - smp->nodes[1].next.sep.ptr = NULL; - smp->nodes[1].next.sep.count = 0; - - /* initialize avail list */ - for (i=2; inodes[i].next.sep.ptr = i+1; - smp->nodes[i].next.sep.count = 0; - } - smp->nodes[MAX_NODES].next.sep.ptr = NULL; - smp->nodes[MAX_NODES].next.sep.count = 0; - - /* initialize queue contents */ - if (initial_nodes > 0) { - for (i=2; inodes[i].value = i; - smp->nodes[i-1].next.sep.ptr = i; - smp->nodes[i].next.sep.ptr = NULL; + /* Initialize each thread's free list with INITIAL_FREE pointers */ + /* The actual nodes are initialized with poison indexes */ + free_lists = malloc(num_threads * sizeof(*free_lists)); + for (i = 0; i < num_threads; i++) { + for (j = 0; j < INITIAL_FREE; j++) { + free_lists[i][j] = 2 + i * MAX_FREELIST + j; + atomic_init(&q->nodes[free_lists[i][j]].next, MAKE_POINTER(POISON_IDX, 0)); } - smp->head.sep.ptr = 1; - smp->tail.sep.ptr = 1 + initial_nodes; } + + /* initialize queue */ + atomic_init(&q->head, MAKE_POINTER(1, 0)); + atomic_init(&q->tail, MAKE_POINTER(1, 0)); + atomic_init(&q->nodes[1].next, MAKE_POINTER(0, 0)); } -void enqueue(unsigned val) +void enqueue(queue_t *q, unsigned int val) { - unsigned success; - unsigned node; - pointer_t tail; - pointer_t next; + int success = 0; + unsigned int node; + pointer tail; + pointer next; + pointer tmp; node = new_node(); - smp->nodes[node].value = val; - smp->nodes[node].next.sep.ptr = NULL; - - for (success = FALSE; success == FALSE; ) { - tail.con = smp->tail.con; - next.con = smp->nodes[tail.sep.ptr].next.con; - if (tail.con == smp->tail.con) { - if (next.sep.ptr == NULL) { - success = cas(&smp->nodes[tail.sep.ptr].next, - next.con, - MAKE_LONG(node, next.sep.count+1)); + store_32(&q->nodes[node].value, val); + tmp = atomic_load_explicit(&q->nodes[node].next, relaxed); + set_ptr(&tmp, 0); // NULL + atomic_store_explicit(&q->nodes[node].next, tmp, relaxed); + + while (!success) { + tail = atomic_load_explicit(&q->tail, acquire); + next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire); + if (tail == atomic_load_explicit(&q->tail, relaxed)) { + + /* Check for uninitialized 'next' */ + MODEL_ASSERT(get_ptr(next) != POISON_IDX); + + if (get_ptr(next) == 0) { // == NULL + pointer value = MAKE_POINTER(node, get_count(next) + 1); + success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next, + &next, value, release, release); } - if (success == FALSE) { - cas(&smp->tail, - tail.con, - MAKE_LONG(smp->nodes[tail.sep.ptr].next.sep.ptr, - tail.sep.count+1)); + if (!success) { + unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, acquire)); + pointer value = MAKE_POINTER(ptr, + get_count(tail) + 1); + atomic_compare_exchange_strong_explicit(&q->tail, + &tail, value, + release, release); thrd_yield(); } } } - cas(&smp->tail, - tail.con, - MAKE_LONG(node, tail.sep.count+1)); + atomic_compare_exchange_strong_explicit(&q->tail, + &tail, + MAKE_POINTER(node, get_count(tail) + 1), + release, release); } -unsigned dequeue() +unsigned int dequeue(queue_t *q) { - unsigned value; - unsigned success; - pointer_t head; - pointer_t tail; - pointer_t next; - - for (success = FALSE; success == FALSE; ) { - head.con = smp->head.con; - tail.con = smp->tail.con; - next.con = smp->nodes[head.sep.ptr].next.con; - if (smp->head.con == head.con) { - if (head.sep.ptr == tail.sep.ptr) { - if (next.sep.ptr == NULL) { - return NULL; + unsigned int value; + int success = 0; + pointer head; + pointer tail; + pointer next; + + while (!success) { + head = atomic_load_explicit(&q->head, acquire); + tail = atomic_load_explicit(&q->tail, relaxed); + next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, acquire); + if (atomic_load_explicit(&q->head, relaxed) == head) { + if (get_ptr(head) == get_ptr(tail)) { + + /* Check for uninitialized 'next' */ + MODEL_ASSERT(get_ptr(next) != POISON_IDX); + + if (get_ptr(next) == 0) { // NULL + return 0; // NULL } - cas(&smp->tail, - tail.con, - MAKE_LONG(next.sep.ptr, tail.sep.count+1)); + atomic_compare_exchange_strong_explicit(&q->tail, + &tail, + MAKE_POINTER(get_ptr(next), get_count(tail) + 1), + release, release); thrd_yield(); } else { - value = smp->nodes[next.sep.ptr].value; - success = cas(&smp->head, - head.con, - MAKE_LONG(next.sep.ptr, head.sep.count+1)); - if (success == FALSE) { + value = load_32(&q->nodes[get_ptr(next)].value); + success = atomic_compare_exchange_strong_explicit(&q->head, + &head, + MAKE_POINTER(get_ptr(next), get_count(head) + 1), + release, release); + if (!success) thrd_yield(); - } } } } - reclaim(head.sep.ptr); + reclaim(get_ptr(head)); return value; }