| 1 |
/* |
| 2 |
* Copyright 2010-2015 Samy Al Bahra. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 |
* SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/* |
| 28 |
* This is a unit test similar to the implementation in John Dybnis's nbds |
| 29 |
* test. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <assert.h> |
| 33 |
#include <errno.h> |
| 34 |
#include <inttypes.h> |
| 35 |
#include <pthread.h> |
| 36 |
#include <stdint.h> |
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <strings.h> |
| 40 |
#include <unistd.h> |
| 41 |
#include <sys/time.h> |
| 42 |
|
| 43 |
#include <ck_pr.h> |
| 44 |
|
| 45 |
#include <ck_backoff.h> |
| 46 |
#include <ck_cc.h> |
| 47 |
#include <ck_pr.h> |
| 48 |
#include <stdbool.h> |
| 49 |
#include <stddef.h> |
| 50 |
#include <ck_hp.h> |
| 51 |
|
| 52 |
#include "../../common.h" |
| 53 |
|
| 54 |
#define STACK_CONTAINER(T, M, N) CK_CC_CONTAINER(stack_entry_t, T, M, N) |
| 55 |
|
| 56 |
struct stack_entry { |
| 57 |
struct stack_entry *next; |
| 58 |
} CK_CC_ALIGN(8); |
| 59 |
typedef struct stack_entry stack_entry_t; |
| 60 |
|
| 61 |
struct stack { |
| 62 |
struct stack_entry *head; |
| 63 |
char *generation; |
| 64 |
} CK_CC_PACKED CK_CC_ALIGN(16); |
| 65 |
typedef struct stack hp_stack_t; |
| 66 |
|
| 67 |
static unsigned int threshold; |
| 68 |
static unsigned int n_threads; |
| 69 |
static unsigned int barrier; |
| 70 |
static unsigned int e_barrier; |
| 71 |
static unsigned int global_tid; |
| 72 |
static unsigned int pops; |
| 73 |
static unsigned int pushs; |
| 74 |
|
| 75 |
#ifndef PAIRS |
| 76 |
#define PAIRS 1000000 |
| 77 |
#endif |
| 78 |
|
| 79 |
struct node { |
| 80 |
unsigned int value; |
| 81 |
ck_hp_hazard_t hazard; |
| 82 |
stack_entry_t stack_entry; |
| 83 |
}; |
| 84 |
hp_stack_t stack = {NULL, NULL}; |
| 85 |
ck_hp_t stack_hp; |
| 86 |
|
| 87 |
STACK_CONTAINER(struct node, stack_entry, stack_container) |
| 88 |
static struct affinity a; |
| 89 |
|
| 90 |
/* |
| 91 |
* Stack producer operation safe for multiple unique producers and multiple consumers. |
| 92 |
*/ |
| 93 |
CK_CC_INLINE static void |
| 94 |
stack_push_mpmc(struct stack *target, struct stack_entry *entry) |
| 95 |
{ |
| 96 |
struct stack_entry *lstack; |
| 97 |
ck_backoff_t backoff = CK_BACKOFF_INITIALIZER; |
| 98 |
|
| 99 |
lstack = ck_pr_load_ptr(&target->head); |
| 100 |
ck_pr_store_ptr(&entry->next, lstack); |
| 101 |
ck_pr_fence_store(); |
| 102 |
|
| 103 |
while (ck_pr_cas_ptr_value(&target->head, lstack, entry, &lstack) == false) { |
| 104 |
ck_pr_store_ptr(&entry->next, lstack); |
| 105 |
ck_pr_fence_store(); |
| 106 |
ck_backoff_eb(&backoff); |
| 107 |
} |
| 108 |
|
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
/* |
| 113 |
* Stack consumer operation safe for multiple unique producers and multiple consumers. |
| 114 |
*/ |
| 115 |
CK_CC_INLINE static struct stack_entry * |
| 116 |
stack_pop_mpmc(ck_hp_record_t *record, struct stack *target) |
| 117 |
{ |
| 118 |
struct stack_entry *entry; |
| 119 |
ck_backoff_t backoff = CK_BACKOFF_INITIALIZER; |
| 120 |
|
| 121 |
do { |
| 122 |
entry = ck_pr_load_ptr(&target->head); |
| 123 |
if (entry == NULL) |
| 124 |
return (NULL); |
| 125 |
|
| 126 |
ck_hp_set_fence(record, 0, entry); |
| 127 |
} while (entry != ck_pr_load_ptr(&target->head)); |
| 128 |
|
| 129 |
while (ck_pr_cas_ptr_value(&target->head, entry, entry->next, &entry) == false) { |
| 130 |
if (ck_pr_load_ptr(&entry) == NULL) |
| 131 |
break; |
| 132 |
|
| 133 |
ck_hp_set_fence(record, 0, entry); |
| 134 |
if (entry != ck_pr_load_ptr(&target->head)) |
| 135 |
continue; |
| 136 |
|
| 137 |
ck_backoff_eb(&backoff); |
| 138 |
} |
| 139 |
|
| 140 |
return (entry); |
| 141 |
} |
| 142 |
|
| 143 |
static void * |
| 144 |
thread(void *unused CK_CC_UNUSED) |
| 145 |
{ |
| 146 |
struct node *entry, *e; |
| 147 |
unsigned int i; |
| 148 |
ck_hp_record_t record; |
| 149 |
void **pointers; |
| 150 |
stack_entry_t *s; |
| 151 |
unsigned int tid = ck_pr_faa_uint(&global_tid, 1) + 1; |
| 152 |
unsigned int r = (unsigned int)(tid + 1) * 0x5bd1e995; |
| 153 |
|
| 154 |
unused = NULL; |
| 155 |
pointers = malloc(sizeof(void *)); |
| 156 |
ck_hp_register(&stack_hp, &record, pointers); |
| 157 |
|
| 158 |
if (aff_iterate(&a)) { |
| 159 |
perror("ERROR: failed to affine thread"); |
| 160 |
exit(EXIT_FAILURE); |
| 161 |
} |
| 162 |
|
| 163 |
ck_pr_inc_uint(&barrier); |
| 164 |
while (ck_pr_load_uint(&barrier) < n_threads) |
| 165 |
ck_pr_stall(); |
| 166 |
|
| 167 |
for (i = 0; i < PAIRS; i++) { |
| 168 |
r ^= r << 6; r ^= r >> 21; r ^= r << 7; |
| 169 |
|
| 170 |
if (r & 0x1000) { |
| 171 |
entry = malloc(sizeof(struct node)); |
| 172 |
assert(entry); |
| 173 |
stack_push_mpmc(&stack, &entry->stack_entry); |
| 174 |
ck_pr_inc_uint(&pushs); |
| 175 |
} else { |
| 176 |
s = stack_pop_mpmc(&record, &stack); |
| 177 |
if (s == NULL) |
| 178 |
continue; |
| 179 |
|
| 180 |
e = stack_container(s); |
| 181 |
ck_hp_free(&record, &e->hazard, e, s); |
| 182 |
ck_pr_inc_uint(&pops); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
ck_pr_inc_uint(&e_barrier); |
| 187 |
while (ck_pr_load_uint(&e_barrier) < n_threads); |
| 188 |
|
| 189 |
return (NULL); |
| 190 |
} |
| 191 |
|
| 192 |
static void |
| 193 |
destructor(void *p) |
| 194 |
{ |
| 195 |
free(p); |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
int |
| 200 |
main(int argc, char *argv[]) |
| 201 |
{ |
| 202 |
unsigned int i; |
| 203 |
pthread_t *threads; |
| 204 |
|
| 205 |
if (argc != 4) { |
| 206 |
ck_error("Usage: stack <threads> <threshold> <delta>\n"); |
| 207 |
} |
| 208 |
|
| 209 |
n_threads = atoi(argv[1]); |
| 210 |
threshold = atoi(argv[2]); |
| 211 |
a.delta = atoi(argv[3]); |
| 212 |
a.request = 0; |
| 213 |
|
| 214 |
threads = malloc(sizeof(pthread_t) * n_threads); |
| 215 |
|
| 216 |
ck_hp_init(&stack_hp, 1, threshold, destructor); |
| 217 |
|
| 218 |
for (i = 0; i < n_threads; i++) |
| 219 |
pthread_create(threads + i, NULL, thread, NULL); |
| 220 |
|
| 221 |
for (i = 0; i < n_threads; i++) |
| 222 |
pthread_join(threads[i], NULL); |
| 223 |
|
| 224 |
fprintf(stderr, "Push: %u\nPop: %u\n", pushs, pops); |
| 225 |
return (0); |
| 226 |
} |