| 1 | /* |
| 2 | * Copyright (c) 2003 by Hewlett-Packard Company. All rights reserved. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | /* The following is useful primarily for debugging and documentation. */ |
| 24 | /* We define various atomic operations by acquiring a global pthread */ |
| 25 | /* lock. The resulting implementation will perform poorly, but should */ |
| 26 | /* be correct unless it is used from signal handlers. */ |
| 27 | /* We assume that all pthread operations act like full memory barriers. */ |
| 28 | /* (We believe that is the intent of the specification.) */ |
| 29 | |
| 30 | #include <pthread.h> |
| 31 | |
| 32 | #include "test_and_set_t_is_ao_t.h" |
| 33 | /* This is not necessarily compatible with the native */ |
| 34 | /* implementation. But those can't be safely mixed anyway. */ |
| 35 | |
| 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | /* We define only the full barrier variants, and count on the */ |
| 41 | /* generalization section below to fill in the rest. */ |
| 42 | AO_API pthread_mutex_t AO_pt_lock; |
| 43 | |
| 44 | #ifdef __cplusplus |
| 45 | } /* extern "C" */ |
| 46 | #endif |
| 47 | |
| 48 | AO_INLINE void |
| 49 | AO_nop_full(void) |
| 50 | { |
| 51 | pthread_mutex_lock(&AO_pt_lock); |
| 52 | pthread_mutex_unlock(&AO_pt_lock); |
| 53 | } |
| 54 | #define AO_HAVE_nop_full |
| 55 | |
| 56 | AO_INLINE AO_t |
| 57 | AO_load_full(const volatile AO_t *addr) |
| 58 | { |
| 59 | AO_t result; |
| 60 | pthread_mutex_lock(&AO_pt_lock); |
| 61 | result = *addr; |
| 62 | pthread_mutex_unlock(&AO_pt_lock); |
| 63 | return result; |
| 64 | } |
| 65 | #define AO_HAVE_load_full |
| 66 | |
| 67 | AO_INLINE void |
| 68 | AO_store_full(volatile AO_t *addr, AO_t val) |
| 69 | { |
| 70 | pthread_mutex_lock(&AO_pt_lock); |
| 71 | *addr = val; |
| 72 | pthread_mutex_unlock(&AO_pt_lock); |
| 73 | } |
| 74 | #define AO_HAVE_store_full |
| 75 | |
| 76 | AO_INLINE unsigned char |
| 77 | AO_char_load_full(const volatile unsigned char *addr) |
| 78 | { |
| 79 | unsigned char result; |
| 80 | pthread_mutex_lock(&AO_pt_lock); |
| 81 | result = *addr; |
| 82 | pthread_mutex_unlock(&AO_pt_lock); |
| 83 | return result; |
| 84 | } |
| 85 | #define AO_HAVE_char_load_full |
| 86 | |
| 87 | AO_INLINE void |
| 88 | AO_char_store_full(volatile unsigned char *addr, unsigned char val) |
| 89 | { |
| 90 | pthread_mutex_lock(&AO_pt_lock); |
| 91 | *addr = val; |
| 92 | pthread_mutex_unlock(&AO_pt_lock); |
| 93 | } |
| 94 | #define AO_HAVE_char_store_full |
| 95 | |
| 96 | AO_INLINE unsigned short |
| 97 | AO_short_load_full(const volatile unsigned short *addr) |
| 98 | { |
| 99 | unsigned short result; |
| 100 | pthread_mutex_lock(&AO_pt_lock); |
| 101 | result = *addr; |
| 102 | pthread_mutex_unlock(&AO_pt_lock); |
| 103 | return result; |
| 104 | } |
| 105 | #define AO_HAVE_short_load_full |
| 106 | |
| 107 | AO_INLINE void |
| 108 | AO_short_store_full(volatile unsigned short *addr, unsigned short val) |
| 109 | { |
| 110 | pthread_mutex_lock(&AO_pt_lock); |
| 111 | *addr = val; |
| 112 | pthread_mutex_unlock(&AO_pt_lock); |
| 113 | } |
| 114 | #define AO_HAVE_short_store_full |
| 115 | |
| 116 | AO_INLINE unsigned int |
| 117 | AO_int_load_full(const volatile unsigned int *addr) |
| 118 | { |
| 119 | unsigned int result; |
| 120 | pthread_mutex_lock(&AO_pt_lock); |
| 121 | result = *addr; |
| 122 | pthread_mutex_unlock(&AO_pt_lock); |
| 123 | return result; |
| 124 | } |
| 125 | #define AO_HAVE_int_load_full |
| 126 | |
| 127 | AO_INLINE void |
| 128 | AO_int_store_full(volatile unsigned int *addr, unsigned int val) |
| 129 | { |
| 130 | pthread_mutex_lock(&AO_pt_lock); |
| 131 | *addr = val; |
| 132 | pthread_mutex_unlock(&AO_pt_lock); |
| 133 | } |
| 134 | #define AO_HAVE_int_store_full |
| 135 | |
| 136 | AO_INLINE AO_TS_VAL_t |
| 137 | AO_test_and_set_full(volatile AO_TS_t *addr) |
| 138 | { |
| 139 | AO_TS_VAL_t result; |
| 140 | pthread_mutex_lock(&AO_pt_lock); |
| 141 | result = (AO_TS_VAL_t)(*addr); |
| 142 | *addr = AO_TS_SET; |
| 143 | pthread_mutex_unlock(&AO_pt_lock); |
| 144 | assert(result == AO_TS_SET || result == AO_TS_CLEAR); |
| 145 | return result; |
| 146 | } |
| 147 | #define AO_HAVE_test_and_set_full |
| 148 | |
| 149 | AO_INLINE AO_t |
| 150 | AO_fetch_and_add_full(volatile AO_t *p, AO_t incr) |
| 151 | { |
| 152 | AO_t old_val; |
| 153 | |
| 154 | pthread_mutex_lock(&AO_pt_lock); |
| 155 | old_val = *p; |
| 156 | *p = old_val + incr; |
| 157 | pthread_mutex_unlock(&AO_pt_lock); |
| 158 | return old_val; |
| 159 | } |
| 160 | #define AO_HAVE_fetch_and_add_full |
| 161 | |
| 162 | AO_INLINE unsigned char |
| 163 | AO_char_fetch_and_add_full(volatile unsigned char *p, unsigned char incr) |
| 164 | { |
| 165 | unsigned char old_val; |
| 166 | |
| 167 | pthread_mutex_lock(&AO_pt_lock); |
| 168 | old_val = *p; |
| 169 | *p = old_val + incr; |
| 170 | pthread_mutex_unlock(&AO_pt_lock); |
| 171 | return old_val; |
| 172 | } |
| 173 | #define AO_HAVE_char_fetch_and_add_full |
| 174 | |
| 175 | AO_INLINE unsigned short |
| 176 | AO_short_fetch_and_add_full(volatile unsigned short *p, unsigned short incr) |
| 177 | { |
| 178 | unsigned short old_val; |
| 179 | |
| 180 | pthread_mutex_lock(&AO_pt_lock); |
| 181 | old_val = *p; |
| 182 | *p = old_val + incr; |
| 183 | pthread_mutex_unlock(&AO_pt_lock); |
| 184 | return old_val; |
| 185 | } |
| 186 | #define AO_HAVE_short_fetch_and_add_full |
| 187 | |
| 188 | AO_INLINE unsigned int |
| 189 | AO_int_fetch_and_add_full(volatile unsigned int *p, unsigned int incr) |
| 190 | { |
| 191 | unsigned int old_val; |
| 192 | |
| 193 | pthread_mutex_lock(&AO_pt_lock); |
| 194 | old_val = *p; |
| 195 | *p = old_val + incr; |
| 196 | pthread_mutex_unlock(&AO_pt_lock); |
| 197 | return old_val; |
| 198 | } |
| 199 | #define AO_HAVE_int_fetch_and_add_full |
| 200 | |
| 201 | AO_INLINE void |
| 202 | AO_and_full(volatile AO_t *p, AO_t value) |
| 203 | { |
| 204 | pthread_mutex_lock(&AO_pt_lock); |
| 205 | *p &= value; |
| 206 | pthread_mutex_unlock(&AO_pt_lock); |
| 207 | } |
| 208 | #define AO_HAVE_and_full |
| 209 | |
| 210 | AO_INLINE void |
| 211 | AO_or_full(volatile AO_t *p, AO_t value) |
| 212 | { |
| 213 | pthread_mutex_lock(&AO_pt_lock); |
| 214 | *p |= value; |
| 215 | pthread_mutex_unlock(&AO_pt_lock); |
| 216 | } |
| 217 | #define AO_HAVE_or_full |
| 218 | |
| 219 | AO_INLINE void |
| 220 | AO_xor_full(volatile AO_t *p, AO_t value) |
| 221 | { |
| 222 | pthread_mutex_lock(&AO_pt_lock); |
| 223 | *p ^= value; |
| 224 | pthread_mutex_unlock(&AO_pt_lock); |
| 225 | } |
| 226 | #define AO_HAVE_xor_full |
| 227 | |
| 228 | AO_INLINE void |
| 229 | AO_char_and_full(volatile unsigned char *p, unsigned char value) |
| 230 | { |
| 231 | pthread_mutex_lock(&AO_pt_lock); |
| 232 | *p &= value; |
| 233 | pthread_mutex_unlock(&AO_pt_lock); |
| 234 | } |
| 235 | #define AO_HAVE_char_and_full |
| 236 | |
| 237 | AO_INLINE void |
| 238 | AO_char_or_full(volatile unsigned char *p, unsigned char value) |
| 239 | { |
| 240 | pthread_mutex_lock(&AO_pt_lock); |
| 241 | *p |= value; |
| 242 | pthread_mutex_unlock(&AO_pt_lock); |
| 243 | } |
| 244 | #define AO_HAVE_char_or_full |
| 245 | |
| 246 | AO_INLINE void |
| 247 | AO_char_xor_full(volatile unsigned char *p, unsigned char value) |
| 248 | { |
| 249 | pthread_mutex_lock(&AO_pt_lock); |
| 250 | *p ^= value; |
| 251 | pthread_mutex_unlock(&AO_pt_lock); |
| 252 | } |
| 253 | #define AO_HAVE_char_xor_full |
| 254 | |
| 255 | AO_INLINE void |
| 256 | AO_short_and_full(volatile unsigned short *p, unsigned short value) |
| 257 | { |
| 258 | pthread_mutex_lock(&AO_pt_lock); |
| 259 | *p &= value; |
| 260 | pthread_mutex_unlock(&AO_pt_lock); |
| 261 | } |
| 262 | #define AO_HAVE_short_and_full |
| 263 | |
| 264 | AO_INLINE void |
| 265 | AO_short_or_full(volatile unsigned short *p, unsigned short value) |
| 266 | { |
| 267 | pthread_mutex_lock(&AO_pt_lock); |
| 268 | *p |= value; |
| 269 | pthread_mutex_unlock(&AO_pt_lock); |
| 270 | } |
| 271 | #define AO_HAVE_short_or_full |
| 272 | |
| 273 | AO_INLINE void |
| 274 | AO_short_xor_full(volatile unsigned short *p, unsigned short value) |
| 275 | { |
| 276 | pthread_mutex_lock(&AO_pt_lock); |
| 277 | *p ^= value; |
| 278 | pthread_mutex_unlock(&AO_pt_lock); |
| 279 | } |
| 280 | #define AO_HAVE_short_xor_full |
| 281 | |
| 282 | AO_INLINE void |
| 283 | AO_int_and_full(volatile unsigned *p, unsigned value) |
| 284 | { |
| 285 | pthread_mutex_lock(&AO_pt_lock); |
| 286 | *p &= value; |
| 287 | pthread_mutex_unlock(&AO_pt_lock); |
| 288 | } |
| 289 | #define AO_HAVE_int_and_full |
| 290 | |
| 291 | AO_INLINE void |
| 292 | AO_int_or_full(volatile unsigned *p, unsigned value) |
| 293 | { |
| 294 | pthread_mutex_lock(&AO_pt_lock); |
| 295 | *p |= value; |
| 296 | pthread_mutex_unlock(&AO_pt_lock); |
| 297 | } |
| 298 | #define AO_HAVE_int_or_full |
| 299 | |
| 300 | AO_INLINE void |
| 301 | AO_int_xor_full(volatile unsigned *p, unsigned value) |
| 302 | { |
| 303 | pthread_mutex_lock(&AO_pt_lock); |
| 304 | *p ^= value; |
| 305 | pthread_mutex_unlock(&AO_pt_lock); |
| 306 | } |
| 307 | #define AO_HAVE_int_xor_full |
| 308 | |
| 309 | AO_INLINE AO_t |
| 310 | AO_fetch_compare_and_swap_full(volatile AO_t *addr, AO_t old_val, |
| 311 | AO_t new_val) |
| 312 | { |
| 313 | AO_t fetched_val; |
| 314 | |
| 315 | pthread_mutex_lock(&AO_pt_lock); |
| 316 | fetched_val = *addr; |
| 317 | if (fetched_val == old_val) |
| 318 | *addr = new_val; |
| 319 | pthread_mutex_unlock(&AO_pt_lock); |
| 320 | return fetched_val; |
| 321 | } |
| 322 | #define AO_HAVE_fetch_compare_and_swap_full |
| 323 | |
| 324 | AO_INLINE unsigned char |
| 325 | AO_char_fetch_compare_and_swap_full(volatile unsigned char *addr, |
| 326 | unsigned char old_val, |
| 327 | unsigned char new_val) |
| 328 | { |
| 329 | unsigned char fetched_val; |
| 330 | |
| 331 | pthread_mutex_lock(&AO_pt_lock); |
| 332 | fetched_val = *addr; |
| 333 | if (fetched_val == old_val) |
| 334 | *addr = new_val; |
| 335 | pthread_mutex_unlock(&AO_pt_lock); |
| 336 | return fetched_val; |
| 337 | } |
| 338 | #define AO_HAVE_char_fetch_compare_and_swap_full |
| 339 | |
| 340 | AO_INLINE unsigned short |
| 341 | AO_short_fetch_compare_and_swap_full(volatile unsigned short *addr, |
| 342 | unsigned short old_val, |
| 343 | unsigned short new_val) |
| 344 | { |
| 345 | unsigned short fetched_val; |
| 346 | |
| 347 | pthread_mutex_lock(&AO_pt_lock); |
| 348 | fetched_val = *addr; |
| 349 | if (fetched_val == old_val) |
| 350 | *addr = new_val; |
| 351 | pthread_mutex_unlock(&AO_pt_lock); |
| 352 | return fetched_val; |
| 353 | } |
| 354 | #define AO_HAVE_short_fetch_compare_and_swap_full |
| 355 | |
| 356 | AO_INLINE unsigned |
| 357 | AO_int_fetch_compare_and_swap_full(volatile unsigned *addr, unsigned old_val, |
| 358 | unsigned new_val) |
| 359 | { |
| 360 | unsigned fetched_val; |
| 361 | |
| 362 | pthread_mutex_lock(&AO_pt_lock); |
| 363 | fetched_val = *addr; |
| 364 | if (fetched_val == old_val) |
| 365 | *addr = new_val; |
| 366 | pthread_mutex_unlock(&AO_pt_lock); |
| 367 | return fetched_val; |
| 368 | } |
| 369 | #define AO_HAVE_int_fetch_compare_and_swap_full |
| 370 | |
| 371 | /* Unlike real architectures, we define both double-width CAS variants. */ |
| 372 | |
| 373 | typedef struct { |
| 374 | AO_t AO_val1; |
| 375 | AO_t AO_val2; |
| 376 | } AO_double_t; |
| 377 | #define AO_HAVE_double_t |
| 378 | |
| 379 | #define AO_DOUBLE_T_INITIALIZER { (AO_t)0, (AO_t)0 } |
| 380 | |
| 381 | AO_INLINE AO_double_t |
| 382 | AO_double_load_full(const volatile AO_double_t *addr) |
| 383 | { |
| 384 | AO_double_t result; |
| 385 | |
| 386 | pthread_mutex_lock(&AO_pt_lock); |
| 387 | result.AO_val1 = addr->AO_val1; |
| 388 | result.AO_val2 = addr->AO_val2; |
| 389 | pthread_mutex_unlock(&AO_pt_lock); |
| 390 | return result; |
| 391 | } |
| 392 | #define AO_HAVE_double_load_full |
| 393 | |
| 394 | AO_INLINE void |
| 395 | AO_double_store_full(volatile AO_double_t *addr, AO_double_t value) |
| 396 | { |
| 397 | pthread_mutex_lock(&AO_pt_lock); |
| 398 | addr->AO_val1 = value.AO_val1; |
| 399 | addr->AO_val2 = value.AO_val2; |
| 400 | pthread_mutex_unlock(&AO_pt_lock); |
| 401 | } |
| 402 | #define AO_HAVE_double_store_full |
| 403 | |
| 404 | AO_INLINE int |
| 405 | AO_compare_double_and_swap_double_full(volatile AO_double_t *addr, |
| 406 | AO_t old1, AO_t old2, |
| 407 | AO_t new1, AO_t new2) |
| 408 | { |
| 409 | pthread_mutex_lock(&AO_pt_lock); |
| 410 | if (addr -> AO_val1 == old1 && addr -> AO_val2 == old2) |
| 411 | { |
| 412 | addr -> AO_val1 = new1; |
| 413 | addr -> AO_val2 = new2; |
| 414 | pthread_mutex_unlock(&AO_pt_lock); |
| 415 | return 1; |
| 416 | } |
| 417 | else |
| 418 | pthread_mutex_unlock(&AO_pt_lock); |
| 419 | return 0; |
| 420 | } |
| 421 | #define AO_HAVE_compare_double_and_swap_double_full |
| 422 | |
| 423 | AO_INLINE int |
| 424 | AO_compare_and_swap_double_full(volatile AO_double_t *addr, |
| 425 | AO_t old1, AO_t new1, AO_t new2) |
| 426 | { |
| 427 | pthread_mutex_lock(&AO_pt_lock); |
| 428 | if (addr -> AO_val1 == old1) |
| 429 | { |
| 430 | addr -> AO_val1 = new1; |
| 431 | addr -> AO_val2 = new2; |
| 432 | pthread_mutex_unlock(&AO_pt_lock); |
| 433 | return 1; |
| 434 | } |
| 435 | else |
| 436 | pthread_mutex_unlock(&AO_pt_lock); |
| 437 | return 0; |
| 438 | } |
| 439 | #define AO_HAVE_compare_and_swap_double_full |
| 440 | |
| 441 | /* We can't use hardware loads and stores, since they don't */ |
| 442 | /* interact correctly with atomic updates. */ |
| 443 | |