| 1 |
/* |
| 2 |
* Copyright 2015 John Esmet. |
| 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 |
#include <assert.h> |
| 28 |
#include <pthread.h> |
| 29 |
#include <stdbool.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <time.h> |
| 34 |
#include <unistd.h> |
| 35 |
|
| 36 |
#include <ck_epoch.h> |
| 37 |
|
| 38 |
#include "../../common.h" |
| 39 |
|
| 40 |
static ck_epoch_t epc; |
| 41 |
static ck_epoch_record_t record, record2; |
| 42 |
static unsigned int cleanup_calls; |
| 43 |
|
| 44 |
static void |
| 45 |
setup_test(void) |
| 46 |
{ |
| 47 |
|
| 48 |
ck_epoch_init(&epc); |
| 49 |
ck_epoch_register(&epc, &record, NULL); |
| 50 |
ck_epoch_register(&epc, &record2, NULL); |
| 51 |
cleanup_calls = 0; |
| 52 |
|
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
teardown_test(void) |
| 58 |
{ |
| 59 |
|
| 60 |
memset(&epc, 0, sizeof(ck_epoch_t)); |
| 61 |
ck_epoch_unregister(&record); |
| 62 |
memset(&record, 0, sizeof(ck_epoch_record_t)); |
| 63 |
memset(&record2, 0, sizeof(ck_epoch_record_t)); |
| 64 |
cleanup_calls = 0; |
| 65 |
|
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
cleanup(ck_epoch_entry_t *e) |
| 71 |
{ |
| 72 |
(void) e; |
| 73 |
|
| 74 |
cleanup_calls++; |
| 75 |
|
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
test_simple_read_section(void) |
| 81 |
{ |
| 82 |
ck_epoch_entry_t entry; |
| 83 |
ck_epoch_section_t section; |
| 84 |
|
| 85 |
memset(&entry, 0, sizeof(ck_epoch_entry_t)); |
| 86 |
setup_test(); |
| 87 |
|
| 88 |
ck_epoch_begin(&record, §ion); |
| 89 |
ck_epoch_call(&record, &entry, cleanup); |
| 90 |
assert(cleanup_calls == 0); |
| 91 |
if (ck_epoch_end(&record, §ion) == false) |
| 92 |
ck_error("expected no more sections"); |
| 93 |
ck_epoch_barrier(&record); |
| 94 |
assert(cleanup_calls == 1); |
| 95 |
|
| 96 |
teardown_test(); |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
test_nested_read_section(void) |
| 102 |
{ |
| 103 |
ck_epoch_entry_t entry1, entry2; |
| 104 |
ck_epoch_section_t section1, section2; |
| 105 |
|
| 106 |
memset(&entry1, 0, sizeof(ck_epoch_entry_t)); |
| 107 |
memset(&entry2, 0, sizeof(ck_epoch_entry_t)); |
| 108 |
setup_test(); |
| 109 |
|
| 110 |
ck_epoch_begin(&record, §ion1); |
| 111 |
ck_epoch_call(&record, &entry1, cleanup); |
| 112 |
assert(cleanup_calls == 0); |
| 113 |
|
| 114 |
ck_epoch_begin(&record, §ion2); |
| 115 |
ck_epoch_call(&record, &entry2, cleanup); |
| 116 |
assert(cleanup_calls == 0); |
| 117 |
|
| 118 |
ck_epoch_end(&record, §ion2); |
| 119 |
assert(cleanup_calls == 0); |
| 120 |
|
| 121 |
ck_epoch_end(&record, §ion1); |
| 122 |
assert(cleanup_calls == 0); |
| 123 |
|
| 124 |
ck_epoch_barrier(&record); |
| 125 |
assert(cleanup_calls == 2); |
| 126 |
|
| 127 |
teardown_test(); |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
struct obj { |
| 132 |
ck_epoch_entry_t entry; |
| 133 |
unsigned int destroyed; |
| 134 |
}; |
| 135 |
|
| 136 |
static void * |
| 137 |
barrier_work(void *arg) |
| 138 |
{ |
| 139 |
unsigned int *run; |
| 140 |
|
| 141 |
run = (unsigned int *)arg; |
| 142 |
while (ck_pr_load_uint(run) != 0) { |
| 143 |
/* |
| 144 |
* Need to use record2, as record is local |
| 145 |
* to the test thread. |
| 146 |
*/ |
| 147 |
ck_epoch_barrier(&record2); |
| 148 |
usleep(5 * 1000); |
| 149 |
} |
| 150 |
|
| 151 |
return NULL; |
| 152 |
} |
| 153 |
|
| 154 |
static void * |
| 155 |
reader_work(void *arg) |
| 156 |
{ |
| 157 |
ck_epoch_record_t local_record; |
| 158 |
ck_epoch_section_t section; |
| 159 |
struct obj *o; |
| 160 |
|
| 161 |
ck_epoch_register(&epc, &local_record, NULL); |
| 162 |
|
| 163 |
o = (struct obj *)arg; |
| 164 |
|
| 165 |
/* |
| 166 |
* Begin a read section. The calling thread has an open read section, |
| 167 |
* so the object should not be destroyed for the lifetime of this |
| 168 |
* thread. |
| 169 |
*/ |
| 170 |
ck_epoch_begin(&local_record, §ion); |
| 171 |
usleep((common_rand() % 100) * 1000); |
| 172 |
assert(ck_pr_load_uint(&o->destroyed) == 0); |
| 173 |
ck_epoch_end(&local_record, §ion); |
| 174 |
|
| 175 |
ck_epoch_unregister(&local_record); |
| 176 |
|
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
obj_destroy(ck_epoch_entry_t *e) |
| 182 |
{ |
| 183 |
struct obj *o; |
| 184 |
|
| 185 |
o = (struct obj *)e; |
| 186 |
ck_pr_fas_uint(&o->destroyed, 1); |
| 187 |
|
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
test_single_reader_with_barrier_thread(void) |
| 193 |
{ |
| 194 |
const int num_sections = 10; |
| 195 |
struct obj o; |
| 196 |
unsigned int run; |
| 197 |
pthread_t thread; |
| 198 |
ck_epoch_section_t sections[num_sections]; |
| 199 |
int shuffled[num_sections]; |
| 200 |
|
| 201 |
run = 1; |
| 202 |
memset(&o, 0, sizeof(struct obj)); |
| 203 |
common_srand(time(NULL)); |
| 204 |
setup_test(); |
| 205 |
|
| 206 |
if (pthread_create(&thread, NULL, barrier_work, &run) != 0) { |
| 207 |
abort(); |
| 208 |
} |
| 209 |
|
| 210 |
/* Start a bunch of sections. */ |
| 211 |
for (int i = 0; i < num_sections; i++) { |
| 212 |
ck_epoch_begin(&record, §ions[i]); |
| 213 |
shuffled[i] = i; |
| 214 |
if (i == num_sections / 2) { |
| 215 |
usleep(1 * 1000); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/* Generate a shuffle. */ |
| 220 |
for (int i = num_sections - 1; i >= 0; i--) { |
| 221 |
int k = common_rand() % (i + 1); |
| 222 |
int tmp = shuffled[k]; |
| 223 |
shuffled[k] = shuffled[i]; |
| 224 |
shuffled[i] = tmp; |
| 225 |
} |
| 226 |
|
| 227 |
ck_epoch_call(&record, &o.entry, obj_destroy); |
| 228 |
|
| 229 |
/* Close the sections in shuffle-order. */ |
| 230 |
for (int i = 0; i < num_sections; i++) { |
| 231 |
ck_epoch_end(&record, §ions[shuffled[i]]); |
| 232 |
if (i != num_sections - 1) { |
| 233 |
assert(ck_pr_load_uint(&o.destroyed) == 0); |
| 234 |
usleep(3 * 1000); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
ck_pr_store_uint(&run, 0); |
| 239 |
if (pthread_join(thread, NULL) != 0) { |
| 240 |
abort(); |
| 241 |
} |
| 242 |
|
| 243 |
ck_epoch_barrier(&record); |
| 244 |
assert(ck_pr_load_uint(&o.destroyed) == 1); |
| 245 |
|
| 246 |
teardown_test(); |
| 247 |
|
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
static void |
| 252 |
test_multiple_readers_with_barrier_thread(void) |
| 253 |
{ |
| 254 |
const int num_readers = 10; |
| 255 |
struct obj o; |
| 256 |
unsigned int run; |
| 257 |
ck_epoch_section_t section; |
| 258 |
pthread_t threads[num_readers + 1]; |
| 259 |
|
| 260 |
run = 1; |
| 261 |
memset(&o, 0, sizeof(struct obj)); |
| 262 |
memset(§ion, 0, sizeof(ck_epoch_section_t)); |
| 263 |
common_srand(time(NULL)); |
| 264 |
setup_test(); |
| 265 |
|
| 266 |
/* Create a thread to call barrier() while we create reader threads. |
| 267 |
* Each barrier will attempt to move the global epoch forward so |
| 268 |
* it will make the read section code coverage more interesting. */ |
| 269 |
if (pthread_create(&threads[num_readers], NULL, |
| 270 |
barrier_work, &run) != 0) { |
| 271 |
abort(); |
| 272 |
} |
| 273 |
|
| 274 |
ck_epoch_begin(&record, §ion); |
| 275 |
ck_epoch_call(&record, &o.entry, obj_destroy); |
| 276 |
|
| 277 |
for (int i = 0; i < num_readers; i++) { |
| 278 |
if (pthread_create(&threads[i], NULL, reader_work, &o) != 0) { |
| 279 |
abort(); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
ck_epoch_end(&record, §ion); |
| 284 |
|
| 285 |
ck_pr_store_uint(&run, 0); |
| 286 |
if (pthread_join(threads[num_readers], NULL) != 0) { |
| 287 |
abort(); |
| 288 |
} |
| 289 |
|
| 290 |
/* After the barrier, the object should be destroyed and readers |
| 291 |
* should return. */ |
| 292 |
for (int i = 0; i < num_readers; i++) { |
| 293 |
if (pthread_join(threads[i], NULL) != 0) { |
| 294 |
abort(); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
teardown_test(); |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
int |
| 303 |
main(void) |
| 304 |
{ |
| 305 |
|
| 306 |
test_simple_read_section(); |
| 307 |
test_nested_read_section(); |
| 308 |
test_single_reader_with_barrier_thread(); |
| 309 |
test_multiple_readers_with_barrier_thread(); |
| 310 |
|
| 311 |
return 0; |
| 312 |
} |