regressions/ck_hp_fifo_donner: This was submitted from a Windows editor.

Switch to using UNIX EOL.
ck_pring
Samy Al Bahra 12 years ago
parent c6ff128510
commit 9152cc7cea

@ -1,213 +1,213 @@
/* /*
* Copyright 2012 Hendrik Donner * Copyright 2012 Hendrik Donner
* Copyright 2012-2013 Samy Al Bahra. * Copyright 2012-2013 Samy Al Bahra.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * 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 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include <ck_hp.h> #include <ck_hp.h>
#include <ck_hp_fifo.h> #include <ck_hp_fifo.h>
#include <ck_pr.h> #include <ck_pr.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <pthread.h> #include <pthread.h>
#include <sys/time.h> #include <sys/time.h>
#include <assert.h> #include <assert.h>
#include "../../common.h" #include "../../common.h"
/* FIFO queue */ /* FIFO queue */
static ck_hp_fifo_t fifo; static ck_hp_fifo_t fifo;
/* Hazard pointer global */ /* Hazard pointer global */
static ck_hp_t fifo_hp; static ck_hp_t fifo_hp;
/* thread local element count */ /* thread local element count */
static unsigned long *count; static unsigned long *count;
static unsigned long thread_count; static unsigned long thread_count;
static unsigned int start_barrier; static unsigned int start_barrier;
static unsigned int end_barrier; static unsigned int end_barrier;
/* destructor for FIFO queue */ /* destructor for FIFO queue */
static void static void
destructor(void *p) destructor(void *p)
{ {
free(p); free(p);
return; return;
} }
/* entry struct for FIFO queue entries */ /* entry struct for FIFO queue entries */
struct entry { struct entry {
unsigned long value; unsigned long value;
}; };
/* function for thread */ /* function for thread */
static void * static void *
queue_50_50(void *elements) queue_50_50(void *elements)
{ {
struct entry *entry; struct entry *entry;
ck_hp_fifo_entry_t *fifo_entry; ck_hp_fifo_entry_t *fifo_entry;
ck_hp_record_t *record; ck_hp_record_t *record;
void *slots; void *slots;
unsigned long j, element_count = *(unsigned long *)elements; unsigned long j, element_count = *(unsigned long *)elements;
unsigned int seed; unsigned int seed;
record = malloc(sizeof(ck_hp_record_t)); record = malloc(sizeof(ck_hp_record_t));
assert(record); assert(record);
slots = malloc(CK_HP_FIFO_SLOTS_SIZE); slots = malloc(CK_HP_FIFO_SLOTS_SIZE);
assert(slots); assert(slots);
/* different seed for each thread */ /* different seed for each thread */
seed = 1337; /*(unsigned int) pthread_self(); */ seed = 1337; /*(unsigned int) pthread_self(); */
/* /*
* This subscribes the thread to the fifo_hp state using the thread-owned * This subscribes the thread to the fifo_hp state using the thread-owned
* record. * record.
* FIFO queue needs 2 hazard pointers. * FIFO queue needs 2 hazard pointers.
*/ */
ck_hp_register(&fifo_hp, record, slots); ck_hp_register(&fifo_hp, record, slots);
/* start barrier */ /* start barrier */
ck_pr_inc_uint(&start_barrier); ck_pr_inc_uint(&start_barrier);
while (ck_pr_load_uint(&start_barrier) < thread_count + 1) while (ck_pr_load_uint(&start_barrier) < thread_count + 1)
ck_pr_stall(); ck_pr_stall();
/* 50/50 enqueue-dequeue */ /* 50/50 enqueue-dequeue */
for(j = 0; j < element_count; j++) { for(j = 0; j < element_count; j++) {
/* rand_r with thread local state should be thread safe */ /* rand_r with thread local state should be thread safe */
if( 50 < (1+(int) (100.0*rand_r(&seed)/(RAND_MAX+1.0)))) { if( 50 < (1+(int) (100.0*rand_r(&seed)/(RAND_MAX+1.0)))) {
/* This is the container for the enqueued data. */ /* This is the container for the enqueued data. */
fifo_entry = malloc(sizeof(ck_hp_fifo_entry_t)); fifo_entry = malloc(sizeof(ck_hp_fifo_entry_t));
if (fifo_entry == NULL) { if (fifo_entry == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* This is the data. */ /* This is the data. */
entry = malloc(sizeof(struct entry)); entry = malloc(sizeof(struct entry));
if (entry != NULL) { if (entry != NULL) {
entry->value = j; entry->value = j;
} }
/* /*
* Enqueue the value of the pointer entry into FIFO queue using the * Enqueue the value of the pointer entry into FIFO queue using the
* container fifo_entry. * container fifo_entry.
*/ */
ck_hp_fifo_enqueue_mpmc(record, &fifo, fifo_entry, entry); ck_hp_fifo_enqueue_mpmc(record, &fifo, fifo_entry, entry);
} else { } else {
/* /*
* ck_hp_fifo_dequeue_mpmc will return a pointer to the first unused node and store * ck_hp_fifo_dequeue_mpmc will return a pointer to the first unused node and store
* the value of the first pointer in the FIFO queue in entry. * the value of the first pointer in the FIFO queue in entry.
*/ */
fifo_entry = ck_hp_fifo_dequeue_mpmc(record, &fifo, &entry); fifo_entry = ck_hp_fifo_dequeue_mpmc(record, &fifo, &entry);
if (fifo_entry != NULL) { if (fifo_entry != NULL) {
/* /*
* Safely reclaim memory associated with fifo_entry. * Safely reclaim memory associated with fifo_entry.
* This inserts garbage into a local list. Once the list (plist) reaches * This inserts garbage into a local list. Once the list (plist) reaches
* a length of 100, ck_hp_free will attempt to reclaim all references * a length of 100, ck_hp_free will attempt to reclaim all references
* to objects on the list. * to objects on the list.
*/ */
ck_hp_free(record, &fifo_entry->hazard, fifo_entry, fifo_entry); ck_hp_free(record, &fifo_entry->hazard, fifo_entry, fifo_entry);
} }
} }
} }
/* end barrier */ /* end barrier */
ck_pr_inc_uint(&end_barrier); ck_pr_inc_uint(&end_barrier);
while (ck_pr_load_uint(&end_barrier) < thread_count + 1) while (ck_pr_load_uint(&end_barrier) < thread_count + 1)
ck_pr_stall(); ck_pr_stall();
return NULL; return NULL;
} }
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
ck_hp_fifo_entry_t *stub; ck_hp_fifo_entry_t *stub;
unsigned long element_count, i; unsigned long element_count, i;
pthread_t *thr; pthread_t *thr;
if (argc != 3) { if (argc != 3) {
ck_error("Usage: cktest <thread_count> <element_count>\n"); ck_error("Usage: cktest <thread_count> <element_count>\n");
} }
/* Get element count from argument */ /* Get element count from argument */
element_count = atoi(argv[2]); element_count = atoi(argv[2]);
/* Get element count from argument */ /* Get element count from argument */
thread_count = atoi(argv[1]); thread_count = atoi(argv[1]);
/* pthread handles */ /* pthread handles */
thr = malloc(sizeof(pthread_t) * thread_count); thr = malloc(sizeof(pthread_t) * thread_count);
/* array for local operation count */ /* array for local operation count */
count = malloc(sizeof(unsigned long *) * thread_count); count = malloc(sizeof(unsigned long *) * thread_count);
/* /*
* Initialize global hazard pointer safe memory reclamation to execute free() * Initialize global hazard pointer safe memory reclamation to execute free()
* when a fifo_entry is safe to be deleted. * when a fifo_entry is safe to be deleted.
* Hazard pointer scan routine will be called when the thread local intern plist's * Hazard pointer scan routine will be called when the thread local intern plist's
* size exceed 100 entries. * size exceed 100 entries.
*/ */
/* FIFO queue needs 2 hazard pointers */ /* FIFO queue needs 2 hazard pointers */
ck_hp_init(&fifo_hp, CK_HP_FIFO_SLOTS_COUNT, 100, destructor); ck_hp_init(&fifo_hp, CK_HP_FIFO_SLOTS_COUNT, 100, destructor);
/* The FIFO requires one stub entry on initialization. */ /* The FIFO requires one stub entry on initialization. */
stub = malloc(sizeof(ck_hp_fifo_entry_t)); stub = malloc(sizeof(ck_hp_fifo_entry_t));
/* Behavior is undefined if stub is NULL. */ /* Behavior is undefined if stub is NULL. */
if (stub == NULL) { if (stub == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* This is called once to initialize the fifo. */ /* This is called once to initialize the fifo. */
ck_hp_fifo_init(&fifo, stub); ck_hp_fifo_init(&fifo, stub);
/* Create threads */ /* Create threads */
for (i = 0; i < thread_count; i++) { for (i = 0; i < thread_count; i++) {
count[i] = (element_count + i) / thread_count; count[i] = (element_count + i) / thread_count;
if (pthread_create(&thr[i], NULL, queue_50_50, (void *) &count[i]) != 0) { if (pthread_create(&thr[i], NULL, queue_50_50, (void *) &count[i]) != 0) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
/* start barrier */ /* start barrier */
ck_pr_inc_uint(&start_barrier); ck_pr_inc_uint(&start_barrier);
while (ck_pr_load_uint(&start_barrier) < thread_count + 1); while (ck_pr_load_uint(&start_barrier) < thread_count + 1);
/* end barrier */ /* end barrier */
ck_pr_inc_uint(&end_barrier); ck_pr_inc_uint(&end_barrier);
while (ck_pr_load_uint(&end_barrier) < thread_count + 1); while (ck_pr_load_uint(&end_barrier) < thread_count + 1);
/* Join threads */ /* Join threads */
for (i = 0; i < thread_count; i++) for (i = 0; i < thread_count; i++)
pthread_join(thr[i], NULL); pthread_join(thr[i], NULL);
return 0; return 0;
} }

Loading…
Cancel
Save