1 /* 2 * kmp_lock.h -- lock header file 3 */ 4 5 //===----------------------------------------------------------------------===// 6 // 7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 8 // See https://llvm.org/LICENSE.txt for license information. 9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef KMP_LOCK_H 14 #define KMP_LOCK_H 15 16 #include <limits.h> // CHAR_BIT 17 #include <stddef.h> // offsetof 18 19 #include "kmp_debug.h" 20 #include "kmp_os.h" 21 22 #ifdef __cplusplus 23 #include <atomic> 24 25 extern "C" { 26 #endif // __cplusplus 27 28 // ---------------------------------------------------------------------------- 29 // Have to copy these definitions from kmp.h because kmp.h cannot be included 30 // due to circular dependencies. Will undef these at end of file. 31 32 #define KMP_PAD(type, sz) \ 33 (sizeof(type) + (sz - ((sizeof(type) - 1) % (sz)) - 1)) 34 #define KMP_GTID_DNE (-2) 35 36 // Forward declaration of ident and ident_t 37 38 struct ident; 39 typedef struct ident ident_t; 40 41 // End of copied code. 42 // ---------------------------------------------------------------------------- 43 44 // We need to know the size of the area we can assume that the compiler(s) 45 // allocated for objects of type omp_lock_t and omp_nest_lock_t. The Intel 46 // compiler always allocates a pointer-sized area, as does visual studio. 47 // 48 // gcc however, only allocates 4 bytes for regular locks, even on 64-bit 49 // intel archs. It allocates at least 8 bytes for nested lock (more on 50 // recent versions), but we are bounded by the pointer-sized chunks that 51 // the Intel compiler allocates. 52 53 #if KMP_OS_LINUX && defined(KMP_GOMP_COMPAT) 54 #define OMP_LOCK_T_SIZE sizeof(int) 55 #define OMP_NEST_LOCK_T_SIZE sizeof(void *) 56 #else 57 #define OMP_LOCK_T_SIZE sizeof(void *) 58 #define OMP_NEST_LOCK_T_SIZE sizeof(void *) 59 #endif 60 61 // The Intel compiler allocates a 32-byte chunk for a critical section. 62 // Both gcc and visual studio only allocate enough space for a pointer. 63 // Sometimes we know that the space was allocated by the Intel compiler. 64 #define OMP_CRITICAL_SIZE sizeof(void *) 65 #define INTEL_CRITICAL_SIZE 32 66 67 // lock flags 68 typedef kmp_uint32 kmp_lock_flags_t; 69 70 #define kmp_lf_critical_section 1 71 72 // When a lock table is used, the indices are of kmp_lock_index_t 73 typedef kmp_uint32 kmp_lock_index_t; 74 75 // When memory allocated for locks are on the lock pool (free list), 76 // it is treated as structs of this type. 77 struct kmp_lock_pool { 78 union kmp_user_lock *next; 79 kmp_lock_index_t index; 80 }; 81 82 typedef struct kmp_lock_pool kmp_lock_pool_t; 83 84 extern void __kmp_validate_locks(void); 85 86 // ---------------------------------------------------------------------------- 87 // There are 5 lock implementations: 88 // 1. Test and set locks. 89 // 2. futex locks (Linux* OS on x86 and 90 // Intel(R) Many Integrated Core Architecture) 91 // 3. Ticket (Lamport bakery) locks. 92 // 4. Queuing locks (with separate spin fields). 93 // 5. DRPA (Dynamically Reconfigurable Distributed Polling Area) locks 94 // 95 // and 3 lock purposes: 96 // 1. Bootstrap locks -- Used for a few locks available at library 97 // startup-shutdown time. 98 // These do not require non-negative global thread ID's. 99 // 2. Internal RTL locks -- Used everywhere else in the RTL 100 // 3. User locks (includes critical sections) 101 // ---------------------------------------------------------------------------- 102 103 // ============================================================================ 104 // Lock implementations. 105 // 106 // Test and set locks. 107 // 108 // Non-nested test and set locks differ from the other lock kinds (except 109 // futex) in that we use the memory allocated by the compiler for the lock, 110 // rather than a pointer to it. 111 // 112 // On lin32, lin_32e, and win_32, the space allocated may be as small as 4 113 // bytes, so we have to use a lock table for nested locks, and avoid accessing 114 // the depth_locked field for non-nested locks. 115 // 116 // Information normally available to the tools, such as lock location, lock 117 // usage (normal lock vs. critical section), etc. is not available with test and 118 // set locks. 119 // ---------------------------------------------------------------------------- 120 121 struct kmp_base_tas_lock { 122 // KMP_LOCK_FREE(tas) => unlocked; locked: (gtid+1) of owning thread 123 std::atomic<kmp_int32> poll; 124 kmp_int32 depth_locked; // depth locked, for nested locks only 125 }; 126 127 typedef struct kmp_base_tas_lock kmp_base_tas_lock_t; 128 129 union kmp_tas_lock { 130 kmp_base_tas_lock_t lk; 131 kmp_lock_pool_t pool; // make certain struct is large enough 132 double lk_align; // use worst case alignment; no cache line padding 133 }; 134 135 typedef union kmp_tas_lock kmp_tas_lock_t; 136 137 // Static initializer for test and set lock variables. Usage: 138 // kmp_tas_lock_t xlock = KMP_TAS_LOCK_INITIALIZER( xlock ); 139 #define KMP_TAS_LOCK_INITIALIZER(lock) \ 140 { \ 141 { ATOMIC_VAR_INIT(KMP_LOCK_FREE(tas)), 0 } \ 142 } 143 144 extern int __kmp_acquire_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid); 145 extern int __kmp_test_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid); 146 extern int __kmp_release_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid); 147 extern void __kmp_init_tas_lock(kmp_tas_lock_t *lck); 148 extern void __kmp_destroy_tas_lock(kmp_tas_lock_t *lck); 149 150 extern int __kmp_acquire_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid); 151 extern int __kmp_test_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid); 152 extern int __kmp_release_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid); 153 extern void __kmp_init_nested_tas_lock(kmp_tas_lock_t *lck); 154 extern void __kmp_destroy_nested_tas_lock(kmp_tas_lock_t *lck); 155 156 #define KMP_LOCK_RELEASED 1 157 #define KMP_LOCK_STILL_HELD 0 158 #define KMP_LOCK_ACQUIRED_FIRST 1 159 #define KMP_LOCK_ACQUIRED_NEXT 0 160 #ifndef KMP_USE_FUTEX 161 #define KMP_USE_FUTEX \ 162 (KMP_OS_LINUX && \ 163 (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)) 164 #endif 165 #if KMP_USE_FUTEX 166 167 // ---------------------------------------------------------------------------- 168 // futex locks. futex locks are only available on Linux* OS. 169 // 170 // Like non-nested test and set lock, non-nested futex locks use the memory 171 // allocated by the compiler for the lock, rather than a pointer to it. 172 // 173 // Information normally available to the tools, such as lock location, lock 174 // usage (normal lock vs. critical section), etc. is not available with test and 175 // set locks. With non-nested futex locks, the lock owner is not even available. 176 // ---------------------------------------------------------------------------- 177 178 struct kmp_base_futex_lock { 179 volatile kmp_int32 poll; // KMP_LOCK_FREE(futex) => unlocked 180 // 2*(gtid+1) of owning thread, 0 if unlocked 181 // locked: (gtid+1) of owning thread 182 kmp_int32 depth_locked; // depth locked, for nested locks only 183 }; 184 185 typedef struct kmp_base_futex_lock kmp_base_futex_lock_t; 186 187 union kmp_futex_lock { 188 kmp_base_futex_lock_t lk; 189 kmp_lock_pool_t pool; // make certain struct is large enough 190 double lk_align; // use worst case alignment 191 // no cache line padding 192 }; 193 194 typedef union kmp_futex_lock kmp_futex_lock_t; 195 196 // Static initializer for futex lock variables. Usage: 197 // kmp_futex_lock_t xlock = KMP_FUTEX_LOCK_INITIALIZER( xlock ); 198 #define KMP_FUTEX_LOCK_INITIALIZER(lock) \ 199 { \ 200 { KMP_LOCK_FREE(futex), 0 } \ 201 } 202 203 extern int __kmp_acquire_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid); 204 extern int __kmp_test_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid); 205 extern int __kmp_release_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid); 206 extern void __kmp_init_futex_lock(kmp_futex_lock_t *lck); 207 extern void __kmp_destroy_futex_lock(kmp_futex_lock_t *lck); 208 209 extern int __kmp_acquire_nested_futex_lock(kmp_futex_lock_t *lck, 210 kmp_int32 gtid); 211 extern int __kmp_test_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid); 212 extern int __kmp_release_nested_futex_lock(kmp_futex_lock_t *lck, 213 kmp_int32 gtid); 214 extern void __kmp_init_nested_futex_lock(kmp_futex_lock_t *lck); 215 extern void __kmp_destroy_nested_futex_lock(kmp_futex_lock_t *lck); 216 217 #endif // KMP_USE_FUTEX 218 219 // ---------------------------------------------------------------------------- 220 // Ticket locks. 221 222 #ifdef __cplusplus 223 224 #ifdef _MSC_VER 225 // MSVC won't allow use of std::atomic<> in a union since it has non-trivial 226 // copy constructor. 227 228 struct kmp_base_ticket_lock { 229 // `initialized' must be the first entry in the lock data structure! 230 std::atomic_bool initialized; 231 volatile union kmp_ticket_lock *self; // points to the lock union 232 ident_t const *location; // Source code location of omp_init_lock(). 233 std::atomic_uint 234 next_ticket; // ticket number to give to next thread which acquires 235 std::atomic_uint now_serving; // ticket number for thread which holds the lock 236 std::atomic_int owner_id; // (gtid+1) of owning thread, 0 if unlocked 237 std::atomic_int depth_locked; // depth locked, for nested locks only 238 kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock 239 }; 240 #else 241 struct kmp_base_ticket_lock { 242 // `initialized' must be the first entry in the lock data structure! 243 std::atomic<bool> initialized; 244 volatile union kmp_ticket_lock *self; // points to the lock union 245 ident_t const *location; // Source code location of omp_init_lock(). 246 std::atomic<unsigned> 247 next_ticket; // ticket number to give to next thread which acquires 248 std::atomic<unsigned> 249 now_serving; // ticket number for thread which holds the lock 250 std::atomic<int> owner_id; // (gtid+1) of owning thread, 0 if unlocked 251 std::atomic<int> depth_locked; // depth locked, for nested locks only 252 kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock 253 }; 254 #endif 255 256 #else // __cplusplus 257 258 struct kmp_base_ticket_lock; 259 260 #endif // !__cplusplus 261 262 typedef struct kmp_base_ticket_lock kmp_base_ticket_lock_t; 263 264 union KMP_ALIGN_CACHE kmp_ticket_lock { 265 kmp_base_ticket_lock_t 266 lk; // This field must be first to allow static initializing. 267 kmp_lock_pool_t pool; 268 double lk_align; // use worst case alignment 269 char lk_pad[KMP_PAD(kmp_base_ticket_lock_t, CACHE_LINE)]; 270 }; 271 272 typedef union kmp_ticket_lock kmp_ticket_lock_t; 273 274 // Static initializer for simple ticket lock variables. Usage: 275 // kmp_ticket_lock_t xlock = KMP_TICKET_LOCK_INITIALIZER( xlock ); 276 // Note the macro argument. It is important to make var properly initialized. 277 #define KMP_TICKET_LOCK_INITIALIZER(lock) \ 278 { \ 279 { \ 280 ATOMIC_VAR_INIT(true) \ 281 , &(lock), NULL, ATOMIC_VAR_INIT(0U), ATOMIC_VAR_INIT(0U), \ 282 ATOMIC_VAR_INIT(0), ATOMIC_VAR_INIT(-1) \ 283 } \ 284 } 285 286 extern int __kmp_acquire_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid); 287 extern int __kmp_test_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid); 288 extern int __kmp_test_ticket_lock_with_cheks(kmp_ticket_lock_t *lck, 289 kmp_int32 gtid); 290 extern int __kmp_release_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid); 291 extern void __kmp_init_ticket_lock(kmp_ticket_lock_t *lck); 292 extern void __kmp_destroy_ticket_lock(kmp_ticket_lock_t *lck); 293 294 extern int __kmp_acquire_nested_ticket_lock(kmp_ticket_lock_t *lck, 295 kmp_int32 gtid); 296 extern int __kmp_test_nested_ticket_lock(kmp_ticket_lock_t *lck, 297 kmp_int32 gtid); 298 extern int __kmp_release_nested_ticket_lock(kmp_ticket_lock_t *lck, 299 kmp_int32 gtid); 300 extern void __kmp_init_nested_ticket_lock(kmp_ticket_lock_t *lck); 301 extern void __kmp_destroy_nested_ticket_lock(kmp_ticket_lock_t *lck); 302 303 // ---------------------------------------------------------------------------- 304 // Queuing locks. 305 306 #if KMP_USE_ADAPTIVE_LOCKS 307 308 struct kmp_adaptive_lock_info; 309 310 typedef struct kmp_adaptive_lock_info kmp_adaptive_lock_info_t; 311 312 #if KMP_DEBUG_ADAPTIVE_LOCKS 313 314 struct kmp_adaptive_lock_statistics { 315 /* So we can get stats from locks that haven't been destroyed. */ 316 kmp_adaptive_lock_info_t *next; 317 kmp_adaptive_lock_info_t *prev; 318 319 /* Other statistics */ 320 kmp_uint32 successfulSpeculations; 321 kmp_uint32 hardFailedSpeculations; 322 kmp_uint32 softFailedSpeculations; 323 kmp_uint32 nonSpeculativeAcquires; 324 kmp_uint32 nonSpeculativeAcquireAttempts; 325 kmp_uint32 lemmingYields; 326 }; 327 328 typedef struct kmp_adaptive_lock_statistics kmp_adaptive_lock_statistics_t; 329 330 extern void __kmp_print_speculative_stats(); 331 extern void __kmp_init_speculative_stats(); 332 333 #endif // KMP_DEBUG_ADAPTIVE_LOCKS 334 335 struct kmp_adaptive_lock_info { 336 /* Values used for adaptivity. 337 Although these are accessed from multiple threads we don't access them 338 atomically, because if we miss updates it probably doesn't matter much. (It 339 just affects our decision about whether to try speculation on the lock). */ 340 kmp_uint32 volatile badness; 341 kmp_uint32 volatile acquire_attempts; 342 /* Parameters of the lock. */ 343 kmp_uint32 max_badness; 344 kmp_uint32 max_soft_retries; 345 346 #if KMP_DEBUG_ADAPTIVE_LOCKS 347 kmp_adaptive_lock_statistics_t volatile stats; 348 #endif 349 }; 350 351 #endif // KMP_USE_ADAPTIVE_LOCKS 352 353 struct kmp_base_queuing_lock { 354 355 // `initialized' must be the first entry in the lock data structure! 356 volatile union kmp_queuing_lock 357 *initialized; // Points to the lock union if in initialized state. 358 359 ident_t const *location; // Source code location of omp_init_lock(). 360 361 KMP_ALIGN(8) // tail_id must be 8-byte aligned! 362 363 volatile kmp_int32 364 tail_id; // (gtid+1) of thread at tail of wait queue, 0 if empty 365 // Must be no padding here since head/tail used in 8-byte CAS 366 volatile kmp_int32 367 head_id; // (gtid+1) of thread at head of wait queue, 0 if empty 368 // Decl order assumes little endian 369 // bakery-style lock 370 volatile kmp_uint32 371 next_ticket; // ticket number to give to next thread which acquires 372 volatile kmp_uint32 373 now_serving; // ticket number for thread which holds the lock 374 volatile kmp_int32 owner_id; // (gtid+1) of owning thread, 0 if unlocked 375 kmp_int32 depth_locked; // depth locked, for nested locks only 376 377 kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock 378 }; 379 380 typedef struct kmp_base_queuing_lock kmp_base_queuing_lock_t; 381 382 KMP_BUILD_ASSERT(offsetof(kmp_base_queuing_lock_t, tail_id) % 8 == 0); 383 384 union KMP_ALIGN_CACHE kmp_queuing_lock { 385 kmp_base_queuing_lock_t 386 lk; // This field must be first to allow static initializing. 387 kmp_lock_pool_t pool; 388 double lk_align; // use worst case alignment 389 char lk_pad[KMP_PAD(kmp_base_queuing_lock_t, CACHE_LINE)]; 390 }; 391 392 typedef union kmp_queuing_lock kmp_queuing_lock_t; 393 394 extern int __kmp_acquire_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid); 395 extern int __kmp_test_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid); 396 extern int __kmp_release_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid); 397 extern void __kmp_init_queuing_lock(kmp_queuing_lock_t *lck); 398 extern void __kmp_destroy_queuing_lock(kmp_queuing_lock_t *lck); 399 400 extern int __kmp_acquire_nested_queuing_lock(kmp_queuing_lock_t *lck, 401 kmp_int32 gtid); 402 extern int __kmp_test_nested_queuing_lock(kmp_queuing_lock_t *lck, 403 kmp_int32 gtid); 404 extern int __kmp_release_nested_queuing_lock(kmp_queuing_lock_t *lck, 405 kmp_int32 gtid); 406 extern void __kmp_init_nested_queuing_lock(kmp_queuing_lock_t *lck); 407 extern void __kmp_destroy_nested_queuing_lock(kmp_queuing_lock_t *lck); 408 409 #if KMP_USE_ADAPTIVE_LOCKS 410 411 // ---------------------------------------------------------------------------- 412 // Adaptive locks. 413 struct kmp_base_adaptive_lock { 414 kmp_base_queuing_lock qlk; 415 KMP_ALIGN(CACHE_LINE) 416 kmp_adaptive_lock_info_t 417 adaptive; // Information for the speculative adaptive lock 418 }; 419 420 typedef struct kmp_base_adaptive_lock kmp_base_adaptive_lock_t; 421 422 union KMP_ALIGN_CACHE kmp_adaptive_lock { 423 kmp_base_adaptive_lock_t lk; 424 kmp_lock_pool_t pool; 425 double lk_align; 426 char lk_pad[KMP_PAD(kmp_base_adaptive_lock_t, CACHE_LINE)]; 427 }; 428 typedef union kmp_adaptive_lock kmp_adaptive_lock_t; 429 430 #define GET_QLK_PTR(l) ((kmp_queuing_lock_t *)&(l)->lk.qlk) 431 432 #endif // KMP_USE_ADAPTIVE_LOCKS 433 434 // ---------------------------------------------------------------------------- 435 // DRDPA ticket locks. 436 struct kmp_base_drdpa_lock { 437 // All of the fields on the first cache line are only written when 438 // initializing or reconfiguring the lock. These are relatively rare 439 // operations, so data from the first cache line will usually stay resident in 440 // the cache of each thread trying to acquire the lock. 441 // 442 // initialized must be the first entry in the lock data structure! 443 KMP_ALIGN_CACHE 444 445 volatile union kmp_drdpa_lock 446 *initialized; // points to the lock union if in initialized state 447 ident_t const *location; // Source code location of omp_init_lock(). 448 std::atomic<std::atomic<kmp_uint64> *> polls; 449 std::atomic<kmp_uint64> mask; // is 2**num_polls-1 for mod op 450 kmp_uint64 cleanup_ticket; // thread with cleanup ticket 451 std::atomic<kmp_uint64> *old_polls; // will deallocate old_polls 452 kmp_uint32 num_polls; // must be power of 2 453 454 // next_ticket it needs to exist in a separate cache line, as it is 455 // invalidated every time a thread takes a new ticket. 456 KMP_ALIGN_CACHE 457 458 std::atomic<kmp_uint64> next_ticket; 459 460 // now_serving is used to store our ticket value while we hold the lock. It 461 // has a slightly different meaning in the DRDPA ticket locks (where it is 462 // written by the acquiring thread) than it does in the simple ticket locks 463 // (where it is written by the releasing thread). 464 // 465 // Since now_serving is only read and written in the critical section, 466 // it is non-volatile, but it needs to exist on a separate cache line, 467 // as it is invalidated at every lock acquire. 468 // 469 // Likewise, the vars used for nested locks (owner_id and depth_locked) are 470 // only written by the thread owning the lock, so they are put in this cache 471 // line. owner_id is read by other threads, so it must be declared volatile. 472 KMP_ALIGN_CACHE 473 kmp_uint64 now_serving; // doesn't have to be volatile 474 volatile kmp_uint32 owner_id; // (gtid+1) of owning thread, 0 if unlocked 475 kmp_int32 depth_locked; // depth locked 476 kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock 477 }; 478 479 typedef struct kmp_base_drdpa_lock kmp_base_drdpa_lock_t; 480 481 union KMP_ALIGN_CACHE kmp_drdpa_lock { 482 kmp_base_drdpa_lock_t 483 lk; // This field must be first to allow static initializing. */ 484 kmp_lock_pool_t pool; 485 double lk_align; // use worst case alignment 486 char lk_pad[KMP_PAD(kmp_base_drdpa_lock_t, CACHE_LINE)]; 487 }; 488 489 typedef union kmp_drdpa_lock kmp_drdpa_lock_t; 490 491 extern int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid); 492 extern int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid); 493 extern int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid); 494 extern void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck); 495 extern void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck); 496 497 extern int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck, 498 kmp_int32 gtid); 499 extern int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid); 500 extern int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck, 501 kmp_int32 gtid); 502 extern void __kmp_init_nested_drdpa_lock(kmp_drdpa_lock_t *lck); 503 extern void __kmp_destroy_nested_drdpa_lock(kmp_drdpa_lock_t *lck); 504 505 // ============================================================================ 506 // Lock purposes. 507 // ============================================================================ 508 509 // Bootstrap locks. 510 // 511 // Bootstrap locks -- very few locks used at library initialization time. 512 // Bootstrap locks are currently implemented as ticket locks. 513 // They could also be implemented as test and set lock, but cannot be 514 // implemented with other lock kinds as they require gtids which are not 515 // available at initialization time. 516 517 typedef kmp_ticket_lock_t kmp_bootstrap_lock_t; 518 519 #define KMP_BOOTSTRAP_LOCK_INITIALIZER(lock) KMP_TICKET_LOCK_INITIALIZER((lock)) 520 #define KMP_BOOTSTRAP_LOCK_INIT(lock) \ 521 kmp_bootstrap_lock_t lock = KMP_TICKET_LOCK_INITIALIZER(lock) 522 523 static inline int __kmp_acquire_bootstrap_lock(kmp_bootstrap_lock_t *lck) { 524 return __kmp_acquire_ticket_lock(lck, KMP_GTID_DNE); 525 } 526 527 static inline int __kmp_test_bootstrap_lock(kmp_bootstrap_lock_t *lck) { 528 return __kmp_test_ticket_lock(lck, KMP_GTID_DNE); 529 } 530 531 static inline void __kmp_release_bootstrap_lock(kmp_bootstrap_lock_t *lck) { 532 __kmp_release_ticket_lock(lck, KMP_GTID_DNE); 533 } 534 535 static inline void __kmp_init_bootstrap_lock(kmp_bootstrap_lock_t *lck) { 536 __kmp_init_ticket_lock(lck); 537 } 538 539 static inline void __kmp_destroy_bootstrap_lock(kmp_bootstrap_lock_t *lck) { 540 __kmp_destroy_ticket_lock(lck); 541 } 542 543 // Internal RTL locks. 544 // 545 // Internal RTL locks are also implemented as ticket locks, for now. 546 // 547 // FIXME - We should go through and figure out which lock kind works best for 548 // each internal lock, and use the type declaration and function calls for 549 // that explicit lock kind (and get rid of this section). 550 551 typedef kmp_ticket_lock_t kmp_lock_t; 552 553 #define KMP_LOCK_INIT(lock) kmp_lock_t lock = KMP_TICKET_LOCK_INITIALIZER(lock) 554 555 static inline int __kmp_acquire_lock(kmp_lock_t *lck, kmp_int32 gtid) { 556 return __kmp_acquire_ticket_lock(lck, gtid); 557 } 558 559 static inline int __kmp_test_lock(kmp_lock_t *lck, kmp_int32 gtid) { 560 return __kmp_test_ticket_lock(lck, gtid); 561 } 562 563 static inline void __kmp_release_lock(kmp_lock_t *lck, kmp_int32 gtid) { 564 __kmp_release_ticket_lock(lck, gtid); 565 } 566 567 static inline void __kmp_init_lock(kmp_lock_t *lck) { 568 __kmp_init_ticket_lock(lck); 569 } 570 571 static inline void __kmp_destroy_lock(kmp_lock_t *lck) { 572 __kmp_destroy_ticket_lock(lck); 573 } 574 575 // User locks. 576 // 577 // Do not allocate objects of type union kmp_user_lock!!! This will waste space 578 // unless __kmp_user_lock_kind == lk_drdpa. Instead, check the value of 579 // __kmp_user_lock_kind and allocate objects of the type of the appropriate 580 // union member, and cast their addresses to kmp_user_lock_p. 581 582 enum kmp_lock_kind { 583 lk_default = 0, 584 lk_tas, 585 #if KMP_USE_FUTEX 586 lk_futex, 587 #endif 588 #if KMP_USE_DYNAMIC_LOCK && KMP_USE_TSX 589 lk_hle, 590 lk_rtm_queuing, 591 lk_rtm_spin, 592 #endif 593 lk_ticket, 594 lk_queuing, 595 lk_drdpa, 596 #if KMP_USE_ADAPTIVE_LOCKS 597 lk_adaptive 598 #endif // KMP_USE_ADAPTIVE_LOCKS 599 }; 600 601 typedef enum kmp_lock_kind kmp_lock_kind_t; 602 603 extern kmp_lock_kind_t __kmp_user_lock_kind; 604 605 union kmp_user_lock { 606 kmp_tas_lock_t tas; 607 #if KMP_USE_FUTEX 608 kmp_futex_lock_t futex; 609 #endif 610 kmp_ticket_lock_t ticket; 611 kmp_queuing_lock_t queuing; 612 kmp_drdpa_lock_t drdpa; 613 #if KMP_USE_ADAPTIVE_LOCKS 614 kmp_adaptive_lock_t adaptive; 615 #endif // KMP_USE_ADAPTIVE_LOCKS 616 kmp_lock_pool_t pool; 617 }; 618 619 typedef union kmp_user_lock *kmp_user_lock_p; 620 621 #if !KMP_USE_DYNAMIC_LOCK 622 623 extern size_t __kmp_base_user_lock_size; 624 extern size_t __kmp_user_lock_size; 625 626 extern kmp_int32 (*__kmp_get_user_lock_owner_)(kmp_user_lock_p lck); 627 628 static inline kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p lck) { 629 KMP_DEBUG_ASSERT(__kmp_get_user_lock_owner_ != NULL); 630 return (*__kmp_get_user_lock_owner_)(lck); 631 } 632 633 extern int (*__kmp_acquire_user_lock_with_checks_)(kmp_user_lock_p lck, 634 kmp_int32 gtid); 635 636 #if KMP_OS_LINUX && \ 637 (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) 638 639 #define __kmp_acquire_user_lock_with_checks(lck, gtid) \ 640 if (__kmp_user_lock_kind == lk_tas) { \ 641 if (__kmp_env_consistency_check) { \ 642 char const *const func = "omp_set_lock"; \ 643 if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) && \ 644 lck->tas.lk.depth_locked != -1) { \ 645 KMP_FATAL(LockNestableUsedAsSimple, func); \ 646 } \ 647 if ((gtid >= 0) && (lck->tas.lk.poll - 1 == gtid)) { \ 648 KMP_FATAL(LockIsAlreadyOwned, func); \ 649 } \ 650 } \ 651 if (lck->tas.lk.poll != 0 || \ 652 !__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)) { \ 653 kmp_uint32 spins; \ 654 kmp_uint64 time; \ 655 KMP_FSYNC_PREPARE(lck); \ 656 KMP_INIT_YIELD(spins); \ 657 KMP_INIT_BACKOFF(time); \ 658 do { \ 659 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time); \ 660 } while ( \ 661 lck->tas.lk.poll != 0 || \ 662 !__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)); \ 663 } \ 664 KMP_FSYNC_ACQUIRED(lck); \ 665 } else { \ 666 KMP_DEBUG_ASSERT(__kmp_acquire_user_lock_with_checks_ != NULL); \ 667 (*__kmp_acquire_user_lock_with_checks_)(lck, gtid); \ 668 } 669 670 #else 671 static inline int __kmp_acquire_user_lock_with_checks(kmp_user_lock_p lck, 672 kmp_int32 gtid) { 673 KMP_DEBUG_ASSERT(__kmp_acquire_user_lock_with_checks_ != NULL); 674 return (*__kmp_acquire_user_lock_with_checks_)(lck, gtid); 675 } 676 #endif 677 678 extern int (*__kmp_test_user_lock_with_checks_)(kmp_user_lock_p lck, 679 kmp_int32 gtid); 680 681 #if KMP_OS_LINUX && \ 682 (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) 683 684 #include "kmp_i18n.h" /* AC: KMP_FATAL definition */ 685 extern int __kmp_env_consistency_check; /* AC: copy from kmp.h here */ 686 static inline int __kmp_test_user_lock_with_checks(kmp_user_lock_p lck, 687 kmp_int32 gtid) { 688 if (__kmp_user_lock_kind == lk_tas) { 689 if (__kmp_env_consistency_check) { 690 char const *const func = "omp_test_lock"; 691 if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) && 692 lck->tas.lk.depth_locked != -1) { 693 KMP_FATAL(LockNestableUsedAsSimple, func); 694 } 695 } 696 return ((lck->tas.lk.poll == 0) && 697 __kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)); 698 } else { 699 KMP_DEBUG_ASSERT(__kmp_test_user_lock_with_checks_ != NULL); 700 return (*__kmp_test_user_lock_with_checks_)(lck, gtid); 701 } 702 } 703 #else 704 static inline int __kmp_test_user_lock_with_checks(kmp_user_lock_p lck, 705 kmp_int32 gtid) { 706 KMP_DEBUG_ASSERT(__kmp_test_user_lock_with_checks_ != NULL); 707 return (*__kmp_test_user_lock_with_checks_)(lck, gtid); 708 } 709 #endif 710 711 extern int (*__kmp_release_user_lock_with_checks_)(kmp_user_lock_p lck, 712 kmp_int32 gtid); 713 714 static inline void __kmp_release_user_lock_with_checks(kmp_user_lock_p lck, 715 kmp_int32 gtid) { 716 KMP_DEBUG_ASSERT(__kmp_release_user_lock_with_checks_ != NULL); 717 (*__kmp_release_user_lock_with_checks_)(lck, gtid); 718 } 719 720 extern void (*__kmp_init_user_lock_with_checks_)(kmp_user_lock_p lck); 721 722 static inline void __kmp_init_user_lock_with_checks(kmp_user_lock_p lck) { 723 KMP_DEBUG_ASSERT(__kmp_init_user_lock_with_checks_ != NULL); 724 (*__kmp_init_user_lock_with_checks_)(lck); 725 } 726 727 // We need a non-checking version of destroy lock for when the RTL is 728 // doing the cleanup as it can't always tell if the lock is nested or not. 729 extern void (*__kmp_destroy_user_lock_)(kmp_user_lock_p lck); 730 731 static inline void __kmp_destroy_user_lock(kmp_user_lock_p lck) { 732 KMP_DEBUG_ASSERT(__kmp_destroy_user_lock_ != NULL); 733 (*__kmp_destroy_user_lock_)(lck); 734 } 735 736 extern void (*__kmp_destroy_user_lock_with_checks_)(kmp_user_lock_p lck); 737 738 static inline void __kmp_destroy_user_lock_with_checks(kmp_user_lock_p lck) { 739 KMP_DEBUG_ASSERT(__kmp_destroy_user_lock_with_checks_ != NULL); 740 (*__kmp_destroy_user_lock_with_checks_)(lck); 741 } 742 743 extern int (*__kmp_acquire_nested_user_lock_with_checks_)(kmp_user_lock_p lck, 744 kmp_int32 gtid); 745 746 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64) 747 748 #define __kmp_acquire_nested_user_lock_with_checks(lck, gtid, depth) \ 749 if (__kmp_user_lock_kind == lk_tas) { \ 750 if (__kmp_env_consistency_check) { \ 751 char const *const func = "omp_set_nest_lock"; \ 752 if ((sizeof(kmp_tas_lock_t) <= OMP_NEST_LOCK_T_SIZE) && \ 753 lck->tas.lk.depth_locked == -1) { \ 754 KMP_FATAL(LockSimpleUsedAsNestable, func); \ 755 } \ 756 } \ 757 if (lck->tas.lk.poll - 1 == gtid) { \ 758 lck->tas.lk.depth_locked += 1; \ 759 *depth = KMP_LOCK_ACQUIRED_NEXT; \ 760 } else { \ 761 if ((lck->tas.lk.poll != 0) || \ 762 !__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)) { \ 763 kmp_uint32 spins; \ 764 kmp_uint64 time; \ 765 KMP_FSYNC_PREPARE(lck); \ 766 KMP_INIT_YIELD(spins); \ 767 KMP_INIT_BACKOFF(time); \ 768 do { \ 769 KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time); \ 770 } while ( \ 771 (lck->tas.lk.poll != 0) || \ 772 !__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)); \ 773 } \ 774 lck->tas.lk.depth_locked = 1; \ 775 *depth = KMP_LOCK_ACQUIRED_FIRST; \ 776 } \ 777 KMP_FSYNC_ACQUIRED(lck); \ 778 } else { \ 779 KMP_DEBUG_ASSERT(__kmp_acquire_nested_user_lock_with_checks_ != NULL); \ 780 *depth = (*__kmp_acquire_nested_user_lock_with_checks_)(lck, gtid); \ 781 } 782 783 #else 784 static inline void 785 __kmp_acquire_nested_user_lock_with_checks(kmp_user_lock_p lck, kmp_int32 gtid, 786 int *depth) { 787 KMP_DEBUG_ASSERT(__kmp_acquire_nested_user_lock_with_checks_ != NULL); 788 *depth = (*__kmp_acquire_nested_user_lock_with_checks_)(lck, gtid); 789 } 790 #endif 791 792 extern int (*__kmp_test_nested_user_lock_with_checks_)(kmp_user_lock_p lck, 793 kmp_int32 gtid); 794 795 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64) 796 static inline int __kmp_test_nested_user_lock_with_checks(kmp_user_lock_p lck, 797 kmp_int32 gtid) { 798 if (__kmp_user_lock_kind == lk_tas) { 799 int retval; 800 if (__kmp_env_consistency_check) { 801 char const *const func = "omp_test_nest_lock"; 802 if ((sizeof(kmp_tas_lock_t) <= OMP_NEST_LOCK_T_SIZE) && 803 lck->tas.lk.depth_locked == -1) { 804 KMP_FATAL(LockSimpleUsedAsNestable, func); 805 } 806 } 807 KMP_DEBUG_ASSERT(gtid >= 0); 808 if (lck->tas.lk.poll - 1 == 809 gtid) { /* __kmp_get_tas_lock_owner( lck ) == gtid */ 810 return ++lck->tas.lk.depth_locked; /* same owner, depth increased */ 811 } 812 retval = ((lck->tas.lk.poll == 0) && 813 __kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)); 814 if (retval) { 815 KMP_MB(); 816 lck->tas.lk.depth_locked = 1; 817 } 818 return retval; 819 } else { 820 KMP_DEBUG_ASSERT(__kmp_test_nested_user_lock_with_checks_ != NULL); 821 return (*__kmp_test_nested_user_lock_with_checks_)(lck, gtid); 822 } 823 } 824 #else 825 static inline int __kmp_test_nested_user_lock_with_checks(kmp_user_lock_p lck, 826 kmp_int32 gtid) { 827 KMP_DEBUG_ASSERT(__kmp_test_nested_user_lock_with_checks_ != NULL); 828 return (*__kmp_test_nested_user_lock_with_checks_)(lck, gtid); 829 } 830 #endif 831 832 extern int (*__kmp_release_nested_user_lock_with_checks_)(kmp_user_lock_p lck, 833 kmp_int32 gtid); 834 835 static inline int 836 __kmp_release_nested_user_lock_with_checks(kmp_user_lock_p lck, 837 kmp_int32 gtid) { 838 KMP_DEBUG_ASSERT(__kmp_release_nested_user_lock_with_checks_ != NULL); 839 return (*__kmp_release_nested_user_lock_with_checks_)(lck, gtid); 840 } 841 842 extern void (*__kmp_init_nested_user_lock_with_checks_)(kmp_user_lock_p lck); 843 844 static inline void 845 __kmp_init_nested_user_lock_with_checks(kmp_user_lock_p lck) { 846 KMP_DEBUG_ASSERT(__kmp_init_nested_user_lock_with_checks_ != NULL); 847 (*__kmp_init_nested_user_lock_with_checks_)(lck); 848 } 849 850 extern void (*__kmp_destroy_nested_user_lock_with_checks_)(kmp_user_lock_p lck); 851 852 static inline void 853 __kmp_destroy_nested_user_lock_with_checks(kmp_user_lock_p lck) { 854 KMP_DEBUG_ASSERT(__kmp_destroy_nested_user_lock_with_checks_ != NULL); 855 (*__kmp_destroy_nested_user_lock_with_checks_)(lck); 856 } 857 858 // user lock functions which do not necessarily exist for all lock kinds. 859 // 860 // The "set" functions usually have wrapper routines that check for a NULL set 861 // function pointer and call it if non-NULL. 862 // 863 // In some cases, it makes sense to have a "get" wrapper function check for a 864 // NULL get function pointer and return NULL / invalid value / error code if 865 // the function pointer is NULL. 866 // 867 // In other cases, the calling code really should differentiate between an 868 // unimplemented function and one that is implemented but returning NULL / 869 // invalid value. If this is the case, no get function wrapper exists. 870 871 extern int (*__kmp_is_user_lock_initialized_)(kmp_user_lock_p lck); 872 873 // no set function; fields set during local allocation 874 875 extern const ident_t *(*__kmp_get_user_lock_location_)(kmp_user_lock_p lck); 876 877 static inline const ident_t *__kmp_get_user_lock_location(kmp_user_lock_p lck) { 878 if (__kmp_get_user_lock_location_ != NULL) { 879 return (*__kmp_get_user_lock_location_)(lck); 880 } else { 881 return NULL; 882 } 883 } 884 885 extern void (*__kmp_set_user_lock_location_)(kmp_user_lock_p lck, 886 const ident_t *loc); 887 888 static inline void __kmp_set_user_lock_location(kmp_user_lock_p lck, 889 const ident_t *loc) { 890 if (__kmp_set_user_lock_location_ != NULL) { 891 (*__kmp_set_user_lock_location_)(lck, loc); 892 } 893 } 894 895 extern kmp_lock_flags_t (*__kmp_get_user_lock_flags_)(kmp_user_lock_p lck); 896 897 extern void (*__kmp_set_user_lock_flags_)(kmp_user_lock_p lck, 898 kmp_lock_flags_t flags); 899 900 static inline void __kmp_set_user_lock_flags(kmp_user_lock_p lck, 901 kmp_lock_flags_t flags) { 902 if (__kmp_set_user_lock_flags_ != NULL) { 903 (*__kmp_set_user_lock_flags_)(lck, flags); 904 } 905 } 906 907 // The function which sets up all of the vtbl pointers for kmp_user_lock_t. 908 extern void __kmp_set_user_lock_vptrs(kmp_lock_kind_t user_lock_kind); 909 910 // Macros for binding user lock functions. 911 #define KMP_BIND_USER_LOCK_TEMPLATE(nest, kind, suffix) \ 912 { \ 913 __kmp_acquire##nest##user_lock_with_checks_ = (int (*)( \ 914 kmp_user_lock_p, kmp_int32))__kmp_acquire##nest##kind##_##suffix; \ 915 __kmp_release##nest##user_lock_with_checks_ = (int (*)( \ 916 kmp_user_lock_p, kmp_int32))__kmp_release##nest##kind##_##suffix; \ 917 __kmp_test##nest##user_lock_with_checks_ = (int (*)( \ 918 kmp_user_lock_p, kmp_int32))__kmp_test##nest##kind##_##suffix; \ 919 __kmp_init##nest##user_lock_with_checks_ = \ 920 (void (*)(kmp_user_lock_p))__kmp_init##nest##kind##_##suffix; \ 921 __kmp_destroy##nest##user_lock_with_checks_ = \ 922 (void (*)(kmp_user_lock_p))__kmp_destroy##nest##kind##_##suffix; \ 923 } 924 925 #define KMP_BIND_USER_LOCK(kind) KMP_BIND_USER_LOCK_TEMPLATE(_, kind, lock) 926 #define KMP_BIND_USER_LOCK_WITH_CHECKS(kind) \ 927 KMP_BIND_USER_LOCK_TEMPLATE(_, kind, lock_with_checks) 928 #define KMP_BIND_NESTED_USER_LOCK(kind) \ 929 KMP_BIND_USER_LOCK_TEMPLATE(_nested_, kind, lock) 930 #define KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(kind) \ 931 KMP_BIND_USER_LOCK_TEMPLATE(_nested_, kind, lock_with_checks) 932 933 // User lock table & lock allocation 934 /* On 64-bit Linux* OS (and OS X*) GNU compiler allocates only 4 bytems memory 935 for lock variable, which is not enough to store a pointer, so we have to use 936 lock indexes instead of pointers and maintain lock table to map indexes to 937 pointers. 938 939 940 Note: The first element of the table is not a pointer to lock! It is a 941 pointer to previously allocated table (or NULL if it is the first table). 942 943 Usage: 944 945 if ( OMP_LOCK_T_SIZE < sizeof( <lock> ) ) { // or OMP_NEST_LOCK_T_SIZE 946 Lock table is fully utilized. User locks are indexes, so table is used on 947 user lock operation. 948 Note: it may be the case (lin_32) that we don't need to use a lock 949 table for regular locks, but do need the table for nested locks. 950 } 951 else { 952 Lock table initialized but not actually used. 953 } 954 */ 955 956 struct kmp_lock_table { 957 kmp_lock_index_t used; // Number of used elements 958 kmp_lock_index_t allocated; // Number of allocated elements 959 kmp_user_lock_p *table; // Lock table. 960 }; 961 962 typedef struct kmp_lock_table kmp_lock_table_t; 963 964 extern kmp_lock_table_t __kmp_user_lock_table; 965 extern kmp_user_lock_p __kmp_lock_pool; 966 967 struct kmp_block_of_locks { 968 struct kmp_block_of_locks *next_block; 969 void *locks; 970 }; 971 972 typedef struct kmp_block_of_locks kmp_block_of_locks_t; 973 974 extern kmp_block_of_locks_t *__kmp_lock_blocks; 975 extern int __kmp_num_locks_in_block; 976 977 extern kmp_user_lock_p __kmp_user_lock_allocate(void **user_lock, 978 kmp_int32 gtid, 979 kmp_lock_flags_t flags); 980 extern void __kmp_user_lock_free(void **user_lock, kmp_int32 gtid, 981 kmp_user_lock_p lck); 982 extern kmp_user_lock_p __kmp_lookup_user_lock(void **user_lock, 983 char const *func); 984 extern void __kmp_cleanup_user_locks(); 985 986 #define KMP_CHECK_USER_LOCK_INIT() \ 987 { \ 988 if (!TCR_4(__kmp_init_user_locks)) { \ 989 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); \ 990 if (!TCR_4(__kmp_init_user_locks)) { \ 991 TCW_4(__kmp_init_user_locks, TRUE); \ 992 } \ 993 __kmp_release_bootstrap_lock(&__kmp_initz_lock); \ 994 } \ 995 } 996 997 #endif // KMP_USE_DYNAMIC_LOCK 998 999 #undef KMP_PAD 1000 #undef KMP_GTID_DNE 1001 1002 #if KMP_USE_DYNAMIC_LOCK 1003 // KMP_USE_DYNAMIC_LOCK enables dynamic dispatch of lock functions without 1004 // breaking the current compatibility. Essential functionality of this new code 1005 // is dynamic dispatch, but it also implements (or enables implementation of) 1006 // hinted user lock and critical section which will be part of OMP 4.5 soon. 1007 // 1008 // Lock type can be decided at creation time (i.e., lock initialization), and 1009 // subsequent lock function call on the created lock object requires type 1010 // extraction and call through jump table using the extracted type. This type 1011 // information is stored in two different ways depending on the size of the lock 1012 // object, and we differentiate lock types by this size requirement - direct and 1013 // indirect locks. 1014 // 1015 // Direct locks: 1016 // A direct lock object fits into the space created by the compiler for an 1017 // omp_lock_t object, and TAS/Futex lock falls into this category. We use low 1018 // one byte of the lock object as the storage for the lock type, and appropriate 1019 // bit operation is required to access the data meaningful to the lock 1020 // algorithms. Also, to differentiate direct lock from indirect lock, 1 is 1021 // written to LSB of the lock object. The newly introduced "hle" lock is also a 1022 // direct lock. 1023 // 1024 // Indirect locks: 1025 // An indirect lock object requires more space than the compiler-generated 1026 // space, and it should be allocated from heap. Depending on the size of the 1027 // compiler-generated space for the lock (i.e., size of omp_lock_t), this 1028 // omp_lock_t object stores either the address of the heap-allocated indirect 1029 // lock (void * fits in the object) or an index to the indirect lock table entry 1030 // that holds the address. Ticket/Queuing/DRDPA/Adaptive lock falls into this 1031 // category, and the newly introduced "rtm" lock is also an indirect lock which 1032 // was implemented on top of the Queuing lock. When the omp_lock_t object holds 1033 // an index (not lock address), 0 is written to LSB to differentiate the lock 1034 // from a direct lock, and the remaining part is the actual index to the 1035 // indirect lock table. 1036 1037 #include <stdint.h> // for uintptr_t 1038 1039 // Shortcuts 1040 #define KMP_USE_INLINED_TAS \ 1041 (KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM)) && 1 1042 #define KMP_USE_INLINED_FUTEX KMP_USE_FUTEX && 0 1043 1044 // List of lock definitions; all nested locks are indirect locks. 1045 // hle lock is xchg lock prefixed with XACQUIRE/XRELEASE. 1046 // All nested locks are indirect lock types. 1047 #if KMP_USE_TSX 1048 #if KMP_USE_FUTEX 1049 #define KMP_FOREACH_D_LOCK(m, a) m(tas, a) m(futex, a) m(hle, a) m(rtm_spin, a) 1050 #define KMP_FOREACH_I_LOCK(m, a) \ 1051 m(ticket, a) m(queuing, a) m(adaptive, a) m(drdpa, a) m(rtm_queuing, a) \ 1052 m(nested_tas, a) m(nested_futex, a) m(nested_ticket, a) \ 1053 m(nested_queuing, a) m(nested_drdpa, a) 1054 #else 1055 #define KMP_FOREACH_D_LOCK(m, a) m(tas, a) m(hle, a) m(rtm_spin, a) 1056 #define KMP_FOREACH_I_LOCK(m, a) \ 1057 m(ticket, a) m(queuing, a) m(adaptive, a) m(drdpa, a) m(rtm_queuing, a) \ 1058 m(nested_tas, a) m(nested_ticket, a) m(nested_queuing, a) \ 1059 m(nested_drdpa, a) 1060 #endif // KMP_USE_FUTEX 1061 #define KMP_LAST_D_LOCK lockseq_rtm_spin 1062 #else 1063 #if KMP_USE_FUTEX 1064 #define KMP_FOREACH_D_LOCK(m, a) m(tas, a) m(futex, a) 1065 #define KMP_FOREACH_I_LOCK(m, a) \ 1066 m(ticket, a) m(queuing, a) m(drdpa, a) m(nested_tas, a) m(nested_futex, a) \ 1067 m(nested_ticket, a) m(nested_queuing, a) m(nested_drdpa, a) 1068 #define KMP_LAST_D_LOCK lockseq_futex 1069 #else 1070 #define KMP_FOREACH_D_LOCK(m, a) m(tas, a) 1071 #define KMP_FOREACH_I_LOCK(m, a) \ 1072 m(ticket, a) m(queuing, a) m(drdpa, a) m(nested_tas, a) m(nested_ticket, a) \ 1073 m(nested_queuing, a) m(nested_drdpa, a) 1074 #define KMP_LAST_D_LOCK lockseq_tas 1075 #endif // KMP_USE_FUTEX 1076 #endif // KMP_USE_TSX 1077 1078 // Information used in dynamic dispatch 1079 #define KMP_LOCK_SHIFT \ 1080 8 // number of low bits to be used as tag for direct locks 1081 #define KMP_FIRST_D_LOCK lockseq_tas 1082 #define KMP_FIRST_I_LOCK lockseq_ticket 1083 #define KMP_LAST_I_LOCK lockseq_nested_drdpa 1084 #define KMP_NUM_I_LOCKS \ 1085 (locktag_nested_drdpa + 1) // number of indirect lock types 1086 1087 // Base type for dynamic locks. 1088 typedef kmp_uint32 kmp_dyna_lock_t; 1089 1090 // Lock sequence that enumerates all lock kinds. Always make this enumeration 1091 // consistent with kmp_lockseq_t in the include directory. 1092 typedef enum { 1093 lockseq_indirect = 0, 1094 #define expand_seq(l, a) lockseq_##l, 1095 KMP_FOREACH_D_LOCK(expand_seq, 0) KMP_FOREACH_I_LOCK(expand_seq, 0) 1096 #undef expand_seq 1097 } kmp_dyna_lockseq_t; 1098 1099 // Enumerates indirect lock tags. 1100 typedef enum { 1101 #define expand_tag(l, a) locktag_##l, 1102 KMP_FOREACH_I_LOCK(expand_tag, 0) 1103 #undef expand_tag 1104 } kmp_indirect_locktag_t; 1105 1106 // Utility macros that extract information from lock sequences. 1107 #define KMP_IS_D_LOCK(seq) \ 1108 ((seq) >= KMP_FIRST_D_LOCK && (seq) <= KMP_LAST_D_LOCK) 1109 #define KMP_IS_I_LOCK(seq) \ 1110 ((seq) >= KMP_FIRST_I_LOCK && (seq) <= KMP_LAST_I_LOCK) 1111 #define KMP_GET_I_TAG(seq) (kmp_indirect_locktag_t)((seq)-KMP_FIRST_I_LOCK) 1112 #define KMP_GET_D_TAG(seq) ((seq) << 1 | 1) 1113 1114 // Enumerates direct lock tags starting from indirect tag. 1115 typedef enum { 1116 #define expand_tag(l, a) locktag_##l = KMP_GET_D_TAG(lockseq_##l), 1117 KMP_FOREACH_D_LOCK(expand_tag, 0) 1118 #undef expand_tag 1119 } kmp_direct_locktag_t; 1120 1121 // Indirect lock type 1122 typedef struct { 1123 kmp_user_lock_p lock; 1124 kmp_indirect_locktag_t type; 1125 } kmp_indirect_lock_t; 1126 1127 // Function tables for direct locks. Set/unset/test differentiate functions 1128 // with/without consistency checking. 1129 extern void (*__kmp_direct_init[])(kmp_dyna_lock_t *, kmp_dyna_lockseq_t); 1130 extern void (**__kmp_direct_destroy)(kmp_dyna_lock_t *); 1131 extern int (**__kmp_direct_set)(kmp_dyna_lock_t *, kmp_int32); 1132 extern int (**__kmp_direct_unset)(kmp_dyna_lock_t *, kmp_int32); 1133 extern int (**__kmp_direct_test)(kmp_dyna_lock_t *, kmp_int32); 1134 1135 // Function tables for indirect locks. Set/unset/test differentiate functions 1136 // with/without consistency checking. 1137 extern void (*__kmp_indirect_init[])(kmp_user_lock_p); 1138 extern void (**__kmp_indirect_destroy)(kmp_user_lock_p); 1139 extern int (**__kmp_indirect_set)(kmp_user_lock_p, kmp_int32); 1140 extern int (**__kmp_indirect_unset)(kmp_user_lock_p, kmp_int32); 1141 extern int (**__kmp_indirect_test)(kmp_user_lock_p, kmp_int32); 1142 1143 // Extracts direct lock tag from a user lock pointer 1144 #define KMP_EXTRACT_D_TAG(l) \ 1145 (*((kmp_dyna_lock_t *)(l)) & ((1 << KMP_LOCK_SHIFT) - 1) & \ 1146 -(*((kmp_dyna_lock_t *)(l)) & 1)) 1147 1148 // Extracts indirect lock index from a user lock pointer 1149 #define KMP_EXTRACT_I_INDEX(l) (*(kmp_lock_index_t *)(l) >> 1) 1150 1151 // Returns function pointer to the direct lock function with l (kmp_dyna_lock_t 1152 // *) and op (operation type). 1153 #define KMP_D_LOCK_FUNC(l, op) __kmp_direct_##op[KMP_EXTRACT_D_TAG(l)] 1154 1155 // Returns function pointer to the indirect lock function with l 1156 // (kmp_indirect_lock_t *) and op (operation type). 1157 #define KMP_I_LOCK_FUNC(l, op) \ 1158 __kmp_indirect_##op[((kmp_indirect_lock_t *)(l))->type] 1159 1160 // Initializes a direct lock with the given lock pointer and lock sequence. 1161 #define KMP_INIT_D_LOCK(l, seq) \ 1162 __kmp_direct_init[KMP_GET_D_TAG(seq)]((kmp_dyna_lock_t *)l, seq) 1163 1164 // Initializes an indirect lock with the given lock pointer and lock sequence. 1165 #define KMP_INIT_I_LOCK(l, seq) \ 1166 __kmp_direct_init[0]((kmp_dyna_lock_t *)(l), seq) 1167 1168 // Returns "free" lock value for the given lock type. 1169 #define KMP_LOCK_FREE(type) (locktag_##type) 1170 1171 // Returns "busy" lock value for the given lock teyp. 1172 #define KMP_LOCK_BUSY(v, type) ((v) << KMP_LOCK_SHIFT | locktag_##type) 1173 1174 // Returns lock value after removing (shifting) lock tag. 1175 #define KMP_LOCK_STRIP(v) ((v) >> KMP_LOCK_SHIFT) 1176 1177 // Initializes global states and data structures for managing dynamic user 1178 // locks. 1179 extern void __kmp_init_dynamic_user_locks(); 1180 1181 // Allocates and returns an indirect lock with the given indirect lock tag. 1182 extern kmp_indirect_lock_t * 1183 __kmp_allocate_indirect_lock(void **, kmp_int32, kmp_indirect_locktag_t); 1184 1185 // Cleans up global states and data structures for managing dynamic user locks. 1186 extern void __kmp_cleanup_indirect_user_locks(); 1187 1188 // Default user lock sequence when not using hinted locks. 1189 extern kmp_dyna_lockseq_t __kmp_user_lock_seq; 1190 1191 // Jump table for "set lock location", available only for indirect locks. 1192 extern void (*__kmp_indirect_set_location[KMP_NUM_I_LOCKS])(kmp_user_lock_p, 1193 const ident_t *); 1194 #define KMP_SET_I_LOCK_LOCATION(lck, loc) \ 1195 { \ 1196 if (__kmp_indirect_set_location[(lck)->type] != NULL) \ 1197 __kmp_indirect_set_location[(lck)->type]((lck)->lock, loc); \ 1198 } 1199 1200 // Jump table for "set lock flags", available only for indirect locks. 1201 extern void (*__kmp_indirect_set_flags[KMP_NUM_I_LOCKS])(kmp_user_lock_p, 1202 kmp_lock_flags_t); 1203 #define KMP_SET_I_LOCK_FLAGS(lck, flag) \ 1204 { \ 1205 if (__kmp_indirect_set_flags[(lck)->type] != NULL) \ 1206 __kmp_indirect_set_flags[(lck)->type]((lck)->lock, flag); \ 1207 } 1208 1209 // Jump table for "get lock location", available only for indirect locks. 1210 extern const ident_t *(*__kmp_indirect_get_location[KMP_NUM_I_LOCKS])( 1211 kmp_user_lock_p); 1212 #define KMP_GET_I_LOCK_LOCATION(lck) \ 1213 (__kmp_indirect_get_location[(lck)->type] != NULL \ 1214 ? __kmp_indirect_get_location[(lck)->type]((lck)->lock) \ 1215 : NULL) 1216 1217 // Jump table for "get lock flags", available only for indirect locks. 1218 extern kmp_lock_flags_t (*__kmp_indirect_get_flags[KMP_NUM_I_LOCKS])( 1219 kmp_user_lock_p); 1220 #define KMP_GET_I_LOCK_FLAGS(lck) \ 1221 (__kmp_indirect_get_flags[(lck)->type] != NULL \ 1222 ? __kmp_indirect_get_flags[(lck)->type]((lck)->lock) \ 1223 : NULL) 1224 1225 // number of kmp_indirect_lock_t objects to be allocated together 1226 #define KMP_I_LOCK_CHUNK 1024 1227 // Keep at a power of 2 since it is used in multiplication & division 1228 KMP_BUILD_ASSERT(KMP_I_LOCK_CHUNK % 2 == 0); 1229 // number of row entries in the initial lock table 1230 #define KMP_I_LOCK_TABLE_INIT_NROW_PTRS 8 1231 1232 // Lock table for indirect locks. 1233 typedef struct kmp_indirect_lock_table { 1234 kmp_indirect_lock_t **table; // blocks of indirect locks allocated 1235 kmp_uint32 nrow_ptrs; // number *table pointer entries in table 1236 kmp_lock_index_t next; // index to the next lock to be allocated 1237 struct kmp_indirect_lock_table *next_table; 1238 } kmp_indirect_lock_table_t; 1239 1240 extern kmp_indirect_lock_table_t __kmp_i_lock_table; 1241 1242 // Returns the indirect lock associated with the given index. 1243 // Returns nullptr if no lock at given index 1244 static inline kmp_indirect_lock_t *__kmp_get_i_lock(kmp_lock_index_t idx) { 1245 kmp_indirect_lock_table_t *lock_table = &__kmp_i_lock_table; 1246 while (lock_table) { 1247 kmp_lock_index_t max_locks = lock_table->nrow_ptrs * KMP_I_LOCK_CHUNK; 1248 if (idx < max_locks) { 1249 kmp_lock_index_t row = idx / KMP_I_LOCK_CHUNK; 1250 kmp_lock_index_t col = idx % KMP_I_LOCK_CHUNK; 1251 if (!lock_table->table[row] || idx >= lock_table->next) 1252 break; 1253 return &lock_table->table[row][col]; 1254 } 1255 idx -= max_locks; 1256 lock_table = lock_table->next_table; 1257 } 1258 return nullptr; 1259 } 1260 1261 // Number of locks in a lock block, which is fixed to "1" now. 1262 // TODO: No lock block implementation now. If we do support, we need to manage 1263 // lock block data structure for each indirect lock type. 1264 extern int __kmp_num_locks_in_block; 1265 1266 // Fast lock table lookup without consistency checking 1267 #define KMP_LOOKUP_I_LOCK(l) \ 1268 ((OMP_LOCK_T_SIZE < sizeof(void *)) \ 1269 ? __kmp_get_i_lock(KMP_EXTRACT_I_INDEX(l)) \ 1270 : *((kmp_indirect_lock_t **)(l))) 1271 1272 // Used once in kmp_error.cpp 1273 extern kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p, kmp_uint32); 1274 1275 #else // KMP_USE_DYNAMIC_LOCK 1276 1277 #define KMP_LOCK_BUSY(v, type) (v) 1278 #define KMP_LOCK_FREE(type) 0 1279 #define KMP_LOCK_STRIP(v) (v) 1280 1281 #endif // KMP_USE_DYNAMIC_LOCK 1282 1283 // data structure for using backoff within spin locks. 1284 typedef struct { 1285 kmp_uint32 step; // current step 1286 kmp_uint32 max_backoff; // upper bound of outer delay loop 1287 kmp_uint32 min_tick; // size of inner delay loop in ticks (machine-dependent) 1288 } kmp_backoff_t; 1289 1290 // Runtime's default backoff parameters 1291 extern kmp_backoff_t __kmp_spin_backoff_params; 1292 1293 // Backoff function 1294 extern void __kmp_spin_backoff(kmp_backoff_t *); 1295 1296 #ifdef __cplusplus 1297 } // extern "C" 1298 #endif // __cplusplus 1299 1300 #endif /* KMP_LOCK_H */ 1301