LLVM OpenMP* Runtime Library
kmp_lock.h
1 /*
2  * kmp_lock.h -- lock header file
3  */
4 
5 //===----------------------------------------------------------------------===//
6 //
7 // The LLVM Compiler Infrastructure
8 //
9 // This file is dual licensed under the MIT and the University of Illinois Open
10 // Source Licenses. See LICENSE.txt for details.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef KMP_LOCK_H
15 #define KMP_LOCK_H
16 
17 #include <limits.h> // CHAR_BIT
18 #include <stddef.h> // offsetof
19 
20 #include "kmp_debug.h"
21 #include "kmp_os.h"
22 
23 #ifdef __cplusplus
24 #include <atomic>
25 
26 extern "C" {
27 #endif // __cplusplus
28 
29 // ----------------------------------------------------------------------------
30 // Have to copy these definitions from kmp.h because kmp.h cannot be included
31 // due to circular dependencies. Will undef these at end of file.
32 
33 #define KMP_PAD(type, sz) \
34  (sizeof(type) + (sz - ((sizeof(type) - 1) % (sz)) - 1))
35 #define KMP_GTID_DNE (-2)
36 
37 // Forward declaration of ident and ident_t
38 
39 struct ident;
40 typedef struct ident ident_t;
41 
42 // End of copied code.
43 // ----------------------------------------------------------------------------
44 
45 // We need to know the size of the area we can assume that the compiler(s)
46 // allocated for obects of type omp_lock_t and omp_nest_lock_t. The Intel
47 // compiler always allocates a pointer-sized area, as does visual studio.
48 //
49 // gcc however, only allocates 4 bytes for regular locks, even on 64-bit
50 // intel archs. It allocates at least 8 bytes for nested lock (more on
51 // recent versions), but we are bounded by the pointer-sized chunks that
52 // the Intel compiler allocates.
53 
54 #if KMP_OS_LINUX && defined(KMP_GOMP_COMPAT)
55 #define OMP_LOCK_T_SIZE sizeof(int)
56 #define OMP_NEST_LOCK_T_SIZE sizeof(void *)
57 #else
58 #define OMP_LOCK_T_SIZE sizeof(void *)
59 #define OMP_NEST_LOCK_T_SIZE sizeof(void *)
60 #endif
61 
62 // The Intel compiler allocates a 32-byte chunk for a critical section.
63 // Both gcc and visual studio only allocate enough space for a pointer.
64 // Sometimes we know that the space was allocated by the Intel compiler.
65 #define OMP_CRITICAL_SIZE sizeof(void *)
66 #define INTEL_CRITICAL_SIZE 32
67 
68 // lock flags
69 typedef kmp_uint32 kmp_lock_flags_t;
70 
71 #define kmp_lf_critical_section 1
72 
73 // When a lock table is used, the indices are of kmp_lock_index_t
74 typedef kmp_uint32 kmp_lock_index_t;
75 
76 // When memory allocated for locks are on the lock pool (free list),
77 // it is treated as structs of this type.
78 struct kmp_lock_pool {
79  union kmp_user_lock *next;
80  kmp_lock_index_t index;
81 };
82 
83 typedef struct kmp_lock_pool kmp_lock_pool_t;
84 
85 extern void __kmp_validate_locks(void);
86 
87 // ----------------------------------------------------------------------------
88 // There are 5 lock implementations:
89 // 1. Test and set locks.
90 // 2. futex locks (Linux* OS on x86 and
91 // Intel(R) Many Integrated Core Architecture)
92 // 3. Ticket (Lamport bakery) locks.
93 // 4. Queuing locks (with separate spin fields).
94 // 5. DRPA (Dynamically Reconfigurable Distributed Polling Area) locks
95 //
96 // and 3 lock purposes:
97 // 1. Bootstrap locks -- Used for a few locks available at library
98 // startup-shutdown time.
99 // These do not require non-negative global thread ID's.
100 // 2. Internal RTL locks -- Used everywhere else in the RTL
101 // 3. User locks (includes critical sections)
102 // ----------------------------------------------------------------------------
103 
104 // ============================================================================
105 // Lock implementations.
106 //
107 // Test and set locks.
108 //
109 // Non-nested test and set locks differ from the other lock kinds (except
110 // futex) in that we use the memory allocated by the compiler for the lock,
111 // rather than a pointer to it.
112 //
113 // On lin32, lin_32e, and win_32, the space allocated may be as small as 4
114 // bytes, so we have to use a lock table for nested locks, and avoid accessing
115 // the depth_locked field for non-nested locks.
116 //
117 // Information normally available to the tools, such as lock location, lock
118 // usage (normal lock vs. critical section), etc. is not available with test and
119 // set locks.
120 // ----------------------------------------------------------------------------
121 
122 struct kmp_base_tas_lock {
123  // KMP_LOCK_FREE(tas) => unlocked; locked: (gtid+1) of owning thread
124  std::atomic<kmp_int32> poll;
125  kmp_int32 depth_locked; // depth locked, for nested locks only
126 };
127 
128 typedef struct kmp_base_tas_lock kmp_base_tas_lock_t;
129 
130 union kmp_tas_lock {
131  kmp_base_tas_lock_t lk;
132  kmp_lock_pool_t pool; // make certain struct is large enough
133  double lk_align; // use worst case alignment; no cache line padding
134 };
135 
136 typedef union kmp_tas_lock kmp_tas_lock_t;
137 
138 // Static initializer for test and set lock variables. Usage:
139 // kmp_tas_lock_t xlock = KMP_TAS_LOCK_INITIALIZER( xlock );
140 #define KMP_TAS_LOCK_INITIALIZER(lock) \
141  { \
142  { ATOMIC_VAR_INIT(KMP_LOCK_FREE(tas)), 0 } \
143  }
144 
145 extern int __kmp_acquire_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
146 extern int __kmp_test_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
147 extern int __kmp_release_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
148 extern void __kmp_init_tas_lock(kmp_tas_lock_t *lck);
149 extern void __kmp_destroy_tas_lock(kmp_tas_lock_t *lck);
150 
151 extern int __kmp_acquire_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
152 extern int __kmp_test_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
153 extern int __kmp_release_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
154 extern void __kmp_init_nested_tas_lock(kmp_tas_lock_t *lck);
155 extern void __kmp_destroy_nested_tas_lock(kmp_tas_lock_t *lck);
156 
157 #define KMP_LOCK_RELEASED 1
158 #define KMP_LOCK_STILL_HELD 0
159 #define KMP_LOCK_ACQUIRED_FIRST 1
160 #define KMP_LOCK_ACQUIRED_NEXT 0
161 #ifndef KMP_USE_FUTEX
162 #define KMP_USE_FUTEX \
163  (KMP_OS_LINUX && !KMP_OS_CNK && \
164  (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64))
165 #endif
166 #if KMP_USE_FUTEX
167 
168 // ----------------------------------------------------------------------------
169 // futex locks. futex locks are only available on Linux* OS.
170 //
171 // Like non-nested test and set lock, non-nested futex locks use the memory
172 // allocated by the compiler for the lock, rather than a pointer to it.
173 //
174 // Information normally available to the tools, such as lock location, lock
175 // usage (normal lock vs. critical section), etc. is not available with test and
176 // set locks. With non-nested futex locks, the lock owner is not even available.
177 // ----------------------------------------------------------------------------
178 
179 struct kmp_base_futex_lock {
180  volatile kmp_int32 poll; // KMP_LOCK_FREE(futex) => unlocked
181  // 2*(gtid+1) of owning thread, 0 if unlocked
182  // locked: (gtid+1) of owning thread
183  kmp_int32 depth_locked; // depth locked, for nested locks only
184 };
185 
186 typedef struct kmp_base_futex_lock kmp_base_futex_lock_t;
187 
188 union kmp_futex_lock {
189  kmp_base_futex_lock_t lk;
190  kmp_lock_pool_t pool; // make certain struct is large enough
191  double lk_align; // use worst case alignment
192  // no cache line padding
193 };
194 
195 typedef union kmp_futex_lock kmp_futex_lock_t;
196 
197 // Static initializer for futex lock variables. Usage:
198 // kmp_futex_lock_t xlock = KMP_FUTEX_LOCK_INITIALIZER( xlock );
199 #define KMP_FUTEX_LOCK_INITIALIZER(lock) \
200  { \
201  { KMP_LOCK_FREE(futex), 0 } \
202  }
203 
204 extern int __kmp_acquire_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);
205 extern int __kmp_test_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);
206 extern int __kmp_release_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);
207 extern void __kmp_init_futex_lock(kmp_futex_lock_t *lck);
208 extern void __kmp_destroy_futex_lock(kmp_futex_lock_t *lck);
209 
210 extern int __kmp_acquire_nested_futex_lock(kmp_futex_lock_t *lck,
211  kmp_int32 gtid);
212 extern int __kmp_test_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);
213 extern int __kmp_release_nested_futex_lock(kmp_futex_lock_t *lck,
214  kmp_int32 gtid);
215 extern void __kmp_init_nested_futex_lock(kmp_futex_lock_t *lck);
216 extern void __kmp_destroy_nested_futex_lock(kmp_futex_lock_t *lck);
217 
218 #endif // KMP_USE_FUTEX
219 
220 // ----------------------------------------------------------------------------
221 // Ticket locks.
222 
223 #ifdef __cplusplus
224 
225 #ifdef _MSC_VER
226 // MSVC won't allow use of std::atomic<> in a union since it has non-trivial
227 // copy constructor.
228 
229 struct kmp_base_ticket_lock {
230  // `initialized' must be the first entry in the lock data structure!
231  std::atomic_bool initialized;
232  volatile union kmp_ticket_lock *self; // points to the lock union
233  ident_t const *location; // Source code location of omp_init_lock().
234  std::atomic_uint
235  next_ticket; // ticket number to give to next thread which acquires
236  std::atomic_uint now_serving; // ticket number for thread which holds the lock
237  std::atomic_int owner_id; // (gtid+1) of owning thread, 0 if unlocked
238  std::atomic_int depth_locked; // depth locked, for nested locks only
239  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
240 };
241 #else
242 struct kmp_base_ticket_lock {
243  // `initialized' must be the first entry in the lock data structure!
244  std::atomic<bool> initialized;
245  volatile union kmp_ticket_lock *self; // points to the lock union
246  ident_t const *location; // Source code location of omp_init_lock().
247  std::atomic<unsigned>
248  next_ticket; // ticket number to give to next thread which acquires
249  std::atomic<unsigned>
250  now_serving; // ticket number for thread which holds the lock
251  std::atomic<int> owner_id; // (gtid+1) of owning thread, 0 if unlocked
252  std::atomic<int> depth_locked; // depth locked, for nested locks only
253  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
254 };
255 #endif
256 
257 #else // __cplusplus
258 
259 struct kmp_base_ticket_lock;
260 
261 #endif // !__cplusplus
262 
263 typedef struct kmp_base_ticket_lock kmp_base_ticket_lock_t;
264 
265 union KMP_ALIGN_CACHE kmp_ticket_lock {
266  kmp_base_ticket_lock_t
267  lk; // This field must be first to allow static initializing.
268  kmp_lock_pool_t pool;
269  double lk_align; // use worst case alignment
270  char lk_pad[KMP_PAD(kmp_base_ticket_lock_t, CACHE_LINE)];
271 };
272 
273 typedef union kmp_ticket_lock kmp_ticket_lock_t;
274 
275 // Static initializer for simple ticket lock variables. Usage:
276 // kmp_ticket_lock_t xlock = KMP_TICKET_LOCK_INITIALIZER( xlock );
277 // Note the macro argument. It is important to make var properly initialized.
278 #define KMP_TICKET_LOCK_INITIALIZER(lock) \
279  { \
280  { \
281  ATOMIC_VAR_INIT(true) \
282  , &(lock), NULL, ATOMIC_VAR_INIT(0U), ATOMIC_VAR_INIT(0U), \
283  ATOMIC_VAR_INIT(0), ATOMIC_VAR_INIT(-1) \
284  } \
285  }
286 
287 extern int __kmp_acquire_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid);
288 extern int __kmp_test_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid);
289 extern int __kmp_test_ticket_lock_with_cheks(kmp_ticket_lock_t *lck,
290  kmp_int32 gtid);
291 extern int __kmp_release_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid);
292 extern void __kmp_init_ticket_lock(kmp_ticket_lock_t *lck);
293 extern void __kmp_destroy_ticket_lock(kmp_ticket_lock_t *lck);
294 
295 extern int __kmp_acquire_nested_ticket_lock(kmp_ticket_lock_t *lck,
296  kmp_int32 gtid);
297 extern int __kmp_test_nested_ticket_lock(kmp_ticket_lock_t *lck,
298  kmp_int32 gtid);
299 extern int __kmp_release_nested_ticket_lock(kmp_ticket_lock_t *lck,
300  kmp_int32 gtid);
301 extern void __kmp_init_nested_ticket_lock(kmp_ticket_lock_t *lck);
302 extern void __kmp_destroy_nested_ticket_lock(kmp_ticket_lock_t *lck);
303 
304 // ----------------------------------------------------------------------------
305 // Queuing locks.
306 
307 #if KMP_USE_ADAPTIVE_LOCKS
308 
309 struct kmp_adaptive_lock_info;
310 
311 typedef struct kmp_adaptive_lock_info kmp_adaptive_lock_info_t;
312 
313 #if KMP_DEBUG_ADAPTIVE_LOCKS
314 
315 struct kmp_adaptive_lock_statistics {
316  /* So we can get stats from locks that haven't been destroyed. */
317  kmp_adaptive_lock_info_t *next;
318  kmp_adaptive_lock_info_t *prev;
319 
320  /* Other statistics */
321  kmp_uint32 successfulSpeculations;
322  kmp_uint32 hardFailedSpeculations;
323  kmp_uint32 softFailedSpeculations;
324  kmp_uint32 nonSpeculativeAcquires;
325  kmp_uint32 nonSpeculativeAcquireAttempts;
326  kmp_uint32 lemmingYields;
327 };
328 
329 typedef struct kmp_adaptive_lock_statistics kmp_adaptive_lock_statistics_t;
330 
331 extern void __kmp_print_speculative_stats();
332 extern void __kmp_init_speculative_stats();
333 
334 #endif // KMP_DEBUG_ADAPTIVE_LOCKS
335 
336 struct kmp_adaptive_lock_info {
337  /* Values used for adaptivity.
338  Although these are accessed from multiple threads we don't access them
339  atomically, because if we miss updates it probably doesn't matter much. (It
340  just affects our decision about whether to try speculation on the lock). */
341  kmp_uint32 volatile badness;
342  kmp_uint32 volatile acquire_attempts;
343  /* Parameters of the lock. */
344  kmp_uint32 max_badness;
345  kmp_uint32 max_soft_retries;
346 
347 #if KMP_DEBUG_ADAPTIVE_LOCKS
348  kmp_adaptive_lock_statistics_t volatile stats;
349 #endif
350 };
351 
352 #endif // KMP_USE_ADAPTIVE_LOCKS
353 
354 struct kmp_base_queuing_lock {
355 
356  // `initialized' must be the first entry in the lock data structure!
357  volatile union kmp_queuing_lock
358  *initialized; // Points to the lock union if in initialized state.
359 
360  ident_t const *location; // Source code location of omp_init_lock().
361 
362  KMP_ALIGN(8) // tail_id must be 8-byte aligned!
363 
364  volatile kmp_int32
365  tail_id; // (gtid+1) of thread at tail of wait queue, 0 if empty
366  // Must be no padding here since head/tail used in 8-byte CAS
367  volatile kmp_int32
368  head_id; // (gtid+1) of thread at head of wait queue, 0 if empty
369  // Decl order assumes little endian
370  // bakery-style lock
371  volatile kmp_uint32
372  next_ticket; // ticket number to give to next thread which acquires
373  volatile kmp_uint32
374  now_serving; // ticket number for thread which holds the lock
375  volatile kmp_int32 owner_id; // (gtid+1) of owning thread, 0 if unlocked
376  kmp_int32 depth_locked; // depth locked, for nested locks only
377 
378  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
379 };
380 
381 typedef struct kmp_base_queuing_lock kmp_base_queuing_lock_t;
382 
383 KMP_BUILD_ASSERT(offsetof(kmp_base_queuing_lock_t, tail_id) % 8 == 0);
384 
385 union KMP_ALIGN_CACHE kmp_queuing_lock {
386  kmp_base_queuing_lock_t
387  lk; // This field must be first to allow static initializing.
388  kmp_lock_pool_t pool;
389  double lk_align; // use worst case alignment
390  char lk_pad[KMP_PAD(kmp_base_queuing_lock_t, CACHE_LINE)];
391 };
392 
393 typedef union kmp_queuing_lock kmp_queuing_lock_t;
394 
395 extern int __kmp_acquire_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid);
396 extern int __kmp_test_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid);
397 extern int __kmp_release_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid);
398 extern void __kmp_init_queuing_lock(kmp_queuing_lock_t *lck);
399 extern void __kmp_destroy_queuing_lock(kmp_queuing_lock_t *lck);
400 
401 extern int __kmp_acquire_nested_queuing_lock(kmp_queuing_lock_t *lck,
402  kmp_int32 gtid);
403 extern int __kmp_test_nested_queuing_lock(kmp_queuing_lock_t *lck,
404  kmp_int32 gtid);
405 extern int __kmp_release_nested_queuing_lock(kmp_queuing_lock_t *lck,
406  kmp_int32 gtid);
407 extern void __kmp_init_nested_queuing_lock(kmp_queuing_lock_t *lck);
408 extern void __kmp_destroy_nested_queuing_lock(kmp_queuing_lock_t *lck);
409 
410 #if KMP_USE_ADAPTIVE_LOCKS
411 
412 // ----------------------------------------------------------------------------
413 // Adaptive locks.
414 struct kmp_base_adaptive_lock {
415  kmp_base_queuing_lock qlk;
416  KMP_ALIGN(CACHE_LINE)
417  kmp_adaptive_lock_info_t
418  adaptive; // Information for the speculative adaptive lock
419 };
420 
421 typedef struct kmp_base_adaptive_lock kmp_base_adaptive_lock_t;
422 
423 union KMP_ALIGN_CACHE kmp_adaptive_lock {
424  kmp_base_adaptive_lock_t lk;
425  kmp_lock_pool_t pool;
426  double lk_align;
427  char lk_pad[KMP_PAD(kmp_base_adaptive_lock_t, CACHE_LINE)];
428 };
429 typedef union kmp_adaptive_lock kmp_adaptive_lock_t;
430 
431 #define GET_QLK_PTR(l) ((kmp_queuing_lock_t *)&(l)->lk.qlk)
432 
433 #endif // KMP_USE_ADAPTIVE_LOCKS
434 
435 // ----------------------------------------------------------------------------
436 // DRDPA ticket locks.
437 struct kmp_base_drdpa_lock {
438  // All of the fields on the first cache line are only written when
439  // initializing or reconfiguring the lock. These are relatively rare
440  // operations, so data from the first cache line will usually stay resident in
441  // the cache of each thread trying to acquire the lock.
442  //
443  // initialized must be the first entry in the lock data structure!
444  KMP_ALIGN_CACHE
445 
446  volatile union kmp_drdpa_lock
447  *initialized; // points to the lock union if in initialized state
448  ident_t const *location; // Source code location of omp_init_lock().
449  std::atomic<std::atomic<kmp_uint64> *> polls;
450  std::atomic<kmp_uint64> mask; // is 2**num_polls-1 for mod op
451  kmp_uint64 cleanup_ticket; // thread with cleanup ticket
452  std::atomic<kmp_uint64> *old_polls; // will deallocate old_polls
453  kmp_uint32 num_polls; // must be power of 2
454 
455  // next_ticket it needs to exist in a separate cache line, as it is
456  // invalidated every time a thread takes a new ticket.
457  KMP_ALIGN_CACHE
458 
459  std::atomic<kmp_uint64> next_ticket;
460 
461  // now_serving is used to store our ticket value while we hold the lock. It
462  // has a slightly different meaning in the DRDPA ticket locks (where it is
463  // written by the acquiring thread) than it does in the simple ticket locks
464  // (where it is written by the releasing thread).
465  //
466  // Since now_serving is only read an written in the critical section,
467  // it is non-volatile, but it needs to exist on a separate cache line,
468  // as it is invalidated at every lock acquire.
469  //
470  // Likewise, the vars used for nested locks (owner_id and depth_locked) are
471  // only written by the thread owning the lock, so they are put in this cache
472  // line. owner_id is read by other threads, so it must be declared volatile.
473  KMP_ALIGN_CACHE
474  kmp_uint64 now_serving; // doesn't have to be volatile
475  volatile kmp_uint32 owner_id; // (gtid+1) of owning thread, 0 if unlocked
476  kmp_int32 depth_locked; // depth locked
477  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
478 };
479 
480 typedef struct kmp_base_drdpa_lock kmp_base_drdpa_lock_t;
481 
482 union KMP_ALIGN_CACHE kmp_drdpa_lock {
483  kmp_base_drdpa_lock_t
484  lk; // This field must be first to allow static initializing. */
485  kmp_lock_pool_t pool;
486  double lk_align; // use worst case alignment
487  char lk_pad[KMP_PAD(kmp_base_drdpa_lock_t, CACHE_LINE)];
488 };
489 
490 typedef union kmp_drdpa_lock kmp_drdpa_lock_t;
491 
492 extern int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);
493 extern int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);
494 extern int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);
495 extern void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck);
496 extern void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck);
497 
498 extern int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck,
499  kmp_int32 gtid);
500 extern int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);
501 extern int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck,
502  kmp_int32 gtid);
503 extern void __kmp_init_nested_drdpa_lock(kmp_drdpa_lock_t *lck);
504 extern void __kmp_destroy_nested_drdpa_lock(kmp_drdpa_lock_t *lck);
505 
506 // ============================================================================
507 // Lock purposes.
508 // ============================================================================
509 
510 // Bootstrap locks.
511 //
512 // Bootstrap locks -- very few locks used at library initialization time.
513 // Bootstrap locks are currently implemented as ticket locks.
514 // They could also be implemented as test and set lock, but cannot be
515 // implemented with other lock kinds as they require gtids which are not
516 // available at initialization time.
517 
518 typedef kmp_ticket_lock_t kmp_bootstrap_lock_t;
519 
520 #define KMP_BOOTSTRAP_LOCK_INITIALIZER(lock) KMP_TICKET_LOCK_INITIALIZER((lock))
521 #define KMP_BOOTSTRAP_LOCK_INIT(lock) \
522  kmp_bootstrap_lock_t lock = KMP_TICKET_LOCK_INITIALIZER(lock)
523 
524 static inline int __kmp_acquire_bootstrap_lock(kmp_bootstrap_lock_t *lck) {
525  return __kmp_acquire_ticket_lock(lck, KMP_GTID_DNE);
526 }
527 
528 static inline int __kmp_test_bootstrap_lock(kmp_bootstrap_lock_t *lck) {
529  return __kmp_test_ticket_lock(lck, KMP_GTID_DNE);
530 }
531 
532 static inline void __kmp_release_bootstrap_lock(kmp_bootstrap_lock_t *lck) {
533  __kmp_release_ticket_lock(lck, KMP_GTID_DNE);
534 }
535 
536 static inline void __kmp_init_bootstrap_lock(kmp_bootstrap_lock_t *lck) {
537  __kmp_init_ticket_lock(lck);
538 }
539 
540 static inline void __kmp_destroy_bootstrap_lock(kmp_bootstrap_lock_t *lck) {
541  __kmp_destroy_ticket_lock(lck);
542 }
543 
544 // Internal RTL locks.
545 //
546 // Internal RTL locks are also implemented as ticket locks, for now.
547 //
548 // FIXME - We should go through and figure out which lock kind works best for
549 // each internal lock, and use the type declaration and function calls for
550 // that explicit lock kind (and get rid of this section).
551 
552 typedef kmp_ticket_lock_t kmp_lock_t;
553 
554 #define KMP_LOCK_INIT(lock) kmp_lock_t lock = KMP_TICKET_LOCK_INITIALIZER(lock)
555 
556 static inline int __kmp_acquire_lock(kmp_lock_t *lck, kmp_int32 gtid) {
557  return __kmp_acquire_ticket_lock(lck, gtid);
558 }
559 
560 static inline int __kmp_test_lock(kmp_lock_t *lck, kmp_int32 gtid) {
561  return __kmp_test_ticket_lock(lck, gtid);
562 }
563 
564 static inline void __kmp_release_lock(kmp_lock_t *lck, kmp_int32 gtid) {
565  __kmp_release_ticket_lock(lck, gtid);
566 }
567 
568 static inline void __kmp_init_lock(kmp_lock_t *lck) {
569  __kmp_init_ticket_lock(lck);
570 }
571 
572 static inline void __kmp_destroy_lock(kmp_lock_t *lck) {
573  __kmp_destroy_ticket_lock(lck);
574 }
575 
576 // User locks.
577 //
578 // Do not allocate objects of type union kmp_user_lock!!! This will waste space
579 // unless __kmp_user_lock_kind == lk_drdpa. Instead, check the value of
580 // __kmp_user_lock_kind and allocate objects of the type of the appropriate
581 // union member, and cast their addresses to kmp_user_lock_p.
582 
583 enum kmp_lock_kind {
584  lk_default = 0,
585  lk_tas,
586 #if KMP_USE_FUTEX
587  lk_futex,
588 #endif
589 #if KMP_USE_DYNAMIC_LOCK && KMP_USE_TSX
590  lk_hle,
591  lk_rtm,
592 #endif
593  lk_ticket,
594  lk_queuing,
595  lk_drdpa,
596 #if KMP_USE_ADAPTIVE_LOCKS
597  lk_adaptive
598 #endif // KMP_USE_ADAPTIVE_LOCKS
599 };
600 
601 typedef enum kmp_lock_kind kmp_lock_kind_t;
602 
603 extern kmp_lock_kind_t __kmp_user_lock_kind;
604 
605 union kmp_user_lock {
606  kmp_tas_lock_t tas;
607 #if KMP_USE_FUTEX
608  kmp_futex_lock_t futex;
609 #endif
610  kmp_ticket_lock_t ticket;
611  kmp_queuing_lock_t queuing;
612  kmp_drdpa_lock_t drdpa;
613 #if KMP_USE_ADAPTIVE_LOCKS
614  kmp_adaptive_lock_t adaptive;
615 #endif // KMP_USE_ADAPTIVE_LOCKS
616  kmp_lock_pool_t pool;
617 };
618 
619 typedef union kmp_user_lock *kmp_user_lock_p;
620 
621 #if !KMP_USE_DYNAMIC_LOCK
622 
623 extern size_t __kmp_base_user_lock_size;
624 extern size_t __kmp_user_lock_size;
625 
626 extern kmp_int32 (*__kmp_get_user_lock_owner_)(kmp_user_lock_p lck);
627 
628 static inline kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p lck) {
629  KMP_DEBUG_ASSERT(__kmp_get_user_lock_owner_ != NULL);
630  return (*__kmp_get_user_lock_owner_)(lck);
631 }
632 
633 extern int (*__kmp_acquire_user_lock_with_checks_)(kmp_user_lock_p lck,
634  kmp_int32 gtid);
635 
636 #if KMP_OS_LINUX && \
637  (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
638 
639 #define __kmp_acquire_user_lock_with_checks(lck, gtid) \
640  if (__kmp_user_lock_kind == lk_tas) { \
641  if (__kmp_env_consistency_check) { \
642  char const *const func = "omp_set_lock"; \
643  if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) && \
644  lck->tas.lk.depth_locked != -1) { \
645  KMP_FATAL(LockNestableUsedAsSimple, func); \
646  } \
647  if ((gtid >= 0) && (lck->tas.lk.poll - 1 == gtid)) { \
648  KMP_FATAL(LockIsAlreadyOwned, func); \
649  } \
650  } \
651  if (lck->tas.lk.poll != 0 || \
652  !__kmp_compare_and_store_acq(&lck->tas.lk.poll, 0, gtid + 1)) { \
653  kmp_uint32 spins; \
654  KMP_FSYNC_PREPARE(lck); \
655  KMP_INIT_YIELD(spins); \
656  if (TCR_4(__kmp_nth) > \
657  (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) { \
658  KMP_YIELD(TRUE); \
659  } else { \
660  KMP_YIELD_SPIN(spins); \
661  } \
662  while (lck->tas.lk.poll != 0 || \
663  !__kmp_compare_and_store_acq(&lck->tas.lk.poll, 0, gtid + 1)) { \
664  if (TCR_4(__kmp_nth) > \
665  (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) { \
666  KMP_YIELD(TRUE); \
667  } else { \
668  KMP_YIELD_SPIN(spins); \
669  } \
670  } \
671  } \
672  KMP_FSYNC_ACQUIRED(lck); \
673  } else { \
674  KMP_DEBUG_ASSERT(__kmp_acquire_user_lock_with_checks_ != NULL); \
675  (*__kmp_acquire_user_lock_with_checks_)(lck, gtid); \
676  }
677 
678 #else
679 static inline int __kmp_acquire_user_lock_with_checks(kmp_user_lock_p lck,
680  kmp_int32 gtid) {
681  KMP_DEBUG_ASSERT(__kmp_acquire_user_lock_with_checks_ != NULL);
682  return (*__kmp_acquire_user_lock_with_checks_)(lck, gtid);
683 }
684 #endif
685 
686 extern int (*__kmp_test_user_lock_with_checks_)(kmp_user_lock_p lck,
687  kmp_int32 gtid);
688 
689 #if KMP_OS_LINUX && \
690  (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
691 
692 #include "kmp_i18n.h" /* AC: KMP_FATAL definition */
693 extern int __kmp_env_consistency_check; /* AC: copy from kmp.h here */
694 static inline int __kmp_test_user_lock_with_checks(kmp_user_lock_p lck,
695  kmp_int32 gtid) {
696  if (__kmp_user_lock_kind == lk_tas) {
697  if (__kmp_env_consistency_check) {
698  char const *const func = "omp_test_lock";
699  if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
700  lck->tas.lk.depth_locked != -1) {
701  KMP_FATAL(LockNestableUsedAsSimple, func);
702  }
703  }
704  return ((lck->tas.lk.poll == 0) &&
705  __kmp_compare_and_store_acq(&lck->tas.lk.poll, 0, gtid + 1));
706  } else {
707  KMP_DEBUG_ASSERT(__kmp_test_user_lock_with_checks_ != NULL);
708  return (*__kmp_test_user_lock_with_checks_)(lck, gtid);
709  }
710 }
711 #else
712 static inline int __kmp_test_user_lock_with_checks(kmp_user_lock_p lck,
713  kmp_int32 gtid) {
714  KMP_DEBUG_ASSERT(__kmp_test_user_lock_with_checks_ != NULL);
715  return (*__kmp_test_user_lock_with_checks_)(lck, gtid);
716 }
717 #endif
718 
719 extern int (*__kmp_release_user_lock_with_checks_)(kmp_user_lock_p lck,
720  kmp_int32 gtid);
721 
722 static inline void __kmp_release_user_lock_with_checks(kmp_user_lock_p lck,
723  kmp_int32 gtid) {
724  KMP_DEBUG_ASSERT(__kmp_release_user_lock_with_checks_ != NULL);
725  (*__kmp_release_user_lock_with_checks_)(lck, gtid);
726 }
727 
728 extern void (*__kmp_init_user_lock_with_checks_)(kmp_user_lock_p lck);
729 
730 static inline void __kmp_init_user_lock_with_checks(kmp_user_lock_p lck) {
731  KMP_DEBUG_ASSERT(__kmp_init_user_lock_with_checks_ != NULL);
732  (*__kmp_init_user_lock_with_checks_)(lck);
733 }
734 
735 // We need a non-checking version of destroy lock for when the RTL is
736 // doing the cleanup as it can't always tell if the lock is nested or not.
737 extern void (*__kmp_destroy_user_lock_)(kmp_user_lock_p lck);
738 
739 static inline void __kmp_destroy_user_lock(kmp_user_lock_p lck) {
740  KMP_DEBUG_ASSERT(__kmp_destroy_user_lock_ != NULL);
741  (*__kmp_destroy_user_lock_)(lck);
742 }
743 
744 extern void (*__kmp_destroy_user_lock_with_checks_)(kmp_user_lock_p lck);
745 
746 static inline void __kmp_destroy_user_lock_with_checks(kmp_user_lock_p lck) {
747  KMP_DEBUG_ASSERT(__kmp_destroy_user_lock_with_checks_ != NULL);
748  (*__kmp_destroy_user_lock_with_checks_)(lck);
749 }
750 
751 extern int (*__kmp_acquire_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
752  kmp_int32 gtid);
753 
754 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)
755 
756 #define __kmp_acquire_nested_user_lock_with_checks(lck, gtid, depth) \
757  if (__kmp_user_lock_kind == lk_tas) { \
758  if (__kmp_env_consistency_check) { \
759  char const *const func = "omp_set_nest_lock"; \
760  if ((sizeof(kmp_tas_lock_t) <= OMP_NEST_LOCK_T_SIZE) && \
761  lck->tas.lk.depth_locked == -1) { \
762  KMP_FATAL(LockSimpleUsedAsNestable, func); \
763  } \
764  } \
765  if (lck->tas.lk.poll - 1 == gtid) { \
766  lck->tas.lk.depth_locked += 1; \
767  *depth = KMP_LOCK_ACQUIRED_NEXT; \
768  } else { \
769  if ((lck->tas.lk.poll != 0) || \
770  !__kmp_compare_and_store_acq(&lck->tas.lk.poll, 0, gtid + 1)) { \
771  kmp_uint32 spins; \
772  KMP_FSYNC_PREPARE(lck); \
773  KMP_INIT_YIELD(spins); \
774  if (TCR_4(__kmp_nth) > \
775  (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) { \
776  KMP_YIELD(TRUE); \
777  } else { \
778  KMP_YIELD_SPIN(spins); \
779  } \
780  while ((lck->tas.lk.poll != 0) || \
781  !__kmp_compare_and_store_acq(&lck->tas.lk.poll, 0, gtid + 1)) { \
782  if (TCR_4(__kmp_nth) > \
783  (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) { \
784  KMP_YIELD(TRUE); \
785  } else { \
786  KMP_YIELD_SPIN(spins); \
787  } \
788  } \
789  } \
790  lck->tas.lk.depth_locked = 1; \
791  *depth = KMP_LOCK_ACQUIRED_FIRST; \
792  } \
793  KMP_FSYNC_ACQUIRED(lck); \
794  } else { \
795  KMP_DEBUG_ASSERT(__kmp_acquire_nested_user_lock_with_checks_ != NULL); \
796  *depth = (*__kmp_acquire_nested_user_lock_with_checks_)(lck, gtid); \
797  }
798 
799 #else
800 static inline void
801 __kmp_acquire_nested_user_lock_with_checks(kmp_user_lock_p lck, kmp_int32 gtid,
802  int *depth) {
803  KMP_DEBUG_ASSERT(__kmp_acquire_nested_user_lock_with_checks_ != NULL);
804  *depth = (*__kmp_acquire_nested_user_lock_with_checks_)(lck, gtid);
805 }
806 #endif
807 
808 extern int (*__kmp_test_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
809  kmp_int32 gtid);
810 
811 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)
812 static inline int __kmp_test_nested_user_lock_with_checks(kmp_user_lock_p lck,
813  kmp_int32 gtid) {
814  if (__kmp_user_lock_kind == lk_tas) {
815  int retval;
816  if (__kmp_env_consistency_check) {
817  char const *const func = "omp_test_nest_lock";
818  if ((sizeof(kmp_tas_lock_t) <= OMP_NEST_LOCK_T_SIZE) &&
819  lck->tas.lk.depth_locked == -1) {
820  KMP_FATAL(LockSimpleUsedAsNestable, func);
821  }
822  }
823  KMP_DEBUG_ASSERT(gtid >= 0);
824  if (lck->tas.lk.poll - 1 ==
825  gtid) { /* __kmp_get_tas_lock_owner( lck ) == gtid */
826  return ++lck->tas.lk.depth_locked; /* same owner, depth increased */
827  }
828  retval = ((lck->tas.lk.poll == 0) &&
829  __kmp_compare_and_store_acq(&lck->tas.lk.poll, 0, gtid + 1));
830  if (retval) {
831  KMP_MB();
832  lck->tas.lk.depth_locked = 1;
833  }
834  return retval;
835  } else {
836  KMP_DEBUG_ASSERT(__kmp_test_nested_user_lock_with_checks_ != NULL);
837  return (*__kmp_test_nested_user_lock_with_checks_)(lck, gtid);
838  }
839 }
840 #else
841 static inline int __kmp_test_nested_user_lock_with_checks(kmp_user_lock_p lck,
842  kmp_int32 gtid) {
843  KMP_DEBUG_ASSERT(__kmp_test_nested_user_lock_with_checks_ != NULL);
844  return (*__kmp_test_nested_user_lock_with_checks_)(lck, gtid);
845 }
846 #endif
847 
848 extern int (*__kmp_release_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
849  kmp_int32 gtid);
850 
851 static inline int
852 __kmp_release_nested_user_lock_with_checks(kmp_user_lock_p lck,
853  kmp_int32 gtid) {
854  KMP_DEBUG_ASSERT(__kmp_release_nested_user_lock_with_checks_ != NULL);
855  return (*__kmp_release_nested_user_lock_with_checks_)(lck, gtid);
856 }
857 
858 extern void (*__kmp_init_nested_user_lock_with_checks_)(kmp_user_lock_p lck);
859 
860 static inline void
861 __kmp_init_nested_user_lock_with_checks(kmp_user_lock_p lck) {
862  KMP_DEBUG_ASSERT(__kmp_init_nested_user_lock_with_checks_ != NULL);
863  (*__kmp_init_nested_user_lock_with_checks_)(lck);
864 }
865 
866 extern void (*__kmp_destroy_nested_user_lock_with_checks_)(kmp_user_lock_p lck);
867 
868 static inline void
869 __kmp_destroy_nested_user_lock_with_checks(kmp_user_lock_p lck) {
870  KMP_DEBUG_ASSERT(__kmp_destroy_nested_user_lock_with_checks_ != NULL);
871  (*__kmp_destroy_nested_user_lock_with_checks_)(lck);
872 }
873 
874 // user lock functions which do not necessarily exist for all lock kinds.
875 //
876 // The "set" functions usually have wrapper routines that check for a NULL set
877 // function pointer and call it if non-NULL.
878 //
879 // In some cases, it makes sense to have a "get" wrapper function check for a
880 // NULL get function pointer and return NULL / invalid value / error code if
881 // the function pointer is NULL.
882 //
883 // In other cases, the calling code really should differentiate between an
884 // unimplemented function and one that is implemented but returning NULL /
885 // invalied valuature. 00605"> 605 void 1850  , int gtid, kmp_int32 *lhs,
<;
iv class="line">);"keywordtype">int) *pan class="keywordtype">int gtin", lck)); 3650  // element oan>  limit = chunk + init - 1; 111  incr = pr->>
void __kmp_itt_barrierdiv class="line"> 695 __kmp_dispatc name="l008s="line"> 2757#160; __ompt_get_mutex_impl_type(user_lock), (omp_wait_id_t)user_lock // Output character index.
// unimplemented function and one that is implemented but x_impl_type(user_lock), (omp_wait_id_t)user_lock
797  }
void
< lass=/commentKMP_CHECKmUSER_LOCK_INIlass="line"> // There are 5 lock implementations: