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=%d 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);
603 #endif // KMP_USE_FUTEX 608 static kmp_int32 __kmp_get_ticket_lock_owner(kmp_ticket_lock_t *lck) {
609 return std::atomic_load_explicit(&lck->lk.owner_id,
610 std::memory_order_relaxed) -
614 static inline bool __kmp_is_ticket_lock_nestable(kmp_ticket_lock_t *lck) {
615 return std::atomic_load_explicit(&lck->lk.depth_locked,
616 std::memory_order_relaxed) != -1;
619 static kmp_uint32 __kmp_bakery_check(
void *now_serving, kmp_uint32 my_ticket) {
620 return std::atomic_load_explicit((std::atomic<unsigned> *)now_serving,
621 std::memory_order_acquire) == my_ticket;
624 __forceinline
static int 625 __kmp_acquire_ticket_lock_timed_template(kmp_ticket_lock_t *lck,
627 kmp_uint32 my_ticket = std::atomic_fetch_add_explicit(
628 &lck->lk.next_ticket, 1U, std::memory_order_relaxed);
630 #ifdef USE_LOCK_PROFILE 631 if (std::atomic_load_explicit(&lck->lk.now_serving,
632 std::memory_order_relaxed) != my_ticket)
633 __kmp_printf(
"LOCK CONTENTION: %p\n", lck);
637 if (std::atomic_load_explicit(&lck->lk.now_serving,
638 std::memory_order_acquire) == my_ticket) {
639 return KMP_LOCK_ACQUIRED_FIRST;
641 KMP_WAIT_PTR(&lck->lk.now_serving, my_ticket, __kmp_bakery_check, lck);
642 return KMP_LOCK_ACQUIRED_FIRST;
645 int __kmp_acquire_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
646 int retval = __kmp_acquire_ticket_lock_timed_template(lck, gtid);
647 ANNOTATE_TICKET_ACQUIRED(lck);
651 static int __kmp_acquire_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
653 char const *
const func =
"omp_set_lock";
655 if (!std::atomic_load_explicit(&lck->lk.initialized,
656 std::memory_order_relaxed)) {
657 KMP_FATAL(LockIsUninitialized, func);
659 if (lck->lk.self != lck) {
660 KMP_FATAL(LockIsUninitialized, func);
662 if (__kmp_is_ticket_lock_nestable(lck)) {
663 KMP_FATAL(LockNestableUsedAsSimple, func);
665 if ((gtid >= 0) && (__kmp_get_ticket_lock_owner(lck) == gtid)) {
666 KMP_FATAL(LockIsAlreadyOwned, func);
669 __kmp_acquire_ticket_lock(lck, gtid);
671 std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
672 std::memory_order_relaxed);
673 return KMP_LOCK_ACQUIRED_FIRST;
676 int __kmp_test_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
677 kmp_uint32 my_ticket = std::atomic_load_explicit(&lck->lk.next_ticket,
678 std::memory_order_relaxed);
680 if (std::atomic_load_explicit(&lck->lk.now_serving,
681 std::memory_order_relaxed) == my_ticket) {
682 kmp_uint32 next_ticket = my_ticket + 1;
683 if (std::atomic_compare_exchange_strong_explicit(
684 &lck->lk.next_ticket, &my_ticket, next_ticket,
685 std::memory_order_acquire, std::memory_order_acquire)) {
692 static int __kmp_test_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
694 char const *
const func =
"omp_test_lock";
696 if (!std::atomic_load_explicit(&lck->lk.initialized,
697 std::memory_order_relaxed)) {
698 KMP_FATAL(LockIsUninitialized, func);
700 if (lck->lk.self != lck) {
701 KMP_FATAL(LockIsUninitialized, func);
703 if (__kmp_is_ticket_lock_nestable(lck)) {
704 KMP_FATAL(LockNestableUsedAsSimple, func);
707 int retval = __kmp_test_ticket_lock(lck, gtid);
710 std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
711 std::memory_order_relaxed);
716 int __kmp_release_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
717 kmp_uint32 distance = std::atomic_load_explicit(&lck->lk.next_ticket,
718 std::memory_order_relaxed) -
719 std::atomic_load_explicit(&lck->lk.now_serving,
720 std::memory_order_relaxed);
722 ANNOTATE_TICKET_RELEASED(lck);
723 std::atomic_fetch_add_explicit(&lck->lk.now_serving, 1U,
724 std::memory_order_release);
727 (kmp_uint32)(__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc));
728 return KMP_LOCK_RELEASED;
731 static int __kmp_release_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
733 char const *
const func =
"omp_unset_lock";
735 if (!std::atomic_load_explicit(&lck->lk.initialized,
736 std::memory_order_relaxed)) {
737 KMP_FATAL(LockIsUninitialized, func);
739 if (lck->lk.self != lck) {
740 KMP_FATAL(LockIsUninitialized, func);
742 if (__kmp_is_ticket_lock_nestable(lck)) {
743 KMP_FATAL(LockNestableUsedAsSimple, func);
745 if (__kmp_get_ticket_lock_owner(lck) == -1) {
746 KMP_FATAL(LockUnsettingFree, func);
748 if ((gtid >= 0) && (__kmp_get_ticket_lock_owner(lck) >= 0) &&
749 (__kmp_get_ticket_lock_owner(lck) != gtid)) {
750 KMP_FATAL(LockUnsettingSetByAnother, func);
752 std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
753 return __kmp_release_ticket_lock(lck, gtid);
756 void __kmp_init_ticket_lock(kmp_ticket_lock_t *lck) {
757 lck->lk.location = NULL;
759 std::atomic_store_explicit(&lck->lk.next_ticket, 0U,
760 std::memory_order_relaxed);
761 std::atomic_store_explicit(&lck->lk.now_serving, 0U,
762 std::memory_order_relaxed);
763 std::atomic_store_explicit(
764 &lck->lk.owner_id, 0,
765 std::memory_order_relaxed);
766 std::atomic_store_explicit(
767 &lck->lk.depth_locked, -1,
768 std::memory_order_relaxed);
769 std::atomic_store_explicit(&lck->lk.initialized,
true,
770 std::memory_order_release);
773 void __kmp_destroy_ticket_lock(kmp_ticket_lock_t *lck) {
774 std::atomic_store_explicit(&lck->lk.initialized,
false,
775 std::memory_order_release);
777 lck->lk.location = NULL;
778 std::atomic_store_explicit(&lck->lk.next_ticket, 0U,
779 std::memory_order_relaxed);
780 std::atomic_store_explicit(&lck->lk.now_serving, 0U,
781 std::memory_order_relaxed);
782 std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
783 std::atomic_store_explicit(&lck->lk.depth_locked, -1,
784 std::memory_order_relaxed);
787 static void __kmp_destroy_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
788 char const *
const func =
"omp_destroy_lock";
790 if (!std::atomic_load_explicit(&lck->lk.initialized,
791 std::memory_order_relaxed)) {
792 KMP_FATAL(LockIsUninitialized, func);
794 if (lck->lk.self != lck) {
795 KMP_FATAL(LockIsUninitialized, func);
797 if (__kmp_is_ticket_lock_nestable(lck)) {
798 KMP_FATAL(LockNestableUsedAsSimple, func);
800 if (__kmp_get_ticket_lock_owner(lck) != -1) {
801 KMP_FATAL(LockStillOwned, func);
803 __kmp_destroy_ticket_lock(lck);
808 int __kmp_acquire_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
809 KMP_DEBUG_ASSERT(gtid >= 0);
811 if (__kmp_get_ticket_lock_owner(lck) == gtid) {
812 std::atomic_fetch_add_explicit(&lck->lk.depth_locked, 1,
813 std::memory_order_relaxed);
814 return KMP_LOCK_ACQUIRED_NEXT;
816 __kmp_acquire_ticket_lock_timed_template(lck, gtid);
817 ANNOTATE_TICKET_ACQUIRED(lck);
818 std::atomic_store_explicit(&lck->lk.depth_locked, 1,
819 std::memory_order_relaxed);
820 std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
821 std::memory_order_relaxed);
822 return KMP_LOCK_ACQUIRED_FIRST;
826 static int __kmp_acquire_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
828 char const *
const func =
"omp_set_nest_lock";
830 if (!std::atomic_load_explicit(&lck->lk.initialized,
831 std::memory_order_relaxed)) {
832 KMP_FATAL(LockIsUninitialized, func);
834 if (lck->lk.self != lck) {
835 KMP_FATAL(LockIsUninitialized, func);
837 if (!__kmp_is_ticket_lock_nestable(lck)) {
838 KMP_FATAL(LockSimpleUsedAsNestable, func);
840 return __kmp_acquire_nested_ticket_lock(lck, gtid);
843 int __kmp_test_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
846 KMP_DEBUG_ASSERT(gtid >= 0);
848 if (__kmp_get_ticket_lock_owner(lck) == gtid) {
849 retval = std::atomic_fetch_add_explicit(&lck->lk.depth_locked, 1,
850 std::memory_order_relaxed) +
852 }
else if (!__kmp_test_ticket_lock(lck, gtid)) {
855 std::atomic_store_explicit(&lck->lk.depth_locked, 1,
856 std::memory_order_relaxed);
857 std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
858 std::memory_order_relaxed);
864 static int __kmp_test_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
866 char const *
const func =
"omp_test_nest_lock";
868 if (!std::atomic_load_explicit(&lck->lk.initialized,
869 std::memory_order_relaxed)) {
870 KMP_FATAL(LockIsUninitialized, func);
872 if (lck->lk.self != lck) {
873 KMP_FATAL(LockIsUninitialized, func);
875 if (!__kmp_is_ticket_lock_nestable(lck)) {
876 KMP_FATAL(LockSimpleUsedAsNestable, func);
878 return __kmp_test_nested_ticket_lock(lck, gtid);
881 int __kmp_release_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
882 KMP_DEBUG_ASSERT(gtid >= 0);
884 if ((std::atomic_fetch_add_explicit(&lck->lk.depth_locked, -1,
885 std::memory_order_relaxed) -
887 std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
888 __kmp_release_ticket_lock(lck, gtid);
889 return KMP_LOCK_RELEASED;
891 return KMP_LOCK_STILL_HELD;
894 static int __kmp_release_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
896 char const *
const func =
"omp_unset_nest_lock";
898 if (!std::atomic_load_explicit(&lck->lk.initialized,
899 std::memory_order_relaxed)) {
900 KMP_FATAL(LockIsUninitialized, func);
902 if (lck->lk.self != lck) {
903 KMP_FATAL(LockIsUninitialized, func);
905 if (!__kmp_is_ticket_lock_nestable(lck)) {
906 KMP_FATAL(LockSimpleUsedAsNestable, func);
908 if (__kmp_get_ticket_lock_owner(lck) == -1) {
909 KMP_FATAL(LockUnsettingFree, func);
911 if (__kmp_get_ticket_lock_owner(lck) != gtid) {
912 KMP_FATAL(LockUnsettingSetByAnother, func);
914 return __kmp_release_nested_ticket_lock(lck, gtid);
917 void __kmp_init_nested_ticket_lock(kmp_ticket_lock_t *lck) {
918 __kmp_init_ticket_lock(lck);
919 std::atomic_store_explicit(&lck->lk.depth_locked, 0,
920 std::memory_order_relaxed);
924 void __kmp_destroy_nested_ticket_lock(kmp_ticket_lock_t *lck) {
925 __kmp_destroy_ticket_lock(lck);
926 std::atomic_store_explicit(&lck->lk.depth_locked, 0,
927 std::memory_order_relaxed);
931 __kmp_destroy_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
932 char const *
const func =
"omp_destroy_nest_lock";
934 if (!std::atomic_load_explicit(&lck->lk.initialized,
935 std::memory_order_relaxed)) {
936 KMP_FATAL(LockIsUninitialized, func);
938 if (lck->lk.self != lck) {
939 KMP_FATAL(LockIsUninitialized, func);
941 if (!__kmp_is_ticket_lock_nestable(lck)) {
942 KMP_FATAL(LockSimpleUsedAsNestable, func);
944 if (__kmp_get_ticket_lock_owner(lck) != -1) {
945 KMP_FATAL(LockStillOwned, func);
947 __kmp_destroy_nested_ticket_lock(lck);
952 static const ident_t *__kmp_get_ticket_lock_location(kmp_ticket_lock_t *lck) {
953 return lck->lk.location;
956 static void __kmp_set_ticket_lock_location(kmp_ticket_lock_t *lck,
958 lck->lk.location = loc;
961 static kmp_lock_flags_t __kmp_get_ticket_lock_flags(kmp_ticket_lock_t *lck) {
962 return lck->lk.flags;
965 static void __kmp_set_ticket_lock_flags(kmp_ticket_lock_t *lck,
966 kmp_lock_flags_t flags) {
967 lck->lk.flags = flags;
1025 #ifdef DEBUG_QUEUING_LOCKS 1028 #define TRACE_BUF_ELE 1024 1029 static char traces[TRACE_BUF_ELE][128] = {0};
1031 #define TRACE_LOCK(X, Y) \ 1032 KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s\n", X, Y); 1033 #define TRACE_LOCK_T(X, Y, Z) \ 1034 KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s%d\n", X, Y, Z); 1035 #define TRACE_LOCK_HT(X, Y, Z, Q) \ 1036 KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s %d,%d\n", X, Y, \ 1039 static void __kmp_dump_queuing_lock(kmp_info_t *this_thr, kmp_int32 gtid,
1040 kmp_queuing_lock_t *lck, kmp_int32 head_id,
1041 kmp_int32 tail_id) {
1044 __kmp_printf_no_lock(
"\n__kmp_dump_queuing_lock: TRACE BEGINS HERE! \n");
1046 i = tc % TRACE_BUF_ELE;
1047 __kmp_printf_no_lock(
"%s\n", traces[i]);
1048 i = (i + 1) % TRACE_BUF_ELE;
1049 while (i != (tc % TRACE_BUF_ELE)) {
1050 __kmp_printf_no_lock(
"%s", traces[i]);
1051 i = (i + 1) % TRACE_BUF_ELE;
1053 __kmp_printf_no_lock(
"\n");
1055 __kmp_printf_no_lock(
"\n__kmp_dump_queuing_lock: gtid+1:%d, spin_here:%d, " 1056 "next_wait:%d, head_id:%d, tail_id:%d\n",
1057 gtid + 1, this_thr->th.th_spin_here,
1058 this_thr->th.th_next_waiting, head_id, tail_id);
1060 __kmp_printf_no_lock(
"\t\thead: %d ", lck->lk.head_id);
1062 if (lck->lk.head_id >= 1) {
1063 t = __kmp_threads[lck->lk.head_id - 1]->th.th_next_waiting;
1065 __kmp_printf_no_lock(
"-> %d ", t);
1066 t = __kmp_threads[t - 1]->th.th_next_waiting;
1069 __kmp_printf_no_lock(
"; tail: %d ", lck->lk.tail_id);
1070 __kmp_printf_no_lock(
"\n\n");
1075 static kmp_int32 __kmp_get_queuing_lock_owner(kmp_queuing_lock_t *lck) {
1076 return TCR_4(lck->lk.owner_id) - 1;
1079 static inline bool __kmp_is_queuing_lock_nestable(kmp_queuing_lock_t *lck) {
1080 return lck->lk.depth_locked != -1;
1084 template <
bool takeTime>
1087 __forceinline
static int 1088 __kmp_acquire_queuing_lock_timed_template(kmp_queuing_lock_t *lck,
1090 kmp_info_t *this_thr = __kmp_thread_from_gtid(gtid);
1091 volatile kmp_int32 *head_id_p = &lck->lk.head_id;
1092 volatile kmp_int32 *tail_id_p = &lck->lk.tail_id;
1093 volatile kmp_uint32 *spin_here_p;
1094 kmp_int32 need_mf = 1;
1097 ompt_state_t prev_state = ompt_state_undefined;
1101 (
"__kmp_acquire_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));
1103 KMP_FSYNC_PREPARE(lck);
1104 KMP_DEBUG_ASSERT(this_thr != NULL);
1105 spin_here_p = &this_thr->th.th_spin_here;
1107 #ifdef DEBUG_QUEUING_LOCKS 1108 TRACE_LOCK(gtid + 1,
"acq ent");
1110 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1111 if (this_thr->th.th_next_waiting != 0)
1112 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1114 KMP_DEBUG_ASSERT(!*spin_here_p);
1115 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
1124 *spin_here_p = TRUE;
1136 #ifdef DEBUG_QUEUING_LOCKS 1138 TRACE_LOCK_HT(gtid + 1,
"acq read: ", head, tail);
1147 enqueued = KMP_COMPARE_AND_STORE_ACQ64((
volatile kmp_int64 *)tail_id_p,
1149 KMP_PACK_64(gtid + 1, gtid + 1));
1150 #ifdef DEBUG_QUEUING_LOCKS 1152 TRACE_LOCK(gtid + 1,
"acq enq: (-1,0)->(tid,tid)");
1158 KMP_DEBUG_ASSERT(tail != gtid + 1);
1160 #ifdef DEBUG_QUEUING_LOCKS 1161 TRACE_LOCK_HT(gtid + 1,
"acq read: ", head, tail);
1169 enqueued = KMP_COMPARE_AND_STORE_ACQ32(tail_id_p, tail, gtid + 1);
1171 #ifdef DEBUG_QUEUING_LOCKS 1173 TRACE_LOCK(gtid + 1,
"acq enq: (h,t)->(h,tid)");
1180 kmp_int32 grabbed_lock;
1182 #ifdef DEBUG_QUEUING_LOCKS 1184 TRACE_LOCK_HT(gtid + 1,
"acq read: ", head, tail);
1190 grabbed_lock = KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1);
1194 *spin_here_p = FALSE;
1198 (
"__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: no queuing\n",
1200 #ifdef DEBUG_QUEUING_LOCKS 1201 TRACE_LOCK_HT(gtid + 1,
"acq exit: ", head, 0);
1205 if (ompt_enabled.enabled && prev_state != ompt_state_undefined) {
1207 this_thr->th.ompt_thread_info.state = prev_state;
1208 this_thr->th.ompt_thread_info.wait_id = 0;
1212 KMP_FSYNC_ACQUIRED(lck);
1213 return KMP_LOCK_ACQUIRED_FIRST;
1220 if (ompt_enabled.enabled && prev_state == ompt_state_undefined) {
1222 prev_state = this_thr->th.ompt_thread_info.state;
1223 this_thr->th.ompt_thread_info.wait_id = (uint64_t)lck;
1224 this_thr->th.ompt_thread_info.state = ompt_state_wait_lock;
1230 kmp_info_t *tail_thr = __kmp_thread_from_gtid(tail - 1);
1231 KMP_ASSERT(tail_thr != NULL);
1232 tail_thr->th.th_next_waiting = gtid + 1;
1236 (
"__kmp_acquire_queuing_lock: lck:%p, T#%d waiting for lock\n",
1241 KMP_WAIT(spin_here_p, FALSE, KMP_EQ, lck);
1246 #ifdef DEBUG_QUEUING_LOCKS 1247 TRACE_LOCK(gtid + 1,
"acq spin");
1249 if (this_thr->th.th_next_waiting != 0)
1250 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1252 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
1253 KA_TRACE(1000, (
"__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: after " 1254 "waiting on queue\n",
1257 #ifdef DEBUG_QUEUING_LOCKS 1258 TRACE_LOCK(gtid + 1,
"acq exit 2");
1263 this_thr->th.ompt_thread_info.state = prev_state;
1264 this_thr->th.ompt_thread_info.wait_id = 0;
1268 return KMP_LOCK_ACQUIRED_FIRST;
1274 KMP_YIELD_OVERSUB();
1276 #ifdef DEBUG_QUEUING_LOCKS 1277 TRACE_LOCK(gtid + 1,
"acq retry");
1280 KMP_ASSERT2(0,
"should not get here");
1281 return KMP_LOCK_ACQUIRED_FIRST;
1284 int __kmp_acquire_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1285 KMP_DEBUG_ASSERT(gtid >= 0);
1287 int retval = __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
1288 ANNOTATE_QUEUING_ACQUIRED(lck);
1292 static int __kmp_acquire_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1294 char const *
const func =
"omp_set_lock";
1295 if (lck->lk.initialized != lck) {
1296 KMP_FATAL(LockIsUninitialized, func);
1298 if (__kmp_is_queuing_lock_nestable(lck)) {
1299 KMP_FATAL(LockNestableUsedAsSimple, func);
1301 if (__kmp_get_queuing_lock_owner(lck) == gtid) {
1302 KMP_FATAL(LockIsAlreadyOwned, func);
1305 __kmp_acquire_queuing_lock(lck, gtid);
1307 lck->lk.owner_id = gtid + 1;
1308 return KMP_LOCK_ACQUIRED_FIRST;
1311 int __kmp_test_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1312 volatile kmp_int32 *head_id_p = &lck->lk.head_id;
1315 kmp_info_t *this_thr;
1318 KA_TRACE(1000, (
"__kmp_test_queuing_lock: T#%d entering\n", gtid));
1319 KMP_DEBUG_ASSERT(gtid >= 0);
1321 this_thr = __kmp_thread_from_gtid(gtid);
1322 KMP_DEBUG_ASSERT(this_thr != NULL);
1323 KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
1330 if (KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1)) {
1332 (
"__kmp_test_queuing_lock: T#%d exiting: holding lock\n", gtid));
1333 KMP_FSYNC_ACQUIRED(lck);
1334 ANNOTATE_QUEUING_ACQUIRED(lck);
1340 (
"__kmp_test_queuing_lock: T#%d exiting: without lock\n", gtid));
1344 static int __kmp_test_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1346 char const *
const func =
"omp_test_lock";
1347 if (lck->lk.initialized != lck) {
1348 KMP_FATAL(LockIsUninitialized, func);
1350 if (__kmp_is_queuing_lock_nestable(lck)) {
1351 KMP_FATAL(LockNestableUsedAsSimple, func);
1354 int retval = __kmp_test_queuing_lock(lck, gtid);
1357 lck->lk.owner_id = gtid + 1;
1362 int __kmp_release_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1363 kmp_info_t *this_thr;
1364 volatile kmp_int32 *head_id_p = &lck->lk.head_id;
1365 volatile kmp_int32 *tail_id_p = &lck->lk.tail_id;
1368 (
"__kmp_release_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));
1369 KMP_DEBUG_ASSERT(gtid >= 0);
1370 this_thr = __kmp_thread_from_gtid(gtid);
1371 KMP_DEBUG_ASSERT(this_thr != NULL);
1372 #ifdef DEBUG_QUEUING_LOCKS 1373 TRACE_LOCK(gtid + 1,
"rel ent");
1375 if (this_thr->th.th_spin_here)
1376 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1377 if (this_thr->th.th_next_waiting != 0)
1378 __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
1380 KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
1381 KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
1383 KMP_FSYNC_RELEASING(lck);
1384 ANNOTATE_QUEUING_RELEASED(lck);
1393 #ifdef DEBUG_QUEUING_LOCKS 1395 TRACE_LOCK_HT(gtid + 1,
"rel read: ", head, tail);
1397 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1399 KMP_DEBUG_ASSERT(head !=
1404 if (KMP_COMPARE_AND_STORE_REL32(head_id_p, -1, 0)) {
1407 (
"__kmp_release_queuing_lock: lck:%p, T#%d exiting: queue empty\n",
1409 #ifdef DEBUG_QUEUING_LOCKS 1410 TRACE_LOCK_HT(gtid + 1,
"rel exit: ", 0, 0);
1416 return KMP_LOCK_RELEASED;
1423 #ifdef DEBUG_QUEUING_LOCKS 1425 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1427 KMP_DEBUG_ASSERT(head > 0);
1430 dequeued = KMP_COMPARE_AND_STORE_REL64(
1431 RCAST(
volatile kmp_int64 *, tail_id_p), KMP_PACK_64(head, head),
1432 KMP_PACK_64(-1, 0));
1433 #ifdef DEBUG_QUEUING_LOCKS 1434 TRACE_LOCK(gtid + 1,
"rel deq: (h,h)->(-1,0)");
1438 volatile kmp_int32 *waiting_id_p;
1439 kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
1440 KMP_DEBUG_ASSERT(head_thr != NULL);
1441 waiting_id_p = &head_thr->th.th_next_waiting;
1444 #ifdef DEBUG_QUEUING_LOCKS 1445 if (head <= 0 || tail <= 0)
1446 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1448 KMP_DEBUG_ASSERT(head > 0 && tail > 0);
1455 KMP_WAIT((
volatile kmp_uint32 *)waiting_id_p, 0, KMP_NEQ, NULL);
1456 #ifdef DEBUG_QUEUING_LOCKS 1457 TRACE_LOCK(gtid + 1,
"rel deq: (h,t)->(h',t)");
1464 kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
1465 KMP_DEBUG_ASSERT(head_thr != NULL);
1468 #ifdef DEBUG_QUEUING_LOCKS 1469 if (head <= 0 || tail <= 0)
1470 __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
1472 KMP_DEBUG_ASSERT(head > 0 && tail > 0);
1476 head_thr->th.th_next_waiting = 0;
1477 #ifdef DEBUG_QUEUING_LOCKS 1478 TRACE_LOCK_T(gtid + 1,
"rel nw=0 for t=", head);
1483 head_thr->th.th_spin_here = FALSE;
1485 KA_TRACE(1000, (
"__kmp_release_queuing_lock: lck:%p, T#%d exiting: after " 1488 #ifdef DEBUG_QUEUING_LOCKS 1489 TRACE_LOCK(gtid + 1,
"rel exit 2");
1491 return KMP_LOCK_RELEASED;
1496 #ifdef DEBUG_QUEUING_LOCKS 1497 TRACE_LOCK(gtid + 1,
"rel retry");
1501 KMP_ASSERT2(0,
"should not get here");
1502 return KMP_LOCK_RELEASED;
1505 static int __kmp_release_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1507 char const *
const func =
"omp_unset_lock";
1509 if (lck->lk.initialized != lck) {
1510 KMP_FATAL(LockIsUninitialized, func);
1512 if (__kmp_is_queuing_lock_nestable(lck)) {
1513 KMP_FATAL(LockNestableUsedAsSimple, func);
1515 if (__kmp_get_queuing_lock_owner(lck) == -1) {
1516 KMP_FATAL(LockUnsettingFree, func);
1518 if (__kmp_get_queuing_lock_owner(lck) != gtid) {
1519 KMP_FATAL(LockUnsettingSetByAnother, func);
1521 lck->lk.owner_id = 0;
1522 return __kmp_release_queuing_lock(lck, gtid);
1525 void __kmp_init_queuing_lock(kmp_queuing_lock_t *lck) {
1526 lck->lk.location = NULL;
1527 lck->lk.head_id = 0;
1528 lck->lk.tail_id = 0;
1529 lck->lk.next_ticket = 0;
1530 lck->lk.now_serving = 0;
1531 lck->lk.owner_id = 0;
1532 lck->lk.depth_locked = -1;
1533 lck->lk.initialized = lck;
1535 KA_TRACE(1000, (
"__kmp_init_queuing_lock: lock %p initialized\n", lck));
1538 void __kmp_destroy_queuing_lock(kmp_queuing_lock_t *lck) {
1539 lck->lk.initialized = NULL;
1540 lck->lk.location = NULL;
1541 lck->lk.head_id = 0;
1542 lck->lk.tail_id = 0;
1543 lck->lk.next_ticket = 0;
1544 lck->lk.now_serving = 0;
1545 lck->lk.owner_id = 0;
1546 lck->lk.depth_locked = -1;
1549 static void __kmp_destroy_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
1550 char const *
const func =
"omp_destroy_lock";
1551 if (lck->lk.initialized != lck) {
1552 KMP_FATAL(LockIsUninitialized, func);
1554 if (__kmp_is_queuing_lock_nestable(lck)) {
1555 KMP_FATAL(LockNestableUsedAsSimple, func);
1557 if (__kmp_get_queuing_lock_owner(lck) != -1) {
1558 KMP_FATAL(LockStillOwned, func);
1560 __kmp_destroy_queuing_lock(lck);
1565 int __kmp_acquire_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1566 KMP_DEBUG_ASSERT(gtid >= 0);
1568 if (__kmp_get_queuing_lock_owner(lck) == gtid) {
1569 lck->lk.depth_locked += 1;
1570 return KMP_LOCK_ACQUIRED_NEXT;
1572 __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
1573 ANNOTATE_QUEUING_ACQUIRED(lck);
1575 lck->lk.depth_locked = 1;
1577 lck->lk.owner_id = gtid + 1;
1578 return KMP_LOCK_ACQUIRED_FIRST;
1583 __kmp_acquire_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1585 char const *
const func =
"omp_set_nest_lock";
1586 if (lck->lk.initialized != lck) {
1587 KMP_FATAL(LockIsUninitialized, func);
1589 if (!__kmp_is_queuing_lock_nestable(lck)) {
1590 KMP_FATAL(LockSimpleUsedAsNestable, func);
1592 return __kmp_acquire_nested_queuing_lock(lck, gtid);
1595 int __kmp_test_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1598 KMP_DEBUG_ASSERT(gtid >= 0);
1600 if (__kmp_get_queuing_lock_owner(lck) == gtid) {
1601 retval = ++lck->lk.depth_locked;
1602 }
else if (!__kmp_test_queuing_lock(lck, gtid)) {
1606 retval = lck->lk.depth_locked = 1;
1608 lck->lk.owner_id = gtid + 1;
1613 static int __kmp_test_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1615 char const *
const func =
"omp_test_nest_lock";
1616 if (lck->lk.initialized != lck) {
1617 KMP_FATAL(LockIsUninitialized, func);
1619 if (!__kmp_is_queuing_lock_nestable(lck)) {
1620 KMP_FATAL(LockSimpleUsedAsNestable, func);
1622 return __kmp_test_nested_queuing_lock(lck, gtid);
1625 int __kmp_release_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
1626 KMP_DEBUG_ASSERT(gtid >= 0);
1629 if (--(lck->lk.depth_locked) == 0) {
1631 lck->lk.owner_id = 0;
1632 __kmp_release_queuing_lock(lck, gtid);
1633 return KMP_LOCK_RELEASED;
1635 return KMP_LOCK_STILL_HELD;
1639 __kmp_release_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
1641 char const *
const func =
"omp_unset_nest_lock";
1643 if (lck->lk.initialized != lck) {
1644 KMP_FATAL(LockIsUninitialized, func);
1646 if (!__kmp_is_queuing_lock_nestable(lck)) {
1647 KMP_FATAL(LockSimpleUsedAsNestable, func);
1649 if (__kmp_get_queuing_lock_owner(lck) == -1) {
1650 KMP_FATAL(LockUnsettingFree, func);
1652 if (__kmp_get_queuing_lock_owner(lck) != gtid) {
1653 KMP_FATAL(LockUnsettingSetByAnother, func);
1655 return __kmp_release_nested_queuing_lock(lck, gtid);
1658 void __kmp_init_nested_queuing_lock(kmp_queuing_lock_t *lck) {
1659 __kmp_init_queuing_lock(lck);
1660 lck->lk.depth_locked = 0;
1663 void __kmp_destroy_nested_queuing_lock(kmp_queuing_lock_t *lck) {
1664 __kmp_destroy_queuing_lock(lck);
1665 lck->lk.depth_locked = 0;
1669 __kmp_destroy_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
1670 char const *
const func =
"omp_destroy_nest_lock";
1671 if (lck->lk.initialized != lck) {
1672 KMP_FATAL(LockIsUninitialized, func);
1674 if (!__kmp_is_queuing_lock_nestable(lck)) {
1675 KMP_FATAL(LockSimpleUsedAsNestable, func);
1677 if (__kmp_get_queuing_lock_owner(lck) != -1) {
1678 KMP_FATAL(LockStillOwned, func);
1680 __kmp_destroy_nested_queuing_lock(lck);
1685 static const ident_t *__kmp_get_queuing_lock_location(kmp_queuing_lock_t *lck) {
1686 return lck->lk.location;
1689 static void __kmp_set_queuing_lock_location(kmp_queuing_lock_t *lck,
1691 lck->lk.location = loc;
1694 static kmp_lock_flags_t __kmp_get_queuing_lock_flags(kmp_queuing_lock_t *lck) {
1695 return lck->lk.flags;
1698 static void __kmp_set_queuing_lock_flags(kmp_queuing_lock_t *lck,
1699 kmp_lock_flags_t flags) {
1700 lck->lk.flags = flags;
1703 #if KMP_USE_ADAPTIVE_LOCKS 1707 #if (KMP_COMPILER_ICC && __INTEL_COMPILER >= 1300) || \ 1708 (KMP_COMPILER_MSVC && _MSC_VER >= 1700) || \ 1709 (KMP_COMPILER_CLANG && (KMP_MSVC_COMPAT || __MINGW32__)) || \ 1710 (KMP_COMPILER_GCC && __MINGW32__) 1712 #include <immintrin.h> 1713 #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT) 1718 #define _XBEGIN_STARTED (~0u) 1719 #define _XABORT_EXPLICIT (1 << 0) 1720 #define _XABORT_RETRY (1 << 1) 1721 #define _XABORT_CONFLICT (1 << 2) 1722 #define _XABORT_CAPACITY (1 << 3) 1723 #define _XABORT_DEBUG (1 << 4) 1724 #define _XABORT_NESTED (1 << 5) 1725 #define _XABORT_CODE(x) ((unsigned char)(((x) >> 24) & 0xFF)) 1728 #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT) 1730 #define STRINGIZE_INTERNAL(arg) #arg 1731 #define STRINGIZE(arg) STRINGIZE_INTERNAL(arg) 1737 static __inline
int _xbegin() {
1765 #endif // KMP_ARCH_X86_64 1774 __asm__
volatile(
"1: .byte 0xC7; .byte 0xF8;\n" 1777 "1: movl %%eax,%0\n" 1779 :
"+r"(res)::
"memory",
"%eax");
1780 #endif // KMP_OS_WINDOWS 1785 static __inline
void _xend() {
1793 __asm__
volatile(
".byte 0x0f; .byte 0x01; .byte 0xd5" :::
"memory");
1802 #define _xabort(ARG) _asm _emit 0xc6 _asm _emit 0xf8 _asm _emit ARG 1804 #define _xabort(ARG) \ 1805 __asm__ volatile(".byte 0xC6; .byte 0xF8; .byte " STRINGIZE(ARG):::"memory"); 1808 #endif // KMP_COMPILER_ICC && __INTEL_COMPILER >= 1300 1811 #if KMP_DEBUG_ADAPTIVE_LOCKS 1816 static kmp_adaptive_lock_statistics_t destroyedStats;
1819 static kmp_adaptive_lock_info_t liveLocks;
1822 static kmp_bootstrap_lock_t chain_lock =
1823 KMP_BOOTSTRAP_LOCK_INITIALIZER(chain_lock);
1826 void __kmp_init_speculative_stats() {
1827 kmp_adaptive_lock_info_t *lck = &liveLocks;
1829 memset(CCAST(kmp_adaptive_lock_statistics_t *, &(lck->stats)), 0,
1830 sizeof(lck->stats));
1831 lck->stats.next = lck;
1832 lck->stats.prev = lck;
1834 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1835 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1837 __kmp_init_bootstrap_lock(&chain_lock);
1841 static void __kmp_remember_lock(kmp_adaptive_lock_info_t *lck) {
1842 __kmp_acquire_bootstrap_lock(&chain_lock);
1844 lck->stats.next = liveLocks.stats.next;
1845 lck->stats.prev = &liveLocks;
1847 liveLocks.stats.next = lck;
1848 lck->stats.next->stats.prev = lck;
1850 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1851 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1853 __kmp_release_bootstrap_lock(&chain_lock);
1856 static void __kmp_forget_lock(kmp_adaptive_lock_info_t *lck) {
1857 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1858 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1860 kmp_adaptive_lock_info_t *n = lck->stats.next;
1861 kmp_adaptive_lock_info_t *p = lck->stats.prev;
1867 static void __kmp_zero_speculative_stats(kmp_adaptive_lock_info_t *lck) {
1868 memset(CCAST(kmp_adaptive_lock_statistics_t *, &lck->stats), 0,
1869 sizeof(lck->stats));
1870 __kmp_remember_lock(lck);
1873 static void __kmp_add_stats(kmp_adaptive_lock_statistics_t *t,
1874 kmp_adaptive_lock_info_t *lck) {
1875 kmp_adaptive_lock_statistics_t
volatile *s = &lck->stats;
1877 t->nonSpeculativeAcquireAttempts += lck->acquire_attempts;
1878 t->successfulSpeculations += s->successfulSpeculations;
1879 t->hardFailedSpeculations += s->hardFailedSpeculations;
1880 t->softFailedSpeculations += s->softFailedSpeculations;
1881 t->nonSpeculativeAcquires += s->nonSpeculativeAcquires;
1882 t->lemmingYields += s->lemmingYields;
1885 static void __kmp_accumulate_speculative_stats(kmp_adaptive_lock_info_t *lck) {
1886 __kmp_acquire_bootstrap_lock(&chain_lock);
1888 __kmp_add_stats(&destroyedStats, lck);
1889 __kmp_forget_lock(lck);
1891 __kmp_release_bootstrap_lock(&chain_lock);
1894 static float percent(kmp_uint32 count, kmp_uint32 total) {
1895 return (total == 0) ? 0.0 : (100.0 * count) / total;
1898 static FILE *__kmp_open_stats_file() {
1899 if (strcmp(__kmp_speculative_statsfile,
"-") == 0)
1902 size_t buffLen = KMP_STRLEN(__kmp_speculative_statsfile) + 20;
1903 char buffer[buffLen];
1904 KMP_SNPRINTF(&buffer[0], buffLen, __kmp_speculative_statsfile,
1905 (kmp_int32)getpid());
1906 FILE *result = fopen(&buffer[0],
"w");
1909 return result ? result : stdout;
1912 void __kmp_print_speculative_stats() {
1913 kmp_adaptive_lock_statistics_t total = destroyedStats;
1914 kmp_adaptive_lock_info_t *lck;
1916 for (lck = liveLocks.stats.next; lck != &liveLocks; lck = lck->stats.next) {
1917 __kmp_add_stats(&total, lck);
1919 kmp_adaptive_lock_statistics_t *t = &total;
1920 kmp_uint32 totalSections =
1921 t->nonSpeculativeAcquires + t->successfulSpeculations;
1922 kmp_uint32 totalSpeculations = t->successfulSpeculations +
1923 t->hardFailedSpeculations +
1924 t->softFailedSpeculations;
1925 if (totalSections <= 0)
1928 FILE *statsFile = __kmp_open_stats_file();
1930 fprintf(statsFile,
"Speculative lock statistics (all approximate!)\n");
1931 fprintf(statsFile,
" Lock parameters: \n" 1932 " max_soft_retries : %10d\n" 1933 " max_badness : %10d\n",
1934 __kmp_adaptive_backoff_params.max_soft_retries,
1935 __kmp_adaptive_backoff_params.max_badness);
1936 fprintf(statsFile,
" Non-speculative acquire attempts : %10d\n",
1937 t->nonSpeculativeAcquireAttempts);
1938 fprintf(statsFile,
" Total critical sections : %10d\n",
1940 fprintf(statsFile,
" Successful speculations : %10d (%5.1f%%)\n",
1941 t->successfulSpeculations,
1942 percent(t->successfulSpeculations, totalSections));
1943 fprintf(statsFile,
" Non-speculative acquires : %10d (%5.1f%%)\n",
1944 t->nonSpeculativeAcquires,
1945 percent(t->nonSpeculativeAcquires, totalSections));
1946 fprintf(statsFile,
" Lemming yields : %10d\n\n",
1949 fprintf(statsFile,
" Speculative acquire attempts : %10d\n",
1951 fprintf(statsFile,
" Successes : %10d (%5.1f%%)\n",
1952 t->successfulSpeculations,
1953 percent(t->successfulSpeculations, totalSpeculations));
1954 fprintf(statsFile,
" Soft failures : %10d (%5.1f%%)\n",
1955 t->softFailedSpeculations,
1956 percent(t->softFailedSpeculations, totalSpeculations));
1957 fprintf(statsFile,
" Hard failures : %10d (%5.1f%%)\n",
1958 t->hardFailedSpeculations,
1959 percent(t->hardFailedSpeculations, totalSpeculations));
1961 if (statsFile != stdout)
1965 #define KMP_INC_STAT(lck, stat) (lck->lk.adaptive.stats.stat++) 1967 #define KMP_INC_STAT(lck, stat) 1969 #endif // KMP_DEBUG_ADAPTIVE_LOCKS 1971 static inline bool __kmp_is_unlocked_queuing_lock(kmp_queuing_lock_t *lck) {
1974 bool res = lck->lk.head_id == 0;
1978 #if KMP_COMPILER_ICC 1981 __sync_synchronize();
1988 static __inline
void 1989 __kmp_update_badness_after_success(kmp_adaptive_lock_t *lck) {
1991 lck->lk.adaptive.badness = 0;
1992 KMP_INC_STAT(lck, successfulSpeculations);
1996 static __inline
void __kmp_step_badness(kmp_adaptive_lock_t *lck) {
1997 kmp_uint32 newBadness = (lck->lk.adaptive.badness << 1) | 1;
1998 if (newBadness > lck->lk.adaptive.max_badness) {
2001 lck->lk.adaptive.badness = newBadness;
2006 static __inline
int __kmp_should_speculate(kmp_adaptive_lock_t *lck,
2008 kmp_uint32 badness = lck->lk.adaptive.badness;
2009 kmp_uint32 attempts = lck->lk.adaptive.acquire_attempts;
2010 int res = (attempts & badness) == 0;
2016 static int __kmp_test_adaptive_lock_only(kmp_adaptive_lock_t *lck,
2018 int retries = lck->lk.adaptive.max_soft_retries;
2025 kmp_uint32 status = _xbegin();
2030 if (status == _XBEGIN_STARTED) {
2035 if (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2039 KMP_ASSERT2(0,
"should not get here");
2044 if (status & SOFT_ABORT_MASK) {
2045 KMP_INC_STAT(lck, softFailedSpeculations);
2048 KMP_INC_STAT(lck, hardFailedSpeculations);
2053 }
while (retries--);
2057 __kmp_step_badness(lck);
2064 static int __kmp_test_adaptive_lock(kmp_adaptive_lock_t *lck, kmp_int32 gtid) {
2066 if (__kmp_should_speculate(lck, gtid) &&
2067 __kmp_test_adaptive_lock_only(lck, gtid))
2072 lck->lk.adaptive.acquire_attempts++;
2075 if (__kmp_test_queuing_lock(GET_QLK_PTR(lck), gtid)) {
2076 KMP_INC_STAT(lck, nonSpeculativeAcquires);
2083 static int __kmp_test_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2085 char const *
const func =
"omp_test_lock";
2086 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2087 KMP_FATAL(LockIsUninitialized, func);
2090 int retval = __kmp_test_adaptive_lock(lck, gtid);
2093 lck->lk.qlk.owner_id = gtid + 1;
2109 static void __kmp_acquire_adaptive_lock(kmp_adaptive_lock_t *lck,
2111 if (__kmp_should_speculate(lck, gtid)) {
2112 if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2113 if (__kmp_test_adaptive_lock_only(lck, gtid))
2122 while (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2123 KMP_INC_STAT(lck, lemmingYields);
2127 if (__kmp_test_adaptive_lock_only(lck, gtid))
2134 lck->lk.adaptive.acquire_attempts++;
2136 __kmp_acquire_queuing_lock_timed_template<FALSE>(GET_QLK_PTR(lck), gtid);
2138 KMP_INC_STAT(lck, nonSpeculativeAcquires);
2139 ANNOTATE_QUEUING_ACQUIRED(lck);
2142 static void __kmp_acquire_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2144 char const *
const func =
"omp_set_lock";
2145 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2146 KMP_FATAL(LockIsUninitialized, func);
2148 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == gtid) {
2149 KMP_FATAL(LockIsAlreadyOwned, func);
2152 __kmp_acquire_adaptive_lock(lck, gtid);
2154 lck->lk.qlk.owner_id = gtid + 1;
2157 static int __kmp_release_adaptive_lock(kmp_adaptive_lock_t *lck,
2159 if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(
2164 __kmp_update_badness_after_success(lck);
2167 __kmp_release_queuing_lock(GET_QLK_PTR(lck), gtid);
2169 return KMP_LOCK_RELEASED;
2172 static int __kmp_release_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2174 char const *
const func =
"omp_unset_lock";
2176 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2177 KMP_FATAL(LockIsUninitialized, func);
2179 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == -1) {
2180 KMP_FATAL(LockUnsettingFree, func);
2182 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != gtid) {
2183 KMP_FATAL(LockUnsettingSetByAnother, func);
2185 lck->lk.qlk.owner_id = 0;
2186 __kmp_release_adaptive_lock(lck, gtid);
2187 return KMP_LOCK_RELEASED;
2190 static void __kmp_init_adaptive_lock(kmp_adaptive_lock_t *lck) {
2191 __kmp_init_queuing_lock(GET_QLK_PTR(lck));
2192 lck->lk.adaptive.badness = 0;
2193 lck->lk.adaptive.acquire_attempts = 0;
2194 lck->lk.adaptive.max_soft_retries =
2195 __kmp_adaptive_backoff_params.max_soft_retries;
2196 lck->lk.adaptive.max_badness = __kmp_adaptive_backoff_params.max_badness;
2197 #if KMP_DEBUG_ADAPTIVE_LOCKS 2198 __kmp_zero_speculative_stats(&lck->lk.adaptive);
2200 KA_TRACE(1000, (
"__kmp_init_adaptive_lock: lock %p initialized\n", lck));
2203 static void __kmp_destroy_adaptive_lock(kmp_adaptive_lock_t *lck) {
2204 #if KMP_DEBUG_ADAPTIVE_LOCKS 2205 __kmp_accumulate_speculative_stats(&lck->lk.adaptive);
2207 __kmp_destroy_queuing_lock(GET_QLK_PTR(lck));
2211 static void __kmp_destroy_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
2212 char const *
const func =
"omp_destroy_lock";
2213 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2214 KMP_FATAL(LockIsUninitialized, func);
2216 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != -1) {
2217 KMP_FATAL(LockStillOwned, func);
2219 __kmp_destroy_adaptive_lock(lck);
2222 #endif // KMP_USE_ADAPTIVE_LOCKS 2228 static kmp_int32 __kmp_get_drdpa_lock_owner(kmp_drdpa_lock_t *lck) {
2229 return lck->lk.owner_id - 1;
2232 static inline bool __kmp_is_drdpa_lock_nestable(kmp_drdpa_lock_t *lck) {
2233 return lck->lk.depth_locked != -1;
2236 __forceinline
static int 2237 __kmp_acquire_drdpa_lock_timed_template(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2238 kmp_uint64 ticket = KMP_ATOMIC_INC(&lck->lk.next_ticket);
2239 kmp_uint64 mask = lck->lk.mask;
2240 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2242 #ifdef USE_LOCK_PROFILE 2243 if (polls[ticket & mask] != ticket)
2244 __kmp_printf(
"LOCK CONTENTION: %p\n", lck);
2256 KMP_FSYNC_PREPARE(lck);
2257 KMP_INIT_YIELD(spins);
2258 while (polls[ticket & mask] < ticket) {
2259 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
2267 mask = lck->lk.mask;
2268 polls = lck->lk.polls;
2272 KMP_FSYNC_ACQUIRED(lck);
2273 KA_TRACE(1000, (
"__kmp_acquire_drdpa_lock: ticket #%lld acquired lock %p\n",
2275 lck->lk.now_serving = ticket;
2282 if ((lck->lk.old_polls != NULL) && (ticket >= lck->lk.cleanup_ticket)) {
2283 __kmp_free(lck->lk.old_polls);
2284 lck->lk.old_polls = NULL;
2285 lck->lk.cleanup_ticket = 0;
2291 if (lck->lk.old_polls == NULL) {
2292 bool reconfigure =
false;
2293 std::atomic<kmp_uint64> *old_polls = polls;
2294 kmp_uint32 num_polls = TCR_4(lck->lk.num_polls);
2296 if (TCR_4(__kmp_nth) >
2297 (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) {
2300 if (num_polls > 1) {
2302 num_polls = TCR_4(lck->lk.num_polls);
2305 polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
2313 kmp_uint64 num_waiting = TCR_8(lck->lk.next_ticket) - ticket - 1;
2314 if (num_waiting > num_polls) {
2315 kmp_uint32 old_num_polls = num_polls;
2318 mask = (mask << 1) | 1;
2320 }
while (num_polls <= num_waiting);
2326 polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
2329 for (i = 0; i < old_num_polls; i++) {
2330 polls[i].store(old_polls[i]);
2345 KA_TRACE(1000, (
"__kmp_acquire_drdpa_lock: ticket #%lld reconfiguring " 2346 "lock %p to %d polls\n",
2347 ticket, lck, num_polls));
2349 lck->lk.old_polls = old_polls;
2350 lck->lk.polls = polls;
2354 lck->lk.num_polls = num_polls;
2355 lck->lk.mask = mask;
2363 lck->lk.cleanup_ticket = lck->lk.next_ticket;
2366 return KMP_LOCK_ACQUIRED_FIRST;
2369 int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2370 int retval = __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
2371 ANNOTATE_DRDPA_ACQUIRED(lck);
2375 static int __kmp_acquire_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2377 char const *
const func =
"omp_set_lock";
2378 if (lck->lk.initialized != lck) {
2379 KMP_FATAL(LockIsUninitialized, func);
2381 if (__kmp_is_drdpa_lock_nestable(lck)) {
2382 KMP_FATAL(LockNestableUsedAsSimple, func);
2384 if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) == gtid)) {
2385 KMP_FATAL(LockIsAlreadyOwned, func);
2388 __kmp_acquire_drdpa_lock(lck, gtid);
2390 lck->lk.owner_id = gtid + 1;
2391 return KMP_LOCK_ACQUIRED_FIRST;
2394 int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2397 kmp_uint64 ticket = lck->lk.next_ticket;
2398 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2399 kmp_uint64 mask = lck->lk.mask;
2400 if (polls[ticket & mask] == ticket) {
2401 kmp_uint64 next_ticket = ticket + 1;
2402 if (__kmp_atomic_compare_store_acq(&lck->lk.next_ticket, ticket,
2404 KMP_FSYNC_ACQUIRED(lck);
2405 KA_TRACE(1000, (
"__kmp_test_drdpa_lock: ticket #%lld acquired lock %p\n",
2407 lck->lk.now_serving = ticket;
2421 static int __kmp_test_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2423 char const *
const func =
"omp_test_lock";
2424 if (lck->lk.initialized != lck) {
2425 KMP_FATAL(LockIsUninitialized, func);
2427 if (__kmp_is_drdpa_lock_nestable(lck)) {
2428 KMP_FATAL(LockNestableUsedAsSimple, func);
2431 int retval = __kmp_test_drdpa_lock(lck, gtid);
2434 lck->lk.owner_id = gtid + 1;
2439 int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2442 kmp_uint64 ticket = lck->lk.now_serving + 1;
2443 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2444 kmp_uint64 mask = lck->lk.mask;
2445 KA_TRACE(1000, (
"__kmp_release_drdpa_lock: ticket #%lld released lock %p\n",
2447 KMP_FSYNC_RELEASING(lck);
2448 ANNOTATE_DRDPA_RELEASED(lck);
2449 polls[ticket & mask] = ticket;
2450 return KMP_LOCK_RELEASED;
2453 static int __kmp_release_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2455 char const *
const func =
"omp_unset_lock";
2457 if (lck->lk.initialized != lck) {
2458 KMP_FATAL(LockIsUninitialized, func);
2460 if (__kmp_is_drdpa_lock_nestable(lck)) {
2461 KMP_FATAL(LockNestableUsedAsSimple, func);
2463 if (__kmp_get_drdpa_lock_owner(lck) == -1) {
2464 KMP_FATAL(LockUnsettingFree, func);
2466 if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) >= 0) &&
2467 (__kmp_get_drdpa_lock_owner(lck) != gtid)) {
2468 KMP_FATAL(LockUnsettingSetByAnother, func);
2470 lck->lk.owner_id = 0;
2471 return __kmp_release_drdpa_lock(lck, gtid);
2474 void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck) {
2475 lck->lk.location = NULL;
2477 lck->lk.num_polls = 1;
2478 lck->lk.polls = (std::atomic<kmp_uint64> *)__kmp_allocate(
2479 lck->lk.num_polls *
sizeof(*(lck->lk.polls)));
2480 lck->lk.cleanup_ticket = 0;
2481 lck->lk.old_polls = NULL;
2482 lck->lk.next_ticket = 0;
2483 lck->lk.now_serving = 0;
2484 lck->lk.owner_id = 0;
2485 lck->lk.depth_locked = -1;
2486 lck->lk.initialized = lck;
2488 KA_TRACE(1000, (
"__kmp_init_drdpa_lock: lock %p initialized\n", lck));
2491 void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck) {
2492 lck->lk.initialized = NULL;
2493 lck->lk.location = NULL;
2494 if (lck->lk.polls.load() != NULL) {
2495 __kmp_free(lck->lk.polls.load());
2496 lck->lk.polls = NULL;
2498 if (lck->lk.old_polls != NULL) {
2499 __kmp_free(lck->lk.old_polls);
2500 lck->lk.old_polls = NULL;
2503 lck->lk.num_polls = 0;
2504 lck->lk.cleanup_ticket = 0;
2505 lck->lk.next_ticket = 0;
2506 lck->lk.now_serving = 0;
2507 lck->lk.owner_id = 0;
2508 lck->lk.depth_locked = -1;
2511 static void __kmp_destroy_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
2512 char const *
const func =
"omp_destroy_lock";
2513 if (lck->lk.initialized != lck) {
2514 KMP_FATAL(LockIsUninitialized, func);
2516 if (__kmp_is_drdpa_lock_nestable(lck)) {
2517 KMP_FATAL(LockNestableUsedAsSimple, func);
2519 if (__kmp_get_drdpa_lock_owner(lck) != -1) {
2520 KMP_FATAL(LockStillOwned, func);
2522 __kmp_destroy_drdpa_lock(lck);
2527 int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2528 KMP_DEBUG_ASSERT(gtid >= 0);
2530 if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
2531 lck->lk.depth_locked += 1;
2532 return KMP_LOCK_ACQUIRED_NEXT;
2534 __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
2535 ANNOTATE_DRDPA_ACQUIRED(lck);
2537 lck->lk.depth_locked = 1;
2539 lck->lk.owner_id = gtid + 1;
2540 return KMP_LOCK_ACQUIRED_FIRST;
2544 static void __kmp_acquire_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2546 char const *
const func =
"omp_set_nest_lock";
2547 if (lck->lk.initialized != lck) {
2548 KMP_FATAL(LockIsUninitialized, func);
2550 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2551 KMP_FATAL(LockSimpleUsedAsNestable, func);
2553 __kmp_acquire_nested_drdpa_lock(lck, gtid);
2556 int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2559 KMP_DEBUG_ASSERT(gtid >= 0);
2561 if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
2562 retval = ++lck->lk.depth_locked;
2563 }
else if (!__kmp_test_drdpa_lock(lck, gtid)) {
2567 retval = lck->lk.depth_locked = 1;
2569 lck->lk.owner_id = gtid + 1;
2574 static int __kmp_test_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2576 char const *
const func =
"omp_test_nest_lock";
2577 if (lck->lk.initialized != lck) {
2578 KMP_FATAL(LockIsUninitialized, func);
2580 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2581 KMP_FATAL(LockSimpleUsedAsNestable, func);
2583 return __kmp_test_nested_drdpa_lock(lck, gtid);
2586 int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2587 KMP_DEBUG_ASSERT(gtid >= 0);
2590 if (--(lck->lk.depth_locked) == 0) {
2592 lck->lk.owner_id = 0;
2593 __kmp_release_drdpa_lock(lck, gtid);
2594 return KMP_LOCK_RELEASED;
2596 return KMP_LOCK_STILL_HELD;