21 #include "kmp_wait_release.h"
22 #include "kmp_wrapper_getpid.h"
24 #include "tsan_annotations.h"
27 #include <sys/syscall.h>
45 void __kmp_validate_locks(
void) {
50 x = ~((kmp_uint32)0) - 2;
53 for (i = 0; i < 8; ++i, ++x, ++y) {
54 kmp_uint32 z = (x - y);
58 KMP_ASSERT(offsetof(kmp_base_queuing_lock, tail_id) % 8 == 0);
72 static kmp_int32 __kmp_get_tas_lock_owner(kmp_tas_lock_t *lck) {
73 return KMP_LOCK_STRIP(KMP_ATOMIC_LD_RLX(&lck->lk.poll)) - 1;
76 static inline bool __kmp_is_tas_lock_nestable(kmp_tas_lock_t *lck) {
77 return lck->lk.depth_locked != -1;
80 __forceinline
static int
81 __kmp_acquire_tas_lock_timed_template(kmp_tas_lock_t *lck, kmp_int32 gtid) {
84 #ifdef USE_LOCK_PROFILE
85 kmp_uint32 curr = KMP_LOCK_STRIP(lck->lk.poll);
86 if ((curr != 0) && (curr != gtid + 1))
87 __kmp_printf(
"LOCK CONTENTION: %p\n", lck);
91 kmp_int32 tas_free = KMP_LOCK_FREE(tas);
92 kmp_int32 tas_busy = KMP_LOCK_BUSY(gtid + 1, tas);
94 if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == tas_free &&
95 __kmp_atomic_compare_store_acq(&lck->lk.poll, tas_free, tas_busy)) {
96 KMP_FSYNC_ACQUIRED(lck);
97 return KMP_LOCK_ACQUIRED_FIRST;
101 KMP_FSYNC_PREPARE(lck);
102 KMP_INIT_YIELD(spins);
103 kmp_backoff_t backoff = __kmp_spin_backoff_params;
105 __kmp_spin_backoff(&backoff);
106 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
107 }
while (KMP_ATOMIC_LD_RLX(&lck->lk.poll) != tas_free ||
108 !__kmp_atomic_compare_store_acq(&lck->lk.poll, tas_free, tas_busy));
109 KMP_FSYNC_ACQUIRED(lck);
110 return KMP_LOCK_ACQUIRED_FIRST;
113 int __kmp_acquire_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
114 int retval = __kmp_acquire_tas_lock_timed_template(lck, gtid);
115 ANNOTATE_TAS_ACQUIRED(lck);
119 static int __kmp_acquire_tas_lock_with_checks(kmp_tas_lock_t *lck,
121 char const *
const func =
"omp_set_lock";
122 if ((
sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
123 __kmp_is_tas_lock_nestable(lck)) {
124 KMP_FATAL(LockNestableUsedAsSimple, func);
126 if ((gtid >= 0) && (__kmp_get_tas_lock_owner(lck) == gtid)) {
127 KMP_FATAL(LockIsAlreadyOwned, func);
129 return __kmp_acquire_tas_lock(lck, gtid);
132 int __kmp_test_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
133 kmp_int32 tas_free = KMP_LOCK_FREE(tas);
134 kmp_int32 tas_busy = KMP_LOCK_BUSY(gtid + 1, tas);
135 if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == tas_free &&
136 __kmp_atomic_compare_store_acq(&lck->lk.poll, tas_free, tas_busy)) {
137 KMP_FSYNC_ACQUIRED(lck);
143 static int __kmp_test_tas_lock_with_checks(kmp_tas_lock_t *lck,
145 char const *
const func =
"omp_test_lock";
146 if ((
sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
147 __kmp_is_tas_lock_nestable(lck)) {
148 KMP_FATAL(LockNestableUsedAsSimple, func);
150 return __kmp_test_tas_lock(lck, gtid);
153 int __kmp_release_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
156 KMP_FSYNC_RELEASING(lck);
157 ANNOTATE_TAS_RELEASED(lck);
158 KMP_ATOMIC_ST_REL(&lck->lk.poll, KMP_LOCK_FREE(tas));
162 return KMP_LOCK_RELEASED;
165 static int __kmp_release_tas_lock_with_checks(kmp_tas_lock_t *lck,
167 char const *
const func =
"omp_unset_lock";
169 if ((
sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
170 __kmp_is_tas_lock_nestable(lck)) {
171 KMP_FATAL(LockNestableUsedAsSimple, func);
173 if (__kmp_get_tas_lock_owner(lck) == -1) {
174 KMP_FATAL(LockUnsettingFree, func);
176 if ((gtid >= 0) && (__kmp_get_tas_lock_owner(lck) >= 0) &&
177 (__kmp_get_tas_lock_owner(lck) != gtid)) {
178 KMP_FATAL(LockUnsettingSetByAnother, func);
180 return __kmp_release_tas_lock(lck, gtid);
183 void __kmp_init_tas_lock(kmp_tas_lock_t *lck) {
184 lck->lk.poll = KMP_LOCK_FREE(tas);
187 void __kmp_destroy_tas_lock(kmp_tas_lock_t *lck) { lck->lk.poll = 0; }
189 static void __kmp_destroy_tas_lock_with_checks(kmp_tas_lock_t *lck) {
190 char const *
const func =
"omp_destroy_lock";
191 if ((
sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
192 __kmp_is_tas_lock_nestable(lck)) {
193 KMP_FATAL(LockNestableUsedAsSimple, func);
195 if (__kmp_get_tas_lock_owner(lck) != -1) {
196 KMP_FATAL(LockStillOwned, func);
198 __kmp_destroy_tas_lock(lck);
203 int __kmp_acquire_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
204 KMP_DEBUG_ASSERT(gtid >= 0);
206 if (__kmp_get_tas_lock_owner(lck) == gtid) {
207 lck->lk.depth_locked += 1;
208 return KMP_LOCK_ACQUIRED_NEXT;
210 __kmp_acquire_tas_lock_timed_template(lck, gtid);
211 ANNOTATE_TAS_ACQUIRED(lck);
212 lck->lk.depth_locked = 1;
213 return KMP_LOCK_ACQUIRED_FIRST;
217 static int __kmp_acquire_nested_tas_lock_with_checks(kmp_tas_lock_t *lck,
219 char const *
const func =
"omp_set_nest_lock";
220 if (!__kmp_is_tas_lock_nestable(lck)) {
221 KMP_FATAL(LockSimpleUsedAsNestable, func);
223 return __kmp_acquire_nested_tas_lock(lck, gtid);
226 int __kmp_test_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
229 KMP_DEBUG_ASSERT(gtid >= 0);
231 if (__kmp_get_tas_lock_owner(lck) == gtid) {
232 retval = ++lck->lk.depth_locked;
233 }
else if (!__kmp_test_tas_lock(lck, gtid)) {
237 retval = lck->lk.depth_locked = 1;
242 static int __kmp_test_nested_tas_lock_with_checks(kmp_tas_lock_t *lck,
244 char const *
const func =
"omp_test_nest_lock";
245 if (!__kmp_is_tas_lock_nestable(lck)) {
246 KMP_FATAL(LockSimpleUsedAsNestable, func);
248 return __kmp_test_nested_tas_lock(lck, gtid);
251 int __kmp_release_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
252 KMP_DEBUG_ASSERT(gtid >= 0);
255 if (--(lck->lk.depth_locked) == 0) {
256 __kmp_release_tas_lock(lck, gtid);
257 return KMP_LOCK_RELEASED;
259 return KMP_LOCK_STILL_HELD;
262 static int __kmp_release_nested_tas_lock_with_checks(kmp_tas_lock_t *lck,
264 char const *
const func =
"omp_unset_nest_lock";
266 if (!__kmp_is_tas_lock_nestable(lck)) {
267 KMP_FATAL(LockSimpleUsedAsNestable, func);
269 if (__kmp_get_tas_lock_owner(lck) == -1) {
270 KMP_FATAL(LockUnsettingFree, func);
272 if (__kmp_get_tas_lock_owner(lck) != gtid) {
273 KMP_FATAL(LockUnsettingSetByAnother, func);
275 return __kmp_release_nested_tas_lock(lck, gtid);
278 void __kmp_init_nested_tas_lock(kmp_tas_lock_t *lck) {
279 __kmp_init_tas_lock(lck);
280 lck->lk.depth_locked = 0;
283 void __kmp_destroy_nested_tas_lock(kmp_tas_lock_t *lck) {
284 __kmp_destroy_tas_lock(lck);
285 lck->lk.depth_locked = 0;
288 static void __kmp_destroy_nested_tas_lock_with_checks(kmp_tas_lock_t *lck) {
289 char const *
const func =
"omp_destroy_nest_lock";
290 if (!__kmp_is_tas_lock_nestable(lck)) {
291 KMP_FATAL(LockSimpleUsedAsNestable, func);
293 if (__kmp_get_tas_lock_owner(lck) != -1) {
294 KMP_FATAL(LockStillOwned, func);
296 __kmp_destroy_nested_tas_lock(lck);
309 static kmp_int32 __kmp_get_futex_lock_owner(kmp_futex_lock_t *lck) {
310 return KMP_LOCK_STRIP((TCR_4(lck->lk.poll) >> 1)) - 1;
313 static inline bool __kmp_is_futex_lock_nestable(kmp_futex_lock_t *lck) {
314 return lck->lk.depth_locked != -1;
317 __forceinline
static int
318 __kmp_acquire_futex_lock_timed_template(kmp_futex_lock_t *lck, kmp_int32 gtid) {
319 kmp_int32 gtid_code = (gtid + 1) << 1;
323 #ifdef USE_LOCK_PROFILE
324 kmp_uint32 curr = KMP_LOCK_STRIP(TCR_4(lck->lk.poll));
325 if ((curr != 0) && (curr != gtid_code))
326 __kmp_printf(
"LOCK CONTENTION: %p\n", lck);
330 KMP_FSYNC_PREPARE(lck);
331 KA_TRACE(1000, (
"__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d entering\n",
332 lck, lck->lk.poll, gtid));
336 while ((poll_val = KMP_COMPARE_AND_STORE_RET32(
337 &(lck->lk.poll), KMP_LOCK_FREE(futex),
338 KMP_LOCK_BUSY(gtid_code, futex))) != KMP_LOCK_FREE(futex)) {
340 kmp_int32 cond = KMP_LOCK_STRIP(poll_val) & 1;
343 (
"__kmp_acquire_futex_lock: lck:%p, T#%d poll_val = 0x%x cond = 0x%x\n",
344 lck, gtid, poll_val, cond));
355 if (!KMP_COMPARE_AND_STORE_REL32(&(lck->lk.poll), poll_val,
356 poll_val | KMP_LOCK_BUSY(1, futex))) {
359 (
"__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d can't set bit 0\n",
360 lck, lck->lk.poll, gtid));
363 poll_val |= KMP_LOCK_BUSY(1, futex);
366 (
"__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d bit 0 set\n", lck,
367 lck->lk.poll, gtid));
372 (
"__kmp_acquire_futex_lock: lck:%p, T#%d before futex_wait(0x%x)\n",
373 lck, gtid, poll_val));
376 if ((rc = syscall(__NR_futex, &(lck->lk.poll), FUTEX_WAIT, poll_val, NULL,
378 KA_TRACE(1000, (
"__kmp_acquire_futex_lock: lck:%p, T#%d futex_wait(0x%x) "
379 "failed (rc=%ld errno=%d)\n",
380 lck, gtid, poll_val, rc, errno));
385 (
"__kmp_acquire_futex_lock: lck:%p, T#%d after futex_wait(0x%x)\n",
386 lck, gtid, poll_val));
393 KMP_FSYNC_ACQUIRED(lck);
394 KA_TRACE(1000, (
"__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d exiting\n", lck,
395 lck->lk.poll, gtid));
396 return KMP_LOCK_ACQUIRED_FIRST;
399 int __kmp_acquire_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
400 int retval = __kmp_acquire_futex_lock_timed_template(lck, gtid);
401 ANNOTATE_FUTEX_ACQUIRED(lck);
405 static int __kmp_acquire_futex_lock_with_checks(kmp_futex_lock_t *lck,
407 char const *
const func =
"omp_set_lock";
408 if ((
sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
409 __kmp_is_futex_lock_nestable(lck)) {
410 KMP_FATAL(LockNestableUsedAsSimple, func);
412 if ((gtid >= 0) && (__kmp_get_futex_lock_owner(lck) == gtid)) {
413 KMP_FATAL(LockIsAlreadyOwned, func);
415 return __kmp_acquire_futex_lock(lck, gtid);
418 int __kmp_test_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
419 if (KMP_COMPARE_AND_STORE_ACQ32(&(lck->lk.poll), KMP_LOCK_FREE(futex),
420 KMP_LOCK_BUSY((gtid + 1) << 1, futex))) {
421 KMP_FSYNC_ACQUIRED(lck);
427 static int __kmp_test_futex_lock_with_checks(kmp_futex_lock_t *lck,
429 char const *
const func =
"omp_test_lock";
430 if ((
sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
431 __kmp_is_futex_lock_nestable(lck)) {
432 KMP_FATAL(LockNestableUsedAsSimple, func);
434 return __kmp_test_futex_lock(lck, gtid);
437 int __kmp_release_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
440 KA_TRACE(1000, (
"__kmp_release_futex_lock: lck:%p(0x%x), T#%d entering\n",
441 lck, lck->lk.poll, gtid));
443 KMP_FSYNC_RELEASING(lck);
444 ANNOTATE_FUTEX_RELEASED(lck);
446 kmp_int32 poll_val = KMP_XCHG_FIXED32(&(lck->lk.poll), KMP_LOCK_FREE(futex));
449 (
"__kmp_release_futex_lock: lck:%p, T#%d released poll_val = 0x%x\n",
450 lck, gtid, poll_val));
452 if (KMP_LOCK_STRIP(poll_val) & 1) {
454 (
"__kmp_release_futex_lock: lck:%p, T#%d futex_wake 1 thread\n",
456 syscall(__NR_futex, &(lck->lk.poll), FUTEX_WAKE, KMP_LOCK_BUSY(1, futex),
462 KA_TRACE(1000, (
"__kmp_release_futex_lock: lck:%p(0x%x), T#%d exiting\n", lck,
463 lck->lk.poll, gtid));
466 return KMP_LOCK_RELEASED;
469 static int __kmp_release_futex_lock_with_checks(kmp_futex_lock_t *lck,
471 char const *
const func =
"omp_unset_lock";
473 if ((
sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
474 __kmp_is_futex_lock_nestable(lck)) {
475 KMP_FATAL(LockNestableUsedAsSimple, func);
477 if (__kmp_get_futex_lock_owner(lck) == -1) {
478 KMP_FATAL(LockUnsettingFree, func);
480 if ((gtid >= 0) && (__kmp_get_futex_lock_owner(lck) >= 0) &&
481 (__kmp_get_futex_lock_owner(lck) != gtid)) {
482 KMP_FATAL(LockUnsettingSetByAnother, func);
484 return __kmp_release_futex_lock(lck, gtid);
487 void __kmp_init_futex_lock(kmp_futex_lock_t *lck) {
488 TCW_4(lck->lk.poll, KMP_LOCK_FREE(futex));
491 void __kmp_destroy_futex_lock(kmp_futex_lock_t *lck) { lck->lk.poll = 0; }
493 static void __kmp_destroy_futex_lock_with_checks(kmp_futex_lock_t *lck) {
494 char const *
const func =
"omp_destroy_lock";
495 if ((
sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
496 __kmp_is_futex_lock_nestable(lck)) {
497 KMP_FATAL(LockNestableUsedAsSimple, func);
499 if (__kmp_get_futex_lock_owner(lck) != -1) {
500 KMP_FATAL(LockStillOwned, func);
502 __kmp_destroy_futex_lock(lck);
507 int __kmp_acquire_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
508 KMP_DEBUG_ASSERT(gtid >= 0);
510 if (__kmp_get_futex_lock_owner(lck) == gtid) {
511 lck->lk.depth_locked += 1;
512 return KMP_LOCK_ACQUIRED_NEXT;
514 __kmp_acquire_futex_lock_timed_template(lck, gtid);
515 ANNOTATE_FUTEX_ACQUIRED(lck);
516 lck->lk.depth_locked = 1;
517 return KMP_LOCK_ACQUIRED_FIRST;
521 static int __kmp_acquire_nested_futex_lock_with_checks(kmp_futex_lock_t *lck,
523 char const *
const func =
"omp_set_nest_lock";
524 if (!__kmp_is_futex_lock_nestable(lck)) {
525 KMP_FATAL(LockSimpleUsedAsNestable, func);
527 return __kmp_acquire_nested_futex_lock(lck, gtid);
530 int __kmp_test_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
533 KMP_DEBUG_ASSERT(gtid >= 0);
535 if (__kmp_get_futex_lock_owner(lck) == gtid) {
536 retval = ++lck->lk.depth_locked;
537 }
else if (!__kmp_test_futex_lock(lck, gtid)) {
541 retval = lck->lk.depth_locked = 1;
546 static int __kmp_test_nested_futex_lock_with_checks(kmp_futex_lock_t *lck,
548 char const *
const func =
"omp_test_nest_lock";
549 if (!__kmp_is_futex_lock_nestable(lck)) {
550 KMP_FATAL(LockSimpleUsedAsNestable, func);
552 return __kmp_test_nested_futex_lock(lck, gtid);
555 int __kmp_release_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
556 KMP_DEBUG_ASSERT(gtid >= 0);
559 if (--(lck->lk.depth_locked) == 0) {
560 __kmp_release_futex_lock(lck, gtid);
561 return KMP_LOCK_RELEASED;
563 return KMP_LOCK_STILL_HELD;
566 static int __kmp_release_nested_futex_lock_with_checks(kmp_futex_lock_t *lck,
568 char const *
const func =
"omp_unset_nest_lock";
570 if (!__kmp_is_futex_lock_nestable(lck)) {
571 KMP_FATAL(LockSimpleUsedAsNestable, func);
573 if (__kmp_get_futex_lock_owner(lck) == -1) {
574 KMP_FATAL(LockUnsettingFree, func);
576 if (__kmp_get_futex_lock_owner(lck) != gtid) {
577 KMP_FATAL(LockUnsettingSetByAnother, func);
579 return __kmp_release_nested_futex_lock(lck, gtid);
582 void __kmp_init_nested_futex_lock(kmp_futex_lock_t *lck) {
583 __kmp_init_futex_lock(lck);
584 lck->lk.depth_locked = 0;
587 void __kmp_destroy_nested_futex_lock(kmp_futex_lock_t *lck) {
588 __kmp_destroy_futex_lock(lck);
589 lck->lk.depth_locked = 0;
592 static void __kmp_destroy_nested_futex_lock_with_checks(kmp_futex_lock_t *lck) {
593 char const *
const func =
"omp_destroy_nest_lock";
594 if (!__kmp_is_futex_lock_nestable(lck)) {
595 KMP_FATAL(LockSimpleUsedAsNestable, func);
597 if (__kmp_get_futex_lock_owner(lck) != -1) {
598 KMP_FATAL(LockStillOwned, func);
600 __kmp_destroy_nested_futex_lock(lck);
608 static kmp_int32 __kmp_get_ticket_lock_owner(kmp_ticket_lock_t *lck) {
609 return std::atomic_load_explicit(&lck->lk.owner_id,
610 std::memory_order_relaxed) -
614 static inline bool __kmp_is_ticket_lock_nestable(kmp_ticket_lock_t *lck) {
615 return std::atomic_load_explicit(&lck->lk.depth_locked,
616 std::memory_order_relaxed) != -1;
619 static kmp_uint32 __kmp_bakery_check(
void *now_serving, kmp_uint32 my_ticket) {
620 return std::atomic_load_explicit((std::atomic<unsigned> *)now_serving,
621 std::memory_order_acquire) == my_ticket;
624 __forceinline
static int
625 __kmp_acquire_ticket_lock_timed_template(kmp_ticket_lock_t *lck,
627 kmp_uint32 my_ticket = std::atomic_fetch_add_explicit(
628 &lck->lk.next_ticket, 1U, std::memory_order_relaxed);
630 #ifdef USE_LOCK_PROFILE
631 if (std::atomic_load_explicit(&lck->lk.now_serving,
632 std::memory_order_relaxed) != my_ticket)
633 __kmp_printf(
"LOCK CONTENTION: %p\n", lck);
637 if (std::atomic_load_explicit(&lck->lk.now_serving,
638 std::memory_order_acquire) == my_ticket) {
639 return KMP_LOCK_ACQUIRED_FIRST;
641 KMP_WAIT_PTR(&lck->lk.now_serving, my_ticket, __kmp_bakery_check, lck);
642 return KMP_LOCK_ACQUIRED_FIRST;
645 int __kmp_acquire_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
646 int retval = __kmp_acquire_ticket_lock_timed_template(lck, gtid);
647 ANNOTATE_TICKET_ACQUIRED(lck);
651 static int __kmp_acquire_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
653 char const *
const func =
"omp_set_lock";
655 if (!std::atomic_load_explicit(&lck->lk.initialized,
656 std::memory_order_relaxed)) {
657 KMP_FATAL(LockIsUninitialized, func);
659 if (lck->lk.self != lck) {
660 KMP_FATAL(LockIsUninitialized, func);
662 if (__kmp_is_ticket_lock_nestable(lck)) {
663 KMP_FATAL(LockNestableUsedAsSimple, func);
665 if ((gtid >= 0) && (__kmp_get_ticket_lock_owner(lck) == gtid)) {
666 KMP_FATAL(LockIsAlreadyOwned, func);
669 __kmp_acquire_ticket_lock(lck, gtid);
671 std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
672 std::memory_order_relaxed);
673 return KMP_LOCK_ACQUIRED_FIRST;
676 int __kmp_test_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
677 kmp_uint32 my_ticket = std::atomic_load_explicit(&lck->lk.next_ticket,
678 std::memory_order_relaxed);
680 if (std::atomic_load_explicit(&lck->lk.now_serving,
681 std::memory_order_relaxed) == my_ticket) {
682 kmp_uint32 next_ticket = my_ticket + 1;
683 if (std::atomic_compare_exchange_strong_explicit(
684 &lck->lk.next_ticket, &my_ticket, next_ticket,
685 std::memory_order_acquire, std::memory_order_acquire)) {
692 static int __kmp_test_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
694 char const *
const func =
"omp_test_lock";
696 if (!std::atomic_load_explicit(&lck->lk.initialized,
697 std::memory_order_relaxed)) {
698 KMP_FATAL(LockIsUninitialized, func);
700 if (lck->lk.self != lck) {
701 KMP_FATAL(LockIsUninitialized, func);
703 if (__kmp_is_ticket_lock_nestable(lck)) {
704 KMP_FATAL(LockNestableUsedAsSimple, func);
707 int retval = __kmp_test_ticket_lock(lck, gtid);
710 std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
711 std::memory_order_relaxed);
716 int __kmp_release_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
717 kmp_uint32 distance = std::atomic_load_explicit(&lck->lk.next_ticket,
718 std::memory_order_relaxed) -
719 std::atomic_load_explicit(&lck->lk.now_serving,
720 std::memory_order_relaxed);
722 ANNOTATE_TICKET_RELEASED(lck);
723 std::atomic_fetch_add_explicit(&lck->lk.now_serving, 1U,
724 std::memory_order_release);
727 (kmp_uint32)(__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc));
728 return KMP_LOCK_RELEASED;
731 static int __kmp_release_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
733 char const *
const func =
"omp_unset_lock";
735 if (!std::atomic_load_explicit(&lck->lk.initialized,
736 std::memory_order_relaxed)) {
737 KMP_FATAL(LockIsUninitialized, func);
739 if (lck->lk.self != lck) {
740 KMP_FATAL(LockIsUninitialized, func);
742 if (__kmp_is_ticket_lock_nestable(lck)) {
743 KMP_FATAL(LockNestableUsedAsSimple, func);
745 if (__kmp_get_ticket_lock_owner(lck) == -1) {
746 KMP_FATAL(LockUnsettingFree, func);
748 if ((gtid >= 0) && (__kmp_get_ticket_lock_owner(lck) >= 0) &&
749 (__kmp_get_ticket_lock_owner(lck) != gtid)) {
750 KMP_FATAL(LockUnsettingSetByAnother, func);
752 std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
753 return __kmp_release_ticket_lock(lck, gtid);
756 void __kmp_init_ticket_lock(kmp_ticket_lock_t *lck) {
757 lck->lk.location = NULL;
759 std::atomic_store_explicit(&lck->lk.next_ticket, 0U,
760 std::memory_order_relaxed);
761 std::atomic_store_explicit(&lck->lk.now_serving, 0U,
762 std::memory_order_relaxed);
763 std::atomic_store_explicit(
764 &lck->lk.owner_id, 0,
765 std::memory_order_relaxed);
766 std::atomic_store_explicit(
767 &lck->lk.depth_locked, -1,
768 std::memory_order_relaxed);
769 std::atomic_store_explicit(&lck->lk.initialized,
true,
770 std::memory_order_release);
773 void __kmp_destroy_ticket_lock(kmp_ticket_lock_t *lck) {
774 std::atomic_store_explicit(&lck->lk.initialized,
false,
775 std::memory_order_release);
777 lck->lk.location = NULL;
778 std::atomic_store_explicit(&lck->lk.next_ticket, 0U,
779 std::memory_order_relaxed);
780 std::atomic_store_explicit(&lck->lk.now_serving, 0U,
781 std::memory_order_relaxed);
782 std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
783 std::atomic_store_explicit(&lck->lk.depth_locked, -1,
784 std::memory_order_relaxed);
787 static void __kmp_destroy_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
788 char const *
const func =
"omp_destroy_lock";
790 if (!std::atomic_load_explicit(&lck->lk.initialized,
791 std::memory_order_relaxed)) {
792 KMP_FATAL(LockIsUninitialized, func);
794 if (lck->lk.self != lck) {
795 KMP_FATAL(LockIsUninitialized, func);
797 if (__kmp_is_ticket_lock_nestable(lck)) {
798 KMP_FATAL(LockNestableUsedAsSimple, func);
800 if (__kmp_get_ticket_lock_owner(lck) != -1) {
801 KMP_FATAL(LockStillOwned, func);
803 __kmp_destroy_ticket_lock(lck);
808 int __kmp_acquire_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
809 KMP_DEBUG_ASSERT(gtid >= 0);
811 if (__kmp_get_ticket_lock_owner(lck) == gtid) {
812 std::atomic_fetch_add_explicit(&lck->lk.depth_locked, 1,
813 std::memory_order_relaxed);
814 return KMP_LOCK_ACQUIRED_NEXT;
816 __kmp_acquire_ticket_lock_timed_template(lck, gtid);
817 ANNOTATE_TICKET_ACQUIRED(lck);
818 std::atomic_store_explicit(&lck->lk.depth_locked, 1,
819 std::memory_order_relaxed);
820 std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
821 std::memory_order_relaxed);
822 return KMP_LOCK_ACQUIRED_FIRST;
826 static int __kmp_acquire_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
828 char const *
const func =
"omp_set_nest_lock";
830 if (!std::atomic_load_explicit(&lck->lk.initialized,
831 std::memory_order_relaxed)) {
832 KMP_FATAL(LockIsUninitialized, func);
834 if (lck->lk.self != lck) {
835 KMP_FATAL(LockIsUninitialized, func);
837 if (!__kmp_is_ticket_lock_nestable(lck)) {
838 KMP_FATAL(LockSimpleUsedAsNestable, func);
840 return __kmp_acquire_nested_ticket_lock(lck, gtid);
843 int __kmp_test_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
846 KMP_DEBUG_ASSERT(gtid >= 0);
848 if (__kmp_get_ticket_lock_owner(lck) == gtid) {
849 retval = std::atomic_fetch_add_explicit(&lck->lk.depth_locked, 1,
850 std::memory_order_relaxed) +
852 }
else if (!__kmp_test_ticket_lock(lck, gtid)) {
855 std::atomic_store_explicit(&lck->lk.depth_locked, 1,
856 std::memory_order_relaxed);
857 std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
858 std::memory_order_relaxed);
864 static int __kmp_test_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
866 char const *
const func =
"omp_test_nest_lock";
868 if (!std::atomic_load_explicit(&lck->lk.initialized,
869 std::memory_order_relaxed)) {
870 KMP_FATAL(LockIsUninitialized, func);
872 if (lck->lk.self != lck) {
873 KMP_FATAL(LockIsUninitialized, func);
875 if (!__kmp_is_ticket_lock_nestable(lck)) {
876 KMP_FATAL(LockSimpleUsedAsNestable, func);
878 return __kmp_test_nested_ticket_lock(lck, gtid);
881 int __kmp_release_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
882 KMP_DEBUG_ASSERT(gtid >= 0);
884 if ((std::atomic_fetch_add_explicit(&lck->lk.depth_locked, -1,
885 std::memory_order_relaxed) -
887 std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
888 __kmp_release_ticket_lock(lck, gtid);
889 return KMP_LOCK_RELEASED;
891 return KMP_LOCK_STILL_HELD;
894 static int __kmp_release_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
896 char const *
const func =
"omp_unset_nest_lock";
898 if (!std::atomic_load_explicit(&lck->lk.initialized,
899 std::memory_order_relaxed)) {
900 KMP_FATAL(LockIsUninitialized, func);
902 if (lck->lk.self != lck) {
903 KMP_FATAL(LockIsUninitialized, func);
905 if (!__kmp_is_ticket_lock_nestable(lck)) {
906 KMP_FATAL(LockSimpleUsedAsNestable, func);
908 if (__kmp_get_ticket_lock_owner(lck) == -1) {
909 KMP_FATAL(LockUnsettingFree, func);
911 if (__kmp_get_ticket_lock_owner(lck) != gtid) {
912 KMP_FATAL(LockUnsettingSetByAnother, func);
914 return __kmp_release_nested_ticket_lock(lck, gtid);
917 void __kmp_init_nested_ticket_lock(kmp_ticket_lock_t *lck) {
918 __kmp_init_ticket_lock(lck);
919 std::atomic_store_explicit(&lck->lk.depth_locked, 0,
920 std::memory_order_relaxed);
924 void __kmp_destroy_nested_ticket_lock(kmp_ticket_lock_t *lck) {
925 __kmp_destroy_ticket_lock(lck);
926 std::atomic_store_explicit(&lck->lk.depth_locked, 0,
927 std::memory_order_relaxed);
931 __kmp_destroy_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
932 char const *
const func =
"omp_destroy_nest_lock";
934 if (!std::atomic_load_explicit(&lck->lk.initialized,
935 std::memory_order_relaxed)) {
936 KMP_FATAL(LockIsUninitialized, func);
938 if (lck->lk.self != lck) {
939 KMP_FATAL(LockIsUninitialized, func);
941 if (!__kmp_is_ticket_lock_nestable(lck)) {
942 KMP_FATAL(LockSimpleUsedAsNestable, func);
944 if (__kmp_get_ticket_lock_owner(lck) != -1) {
945 KMP_FATAL(LockStillOwned, func);
947 __kmp_destroy_nested_ticket_lock(lck);
952 static const ident_t *__kmp_get_ticket_lock_location(kmp_ticket_lock_t *lck) {
953 return lck->lk.location;
956 static void __kmp_set_ticket_lock_location(kmp_ticket_lock_t *lck,
958 lck->lk.location = loc;
961 static kmp_lock_flags_t __kmp_get_ticket_lock_flags(kmp_ticket_lock_t *lck) {
962 return lck->lk.flags;
965 static void __kmp_set_ticket_lock_flags(kmp_ticket_lock_t *lck,
966 kmp_lock_flags_t flags) {
967 lck->lk.flags = flags;
1025 #ifdef DEBUG_QUEUING_LOCKS
1028 #define TRACE_BUF_ELE 1024
1029 static char traces[TRACE_BUF_ELE][128] = {0};
1031 #define TRACE_LOCK(X, Y) \
1032 KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s\n", X, Y);
1033 #define TRACE_LOCK_T(X, Y, Z) \
1034 KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s%d\n", X, Y, Z);
1035 #define TRACE_LOCK_HT(X, Y, Z, Q) \
1036 KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s %d,%d\n", X, Y, \
1039 static void __kmp_dump_queuing_lock(kmp_info_t *this_thr, kmp_int32 gtid,
1040 kmp_queuing_lock_t *lck, kmp_int32 head_id,
1041 kmp_int32 tail_id) {
1044 __kmp_printf_no_lock(
"\n__kmp_dump_queuing_lock: TRACE BEGINS HERE! \n");
1046 i = tc % TRACE_BUF_ELE;
1047 __kmp_printf_no_lock(
"%s\n", traces[i]);
1048 i = (i + 1) % TRACE_BUF_ELE;
1049 while (i != (tc % TRACE_BUF_ELE)) {
1050 __kmp_printf_no_lock(
"%s", traces[i]);
1051 i = (i + 1) % TRACE_BUF_ELE;
1053 __kmp_printf_no_lock(
"\n");
1055 __kmp_printf_no_lock(
"\n__kmp_dump_queuing_lock: gtid+1:%d, spin_here:%d, "
1056 "next_wait:%d, head_id:%d, tail_id:%d\n",
1057 gtid + 1, this_thr->th.th_spin_here,
1058 this_thr->th.th_next_waiting, head_id, tail_id);
1060 __kmp_printf_no_lock(
"\t\thead: %d ", lck->lk.head_id);
1062 if (lck->lk.head_id >= 1) {
1063 t = __kmp_threads[lck->lk.head_id - 1]->th.th_next_waiting;
1065 __kmp_printf_no_lock(
"-> %d ", t);
1066 t = __kmp_threads[t - 1]->th.th_next_waiting;
1069 __kmp_printf_no_lock(
"; tail: %d ", lck->lk.tail_id);
1070 __kmp_printf_no_lock(
"\n\n");
1075 static kmp_int32 __kmp_get_queuing_lock_owner(kmp_queuing_lock_t *lck) {
1076 return TCR_4(lck->lk.owner_id) - 1;
1079 static inline bool __kmp_is_queuing_lock_nestable(kmp_queuing_lock_t *lck) {
1080 return lck->lk.depth_locked != -1;
1084 template <
bool takeTime>
1087 __forceinline
static int
1088 __kmp_acquire_queuing_lock_timed_template(kmp_queuing_lock_t *lck,
1090 kmp_info_t *this_thr = __kmp_thread_from_gtid(gtid);
1091 volatile kmp_int32 *head_id_p = &lck->lk.head_id;
1092 volatile kmp_int32 *tail_id_p = &lck->lk.tail_id;
1093 volatile kmp_uint32 *spin_here_p;
1094 kmp_int32 need_mf = 1;
1097 ompt_state_t prev_state = ompt_state_undefined;
1101 (
"__kmp_acquire_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));
1103 KMP_FSYNC_PREPARE(lck);
1104 KMP_DEBUG_ASSERT(this_thr != NULL);
1105 spin_here_p = &this_thr->th.th_spin_here;
1107 #ifdef DEBUG_QUEUING_LOCKS
1108 TRACE_LOCK(gtid + 1,
"acq ent");
1110 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1111 if (this_thr->th.th_next_waiting != 0)
1112 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1114 KMP_DEBUG_ASSERT(!*spin_here_p);
1115 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
1124 *spin_here_p = TRUE;
1136 #ifdef DEBUG_QUEUING_LOCKS
1138 TRACE_LOCK_HT(gtid + 1,
"acq read: ", head, tail);
1147 enqueued = KMP_COMPARE_AND_STORE_ACQ64((
volatile kmp_int64 *)tail_id_p,
1149 KMP_PACK_64(gtid + 1, gtid + 1));
1150 #ifdef DEBUG_QUEUING_LOCKS
1152 TRACE_LOCK(gtid + 1,
"acq enq: (-1,0)->(tid,tid)");
1158 KMP_DEBUG_ASSERT(tail != gtid + 1);
1160 #ifdef DEBUG_QUEUING_LOCKS
1161 TRACE_LOCK_HT(gtid + 1,
"acq read: ", head, tail);
1169 enqueued = KMP_COMPARE_AND_STORE_ACQ32(tail_id_p, tail, gtid + 1);
1171 #ifdef DEBUG_QUEUING_LOCKS
1173 TRACE_LOCK(gtid + 1,
"acq enq: (h,t)->(h,tid)");
1180 kmp_int32 grabbed_lock;
1182 #ifdef DEBUG_QUEUING_LOCKS
1184 TRACE_LOCK_HT(gtid + 1,
"acq read: ", head, tail);
1190 grabbed_lock = KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1);
1194 *spin_here_p = FALSE;
1198 (
"__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: no queuing\n",
1200 #ifdef DEBUG_QUEUING_LOCKS
1201 TRACE_LOCK_HT(gtid + 1,
"acq exit: ", head, 0);
1205 if (ompt_enabled.enabled && prev_state != ompt_state_undefined) {
1207 this_thr->th.ompt_thread_info.state = prev_state;
1208 this_thr->th.ompt_thread_info.wait_id = 0;
1212 KMP_FSYNC_ACQUIRED(lck);
1213 return KMP_LOCK_ACQUIRED_FIRST;
1220 if (ompt_enabled.enabled && prev_state == ompt_state_undefined) {
1222 prev_state = this_thr->th.ompt_thread_info.state;
1223 this_thr->th.ompt_thread_info.wait_id = (uint64_t)lck;
1224 this_thr->th.ompt_thread_info.state = ompt_state_wait_lock;
1230 kmp_info_t *tail_thr = __kmp_thread_from_gtid(tail - 1);
1231 KMP_ASSERT(tail_thr != NULL);
1232 tail_thr->th.th_next_waiting = gtid + 1;
1236 (
"__kmp_acquire_queuing_lock: lck:%p, T#%d waiting for lock\n",
1241 KMP_WAIT(spin_here_p, FALSE, KMP_EQ, lck);
1246 #ifdef DEBUG_QUEUING_LOCKS
1247 TRACE_LOCK(gtid + 1,
"acq spin");
1249 if (this_thr->th.th_next_waiting != 0)
1250 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1252 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
1253 KA_TRACE(1000, (
"__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: after "
1254 "waiting on queue\n",
1257 #ifdef DEBUG_QUEUING_LOCKS
1258 TRACE_LOCK(gtid + 1,
"acq exit 2");
1263 this_thr->th.ompt_thread_info.state = prev_state;
1264 this_thr->th.ompt_thread_info.wait_id = 0;
1268 return KMP_LOCK_ACQUIRED_FIRST;
1274 KMP_YIELD_OVERSUB();
1276 #ifdef DEBUG_QUEUING_LOCKS
1277 TRACE_LOCK(gtid + 1,
"acq retry");
1280 KMP_ASSERT2(0,
"should not get here");
1281 return KMP_LOCK_ACQUIRED_FIRST;
1284 int __kmp_acquire_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1285 KMP_DEBUG_ASSERT(gtid >= 0);
1287 int retval = __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
1288 ANNOTATE_QUEUING_ACQUIRED(lck);
1292 static int __kmp_acquire_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1294 char const *
const func =
"omp_set_lock";
1295 if (lck->lk.initialized != lck) {
1296 KMP_FATAL(LockIsUninitialized, func);
1298 if (__kmp_is_queuing_lock_nestable(lck)) {
1299 KMP_FATAL(LockNestableUsedAsSimple, func);
1301 if (__kmp_get_queuing_lock_owner(lck) == gtid) {
1302 KMP_FATAL(LockIsAlreadyOwned, func);
1305 __kmp_acquire_queuing_lock(lck, gtid);
1307 lck->lk.owner_id = gtid + 1;
1308 return KMP_LOCK_ACQUIRED_FIRST;
1311 int __kmp_test_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1312 volatile kmp_int32 *head_id_p = &lck->lk.head_id;
1315 kmp_info_t *this_thr;
1318 KA_TRACE(1000, (
"__kmp_test_queuing_lock: T#%d entering\n", gtid));
1319 KMP_DEBUG_ASSERT(gtid >= 0);
1321 this_thr = __kmp_thread_from_gtid(gtid);
1322 KMP_DEBUG_ASSERT(this_thr != NULL);
1323 KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
1330 if (KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1)) {
1332 (
"__kmp_test_queuing_lock: T#%d exiting: holding lock\n", gtid));
1333 KMP_FSYNC_ACQUIRED(lck);
1334 ANNOTATE_QUEUING_ACQUIRED(lck);
1340 (
"__kmp_test_queuing_lock: T#%d exiting: without lock\n", gtid));
1344 static int __kmp_test_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1346 char const *
const func =
"omp_test_lock";
1347 if (lck->lk.initialized != lck) {
1348 KMP_FATAL(LockIsUninitialized, func);
1350 if (__kmp_is_queuing_lock_nestable(lck)) {
1351 KMP_FATAL(LockNestableUsedAsSimple, func);
1354 int retval = __kmp_test_queuing_lock(lck, gtid);
1357 lck->lk.owner_id = gtid + 1;
1362 int __kmp_release_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1363 kmp_info_t *this_thr;
1364 volatile kmp_int32 *head_id_p = &lck->lk.head_id;
1365 volatile kmp_int32 *tail_id_p = &lck->lk.tail_id;
1368 (
"__kmp_release_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));
1369 KMP_DEBUG_ASSERT(gtid >= 0);
1370 this_thr = __kmp_thread_from_gtid(gtid);
1371 KMP_DEBUG_ASSERT(this_thr != NULL);
1372 #ifdef DEBUG_QUEUING_LOCKS
1373 TRACE_LOCK(gtid + 1,
"rel ent");
1375 if (this_thr->th.th_spin_here)
1376 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1377 if (this_thr->th.th_next_waiting != 0)
1378 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1380 KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
1381 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
1383 KMP_FSYNC_RELEASING(lck);
1384 ANNOTATE_QUEUING_RELEASED(lck);
1393 #ifdef DEBUG_QUEUING_LOCKS
1395 TRACE_LOCK_HT(gtid + 1,
"rel read: ", head, tail);
1397 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1399 KMP_DEBUG_ASSERT(head !=
1404 if (KMP_COMPARE_AND_STORE_REL32(head_id_p, -1, 0)) {
1407 (
"__kmp_release_queuing_lock: lck:%p, T#%d exiting: queue empty\n",
1409 #ifdef DEBUG_QUEUING_LOCKS
1410 TRACE_LOCK_HT(gtid + 1,
"rel exit: ", 0, 0);
1416 return KMP_LOCK_RELEASED;
1423 #ifdef DEBUG_QUEUING_LOCKS
1425 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1427 KMP_DEBUG_ASSERT(head > 0);
1430 dequeued = KMP_COMPARE_AND_STORE_REL64(
1431 RCAST(
volatile kmp_int64 *, tail_id_p), KMP_PACK_64(head, head),
1432 KMP_PACK_64(-1, 0));
1433 #ifdef DEBUG_QUEUING_LOCKS
1434 TRACE_LOCK(gtid + 1,
"rel deq: (h,h)->(-1,0)");
1438 volatile kmp_int32 *waiting_id_p;
1439 kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
1440 KMP_DEBUG_ASSERT(head_thr != NULL);
1441 waiting_id_p = &head_thr->th.th_next_waiting;
1444 #ifdef DEBUG_QUEUING_LOCKS
1445 if (head <= 0 || tail <= 0)
1446 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1448 KMP_DEBUG_ASSERT(head > 0 && tail > 0);
1455 KMP_WAIT((
volatile kmp_uint32 *)waiting_id_p, 0, KMP_NEQ, NULL);
1456 #ifdef DEBUG_QUEUING_LOCKS
1457 TRACE_LOCK(gtid + 1,
"rel deq: (h,t)->(h',t)");
1464 kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
1465 KMP_DEBUG_ASSERT(head_thr != NULL);
1468 #ifdef DEBUG_QUEUING_LOCKS
1469 if (head <= 0 || tail <= 0)
1470 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1472 KMP_DEBUG_ASSERT(head > 0 && tail > 0);
1476 head_thr->th.th_next_waiting = 0;
1477 #ifdef DEBUG_QUEUING_LOCKS
1478 TRACE_LOCK_T(gtid + 1,
"rel nw=0 for t=", head);
1483 head_thr->th.th_spin_here = FALSE;
1485 KA_TRACE(1000, (
"__kmp_release_queuing_lock: lck:%p, T#%d exiting: after "
1488 #ifdef DEBUG_QUEUING_LOCKS
1489 TRACE_LOCK(gtid + 1,
"rel exit 2");
1491 return KMP_LOCK_RELEASED;
1496 #ifdef DEBUG_QUEUING_LOCKS
1497 TRACE_LOCK(gtid + 1,
"rel retry");
1501 KMP_ASSERT2(0,
"should not get here");
1502 return KMP_LOCK_RELEASED;
1505 static int __kmp_release_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1507 char const *
const func =
"omp_unset_lock";
1509 if (lck->lk.initialized != lck) {
1510 KMP_FATAL(LockIsUninitialized, func);
1512 if (__kmp_is_queuing_lock_nestable(lck)) {
1513 KMP_FATAL(LockNestableUsedAsSimple, func);
1515 if (__kmp_get_queuing_lock_owner(lck) == -1) {
1516 KMP_FATAL(LockUnsettingFree, func);
1518 if (__kmp_get_queuing_lock_owner(lck) != gtid) {
1519 KMP_FATAL(LockUnsettingSetByAnother, func);
1521 lck->lk.owner_id = 0;
1522 return __kmp_release_queuing_lock(lck, gtid);
1525 void __kmp_init_queuing_lock(kmp_queuing_lock_t *lck) {
1526 lck->lk.location = NULL;
1527 lck->lk.head_id = 0;
1528 lck->lk.tail_id = 0;
1529 lck->lk.next_ticket = 0;
1530 lck->lk.now_serving = 0;
1531 lck->lk.owner_id = 0;
1532 lck->lk.depth_locked = -1;
1533 lck->lk.initialized = lck;
1535 KA_TRACE(1000, (
"__kmp_init_queuing_lock: lock %p initialized\n", lck));
1538 void __kmp_destroy_queuing_lock(kmp_queuing_lock_t *lck) {
1539 lck->lk.initialized = NULL;
1540 lck->lk.location = NULL;
1541 lck->lk.head_id = 0;
1542 lck->lk.tail_id = 0;
1543 lck->lk.next_ticket = 0;
1544 lck->lk.now_serving = 0;
1545 lck->lk.owner_id = 0;
1546 lck->lk.depth_locked = -1;
1549 static void __kmp_destroy_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
1550 char const *
const func =
"omp_destroy_lock";
1551 if (lck->lk.initialized != lck) {
1552 KMP_FATAL(LockIsUninitialized, func);
1554 if (__kmp_is_queuing_lock_nestable(lck)) {
1555 KMP_FATAL(LockNestableUsedAsSimple, func);
1557 if (__kmp_get_queuing_lock_owner(lck) != -1) {
1558 KMP_FATAL(LockStillOwned, func);
1560 __kmp_destroy_queuing_lock(lck);
1565 int __kmp_acquire_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1566 KMP_DEBUG_ASSERT(gtid >= 0);
1568 if (__kmp_get_queuing_lock_owner(lck) == gtid) {
1569 lck->lk.depth_locked += 1;
1570 return KMP_LOCK_ACQUIRED_NEXT;
1572 __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
1573 ANNOTATE_QUEUING_ACQUIRED(lck);
1575 lck->lk.depth_locked = 1;
1577 lck->lk.owner_id = gtid + 1;
1578 return KMP_LOCK_ACQUIRED_FIRST;
1583 __kmp_acquire_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1585 char const *
const func =
"omp_set_nest_lock";
1586 if (lck->lk.initialized != lck) {
1587 KMP_FATAL(LockIsUninitialized, func);
1589 if (!__kmp_is_queuing_lock_nestable(lck)) {
1590 KMP_FATAL(LockSimpleUsedAsNestable, func);
1592 return __kmp_acquire_nested_queuing_lock(lck, gtid);
1595 int __kmp_test_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1598 KMP_DEBUG_ASSERT(gtid >= 0);
1600 if (__kmp_get_queuing_lock_owner(lck) == gtid) {
1601 retval = ++lck->lk.depth_locked;
1602 }
else if (!__kmp_test_queuing_lock(lck, gtid)) {
1606 retval = lck->lk.depth_locked = 1;
1608 lck->lk.owner_id = gtid + 1;
1613 static int __kmp_test_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1615 char const *
const func =
"omp_test_nest_lock";
1616 if (lck->lk.initialized != lck) {
1617 KMP_FATAL(LockIsUninitialized, func);
1619 if (!__kmp_is_queuing_lock_nestable(lck)) {
1620 KMP_FATAL(LockSimpleUsedAsNestable, func);
1622 return __kmp_test_nested_queuing_lock(lck, gtid);
1625 int __kmp_release_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1626 KMP_DEBUG_ASSERT(gtid >= 0);
1629 if (--(lck->lk.depth_locked) == 0) {
1631 lck->lk.owner_id = 0;
1632 __kmp_release_queuing_lock(lck, gtid);
1633 return KMP_LOCK_RELEASED;
1635 return KMP_LOCK_STILL_HELD;
1639 __kmp_release_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1641 char const *
const func =
"omp_unset_nest_lock";
1643 if (lck->lk.initialized != lck) {
1644 KMP_FATAL(LockIsUninitialized, func);
1646 if (!__kmp_is_queuing_lock_nestable(lck)) {
1647 KMP_FATAL(LockSimpleUsedAsNestable, func);
1649 if (__kmp_get_queuing_lock_owner(lck) == -1) {
1650 KMP_FATAL(LockUnsettingFree, func);
1652 if (__kmp_get_queuing_lock_owner(lck) != gtid) {
1653 KMP_FATAL(LockUnsettingSetByAnother, func);
1655 return __kmp_release_nested_queuing_lock(lck, gtid);
1658 void __kmp_init_nested_queuing_lock(kmp_queuing_lock_t *lck) {
1659 __kmp_init_queuing_lock(lck);
1660 lck->lk.depth_locked = 0;
1663 void __kmp_destroy_nested_queuing_lock(kmp_queuing_lock_t *lck) {
1664 __kmp_destroy_queuing_lock(lck);
1665 lck->lk.depth_locked = 0;
1669 __kmp_destroy_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
1670 char const *
const func =
"omp_destroy_nest_lock";
1671 if (lck->lk.initialized != lck) {
1672 KMP_FATAL(LockIsUninitialized, func);
1674 if (!__kmp_is_queuing_lock_nestable(lck)) {
1675 KMP_FATAL(LockSimpleUsedAsNestable, func);
1677 if (__kmp_get_queuing_lock_owner(lck) != -1) {
1678 KMP_FATAL(LockStillOwned, func);
1680 __kmp_destroy_nested_queuing_lock(lck);
1685 static const ident_t *__kmp_get_queuing_lock_location(kmp_queuing_lock_t *lck) {
1686 return lck->lk.location;
1689 static void __kmp_set_queuing_lock_location(kmp_queuing_lock_t *lck,
1691 lck->lk.location = loc;
1694 static kmp_lock_flags_t __kmp_get_queuing_lock_flags(kmp_queuing_lock_t *lck) {
1695 return lck->lk.flags;
1698 static void __kmp_set_queuing_lock_flags(kmp_queuing_lock_t *lck,
1699 kmp_lock_flags_t flags) {
1700 lck->lk.flags = flags;
1703 #if KMP_USE_ADAPTIVE_LOCKS
1707 #if KMP_HAVE_RTM_INTRINSICS
1708 #include <immintrin.h>
1709 #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT)
1714 #define _XBEGIN_STARTED (~0u)
1715 #define _XABORT_EXPLICIT (1 << 0)
1716 #define _XABORT_RETRY (1 << 1)
1717 #define _XABORT_CONFLICT (1 << 2)
1718 #define _XABORT_CAPACITY (1 << 3)
1719 #define _XABORT_DEBUG (1 << 4)
1720 #define _XABORT_NESTED (1 << 5)
1721 #define _XABORT_CODE(x) ((unsigned char)(((x) >> 24) & 0xFF))
1724 #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT)
1726 #define STRINGIZE_INTERNAL(arg) #arg
1727 #define STRINGIZE(arg) STRINGIZE_INTERNAL(arg)
1733 static __inline
int _xbegin() {
1770 __asm__
volatile(
"1: .byte 0xC7; .byte 0xF8;\n"
1773 "1: movl %%eax,%0\n"
1775 :
"+r"(res)::
"memory",
"%eax");
1781 static __inline
void _xend() {
1789 __asm__
volatile(
".byte 0x0f; .byte 0x01; .byte 0xd5" :::
"memory");
1798 #define _xabort(ARG) _asm _emit 0xc6 _asm _emit 0xf8 _asm _emit ARG
1800 #define _xabort(ARG) \
1801 __asm__ volatile(".byte 0xC6; .byte 0xF8; .byte " STRINGIZE(ARG):::"memory");
1807 #if KMP_DEBUG_ADAPTIVE_LOCKS
1812 static kmp_adaptive_lock_statistics_t destroyedStats;
1815 static kmp_adaptive_lock_info_t liveLocks;
1818 static kmp_bootstrap_lock_t chain_lock =
1819 KMP_BOOTSTRAP_LOCK_INITIALIZER(chain_lock);
1822 void __kmp_init_speculative_stats() {
1823 kmp_adaptive_lock_info_t *lck = &liveLocks;
1825 memset(CCAST(kmp_adaptive_lock_statistics_t *, &(lck->stats)), 0,
1826 sizeof(lck->stats));
1827 lck->stats.next = lck;
1828 lck->stats.prev = lck;
1830 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1831 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1833 __kmp_init_bootstrap_lock(&chain_lock);
1837 static void __kmp_remember_lock(kmp_adaptive_lock_info_t *lck) {
1838 __kmp_acquire_bootstrap_lock(&chain_lock);
1840 lck->stats.next = liveLocks.stats.next;
1841 lck->stats.prev = &liveLocks;
1843 liveLocks.stats.next = lck;
1844 lck->stats.next->stats.prev = lck;
1846 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1847 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1849 __kmp_release_bootstrap_lock(&chain_lock);
1852 static void __kmp_forget_lock(kmp_adaptive_lock_info_t *lck) {
1853 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1854 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1856 kmp_adaptive_lock_info_t *n = lck->stats.next;
1857 kmp_adaptive_lock_info_t *p = lck->stats.prev;
1863 static void __kmp_zero_speculative_stats(kmp_adaptive_lock_info_t *lck) {
1864 memset(CCAST(kmp_adaptive_lock_statistics_t *, &lck->stats), 0,
1865 sizeof(lck->stats));
1866 __kmp_remember_lock(lck);
1869 static void __kmp_add_stats(kmp_adaptive_lock_statistics_t *t,
1870 kmp_adaptive_lock_info_t *lck) {
1871 kmp_adaptive_lock_statistics_t
volatile *s = &lck->stats;
1873 t->nonSpeculativeAcquireAttempts += lck->acquire_attempts;
1874 t->successfulSpeculations += s->successfulSpeculations;
1875 t->hardFailedSpeculations += s->hardFailedSpeculations;
1876 t->softFailedSpeculations += s->softFailedSpeculations;
1877 t->nonSpeculativeAcquires += s->nonSpeculativeAcquires;
1878 t->lemmingYields += s->lemmingYields;
1881 static void __kmp_accumulate_speculative_stats(kmp_adaptive_lock_info_t *lck) {
1882 __kmp_acquire_bootstrap_lock(&chain_lock);
1884 __kmp_add_stats(&destroyedStats, lck);
1885 __kmp_forget_lock(lck);
1887 __kmp_release_bootstrap_lock(&chain_lock);
1890 static float percent(kmp_uint32 count, kmp_uint32 total) {
1891 return (total == 0) ? 0.0 : (100.0 * count) / total;
1894 void __kmp_print_speculative_stats() {
1895 kmp_adaptive_lock_statistics_t total = destroyedStats;
1896 kmp_adaptive_lock_info_t *lck;
1898 for (lck = liveLocks.stats.next; lck != &liveLocks; lck = lck->stats.next) {
1899 __kmp_add_stats(&total, lck);
1901 kmp_adaptive_lock_statistics_t *t = &total;
1902 kmp_uint32 totalSections =
1903 t->nonSpeculativeAcquires + t->successfulSpeculations;
1904 kmp_uint32 totalSpeculations = t->successfulSpeculations +
1905 t->hardFailedSpeculations +
1906 t->softFailedSpeculations;
1907 if (totalSections <= 0)
1911 if (strcmp(__kmp_speculative_statsfile,
"-") == 0) {
1914 size_t buffLen = KMP_STRLEN(__kmp_speculative_statsfile) + 20;
1915 char buffer[buffLen];
1916 KMP_SNPRINTF(&buffer[0], buffLen, __kmp_speculative_statsfile,
1917 (kmp_int32)getpid());
1918 statsFile.
open(buffer,
"w");
1921 fprintf(statsFile,
"Speculative lock statistics (all approximate!)\n");
1923 " Lock parameters: \n"
1924 " max_soft_retries : %10d\n"
1925 " max_badness : %10d\n",
1926 __kmp_adaptive_backoff_params.max_soft_retries,
1927 __kmp_adaptive_backoff_params.max_badness);
1928 fprintf(statsFile,
" Non-speculative acquire attempts : %10d\n",
1929 t->nonSpeculativeAcquireAttempts);
1930 fprintf(statsFile,
" Total critical sections : %10d\n",
1932 fprintf(statsFile,
" Successful speculations : %10d (%5.1f%%)\n",
1933 t->successfulSpeculations,
1934 percent(t->successfulSpeculations, totalSections));
1935 fprintf(statsFile,
" Non-speculative acquires : %10d (%5.1f%%)\n",
1936 t->nonSpeculativeAcquires,
1937 percent(t->nonSpeculativeAcquires, totalSections));
1938 fprintf(statsFile,
" Lemming yields : %10d\n\n",
1941 fprintf(statsFile,
" Speculative acquire attempts : %10d\n",
1943 fprintf(statsFile,
" Successes : %10d (%5.1f%%)\n",
1944 t->successfulSpeculations,
1945 percent(t->successfulSpeculations, totalSpeculations));
1946 fprintf(statsFile,
" Soft failures : %10d (%5.1f%%)\n",
1947 t->softFailedSpeculations,
1948 percent(t->softFailedSpeculations, totalSpeculations));
1949 fprintf(statsFile,
" Hard failures : %10d (%5.1f%%)\n",
1950 t->hardFailedSpeculations,
1951 percent(t->hardFailedSpeculations, totalSpeculations));
1954 #define KMP_INC_STAT(lck, stat) (lck->lk.adaptive.stats.stat++)
1956 #define KMP_INC_STAT(lck, stat)
1960 static inline bool __kmp_is_unlocked_queuing_lock(kmp_queuing_lock_t *lck) {
1963 bool res = lck->lk.head_id == 0;
1967 #if KMP_COMPILER_ICC
1970 __sync_synchronize();
1977 static __inline
void
1978 __kmp_update_badness_after_success(kmp_adaptive_lock_t *lck) {
1980 lck->lk.adaptive.badness = 0;
1981 KMP_INC_STAT(lck, successfulSpeculations);
1985 static __inline
void __kmp_step_badness(kmp_adaptive_lock_t *lck) {
1986 kmp_uint32 newBadness = (lck->lk.adaptive.badness << 1) | 1;
1987 if (newBadness > lck->lk.adaptive.max_badness) {
1990 lck->lk.adaptive.badness = newBadness;
1995 KMP_ATTRIBUTE_TARGET_RTM
1996 static __inline
int __kmp_should_speculate(kmp_adaptive_lock_t *lck,
1998 kmp_uint32 badness = lck->lk.adaptive.badness;
1999 kmp_uint32 attempts = lck->lk.adaptive.acquire_attempts;
2000 int res = (attempts & badness) == 0;
2006 KMP_ATTRIBUTE_TARGET_RTM
2007 static int __kmp_test_adaptive_lock_only(kmp_adaptive_lock_t *lck,
2009 int retries = lck->lk.adaptive.max_soft_retries;
2016 kmp_uint32 status = _xbegin();
2021 if (status == _XBEGIN_STARTED) {
2026 if (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2030 KMP_ASSERT2(0,
"should not get here");
2035 if (status & SOFT_ABORT_MASK) {
2036 KMP_INC_STAT(lck, softFailedSpeculations);
2039 KMP_INC_STAT(lck, hardFailedSpeculations);
2044 }
while (retries--);
2048 __kmp_step_badness(lck);
2055 static int __kmp_test_adaptive_lock(kmp_adaptive_lock_t *lck, kmp_int32 gtid) {
2057 if (__kmp_should_speculate(lck, gtid) &&
2058 __kmp_test_adaptive_lock_only(lck, gtid))
2063 lck->lk.adaptive.acquire_attempts++;
2066 if (__kmp_test_queuing_lock(GET_QLK_PTR(lck), gtid)) {
2067 KMP_INC_STAT(lck, nonSpeculativeAcquires);
2074 static int __kmp_test_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2076 char const *
const func =
"omp_test_lock";
2077 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2078 KMP_FATAL(LockIsUninitialized, func);
2081 int retval = __kmp_test_adaptive_lock(lck, gtid);
2084 lck->lk.qlk.owner_id = gtid + 1;
2100 static void __kmp_acquire_adaptive_lock(kmp_adaptive_lock_t *lck,
2102 if (__kmp_should_speculate(lck, gtid)) {
2103 if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2104 if (__kmp_test_adaptive_lock_only(lck, gtid))
2113 while (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2114 KMP_INC_STAT(lck, lemmingYields);
2118 if (__kmp_test_adaptive_lock_only(lck, gtid))
2125 lck->lk.adaptive.acquire_attempts++;
2127 __kmp_acquire_queuing_lock_timed_template<FALSE>(GET_QLK_PTR(lck), gtid);
2129 KMP_INC_STAT(lck, nonSpeculativeAcquires);
2130 ANNOTATE_QUEUING_ACQUIRED(lck);
2133 static void __kmp_acquire_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2135 char const *
const func =
"omp_set_lock";
2136 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2137 KMP_FATAL(LockIsUninitialized, func);
2139 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == gtid) {
2140 KMP_FATAL(LockIsAlreadyOwned, func);
2143 __kmp_acquire_adaptive_lock(lck, gtid);
2145 lck->lk.qlk.owner_id = gtid + 1;
2148 KMP_ATTRIBUTE_TARGET_RTM
2149 static int __kmp_release_adaptive_lock(kmp_adaptive_lock_t *lck,
2151 if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(
2156 __kmp_update_badness_after_success(lck);
2159 __kmp_release_queuing_lock(GET_QLK_PTR(lck), gtid);
2161 return KMP_LOCK_RELEASED;
2164 static int __kmp_release_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2166 char const *
const func =
"omp_unset_lock";
2168 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2169 KMP_FATAL(LockIsUninitialized, func);
2171 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == -1) {
2172 KMP_FATAL(LockUnsettingFree, func);
2174 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != gtid) {
2175 KMP_FATAL(LockUnsettingSetByAnother, func);
2177 lck->lk.qlk.owner_id = 0;
2178 __kmp_release_adaptive_lock(lck, gtid);
2179 return KMP_LOCK_RELEASED;
2182 static void __kmp_init_adaptive_lock(kmp_adaptive_lock_t *lck) {
2183 __kmp_init_queuing_lock(GET_QLK_PTR(lck));
2184 lck->lk.adaptive.badness = 0;
2185 lck->lk.adaptive.acquire_attempts = 0;
2186 lck->lk.adaptive.max_soft_retries =
2187 __kmp_adaptive_backoff_params.max_soft_retries;
2188 lck->lk.adaptive.max_badness = __kmp_adaptive_backoff_params.max_badness;
2189 #if KMP_DEBUG_ADAPTIVE_LOCKS
2190 __kmp_zero_speculative_stats(&lck->lk.adaptive);
2192 KA_TRACE(1000, (
"__kmp_init_adaptive_lock: lock %p initialized\n", lck));
2195 static void __kmp_destroy_adaptive_lock(kmp_adaptive_lock_t *lck) {
2196 #if KMP_DEBUG_ADAPTIVE_LOCKS
2197 __kmp_accumulate_speculative_stats(&lck->lk.adaptive);
2199 __kmp_destroy_queuing_lock(GET_QLK_PTR(lck));
2203 static void __kmp_destroy_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
2204 char const *
const func =
"omp_destroy_lock";
2205 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2206 KMP_FATAL(LockIsUninitialized, func);
2208 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != -1) {
2209 KMP_FATAL(LockStillOwned, func);
2211 __kmp_destroy_adaptive_lock(lck);
2220 static kmp_int32 __kmp_get_drdpa_lock_owner(kmp_drdpa_lock_t *lck) {
2221 return lck->lk.owner_id - 1;
2224 static inline bool __kmp_is_drdpa_lock_nestable(kmp_drdpa_lock_t *lck) {
2225 return lck->lk.depth_locked != -1;
2228 __forceinline
static int
2229 __kmp_acquire_drdpa_lock_timed_template(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2230 kmp_uint64 ticket = KMP_ATOMIC_INC(&lck->lk.next_ticket);
2231 kmp_uint64 mask = lck->lk.mask;
2232 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2234 #ifdef USE_LOCK_PROFILE
2235 if (polls[ticket & mask] != ticket)
2236 __kmp_printf(
"LOCK CONTENTION: %p\n", lck);
2248 KMP_FSYNC_PREPARE(lck);
2249 KMP_INIT_YIELD(spins);
2250 while (polls[ticket & mask] < ticket) {
2251 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
2259 mask = lck->lk.mask;
2260 polls = lck->lk.polls;
2264 KMP_FSYNC_ACQUIRED(lck);
2265 KA_TRACE(1000, (
"__kmp_acquire_drdpa_lock: ticket #%lld acquired lock %p\n",
2267 lck->lk.now_serving = ticket;
2274 if ((lck->lk.old_polls != NULL) && (ticket >= lck->lk.cleanup_ticket)) {
2275 __kmp_free(lck->lk.old_polls);
2276 lck->lk.old_polls = NULL;
2277 lck->lk.cleanup_ticket = 0;
2283 if (lck->lk.old_polls == NULL) {
2284 bool reconfigure =
false;
2285 std::atomic<kmp_uint64> *old_polls = polls;
2286 kmp_uint32 num_polls = TCR_4(lck->lk.num_polls);
2288 if (TCR_4(__kmp_nth) >
2289 (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) {
2292 if (num_polls > 1) {
2294 num_polls = TCR_4(lck->lk.num_polls);
2297 polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
2305 kmp_uint64 num_waiting = TCR_8(lck->lk.next_ticket) - ticket - 1;
2306 if (num_waiting > num_polls) {
2307 kmp_uint32 old_num_polls = num_polls;
2310 mask = (mask << 1) | 1;
2312 }
while (num_polls <= num_waiting);
2318 polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
2321 for (i = 0; i < old_num_polls; i++) {
2322 polls[i].store(old_polls[i]);
2337 KA_TRACE(1000, (
"__kmp_acquire_drdpa_lock: ticket #%lld reconfiguring "
2338 "lock %p to %d polls\n",
2339 ticket, lck, num_polls));
2341 lck->lk.old_polls = old_polls;
2342 lck->lk.polls = polls;
2346 lck->lk.num_polls = num_polls;
2347 lck->lk.mask = mask;
2355 lck->lk.cleanup_ticket = lck->lk.next_ticket;
2358 return KMP_LOCK_ACQUIRED_FIRST;
2361 int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2362 int retval = __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
2363 ANNOTATE_DRDPA_ACQUIRED(lck);
2367 static int __kmp_acquire_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2369 char const *
const func =
"omp_set_lock";
2370 if (lck->lk.initialized != lck) {
2371 KMP_FATAL(LockIsUninitialized, func);
2373 if (__kmp_is_drdpa_lock_nestable(lck)) {
2374 KMP_FATAL(LockNestableUsedAsSimple, func);
2376 if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) == gtid)) {
2377 KMP_FATAL(LockIsAlreadyOwned, func);
2380 __kmp_acquire_drdpa_lock(lck, gtid);
2382 lck->lk.owner_id = gtid + 1;
2383 return KMP_LOCK_ACQUIRED_FIRST;
2386 int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2389 kmp_uint64 ticket = lck->lk.next_ticket;
2390 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2391 kmp_uint64 mask = lck->lk.mask;
2392 if (polls[ticket & mask] == ticket) {
2393 kmp_uint64 next_ticket = ticket + 1;
2394 if (__kmp_atomic_compare_store_acq(&lck->lk.next_ticket, ticket,
2396 KMP_FSYNC_ACQUIRED(lck);
2397 KA_TRACE(1000, (
"__kmp_test_drdpa_lock: ticket #%lld acquired lock %p\n",
2399 lck->lk.now_serving = ticket;
2413 static int __kmp_test_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2415 char const *
const func =
"omp_test_lock";
2416 if (lck->lk.initialized != lck) {
2417 KMP_FATAL(LockIsUninitialized, func);
2419 if (__kmp_is_drdpa_lock_nestable(lck)) {
2420 KMP_FATAL(LockNestableUsedAsSimple, func);
2423 int retval = __kmp_test_drdpa_lock(lck, gtid);
2426 lck->lk.owner_id = gtid + 1;
2431 int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2434 kmp_uint64 ticket = lck->lk.now_serving + 1;
2435 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2436 kmp_uint64 mask = lck->lk.mask;
2437 KA_TRACE(1000, (
"__kmp_release_drdpa_lock: ticket #%lld released lock %p\n",
2439 KMP_FSYNC_RELEASING(lck);
2440 ANNOTATE_DRDPA_RELEASED(lck);
2441 polls[ticket & mask] = ticket;
2442 return KMP_LOCK_RELEASED;
2445 static int __kmp_release_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2447 char const *
const func =
"omp_unset_lock";
2449 if (lck->lk.initialized != lck) {
2450 KMP_FATAL(LockIsUninitialized, func);
2452 if (__kmp_is_drdpa_lock_nestable(lck)) {
2453 KMP_FATAL(LockNestableUsedAsSimple, func);
2455 if (__kmp_get_drdpa_lock_owner(lck) == -1) {
2456 KMP_FATAL(LockUnsettingFree, func);
2458 if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) >= 0) &&
2459 (__kmp_get_drdpa_lock_owner(lck) != gtid)) {
2460 KMP_FATAL(LockUnsettingSetByAnother, func);
2462 lck->lk.owner_id = 0;
2463 return __kmp_release_drdpa_lock(lck, gtid);
2466 void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck) {
2467 lck->lk.location = NULL;
2469 lck->lk.num_polls = 1;
2470 lck->lk.polls = (std::atomic<kmp_uint64> *)__kmp_allocate(
2471 lck->lk.num_polls *
sizeof(*(lck->lk.polls)));
2472 lck->lk.cleanup_ticket = 0;
2473 lck->lk.old_polls = NULL;
2474 lck->lk.next_ticket = 0;
2475 lck->lk.now_serving = 0;
2476 lck->lk.owner_id = 0;
2477 lck->lk.depth_locked = -1;
2478 lck->lk.initialized = lck;
2480 KA_TRACE(1000, (
"__kmp_init_drdpa_lock: lock %p initialized\n", lck));
2483 void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck) {
2484 lck->lk.initialized = NULL;
2485 lck->lk.location = NULL;
2486 if (lck->lk.polls.load() != NULL) {
2487 __kmp_free(lck->lk.polls.load());
2488 lck->lk.polls = NULL;
2490 if (lck->lk.old_polls != NULL) {
2491 __kmp_free(lck->lk.old_polls);
2492 lck->lk.old_polls = NULL;
2495 lck->lk.num_polls = 0;
2496 lck->lk.cleanup_ticket = 0;
2497 lck->lk.next_ticket = 0;
2498 lck->lk.now_serving = 0;
2499 lck->lk.owner_id = 0;
2500 lck->lk.depth_locked = -1;
2503 static void __kmp_destroy_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
2504 char const *
const func =
"omp_destroy_lock";
2505 if (lck->lk.initialized != lck) {
2506 KMP_FATAL(LockIsUninitialized, func);
2508 if (__kmp_is_drdpa_lock_nestable(lck)) {
2509 KMP_FATAL(LockNestableUsedAsSimple, func);
2511 if (__kmp_get_drdpa_lock_owner(lck) != -1) {
2512 KMP_FATAL(LockStillOwned, func);
2514 __kmp_destroy_drdpa_lock(lck);
2519 int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2520 KMP_DEBUG_ASSERT(gtid >= 0);
2522 if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
2523 lck->lk.depth_locked += 1;
2524 return KMP_LOCK_ACQUIRED_NEXT;
2526 __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
2527 ANNOTATE_DRDPA_ACQUIRED(lck);
2529 lck->lk.depth_locked = 1;
2531 lck->lk.owner_id = gtid + 1;
2532 return KMP_LOCK_ACQUIRED_FIRST;
2536 static void __kmp_acquire_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2538 char const *
const func =
"omp_set_nest_lock";
2539 if (lck->lk.initialized != lck) {
2540 KMP_FATAL(LockIsUninitialized, func);
2542 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2543 KMP_FATAL(LockSimpleUsedAsNestable, func);
2545 __kmp_acquire_nested_drdpa_lock(lck, gtid);
2548 int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2551 KMP_DEBUG_ASSERT(gtid >= 0);
2553 if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
2554 retval = ++lck->lk.depth_locked;
2555 }
else if (!__kmp_test_drdpa_lock(lck, gtid)) {
2559 retval = lck->lk.depth_locked = 1;
2561 lck->lk.owner_id = gtid + 1;
2566 static int __kmp_test_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2568 char const *
const func =
"omp_test_nest_lock";
2569 if (lck->lk.initialized != lck) {
2570 KMP_FATAL(LockIsUninitialized, func);
2572 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2573 KMP_FATAL(LockSimpleUsedAsNestable, func);
2575 return __kmp_test_nested_drdpa_lock(lck, gtid);
2578 int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2579 KMP_DEBUG_ASSERT(gtid >= 0);
2582 if (--(lck->lk.depth_locked) == 0) {
2584 lck->lk.owner_id = 0;
2585 __kmp_release_drdpa_lock(lck, gtid);
2586 return KMP_LOCK_RELEASED;
2588 return KMP_LOCK_STILL_HELD;
2591 static int __kmp_release_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2593 char const *
const func =
"omp_unset_nest_lock";
2595 if (lck->lk.initialized != lck) {
2596 KMP_FATAL(LockIsUninitialized, func);
2598 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2599 KMP_FATAL(LockSimpleUsedAsNestable, func);
2601 if (__kmp_get_drdpa_lock_owner(lck) == -1) {
2602 KMP_FATAL(LockUnsettingFree, func);
2604 if (__kmp_get_drdpa_lock_owner(lck) != gtid) {
2605 KMP_FATAL(LockUnsettingSetByAnother, func);
2607 return __kmp_release_nested_drdpa_lock(lck, gtid);
2610 void __kmp_init_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
2611 __kmp_init_drdpa_lock(lck);
2612 lck->lk.depth_locked = 0;
2615 void __kmp_destroy_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
2616 __kmp_destroy_drdpa_lock(lck);
2617 lck->lk.depth_locked = 0;
2620 static void __kmp_destroy_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
2621 char const *
const func =
"omp_destroy_nest_lock";
2622 if (lck->lk.initialized != lck) {
2623 KMP_FATAL(LockIsUninitialized, func);
2625 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2626 KMP_FATAL(LockSimpleUsedAsNestable, func);
2628 if (__kmp_get_drdpa_lock_owner(lck) != -1) {
2629 KMP_FATAL(LockStillOwned, func);
2631 __kmp_destroy_nested_drdpa_lock(lck);
2636 static const ident_t *__kmp_get_drdpa_lock_location(kmp_drdpa_lock_t *lck) {
2637 return lck->lk.location;
2640 static void __kmp_set_drdpa_lock_location(kmp_drdpa_lock_t *lck,
2642 lck->lk.location = loc;
2645 static kmp_lock_flags_t __kmp_get_drdpa_lock_flags(kmp_drdpa_lock_t *lck) {
2646 return lck->lk.flags;
2649 static void __kmp_set_drdpa_lock_flags(kmp_drdpa_lock_t *lck,
2650 kmp_lock_flags_t flags) {
2651 lck->lk.flags = flags;
2655 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
2656 #define __kmp_tsc() __kmp_hardware_timestamp()
2658 kmp_backoff_t __kmp_spin_backoff_params = {1, 4096, 100};
2661 extern kmp_uint64 __kmp_now_nsec();
2662 kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
2663 #define __kmp_tsc() __kmp_now_nsec()
2673 static inline bool before(kmp_uint64 a, kmp_uint64 b) {
2674 return ((kmp_int64)b - (kmp_int64)a) > 0;
2678 void __kmp_spin_backoff(kmp_backoff_t *boff) {
2681 for (i = boff->step; i > 0; i--) {
2682 kmp_uint64 goal = __kmp_tsc() + boff->min_tick;
2685 }
while (before(__kmp_tsc(), goal));
2687 boff->step = (boff->step << 1 | 1) & (boff->max_backoff - 1);
2690 #if KMP_USE_DYNAMIC_LOCK
2694 static void __kmp_init_direct_lock(kmp_dyna_lock_t *lck,
2695 kmp_dyna_lockseq_t seq) {
2696 TCW_4(*lck, KMP_GET_D_TAG(seq));
2699 (
"__kmp_init_direct_lock: initialized direct lock with type#%d\n", seq));
2705 #define HLE_ACQUIRE ".byte 0xf2;"
2706 #define HLE_RELEASE ".byte 0xf3;"
2708 static inline kmp_uint32 swap4(kmp_uint32
volatile *p, kmp_uint32 v) {
2709 __asm__
volatile(HLE_ACQUIRE
"xchg %1,%0" :
"+r"(v),
"+m"(*p) : :
"memory");
2713 static void __kmp_destroy_hle_lock(kmp_dyna_lock_t *lck) { TCW_4(*lck, 0); }
2715 static void __kmp_destroy_hle_lock_with_checks(kmp_dyna_lock_t *lck) {
2719 static void __kmp_acquire_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
2721 if (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle)) {
2724 while (*(kmp_uint32
volatile *)lck != KMP_LOCK_FREE(hle)) {
2725 for (
int i = delay; i != 0; --i)
2727 delay = ((delay << 1) | 1) & 7;
2729 }
while (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle));
2733 static void __kmp_acquire_hle_lock_with_checks(kmp_dyna_lock_t *lck,
2735 __kmp_acquire_hle_lock(lck, gtid);
2738 static int __kmp_release_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
2739 __asm__
volatile(HLE_RELEASE
"movl %1,%0"
2741 :
"r"(KMP_LOCK_FREE(hle))
2743 return KMP_LOCK_RELEASED;
2746 static int __kmp_release_hle_lock_with_checks(kmp_dyna_lock_t *lck,
2748 return __kmp_release_hle_lock(lck, gtid);
2751 static int __kmp_test_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
2752 return swap4(lck, KMP_LOCK_BUSY(1, hle)) == KMP_LOCK_FREE(hle);
2755 static int __kmp_test_hle_lock_with_checks(kmp_dyna_lock_t *lck,
2757 return __kmp_test_hle_lock(lck, gtid);
2760 static void __kmp_init_rtm_queuing_lock(kmp_queuing_lock_t *lck) {
2761 __kmp_init_queuing_lock(lck);
2764 static void __kmp_destroy_rtm_queuing_lock(kmp_queuing_lock_t *lck) {
2765 __kmp_destroy_queuing_lock(lck);
2769 __kmp_destroy_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
2770 __kmp_destroy_queuing_lock_with_checks(lck);
2773 KMP_ATTRIBUTE_TARGET_RTM
2774 static void __kmp_acquire_rtm_queuing_lock(kmp_queuing_lock_t *lck,
2776 unsigned retries = 3, status;
2779 if (status == _XBEGIN_STARTED) {
2780 if (__kmp_is_unlocked_queuing_lock(lck))
2784 if ((status & _XABORT_EXPLICIT) && _XABORT_CODE(status) == 0xff) {
2786 while (!__kmp_is_unlocked_queuing_lock(lck)) {
2789 }
else if (!(status & _XABORT_RETRY))
2791 }
while (retries--);
2794 __kmp_acquire_queuing_lock(lck, gtid);
2797 static void __kmp_acquire_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
2799 __kmp_acquire_rtm_queuing_lock(lck, gtid);
2802 KMP_ATTRIBUTE_TARGET_RTM
2803 static int __kmp_release_rtm_queuing_lock(kmp_queuing_lock_t *lck,
2805 if (__kmp_is_unlocked_queuing_lock(lck)) {
2810 __kmp_release_queuing_lock(lck, gtid);
2812 return KMP_LOCK_RELEASED;
2815 static int __kmp_release_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
2817 return __kmp_release_rtm_queuing_lock(lck, gtid);
2820 KMP_ATTRIBUTE_TARGET_RTM
2821 static int __kmp_test_rtm_queuing_lock(kmp_queuing_lock_t *lck,
2823 unsigned retries = 3, status;
2826 if (status == _XBEGIN_STARTED && __kmp_is_unlocked_queuing_lock(lck)) {
2829 if (!(status & _XABORT_RETRY))
2831 }
while (retries--);
2833 return __kmp_test_queuing_lock(lck, gtid);
2836 static int __kmp_test_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
2838 return __kmp_test_rtm_queuing_lock(lck, gtid);
2842 typedef kmp_tas_lock_t kmp_rtm_spin_lock_t;
2844 static void __kmp_destroy_rtm_spin_lock(kmp_rtm_spin_lock_t *lck) {
2845 KMP_ATOMIC_ST_REL(&lck->lk.poll, 0);
2848 static void __kmp_destroy_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck) {
2849 __kmp_destroy_rtm_spin_lock(lck);
2852 KMP_ATTRIBUTE_TARGET_RTM
2853 static int __kmp_acquire_rtm_spin_lock(kmp_rtm_spin_lock_t *lck,
2855 unsigned retries = 3, status;
2856 kmp_int32 lock_free = KMP_LOCK_FREE(rtm_spin);
2857 kmp_int32 lock_busy = KMP_LOCK_BUSY(1, rtm_spin);
2860 if (status == _XBEGIN_STARTED) {
2861 if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == lock_free)
2862 return KMP_LOCK_ACQUIRED_FIRST;
2865 if ((status & _XABORT_EXPLICIT) && _XABORT_CODE(status) == 0xff) {
2867 while (KMP_ATOMIC_LD_RLX(&lck->lk.poll) != lock_free) {
2870 }
else if (!(status & _XABORT_RETRY))
2872 }
while (retries--);
2875 KMP_FSYNC_PREPARE(lck);
2876 kmp_backoff_t backoff = __kmp_spin_backoff_params;
2877 while (KMP_ATOMIC_LD_RLX(&lck->lk.poll) != lock_free ||
2878 !__kmp_atomic_compare_store_acq(&lck->lk.poll, lock_free, lock_busy)) {
2879 __kmp_spin_backoff(&backoff);
2881 KMP_FSYNC_ACQUIRED(lck);
2882 return KMP_LOCK_ACQUIRED_FIRST;
2885 static int __kmp_acquire_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck,
2887 return __kmp_acquire_rtm_spin_lock(lck, gtid);
2890 KMP_ATTRIBUTE_TARGET_RTM
2891 static int __kmp_release_rtm_spin_lock(kmp_rtm_spin_lock_t *lck,
2893 if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == KMP_LOCK_FREE(rtm_spin)) {
2898 KMP_FSYNC_RELEASING(lck);
2899 KMP_ATOMIC_ST_REL(&lck->lk.poll, KMP_LOCK_FREE(rtm_spin));
2901 return KMP_LOCK_RELEASED;
2904 static int __kmp_release_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck,
2906 return __kmp_release_rtm_spin_lock(lck, gtid);
2909 KMP_ATTRIBUTE_TARGET_RTM
2910 static int __kmp_test_rtm_spin_lock(kmp_rtm_spin_lock_t *lck, kmp_int32 gtid) {
2911 unsigned retries = 3, status;
2912 kmp_int32 lock_free = KMP_LOCK_FREE(rtm_spin);
2913 kmp_int32 lock_busy = KMP_LOCK_BUSY(1, rtm_spin);
2916 if (status == _XBEGIN_STARTED &&
2917 KMP_ATOMIC_LD_RLX(&lck->lk.poll) == lock_free) {
2920 if (!(status & _XABORT_RETRY))
2922 }
while (retries--);
2924 if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == lock_free &&
2925 __kmp_atomic_compare_store_acq(&lck->lk.poll, lock_free, lock_busy)) {
2926 KMP_FSYNC_ACQUIRED(lck);
2932 static int __kmp_test_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck,
2934 return __kmp_test_rtm_spin_lock(lck, gtid);
2940 static void __kmp_init_indirect_lock(kmp_dyna_lock_t *l,
2941 kmp_dyna_lockseq_t tag);
2942 static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock);
2943 static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
2944 static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
2945 static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
2946 static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
2948 static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
2950 static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
2954 #define KMP_FOREACH_LOCK_KIND(m, a) m(ticket, a) m(queuing, a) m(drdpa, a)
2956 #define expand1(lk, op) \
2957 static void __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock) { \
2958 __kmp_##op##_##lk##_##lock(&lock->lk); \
2960 #define expand2(lk, op) \
2961 static int __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock, \
2963 return __kmp_##op##_##lk##_##lock(&lock->lk, gtid); \
2965 #define expand3(lk, op) \
2966 static void __kmp_set_##lk##_##lock_flags(kmp_user_lock_p lock, \
2967 kmp_lock_flags_t flags) { \
2968 __kmp_set_##lk##_lock_flags(&lock->lk, flags); \
2970 #define expand4(lk, op) \
2971 static void __kmp_set_##lk##_##lock_location(kmp_user_lock_p lock, \
2972 const ident_t *loc) { \
2973 __kmp_set_##lk##_lock_location(&lock->lk, loc); \
2976 KMP_FOREACH_LOCK_KIND(expand1, init)
2977 KMP_FOREACH_LOCK_KIND(expand1, init_nested)
2978 KMP_FOREACH_LOCK_KIND(expand1, destroy)
2979 KMP_FOREACH_LOCK_KIND(expand1, destroy_nested)
2980 KMP_FOREACH_LOCK_KIND(expand2, acquire)
2981 KMP_FOREACH_LOCK_KIND(expand2, acquire_nested)
2982 KMP_FOREACH_LOCK_KIND(expand2, release)
2983 KMP_FOREACH_LOCK_KIND(expand2, release_nested)
2984 KMP_FOREACH_LOCK_KIND(expand2, test)
2985 KMP_FOREACH_LOCK_KIND(expand2, test_nested)
2986 KMP_FOREACH_LOCK_KIND(expand3, )
2987 KMP_FOREACH_LOCK_KIND(expand4, )
2998 #define expand(l, op) 0, __kmp_init_direct_lock,
2999 void (*__kmp_direct_init[])(kmp_dyna_lock_t *, kmp_dyna_lockseq_t) = {
3000 __kmp_init_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, init)};
3004 #define expand(l, op) 0, (void (*)(kmp_dyna_lock_t *))__kmp_##op##_##l##_lock,
3005 static void (*direct_destroy[])(kmp_dyna_lock_t *) = {
3006 __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
3008 #define expand(l, op) \
3009 0, (void (*)(kmp_dyna_lock_t *))__kmp_destroy_##l##_lock_with_checks,
3010 static void (*direct_destroy_check[])(kmp_dyna_lock_t *) = {
3011 __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
3015 #define expand(l, op) \
3016 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock,
3017 static int (*direct_set[])(kmp_dyna_lock_t *, kmp_int32) = {
3018 __kmp_set_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, acquire)};
3020 #define expand(l, op) \
3021 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks,
3022 static int (*direct_set_check[])(kmp_dyna_lock_t *, kmp_int32) = {
3023 __kmp_set_indirect_lock_with_checks, 0,
3024 KMP_FOREACH_D_LOCK(expand, acquire)};
3028 #define expand(l, op) \
3029 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock,
3030 static int (*direct_unset[])(kmp_dyna_lock_t *, kmp_int32) = {
3031 __kmp_unset_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, release)};
3032 static int (*direct_test[])(kmp_dyna_lock_t *, kmp_int32) = {
3033 __kmp_test_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, test)};
3035 #define expand(l, op) \
3036 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks,
3037 static int (*direct_unset_check[])(kmp_dyna_lock_t *, kmp_int32) = {
3038 __kmp_unset_indirect_lock_with_checks, 0,
3039 KMP_FOREACH_D_LOCK(expand, release)};
3040 static int (*direct_test_check[])(kmp_dyna_lock_t *, kmp_int32) = {
3041 __kmp_test_indirect_lock_with_checks, 0, KMP_FOREACH_D_LOCK(expand, test)};
3045 void (**__kmp_direct_destroy)(kmp_dyna_lock_t *) = 0;
3046 int (**__kmp_direct_set)(kmp_dyna_lock_t *, kmp_int32) = 0;
3047 int (**__kmp_direct_unset)(kmp_dyna_lock_t *, kmp_int32) = 0;
3048 int (**__kmp_direct_test)(kmp_dyna_lock_t *, kmp_int32) = 0;
3051 #define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock,
3052 void (*__kmp_indirect_init[])(kmp_user_lock_p) = {
3053 KMP_FOREACH_I_LOCK(expand, init)};
3056 #define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock,
3057 static void (*indirect_destroy[])(kmp_user_lock_p) = {
3058 KMP_FOREACH_I_LOCK(expand, destroy)};
3060 #define expand(l, op) \
3061 (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock_with_checks,
3062 static void (*indirect_destroy_check[])(kmp_user_lock_p) = {
3063 KMP_FOREACH_I_LOCK(expand, destroy)};
3067 #define expand(l, op) \
3068 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock,
3069 static int (*indirect_set[])(kmp_user_lock_p,
3070 kmp_int32) = {KMP_FOREACH_I_LOCK(expand, acquire)};
3072 #define expand(l, op) \
3073 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks,
3074 static int (*indirect_set_check[])(kmp_user_lock_p, kmp_int32) = {
3075 KMP_FOREACH_I_LOCK(expand, acquire)};
3079 #define expand(l, op) \
3080 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock,
3081 static int (*indirect_unset[])(kmp_user_lock_p, kmp_int32) = {
3082 KMP_FOREACH_I_LOCK(expand, release)};
3083 static int (*indirect_test[])(kmp_user_lock_p,
3084 kmp_int32) = {KMP_FOREACH_I_LOCK(expand, test)};
3086 #define expand(l, op) \
3087 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks,
3088 static int (*indirect_unset_check[])(kmp_user_lock_p, kmp_int32) = {
3089 KMP_FOREACH_I_LOCK(expand, release)};
3090 static int (*indirect_test_check[])(kmp_user_lock_p, kmp_int32) = {
3091 KMP_FOREACH_I_LOCK(expand, test)};
3095 void (**__kmp_indirect_destroy)(kmp_user_lock_p) = 0;
3096 int (**__kmp_indirect_set)(kmp_user_lock_p, kmp_int32) = 0;
3097 int (**__kmp_indirect_unset)(kmp_user_lock_p, kmp_int32) = 0;
3098 int (**__kmp_indirect_test)(kmp_user_lock_p, kmp_int32) = 0;
3101 kmp_indirect_lock_table_t __kmp_i_lock_table;
3104 static kmp_uint32 __kmp_indirect_lock_size[KMP_NUM_I_LOCKS] = {0};
3107 void (*__kmp_indirect_set_location[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
3109 void (*__kmp_indirect_set_flags[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
3110 kmp_lock_flags_t) = {0};
3111 const ident_t *(*__kmp_indirect_get_location[KMP_NUM_I_LOCKS])(
3112 kmp_user_lock_p) = {0};
3113 kmp_lock_flags_t (*__kmp_indirect_get_flags[KMP_NUM_I_LOCKS])(
3114 kmp_user_lock_p) = {0};
3117 static kmp_indirect_lock_t *__kmp_indirect_lock_pool[KMP_NUM_I_LOCKS] = {0};
3124 kmp_indirect_lock_t *__kmp_allocate_indirect_lock(
void **user_lock,
3126 kmp_indirect_locktag_t tag) {
3127 kmp_indirect_lock_t *lck;
3128 kmp_lock_index_t idx;
3130 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3132 if (__kmp_indirect_lock_pool[tag] != NULL) {
3134 lck = __kmp_indirect_lock_pool[tag];
3135 if (OMP_LOCK_T_SIZE <
sizeof(
void *))
3136 idx = lck->lock->pool.index;
3137 __kmp_indirect_lock_pool[tag] = (kmp_indirect_lock_t *)lck->lock->pool.next;
3138 KA_TRACE(20, (
"__kmp_allocate_indirect_lock: reusing an existing lock %p\n",
3141 idx = __kmp_i_lock_table.next;
3143 if (idx == __kmp_i_lock_table.size) {
3145 int row = __kmp_i_lock_table.size / KMP_I_LOCK_CHUNK;
3146 kmp_indirect_lock_t **new_table = (kmp_indirect_lock_t **)__kmp_allocate(
3147 2 * row *
sizeof(kmp_indirect_lock_t *));
3148 KMP_MEMCPY(new_table, __kmp_i_lock_table.table,
3149 row *
sizeof(kmp_indirect_lock_t *));
3150 kmp_indirect_lock_t **old_table = __kmp_i_lock_table.table;
3151 __kmp_i_lock_table.table = new_table;
3152 __kmp_free(old_table);
3154 for (
int i = row; i < 2 * row; ++i)
3155 *(__kmp_i_lock_table.table + i) = (kmp_indirect_lock_t *)__kmp_allocate(
3156 KMP_I_LOCK_CHUNK *
sizeof(kmp_indirect_lock_t));
3157 __kmp_i_lock_table.size = 2 * idx;
3159 __kmp_i_lock_table.next++;
3160 lck = KMP_GET_I_LOCK(idx);
3162 lck->lock = (kmp_user_lock_p)__kmp_allocate(__kmp_indirect_lock_size[tag]);
3164 (
"__kmp_allocate_indirect_lock: allocated a new lock %p\n", lck));
3167 __kmp_release_lock(&__kmp_global_lock, gtid);
3171 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3172 *((kmp_lock_index_t *)user_lock) = idx
3175 *((kmp_indirect_lock_t **)user_lock) = lck;
3182 static __forceinline kmp_indirect_lock_t *
3183 __kmp_lookup_indirect_lock(
void **user_lock,
const char *func) {
3184 if (__kmp_env_consistency_check) {
3185 kmp_indirect_lock_t *lck = NULL;
3186 if (user_lock == NULL) {
3187 KMP_FATAL(LockIsUninitialized, func);
3189 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3190 kmp_lock_index_t idx = KMP_EXTRACT_I_INDEX(user_lock);
3191 if (idx >= __kmp_i_lock_table.size) {
3192 KMP_FATAL(LockIsUninitialized, func);
3194 lck = KMP_GET_I_LOCK(idx);
3196 lck = *((kmp_indirect_lock_t **)user_lock);
3199 KMP_FATAL(LockIsUninitialized, func);
3203 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3204 return KMP_GET_I_LOCK(KMP_EXTRACT_I_INDEX(user_lock));
3206 return *((kmp_indirect_lock_t **)user_lock);
3211 static void __kmp_init_indirect_lock(kmp_dyna_lock_t *lock,
3212 kmp_dyna_lockseq_t seq) {
3213 #if KMP_USE_ADAPTIVE_LOCKS
3214 if (seq == lockseq_adaptive && !__kmp_cpuinfo.rtm) {
3215 KMP_WARNING(AdaptiveNotSupported,
"kmp_lockseq_t",
"adaptive");
3216 seq = lockseq_queuing;
3220 if (seq == lockseq_rtm_queuing && !__kmp_cpuinfo.rtm) {
3221 seq = lockseq_queuing;
3224 kmp_indirect_locktag_t tag = KMP_GET_I_TAG(seq);
3225 kmp_indirect_lock_t *l =
3226 __kmp_allocate_indirect_lock((
void **)lock, __kmp_entry_gtid(), tag);
3227 KMP_I_LOCK_FUNC(l, init)(l->lock);
3229 20, (
"__kmp_init_indirect_lock: initialized indirect lock with type#%d\n",
3233 static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock) {
3234 kmp_uint32 gtid = __kmp_entry_gtid();
3235 kmp_indirect_lock_t *l =
3236 __kmp_lookup_indirect_lock((
void **)lock,
"omp_destroy_lock");
3237 KMP_I_LOCK_FUNC(l, destroy)(l->lock);
3238 kmp_indirect_locktag_t tag = l->type;
3240 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3243 l->lock->pool.next = (kmp_user_lock_p)__kmp_indirect_lock_pool[tag];
3244 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3245 l->lock->pool.index = KMP_EXTRACT_I_INDEX(lock);
3247 __kmp_indirect_lock_pool[tag] = l;
3249 __kmp_release_lock(&__kmp_global_lock, gtid);
3252 static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
3253 kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
3254 return KMP_I_LOCK_FUNC(l, set)(l->lock, gtid);
3257 static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
3258 kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
3259 return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
3262 static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
3263 kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
3264 return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
3267 static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
3269 kmp_indirect_lock_t *l =
3270 __kmp_lookup_indirect_lock((
void **)lock,
"omp_set_lock");
3271 return KMP_I_LOCK_FUNC(l, set)(l->lock, gtid);
3274 static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
3276 kmp_indirect_lock_t *l =
3277 __kmp_lookup_indirect_lock((
void **)lock,
"omp_unset_lock");
3278 return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
3281 static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
3283 kmp_indirect_lock_t *l =
3284 __kmp_lookup_indirect_lock((
void **)lock,
"omp_test_lock");
3285 return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
3288 kmp_dyna_lockseq_t __kmp_user_lock_seq = lockseq_queuing;
3291 kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p lck, kmp_uint32 seq) {
3294 case lockseq_nested_tas:
3295 return __kmp_get_tas_lock_owner((kmp_tas_lock_t *)lck);
3298 case lockseq_nested_futex:
3299 return __kmp_get_futex_lock_owner((kmp_futex_lock_t *)lck);
3301 case lockseq_ticket:
3302 case lockseq_nested_ticket:
3303 return __kmp_get_ticket_lock_owner((kmp_ticket_lock_t *)lck);
3304 case lockseq_queuing:
3305 case lockseq_nested_queuing:
3306 #if KMP_USE_ADAPTIVE_LOCKS
3307 case lockseq_adaptive:
3309 return __kmp_get_queuing_lock_owner((kmp_queuing_lock_t *)lck);
3311 case lockseq_nested_drdpa:
3312 return __kmp_get_drdpa_lock_owner((kmp_drdpa_lock_t *)lck);
3319 void __kmp_init_dynamic_user_locks() {
3321 if (__kmp_env_consistency_check) {
3322 __kmp_direct_set = direct_set_check;
3323 __kmp_direct_unset = direct_unset_check;
3324 __kmp_direct_test = direct_test_check;
3325 __kmp_direct_destroy = direct_destroy_check;
3326 __kmp_indirect_set = indirect_set_check;
3327 __kmp_indirect_unset = indirect_unset_check;
3328 __kmp_indirect_test = indirect_test_check;
3329 __kmp_indirect_destroy = indirect_destroy_check;
3331 __kmp_direct_set = direct_set;
3332 __kmp_direct_unset = direct_unset;
3333 __kmp_direct_test = direct_test;
3334 __kmp_direct_destroy = direct_destroy;
3335 __kmp_indirect_set = indirect_set;
3336 __kmp_indirect_unset = indirect_unset;
3337 __kmp_indirect_test = indirect_test;
3338 __kmp_indirect_destroy = indirect_destroy;
3343 if (__kmp_init_user_locks)
3347 __kmp_i_lock_table.size = KMP_I_LOCK_CHUNK;
3348 __kmp_i_lock_table.table =
3349 (kmp_indirect_lock_t **)__kmp_allocate(
sizeof(kmp_indirect_lock_t *));
3350 *(__kmp_i_lock_table.table) = (kmp_indirect_lock_t *)__kmp_allocate(
3351 KMP_I_LOCK_CHUNK *
sizeof(kmp_indirect_lock_t));
3352 __kmp_i_lock_table.next = 0;
3355 __kmp_indirect_lock_size[locktag_ticket] =
sizeof(kmp_ticket_lock_t);
3356 __kmp_indirect_lock_size[locktag_queuing] =
sizeof(kmp_queuing_lock_t);
3357 #if KMP_USE_ADAPTIVE_LOCKS
3358 __kmp_indirect_lock_size[locktag_adaptive] =
sizeof(kmp_adaptive_lock_t);
3360 __kmp_indirect_lock_size[locktag_drdpa] =
sizeof(kmp_drdpa_lock_t);
3362 __kmp_indirect_lock_size[locktag_rtm_queuing] =
sizeof(kmp_queuing_lock_t);
3364 __kmp_indirect_lock_size[locktag_nested_tas] =
sizeof(kmp_tas_lock_t);
3366 __kmp_indirect_lock_size[locktag_nested_futex] =
sizeof(kmp_futex_lock_t);
3368 __kmp_indirect_lock_size[locktag_nested_ticket] =
sizeof(kmp_ticket_lock_t);
3369 __kmp_indirect_lock_size[locktag_nested_queuing] =
sizeof(kmp_queuing_lock_t);
3370 __kmp_indirect_lock_size[locktag_nested_drdpa] =
sizeof(kmp_drdpa_lock_t);
3373 #define fill_jumps(table, expand, sep) \
3375 table[locktag##sep##ticket] = expand(ticket); \
3376 table[locktag##sep##queuing] = expand(queuing); \
3377 table[locktag##sep##drdpa] = expand(drdpa); \
3380 #if KMP_USE_ADAPTIVE_LOCKS
3381 #define fill_table(table, expand) \
3383 fill_jumps(table, expand, _); \
3384 table[locktag_adaptive] = expand(queuing); \
3385 fill_jumps(table, expand, _nested_); \
3388 #define fill_table(table, expand) \
3390 fill_jumps(table, expand, _); \
3391 fill_jumps(table, expand, _nested_); \
3396 (void (*)(kmp_user_lock_p, const ident_t *)) __kmp_set_##l##_lock_location
3397 fill_table(__kmp_indirect_set_location, expand);
3400 (void (*)(kmp_user_lock_p, kmp_lock_flags_t)) __kmp_set_##l##_lock_flags
3401 fill_table(__kmp_indirect_set_flags, expand);
3404 (const ident_t *(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_location
3405 fill_table(__kmp_indirect_get_location, expand);
3408 (kmp_lock_flags_t(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_flags
3409 fill_table(__kmp_indirect_get_flags, expand);
3412 __kmp_init_user_locks = TRUE;
3416 void __kmp_cleanup_indirect_user_locks() {
3422 for (k = 0; k < KMP_NUM_I_LOCKS; ++k) {
3423 kmp_indirect_lock_t *l = __kmp_indirect_lock_pool[k];
3425 kmp_indirect_lock_t *ll = l;
3426 l = (kmp_indirect_lock_t *)l->lock->pool.next;
3427 KA_TRACE(20, (
"__kmp_cleanup_indirect_user_locks: freeing %p from pool\n",
3429 __kmp_free(ll->lock);
3432 __kmp_indirect_lock_pool[k] = NULL;
3435 for (i = 0; i < __kmp_i_lock_table.next; i++) {
3436 kmp_indirect_lock_t *l = KMP_GET_I_LOCK(i);
3437 if (l->lock != NULL) {
3439 KMP_I_LOCK_FUNC(l, destroy)(l->lock);
3442 (
"__kmp_cleanup_indirect_user_locks: destroy/freeing %p from table\n",
3444 __kmp_free(l->lock);
3448 for (i = 0; i < __kmp_i_lock_table.size / KMP_I_LOCK_CHUNK; i++)
3449 __kmp_free(__kmp_i_lock_table.table[i]);
3450 __kmp_free(__kmp_i_lock_table.table);
3452 __kmp_init_user_locks = FALSE;
3455 enum kmp_lock_kind __kmp_user_lock_kind = lk_default;
3456 int __kmp_num_locks_in_block = 1;
3460 static void __kmp_init_tas_lock_with_checks(kmp_tas_lock_t *lck) {
3461 __kmp_init_tas_lock(lck);
3464 static void __kmp_init_nested_tas_lock_with_checks(kmp_tas_lock_t *lck) {
3465 __kmp_init_nested_tas_lock(lck);
3469 static void __kmp_init_futex_lock_with_checks(kmp_futex_lock_t *lck) {
3470 __kmp_init_futex_lock(lck);
3473 static void __kmp_init_nested_futex_lock_with_checks(kmp_futex_lock_t *lck) {
3474 __kmp_init_nested_futex_lock(lck);
3478 static int __kmp_is_ticket_lock_initialized(kmp_ticket_lock_t *lck) {
3479 return lck == lck->lk.self;
3482 static void __kmp_init_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
3483 __kmp_init_ticket_lock(lck);
3486 static void __kmp_init_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
3487 __kmp_init_nested_ticket_lock(lck);
3490 static int __kmp_is_queuing_lock_initialized(kmp_queuing_lock_t *lck) {
3491 return lck == lck->lk.initialized;
3494 static void __kmp_init_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
3495 __kmp_init_queuing_lock(lck);
3499 __kmp_init_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
3500 __kmp_init_nested_queuing_lock(lck);
3503 #if KMP_USE_ADAPTIVE_LOCKS
3504 static void __kmp_init_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
3505 __kmp_init_adaptive_lock(lck);
3509 static int __kmp_is_drdpa_lock_initialized(kmp_drdpa_lock_t *lck) {
3510 return lck == lck->lk.initialized;
3513 static void __kmp_init_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
3514 __kmp_init_drdpa_lock(lck);
3517 static void __kmp_init_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
3518 __kmp_init_nested_drdpa_lock(lck);
3525 enum kmp_lock_kind __kmp_user_lock_kind = lk_default;
3527 size_t __kmp_base_user_lock_size = 0;
3528 size_t __kmp_user_lock_size = 0;
3530 kmp_int32 (*__kmp_get_user_lock_owner_)(kmp_user_lock_p lck) = NULL;
3531 int (*__kmp_acquire_user_lock_with_checks_)(kmp_user_lock_p lck,
3532 kmp_int32 gtid) = NULL;
3534 int (*__kmp_test_user_lock_with_checks_)(kmp_user_lock_p lck,
3535 kmp_int32 gtid) = NULL;
3536 int (*__kmp_release_user_lock_with_checks_)(kmp_user_lock_p lck,
3537 kmp_int32 gtid) = NULL;
3538 void (*__kmp_init_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3539 void (*__kmp_destroy_user_lock_)(kmp_user_lock_p lck) = NULL;
3540 void (*__kmp_destroy_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3541 int (*__kmp_acquire_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
3542 kmp_int32 gtid) = NULL;
3544 int (*__kmp_test_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
3545 kmp_int32 gtid) = NULL;
3546 int (*__kmp_release_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
3547 kmp_int32 gtid) = NULL;
3548 void (*__kmp_init_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3549 void (*__kmp_destroy_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3551 int (*__kmp_is_user_lock_initialized_)(kmp_user_lock_p lck) = NULL;
3552 const ident_t *(*__kmp_get_user_lock_location_)(kmp_user_lock_p lck) = NULL;
3553 void (*__kmp_set_user_lock_location_)(kmp_user_lock_p lck,
3555 kmp_lock_flags_t (*__kmp_get_user_lock_flags_)(kmp_user_lock_p lck) = NULL;
3556 void (*__kmp_set_user_lock_flags_)(kmp_user_lock_p lck,
3557 kmp_lock_flags_t flags) = NULL;
3559 void __kmp_set_user_lock_vptrs(kmp_lock_kind_t user_lock_kind) {
3560 switch (user_lock_kind) {
3566 __kmp_base_user_lock_size =
sizeof(kmp_base_tas_lock_t);
3567 __kmp_user_lock_size =
sizeof(kmp_tas_lock_t);
3569 __kmp_get_user_lock_owner_ =
3570 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_tas_lock_owner);
3572 if (__kmp_env_consistency_check) {
3573 KMP_BIND_USER_LOCK_WITH_CHECKS(tas);
3574 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(tas);
3576 KMP_BIND_USER_LOCK(tas);
3577 KMP_BIND_NESTED_USER_LOCK(tas);
3580 __kmp_destroy_user_lock_ =
3581 (void (*)(kmp_user_lock_p))(&__kmp_destroy_tas_lock);
3583 __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;
3585 __kmp_get_user_lock_location_ = (
const ident_t *(*)(kmp_user_lock_p))NULL;
3587 __kmp_set_user_lock_location_ =
3588 (void (*)(kmp_user_lock_p,
const ident_t *))NULL;
3590 __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;
3592 __kmp_set_user_lock_flags_ =
3593 (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
3599 __kmp_base_user_lock_size =
sizeof(kmp_base_futex_lock_t);
3600 __kmp_user_lock_size =
sizeof(kmp_futex_lock_t);
3602 __kmp_get_user_lock_owner_ =
3603 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_futex_lock_owner);
3605 if (__kmp_env_consistency_check) {
3606 KMP_BIND_USER_LOCK_WITH_CHECKS(futex);
3607 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(futex);
3609 KMP_BIND_USER_LOCK(futex);
3610 KMP_BIND_NESTED_USER_LOCK(futex);
3613 __kmp_destroy_user_lock_ =
3614 (void (*)(kmp_user_lock_p))(&__kmp_destroy_futex_lock);
3616 __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;
3618 __kmp_get_user_lock_location_ = (
const ident_t *(*)(kmp_user_lock_p))NULL;
3620 __kmp_set_user_lock_location_ =
3621 (void (*)(kmp_user_lock_p,
const ident_t *))NULL;
3623 __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;
3625 __kmp_set_user_lock_flags_ =
3626 (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
3632 __kmp_base_user_lock_size =
sizeof(kmp_base_ticket_lock_t);
3633 __kmp_user_lock_size =
sizeof(kmp_ticket_lock_t);
3635 __kmp_get_user_lock_owner_ =
3636 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_owner);
3638 if (__kmp_env_consistency_check) {
3639 KMP_BIND_USER_LOCK_WITH_CHECKS(ticket);
3640 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(ticket);
3642 KMP_BIND_USER_LOCK(ticket);
3643 KMP_BIND_NESTED_USER_LOCK(ticket);
3646 __kmp_destroy_user_lock_ =
3647 (void (*)(kmp_user_lock_p))(&__kmp_destroy_ticket_lock);
3649 __kmp_is_user_lock_initialized_ =
3650 (int (*)(kmp_user_lock_p))(&__kmp_is_ticket_lock_initialized);
3652 __kmp_get_user_lock_location_ =
3653 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_location);
3655 __kmp_set_user_lock_location_ = (void (*)(
3656 kmp_user_lock_p,
const ident_t *))(&__kmp_set_ticket_lock_location);
3658 __kmp_get_user_lock_flags_ =
3659 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_flags);
3661 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3662 &__kmp_set_ticket_lock_flags);
3666 __kmp_base_user_lock_size =
sizeof(kmp_base_queuing_lock_t);
3667 __kmp_user_lock_size =
sizeof(kmp_queuing_lock_t);
3669 __kmp_get_user_lock_owner_ =
3670 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);
3672 if (__kmp_env_consistency_check) {
3673 KMP_BIND_USER_LOCK_WITH_CHECKS(queuing);
3674 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(queuing);
3676 KMP_BIND_USER_LOCK(queuing);
3677 KMP_BIND_NESTED_USER_LOCK(queuing);
3680 __kmp_destroy_user_lock_ =
3681 (void (*)(kmp_user_lock_p))(&__kmp_destroy_queuing_lock);
3683 __kmp_is_user_lock_initialized_ =
3684 (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);
3686 __kmp_get_user_lock_location_ =
3687 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);
3689 __kmp_set_user_lock_location_ = (void (*)(
3690 kmp_user_lock_p,
const ident_t *))(&__kmp_set_queuing_lock_location);
3692 __kmp_get_user_lock_flags_ =
3693 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);
3695 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3696 &__kmp_set_queuing_lock_flags);
3699 #if KMP_USE_ADAPTIVE_LOCKS
3701 __kmp_base_user_lock_size =
sizeof(kmp_base_adaptive_lock_t);
3702 __kmp_user_lock_size =
sizeof(kmp_adaptive_lock_t);
3704 __kmp_get_user_lock_owner_ =
3705 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);
3707 if (__kmp_env_consistency_check) {
3708 KMP_BIND_USER_LOCK_WITH_CHECKS(adaptive);
3710 KMP_BIND_USER_LOCK(adaptive);
3713 __kmp_destroy_user_lock_ =
3714 (void (*)(kmp_user_lock_p))(&__kmp_destroy_adaptive_lock);
3716 __kmp_is_user_lock_initialized_ =
3717 (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);
3719 __kmp_get_user_lock_location_ =
3720 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);
3722 __kmp_set_user_lock_location_ = (void (*)(
3723 kmp_user_lock_p,
const ident_t *))(&__kmp_set_queuing_lock_location);
3725 __kmp_get_user_lock_flags_ =
3726 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);
3728 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3729 &__kmp_set_queuing_lock_flags);
3735 __kmp_base_user_lock_size =
sizeof(kmp_base_drdpa_lock_t);
3736 __kmp_user_lock_size =
sizeof(kmp_drdpa_lock_t);
3738 __kmp_get_user_lock_owner_ =
3739 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_owner);
3741 if (__kmp_env_consistency_check) {
3742 KMP_BIND_USER_LOCK_WITH_CHECKS(drdpa);
3743 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(drdpa);
3745 KMP_BIND_USER_LOCK(drdpa);
3746 KMP_BIND_NESTED_USER_LOCK(drdpa);
3749 __kmp_destroy_user_lock_ =
3750 (void (*)(kmp_user_lock_p))(&__kmp_destroy_drdpa_lock);
3752 __kmp_is_user_lock_initialized_ =
3753 (int (*)(kmp_user_lock_p))(&__kmp_is_drdpa_lock_initialized);
3755 __kmp_get_user_lock_location_ =
3756 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_location);
3758 __kmp_set_user_lock_location_ = (void (*)(
3759 kmp_user_lock_p,
const ident_t *))(&__kmp_set_drdpa_lock_location);
3761 __kmp_get_user_lock_flags_ =
3762 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_flags);
3764 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3765 &__kmp_set_drdpa_lock_flags);
3773 kmp_lock_table_t __kmp_user_lock_table = {1, 0, NULL};
3774 kmp_user_lock_p __kmp_lock_pool = NULL;
3777 kmp_block_of_locks *__kmp_lock_blocks = NULL;
3778 int __kmp_num_locks_in_block = 1;
3780 static kmp_lock_index_t __kmp_lock_table_insert(kmp_user_lock_p lck) {
3782 kmp_lock_index_t index;
3783 if (__kmp_user_lock_table.used >= __kmp_user_lock_table.allocated) {
3784 kmp_lock_index_t size;
3785 kmp_user_lock_p *table;
3787 if (__kmp_user_lock_table.allocated == 0) {
3790 size = __kmp_user_lock_table.allocated * 2;
3792 table = (kmp_user_lock_p *)__kmp_allocate(
sizeof(kmp_user_lock_p) * size);
3793 KMP_MEMCPY(table + 1, __kmp_user_lock_table.table + 1,
3794 sizeof(kmp_user_lock_p) * (__kmp_user_lock_table.used - 1));
3795 table[0] = (kmp_user_lock_p)__kmp_user_lock_table.table;
3800 __kmp_user_lock_table.table = table;
3801 __kmp_user_lock_table.allocated = size;
3803 KMP_DEBUG_ASSERT(__kmp_user_lock_table.used <
3804 __kmp_user_lock_table.allocated);
3805 index = __kmp_user_lock_table.used;
3806 __kmp_user_lock_table.table[index] = lck;
3807 ++__kmp_user_lock_table.used;
3811 static kmp_user_lock_p __kmp_lock_block_allocate() {
3813 static int last_index = 0;
3814 if ((last_index >= __kmp_num_locks_in_block) || (__kmp_lock_blocks == NULL)) {
3818 KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
3819 size_t space_for_locks = __kmp_user_lock_size * __kmp_num_locks_in_block;
3821 (
char *)__kmp_allocate(space_for_locks +
sizeof(kmp_block_of_locks));
3823 kmp_block_of_locks *new_block =
3824 (kmp_block_of_locks *)(&buffer[space_for_locks]);
3825 new_block->next_block = __kmp_lock_blocks;
3826 new_block->locks = (
void *)buffer;
3829 __kmp_lock_blocks = new_block;
3831 kmp_user_lock_p ret = (kmp_user_lock_p)(&(
3832 ((
char *)(__kmp_lock_blocks->locks))[last_index * __kmp_user_lock_size]));
3839 kmp_user_lock_p __kmp_user_lock_allocate(
void **user_lock, kmp_int32 gtid,
3840 kmp_lock_flags_t flags) {
3841 kmp_user_lock_p lck;
3842 kmp_lock_index_t index;
3843 KMP_DEBUG_ASSERT(user_lock);
3845 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3847 if (__kmp_lock_pool == NULL) {
3852 ANNOTATE_IGNORE_WRITES_BEGIN();
3853 if (__kmp_num_locks_in_block <= 1) {
3854 lck = (kmp_user_lock_p)__kmp_allocate(__kmp_user_lock_size);
3856 lck = __kmp_lock_block_allocate();
3858 ANNOTATE_IGNORE_WRITES_END();
3862 index = __kmp_lock_table_insert(lck);
3865 lck = __kmp_lock_pool;
3866 index = __kmp_lock_pool->pool.index;
3867 __kmp_lock_pool = __kmp_lock_pool->pool.next;
3872 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3873 *((kmp_lock_index_t *)user_lock) = index;
3875 *((kmp_user_lock_p *)user_lock) = lck;
3879 __kmp_set_user_lock_flags(lck, flags);
3881 __kmp_release_lock(&__kmp_global_lock, gtid);
3887 void __kmp_user_lock_free(
void **user_lock, kmp_int32 gtid,
3888 kmp_user_lock_p lck) {
3889 KMP_DEBUG_ASSERT(user_lock != NULL);
3890 KMP_DEBUG_ASSERT(lck != NULL);
3892 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3894 lck->pool.next = __kmp_lock_pool;
3895 __kmp_lock_pool = lck;
3896 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3897 kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
3898 KMP_DEBUG_ASSERT(0 < index && index <= __kmp_user_lock_table.used);
3899 lck->pool.index = index;
3902 __kmp_release_lock(&__kmp_global_lock, gtid);
3905 kmp_user_lock_p __kmp_lookup_user_lock(
void **user_lock,
char const *func) {
3906 kmp_user_lock_p lck = NULL;
3908 if (__kmp_env_consistency_check) {
3909 if (user_lock == NULL) {
3910 KMP_FATAL(LockIsUninitialized, func);
3914 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3915 kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
3916 if (__kmp_env_consistency_check) {
3917 if (!(0 < index && index < __kmp_user_lock_table.used)) {
3918 KMP_FATAL(LockIsUninitialized, func);
3921 KMP_DEBUG_ASSERT(0 < index && index < __kmp_user_lock_table.used);
3922 KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
3923 lck = __kmp_user_lock_table.table[index];
3925 lck = *((kmp_user_lock_p *)user_lock);
3928 if (__kmp_env_consistency_check) {
3930 KMP_FATAL(LockIsUninitialized, func);
3937 void __kmp_cleanup_user_locks(
void) {
3940 __kmp_lock_pool = NULL;
3942 #define IS_CRITICAL(lck) \
3943 ((__kmp_get_user_lock_flags_ != NULL) && \
3944 ((*__kmp_get_user_lock_flags_)(lck)&kmp_lf_critical_section))
3969 while (__kmp_user_lock_table.used > 1) {
3974 kmp_user_lock_p lck =
3975 __kmp_user_lock_table.table[--__kmp_user_lock_table.used];
3977 if ((__kmp_is_user_lock_initialized_ != NULL) &&
3978 (*__kmp_is_user_lock_initialized_)(lck)) {
3982 if (__kmp_env_consistency_check && (!IS_CRITICAL(lck)) &&
3983 ((loc = __kmp_get_user_lock_location(lck)) != NULL) &&
3985 kmp_str_loc_t str_loc = __kmp_str_loc_init(loc->
psource,
false);
3986 KMP_WARNING(CnsLockNotDestroyed, str_loc.file, str_loc.line);
3987 __kmp_str_loc_free(&str_loc);
3991 if (IS_CRITICAL(lck)) {
3994 (
"__kmp_cleanup_user_locks: free critical section lock %p (%p)\n",
3995 lck, *(
void **)lck));
3997 KA_TRACE(20, (
"__kmp_cleanup_user_locks: free lock %p (%p)\n", lck,
4003 __kmp_destroy_user_lock(lck);
4007 if (__kmp_lock_blocks == NULL) {
4015 kmp_user_lock_p *table_ptr = __kmp_user_lock_table.table;
4016 __kmp_user_lock_table.table = NULL;
4017 __kmp_user_lock_table.allocated = 0;
4019 while (table_ptr != NULL) {
4022 kmp_user_lock_p *next = (kmp_user_lock_p *)(table_ptr[0]);
4023 __kmp_free(table_ptr);
4028 kmp_block_of_locks_t *block_ptr = __kmp_lock_blocks;
4029 __kmp_lock_blocks = NULL;
4031 while (block_ptr != NULL) {
4032 kmp_block_of_locks_t *next = block_ptr->next_block;
4033 __kmp_free(block_ptr->locks);
4038 TCW_4(__kmp_init_user_locks, FALSE);
void open(const char *filename, const char *mode, const char *env_var=nullptr)