| 1 |
/* |
| 2 |
* Copyright 2012 Hendrik Donner |
| 3 |
* Copyright 2012-2015 Samy Al Bahra. |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 25 |
* SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <ck_hp.h> |
| 29 |
#include <ck_hp_fifo.h> |
| 30 |
#include <ck_pr.h> |
| 31 |
#include <stdbool.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <pthread.h> |
| 35 |
#include <sys/time.h> |
| 36 |
#include <assert.h> |
| 37 |
#include "../../common.h" |
| 38 |
|
| 39 |
/* FIFO queue */ |
| 40 |
static ck_hp_fifo_t fifo; |
| 41 |
|
| 42 |
/* Hazard pointer global */ |
| 43 |
static ck_hp_t fifo_hp; |
| 44 |
|
| 45 |
/* thread local element count */ |
| 46 |
static unsigned long *count; |
| 47 |
|
| 48 |
static unsigned long thread_count; |
| 49 |
|
| 50 |
static unsigned int start_barrier; |
| 51 |
static unsigned int end_barrier; |
| 52 |
|
| 53 |
/* destructor for FIFO queue */ |
| 54 |
static void |
| 55 |
destructor(void *p) |
| 56 |
{ |
| 57 |
|
| 58 |
free(p); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
/* entry struct for FIFO queue entries */ |
| 63 |
struct entry { |
| 64 |
unsigned long value; |
| 65 |
}; |
| 66 |
|
| 67 |
/* function for thread */ |
| 68 |
static void * |
| 69 |
queue_50_50(void *elements) |
| 70 |
{ |
| 71 |
struct entry *entry; |
| 72 |
ck_hp_fifo_entry_t *fifo_entry; |
| 73 |
ck_hp_record_t *record; |
| 74 |
void *slots; |
| 75 |
unsigned long j, element_count = *(unsigned long *)elements; |
| 76 |
unsigned int seed; |
| 77 |
|
| 78 |
record = malloc(sizeof(ck_hp_record_t)); |
| 79 |
assert(record); |
| 80 |
|
| 81 |
slots = malloc(CK_HP_FIFO_SLOTS_SIZE); |
| 82 |
assert(slots); |
| 83 |
|
| 84 |
/* different seed for each thread */ |
| 85 |
seed = 1337; /*(unsigned int) pthread_self(); */ |
| 86 |
|
| 87 |
/* |
| 88 |
* This subscribes the thread to the fifo_hp state using the thread-owned |
| 89 |
* record. |
| 90 |
* FIFO queue needs 2 hazard pointers. |
| 91 |
*/ |
| 92 |
ck_hp_register(&fifo_hp, record, slots); |
| 93 |
|
| 94 |
/* start barrier */ |
| 95 |
ck_pr_inc_uint(&start_barrier); |
| 96 |
while (ck_pr_load_uint(&start_barrier) < thread_count + 1) |
| 97 |
ck_pr_stall(); |
| 98 |
|
| 99 |
/* 50/50 enqueue-dequeue */ |
| 100 |
for(j = 0; j < element_count; j++) { |
| 101 |
/* rand_r with thread local state should be thread safe */ |
| 102 |
if( 50 < (1+(int) (100.0*common_rand_r(&seed)/(RAND_MAX+1.0)))) { |
| 103 |
/* This is the container for the enqueued data. */ |
| 104 |
fifo_entry = malloc(sizeof(ck_hp_fifo_entry_t)); |
| 105 |
|
| 106 |
if (fifo_entry == NULL) { |
| 107 |
exit(EXIT_FAILURE); |
| 108 |
} |
| 109 |
|
| 110 |
/* This is the data. */ |
| 111 |
entry = malloc(sizeof(struct entry)); |
| 112 |
if (entry != NULL) { |
| 113 |
entry->value = j; |
| 114 |
} |
| 115 |
|
| 116 |
/* |
| 117 |
* Enqueue the value of the pointer entry into FIFO queue using the |
| 118 |
* container fifo_entry. |
| 119 |
*/ |
| 120 |
ck_hp_fifo_enqueue_mpmc(record, &fifo, fifo_entry, entry); |
| 121 |
} else { |
| 122 |
/* |
| 123 |
* ck_hp_fifo_dequeue_mpmc will return a pointer to the first unused node and store |
| 124 |
* the value of the first pointer in the FIFO queue in entry. |
| 125 |
*/ |
| 126 |
fifo_entry = ck_hp_fifo_dequeue_mpmc(record, &fifo, &entry); |
| 127 |
if (fifo_entry != NULL) { |
| 128 |
/* |
| 129 |
* Safely reclaim memory associated with fifo_entry. |
| 130 |
* This inserts garbage into a local list. Once the list (plist) reaches |
| 131 |
* a length of 100, ck_hp_free will attempt to reclaim all references |
| 132 |
* to objects on the list. |
| 133 |
*/ |
| 134 |
ck_hp_free(record, &fifo_entry->hazard, fifo_entry, fifo_entry); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/* end barrier */ |
| 140 |
ck_pr_inc_uint(&end_barrier); |
| 141 |
while (ck_pr_load_uint(&end_barrier) < thread_count + 1) |
| 142 |
ck_pr_stall(); |
| 143 |
|
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
int |
| 148 |
main(int argc, char** argv) |
| 149 |
{ |
| 150 |
ck_hp_fifo_entry_t *stub; |
| 151 |
unsigned long element_count, i; |
| 152 |
pthread_t *thr; |
| 153 |
|
| 154 |
if (argc != 3) { |
| 155 |
ck_error("Usage: cktest <thread_count> <element_count>\n"); |
| 156 |
} |
| 157 |
|
| 158 |
/* Get element count from argument */ |
| 159 |
element_count = atoi(argv[2]); |
| 160 |
|
| 161 |
/* Get element count from argument */ |
| 162 |
thread_count = atoi(argv[1]); |
| 163 |
|
| 164 |
/* pthread handles */ |
| 165 |
thr = malloc(sizeof(pthread_t) * thread_count); |
| 166 |
|
| 167 |
/* array for local operation count */ |
| 168 |
count = malloc(sizeof(unsigned long *) * thread_count); |
| 169 |
|
| 170 |
/* |
| 171 |
* Initialize global hazard pointer safe memory reclamation to execute free() |
| 172 |
* when a fifo_entry is safe to be deleted. |
| 173 |
* Hazard pointer scan routine will be called when the thread local intern plist's |
| 174 |
* size exceed 100 entries. |
| 175 |
*/ |
| 176 |
|
| 177 |
/* FIFO queue needs 2 hazard pointers */ |
| 178 |
ck_hp_init(&fifo_hp, CK_HP_FIFO_SLOTS_COUNT, 100, destructor); |
| 179 |
|
| 180 |
/* The FIFO requires one stub entry on initialization. */ |
| 181 |
stub = malloc(sizeof(ck_hp_fifo_entry_t)); |
| 182 |
|
| 183 |
/* Behavior is undefined if stub is NULL. */ |
| 184 |
if (stub == NULL) { |
| 185 |
exit(EXIT_FAILURE); |
| 186 |
} |
| 187 |
|
| 188 |
/* This is called once to initialize the fifo. */ |
| 189 |
ck_hp_fifo_init(&fifo, stub); |
| 190 |
|
| 191 |
/* Create threads */ |
| 192 |
for (i = 0; i < thread_count; i++) { |
| 193 |
count[i] = (element_count + i) / thread_count; |
| 194 |
if (pthread_create(&thr[i], NULL, queue_50_50, (void *) &count[i]) != 0) { |
| 195 |
exit(EXIT_FAILURE); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/* start barrier */ |
| 200 |
ck_pr_inc_uint(&start_barrier); |
| 201 |
while (ck_pr_load_uint(&start_barrier) < thread_count + 1); |
| 202 |
|
| 203 |
/* end barrier */ |
| 204 |
ck_pr_inc_uint(&end_barrier); |
| 205 |
while (ck_pr_load_uint(&end_barrier) < thread_count + 1); |
| 206 |
|
| 207 |
/* Join threads */ |
| 208 |
for (i = 0; i < thread_count; i++) |
| 209 |
pthread_join(thr[i], NULL); |
| 210 |
|
| 211 |
return 0; |
| 212 |
} |
| 213 |
|