| 1 |
/* |
| 2 |
* Copyright 2011-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 |
#include <errno.h> |
| 28 |
#include <inttypes.h> |
| 29 |
#include <pthread.h> |
| 30 |
#include <math.h> |
| 31 |
#include <stdint.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <strings.h> |
| 35 |
#include <unistd.h> |
| 36 |
#include <sys/time.h> |
| 37 |
|
| 38 |
#include <ck_cc.h> |
| 39 |
#include <ck_pr.h> |
| 40 |
#include <ck_spinlock.h> |
| 41 |
|
| 42 |
#include "../../common.h" |
| 43 |
|
| 44 |
#ifndef ITERATE |
| 45 |
#define ITERATE 1000000 |
| 46 |
#endif |
| 47 |
|
| 48 |
struct block { |
| 49 |
unsigned int tid; |
| 50 |
}; |
| 51 |
|
| 52 |
static struct affinity a; |
| 53 |
static unsigned int locked = 0; |
| 54 |
static uint64_t nthr; |
| 55 |
|
| 56 |
LOCK_DEFINE; |
| 57 |
|
| 58 |
static void * |
| 59 |
thread(void *null CK_CC_UNUSED) |
| 60 |
{ |
| 61 |
#ifdef LOCK_STATE |
| 62 |
LOCK_STATE; |
| 63 |
#endif |
| 64 |
unsigned int i = ITERATE; |
| 65 |
unsigned int j; |
| 66 |
unsigned int core; |
| 67 |
|
| 68 |
if (aff_iterate_core(&a, &core)) { |
| 69 |
perror("ERROR: Could not affine thread"); |
| 70 |
exit(EXIT_FAILURE); |
| 71 |
} |
| 72 |
|
| 73 |
while (i--) { |
| 74 |
#ifdef TRYLOCK |
| 75 |
if (i & 1) { |
| 76 |
LOCK; |
| 77 |
} else { |
| 78 |
while (TRYLOCK == false) |
| 79 |
ck_pr_stall(); |
| 80 |
} |
| 81 |
#else |
| 82 |
LOCK; |
| 83 |
#endif |
| 84 |
|
| 85 |
#ifdef LOCKED |
| 86 |
if (LOCKED == false) |
| 87 |
ck_error("is_locked operation failed."); |
| 88 |
#endif |
| 89 |
|
| 90 |
ck_pr_store_uint(&locked, locked + 1); |
| 91 |
ck_pr_store_uint(&locked, locked + 1); |
| 92 |
ck_pr_store_uint(&locked, locked + 1); |
| 93 |
ck_pr_store_uint(&locked, locked + 1); |
| 94 |
ck_pr_store_uint(&locked, locked + 1); |
| 95 |
ck_pr_store_uint(&locked, locked + 1); |
| 96 |
ck_pr_store_uint(&locked, locked + 1); |
| 97 |
ck_pr_store_uint(&locked, locked + 1); |
| 98 |
ck_pr_store_uint(&locked, locked + 1); |
| 99 |
ck_pr_store_uint(&locked, locked + 1); |
| 100 |
|
| 101 |
j = ck_pr_load_uint(&locked); |
| 102 |
|
| 103 |
if (j != 10) { |
| 104 |
ck_error("ERROR (WR): Race condition (%u)\n", j); |
| 105 |
exit(EXIT_FAILURE); |
| 106 |
} |
| 107 |
|
| 108 |
ck_pr_store_uint(&locked, locked - 1); |
| 109 |
ck_pr_store_uint(&locked, locked - 1); |
| 110 |
ck_pr_store_uint(&locked, locked - 1); |
| 111 |
ck_pr_store_uint(&locked, locked - 1); |
| 112 |
ck_pr_store_uint(&locked, locked - 1); |
| 113 |
ck_pr_store_uint(&locked, locked - 1); |
| 114 |
ck_pr_store_uint(&locked, locked - 1); |
| 115 |
ck_pr_store_uint(&locked, locked - 1); |
| 116 |
ck_pr_store_uint(&locked, locked - 1); |
| 117 |
ck_pr_store_uint(&locked, locked - 1); |
| 118 |
|
| 119 |
UNLOCK; |
| 120 |
LOCK; |
| 121 |
|
| 122 |
j = ck_pr_load_uint(&locked); |
| 123 |
if (j != 0) { |
| 124 |
ck_error("ERROR (RD): Race condition (%u)\n", j); |
| 125 |
exit(EXIT_FAILURE); |
| 126 |
} |
| 127 |
|
| 128 |
UNLOCK; |
| 129 |
} |
| 130 |
|
| 131 |
return (NULL); |
| 132 |
} |
| 133 |
|
| 134 |
int |
| 135 |
main(int argc, char *argv[]) |
| 136 |
{ |
| 137 |
uint64_t i; |
| 138 |
pthread_t *threads; |
| 139 |
|
| 140 |
if (argc != 3) { |
| 141 |
ck_error("Usage: " LOCK_NAME " <number of threads> <affinity delta>\n"); |
| 142 |
exit(EXIT_FAILURE); |
| 143 |
} |
| 144 |
|
| 145 |
nthr = atoi(argv[1]); |
| 146 |
if (nthr <= 0) { |
| 147 |
ck_error("ERROR: Number of threads must be greater than 0\n"); |
| 148 |
exit(EXIT_FAILURE); |
| 149 |
} |
| 150 |
|
| 151 |
#ifdef LOCK_INIT |
| 152 |
LOCK_INIT; |
| 153 |
#endif |
| 154 |
|
| 155 |
threads = malloc(sizeof(pthread_t) * nthr); |
| 156 |
if (threads == NULL) { |
| 157 |
ck_error("ERROR: Could not allocate thread structures\n"); |
| 158 |
exit(EXIT_FAILURE); |
| 159 |
} |
| 160 |
|
| 161 |
a.delta = atoi(argv[2]); |
| 162 |
a.request = 0; |
| 163 |
|
| 164 |
fprintf(stderr, "Creating threads (mutual exclusion)..."); |
| 165 |
for (i = 0; i < nthr; i++) { |
| 166 |
if (pthread_create(&threads[i], NULL, thread, NULL)) { |
| 167 |
ck_error("ERROR: Could not create thread %" PRIu64 "\n", i); |
| 168 |
exit(EXIT_FAILURE); |
| 169 |
} |
| 170 |
} |
| 171 |
fprintf(stderr, "done\n"); |
| 172 |
|
| 173 |
fprintf(stderr, "Waiting for threads to finish correctness regression..."); |
| 174 |
for (i = 0; i < nthr; i++) |
| 175 |
pthread_join(threads[i], NULL); |
| 176 |
fprintf(stderr, "done (passed)\n"); |
| 177 |
|
| 178 |
return (0); |
| 179 |
} |
| 180 |
|