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;
1096 ompt_state_t prev_state = ompt_state_undefined;
1100 (
"__kmp_acquire_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));
1102 KMP_FSYNC_PREPARE(lck);
1103 KMP_DEBUG_ASSERT(this_thr != NULL);
1104 spin_here_p = &this_thr->th.th_spin_here;
1106 #ifdef DEBUG_QUEUING_LOCKS
1107 TRACE_LOCK(gtid + 1,
"acq ent");
1109 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1110 if (this_thr->th.th_next_waiting != 0)
1111 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1113 KMP_DEBUG_ASSERT(!*spin_here_p);
1114 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
1123 *spin_here_p = TRUE;
1135 #ifdef DEBUG_QUEUING_LOCKS
1137 TRACE_LOCK_HT(gtid + 1,
"acq read: ", head, tail);
1145 enqueued = KMP_COMPARE_AND_STORE_ACQ64((
volatile kmp_int64 *)tail_id_p,
1147 KMP_PACK_64(gtid + 1, gtid + 1));
1148 #ifdef DEBUG_QUEUING_LOCKS
1150 TRACE_LOCK(gtid + 1,
"acq enq: (-1,0)->(tid,tid)");
1156 KMP_DEBUG_ASSERT(tail != gtid + 1);
1158 #ifdef DEBUG_QUEUING_LOCKS
1159 TRACE_LOCK_HT(gtid + 1,
"acq read: ", head, tail);
1166 enqueued = KMP_COMPARE_AND_STORE_ACQ32(tail_id_p, tail, gtid + 1);
1168 #ifdef DEBUG_QUEUING_LOCKS
1170 TRACE_LOCK(gtid + 1,
"acq enq: (h,t)->(h,tid)");
1177 kmp_int32 grabbed_lock;
1179 #ifdef DEBUG_QUEUING_LOCKS
1181 TRACE_LOCK_HT(gtid + 1,
"acq read: ", head, tail);
1187 grabbed_lock = KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1);
1191 *spin_here_p = FALSE;
1195 (
"__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: no queuing\n",
1197 #ifdef DEBUG_QUEUING_LOCKS
1198 TRACE_LOCK_HT(gtid + 1,
"acq exit: ", head, 0);
1202 if (ompt_enabled.enabled && prev_state != ompt_state_undefined) {
1204 this_thr->th.ompt_thread_info.state = prev_state;
1205 this_thr->th.ompt_thread_info.wait_id = 0;
1209 KMP_FSYNC_ACQUIRED(lck);
1210 return KMP_LOCK_ACQUIRED_FIRST;
1217 if (ompt_enabled.enabled && prev_state == ompt_state_undefined) {
1219 prev_state = this_thr->th.ompt_thread_info.state;
1220 this_thr->th.ompt_thread_info.wait_id = (uint64_t)lck;
1221 this_thr->th.ompt_thread_info.state = ompt_state_wait_lock;
1227 kmp_info_t *tail_thr = __kmp_thread_from_gtid(tail - 1);
1228 KMP_ASSERT(tail_thr != NULL);
1229 tail_thr->th.th_next_waiting = gtid + 1;
1233 (
"__kmp_acquire_queuing_lock: lck:%p, T#%d waiting for lock\n",
1238 KMP_WAIT(spin_here_p, FALSE, KMP_EQ, lck);
1243 #ifdef DEBUG_QUEUING_LOCKS
1244 TRACE_LOCK(gtid + 1,
"acq spin");
1246 if (this_thr->th.th_next_waiting != 0)
1247 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1249 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
1250 KA_TRACE(1000, (
"__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: after "
1251 "waiting on queue\n",
1254 #ifdef DEBUG_QUEUING_LOCKS
1255 TRACE_LOCK(gtid + 1,
"acq exit 2");
1260 this_thr->th.ompt_thread_info.state = prev_state;
1261 this_thr->th.ompt_thread_info.wait_id = 0;
1265 return KMP_LOCK_ACQUIRED_FIRST;
1271 KMP_YIELD_OVERSUB();
1273 #ifdef DEBUG_QUEUING_LOCKS
1274 TRACE_LOCK(gtid + 1,
"acq retry");
1277 KMP_ASSERT2(0,
"should not get here");
1278 return KMP_LOCK_ACQUIRED_FIRST;
1281 int __kmp_acquire_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1282 KMP_DEBUG_ASSERT(gtid >= 0);
1284 int retval = __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
1285 ANNOTATE_QUEUING_ACQUIRED(lck);
1289 static int __kmp_acquire_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1291 char const *
const func =
"omp_set_lock";
1292 if (lck->lk.initialized != lck) {
1293 KMP_FATAL(LockIsUninitialized, func);
1295 if (__kmp_is_queuing_lock_nestable(lck)) {
1296 KMP_FATAL(LockNestableUsedAsSimple, func);
1298 if (__kmp_get_queuing_lock_owner(lck) == gtid) {
1299 KMP_FATAL(LockIsAlreadyOwned, func);
1302 __kmp_acquire_queuing_lock(lck, gtid);
1304 lck->lk.owner_id = gtid + 1;
1305 return KMP_LOCK_ACQUIRED_FIRST;
1308 int __kmp_test_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1309 volatile kmp_int32 *head_id_p = &lck->lk.head_id;
1312 kmp_info_t *this_thr;
1315 KA_TRACE(1000, (
"__kmp_test_queuing_lock: T#%d entering\n", gtid));
1316 KMP_DEBUG_ASSERT(gtid >= 0);
1318 this_thr = __kmp_thread_from_gtid(gtid);
1319 KMP_DEBUG_ASSERT(this_thr != NULL);
1320 KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
1327 if (KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1)) {
1329 (
"__kmp_test_queuing_lock: T#%d exiting: holding lock\n", gtid));
1330 KMP_FSYNC_ACQUIRED(lck);
1331 ANNOTATE_QUEUING_ACQUIRED(lck);
1337 (
"__kmp_test_queuing_lock: T#%d exiting: without lock\n", gtid));
1341 static int __kmp_test_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1343 char const *
const func =
"omp_test_lock";
1344 if (lck->lk.initialized != lck) {
1345 KMP_FATAL(LockIsUninitialized, func);
1347 if (__kmp_is_queuing_lock_nestable(lck)) {
1348 KMP_FATAL(LockNestableUsedAsSimple, func);
1351 int retval = __kmp_test_queuing_lock(lck, gtid);
1354 lck->lk.owner_id = gtid + 1;
1359 int __kmp_release_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1360 kmp_info_t *this_thr;
1361 volatile kmp_int32 *head_id_p = &lck->lk.head_id;
1362 volatile kmp_int32 *tail_id_p = &lck->lk.tail_id;
1365 (
"__kmp_release_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));
1366 KMP_DEBUG_ASSERT(gtid >= 0);
1367 this_thr = __kmp_thread_from_gtid(gtid);
1368 KMP_DEBUG_ASSERT(this_thr != NULL);
1369 #ifdef DEBUG_QUEUING_LOCKS
1370 TRACE_LOCK(gtid + 1,
"rel ent");
1372 if (this_thr->th.th_spin_here)
1373 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1374 if (this_thr->th.th_next_waiting != 0)
1375 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1377 KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
1378 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
1380 KMP_FSYNC_RELEASING(lck);
1381 ANNOTATE_QUEUING_RELEASED(lck);
1390 #ifdef DEBUG_QUEUING_LOCKS
1392 TRACE_LOCK_HT(gtid + 1,
"rel read: ", head, tail);
1394 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1396 KMP_DEBUG_ASSERT(head !=
1401 if (KMP_COMPARE_AND_STORE_REL32(head_id_p, -1, 0)) {
1404 (
"__kmp_release_queuing_lock: lck:%p, T#%d exiting: queue empty\n",
1406 #ifdef DEBUG_QUEUING_LOCKS
1407 TRACE_LOCK_HT(gtid + 1,
"rel exit: ", 0, 0);
1413 return KMP_LOCK_RELEASED;
1420 #ifdef DEBUG_QUEUING_LOCKS
1422 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1424 KMP_DEBUG_ASSERT(head > 0);
1427 dequeued = KMP_COMPARE_AND_STORE_REL64(
1428 RCAST(
volatile kmp_int64 *, tail_id_p), KMP_PACK_64(head, head),
1429 KMP_PACK_64(-1, 0));
1430 #ifdef DEBUG_QUEUING_LOCKS
1431 TRACE_LOCK(gtid + 1,
"rel deq: (h,h)->(-1,0)");
1435 volatile kmp_int32 *waiting_id_p;
1436 kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
1437 KMP_DEBUG_ASSERT(head_thr != NULL);
1438 waiting_id_p = &head_thr->th.th_next_waiting;
1441 #ifdef DEBUG_QUEUING_LOCKS
1442 if (head <= 0 || tail <= 0)
1443 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1445 KMP_DEBUG_ASSERT(head > 0 && tail > 0);
1452 KMP_WAIT((
volatile kmp_uint32 *)waiting_id_p, 0, KMP_NEQ, NULL);
1453 #ifdef DEBUG_QUEUING_LOCKS
1454 TRACE_LOCK(gtid + 1,
"rel deq: (h,t)->(h',t)");
1461 kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
1462 KMP_DEBUG_ASSERT(head_thr != NULL);
1465 #ifdef DEBUG_QUEUING_LOCKS
1466 if (head <= 0 || tail <= 0)
1467 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1469 KMP_DEBUG_ASSERT(head > 0 && tail > 0);
1473 head_thr->th.th_next_waiting = 0;
1474 #ifdef DEBUG_QUEUING_LOCKS
1475 TRACE_LOCK_T(gtid + 1,
"rel nw=0 for t=", head);
1480 head_thr->th.th_spin_here = FALSE;
1482 KA_TRACE(1000, (
"__kmp_release_queuing_lock: lck:%p, T#%d exiting: after "
1485 #ifdef DEBUG_QUEUING_LOCKS
1486 TRACE_LOCK(gtid + 1,
"rel exit 2");
1488 return KMP_LOCK_RELEASED;
1493 #ifdef DEBUG_QUEUING_LOCKS
1494 TRACE_LOCK(gtid + 1,
"rel retry");
1498 KMP_ASSERT2(0,
"should not get here");
1499 return KMP_LOCK_RELEASED;
1502 static int __kmp_release_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1504 char const *
const func =
"omp_unset_lock";
1506 if (lck->lk.initialized != lck) {
1507 KMP_FATAL(LockIsUninitialized, func);
1509 if (__kmp_is_queuing_lock_nestable(lck)) {
1510 KMP_FATAL(LockNestableUsedAsSimple, func);
1512 if (__kmp_get_queuing_lock_owner(lck) == -1) {
1513 KMP_FATAL(LockUnsettingFree, func);
1515 if (__kmp_get_queuing_lock_owner(lck) != gtid) {
1516 KMP_FATAL(LockUnsettingSetByAnother, func);
1518 lck->lk.owner_id = 0;
1519 return __kmp_release_queuing_lock(lck, gtid);
1522 void __kmp_init_queuing_lock(kmp_queuing_lock_t *lck) {
1523 lck->lk.location = NULL;
1524 lck->lk.head_id = 0;
1525 lck->lk.tail_id = 0;
1526 lck->lk.next_ticket = 0;
1527 lck->lk.now_serving = 0;
1528 lck->lk.owner_id = 0;
1529 lck->lk.depth_locked = -1;
1530 lck->lk.initialized = lck;
1532 KA_TRACE(1000, (
"__kmp_init_queuing_lock: lock %p initialized\n", lck));
1535 void __kmp_destroy_queuing_lock(kmp_queuing_lock_t *lck) {
1536 lck->lk.initialized = NULL;
1537 lck->lk.location = NULL;
1538 lck->lk.head_id = 0;
1539 lck->lk.tail_id = 0;
1540 lck->lk.next_ticket = 0;
1541 lck->lk.now_serving = 0;
1542 lck->lk.owner_id = 0;
1543 lck->lk.depth_locked = -1;
1546 static void __kmp_destroy_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
1547 char const *
const func =
"omp_destroy_lock";
1548 if (lck->lk.initialized != lck) {
1549 KMP_FATAL(LockIsUninitialized, func);
1551 if (__kmp_is_queuing_lock_nestable(lck)) {
1552 KMP_FATAL(LockNestableUsedAsSimple, func);
1554 if (__kmp_get_queuing_lock_owner(lck) != -1) {
1555 KMP_FATAL(LockStillOwned, func);
1557 __kmp_destroy_queuing_lock(lck);
1562 int __kmp_acquire_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1563 KMP_DEBUG_ASSERT(gtid >= 0);
1565 if (__kmp_get_queuing_lock_owner(lck) == gtid) {
1566 lck->lk.depth_locked += 1;
1567 return KMP_LOCK_ACQUIRED_NEXT;
1569 __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
1570 ANNOTATE_QUEUING_ACQUIRED(lck);
1572 lck->lk.depth_locked = 1;
1574 lck->lk.owner_id = gtid + 1;
1575 return KMP_LOCK_ACQUIRED_FIRST;
1580 __kmp_acquire_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1582 char const *
const func =
"omp_set_nest_lock";
1583 if (lck->lk.initialized != lck) {
1584 KMP_FATAL(LockIsUninitialized, func);
1586 if (!__kmp_is_queuing_lock_nestable(lck)) {
1587 KMP_FATAL(LockSimpleUsedAsNestable, func);
1589 return __kmp_acquire_nested_queuing_lock(lck, gtid);
1592 int __kmp_test_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1595 KMP_DEBUG_ASSERT(gtid >= 0);
1597 if (__kmp_get_queuing_lock_owner(lck) == gtid) {
1598 retval = ++lck->lk.depth_locked;
1599 }
else if (!__kmp_test_queuing_lock(lck, gtid)) {
1603 retval = lck->lk.depth_locked = 1;
1605 lck->lk.owner_id = gtid + 1;
1610 static int __kmp_test_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1612 char const *
const func =
"omp_test_nest_lock";
1613 if (lck->lk.initialized != lck) {
1614 KMP_FATAL(LockIsUninitialized, func);
1616 if (!__kmp_is_queuing_lock_nestable(lck)) {
1617 KMP_FATAL(LockSimpleUsedAsNestable, func);
1619 return __kmp_test_nested_queuing_lock(lck, gtid);
1622 int __kmp_release_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1623 KMP_DEBUG_ASSERT(gtid >= 0);
1626 if (--(lck->lk.depth_locked) == 0) {
1628 lck->lk.owner_id = 0;
1629 __kmp_release_queuing_lock(lck, gtid);
1630 return KMP_LOCK_RELEASED;
1632 return KMP_LOCK_STILL_HELD;
1636 __kmp_release_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1638 char const *
const func =
"omp_unset_nest_lock";
1640 if (lck->lk.initialized != lck) {
1641 KMP_FATAL(LockIsUninitialized, func);
1643 if (!__kmp_is_queuing_lock_nestable(lck)) {
1644 KMP_FATAL(LockSimpleUsedAsNestable, func);
1646 if (__kmp_get_queuing_lock_owner(lck) == -1) {
1647 KMP_FATAL(LockUnsettingFree, func);
1649 if (__kmp_get_queuing_lock_owner(lck) != gtid) {
1650 KMP_FATAL(LockUnsettingSetByAnother, func);
1652 return __kmp_release_nested_queuing_lock(lck, gtid);
1655 void __kmp_init_nested_queuing_lock(kmp_queuing_lock_t *lck) {
1656 __kmp_init_queuing_lock(lck);
1657 lck->lk.depth_locked = 0;
1660 void __kmp_destroy_nested_queuing_lock(kmp_queuing_lock_t *lck) {
1661 __kmp_destroy_queuing_lock(lck);
1662 lck->lk.depth_locked = 0;
1666 __kmp_destroy_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
1667 char const *
const func =
"omp_destroy_nest_lock";
1668 if (lck->lk.initialized != lck) {
1669 KMP_FATAL(LockIsUninitialized, func);
1671 if (!__kmp_is_queuing_lock_nestable(lck)) {
1672 KMP_FATAL(LockSimpleUsedAsNestable, func);
1674 if (__kmp_get_queuing_lock_owner(lck) != -1) {
1675 KMP_FATAL(LockStillOwned, func);
1677 __kmp_destroy_nested_queuing_lock(lck);
1682 static const ident_t *__kmp_get_queuing_lock_location(kmp_queuing_lock_t *lck) {
1683 return lck->lk.location;
1686 static void __kmp_set_queuing_lock_location(kmp_queuing_lock_t *lck,
1688 lck->lk.location = loc;
1691 static kmp_lock_flags_t __kmp_get_queuing_lock_flags(kmp_queuing_lock_t *lck) {
1692 return lck->lk.flags;
1695 static void __kmp_set_queuing_lock_flags(kmp_queuing_lock_t *lck,
1696 kmp_lock_flags_t flags) {
1697 lck->lk.flags = flags;
1700 #if KMP_USE_ADAPTIVE_LOCKS
1704 #if KMP_HAVE_RTM_INTRINSICS
1705 #include <immintrin.h>
1706 #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT)
1711 #define _XBEGIN_STARTED (~0u)
1712 #define _XABORT_EXPLICIT (1 << 0)
1713 #define _XABORT_RETRY (1 << 1)
1714 #define _XABORT_CONFLICT (1 << 2)
1715 #define _XABORT_CAPACITY (1 << 3)
1716 #define _XABORT_DEBUG (1 << 4)
1717 #define _XABORT_NESTED (1 << 5)
1718 #define _XABORT_CODE(x) ((unsigned char)(((x) >> 24) & 0xFF))
1721 #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT)
1723 #define STRINGIZE_INTERNAL(arg) #arg
1724 #define STRINGIZE(arg) STRINGIZE_INTERNAL(arg)
1730 static __inline
int _xbegin() {
1767 __asm__
volatile(
"1: .byte 0xC7; .byte 0xF8;\n"
1770 "1: movl %%eax,%0\n"
1772 :
"+r"(res)::
"memory",
"%eax");
1778 static __inline
void _xend() {
1786 __asm__
volatile(
".byte 0x0f; .byte 0x01; .byte 0xd5" :::
"memory");
1795 #define _xabort(ARG) _asm _emit 0xc6 _asm _emit 0xf8 _asm _emit ARG
1797 #define _xabort(ARG) \
1798 __asm__ volatile(".byte 0xC6; .byte 0xF8; .byte " STRINGIZE(ARG):::"memory");
1804 #if KMP_DEBUG_ADAPTIVE_LOCKS
1809 static kmp_adaptive_lock_statistics_t destroyedStats;
1812 static kmp_adaptive_lock_info_t liveLocks;
1815 static kmp_bootstrap_lock_t chain_lock =
1816 KMP_BOOTSTRAP_LOCK_INITIALIZER(chain_lock);
1819 void __kmp_init_speculative_stats() {
1820 kmp_adaptive_lock_info_t *lck = &liveLocks;
1822 memset(CCAST(kmp_adaptive_lock_statistics_t *, &(lck->stats)), 0,
1823 sizeof(lck->stats));
1824 lck->stats.next = lck;
1825 lck->stats.prev = lck;
1827 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1828 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1830 __kmp_init_bootstrap_lock(&chain_lock);
1834 static void __kmp_remember_lock(kmp_adaptive_lock_info_t *lck) {
1835 __kmp_acquire_bootstrap_lock(&chain_lock);
1837 lck->stats.next = liveLocks.stats.next;
1838 lck->stats.prev = &liveLocks;
1840 liveLocks.stats.next = lck;
1841 lck->stats.next->stats.prev = lck;
1843 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1844 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1846 __kmp_release_bootstrap_lock(&chain_lock);
1849 static void __kmp_forget_lock(kmp_adaptive_lock_info_t *lck) {
1850 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1851 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1853 kmp_adaptive_lock_info_t *n = lck->stats.next;
1854 kmp_adaptive_lock_info_t *p = lck->stats.prev;
1860 static void __kmp_zero_speculative_stats(kmp_adaptive_lock_info_t *lck) {
1861 memset(CCAST(kmp_adaptive_lock_statistics_t *, &lck->stats), 0,
1862 sizeof(lck->stats));
1863 __kmp_remember_lock(lck);
1866 static void __kmp_add_stats(kmp_adaptive_lock_statistics_t *t,
1867 kmp_adaptive_lock_info_t *lck) {
1868 kmp_adaptive_lock_statistics_t
volatile *s = &lck->stats;
1870 t->nonSpeculativeAcquireAttempts += lck->acquire_attempts;
1871 t->successfulSpeculations += s->successfulSpeculations;
1872 t->hardFailedSpeculations += s->hardFailedSpeculations;
1873 t->softFailedSpeculations += s->softFailedSpeculations;
1874 t->nonSpeculativeAcquires += s->nonSpeculativeAcquires;
1875 t->lemmingYields += s->lemmingYields;
1878 static void __kmp_accumulate_speculative_stats(kmp_adaptive_lock_info_t *lck) {
1879 __kmp_acquire_bootstrap_lock(&chain_lock);
1881 __kmp_add_stats(&destroyedStats, lck);
1882 __kmp_forget_lock(lck);
1884 __kmp_release_bootstrap_lock(&chain_lock);
1887 static float percent(kmp_uint32 count, kmp_uint32 total) {
1888 return (total == 0) ? 0.0 : (100.0 * count) / total;
1891 void __kmp_print_speculative_stats() {
1892 kmp_adaptive_lock_statistics_t total = destroyedStats;
1893 kmp_adaptive_lock_info_t *lck;
1895 for (lck = liveLocks.stats.next; lck != &liveLocks; lck = lck->stats.next) {
1896 __kmp_add_stats(&total, lck);
1898 kmp_adaptive_lock_statistics_t *t = &total;
1899 kmp_uint32 totalSections =
1900 t->nonSpeculativeAcquires + t->successfulSpeculations;
1901 kmp_uint32 totalSpeculations = t->successfulSpeculations +
1902 t->hardFailedSpeculations +
1903 t->softFailedSpeculations;
1904 if (totalSections <= 0)
1908 if (strcmp(__kmp_speculative_statsfile,
"-") == 0) {
1911 size_t buffLen = KMP_STRLEN(__kmp_speculative_statsfile) + 20;
1912 char buffer[buffLen];
1913 KMP_SNPRINTF(&buffer[0], buffLen, __kmp_speculative_statsfile,
1914 (kmp_int32)getpid());
1915 statsFile.
open(buffer,
"w");
1918 fprintf(statsFile,
"Speculative lock statistics (all approximate!)\n");
1920 " Lock parameters: \n"
1921 " max_soft_retries : %10d\n"
1922 " max_badness : %10d\n",
1923 __kmp_adaptive_backoff_params.max_soft_retries,
1924 __kmp_adaptive_backoff_params.max_badness);
1925 fprintf(statsFile,
" Non-speculative acquire attempts : %10d\n",
1926 t->nonSpeculativeAcquireAttempts);
1927 fprintf(statsFile,
" Total critical sections : %10d\n",
1929 fprintf(statsFile,
" Successful speculations : %10d (%5.1f%%)\n",
1930 t->successfulSpeculations,
1931 percent(t->successfulSpeculations, totalSections));
1932 fprintf(statsFile,
" Non-speculative acquires : %10d (%5.1f%%)\n",
1933 t->nonSpeculativeAcquires,
1934 percent(t->nonSpeculativeAcquires, totalSections));
1935 fprintf(statsFile,
" Lemming yields : %10d\n\n",
1938 fprintf(statsFile,
" Speculative acquire attempts : %10d\n",
1940 fprintf(statsFile,
" Successes : %10d (%5.1f%%)\n",
1941 t->successfulSpeculations,
1942 percent(t->successfulSpeculations, totalSpeculations));
1943 fprintf(statsFile,
" Soft failures : %10d (%5.1f%%)\n",
1944 t->softFailedSpeculations,
1945 percent(t->softFailedSpeculations, totalSpeculations));
1946 fprintf(statsFile,
" Hard failures : %10d (%5.1f%%)\n",
1947 t->hardFailedSpeculations,
1948 percent(t->hardFailedSpeculations, totalSpeculations));
1951 #define KMP_INC_STAT(lck, stat) (lck->lk.adaptive.stats.stat++)
1953 #define KMP_INC_STAT(lck, stat)
1957 static inline bool __kmp_is_unlocked_queuing_lock(kmp_queuing_lock_t *lck) {
1960 bool res = lck->lk.head_id == 0;
1964 #if KMP_COMPILER_ICC
1967 __sync_synchronize();
1974 static __inline
void
1975 __kmp_update_badness_after_success(kmp_adaptive_lock_t *lck) {
1977 lck->lk.adaptive.badness = 0;
1978 KMP_INC_STAT(lck, successfulSpeculations);
1982 static __inline
void __kmp_step_badness(kmp_adaptive_lock_t *lck) {
1983 kmp_uint32 newBadness = (lck->lk.adaptive.badness << 1) | 1;
1984 if (newBadness > lck->lk.adaptive.max_badness) {
1987 lck->lk.adaptive.badness = newBadness;
1992 KMP_ATTRIBUTE_TARGET_RTM
1993 static __inline
int __kmp_should_speculate(kmp_adaptive_lock_t *lck,
1995 kmp_uint32 badness = lck->lk.adaptive.badness;
1996 kmp_uint32 attempts = lck->lk.adaptive.acquire_attempts;
1997 int res = (attempts & badness) == 0;
2003 KMP_ATTRIBUTE_TARGET_RTM
2004 static int __kmp_test_adaptive_lock_only(kmp_adaptive_lock_t *lck,
2006 int retries = lck->lk.adaptive.max_soft_retries;
2013 kmp_uint32 status = _xbegin();
2018 if (status == _XBEGIN_STARTED) {
2023 if (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2027 KMP_ASSERT2(0,
"should not get here");
2032 if (status & SOFT_ABORT_MASK) {
2033 KMP_INC_STAT(lck, softFailedSpeculations);
2036 KMP_INC_STAT(lck, hardFailedSpeculations);
2041 }
while (retries--);
2045 __kmp_step_badness(lck);
2052 static int __kmp_test_adaptive_lock(kmp_adaptive_lock_t *lck, kmp_int32 gtid) {
2054 if (__kmp_should_speculate(lck, gtid) &&
2055 __kmp_test_adaptive_lock_only(lck, gtid))
2060 lck->lk.adaptive.acquire_attempts++;
2063 if (__kmp_test_queuing_lock(GET_QLK_PTR(lck), gtid)) {
2064 KMP_INC_STAT(lck, nonSpeculativeAcquires);
2071 static int __kmp_test_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2073 char const *
const func =
"omp_test_lock";
2074 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2075 KMP_FATAL(LockIsUninitialized, func);
2078 int retval = __kmp_test_adaptive_lock(lck, gtid);
2081 lck->lk.qlk.owner_id = gtid + 1;
2097 static void __kmp_acquire_adaptive_lock(kmp_adaptive_lock_t *lck,
2099 if (__kmp_should_speculate(lck, gtid)) {
2100 if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2101 if (__kmp_test_adaptive_lock_only(lck, gtid))
2110 while (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2111 KMP_INC_STAT(lck, lemmingYields);
2115 if (__kmp_test_adaptive_lock_only(lck, gtid))
2122 lck->lk.adaptive.acquire_attempts++;
2124 __kmp_acquire_queuing_lock_timed_template<FALSE>(GET_QLK_PTR(lck), gtid);
2126 KMP_INC_STAT(lck, nonSpeculativeAcquires);
2127 ANNOTATE_QUEUING_ACQUIRED(lck);
2130 static void __kmp_acquire_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2132 char const *
const func =
"omp_set_lock";
2133 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2134 KMP_FATAL(LockIsUninitialized, func);
2136 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == gtid) {
2137 KMP_FATAL(LockIsAlreadyOwned, func);
2140 __kmp_acquire_adaptive_lock(lck, gtid);
2142 lck->lk.qlk.owner_id = gtid + 1;
2145 KMP_ATTRIBUTE_TARGET_RTM
2146 static int __kmp_release_adaptive_lock(kmp_adaptive_lock_t *lck,
2148 if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(
2153 __kmp_update_badness_after_success(lck);
2156 __kmp_release_queuing_lock(GET_QLK_PTR(lck), gtid);
2158 return KMP_LOCK_RELEASED;
2161 static int __kmp_release_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2163 char const *
const func =
"omp_unset_lock";
2165 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2166 KMP_FATAL(LockIsUninitialized, func);
2168 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == -1) {
2169 KMP_FATAL(LockUnsettingFree, func);
2171 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != gtid) {
2172 KMP_FATAL(LockUnsettingSetByAnother, func);
2174 lck->lk.qlk.owner_id = 0;
2175 __kmp_release_adaptive_lock(lck, gtid);
2176 return KMP_LOCK_RELEASED;
2179 static void __kmp_init_adaptive_lock(kmp_adaptive_lock_t *lck) {
2180 __kmp_init_queuing_lock(GET_QLK_PTR(lck));
2181 lck->lk.adaptive.badness = 0;
2182 lck->lk.adaptive.acquire_attempts = 0;
2183 lck->lk.adaptive.max_soft_retries =
2184 __kmp_adaptive_backoff_params.max_soft_retries;
2185 lck->lk.adaptive.max_badness = __kmp_adaptive_backoff_params.max_badness;
2186 #if KMP_DEBUG_ADAPTIVE_LOCKS
2187 __kmp_zero_speculative_stats(&lck->lk.adaptive);
2189 KA_TRACE(1000, (
"__kmp_init_adaptive_lock: lock %p initialized\n", lck));
2192 static void __kmp_destroy_adaptive_lock(kmp_adaptive_lock_t *lck) {
2193 #if KMP_DEBUG_ADAPTIVE_LOCKS
2194 __kmp_accumulate_speculative_stats(&lck->lk.adaptive);
2196 __kmp_destroy_queuing_lock(GET_QLK_PTR(lck));
2200 static void __kmp_destroy_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
2201 char const *
const func =
"omp_destroy_lock";
2202 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2203 KMP_FATAL(LockIsUninitialized, func);
2205 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != -1) {
2206 KMP_FATAL(LockStillOwned, func);
2208 __kmp_destroy_adaptive_lock(lck);
2217 static kmp_int32 __kmp_get_drdpa_lock_owner(kmp_drdpa_lock_t *lck) {
2218 return lck->lk.owner_id - 1;
2221 static inline bool __kmp_is_drdpa_lock_nestable(kmp_drdpa_lock_t *lck) {
2222 return lck->lk.depth_locked != -1;
2225 __forceinline
static int
2226 __kmp_acquire_drdpa_lock_timed_template(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2227 kmp_uint64 ticket = KMP_ATOMIC_INC(&lck->lk.next_ticket);
2228 kmp_uint64 mask = lck->lk.mask;
2229 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2231 #ifdef USE_LOCK_PROFILE
2232 if (polls[ticket & mask] != ticket)
2233 __kmp_printf(
"LOCK CONTENTION: %p\n", lck);
2245 KMP_FSYNC_PREPARE(lck);
2246 KMP_INIT_YIELD(spins);
2247 while (polls[ticket & mask] < ticket) {
2248 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
2256 mask = lck->lk.mask;
2257 polls = lck->lk.polls;
2261 KMP_FSYNC_ACQUIRED(lck);
2262 KA_TRACE(1000, (
"__kmp_acquire_drdpa_lock: ticket #%lld acquired lock %p\n",
2264 lck->lk.now_serving = ticket;
2271 if ((lck->lk.old_polls != NULL) && (ticket >= lck->lk.cleanup_ticket)) {
2272 __kmp_free(lck->lk.old_polls);
2273 lck->lk.old_polls = NULL;
2274 lck->lk.cleanup_ticket = 0;
2280 if (lck->lk.old_polls == NULL) {
2281 bool reconfigure =
false;
2282 std::atomic<kmp_uint64> *old_polls = polls;
2283 kmp_uint32 num_polls = TCR_4(lck->lk.num_polls);
2285 if (TCR_4(__kmp_nth) >
2286 (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) {
2289 if (num_polls > 1) {
2291 num_polls = TCR_4(lck->lk.num_polls);
2294 polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
2302 kmp_uint64 num_waiting = TCR_8(lck->lk.next_ticket) - ticket - 1;
2303 if (num_waiting > num_polls) {
2304 kmp_uint32 old_num_polls = num_polls;
2307 mask = (mask << 1) | 1;
2309 }
while (num_polls <= num_waiting);
2315 polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
2318 for (i = 0; i < old_num_polls; i++) {
2319 polls[i].store(old_polls[i]);
2334 KA_TRACE(1000, (
"__kmp_acquire_drdpa_lock: ticket #%lld reconfiguring "
2335 "lock %p to %d polls\n",
2336 ticket, lck, num_polls));
2338 lck->lk.old_polls = old_polls;
2339 lck->lk.polls = polls;
2343 lck->lk.num_polls = num_polls;
2344 lck->lk.mask = mask;
2352 lck->lk.cleanup_ticket = lck->lk.next_ticket;
2355 return KMP_LOCK_ACQUIRED_FIRST;
2358 int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2359 int retval = __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
2360 ANNOTATE_DRDPA_ACQUIRED(lck);
2364 static int __kmp_acquire_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2366 char const *
const func =
"omp_set_lock";
2367 if (lck->lk.initialized != lck) {
2368 KMP_FATAL(LockIsUninitialized, func);
2370 if (__kmp_is_drdpa_lock_nestable(lck)) {
2371 KMP_FATAL(LockNestableUsedAsSimple, func);
2373 if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) == gtid)) {
2374 KMP_FATAL(LockIsAlreadyOwned, func);
2377 __kmp_acquire_drdpa_lock(lck, gtid);
2379 lck->lk.owner_id = gtid + 1;
2380 return KMP_LOCK_ACQUIRED_FIRST;
2383 int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2386 kmp_uint64 ticket = lck->lk.next_ticket;
2387 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2388 kmp_uint64 mask = lck->lk.mask;
2389 if (polls[ticket & mask] == ticket) {
2390 kmp_uint64 next_ticket = ticket + 1;
2391 if (__kmp_atomic_compare_store_acq(&lck->lk.next_ticket, ticket,
2393 KMP_FSYNC_ACQUIRED(lck);
2394 KA_TRACE(1000, (
"__kmp_test_drdpa_lock: ticket #%lld acquired lock %p\n",
2396 lck->lk.now_serving = ticket;
2410 static int __kmp_test_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2412 char const *
const func =
"omp_test_lock";
2413 if (lck->lk.initialized != lck) {
2414 KMP_FATAL(LockIsUninitialized, func);
2416 if (__kmp_is_drdpa_lock_nestable(lck)) {
2417 KMP_FATAL(LockNestableUsedAsSimple, func);
2420 int retval = __kmp_test_drdpa_lock(lck, gtid);
2423 lck->lk.owner_id = gtid + 1;
2428 int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2431 kmp_uint64 ticket = lck->lk.now_serving + 1;
2432 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2433 kmp_uint64 mask = lck->lk.mask;
2434 KA_TRACE(1000, (
"__kmp_release_drdpa_lock: ticket #%lld released lock %p\n",
2436 KMP_FSYNC_RELEASING(lck);
2437 ANNOTATE_DRDPA_RELEASED(lck);
2438 polls[ticket & mask] = ticket;
2439 return KMP_LOCK_RELEASED;
2442 static int __kmp_release_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2444 char const *
const func =
"omp_unset_lock";
2446 if (lck->lk.initialized != lck) {
2447 KMP_FATAL(LockIsUninitialized, func);
2449 if (__kmp_is_drdpa_lock_nestable(lck)) {
2450 KMP_FATAL(LockNestableUsedAsSimple, func);
2452 if (__kmp_get_drdpa_lock_owner(lck) == -1) {
2453 KMP_FATAL(LockUnsettingFree, func);
2455 if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) >= 0) &&
2456 (__kmp_get_drdpa_lock_owner(lck) != gtid)) {
2457 KMP_FATAL(LockUnsettingSetByAnother, func);
2459 lck->lk.owner_id = 0;
2460 return __kmp_release_drdpa_lock(lck, gtid);
2463 void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck) {
2464 lck->lk.location = NULL;
2466 lck->lk.num_polls = 1;
2467 lck->lk.polls = (std::atomic<kmp_uint64> *)__kmp_allocate(
2468 lck->lk.num_polls *
sizeof(*(lck->lk.polls)));
2469 lck->lk.cleanup_ticket = 0;
2470 lck->lk.old_polls = NULL;
2471 lck->lk.next_ticket = 0;
2472 lck->lk.now_serving = 0;
2473 lck->lk.owner_id = 0;
2474 lck->lk.depth_locked = -1;
2475 lck->lk.initialized = lck;
2477 KA_TRACE(1000, (
"__kmp_init_drdpa_lock: lock %p initialized\n", lck));
2480 void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck) {
2481 lck->lk.initialized = NULL;
2482 lck->lk.location = NULL;
2483 if (lck->lk.polls.load() != NULL) {
2484 __kmp_free(lck->lk.polls.load());
2485 lck->lk.polls = NULL;
2487 if (lck->lk.old_polls != NULL) {
2488 __kmp_free(lck->lk.old_polls);
2489 lck->lk.old_polls = NULL;
2492 lck->lk.num_polls = 0;
2493 lck->lk.cleanup_ticket = 0;
2494 lck->lk.next_ticket = 0;
2495 lck->lk.now_serving = 0;
2496 lck->lk.owner_id = 0;
2497 lck->lk.depth_locked = -1;
2500 static void __kmp_destroy_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
2501 char const *
const func =
"omp_destroy_lock";
2502 if (lck->lk.initialized != lck) {
2503 KMP_FATAL(LockIsUninitialized, func);
2505 if (__kmp_is_drdpa_lock_nestable(lck)) {
2506 KMP_FATAL(LockNestableUsedAsSimple, func);
2508 if (__kmp_get_drdpa_lock_owner(lck) != -1) {
2509 KMP_FATAL(LockStillOwned, func);
2511 __kmp_destroy_drdpa_lock(lck);
2516 int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2517 KMP_DEBUG_ASSERT(gtid >= 0);
2519 if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
2520 lck->lk.depth_locked += 1;
2521 return KMP_LOCK_ACQUIRED_NEXT;
2523 __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
2524 ANNOTATE_DRDPA_ACQUIRED(lck);
2526 lck->lk.depth_locked = 1;
2528 lck->lk.owner_id = gtid + 1;
2529 return KMP_LOCK_ACQUIRED_FIRST;
2533 static void __kmp_acquire_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2535 char const *
const func =
"omp_set_nest_lock";
2536 if (lck->lk.initialized != lck) {
2537 KMP_FATAL(LockIsUninitialized, func);
2539 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2540 KMP_FATAL(LockSimpleUsedAsNestable, func);
2542 __kmp_acquire_nested_drdpa_lock(lck, gtid);
2545 int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2548 KMP_DEBUG_ASSERT(gtid >= 0);
2550 if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
2551 retval = ++lck->lk.depth_locked;
2552 }
else if (!__kmp_test_drdpa_lock(lck, gtid)) {
2556 retval = lck->lk.depth_locked = 1;
2558 lck->lk.owner_id = gtid + 1;
2563 static int __kmp_test_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2565 char const *
const func =
"omp_test_nest_lock";
2566 if (lck->lk.initialized != lck) {
2567 KMP_FATAL(LockIsUninitialized, func);
2569 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2570 KMP_FATAL(LockSimpleUsedAsNestable, func);
2572 return __kmp_test_nested_drdpa_lock(lck, gtid);
2575 int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2576 KMP_DEBUG_ASSERT(gtid >= 0);
2579 if (--(lck->lk.depth_locked) == 0) {
2581 lck->lk.owner_id = 0;
2582 __kmp_release_drdpa_lock(lck, gtid);
2583 return KMP_LOCK_RELEASED;
2585 return KMP_LOCK_STILL_HELD;
2588 static int __kmp_release_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2590 char const *
const func =
"omp_unset_nest_lock";
2592 if (lck->lk.initialized != lck) {
2593 KMP_FATAL(LockIsUninitialized, func);
2595 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2596 KMP_FATAL(LockSimpleUsedAsNestable, func);
2598 if (__kmp_get_drdpa_lock_owner(lck) == -1) {
2599 KMP_FATAL(LockUnsettingFree, func);
2601 if (__kmp_get_drdpa_lock_owner(lck) != gtid) {
2602 KMP_FATAL(LockUnsettingSetByAnother, func);
2604 return __kmp_release_nested_drdpa_lock(lck, gtid);
2607 void __kmp_init_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
2608 __kmp_init_drdpa_lock(lck);
2609 lck->lk.depth_locked = 0;
2612 void __kmp_destroy_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
2613 __kmp_destroy_drdpa_lock(lck);
2614 lck->lk.depth_locked = 0;
2617 static void __kmp_destroy_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
2618 char const *
const func =
"omp_destroy_nest_lock";
2619 if (lck->lk.initialized != lck) {
2620 KMP_FATAL(LockIsUninitialized, func);
2622 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2623 KMP_FATAL(LockSimpleUsedAsNestable, func);
2625 if (__kmp_get_drdpa_lock_owner(lck) != -1) {
2626 KMP_FATAL(LockStillOwned, func);
2628 __kmp_destroy_nested_drdpa_lock(lck);
2633 static const ident_t *__kmp_get_drdpa_lock_location(kmp_drdpa_lock_t *lck) {
2634 return lck->lk.location;
2637 static void __kmp_set_drdpa_lock_location(kmp_drdpa_lock_t *lck,
2639 lck->lk.location = loc;
2642 static kmp_lock_flags_t __kmp_get_drdpa_lock_flags(kmp_drdpa_lock_t *lck) {
2643 return lck->lk.flags;
2646 static void __kmp_set_drdpa_lock_flags(kmp_drdpa_lock_t *lck,
2647 kmp_lock_flags_t flags) {
2648 lck->lk.flags = flags;
2652 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
2653 #define __kmp_tsc() __kmp_hardware_timestamp()
2655 kmp_backoff_t __kmp_spin_backoff_params = {1, 4096, 100};
2658 extern kmp_uint64 __kmp_now_nsec();
2659 kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
2660 #define __kmp_tsc() __kmp_now_nsec()
2670 static inline bool before(kmp_uint64 a, kmp_uint64 b) {
2671 return ((kmp_int64)b - (kmp_int64)a) > 0;
2675 void __kmp_spin_backoff(kmp_backoff_t *boff) {
2678 for (i = boff->step; i > 0; i--) {
2679 kmp_uint64 goal = __kmp_tsc() + boff->min_tick;
2682 }
while (before(__kmp_tsc(), goal));
2684 boff->step = (boff->step << 1 | 1) & (boff->max_backoff - 1);
2687 #if KMP_USE_DYNAMIC_LOCK
2691 static void __kmp_init_direct_lock(kmp_dyna_lock_t *lck,
2692 kmp_dyna_lockseq_t seq) {
2693 TCW_4(*lck, KMP_GET_D_TAG(seq));
2696 (
"__kmp_init_direct_lock: initialized direct lock with type#%d\n", seq));
2702 #define HLE_ACQUIRE ".byte 0xf2;"
2703 #define HLE_RELEASE ".byte 0xf3;"
2705 static inline kmp_uint32 swap4(kmp_uint32
volatile *p, kmp_uint32 v) {
2706 __asm__
volatile(HLE_ACQUIRE
"xchg %1,%0" :
"+r"(v),
"+m"(*p) : :
"memory");
2710 static void __kmp_destroy_hle_lock(kmp_dyna_lock_t *lck) { TCW_4(*lck, 0); }
2712 static void __kmp_destroy_hle_lock_with_checks(kmp_dyna_lock_t *lck) {
2716 static void __kmp_acquire_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
2718 if (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle)) {
2721 while (*(kmp_uint32
volatile *)lck != KMP_LOCK_FREE(hle)) {
2722 for (
int i = delay; i != 0; --i)
2724 delay = ((delay << 1) | 1) & 7;
2726 }
while (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle));
2730 static void __kmp_acquire_hle_lock_with_checks(kmp_dyna_lock_t *lck,
2732 __kmp_acquire_hle_lock(lck, gtid);
2735 static int __kmp_release_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
2736 __asm__
volatile(HLE_RELEASE
"movl %1,%0"
2738 :
"r"(KMP_LOCK_FREE(hle))
2740 return KMP_LOCK_RELEASED;
2743 static int __kmp_release_hle_lock_with_checks(kmp_dyna_lock_t *lck,
2745 return __kmp_release_hle_lock(lck, gtid);
2748 static int __kmp_test_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
2749 return swap4(lck, KMP_LOCK_BUSY(1, hle)) == KMP_LOCK_FREE(hle);
2752 static int __kmp_test_hle_lock_with_checks(kmp_dyna_lock_t *lck,
2754 return __kmp_test_hle_lock(lck, gtid);
2757 static void __kmp_init_rtm_queuing_lock(kmp_queuing_lock_t *lck) {
2758 __kmp_init_queuing_lock(lck);
2761 static void __kmp_destroy_rtm_queuing_lock(kmp_queuing_lock_t *lck) {
2762 __kmp_destroy_queuing_lock(lck);
2766 __kmp_destroy_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
2767 __kmp_destroy_queuing_lock_with_checks(lck);
2770 KMP_ATTRIBUTE_TARGET_RTM
2771 static void __kmp_acquire_rtm_queuing_lock(kmp_queuing_lock_t *lck,
2773 unsigned retries = 3, status;
2776 if (status == _XBEGIN_STARTED) {
2777 if (__kmp_is_unlocked_queuing_lock(lck))
2781 if ((status & _XABORT_EXPLICIT) && _XABORT_CODE(status) == 0xff) {
2783 while (!__kmp_is_unlocked_queuing_lock(lck)) {
2786 }
else if (!(status & _XABORT_RETRY))
2788 }
while (retries--);
2791 __kmp_acquire_queuing_lock(lck, gtid);
2794 static void __kmp_acquire_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
2796 __kmp_acquire_rtm_queuing_lock(lck, gtid);
2799 KMP_ATTRIBUTE_TARGET_RTM
2800 static int __kmp_release_rtm_queuing_lock(kmp_queuing_lock_t *lck,
2802 if (__kmp_is_unlocked_queuing_lock(lck)) {
2807 __kmp_release_queuing_lock(lck, gtid);
2809 return KMP_LOCK_RELEASED;
2812 static int __kmp_release_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
2814 return __kmp_release_rtm_queuing_lock(lck, gtid);
2817 KMP_ATTRIBUTE_TARGET_RTM
2818 static int __kmp_test_rtm_queuing_lock(kmp_queuing_lock_t *lck,
2820 unsigned retries = 3, status;
2823 if (status == _XBEGIN_STARTED && __kmp_is_unlocked_queuing_lock(lck)) {
2826 if (!(status & _XABORT_RETRY))
2828 }
while (retries--);
2830 return __kmp_test_queuing_lock(lck, gtid);
2833 static int __kmp_test_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
2835 return __kmp_test_rtm_queuing_lock(lck, gtid);
2839 typedef kmp_tas_lock_t kmp_rtm_spin_lock_t;
2841 static void __kmp_destroy_rtm_spin_lock(kmp_rtm_spin_lock_t *lck) {
2842 KMP_ATOMIC_ST_REL(&lck->lk.poll, 0);
2845 static void __kmp_destroy_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck) {
2846 __kmp_destroy_rtm_spin_lock(lck);
2849 KMP_ATTRIBUTE_TARGET_RTM
2850 static int __kmp_acquire_rtm_spin_lock(kmp_rtm_spin_lock_t *lck,
2852 unsigned retries = 3, status;
2853 kmp_int32 lock_free = KMP_LOCK_FREE(rtm_spin);
2854 kmp_int32 lock_busy = KMP_LOCK_BUSY(1, rtm_spin);
2857 if (status == _XBEGIN_STARTED) {
2858 if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == lock_free)
2859 return KMP_LOCK_ACQUIRED_FIRST;
2862 if ((status & _XABORT_EXPLICIT) && _XABORT_CODE(status) == 0xff) {
2864 while (KMP_ATOMIC_LD_RLX(&lck->lk.poll) != lock_free) {
2867 }
else if (!(status & _XABORT_RETRY))
2869 }
while (retries--);
2872 KMP_FSYNC_PREPARE(lck);
2873 kmp_backoff_t backoff = __kmp_spin_backoff_params;
2874 while (KMP_ATOMIC_LD_RLX(&lck->lk.poll) != lock_free ||
2875 !__kmp_atomic_compare_store_acq(&lck->lk.poll, lock_free, lock_busy)) {
2876 __kmp_spin_backoff(&backoff);
2878 KMP_FSYNC_ACQUIRED(lck);
2879 return KMP_LOCK_ACQUIRED_FIRST;
2882 static int __kmp_acquire_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck,
2884 return __kmp_acquire_rtm_spin_lock(lck, gtid);
2887 KMP_ATTRIBUTE_TARGET_RTM
2888 static int __kmp_release_rtm_spin_lock(kmp_rtm_spin_lock_t *lck,
2890 if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == KMP_LOCK_FREE(rtm_spin)) {
2895 KMP_FSYNC_RELEASING(lck);
2896 KMP_ATOMIC_ST_REL(&lck->lk.poll, KMP_LOCK_FREE(rtm_spin));
2898 return KMP_LOCK_RELEASED;
2901 static int __kmp_release_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck,
2903 return __kmp_release_rtm_spin_lock(lck, gtid);
2906 KMP_ATTRIBUTE_TARGET_RTM
2907 static int __kmp_test_rtm_spin_lock(kmp_rtm_spin_lock_t *lck, kmp_int32 gtid) {
2908 unsigned retries = 3, status;
2909 kmp_int32 lock_free = KMP_LOCK_FREE(rtm_spin);
2910 kmp_int32 lock_busy = KMP_LOCK_BUSY(1, rtm_spin);
2913 if (status == _XBEGIN_STARTED &&
2914 KMP_ATOMIC_LD_RLX(&lck->lk.poll) == lock_free) {
2917 if (!(status & _XABORT_RETRY))
2919 }
while (retries--);
2921 if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == lock_free &&
2922 __kmp_atomic_compare_store_acq(&lck->lk.poll, lock_free, lock_busy)) {
2923 KMP_FSYNC_ACQUIRED(lck);
2929 static int __kmp_test_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck,
2931 return __kmp_test_rtm_spin_lock(lck, gtid);
2937 static void __kmp_init_indirect_lock(kmp_dyna_lock_t *l,
2938 kmp_dyna_lockseq_t tag);
2939 static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock);
2940 static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
2941 static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
2942 static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
2943 static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
2945 static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
2947 static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
2951 #define KMP_FOREACH_LOCK_KIND(m, a) m(ticket, a) m(queuing, a) m(drdpa, a)
2953 #define expand1(lk, op) \
2954 static void __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock) { \
2955 __kmp_##op##_##lk##_##lock(&lock->lk); \
2957 #define expand2(lk, op) \
2958 static int __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock, \
2960 return __kmp_##op##_##lk##_##lock(&lock->lk, gtid); \
2962 #define expand3(lk, op) \
2963 static void __kmp_set_##lk##_##lock_flags(kmp_user_lock_p lock, \
2964 kmp_lock_flags_t flags) { \
2965 __kmp_set_##lk##_lock_flags(&lock->lk, flags); \
2967 #define expand4(lk, op) \
2968 static void __kmp_set_##lk##_##lock_location(kmp_user_lock_p lock, \
2969 const ident_t *loc) { \
2970 __kmp_set_##lk##_lock_location(&lock->lk, loc); \
2973 KMP_FOREACH_LOCK_KIND(expand1, init)
2974 KMP_FOREACH_LOCK_KIND(expand1, init_nested)
2975 KMP_FOREACH_LOCK_KIND(expand1, destroy)
2976 KMP_FOREACH_LOCK_KIND(expand1, destroy_nested)
2977 KMP_FOREACH_LOCK_KIND(expand2, acquire)
2978 KMP_FOREACH_LOCK_KIND(expand2, acquire_nested)
2979 KMP_FOREACH_LOCK_KIND(expand2, release)
2980 KMP_FOREACH_LOCK_KIND(expand2, release_nested)
2981 KMP_FOREACH_LOCK_KIND(expand2, test)
2982 KMP_FOREACH_LOCK_KIND(expand2, test_nested)
2983 KMP_FOREACH_LOCK_KIND(expand3, )
2984 KMP_FOREACH_LOCK_KIND(expand4, )
2995 #define expand(l, op) 0, __kmp_init_direct_lock,
2996 void (*__kmp_direct_init[])(kmp_dyna_lock_t *, kmp_dyna_lockseq_t) = {
2997 __kmp_init_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, init)};
3001 #define expand(l, op) 0, (void (*)(kmp_dyna_lock_t *))__kmp_##op##_##l##_lock,
3002 static void (*direct_destroy[])(kmp_dyna_lock_t *) = {
3003 __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
3005 #define expand(l, op) \
3006 0, (void (*)(kmp_dyna_lock_t *))__kmp_destroy_##l##_lock_with_checks,
3007 static void (*direct_destroy_check[])(kmp_dyna_lock_t *) = {
3008 __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
3012 #define expand(l, op) \
3013 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock,
3014 static int (*direct_set[])(kmp_dyna_lock_t *, kmp_int32) = {
3015 __kmp_set_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, acquire)};
3017 #define expand(l, op) \
3018 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks,
3019 static int (*direct_set_check[])(kmp_dyna_lock_t *, kmp_int32) = {
3020 __kmp_set_indirect_lock_with_checks, 0,
3021 KMP_FOREACH_D_LOCK(expand, acquire)};
3025 #define expand(l, op) \
3026 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock,
3027 static int (*direct_unset[])(kmp_dyna_lock_t *, kmp_int32) = {
3028 __kmp_unset_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, release)};
3029 static int (*direct_test[])(kmp_dyna_lock_t *, kmp_int32) = {
3030 __kmp_test_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, test)};
3032 #define expand(l, op) \
3033 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks,
3034 static int (*direct_unset_check[])(kmp_dyna_lock_t *, kmp_int32) = {
3035 __kmp_unset_indirect_lock_with_checks, 0,
3036 KMP_FOREACH_D_LOCK(expand, release)};
3037 static int (*direct_test_check[])(kmp_dyna_lock_t *, kmp_int32) = {
3038 __kmp_test_indirect_lock_with_checks, 0, KMP_FOREACH_D_LOCK(expand, test)};
3042 void (**__kmp_direct_destroy)(kmp_dyna_lock_t *) = 0;
3043 int (**__kmp_direct_set)(kmp_dyna_lock_t *, kmp_int32) = 0;
3044 int (**__kmp_direct_unset)(kmp_dyna_lock_t *, kmp_int32) = 0;
3045 int (**__kmp_direct_test)(kmp_dyna_lock_t *, kmp_int32) = 0;
3048 #define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock,
3049 void (*__kmp_indirect_init[])(kmp_user_lock_p) = {
3050 KMP_FOREACH_I_LOCK(expand, init)};
3053 #define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock,
3054 static void (*indirect_destroy[])(kmp_user_lock_p) = {
3055 KMP_FOREACH_I_LOCK(expand, destroy)};
3057 #define expand(l, op) \
3058 (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock_with_checks,
3059 static void (*indirect_destroy_check[])(kmp_user_lock_p) = {
3060 KMP_FOREACH_I_LOCK(expand, destroy)};
3064 #define expand(l, op) \
3065 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock,
3066 static int (*indirect_set[])(kmp_user_lock_p,
3067 kmp_int32) = {KMP_FOREACH_I_LOCK(expand, acquire)};
3069 #define expand(l, op) \
3070 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks,
3071 static int (*indirect_set_check[])(kmp_user_lock_p, kmp_int32) = {
3072 KMP_FOREACH_I_LOCK(expand, acquire)};
3076 #define expand(l, op) \
3077 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock,
3078 static int (*indirect_unset[])(kmp_user_lock_p, kmp_int32) = {
3079 KMP_FOREACH_I_LOCK(expand, release)};
3080 static int (*indirect_test[])(kmp_user_lock_p,
3081 kmp_int32) = {KMP_FOREACH_I_LOCK(expand, test)};
3083 #define expand(l, op) \
3084 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks,
3085 static int (*indirect_unset_check[])(kmp_user_lock_p, kmp_int32) = {
3086 KMP_FOREACH_I_LOCK(expand, release)};
3087 static int (*indirect_test_check[])(kmp_user_lock_p, kmp_int32) = {
3088 KMP_FOREACH_I_LOCK(expand, test)};
3092 void (**__kmp_indirect_destroy)(kmp_user_lock_p) = 0;
3093 int (**__kmp_indirect_set)(kmp_user_lock_p, kmp_int32) = 0;
3094 int (**__kmp_indirect_unset)(kmp_user_lock_p, kmp_int32) = 0;
3095 int (**__kmp_indirect_test)(kmp_user_lock_p, kmp_int32) = 0;
3098 kmp_indirect_lock_table_t __kmp_i_lock_table;
3101 static kmp_uint32 __kmp_indirect_lock_size[KMP_NUM_I_LOCKS] = {0};
3104 void (*__kmp_indirect_set_location[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
3106 void (*__kmp_indirect_set_flags[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
3107 kmp_lock_flags_t) = {0};
3108 const ident_t *(*__kmp_indirect_get_location[KMP_NUM_I_LOCKS])(
3109 kmp_user_lock_p) = {0};
3110 kmp_lock_flags_t (*__kmp_indirect_get_flags[KMP_NUM_I_LOCKS])(
3111 kmp_user_lock_p) = {0};
3114 static kmp_indirect_lock_t *__kmp_indirect_lock_pool[KMP_NUM_I_LOCKS] = {0};
3121 kmp_indirect_lock_t *__kmp_allocate_indirect_lock(
void **user_lock,
3123 kmp_indirect_locktag_t tag) {
3124 kmp_indirect_lock_t *lck;
3125 kmp_lock_index_t idx;
3127 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3129 if (__kmp_indirect_lock_pool[tag] != NULL) {
3131 lck = __kmp_indirect_lock_pool[tag];
3132 if (OMP_LOCK_T_SIZE <
sizeof(
void *))
3133 idx = lck->lock->pool.index;
3134 __kmp_indirect_lock_pool[tag] = (kmp_indirect_lock_t *)lck->lock->pool.next;
3135 KA_TRACE(20, (
"__kmp_allocate_indirect_lock: reusing an existing lock %p\n",
3138 idx = __kmp_i_lock_table.next;
3140 if (idx == __kmp_i_lock_table.size) {
3142 int row = __kmp_i_lock_table.size / KMP_I_LOCK_CHUNK;
3143 kmp_indirect_lock_t **new_table = (kmp_indirect_lock_t **)__kmp_allocate(
3144 2 * row *
sizeof(kmp_indirect_lock_t *));
3145 KMP_MEMCPY(new_table, __kmp_i_lock_table.table,
3146 row *
sizeof(kmp_indirect_lock_t *));
3147 kmp_indirect_lock_t **old_table = __kmp_i_lock_table.table;
3148 __kmp_i_lock_table.table = new_table;
3149 __kmp_free(old_table);
3151 for (
int i = row; i < 2 * row; ++i)
3152 *(__kmp_i_lock_table.table + i) = (kmp_indirect_lock_t *)__kmp_allocate(
3153 KMP_I_LOCK_CHUNK *
sizeof(kmp_indirect_lock_t));
3154 __kmp_i_lock_table.size = 2 * idx;
3156 __kmp_i_lock_table.next++;
3157 lck = KMP_GET_I_LOCK(idx);
3159 lck->lock = (kmp_user_lock_p)__kmp_allocate(__kmp_indirect_lock_size[tag]);
3161 (
"__kmp_allocate_indirect_lock: allocated a new lock %p\n", lck));
3164 __kmp_release_lock(&__kmp_global_lock, gtid);
3168 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3169 *((kmp_lock_index_t *)user_lock) = idx
3172 *((kmp_indirect_lock_t **)user_lock) = lck;
3179 static __forceinline kmp_indirect_lock_t *
3180 __kmp_lookup_indirect_lock(
void **user_lock,
const char *func) {
3181 if (__kmp_env_consistency_check) {
3182 kmp_indirect_lock_t *lck = NULL;
3183 if (user_lock == NULL) {
3184 KMP_FATAL(LockIsUninitialized, func);
3186 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3187 kmp_lock_index_t idx = KMP_EXTRACT_I_INDEX(user_lock);
3188 if (idx >= __kmp_i_lock_table.size) {
3189 KMP_FATAL(LockIsUninitialized, func);
3191 lck = KMP_GET_I_LOCK(idx);
3193 lck = *((kmp_indirect_lock_t **)user_lock);
3196 KMP_FATAL(LockIsUninitialized, func);
3200 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3201 return KMP_GET_I_LOCK(KMP_EXTRACT_I_INDEX(user_lock));
3203 return *((kmp_indirect_lock_t **)user_lock);
3208 static void __kmp_init_indirect_lock(kmp_dyna_lock_t *lock,
3209 kmp_dyna_lockseq_t seq) {
3210 #if KMP_USE_ADAPTIVE_LOCKS
3211 if (seq == lockseq_adaptive && !__kmp_cpuinfo.rtm) {
3212 KMP_WARNING(AdaptiveNotSupported,
"kmp_lockseq_t",
"adaptive");
3213 seq = lockseq_queuing;
3217 if (seq == lockseq_rtm_queuing && !__kmp_cpuinfo.rtm) {
3218 seq = lockseq_queuing;
3221 kmp_indirect_locktag_t tag = KMP_GET_I_TAG(seq);
3222 kmp_indirect_lock_t *l =
3223 __kmp_allocate_indirect_lock((
void **)lock, __kmp_entry_gtid(), tag);
3224 KMP_I_LOCK_FUNC(l, init)(l->lock);
3226 20, (
"__kmp_init_indirect_lock: initialized indirect lock with type#%d\n",
3230 static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock) {
3231 kmp_uint32 gtid = __kmp_entry_gtid();
3232 kmp_indirect_lock_t *l =
3233 __kmp_lookup_indirect_lock((
void **)lock,
"omp_destroy_lock");
3234 KMP_I_LOCK_FUNC(l, destroy)(l->lock);
3235 kmp_indirect_locktag_t tag = l->type;
3237 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3240 l->lock->pool.next = (kmp_user_lock_p)__kmp_indirect_lock_pool[tag];
3241 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3242 l->lock->pool.index = KMP_EXTRACT_I_INDEX(lock);
3244 __kmp_indirect_lock_pool[tag] = l;
3246 __kmp_release_lock(&__kmp_global_lock, gtid);
3249 static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
3250 kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
3251 return KMP_I_LOCK_FUNC(l, set)(l->lock, gtid);
3254 static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
3255 kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
3256 return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
3259 static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
3260 kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
3261 return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
3264 static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
3266 kmp_indirect_lock_t *l =
3267 __kmp_lookup_indirect_lock((
void **)lock,
"omp_set_lock");
3268 return KMP_I_LOCK_FUNC(l, set)(l->lock, gtid);
3271 static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
3273 kmp_indirect_lock_t *l =
3274 __kmp_lookup_indirect_lock((
void **)lock,
"omp_unset_lock");
3275 return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
3278 static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
3280 kmp_indirect_lock_t *l =
3281 __kmp_lookup_indirect_lock((
void **)lock,
"omp_test_lock");
3282 return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
3285 kmp_dyna_lockseq_t __kmp_user_lock_seq = lockseq_queuing;
3288 kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p lck, kmp_uint32 seq) {
3291 case lockseq_nested_tas:
3292 return __kmp_get_tas_lock_owner((kmp_tas_lock_t *)lck);
3295 case lockseq_nested_futex:
3296 return __kmp_get_futex_lock_owner((kmp_futex_lock_t *)lck);
3298 case lockseq_ticket:
3299 case lockseq_nested_ticket:
3300 return __kmp_get_ticket_lock_owner((kmp_ticket_lock_t *)lck);
3301 case lockseq_queuing:
3302 case lockseq_nested_queuing:
3303 #if KMP_USE_ADAPTIVE_LOCKS
3304 case lockseq_adaptive:
3306 return __kmp_get_queuing_lock_owner((kmp_queuing_lock_t *)lck);
3308 case lockseq_nested_drdpa:
3309 return __kmp_get_drdpa_lock_owner((kmp_drdpa_lock_t *)lck);
3316 void __kmp_init_dynamic_user_locks() {
3318 if (__kmp_env_consistency_check) {
3319 __kmp_direct_set = direct_set_check;
3320 __kmp_direct_unset = direct_unset_check;
3321 __kmp_direct_test = direct_test_check;
3322 __kmp_direct_destroy = direct_destroy_check;
3323 __kmp_indirect_set = indirect_set_check;
3324 __kmp_indirect_unset = indirect_unset_check;
3325 __kmp_indirect_test = indirect_test_check;
3326 __kmp_indirect_destroy = indirect_destroy_check;
3328 __kmp_direct_set = direct_set;
3329 __kmp_direct_unset = direct_unset;
3330 __kmp_direct_test = direct_test;
3331 __kmp_direct_destroy = direct_destroy;
3332 __kmp_indirect_set = indirect_set;
3333 __kmp_indirect_unset = indirect_unset;
3334 __kmp_indirect_test = indirect_test;
3335 __kmp_indirect_destroy = indirect_destroy;
3340 if (__kmp_init_user_locks)
3344 __kmp_i_lock_table.size = KMP_I_LOCK_CHUNK;
3345 __kmp_i_lock_table.table =
3346 (kmp_indirect_lock_t **)__kmp_allocate(
sizeof(kmp_indirect_lock_t *));
3347 *(__kmp_i_lock_table.table) = (kmp_indirect_lock_t *)__kmp_allocate(
3348 KMP_I_LOCK_CHUNK *
sizeof(kmp_indirect_lock_t));
3349 __kmp_i_lock_table.next = 0;
3352 __kmp_indirect_lock_size[locktag_ticket] =
sizeof(kmp_ticket_lock_t);
3353 __kmp_indirect_lock_size[locktag_queuing] =
sizeof(kmp_queuing_lock_t);
3354 #if KMP_USE_ADAPTIVE_LOCKS
3355 __kmp_indirect_lock_size[locktag_adaptive] =
sizeof(kmp_adaptive_lock_t);
3357 __kmp_indirect_lock_size[locktag_drdpa] =
sizeof(kmp_drdpa_lock_t);
3359 __kmp_indirect_lock_size[locktag_rtm_queuing] =
sizeof(kmp_queuing_lock_t);
3361 __kmp_indirect_lock_size[locktag_nested_tas] =
sizeof(kmp_tas_lock_t);
3363 __kmp_indirect_lock_size[locktag_nested_futex] =
sizeof(kmp_futex_lock_t);
3365 __kmp_indirect_lock_size[locktag_nested_ticket] =
sizeof(kmp_ticket_lock_t);
3366 __kmp_indirect_lock_size[locktag_nested_queuing] =
sizeof(kmp_queuing_lock_t);
3367 __kmp_indirect_lock_size[locktag_nested_drdpa] =
sizeof(kmp_drdpa_lock_t);
3370 #define fill_jumps(table, expand, sep) \
3372 table[locktag##sep##ticket] = expand(ticket); \
3373 table[locktag##sep##queuing] = expand(queuing); \
3374 table[locktag##sep##drdpa] = expand(drdpa); \
3377 #if KMP_USE_ADAPTIVE_LOCKS
3378 #define fill_table(table, expand) \
3380 fill_jumps(table, expand, _); \
3381 table[locktag_adaptive] = expand(queuing); \
3382 fill_jumps(table, expand, _nested_); \
3385 #define fill_table(table, expand) \
3387 fill_jumps(table, expand, _); \
3388 fill_jumps(table, expand, _nested_); \
3393 (void (*)(kmp_user_lock_p, const ident_t *)) __kmp_set_##l##_lock_location
3394 fill_table(__kmp_indirect_set_location, expand);
3397 (void (*)(kmp_user_lock_p, kmp_lock_flags_t)) __kmp_set_##l##_lock_flags
3398 fill_table(__kmp_indirect_set_flags, expand);
3401 (const ident_t *(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_location
3402 fill_table(__kmp_indirect_get_location, expand);
3405 (kmp_lock_flags_t(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_flags
3406 fill_table(__kmp_indirect_get_flags, expand);
3409 __kmp_init_user_locks = TRUE;
3413 void __kmp_cleanup_indirect_user_locks() {
3419 for (k = 0; k < KMP_NUM_I_LOCKS; ++k) {
3420 kmp_indirect_lock_t *l = __kmp_indirect_lock_pool[k];
3422 kmp_indirect_lock_t *ll = l;
3423 l = (kmp_indirect_lock_t *)l->lock->pool.next;
3424 KA_TRACE(20, (
"__kmp_cleanup_indirect_user_locks: freeing %p from pool\n",
3426 __kmp_free(ll->lock);
3429 __kmp_indirect_lock_pool[k] = NULL;
3432 for (i = 0; i < __kmp_i_lock_table.next; i++) {
3433 kmp_indirect_lock_t *l = KMP_GET_I_LOCK(i);
3434 if (l->lock != NULL) {
3436 KMP_I_LOCK_FUNC(l, destroy)(l->lock);
3439 (
"__kmp_cleanup_indirect_user_locks: destroy/freeing %p from table\n",
3441 __kmp_free(l->lock);
3445 for (i = 0; i < __kmp_i_lock_table.size / KMP_I_LOCK_CHUNK; i++)
3446 __kmp_free(__kmp_i_lock_table.table[i]);
3447 __kmp_free(__kmp_i_lock_table.table);
3449 __kmp_init_user_locks = FALSE;
3452 enum kmp_lock_kind __kmp_user_lock_kind = lk_default;
3453 int __kmp_num_locks_in_block = 1;
3457 static void __kmp_init_tas_lock_with_checks(kmp_tas_lock_t *lck) {
3458 __kmp_init_tas_lock(lck);
3461 static void __kmp_init_nested_tas_lock_with_checks(kmp_tas_lock_t *lck) {
3462 __kmp_init_nested_tas_lock(lck);
3466 static void __kmp_init_futex_lock_with_checks(kmp_futex_lock_t *lck) {
3467 __kmp_init_futex_lock(lck);
3470 static void __kmp_init_nested_futex_lock_with_checks(kmp_futex_lock_t *lck) {
3471 __kmp_init_nested_futex_lock(lck);
3475 static int __kmp_is_ticket_lock_initialized(kmp_ticket_lock_t *lck) {
3476 return lck == lck->lk.self;
3479 static void __kmp_init_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
3480 __kmp_init_ticket_lock(lck);
3483 static void __kmp_init_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
3484 __kmp_init_nested_ticket_lock(lck);
3487 static int __kmp_is_queuing_lock_initialized(kmp_queuing_lock_t *lck) {
3488 return lck == lck->lk.initialized;
3491 static void __kmp_init_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
3492 __kmp_init_queuing_lock(lck);
3496 __kmp_init_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
3497 __kmp_init_nested_queuing_lock(lck);
3500 #if KMP_USE_ADAPTIVE_LOCKS
3501 static void __kmp_init_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
3502 __kmp_init_adaptive_lock(lck);
3506 static int __kmp_is_drdpa_lock_initialized(kmp_drdpa_lock_t *lck) {
3507 return lck == lck->lk.initialized;
3510 static void __kmp_init_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
3511 __kmp_init_drdpa_lock(lck);
3514 static void __kmp_init_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
3515 __kmp_init_nested_drdpa_lock(lck);
3522 enum kmp_lock_kind __kmp_user_lock_kind = lk_default;
3524 size_t __kmp_base_user_lock_size = 0;
3525 size_t __kmp_user_lock_size = 0;
3527 kmp_int32 (*__kmp_get_user_lock_owner_)(kmp_user_lock_p lck) = NULL;
3528 int (*__kmp_acquire_user_lock_with_checks_)(kmp_user_lock_p lck,
3529 kmp_int32 gtid) = NULL;
3531 int (*__kmp_test_user_lock_with_checks_)(kmp_user_lock_p lck,
3532 kmp_int32 gtid) = NULL;
3533 int (*__kmp_release_user_lock_with_checks_)(kmp_user_lock_p lck,
3534 kmp_int32 gtid) = NULL;
3535 void (*__kmp_init_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3536 void (*__kmp_destroy_user_lock_)(kmp_user_lock_p lck) = NULL;
3537 void (*__kmp_destroy_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3538 int (*__kmp_acquire_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
3539 kmp_int32 gtid) = NULL;
3541 int (*__kmp_test_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
3542 kmp_int32 gtid) = NULL;
3543 int (*__kmp_release_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
3544 kmp_int32 gtid) = NULL;
3545 void (*__kmp_init_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3546 void (*__kmp_destroy_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3548 int (*__kmp_is_user_lock_initialized_)(kmp_user_lock_p lck) = NULL;
3549 const ident_t *(*__kmp_get_user_lock_location_)(kmp_user_lock_p lck) = NULL;
3550 void (*__kmp_set_user_lock_location_)(kmp_user_lock_p lck,
3552 kmp_lock_flags_t (*__kmp_get_user_lock_flags_)(kmp_user_lock_p lck) = NULL;
3553 void (*__kmp_set_user_lock_flags_)(kmp_user_lock_p lck,
3554 kmp_lock_flags_t flags) = NULL;
3556 void __kmp_set_user_lock_vptrs(kmp_lock_kind_t user_lock_kind) {
3557 switch (user_lock_kind) {
3563 __kmp_base_user_lock_size =
sizeof(kmp_base_tas_lock_t);
3564 __kmp_user_lock_size =
sizeof(kmp_tas_lock_t);
3566 __kmp_get_user_lock_owner_ =
3567 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_tas_lock_owner);
3569 if (__kmp_env_consistency_check) {
3570 KMP_BIND_USER_LOCK_WITH_CHECKS(tas);
3571 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(tas);
3573 KMP_BIND_USER_LOCK(tas);
3574 KMP_BIND_NESTED_USER_LOCK(tas);
3577 __kmp_destroy_user_lock_ =
3578 (void (*)(kmp_user_lock_p))(&__kmp_destroy_tas_lock);
3580 __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;
3582 __kmp_get_user_lock_location_ = (
const ident_t *(*)(kmp_user_lock_p))NULL;
3584 __kmp_set_user_lock_location_ =
3585 (void (*)(kmp_user_lock_p,
const ident_t *))NULL;
3587 __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;
3589 __kmp_set_user_lock_flags_ =
3590 (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
3596 __kmp_base_user_lock_size =
sizeof(kmp_base_futex_lock_t);
3597 __kmp_user_lock_size =
sizeof(kmp_futex_lock_t);
3599 __kmp_get_user_lock_owner_ =
3600 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_futex_lock_owner);
3602 if (__kmp_env_consistency_check) {
3603 KMP_BIND_USER_LOCK_WITH_CHECKS(futex);
3604 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(futex);
3606 KMP_BIND_USER_LOCK(futex);
3607 KMP_BIND_NESTED_USER_LOCK(futex);
3610 __kmp_destroy_user_lock_ =
3611 (void (*)(kmp_user_lock_p))(&__kmp_destroy_futex_lock);
3613 __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;
3615 __kmp_get_user_lock_location_ = (
const ident_t *(*)(kmp_user_lock_p))NULL;
3617 __kmp_set_user_lock_location_ =
3618 (void (*)(kmp_user_lock_p,
const ident_t *))NULL;
3620 __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;
3622 __kmp_set_user_lock_flags_ =
3623 (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
3629 __kmp_base_user_lock_size =
sizeof(kmp_base_ticket_lock_t);
3630 __kmp_user_lock_size =
sizeof(kmp_ticket_lock_t);
3632 __kmp_get_user_lock_owner_ =
3633 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_owner);
3635 if (__kmp_env_consistency_check) {
3636 KMP_BIND_USER_LOCK_WITH_CHECKS(ticket);
3637 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(ticket);
3639 KMP_BIND_USER_LOCK(ticket);
3640 KMP_BIND_NESTED_USER_LOCK(ticket);
3643 __kmp_destroy_user_lock_ =
3644 (void (*)(kmp_user_lock_p))(&__kmp_destroy_ticket_lock);
3646 __kmp_is_user_lock_initialized_ =
3647 (int (*)(kmp_user_lock_p))(&__kmp_is_ticket_lock_initialized);
3649 __kmp_get_user_lock_location_ =
3650 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_location);
3652 __kmp_set_user_lock_location_ = (void (*)(
3653 kmp_user_lock_p,
const ident_t *))(&__kmp_set_ticket_lock_location);
3655 __kmp_get_user_lock_flags_ =
3656 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_flags);
3658 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3659 &__kmp_set_ticket_lock_flags);
3663 __kmp_base_user_lock_size =
sizeof(kmp_base_queuing_lock_t);
3664 __kmp_user_lock_size =
sizeof(kmp_queuing_lock_t);
3666 __kmp_get_user_lock_owner_ =
3667 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);
3669 if (__kmp_env_consistency_check) {
3670 KMP_BIND_USER_LOCK_WITH_CHECKS(queuing);
3671 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(queuing);
3673 KMP_BIND_USER_LOCK(queuing);
3674 KMP_BIND_NESTED_USER_LOCK(queuing);
3677 __kmp_destroy_user_lock_ =
3678 (void (*)(kmp_user_lock_p))(&__kmp_destroy_queuing_lock);
3680 __kmp_is_user_lock_initialized_ =
3681 (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);
3683 __kmp_get_user_lock_location_ =
3684 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);
3686 __kmp_set_user_lock_location_ = (void (*)(
3687 kmp_user_lock_p,
const ident_t *))(&__kmp_set_queuing_lock_location);
3689 __kmp_get_user_lock_flags_ =
3690 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);
3692 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3693 &__kmp_set_queuing_lock_flags);
3696 #if KMP_USE_ADAPTIVE_LOCKS
3698 __kmp_base_user_lock_size =
sizeof(kmp_base_adaptive_lock_t);
3699 __kmp_user_lock_size =
sizeof(kmp_adaptive_lock_t);
3701 __kmp_get_user_lock_owner_ =
3702 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);
3704 if (__kmp_env_consistency_check) {
3705 KMP_BIND_USER_LOCK_WITH_CHECKS(adaptive);
3707 KMP_BIND_USER_LOCK(adaptive);
3710 __kmp_destroy_user_lock_ =
3711 (void (*)(kmp_user_lock_p))(&__kmp_destroy_adaptive_lock);
3713 __kmp_is_user_lock_initialized_ =
3714 (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);
3716 __kmp_get_user_lock_location_ =
3717 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);
3719 __kmp_set_user_lock_location_ = (void (*)(
3720 kmp_user_lock_p,
const ident_t *))(&__kmp_set_queuing_lock_location);
3722 __kmp_get_user_lock_flags_ =
3723 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);
3725 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3726 &__kmp_set_queuing_lock_flags);
3732 __kmp_base_user_lock_size =
sizeof(kmp_base_drdpa_lock_t);
3733 __kmp_user_lock_size =
sizeof(kmp_drdpa_lock_t);
3735 __kmp_get_user_lock_owner_ =
3736 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_owner);
3738 if (__kmp_env_consistency_check) {
3739 KMP_BIND_USER_LOCK_WITH_CHECKS(drdpa);
3740 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(drdpa);
3742 KMP_BIND_USER_LOCK(drdpa);
3743 KMP_BIND_NESTED_USER_LOCK(drdpa);
3746 __kmp_destroy_user_lock_ =
3747 (void (*)(kmp_user_lock_p))(&__kmp_destroy_drdpa_lock);
3749 __kmp_is_user_lock_initialized_ =
3750 (int (*)(kmp_user_lock_p))(&__kmp_is_drdpa_lock_initialized);
3752 __kmp_get_user_lock_location_ =
3753 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_location);
3755 __kmp_set_user_lock_location_ = (void (*)(
3756 kmp_user_lock_p,
const ident_t *))(&__kmp_set_drdpa_lock_location);
3758 __kmp_get_user_lock_flags_ =
3759 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_flags);
3761 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3762 &__kmp_set_drdpa_lock_flags);
3770 kmp_lock_table_t __kmp_user_lock_table = {1, 0, NULL};
3771 kmp_user_lock_p __kmp_lock_pool = NULL;
3774 kmp_block_of_locks *__kmp_lock_blocks = NULL;
3775 int __kmp_num_locks_in_block = 1;
3777 static kmp_lock_index_t __kmp_lock_table_insert(kmp_user_lock_p lck) {
3779 kmp_lock_index_t index;
3780 if (__kmp_user_lock_table.used >= __kmp_user_lock_table.allocated) {
3781 kmp_lock_index_t size;
3782 kmp_user_lock_p *table;
3784 if (__kmp_user_lock_table.allocated == 0) {
3787 size = __kmp_user_lock_table.allocated * 2;
3789 table = (kmp_user_lock_p *)__kmp_allocate(
sizeof(kmp_user_lock_p) * size);
3790 KMP_MEMCPY(table + 1, __kmp_user_lock_table.table + 1,
3791 sizeof(kmp_user_lock_p) * (__kmp_user_lock_table.used - 1));
3792 table[0] = (kmp_user_lock_p)__kmp_user_lock_table.table;
3797 __kmp_user_lock_table.table = table;
3798 __kmp_user_lock_table.allocated = size;
3800 KMP_DEBUG_ASSERT(__kmp_user_lock_table.used <
3801 __kmp_user_lock_table.allocated);
3802 index = __kmp_user_lock_table.used;
3803 __kmp_user_lock_table.table[index] = lck;
3804 ++__kmp_user_lock_table.used;
3808 static kmp_user_lock_p __kmp_lock_block_allocate() {
3810 static int last_index = 0;
3811 if ((last_index >= __kmp_num_locks_in_block) || (__kmp_lock_blocks == NULL)) {
3815 KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
3816 size_t space_for_locks = __kmp_user_lock_size * __kmp_num_locks_in_block;
3818 (
char *)__kmp_allocate(space_for_locks +
sizeof(kmp_block_of_locks));
3820 kmp_block_of_locks *new_block =
3821 (kmp_block_of_locks *)(&buffer[space_for_locks]);
3822 new_block->next_block = __kmp_lock_blocks;
3823 new_block->locks = (
void *)buffer;
3826 __kmp_lock_blocks = new_block;
3828 kmp_user_lock_p ret = (kmp_user_lock_p)(&(
3829 ((
char *)(__kmp_lock_blocks->locks))[last_index * __kmp_user_lock_size]));
3836 kmp_user_lock_p __kmp_user_lock_allocate(
void **user_lock, kmp_int32 gtid,
3837 kmp_lock_flags_t flags) {
3838 kmp_user_lock_p lck;
3839 kmp_lock_index_t index;
3840 KMP_DEBUG_ASSERT(user_lock);
3842 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3844 if (__kmp_lock_pool == NULL) {
3849 ANNOTATE_IGNORE_WRITES_BEGIN();
3850 if (__kmp_num_locks_in_block <= 1) {
3851 lck = (kmp_user_lock_p)__kmp_allocate(__kmp_user_lock_size);
3853 lck = __kmp_lock_block_allocate();
3855 ANNOTATE_IGNORE_WRITES_END();
3859 index = __kmp_lock_table_insert(lck);
3862 lck = __kmp_lock_pool;
3863 index = __kmp_lock_pool->pool.index;
3864 __kmp_lock_pool = __kmp_lock_pool->pool.next;
3869 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3870 *((kmp_lock_index_t *)user_lock) = index;
3872 *((kmp_user_lock_p *)user_lock) = lck;
3876 __kmp_set_user_lock_flags(lck, flags);
3878 __kmp_release_lock(&__kmp_global_lock, gtid);
3884 void __kmp_user_lock_free(
void **user_lock, kmp_int32 gtid,
3885 kmp_user_lock_p lck) {
3886 KMP_DEBUG_ASSERT(user_lock != NULL);
3887 KMP_DEBUG_ASSERT(lck != NULL);
3889 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3891 lck->pool.next = __kmp_lock_pool;
3892 __kmp_lock_pool = lck;
3893 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3894 kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
3895 KMP_DEBUG_ASSERT(0 < index && index <= __kmp_user_lock_table.used);
3896 lck->pool.index = index;
3899 __kmp_release_lock(&__kmp_global_lock, gtid);
3902 kmp_user_lock_p __kmp_lookup_user_lock(
void **user_lock,
char const *func) {
3903 kmp_user_lock_p lck = NULL;
3905 if (__kmp_env_consistency_check) {
3906 if (user_lock == NULL) {
3907 KMP_FATAL(LockIsUninitialized, func);
3911 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3912 kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
3913 if (__kmp_env_consistency_check) {
3914 if (!(0 < index && index < __kmp_user_lock_table.used)) {
3915 KMP_FATAL(LockIsUninitialized, func);
3918 KMP_DEBUG_ASSERT(0 < index && index < __kmp_user_lock_table.used);
3919 KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
3920 lck = __kmp_user_lock_table.table[index];
3922 lck = *((kmp_user_lock_p *)user_lock);
3925 if (__kmp_env_consistency_check) {
3927 KMP_FATAL(LockIsUninitialized, func);
3934 void __kmp_cleanup_user_locks(
void) {
3937 __kmp_lock_pool = NULL;
3939 #define IS_CRITICAL(lck) \
3940 ((__kmp_get_user_lock_flags_ != NULL) && \
3941 ((*__kmp_get_user_lock_flags_)(lck)&kmp_lf_critical_section))
3966 while (__kmp_user_lock_table.used > 1) {
3971 kmp_user_lock_p lck =
3972 __kmp_user_lock_table.table[--__kmp_user_lock_table.used];
3974 if ((__kmp_is_user_lock_initialized_ != NULL) &&
3975 (*__kmp_is_user_lock_initialized_)(lck)) {
3979 if (__kmp_env_consistency_check && (!IS_CRITICAL(lck)) &&
3980 ((loc = __kmp_get_user_lock_location(lck)) != NULL) &&
3982 kmp_str_loc_t str_loc = __kmp_str_loc_init(loc->
psource,
false);
3983 KMP_WARNING(CnsLockNotDestroyed, str_loc.file, str_loc.line);
3984 __kmp_str_loc_free(&str_loc);
3988 if (IS_CRITICAL(lck)) {
3991 (
"__kmp_cleanup_user_locks: free critical section lock %p (%p)\n",
3992 lck, *(
void **)lck));
3994 KA_TRACE(20, (
"__kmp_cleanup_user_locks: free lock %p (%p)\n", lck,
4000 __kmp_destroy_user_lock(lck);
4004 if (__kmp_lock_blocks == NULL) {
4012 kmp_user_lock_p *table_ptr = __kmp_user_lock_table.table;
4013 __kmp_user_lock_table.table = NULL;
4014 __kmp_user_lock_table.allocated = 0;
4016 while (table_ptr != NULL) {
4019 kmp_user_lock_p *next = (kmp_user_lock_p *)(table_ptr[0]);
4020 __kmp_free(table_ptr);
4025 kmp_block_of_locks_t *block_ptr = __kmp_lock_blocks;
4026 __kmp_lock_blocks = NULL;
4028 while (block_ptr != NULL) {
4029 kmp_block_of_locks_t *next = block_ptr->next_block;
4030 __kmp_free(block_ptr->locks);
4035 TCW_4(__kmp_init_user_locks, FALSE);
void open(const char *filename, const char *mode, const char *env_var=nullptr)