/usr/include/ck_rwcohort.h is in libck-dev 0.4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | /*
* Copyright 2013-2014 Samy Al Bahra.
* Copyright 2013 Brendon Scheinman.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* 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
* SUCH DAMAGE.
*/
#ifndef _CK_RWCOHORT_H
#define _CK_RWCOHORT_H
/*
* This is an implementation of NUMA-aware reader-writer locks as described in:
* Calciu, I.; Dice, D.; Lev, Y.; Luchangco, V.; Marathe, V.; and Shavit, N. 2014.
* NUMA-Aware Reader-Writer Locks
*/
#include <ck_cc.h>
#include <ck_pr.h>
#include <stddef.h>
#include <ck_cohort.h>
#define CK_RWCOHORT_WP_NAME(N) ck_rwcohort_wp_##N
#define CK_RWCOHORT_WP_INSTANCE(N) struct CK_RWCOHORT_WP_NAME(N)
#define CK_RWCOHORT_WP_INIT(N, RW, WL) ck_rwcohort_wp_##N##_init(RW, WL)
#define CK_RWCOHORT_WP_READ_LOCK(N, RW, C, GC, LC) \
ck_rwcohort_wp_##N##_read_lock(RW, C, GC, LC)
#define CK_RWCOHORT_WP_READ_UNLOCK(N, RW, C, GC, LC) \
ck_rwcohort_wp_##N##_read_unlock(RW)
#define CK_RWCOHORT_WP_WRITE_LOCK(N, RW, C, GC, LC) \
ck_rwcohort_wp_##N##_write_lock(RW, C, GC, LC)
#define CK_RWCOHORT_WP_WRITE_UNLOCK(N, RW, C, GC, LC) \
ck_rwcohort_wp_##N##_write_unlock(RW, C, GC, LC)
#define CK_RWCOHORT_WP_DEFAULT_WAIT_LIMIT 1000
#define CK_RWCOHORT_WP_PROTOTYPE(N) \
CK_RWCOHORT_WP_INSTANCE(N) { \
unsigned int read_counter; \
unsigned int write_barrier; \
unsigned int wait_limit; \
}; \
CK_CC_INLINE static void \
ck_rwcohort_wp_##N##_init(CK_RWCOHORT_WP_INSTANCE(N) *rw_cohort, \
unsigned int wait_limit) \
{ \
\
rw_cohort->read_counter = 0; \
rw_cohort->write_barrier = 0; \
rw_cohort->wait_limit = wait_limit; \
ck_pr_barrier(); \
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_wp_##N##_write_lock(CK_RWCOHORT_WP_INSTANCE(N) *rw_cohort, \
CK_COHORT_INSTANCE(N) *cohort, void *global_context, \
void *local_context) \
{ \
\
while (ck_pr_load_uint(&rw_cohort->write_barrier) > 0) \
ck_pr_stall(); \
\
CK_COHORT_LOCK(N, cohort, global_context, local_context); \
\
while (ck_pr_load_uint(&rw_cohort->read_counter) > 0) \
ck_pr_stall(); \
\
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_wp_##N##_write_unlock(CK_RWCOHORT_WP_INSTANCE(N) *rw_cohort, \
CK_COHORT_INSTANCE(N) *cohort, void *global_context, \
void *local_context) \
{ \
\
(void)rw_cohort; \
CK_COHORT_UNLOCK(N, cohort, global_context, local_context); \
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_wp_##N##_read_lock(CK_RWCOHORT_WP_INSTANCE(N) *rw_cohort, \
CK_COHORT_INSTANCE(N) *cohort, void *global_context, \
void *local_context) \
{ \
unsigned int wait_count = 0; \
bool raised = false; \
\
for (;;) { \
ck_pr_inc_uint(&rw_cohort->read_counter); \
ck_pr_fence_atomic_load(); \
if (CK_COHORT_LOCKED(N, cohort, global_context, \
local_context) == false) \
break; \
\
ck_pr_dec_uint(&rw_cohort->read_counter); \
while (CK_COHORT_LOCKED(N, cohort, global_context, \
local_context) == true) { \
ck_pr_stall(); \
if (++wait_count > rw_cohort->wait_limit && \
raised == false) { \
ck_pr_inc_uint(&rw_cohort->write_barrier); \
raised = true; \
} \
} \
} \
\
if (raised == true) \
ck_pr_dec_uint(&rw_cohort->write_barrier); \
\
ck_pr_fence_load(); \
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_wp_##N##_read_unlock(CK_RWCOHORT_WP_INSTANCE(N) *cohort) \
{ \
\
ck_pr_fence_load_atomic(); \
ck_pr_dec_uint(&cohort->read_counter); \
return; \
}
#define CK_RWCOHORT_WP_INITIALIZER { \
.read_counter = 0, \
.write_barrier = 0, \
.wait_limit = 0 \
}
#define CK_RWCOHORT_RP_NAME(N) ck_rwcohort_rp_##N
#define CK_RWCOHORT_RP_INSTANCE(N) struct CK_RWCOHORT_RP_NAME(N)
#define CK_RWCOHORT_RP_INIT(N, RW, WL) ck_rwcohort_rp_##N##_init(RW, WL)
#define CK_RWCOHORT_RP_READ_LOCK(N, RW, C, GC, LC) \
ck_rwcohort_rp_##N##_read_lock(RW, C, GC, LC)
#define CK_RWCOHORT_RP_READ_UNLOCK(N, RW, C, GC, LC) \
ck_rwcohort_rp_##N##_read_unlock(RW)
#define CK_RWCOHORT_RP_WRITE_LOCK(N, RW, C, GC, LC) \
ck_rwcohort_rp_##N##_write_lock(RW, C, GC, LC)
#define CK_RWCOHORT_RP_WRITE_UNLOCK(N, RW, C, GC, LC) \
ck_rwcohort_rp_##N##_write_unlock(RW, C, GC, LC)
#define CK_RWCOHORT_RP_DEFAULT_WAIT_LIMIT 1000
#define CK_RWCOHORT_RP_PROTOTYPE(N) \
CK_RWCOHORT_RP_INSTANCE(N) { \
unsigned int read_counter; \
unsigned int read_barrier; \
unsigned int wait_limit; \
}; \
CK_CC_INLINE static void \
ck_rwcohort_rp_##N##_init(CK_RWCOHORT_RP_INSTANCE(N) *rw_cohort, \
unsigned int wait_limit) \
{ \
\
rw_cohort->read_counter = 0; \
rw_cohort->read_barrier = 0; \
rw_cohort->wait_limit = wait_limit; \
ck_pr_barrier(); \
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_rp_##N##_write_lock(CK_RWCOHORT_RP_INSTANCE(N) *rw_cohort, \
CK_COHORT_INSTANCE(N) *cohort, void *global_context, \
void *local_context) \
{ \
unsigned int wait_count = 0; \
bool raised = false; \
\
for (;;) { \
CK_COHORT_LOCK(N, cohort, global_context, local_context); \
if (ck_pr_load_uint(&rw_cohort->read_counter) == 0) \
break; \
\
CK_COHORT_UNLOCK(N, cohort, global_context, local_context); \
while (ck_pr_load_uint(&rw_cohort->read_counter) > 0) { \
ck_pr_stall(); \
if (++wait_count > rw_cohort->wait_limit && \
raised == false) { \
ck_pr_inc_uint(&rw_cohort->read_barrier); \
raised = true; \
} \
} \
} \
\
if (raised == true) \
ck_pr_dec_uint(&rw_cohort->read_barrier); \
\
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_rp_##N##_write_unlock(CK_RWCOHORT_RP_INSTANCE(N) *rw_cohort, \
CK_COHORT_INSTANCE(N) *cohort, void *global_context, void *local_context) \
{ \
\
(void)rw_cohort; \
CK_COHORT_UNLOCK(N, cohort, global_context, local_context); \
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_rp_##N##_read_lock(CK_RWCOHORT_RP_INSTANCE(N) *rw_cohort, \
CK_COHORT_INSTANCE(N) *cohort, void *global_context, \
void *local_context) \
{ \
\
while (ck_pr_load_uint(&rw_cohort->read_barrier) > 0) \
ck_pr_stall(); \
\
ck_pr_inc_uint(&rw_cohort->read_counter); \
ck_pr_fence_atomic_load(); \
\
while (CK_COHORT_LOCKED(N, cohort, global_context, \
local_context) == true) \
ck_pr_stall(); \
\
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_rp_##N##_read_unlock(CK_RWCOHORT_RP_INSTANCE(N) *cohort) \
{ \
\
ck_pr_fence_load_atomic(); \
ck_pr_dec_uint(&cohort->read_counter); \
return; \
}
#define CK_RWCOHORT_RP_INITIALIZER { \
.read_counter = 0, \
.read_barrier = 0, \
.wait_limit = 0 \
}
#define CK_RWCOHORT_NEUTRAL_NAME(N) ck_rwcohort_neutral_##N
#define CK_RWCOHORT_NEUTRAL_INSTANCE(N) struct CK_RWCOHORT_NEUTRAL_NAME(N)
#define CK_RWCOHORT_NEUTRAL_INIT(N, RW) ck_rwcohort_neutral_##N##_init(RW)
#define CK_RWCOHORT_NEUTRAL_READ_LOCK(N, RW, C, GC, LC) \
ck_rwcohort_neutral_##N##_read_lock(RW, C, GC, LC)
#define CK_RWCOHORT_NEUTRAL_READ_UNLOCK(N, RW, C, GC, LC) \
ck_rwcohort_neutral_##N##_read_unlock(RW)
#define CK_RWCOHORT_NEUTRAL_WRITE_LOCK(N, RW, C, GC, LC) \
ck_rwcohort_neutral_##N##_write_lock(RW, C, GC, LC)
#define CK_RWCOHORT_NEUTRAL_WRITE_UNLOCK(N, RW, C, GC, LC) \
ck_rwcohort_neutral_##N##_write_unlock(RW, C, GC, LC)
#define CK_RWCOHORT_NEUTRAL_DEFAULT_WAIT_LIMIT 1000
#define CK_RWCOHORT_NEUTRAL_PROTOTYPE(N) \
CK_RWCOHORT_NEUTRAL_INSTANCE(N) { \
unsigned int read_counter; \
}; \
CK_CC_INLINE static void \
ck_rwcohort_neutral_##N##_init(CK_RWCOHORT_NEUTRAL_INSTANCE(N) *rw_cohort) \
{ \
\
rw_cohort->read_counter = 0; \
ck_pr_barrier(); \
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_neutral_##N##_write_lock(CK_RWCOHORT_NEUTRAL_INSTANCE(N) *rw_cohort,\
CK_COHORT_INSTANCE(N) *cohort, void *global_context, \
void *local_context) \
{ \
\
CK_COHORT_LOCK(N, cohort, global_context, local_context); \
while (ck_pr_load_uint(&rw_cohort->read_counter) > 0) { \
ck_pr_stall(); \
} \
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_neutral_##N##_write_unlock(CK_RWCOHORT_NEUTRAL_INSTANCE(N) *rw_cohort,\
CK_COHORT_INSTANCE(N) *cohort, void *global_context, void *local_context) \
{ \
\
(void)rw_cohort; \
CK_COHORT_UNLOCK(N, cohort, global_context, local_context); \
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_neutral_##N##_read_lock(CK_RWCOHORT_NEUTRAL_INSTANCE(N) *rw_cohort, \
CK_COHORT_INSTANCE(N) *cohort, void *global_context, \
void *local_context) \
{ \
\
CK_COHORT_LOCK(N, cohort, global_context, local_context); \
ck_pr_inc_uint(&rw_cohort->read_counter); \
CK_COHORT_UNLOCK(N, cohort, global_context, local_context); \
return; \
} \
CK_CC_INLINE static void \
ck_rwcohort_neutral_##N##_read_unlock(CK_RWCOHORT_NEUTRAL_INSTANCE(N) *cohort) \
{ \
\
ck_pr_fence_load_atomic(); \
ck_pr_dec_uint(&cohort->read_counter); \
return; \
}
#define CK_RWCOHORT_NEUTRAL_INITIALIZER { \
.read_counter = 0, \
}
#endif /* _CK_RWCOHORT_H */
|