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) 1711 #include <immintrin.h> 1712 #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT) 1717 #define _XBEGIN_STARTED (~0u) 1718 #define _XABORT_EXPLICIT (1 << 0) 1719 #define _XABORT_RETRY (1 << 1) 1720 #define _XABORT_CONFLICT (1 << 2) 1721 #define _XABORT_CAPACITY (1 << 3) 1722 #define _XABORT_DEBUG (1 << 4) 1723 #define _XABORT_NESTED (1 << 5) 1724 #define _XABORT_CODE(x) ((unsigned char)(((x) >> 24) & 0xFF)) 1727 #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT) 1729 #define STRINGIZE_INTERNAL(arg) #arg 1730 #define STRINGIZE(arg) STRINGIZE_INTERNAL(arg) 1736 static __inline
int _xbegin() {
1764 #endif // KMP_ARCH_X86_64 1773 __asm__
volatile(
"1: .byte 0xC7; .byte 0xF8;\n" 1776 "1: movl %%eax,%0\n" 1778 :
"+r"(res)::
"memory",
"%eax");
1779 #endif // KMP_OS_WINDOWS 1784 static __inline
void _xend() {
1792 __asm__
volatile(
".byte 0x0f; .byte 0x01; .byte 0xd5" :::
"memory");
1801 #define _xabort(ARG) _asm _emit 0xc6 _asm _emit 0xf8 _asm _emit ARG 1803 #define _xabort(ARG) \ 1804 __asm__ volatile(".byte 0xC6; .byte 0xF8; .byte " STRINGIZE(ARG):::"memory"); 1807 #endif // KMP_COMPILER_ICC && __INTEL_COMPILER >= 1300 1810 #if KMP_DEBUG_ADAPTIVE_LOCKS 1815 static kmp_adaptive_lock_statistics_t destroyedStats;
1818 static kmp_adaptive_lock_info_t liveLocks;
1821 static kmp_bootstrap_lock_t chain_lock =
1822 KMP_BOOTSTRAP_LOCK_INITIALIZER(chain_lock);
1825 void __kmp_init_speculative_stats() {
1826 kmp_adaptive_lock_info_t *lck = &liveLocks;
1828 memset(CCAST(kmp_adaptive_lock_statistics_t *, &(lck->stats)), 0,
1829 sizeof(lck->stats));
1830 lck->stats.next = lck;
1831 lck->stats.prev = lck;
1833 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1834 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1836 __kmp_init_bootstrap_lock(&chain_lock);
1840 static void __kmp_remember_lock(kmp_adaptive_lock_info_t *lck) {
1841 __kmp_acquire_bootstrap_lock(&chain_lock);
1843 lck->stats.next = liveLocks.stats.next;
1844 lck->stats.prev = &liveLocks;
1846 liveLocks.stats.next = lck;
1847 lck->stats.next->stats.prev = lck;
1849 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1850 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1852 __kmp_release_bootstrap_lock(&chain_lock);
1855 static void __kmp_forget_lock(kmp_adaptive_lock_info_t *lck) {
1856 KMP_ASSERT(lck->stats.next->stats.prev == lck);
1857 KMP_ASSERT(lck->stats.prev->stats.next == lck);
1859 kmp_adaptive_lock_info_t *n = lck->stats.next;
1860 kmp_adaptive_lock_info_t *p = lck->stats.prev;
1866 static void __kmp_zero_speculative_stats(kmp_adaptive_lock_info_t *lck) {
1867 memset(CCAST(kmp_adaptive_lock_statistics_t *, &lck->stats), 0,
1868 sizeof(lck->stats));
1869 __kmp_remember_lock(lck);
1872 static void __kmp_add_stats(kmp_adaptive_lock_statistics_t *t,
1873 kmp_adaptive_lock_info_t *lck) {
1874 kmp_adaptive_lock_statistics_t
volatile *s = &lck->stats;
1876 t->nonSpeculativeAcquireAttempts += lck->acquire_attempts;
1877 t->successfulSpeculations += s->successfulSpeculations;
1878 t->hardFailedSpeculations += s->hardFailedSpeculations;
1879 t->softFailedSpeculations += s->softFailedSpeculations;
1880 t->nonSpeculativeAcquires += s->nonSpeculativeAcquires;
1881 t->lemmingYields += s->lemmingYields;
1884 static void __kmp_accumulate_speculative_stats(kmp_adaptive_lock_info_t *lck) {
1885 __kmp_acquire_bootstrap_lock(&chain_lock);
1887 __kmp_add_stats(&destroyedStats, lck);
1888 __kmp_forget_lock(lck);
1890 __kmp_release_bootstrap_lock(&chain_lock);
1893 static float percent(kmp_uint32 count, kmp_uint32 total) {
1894 return (total == 0) ? 0.0 : (100.0 * count) / total;
1897 static FILE *__kmp_open_stats_file() {
1898 if (strcmp(__kmp_speculative_statsfile,
"-") == 0)
1901 size_t buffLen = KMP_STRLEN(__kmp_speculative_statsfile) + 20;
1902 char buffer[buffLen];
1903 KMP_SNPRINTF(&buffer[0], buffLen, __kmp_speculative_statsfile,
1904 (kmp_int32)getpid());
1905 FILE *result = fopen(&buffer[0],
"w");
1908 return result ? result : stdout;
1911 void __kmp_print_speculative_stats() {
1912 kmp_adaptive_lock_statistics_t total = destroyedStats;
1913 kmp_adaptive_lock_info_t *lck;
1915 for (lck = liveLocks.stats.next; lck != &liveLocks; lck = lck->stats.next) {
1916 __kmp_add_stats(&total, lck);
1918 kmp_adaptive_lock_statistics_t *t = &total;
1919 kmp_uint32 totalSections =
1920 t->nonSpeculativeAcquires + t->successfulSpeculations;
1921 kmp_uint32 totalSpeculations = t->successfulSpeculations +
1922 t->hardFailedSpeculations +
1923 t->softFailedSpeculations;
1924 if (totalSections <= 0)
1927 FILE *statsFile = __kmp_open_stats_file();
1929 fprintf(statsFile,
"Speculative lock statistics (all approximate!)\n");
1930 fprintf(statsFile,
" Lock parameters: \n" 1931 " max_soft_retries : %10d\n" 1932 " max_badness : %10d\n",
1933 __kmp_adaptive_backoff_params.max_soft_retries,
1934 __kmp_adaptive_backoff_params.max_badness);
1935 fprintf(statsFile,
" Non-speculative acquire attempts : %10d\n",
1936 t->nonSpeculativeAcquireAttempts);
1937 fprintf(statsFile,
" Total critical sections : %10d\n",
1939 fprintf(statsFile,
" Successful speculations : %10d (%5.1f%%)\n",
1940 t->successfulSpeculations,
1941 percent(t->successfulSpeculations, totalSections));
1942 fprintf(statsFile,
" Non-speculative acquires : %10d (%5.1f%%)\n",
1943 t->nonSpeculativeAcquires,
1944 percent(t->nonSpeculativeAcquires, totalSections));
1945 fprintf(statsFile,
" Lemming yields : %10d\n\n",
1948 fprintf(statsFile,
" Speculative acquire attempts : %10d\n",
1950 fprintf(statsFile,
" Successes : %10d (%5.1f%%)\n",
1951 t->successfulSpeculations,
1952 percent(t->successfulSpeculations, totalSpeculations));
1953 fprintf(statsFile,
" Soft failures : %10d (%5.1f%%)\n",
1954 t->softFailedSpeculations,
1955 percent(t->softFailedSpeculations, totalSpeculations));
1956 fprintf(statsFile,
" Hard failures : %10d (%5.1f%%)\n",
1957 t->hardFailedSpeculations,
1958 percent(t->hardFailedSpeculations, totalSpeculations));
1960 if (statsFile != stdout)
1964 #define KMP_INC_STAT(lck, stat) (lck->lk.adaptive.stats.stat++) 1966 #define KMP_INC_STAT(lck, stat) 1968 #endif // KMP_DEBUG_ADAPTIVE_LOCKS 1970 static inline bool __kmp_is_unlocked_queuing_lock(kmp_queuing_lock_t *lck) {
1973 bool res = lck->lk.head_id == 0;
1977 #if KMP_COMPILER_ICC 1980 __sync_synchronize();
1987 static __inline
void 1988 __kmp_update_badness_after_success(kmp_adaptive_lock_t *lck) {
1990 lck->lk.adaptive.badness = 0;
1991 KMP_INC_STAT(lck, successfulSpeculations);
1995 static __inline
void __kmp_step_badness(kmp_adaptive_lock_t *lck) {
1996 kmp_uint32 newBadness = (lck->lk.adaptive.badness << 1) | 1;
1997 if (newBadness > lck->lk.adaptive.max_badness) {
2000 lck->lk.adaptive.badness = newBadness;
2005 static __inline
int __kmp_should_speculate(kmp_adaptive_lock_t *lck,
2007 kmp_uint32 badness = lck->lk.adaptive.badness;
2008 kmp_uint32 attempts = lck->lk.adaptive.acquire_attempts;
2009 int res = (attempts & badness) == 0;
2015 static int __kmp_test_adaptive_lock_only(kmp_adaptive_lock_t *lck,
2017 int retries = lck->lk.adaptive.max_soft_retries;
2024 kmp_uint32 status = _xbegin();
2029 if (status == _XBEGIN_STARTED) {
2034 if (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2038 KMP_ASSERT2(0,
"should not get here");
2043 if (status & SOFT_ABORT_MASK) {
2044 KMP_INC_STAT(lck, softFailedSpeculations);
2047 KMP_INC_STAT(lck, hardFailedSpeculations);
2052 }
while (retries--);
2056 __kmp_step_badness(lck);
2063 static int __kmp_test_adaptive_lock(kmp_adaptive_lock_t *lck, kmp_int32 gtid) {
2065 if (__kmp_should_speculate(lck, gtid) &&
2066 __kmp_test_adaptive_lock_only(lck, gtid))
2071 lck->lk.adaptive.acquire_attempts++;
2074 if (__kmp_test_queuing_lock(GET_QLK_PTR(lck), gtid)) {
2075 KMP_INC_STAT(lck, nonSpeculativeAcquires);
2082 static int __kmp_test_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2084 char const *
const func =
"omp_test_lock";
2085 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2086 KMP_FATAL(LockIsUninitialized, func);
2089 int retval = __kmp_test_adaptive_lock(lck, gtid);
2092 lck->lk.qlk.owner_id = gtid + 1;
2108 static void __kmp_acquire_adaptive_lock(kmp_adaptive_lock_t *lck,
2110 if (__kmp_should_speculate(lck, gtid)) {
2111 if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2112 if (__kmp_test_adaptive_lock_only(lck, gtid))
2121 while (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
2122 KMP_INC_STAT(lck, lemmingYields);
2126 if (__kmp_test_adaptive_lock_only(lck, gtid))
2133 lck->lk.adaptive.acquire_attempts++;
2135 __kmp_acquire_queuing_lock_timed_template<FALSE>(GET_QLK_PTR(lck), gtid);
2137 KMP_INC_STAT(lck, nonSpeculativeAcquires);
2138 ANNOTATE_QUEUING_ACQUIRED(lck);
2141 static void __kmp_acquire_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2143 char const *
const func =
"omp_set_lock";
2144 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2145 KMP_FATAL(LockIsUninitialized, func);
2147 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == gtid) {
2148 KMP_FATAL(LockIsAlreadyOwned, func);
2151 __kmp_acquire_adaptive_lock(lck, gtid);
2153 lck->lk.qlk.owner_id = gtid + 1;
2156 static int __kmp_release_adaptive_lock(kmp_adaptive_lock_t *lck,
2158 if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(
2163 __kmp_update_badness_after_success(lck);
2166 __kmp_release_queuing_lock(GET_QLK_PTR(lck), gtid);
2168 return KMP_LOCK_RELEASED;
2171 static int __kmp_release_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
2173 char const *
const func =
"omp_unset_lock";
2175 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2176 KMP_FATAL(LockIsUninitialized, func);
2178 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == -1) {
2179 KMP_FATAL(LockUnsettingFree, func);
2181 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != gtid) {
2182 KMP_FATAL(LockUnsettingSetByAnother, func);
2184 lck->lk.qlk.owner_id = 0;
2185 __kmp_release_adaptive_lock(lck, gtid);
2186 return KMP_LOCK_RELEASED;
2189 static void __kmp_init_adaptive_lock(kmp_adaptive_lock_t *lck) {
2190 __kmp_init_queuing_lock(GET_QLK_PTR(lck));
2191 lck->lk.adaptive.badness = 0;
2192 lck->lk.adaptive.acquire_attempts = 0;
2193 lck->lk.adaptive.max_soft_retries =
2194 __kmp_adaptive_backoff_params.max_soft_retries;
2195 lck->lk.adaptive.max_badness = __kmp_adaptive_backoff_params.max_badness;
2196 #if KMP_DEBUG_ADAPTIVE_LOCKS 2197 __kmp_zero_speculative_stats(&lck->lk.adaptive);
2199 KA_TRACE(1000, (
"__kmp_init_adaptive_lock: lock %p initialized\n", lck));
2202 static void __kmp_destroy_adaptive_lock(kmp_adaptive_lock_t *lck) {
2203 #if KMP_DEBUG_ADAPTIVE_LOCKS 2204 __kmp_accumulate_speculative_stats(&lck->lk.adaptive);
2206 __kmp_destroy_queuing_lock(GET_QLK_PTR(lck));
2210 static void __kmp_destroy_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
2211 char const *
const func =
"omp_destroy_lock";
2212 if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
2213 KMP_FATAL(LockIsUninitialized, func);
2215 if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != -1) {
2216 KMP_FATAL(LockStillOwned, func);
2218 __kmp_destroy_adaptive_lock(lck);
2221 #endif // KMP_USE_ADAPTIVE_LOCKS 2227 static kmp_int32 __kmp_get_drdpa_lock_owner(kmp_drdpa_lock_t *lck) {
2228 return lck->lk.owner_id - 1;
2231 static inline bool __kmp_is_drdpa_lock_nestable(kmp_drdpa_lock_t *lck) {
2232 return lck->lk.depth_locked != -1;
2235 __forceinline
static int 2236 __kmp_acquire_drdpa_lock_timed_template(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2237 kmp_uint64 ticket = KMP_ATOMIC_INC(&lck->lk.next_ticket);
2238 kmp_uint64 mask = lck->lk.mask;
2239 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2241 #ifdef USE_LOCK_PROFILE 2242 if (polls[ticket & mask] != ticket)
2243 __kmp_printf(
"LOCK CONTENTION: %p\n", lck);
2255 KMP_FSYNC_PREPARE(lck);
2256 KMP_INIT_YIELD(spins);
2257 while (polls[ticket & mask] < ticket) {
2258 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
2266 mask = lck->lk.mask;
2267 polls = lck->lk.polls;
2271 KMP_FSYNC_ACQUIRED(lck);
2272 KA_TRACE(1000, (
"__kmp_acquire_drdpa_lock: ticket #%lld acquired lock %p\n",
2274 lck->lk.now_serving = ticket;
2281 if ((lck->lk.old_polls != NULL) && (ticket >= lck->lk.cleanup_ticket)) {
2282 __kmp_free(lck->lk.old_polls);
2283 lck->lk.old_polls = NULL;
2284 lck->lk.cleanup_ticket = 0;
2290 if (lck->lk.old_polls == NULL) {
2291 bool reconfigure =
false;
2292 std::atomic<kmp_uint64> *old_polls = polls;
2293 kmp_uint32 num_polls = TCR_4(lck->lk.num_polls);
2295 if (TCR_4(__kmp_nth) >
2296 (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) {
2299 if (num_polls > 1) {
2301 num_polls = TCR_4(lck->lk.num_polls);
2304 polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
2312 kmp_uint64 num_waiting = TCR_8(lck->lk.next_ticket) - ticket - 1;
2313 if (num_waiting > num_polls) {
2314 kmp_uint32 old_num_polls = num_polls;
2317 mask = (mask << 1) | 1;
2319 }
while (num_polls <= num_waiting);
2325 polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
2328 for (i = 0; i < old_num_polls; i++) {
2329 polls[i].store(old_polls[i]);
2344 KA_TRACE(1000, (
"__kmp_acquire_drdpa_lock: ticket #%lld reconfiguring " 2345 "lock %p to %d polls\n",
2346 ticket, lck, num_polls));
2348 lck->lk.old_polls = old_polls;
2349 lck->lk.polls = polls;
2353 lck->lk.num_polls = num_polls;
2354 lck->lk.mask = mask;
2362 lck->lk.cleanup_ticket = lck->lk.next_ticket;
2365 return KMP_LOCK_ACQUIRED_FIRST;
2368 int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2369 int retval = __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
2370 ANNOTATE_DRDPA_ACQUIRED(lck);
2374 static int __kmp_acquire_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2376 char const *
const func =
"omp_set_lock";
2377 if (lck->lk.initialized != lck) {
2378 KMP_FATAL(LockIsUninitialized, func);
2380 if (__kmp_is_drdpa_lock_nestable(lck)) {
2381 KMP_FATAL(LockNestableUsedAsSimple, func);
2383 if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) == gtid)) {
2384 KMP_FATAL(LockIsAlreadyOwned, func);
2387 __kmp_acquire_drdpa_lock(lck, gtid);
2389 lck->lk.owner_id = gtid + 1;
2390 return KMP_LOCK_ACQUIRED_FIRST;
2393 int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2396 kmp_uint64 ticket = lck->lk.next_ticket;
2397 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2398 kmp_uint64 mask = lck->lk.mask;
2399 if (polls[ticket & mask] == ticket) {
2400 kmp_uint64 next_ticket = ticket + 1;
2401 if (__kmp_atomic_compare_store_acq(&lck->lk.next_ticket, ticket,
2403 KMP_FSYNC_ACQUIRED(lck);
2404 KA_TRACE(1000, (
"__kmp_test_drdpa_lock: ticket #%lld acquired lock %p\n",
2406 lck->lk.now_serving = ticket;
2420 static int __kmp_test_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2422 char const *
const func =
"omp_test_lock";
2423 if (lck->lk.initialized != lck) {
2424 KMP_FATAL(LockIsUninitialized, func);
2426 if (__kmp_is_drdpa_lock_nestable(lck)) {
2427 KMP_FATAL(LockNestableUsedAsSimple, func);
2430 int retval = __kmp_test_drdpa_lock(lck, gtid);
2433 lck->lk.owner_id = gtid + 1;
2438 int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2441 kmp_uint64 ticket = lck->lk.now_serving + 1;
2442 std::atomic<kmp_uint64> *polls = lck->lk.polls;
2443 kmp_uint64 mask = lck->lk.mask;
2444 KA_TRACE(1000, (
"__kmp_release_drdpa_lock: ticket #%lld released lock %p\n",
2446 KMP_FSYNC_RELEASING(lck);
2447 ANNOTATE_DRDPA_RELEASED(lck);
2448 polls[ticket & mask] = ticket;
2449 return KMP_LOCK_RELEASED;
2452 static int __kmp_release_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2454 char const *
const func =
"omp_unset_lock";
2456 if (lck->lk.initialized != lck) {
2457 KMP_FATAL(LockIsUninitialized, func);
2459 if (__kmp_is_drdpa_lock_nestable(lck)) {
2460 KMP_FATAL(LockNestableUsedAsSimple, func);
2462 if (__kmp_get_drdpa_lock_owner(lck) == -1) {
2463 KMP_FATAL(LockUnsettingFree, func);
2465 if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) >= 0) &&
2466 (__kmp_get_drdpa_lock_owner(lck) != gtid)) {
2467 KMP_FATAL(LockUnsettingSetByAnother, func);
2469 lck->lk.owner_id = 0;
2470 return __kmp_release_drdpa_lock(lck, gtid);
2473 void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck) {
2474 lck->lk.location = NULL;
2476 lck->lk.num_polls = 1;
2477 lck->lk.polls = (std::atomic<kmp_uint64> *)__kmp_allocate(
2478 lck->lk.num_polls *
sizeof(*(lck->lk.polls)));
2479 lck->lk.cleanup_ticket = 0;
2480 lck->lk.old_polls = NULL;
2481 lck->lk.next_ticket = 0;
2482 lck->lk.now_serving = 0;
2483 lck->lk.owner_id = 0;
2484 lck->lk.depth_locked = -1;
2485 lck->lk.initialized = lck;
2487 KA_TRACE(1000, (
"__kmp_init_drdpa_lock: lock %p initialized\n", lck));
2490 void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck) {
2491 lck->lk.initialized = NULL;
2492 lck->lk.location = NULL;
2493 if (lck->lk.polls.load() != NULL) {
2494 __kmp_free(lck->lk.polls.load());
2495 lck->lk.polls = NULL;
2497 if (lck->lk.old_polls != NULL) {
2498 __kmp_free(lck->lk.old_polls);
2499 lck->lk.old_polls = NULL;
2502 lck->lk.num_polls = 0;
2503 lck->lk.cleanup_ticket = 0;
2504 lck->lk.next_ticket = 0;
2505 lck->lk.now_serving = 0;
2506 lck->lk.owner_id = 0;
2507 lck->lk.depth_locked = -1;
2510 static void __kmp_destroy_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
2511 char const *
const func =
"omp_destroy_lock";
2512 if (lck->lk.initialized != lck) {
2513 KMP_FATAL(LockIsUninitialized, func);
2515 if (__kmp_is_drdpa_lock_nestable(lck)) {
2516 KMP_FATAL(LockNestableUsedAsSimple, func);
2518 if (__kmp_get_drdpa_lock_owner(lck) != -1) {
2519 KMP_FATAL(LockStillOwned, func);
2521 __kmp_destroy_drdpa_lock(lck);
2526 int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2527 KMP_DEBUG_ASSERT(gtid >= 0);
2529 if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
2530 lck->lk.depth_locked += 1;
2531 return KMP_LOCK_ACQUIRED_NEXT;
2533 __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
2534 ANNOTATE_DRDPA_ACQUIRED(lck);
2536 lck->lk.depth_locked = 1;
2538 lck->lk.owner_id = gtid + 1;
2539 return KMP_LOCK_ACQUIRED_FIRST;
2543 static void __kmp_acquire_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2545 char const *
const func =
"omp_set_nest_lock";
2546 if (lck->lk.initialized != lck) {
2547 KMP_FATAL(LockIsUninitialized, func);
2549 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2550 KMP_FATAL(LockSimpleUsedAsNestable, func);
2552 __kmp_acquire_nested_drdpa_lock(lck, gtid);
2555 int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2558 KMP_DEBUG_ASSERT(gtid >= 0);
2560 if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
2561 retval = ++lck->lk.depth_locked;
2562 }
else if (!__kmp_test_drdpa_lock(lck, gtid)) {
2566 retval = lck->lk.depth_locked = 1;
2568 lck->lk.owner_id = gtid + 1;
2573 static int __kmp_test_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2575 char const *
const func =
"omp_test_nest_lock";
2576 if (lck->lk.initialized != lck) {
2577 KMP_FATAL(LockIsUninitialized, func);
2579 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2580 KMP_FATAL(LockSimpleUsedAsNestable, func);
2582 return __kmp_test_nested_drdpa_lock(lck, gtid);
2585 int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
2586 KMP_DEBUG_ASSERT(gtid >= 0);
2589 if (--(lck->lk.depth_locked) == 0) {
2591 lck->lk.owner_id = 0;
2592 __kmp_release_drdpa_lock(lck, gtid);
2593 return KMP_LOCK_RELEASED;
2595 return KMP_LOCK_STILL_HELD;
2598 static int __kmp_release_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
2600 char const *
const func =
"omp_unset_nest_lock";
2602 if (lck->lk.initialized != lck) {
2603 KMP_FATAL(LockIsUninitialized, func);
2605 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2606 KMP_FATAL(LockSimpleUsedAsNestable, func);
2608 if (__kmp_get_drdpa_lock_owner(lck) == -1) {
2609 KMP_FATAL(LockUnsettingFree, func);
2611 if (__kmp_get_drdpa_lock_owner(lck) != gtid) {
2612 KMP_FATAL(LockUnsettingSetByAnother, func);
2614 return __kmp_release_nested_drdpa_lock(lck, gtid);
2617 void __kmp_init_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
2618 __kmp_init_drdpa_lock(lck);
2619 lck->lk.depth_locked = 0;
2622 void __kmp_destroy_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
2623 __kmp_destroy_drdpa_lock(lck);
2624 lck->lk.depth_locked = 0;
2627 static void __kmp_destroy_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
2628 char const *
const func =
"omp_destroy_nest_lock";
2629 if (lck->lk.initialized != lck) {
2630 KMP_FATAL(LockIsUninitialized, func);
2632 if (!__kmp_is_drdpa_lock_nestable(lck)) {
2633 KMP_FATAL(LockSimpleUsedAsNestable, func);
2635 if (__kmp_get_drdpa_lock_owner(lck) != -1) {
2636 KMP_FATAL(LockStillOwned, func);
2638 __kmp_destroy_nested_drdpa_lock(lck);
2643 static const ident_t *__kmp_get_drdpa_lock_location(kmp_drdpa_lock_t *lck) {
2644 return lck->lk.location;
2647 static void __kmp_set_drdpa_lock_location(kmp_drdpa_lock_t *lck,
2649 lck->lk.location = loc;
2652 static kmp_lock_flags_t __kmp_get_drdpa_lock_flags(kmp_drdpa_lock_t *lck) {
2653 return lck->lk.flags;
2656 static void __kmp_set_drdpa_lock_flags(kmp_drdpa_lock_t *lck,
2657 kmp_lock_flags_t flags) {
2658 lck->lk.flags = flags;
2662 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 2663 #define __kmp_tsc() __kmp_hardware_timestamp() 2665 kmp_backoff_t __kmp_spin_backoff_params = {1, 4096, 100};
2668 extern kmp_uint64 __kmp_now_nsec();
2669 kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
2670 #define __kmp_tsc() __kmp_now_nsec() 2680 static inline bool before(kmp_uint64 a, kmp_uint64 b) {
2681 return ((kmp_int64)b - (kmp_int64)a) > 0;
2685 void __kmp_spin_backoff(kmp_backoff_t *boff) {
2688 for (i = boff->step; i > 0; i--) {
2689 kmp_uint64 goal = __kmp_tsc() + boff->min_tick;
2692 }
while (before(__kmp_tsc(), goal));
2694 boff->step = (boff->step << 1 | 1) & (boff->max_backoff - 1);
2697 #if KMP_USE_DYNAMIC_LOCK 2701 static void __kmp_init_direct_lock(kmp_dyna_lock_t *lck,
2702 kmp_dyna_lockseq_t seq) {
2703 TCW_4(*lck, KMP_GET_D_TAG(seq));
2706 (
"__kmp_init_direct_lock: initialized direct lock with type#%d\n", seq));
2712 #define HLE_ACQUIRE ".byte 0xf2;" 2713 #define HLE_RELEASE ".byte 0xf3;" 2715 static inline kmp_uint32 swap4(kmp_uint32
volatile *p, kmp_uint32 v) {
2716 __asm__
volatile(HLE_ACQUIRE
"xchg %1,%0" :
"+r"(v),
"+m"(*p) : :
"memory");
2720 static void __kmp_destroy_hle_lock(kmp_dyna_lock_t *lck) { TCW_4(*lck, 0); }
2722 static void __kmp_destroy_hle_lock_with_checks(kmp_dyna_lock_t *lck) {
2726 static void __kmp_acquire_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
2728 if (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle)) {
2731 while (*(kmp_uint32
volatile *)lck != KMP_LOCK_FREE(hle)) {
2732 for (
int i = delay; i != 0; --i)
2734 delay = ((delay << 1) | 1) & 7;
2736 }
while (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle));
2740 static void __kmp_acquire_hle_lock_with_checks(kmp_dyna_lock_t *lck,
2742 __kmp_acquire_hle_lock(lck, gtid);
2745 static int __kmp_release_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
2746 __asm__
volatile(HLE_RELEASE
"movl %1,%0" 2748 :
"r"(KMP_LOCK_FREE(hle))
2750 return KMP_LOCK_RELEASED;
2753 static int __kmp_release_hle_lock_with_checks(kmp_dyna_lock_t *lck,
2755 return __kmp_release_hle_lock(lck, gtid);
2758 static int __kmp_test_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
2759 return swap4(lck, KMP_LOCK_BUSY(1, hle)) == KMP_LOCK_FREE(hle);
2762 static int __kmp_test_hle_lock_with_checks(kmp_dyna_lock_t *lck,
2764 return __kmp_test_hle_lock(lck, gtid);
2767 static void __kmp_init_rtm_lock(kmp_queuing_lock_t *lck) {
2768 __kmp_init_queuing_lock(lck);
2771 static void __kmp_destroy_rtm_lock(kmp_queuing_lock_t *lck) {
2772 __kmp_destroy_queuing_lock(lck);
2775 static void __kmp_destroy_rtm_lock_with_checks(kmp_queuing_lock_t *lck) {
2776 __kmp_destroy_queuing_lock_with_checks(lck);
2779 static void __kmp_acquire_rtm_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
2780 unsigned retries = 3, status;
2783 if (status == _XBEGIN_STARTED) {
2784 if (__kmp_is_unlocked_queuing_lock(lck))
2788 if ((status & _XABORT_EXPLICIT) && _XABORT_CODE(status) == 0xff) {
2790 while (!__kmp_is_unlocked_queuing_lock(lck)) {
2793 }
else if (!(status & _XABORT_RETRY))
2795 }
while (retries--);
2798 __kmp_acquire_queuing_lock(lck, gtid);
2801 static void __kmp_acquire_rtm_lock_with_checks(kmp_queuing_lock_t *lck,
2803 __kmp_acquire_rtm_lock(lck, gtid);
2806 static int __kmp_release_rtm_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
2807 if (__kmp_is_unlocked_queuing_lock(lck)) {
2812 __kmp_release_queuing_lock(lck, gtid);
2814 return KMP_LOCK_RELEASED;
2817 static int __kmp_release_rtm_lock_with_checks(kmp_queuing_lock_t *lck,
2819 return __kmp_release_rtm_lock(lck, gtid);
2822 static int __kmp_test_rtm_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
2823 unsigned retries = 3, status;
2826 if (status == _XBEGIN_STARTED && __kmp_is_unlocked_queuing_lock(lck)) {
2829 if (!(status & _XABORT_RETRY))
2831 }
while (retries--);
2833 return (__kmp_is_unlocked_queuing_lock(lck)) ? 1 : 0;
2836 static int __kmp_test_rtm_lock_with_checks(kmp_queuing_lock_t *lck,
2838 return __kmp_test_rtm_lock(lck, gtid);
2841 #endif // KMP_USE_TSX 2844 static void __kmp_init_indirect_lock(kmp_dyna_lock_t *l,
2845 kmp_dyna_lockseq_t tag);
2846 static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock);
2847 static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
2848 static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
2849 static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
2850 static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
2852 static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
2854 static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
2858 #define KMP_FOREACH_LOCK_KIND(m, a) m(ticket, a) m(queuing, a) m(drdpa, a) 2860 #define expand1(lk, op) \ 2861 static void __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock) { \ 2862 __kmp_##op##_##lk##_##lock(&lock->lk); \ 2864 #define expand2(lk, op) \ 2865 static int __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock, \ 2867 return __kmp_##op##_##lk##_##lock(&lock->lk, gtid); \ 2869 #define expand3(lk, op) \ 2870 static void __kmp_set_##lk##_##lock_flags(kmp_user_lock_p lock, \ 2871 kmp_lock_flags_t flags) { \ 2872 __kmp_set_##lk##_lock_flags(&lock->lk, flags); \ 2874 #define expand4(lk, op) \ 2875 static void __kmp_set_##lk##_##lock_location(kmp_user_lock_p lock, \ 2876 const ident_t *loc) { \ 2877 __kmp_set_##lk##_lock_location(&lock->lk, loc); \ 2880 KMP_FOREACH_LOCK_KIND(expand1, init)
2881 KMP_FOREACH_LOCK_KIND(expand1, init_nested)
2882 KMP_FOREACH_LOCK_KIND(expand1, destroy)
2883 KMP_FOREACH_LOCK_KIND(expand1, destroy_nested)
2884 KMP_FOREACH_LOCK_KIND(expand2, acquire)
2885 KMP_FOREACH_LOCK_KIND(expand2, acquire_nested)
2886 KMP_FOREACH_LOCK_KIND(expand2, release)
2887 KMP_FOREACH_LOCK_KIND(expand2, release_nested)
2888 KMP_FOREACH_LOCK_KIND(expand2, test)
2889 KMP_FOREACH_LOCK_KIND(expand2, test_nested)
2890 KMP_FOREACH_LOCK_KIND(expand3, )
2891 KMP_FOREACH_LOCK_KIND(expand4, )
2902 #define expand(l, op) 0, __kmp_init_direct_lock, 2903 void (*__kmp_direct_init[])(kmp_dyna_lock_t *, kmp_dyna_lockseq_t) = {
2904 __kmp_init_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, init)};
2908 #define expand(l, op) 0, (void (*)(kmp_dyna_lock_t *))__kmp_##op##_##l##_lock, 2909 static void (*direct_destroy[])(kmp_dyna_lock_t *) = {
2910 __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
2912 #define expand(l, op) \ 2913 0, (void (*)(kmp_dyna_lock_t *))__kmp_destroy_##l##_lock_with_checks, 2914 static void (*direct_destroy_check[])(kmp_dyna_lock_t *) = {
2915 __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
2919 #define expand(l, op) \ 2920 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock, 2921 static int (*direct_set[])(kmp_dyna_lock_t *, kmp_int32) = {
2922 __kmp_set_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, acquire)};
2924 #define expand(l, op) \ 2925 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks, 2926 static int (*direct_set_check[])(kmp_dyna_lock_t *, kmp_int32) = {
2927 __kmp_set_indirect_lock_with_checks, 0,
2928 KMP_FOREACH_D_LOCK(expand, acquire)};
2932 #define expand(l, op) \ 2933 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock, 2934 static int (*direct_unset[])(kmp_dyna_lock_t *, kmp_int32) = {
2935 __kmp_unset_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, release)};
2936 static int (*direct_test[])(kmp_dyna_lock_t *, kmp_int32) = {
2937 __kmp_test_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, test)};
2939 #define expand(l, op) \ 2940 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks, 2941 static int (*direct_unset_check[])(kmp_dyna_lock_t *, kmp_int32) = {
2942 __kmp_unset_indirect_lock_with_checks, 0,
2943 KMP_FOREACH_D_LOCK(expand, release)};
2944 static int (*direct_test_check[])(kmp_dyna_lock_t *, kmp_int32) = {
2945 __kmp_test_indirect_lock_with_checks, 0, KMP_FOREACH_D_LOCK(expand, test)};
2949 void (**__kmp_direct_destroy)(kmp_dyna_lock_t *) = 0;
2950 int (**__kmp_direct_set)(kmp_dyna_lock_t *, kmp_int32) = 0;
2951 int (**__kmp_direct_unset)(kmp_dyna_lock_t *, kmp_int32) = 0;
2952 int (**__kmp_direct_test)(kmp_dyna_lock_t *, kmp_int32) = 0;
2955 #define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock, 2956 void (*__kmp_indirect_init[])(kmp_user_lock_p) = {
2957 KMP_FOREACH_I_LOCK(expand, init)};
2960 #define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock, 2961 static void (*indirect_destroy[])(kmp_user_lock_p) = {
2962 KMP_FOREACH_I_LOCK(expand, destroy)};
2964 #define expand(l, op) \ 2965 (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock_with_checks, 2966 static void (*indirect_destroy_check[])(kmp_user_lock_p) = {
2967 KMP_FOREACH_I_LOCK(expand, destroy)};
2971 #define expand(l, op) \ 2972 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock, 2973 static int (*indirect_set[])(kmp_user_lock_p,
2974 kmp_int32) = {KMP_FOREACH_I_LOCK(expand, acquire)};
2976 #define expand(l, op) \ 2977 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks, 2978 static int (*indirect_set_check[])(kmp_user_lock_p, kmp_int32) = {
2979 KMP_FOREACH_I_LOCK(expand, acquire)};
2983 #define expand(l, op) \ 2984 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock, 2985 static int (*indirect_unset[])(kmp_user_lock_p, kmp_int32) = {
2986 KMP_FOREACH_I_LOCK(expand, release)};
2987 static int (*indirect_test[])(kmp_user_lock_p,
2988 kmp_int32) = {KMP_FOREACH_I_LOCK(expand, test)};
2990 #define expand(l, op) \ 2991 (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks, 2992 static int (*indirect_unset_check[])(kmp_user_lock_p, kmp_int32) = {
2993 KMP_FOREACH_I_LOCK(expand, release)};
2994 static int (*indirect_test_check[])(kmp_user_lock_p, kmp_int32) = {
2995 KMP_FOREACH_I_LOCK(expand, test)};
2999 void (**__kmp_indirect_destroy)(kmp_user_lock_p) = 0;
3000 int (**__kmp_indirect_set)(kmp_user_lock_p, kmp_int32) = 0;
3001 int (**__kmp_indirect_unset)(kmp_user_lock_p, kmp_int32) = 0;
3002 int (**__kmp_indirect_test)(kmp_user_lock_p, kmp_int32) = 0;
3005 kmp_indirect_lock_table_t __kmp_i_lock_table;
3008 static kmp_uint32 __kmp_indirect_lock_size[KMP_NUM_I_LOCKS] = {0};
3011 void (*__kmp_indirect_set_location[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
3013 void (*__kmp_indirect_set_flags[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
3014 kmp_lock_flags_t) = {0};
3015 const ident_t *(*__kmp_indirect_get_location[KMP_NUM_I_LOCKS])(
3016 kmp_user_lock_p) = {0};
3017 kmp_lock_flags_t (*__kmp_indirect_get_flags[KMP_NUM_I_LOCKS])(
3018 kmp_user_lock_p) = {0};
3021 static kmp_indirect_lock_t *__kmp_indirect_lock_pool[KMP_NUM_I_LOCKS] = {0};
3028 kmp_indirect_lock_t *__kmp_allocate_indirect_lock(
void **user_lock,
3030 kmp_indirect_locktag_t tag) {
3031 kmp_indirect_lock_t *lck;
3032 kmp_lock_index_t idx;
3034 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3036 if (__kmp_indirect_lock_pool[tag] != NULL) {
3038 lck = __kmp_indirect_lock_pool[tag];
3039 if (OMP_LOCK_T_SIZE <
sizeof(
void *))
3040 idx = lck->lock->pool.index;
3041 __kmp_indirect_lock_pool[tag] = (kmp_indirect_lock_t *)lck->lock->pool.next;
3042 KA_TRACE(20, (
"__kmp_allocate_indirect_lock: reusing an existing lock %p\n",
3045 idx = __kmp_i_lock_table.next;
3047 if (idx == __kmp_i_lock_table.size) {
3049 int row = __kmp_i_lock_table.size / KMP_I_LOCK_CHUNK;
3050 kmp_indirect_lock_t **new_table = (kmp_indirect_lock_t **)__kmp_allocate(
3051 2 * row *
sizeof(kmp_indirect_lock_t *));
3052 KMP_MEMCPY(new_table, __kmp_i_lock_table.table,
3053 row *
sizeof(kmp_indirect_lock_t *));
3054 kmp_indirect_lock_t **old_table = __kmp_i_lock_table.table;
3055 __kmp_i_lock_table.table = new_table;
3056 __kmp_free(old_table);
3058 for (
int i = row; i < 2 * row; ++i)
3059 *(__kmp_i_lock_table.table + i) = (kmp_indirect_lock_t *)__kmp_allocate(
3060 KMP_I_LOCK_CHUNK *
sizeof(kmp_indirect_lock_t));
3061 __kmp_i_lock_table.size = 2 * idx;
3063 __kmp_i_lock_table.next++;
3064 lck = KMP_GET_I_LOCK(idx);
3066 lck->lock = (kmp_user_lock_p)__kmp_allocate(__kmp_indirect_lock_size[tag]);
3068 (
"__kmp_allocate_indirect_lock: allocated a new lock %p\n", lck));
3071 __kmp_release_lock(&__kmp_global_lock, gtid);
3075 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3076 *((kmp_lock_index_t *)user_lock) = idx
3079 *((kmp_indirect_lock_t **)user_lock) = lck;
3086 static __forceinline kmp_indirect_lock_t *
3087 __kmp_lookup_indirect_lock(
void **user_lock,
const char *func) {
3088 if (__kmp_env_consistency_check) {
3089 kmp_indirect_lock_t *lck = NULL;
3090 if (user_lock == NULL) {
3091 KMP_FATAL(LockIsUninitialized, func);
3093 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3094 kmp_lock_index_t idx = KMP_EXTRACT_I_INDEX(user_lock);
3095 if (idx >= __kmp_i_lock_table.size) {
3096 KMP_FATAL(LockIsUninitialized, func);
3098 lck = KMP_GET_I_LOCK(idx);
3100 lck = *((kmp_indirect_lock_t **)user_lock);
3103 KMP_FATAL(LockIsUninitialized, func);
3107 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3108 return KMP_GET_I_LOCK(KMP_EXTRACT_I_INDEX(user_lock));
3110 return *((kmp_indirect_lock_t **)user_lock);
3115 static void __kmp_init_indirect_lock(kmp_dyna_lock_t *lock,
3116 kmp_dyna_lockseq_t seq) {
3117 #if KMP_USE_ADAPTIVE_LOCKS 3118 if (seq == lockseq_adaptive && !__kmp_cpuinfo.rtm) {
3119 KMP_WARNING(AdaptiveNotSupported,
"kmp_lockseq_t",
"adaptive");
3120 seq = lockseq_queuing;
3124 if (seq == lockseq_rtm && !__kmp_cpuinfo.rtm) {
3125 seq = lockseq_queuing;
3128 kmp_indirect_locktag_t tag = KMP_GET_I_TAG(seq);
3129 kmp_indirect_lock_t *l =
3130 __kmp_allocate_indirect_lock((
void **)lock, __kmp_entry_gtid(), tag);
3131 KMP_I_LOCK_FUNC(l, init)(l->lock);
3133 20, (
"__kmp_init_indirect_lock: initialized indirect lock with type#%d\n",
3137 static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock) {
3138 kmp_uint32 gtid = __kmp_entry_gtid();
3139 kmp_indirect_lock_t *l =
3140 __kmp_lookup_indirect_lock((
void **)lock,
"omp_destroy_lock");
3141 KMP_I_LOCK_FUNC(l, destroy)(l->lock);
3142 kmp_indirect_locktag_t tag = l->type;
3144 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3147 l->lock->pool.next = (kmp_user_lock_p)__kmp_indirect_lock_pool[tag];
3148 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3149 l->lock->pool.index = KMP_EXTRACT_I_INDEX(lock);
3151 __kmp_indirect_lock_pool[tag] = l;
3153 __kmp_release_lock(&__kmp_global_lock, gtid);
3156 static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
3157 kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
3158 return KMP_I_LOCK_FUNC(l,
set)(l->lock, gtid);
3161 static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
3162 kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
3163 return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
3166 static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
3167 kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
3168 return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
3171 static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
3173 kmp_indirect_lock_t *l =
3174 __kmp_lookup_indirect_lock((
void **)lock,
"omp_set_lock");
3175 return KMP_I_LOCK_FUNC(l,
set)(l->lock, gtid);
3178 static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
3180 kmp_indirect_lock_t *l =
3181 __kmp_lookup_indirect_lock((
void **)lock,
"omp_unset_lock");
3182 return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
3185 static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
3187 kmp_indirect_lock_t *l =
3188 __kmp_lookup_indirect_lock((
void **)lock,
"omp_test_lock");
3189 return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
3192 kmp_dyna_lockseq_t __kmp_user_lock_seq = lockseq_queuing;
3195 kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p lck, kmp_uint32 seq) {
3198 case lockseq_nested_tas:
3199 return __kmp_get_tas_lock_owner((kmp_tas_lock_t *)lck);
3202 case lockseq_nested_futex:
3203 return __kmp_get_futex_lock_owner((kmp_futex_lock_t *)lck);
3205 case lockseq_ticket:
3206 case lockseq_nested_ticket:
3207 return __kmp_get_ticket_lock_owner((kmp_ticket_lock_t *)lck);
3208 case lockseq_queuing:
3209 case lockseq_nested_queuing:
3210 #if KMP_USE_ADAPTIVE_LOCKS 3211 case lockseq_adaptive:
3213 return __kmp_get_queuing_lock_owner((kmp_queuing_lock_t *)lck);
3215 case lockseq_nested_drdpa:
3216 return __kmp_get_drdpa_lock_owner((kmp_drdpa_lock_t *)lck);
3223 void __kmp_init_dynamic_user_locks() {
3225 if (__kmp_env_consistency_check) {
3226 __kmp_direct_set = direct_set_check;
3227 __kmp_direct_unset = direct_unset_check;
3228 __kmp_direct_test = direct_test_check;
3229 __kmp_direct_destroy = direct_destroy_check;
3230 __kmp_indirect_set = indirect_set_check;
3231 __kmp_indirect_unset = indirect_unset_check;
3232 __kmp_indirect_test = indirect_test_check;
3233 __kmp_indirect_destroy = indirect_destroy_check;
3235 __kmp_direct_set = direct_set;
3236 __kmp_direct_unset = direct_unset;
3237 __kmp_direct_test = direct_test;
3238 __kmp_direct_destroy = direct_destroy;
3239 __kmp_indirect_set = indirect_set;
3240 __kmp_indirect_unset = indirect_unset;
3241 __kmp_indirect_test = indirect_test;
3242 __kmp_indirect_destroy = indirect_destroy;
3247 if (__kmp_init_user_locks)
3251 __kmp_i_lock_table.size = KMP_I_LOCK_CHUNK;
3252 __kmp_i_lock_table.table =
3253 (kmp_indirect_lock_t **)__kmp_allocate(
sizeof(kmp_indirect_lock_t *));
3254 *(__kmp_i_lock_table.table) = (kmp_indirect_lock_t *)__kmp_allocate(
3255 KMP_I_LOCK_CHUNK *
sizeof(kmp_indirect_lock_t));
3256 __kmp_i_lock_table.next = 0;
3259 __kmp_indirect_lock_size[locktag_ticket] =
sizeof(kmp_ticket_lock_t);
3260 __kmp_indirect_lock_size[locktag_queuing] =
sizeof(kmp_queuing_lock_t);
3261 #if KMP_USE_ADAPTIVE_LOCKS 3262 __kmp_indirect_lock_size[locktag_adaptive] =
sizeof(kmp_adaptive_lock_t);
3264 __kmp_indirect_lock_size[locktag_drdpa] =
sizeof(kmp_drdpa_lock_t);
3266 __kmp_indirect_lock_size[locktag_rtm] =
sizeof(kmp_queuing_lock_t);
3268 __kmp_indirect_lock_size[locktag_nested_tas] =
sizeof(kmp_tas_lock_t);
3270 __kmp_indirect_lock_size[locktag_nested_futex] =
sizeof(kmp_futex_lock_t);
3272 __kmp_indirect_lock_size[locktag_nested_ticket] =
sizeof(kmp_ticket_lock_t);
3273 __kmp_indirect_lock_size[locktag_nested_queuing] =
sizeof(kmp_queuing_lock_t);
3274 __kmp_indirect_lock_size[locktag_nested_drdpa] =
sizeof(kmp_drdpa_lock_t);
3277 #define fill_jumps(table, expand, sep) \ 3279 table[locktag##sep##ticket] = expand(ticket); \ 3280 table[locktag##sep##queuing] = expand(queuing); \ 3281 table[locktag##sep##drdpa] = expand(drdpa); \ 3284 #if KMP_USE_ADAPTIVE_LOCKS 3285 #define fill_table(table, expand) \ 3287 fill_jumps(table, expand, _); \ 3288 table[locktag_adaptive] = expand(queuing); \ 3289 fill_jumps(table, expand, _nested_); \ 3292 #define fill_table(table, expand) \ 3294 fill_jumps(table, expand, _); \ 3295 fill_jumps(table, expand, _nested_); \ 3297 #endif // KMP_USE_ADAPTIVE_LOCKS 3300 (void (*)(kmp_user_lock_p, const ident_t *)) __kmp_set_##l##_lock_location 3301 fill_table(__kmp_indirect_set_location, expand);
3304 (void (*)(kmp_user_lock_p, kmp_lock_flags_t)) __kmp_set_##l##_lock_flags 3305 fill_table(__kmp_indirect_set_flags, expand);
3308 (const ident_t *(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_location 3309 fill_table(__kmp_indirect_get_location, expand);
3312 (kmp_lock_flags_t(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_flags 3313 fill_table(__kmp_indirect_get_flags, expand);
3316 __kmp_init_user_locks = TRUE;
3320 void __kmp_cleanup_indirect_user_locks() {
3326 for (k = 0; k < KMP_NUM_I_LOCKS; ++k) {
3327 kmp_indirect_lock_t *l = __kmp_indirect_lock_pool[k];
3329 kmp_indirect_lock_t *ll = l;
3330 l = (kmp_indirect_lock_t *)l->lock->pool.next;
3331 KA_TRACE(20, (
"__kmp_cleanup_indirect_user_locks: freeing %p from pool\n",
3333 __kmp_free(ll->lock);
3336 __kmp_indirect_lock_pool[k] = NULL;
3339 for (i = 0; i < __kmp_i_lock_table.next; i++) {
3340 kmp_indirect_lock_t *l = KMP_GET_I_LOCK(i);
3341 if (l->lock != NULL) {
3343 KMP_I_LOCK_FUNC(l, destroy)(l->lock);
3346 (
"__kmp_cleanup_indirect_user_locks: destroy/freeing %p from table\n",
3348 __kmp_free(l->lock);
3352 for (i = 0; i < __kmp_i_lock_table.size / KMP_I_LOCK_CHUNK; i++)
3353 __kmp_free(__kmp_i_lock_table.table[i]);
3354 __kmp_free(__kmp_i_lock_table.table);
3356 __kmp_init_user_locks = FALSE;
3359 enum kmp_lock_kind __kmp_user_lock_kind = lk_default;
3360 int __kmp_num_locks_in_block = 1;
3362 #else // KMP_USE_DYNAMIC_LOCK 3364 static void __kmp_init_tas_lock_with_checks(kmp_tas_lock_t *lck) {
3365 __kmp_init_tas_lock(lck);
3368 static void __kmp_init_nested_tas_lock_with_checks(kmp_tas_lock_t *lck) {
3369 __kmp_init_nested_tas_lock(lck);
3373 static void __kmp_init_futex_lock_with_checks(kmp_futex_lock_t *lck) {
3374 __kmp_init_futex_lock(lck);
3377 static void __kmp_init_nested_futex_lock_with_checks(kmp_futex_lock_t *lck) {
3378 __kmp_init_nested_futex_lock(lck);
3382 static int __kmp_is_ticket_lock_initialized(kmp_ticket_lock_t *lck) {
3383 return lck == lck->lk.self;
3386 static void __kmp_init_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
3387 __kmp_init_ticket_lock(lck);
3390 static void __kmp_init_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
3391 __kmp_init_nested_ticket_lock(lck);
3394 static int __kmp_is_queuing_lock_initialized(kmp_queuing_lock_t *lck) {
3395 return lck == lck->lk.initialized;
3398 static void __kmp_init_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
3399 __kmp_init_queuing_lock(lck);
3403 __kmp_init_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
3404 __kmp_init_nested_queuing_lock(lck);
3407 #if KMP_USE_ADAPTIVE_LOCKS 3408 static void __kmp_init_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
3409 __kmp_init_adaptive_lock(lck);
3413 static int __kmp_is_drdpa_lock_initialized(kmp_drdpa_lock_t *lck) {
3414 return lck == lck->lk.initialized;
3417 static void __kmp_init_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
3418 __kmp_init_drdpa_lock(lck);
3421 static void __kmp_init_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
3422 __kmp_init_nested_drdpa_lock(lck);
3429 enum kmp_lock_kind __kmp_user_lock_kind = lk_default;
3431 size_t __kmp_base_user_lock_size = 0;
3432 size_t __kmp_user_lock_size = 0;
3434 kmp_int32 (*__kmp_get_user_lock_owner_)(kmp_user_lock_p lck) = NULL;
3435 int (*__kmp_acquire_user_lock_with_checks_)(kmp_user_lock_p lck,
3436 kmp_int32 gtid) = NULL;
3438 int (*__kmp_test_user_lock_with_checks_)(kmp_user_lock_p lck,
3439 kmp_int32 gtid) = NULL;
3440 int (*__kmp_release_user_lock_with_checks_)(kmp_user_lock_p lck,
3441 kmp_int32 gtid) = NULL;
3442 void (*__kmp_init_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3443 void (*__kmp_destroy_user_lock_)(kmp_user_lock_p lck) = NULL;
3444 void (*__kmp_destroy_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3445 int (*__kmp_acquire_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
3446 kmp_int32 gtid) = NULL;
3448 int (*__kmp_test_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
3449 kmp_int32 gtid) = NULL;
3450 int (*__kmp_release_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
3451 kmp_int32 gtid) = NULL;
3452 void (*__kmp_init_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3453 void (*__kmp_destroy_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
3455 int (*__kmp_is_user_lock_initialized_)(kmp_user_lock_p lck) = NULL;
3456 const ident_t *(*__kmp_get_user_lock_location_)(kmp_user_lock_p lck) = NULL;
3457 void (*__kmp_set_user_lock_location_)(kmp_user_lock_p lck,
3459 kmp_lock_flags_t (*__kmp_get_user_lock_flags_)(kmp_user_lock_p lck) = NULL;
3460 void (*__kmp_set_user_lock_flags_)(kmp_user_lock_p lck,
3461 kmp_lock_flags_t flags) = NULL;
3463 void __kmp_set_user_lock_vptrs(kmp_lock_kind_t user_lock_kind) {
3464 switch (user_lock_kind) {
3470 __kmp_base_user_lock_size =
sizeof(kmp_base_tas_lock_t);
3471 __kmp_user_lock_size =
sizeof(kmp_tas_lock_t);
3473 __kmp_get_user_lock_owner_ =
3474 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_tas_lock_owner);
3476 if (__kmp_env_consistency_check) {
3477 KMP_BIND_USER_LOCK_WITH_CHECKS(tas);
3478 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(tas);
3480 KMP_BIND_USER_LOCK(tas);
3481 KMP_BIND_NESTED_USER_LOCK(tas);
3484 __kmp_destroy_user_lock_ =
3485 (void (*)(kmp_user_lock_p))(&__kmp_destroy_tas_lock);
3487 __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;
3489 __kmp_get_user_lock_location_ = (
const ident_t *(*)(kmp_user_lock_p))NULL;
3491 __kmp_set_user_lock_location_ =
3492 (void (*)(kmp_user_lock_p,
const ident_t *))NULL;
3494 __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;
3496 __kmp_set_user_lock_flags_ =
3497 (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
3503 __kmp_base_user_lock_size =
sizeof(kmp_base_futex_lock_t);
3504 __kmp_user_lock_size =
sizeof(kmp_futex_lock_t);
3506 __kmp_get_user_lock_owner_ =
3507 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_futex_lock_owner);
3509 if (__kmp_env_consistency_check) {
3510 KMP_BIND_USER_LOCK_WITH_CHECKS(futex);
3511 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(futex);
3513 KMP_BIND_USER_LOCK(futex);
3514 KMP_BIND_NESTED_USER_LOCK(futex);
3517 __kmp_destroy_user_lock_ =
3518 (void (*)(kmp_user_lock_p))(&__kmp_destroy_futex_lock);
3520 __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;
3522 __kmp_get_user_lock_location_ = (
const ident_t *(*)(kmp_user_lock_p))NULL;
3524 __kmp_set_user_lock_location_ =
3525 (void (*)(kmp_user_lock_p,
const ident_t *))NULL;
3527 __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;
3529 __kmp_set_user_lock_flags_ =
3530 (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
3533 #endif // KMP_USE_FUTEX 3536 __kmp_base_user_lock_size =
sizeof(kmp_base_ticket_lock_t);
3537 __kmp_user_lock_size =
sizeof(kmp_ticket_lock_t);
3539 __kmp_get_user_lock_owner_ =
3540 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_owner);
3542 if (__kmp_env_consistency_check) {
3543 KMP_BIND_USER_LOCK_WITH_CHECKS(ticket);
3544 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(ticket);
3546 KMP_BIND_USER_LOCK(ticket);
3547 KMP_BIND_NESTED_USER_LOCK(ticket);
3550 __kmp_destroy_user_lock_ =
3551 (void (*)(kmp_user_lock_p))(&__kmp_destroy_ticket_lock);
3553 __kmp_is_user_lock_initialized_ =
3554 (int (*)(kmp_user_lock_p))(&__kmp_is_ticket_lock_initialized);
3556 __kmp_get_user_lock_location_ =
3557 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_location);
3559 __kmp_set_user_lock_location_ = (void (*)(
3560 kmp_user_lock_p,
const ident_t *))(&__kmp_set_ticket_lock_location);
3562 __kmp_get_user_lock_flags_ =
3563 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_flags);
3565 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3566 &__kmp_set_ticket_lock_flags);
3570 __kmp_base_user_lock_size =
sizeof(kmp_base_queuing_lock_t);
3571 __kmp_user_lock_size =
sizeof(kmp_queuing_lock_t);
3573 __kmp_get_user_lock_owner_ =
3574 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);
3576 if (__kmp_env_consistency_check) {
3577 KMP_BIND_USER_LOCK_WITH_CHECKS(queuing);
3578 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(queuing);
3580 KMP_BIND_USER_LOCK(queuing);
3581 KMP_BIND_NESTED_USER_LOCK(queuing);
3584 __kmp_destroy_user_lock_ =
3585 (void (*)(kmp_user_lock_p))(&__kmp_destroy_queuing_lock);
3587 __kmp_is_user_lock_initialized_ =
3588 (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);
3590 __kmp_get_user_lock_location_ =
3591 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);
3593 __kmp_set_user_lock_location_ = (void (*)(
3594 kmp_user_lock_p,
const ident_t *))(&__kmp_set_queuing_lock_location);
3596 __kmp_get_user_lock_flags_ =
3597 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);
3599 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3600 &__kmp_set_queuing_lock_flags);
3603 #if KMP_USE_ADAPTIVE_LOCKS 3605 __kmp_base_user_lock_size =
sizeof(kmp_base_adaptive_lock_t);
3606 __kmp_user_lock_size =
sizeof(kmp_adaptive_lock_t);
3608 __kmp_get_user_lock_owner_ =
3609 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);
3611 if (__kmp_env_consistency_check) {
3612 KMP_BIND_USER_LOCK_WITH_CHECKS(adaptive);
3614 KMP_BIND_USER_LOCK(adaptive);
3617 __kmp_destroy_user_lock_ =
3618 (void (*)(kmp_user_lock_p))(&__kmp_destroy_adaptive_lock);
3620 __kmp_is_user_lock_initialized_ =
3621 (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);
3623 __kmp_get_user_lock_location_ =
3624 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);
3626 __kmp_set_user_lock_location_ = (void (*)(
3627 kmp_user_lock_p,
const ident_t *))(&__kmp_set_queuing_lock_location);
3629 __kmp_get_user_lock_flags_ =
3630 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);
3632 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3633 &__kmp_set_queuing_lock_flags);
3636 #endif // KMP_USE_ADAPTIVE_LOCKS 3639 __kmp_base_user_lock_size =
sizeof(kmp_base_drdpa_lock_t);
3640 __kmp_user_lock_size =
sizeof(kmp_drdpa_lock_t);
3642 __kmp_get_user_lock_owner_ =
3643 (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_owner);
3645 if (__kmp_env_consistency_check) {
3646 KMP_BIND_USER_LOCK_WITH_CHECKS(drdpa);
3647 KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(drdpa);
3649 KMP_BIND_USER_LOCK(drdpa);
3650 KMP_BIND_NESTED_USER_LOCK(drdpa);
3653 __kmp_destroy_user_lock_ =
3654 (void (*)(kmp_user_lock_p))(&__kmp_destroy_drdpa_lock);
3656 __kmp_is_user_lock_initialized_ =
3657 (int (*)(kmp_user_lock_p))(&__kmp_is_drdpa_lock_initialized);
3659 __kmp_get_user_lock_location_ =
3660 (
const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_location);
3662 __kmp_set_user_lock_location_ = (void (*)(
3663 kmp_user_lock_p,
const ident_t *))(&__kmp_set_drdpa_lock_location);
3665 __kmp_get_user_lock_flags_ =
3666 (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_flags);
3668 __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
3669 &__kmp_set_drdpa_lock_flags);
3677 kmp_lock_table_t __kmp_user_lock_table = {1, 0, NULL};
3678 kmp_user_lock_p __kmp_lock_pool = NULL;
3681 kmp_block_of_locks *__kmp_lock_blocks = NULL;
3682 int __kmp_num_locks_in_block = 1;
3684 static kmp_lock_index_t __kmp_lock_table_insert(kmp_user_lock_p lck) {
3686 kmp_lock_index_t index;
3687 if (__kmp_user_lock_table.used >= __kmp_user_lock_table.allocated) {
3688 kmp_lock_index_t size;
3689 kmp_user_lock_p *table;
3691 if (__kmp_user_lock_table.allocated == 0) {
3694 size = __kmp_user_lock_table.allocated * 2;
3696 table = (kmp_user_lock_p *)__kmp_allocate(
sizeof(kmp_user_lock_p) * size);
3697 KMP_MEMCPY(table + 1, __kmp_user_lock_table.table + 1,
3698 sizeof(kmp_user_lock_p) * (__kmp_user_lock_table.used - 1));
3699 table[0] = (kmp_user_lock_p)__kmp_user_lock_table.table;
3704 __kmp_user_lock_table.table = table;
3705 __kmp_user_lock_table.allocated = size;
3707 KMP_DEBUG_ASSERT(__kmp_user_lock_table.used <
3708 __kmp_user_lock_table.allocated);
3709 index = __kmp_user_lock_table.used;
3710 __kmp_user_lock_table.table[index] = lck;
3711 ++__kmp_user_lock_table.used;
3715 static kmp_user_lock_p __kmp_lock_block_allocate() {
3717 static int last_index = 0;
3718 if ((last_index >= __kmp_num_locks_in_block) || (__kmp_lock_blocks == NULL)) {
3722 KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
3723 size_t space_for_locks = __kmp_user_lock_size * __kmp_num_locks_in_block;
3725 (
char *)__kmp_allocate(space_for_locks +
sizeof(kmp_block_of_locks));
3727 kmp_block_of_locks *new_block =
3728 (kmp_block_of_locks *)(&buffer[space_for_locks]);
3729 new_block->next_block = __kmp_lock_blocks;
3730 new_block->locks = (
void *)buffer;
3733 __kmp_lock_blocks = new_block;
3735 kmp_user_lock_p ret = (kmp_user_lock_p)(&(
3736 ((
char *)(__kmp_lock_blocks->locks))[last_index * __kmp_user_lock_size]));
3743 kmp_user_lock_p __kmp_user_lock_allocate(
void **user_lock, kmp_int32 gtid,
3744 kmp_lock_flags_t flags) {
3745 kmp_user_lock_p lck;
3746 kmp_lock_index_t index;
3747 KMP_DEBUG_ASSERT(user_lock);
3749 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3751 if (__kmp_lock_pool == NULL) {
3756 ANNOTATE_IGNORE_WRITES_BEGIN();
3757 if (__kmp_num_locks_in_block <= 1) {
3758 lck = (kmp_user_lock_p)__kmp_allocate(__kmp_user_lock_size);
3760 lck = __kmp_lock_block_allocate();
3762 ANNOTATE_IGNORE_WRITES_END();
3766 index = __kmp_lock_table_insert(lck);
3769 lck = __kmp_lock_pool;
3770 index = __kmp_lock_pool->pool.index;
3771 __kmp_lock_pool = __kmp_lock_pool->pool.next;
3776 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3777 *((kmp_lock_index_t *)user_lock) = index;
3779 *((kmp_user_lock_p *)user_lock) = lck;
3783 __kmp_set_user_lock_flags(lck, flags);
3785 __kmp_release_lock(&__kmp_global_lock, gtid);
3791 void __kmp_user_lock_free(
void **user_lock, kmp_int32 gtid,
3792 kmp_user_lock_p lck) {
3793 KMP_DEBUG_ASSERT(user_lock != NULL);
3794 KMP_DEBUG_ASSERT(lck != NULL);
3796 __kmp_acquire_lock(&__kmp_global_lock, gtid);
3798 lck->pool.next = __kmp_lock_pool;
3799 __kmp_lock_pool = lck;
3800 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3801 kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
3802 KMP_DEBUG_ASSERT(0 < index && index <= __kmp_user_lock_table.used);
3803 lck->pool.index = index;
3806 __kmp_release_lock(&__kmp_global_lock, gtid);
3809 kmp_user_lock_p __kmp_lookup_user_lock(
void **user_lock,
char const *func) {
3810 kmp_user_lock_p lck = NULL;
3812 if (__kmp_env_consistency_check) {
3813 if (user_lock == NULL) {
3814 KMP_FATAL(LockIsUninitialized, func);
3818 if (OMP_LOCK_T_SIZE <
sizeof(
void *)) {
3819 kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
3820 if (__kmp_env_consistency_check) {
3821 if (!(0 < index && index < __kmp_user_lock_table.used)) {
3822 KMP_FATAL(LockIsUninitialized, func);
3825 KMP_DEBUG_ASSERT(0 < index && index < __kmp_user_lock_table.used);
3826 KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
3827 lck = __kmp_user_lock_table.table[index];
3829 lck = *((kmp_user_lock_p *)user_lock);
3832 if (__kmp_env_consistency_check) {
3834 KMP_FATAL(LockIsUninitialized, func);
3841 void __kmp_cleanup_user_locks(
void) {
3844 __kmp_lock_pool = NULL;
3846 #define IS_CRITICAL(lck) \ 3847 ((__kmp_get_user_lock_flags_ != NULL) && \ 3848 ((*__kmp_get_user_lock_flags_)(lck)&kmp_lf_critical_section)) 3873 while (__kmp_user_lock_table.used > 1) {
3878 kmp_user_lock_p lck =
3879 __kmp_user_lock_table.table[--__kmp_user_lock_table.used];
3881 if ((__kmp_is_user_lock_initialized_ != NULL) &&
3882 (*__kmp_is_user_lock_initialized_)(lck)) {
3886 if (__kmp_env_consistency_check && (!IS_CRITICAL(lck)) &&
3887 ((loc = __kmp_get_user_lock_location(lck)) != NULL) &&
3889 kmp_str_loc_t str_loc = __kmp_str_loc_init(loc->
psource, 0);
3890 KMP_WARNING(CnsLockNotDestroyed, str_loc.file, str_loc.line);
3891 __kmp_str_loc_free(&str_loc);
3895 if (IS_CRITICAL(lck)) {
3898 (
"__kmp_cleanup_user_locks: free critical section lock %p (%p)\n",
3899 lck, *(
void **)lck));
3901 KA_TRACE(20, (
"__kmp_cleanup_user_locks: free lock %p (%p)\n", lck,
3907 __kmp_destroy_user_lock(lck);
3911 if (__kmp_lock_blocks == NULL) {
3919 kmp_user_lock_p *table_ptr = __kmp_user_lock_table.table;
3920 __kmp_user_lock_table.table = NULL;
3921 __kmp_user_lock_table.allocated = 0;
3923 while (table_ptr != NULL) {
3926 kmp_user_lock_p *next = (kmp_user_lock_p *)(table_ptr[0]);
3927 __kmp_free(table_ptr);
3932 kmp_block_of_locks_t *block_ptr = __kmp_lock_blocks;
3933 __kmp_lock_blocks = NULL;
3935 while (block_ptr != NULL) {
3936 kmp_block_of_locks_t *next = block_ptr->next_block;
3937 __kmp_free(block_ptr->locks);
3942 TCW_4(__kmp_init_user_locks, FALSE);
3945 #endif // KMP_USE_DYNAMIC_LOCK