LLVM OpenMP* Runtime Library
kmp_lock.h
1 /*
2  * kmp_lock.h -- lock header file
3  */
4 
5 
6 //===----------------------------------------------------------------------===//
7 //
8 // The LLVM Compiler Infrastructure
9 //
10 // This file is dual licensed under the MIT and the University of Illinois Open
11 // Source Licenses. See LICENSE.txt for details.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 
16 #ifndef KMP_LOCK_H
17 #define KMP_LOCK_H
18 
19 #include <limits.h> // CHAR_BIT
20 #include <stddef.h> // offsetof
21 
22 #include "kmp_debug.h"
23 #include "kmp_os.h"
24 
25 #ifdef __cplusplus
26 #include <atomic>
27 
28 extern "C" {
29 #endif // __cplusplus
30 
31 // ----------------------------------------------------------------------------
32 // Have to copy these definitions from kmp.h because kmp.h cannot be included
33 // due to circular dependencies. Will undef these at end of file.
34 
35 #define KMP_PAD(type, sz) \
36  (sizeof(type) + (sz - ((sizeof(type) - 1) % (sz)) - 1))
37 #define KMP_GTID_DNE (-2)
38 
39 // Forward declaration of ident and ident_t
40 
41 struct ident;
42 typedef struct ident ident_t;
43 
44 // End of copied code.
45 // ----------------------------------------------------------------------------
46 
47 // We need to know the size of the area we can assume that the compiler(s)
48 // allocated for obects of type omp_lock_t and omp_nest_lock_t. The Intel
49 // compiler always allocates a pointer-sized area, as does visual studio.
50 //
51 // gcc however, only allocates 4 bytes for regular locks, even on 64-bit
52 // intel archs. It allocates at least 8 bytes for nested lock (more on
53 // recent versions), but we are bounded by the pointer-sized chunks that
54 // the Intel compiler allocates.
55 
56 #if KMP_OS_LINUX && defined(KMP_GOMP_COMPAT)
57 #define OMP_LOCK_T_SIZE sizeof(int)
58 #define OMP_NEST_LOCK_T_SIZE sizeof(void *)
59 #else
60 #define OMP_LOCK_T_SIZE sizeof(void *)
61 #define OMP_NEST_LOCK_T_SIZE sizeof(void *)
62 #endif
63 
64 // The Intel compiler allocates a 32-byte chunk for a critical section.
65 // Both gcc and visual studio only allocate enough space for a pointer.
66 // Sometimes we know that the space was allocated by the Intel compiler.
67 #define OMP_CRITICAL_SIZE sizeof(void *)
68 #define INTEL_CRITICAL_SIZE 32
69 
70 // lock flags
71 typedef kmp_uint32 kmp_lock_flags_t;
72 
73 #define kmp_lf_critical_section 1
74 
75 // When a lock table is used, the indices are of kmp_lock_index_t
76 typedef kmp_uint32 kmp_lock_index_t;
77 
78 // When memory allocated for locks are on the lock pool (free list),
79 // it is treated as structs of this type.
80 struct kmp_lock_pool {
81  union kmp_user_lock *next;
82  kmp_lock_index_t index;
83 };
84 
85 typedef struct kmp_lock_pool kmp_lock_pool_t;
86 
87 extern void __kmp_validate_locks(void);
88 
89 // ----------------------------------------------------------------------------
90 // There are 5 lock implementations:
91 // 1. Test and set locks.
92 // 2. futex locks (Linux* OS on x86 and Intel(R) Many Integrated Core
93 // architecture)
94 // 3. Ticket (Lamport bakery) locks.
95 // 4. Queuing locks (with separate spin fields).
96 // 5. DRPA (Dynamically Reconfigurable Distributed Polling Area) locks
97 //
98 // and 3 lock purposes:
99 // 1. Bootstrap locks -- Used for a few locks available at library
100 // startup-shutdown time.
101 // These do not require non-negative global thread ID's.
102 // 2. Internal RTL locks -- Used everywhere else in the RTL
103 // 3. User locks (includes critical sections)
104 // ----------------------------------------------------------------------------
105 
106 // ============================================================================
107 // Lock implementations.
108 //
109 // Test and set locks.
110 //
111 // Non-nested test and set locks differ from the other lock kinds (except
112 // futex) in that we use the memory allocated by the compiler for the lock,
113 // rather than a pointer to it.
114 //
115 // On lin32, lin_32e, and win_32, the space allocated may be as small as 4
116 // bytes, so we have to use a lock table for nested locks, and avoid accessing
117 // the depth_locked field for non-nested locks.
118 //
119 // Information normally available to the tools, such as lock location, lock
120 // usage (normal lock vs. critical section), etc. is not available with test and
121 // set locks.
122 // ----------------------------------------------------------------------------
123 
124 struct kmp_base_tas_lock {
125  // KMP_LOCK_FREE(tas) => unlocked; locked: (gtid+1) of owning thread
126  volatile kmp_int32 poll;
127  kmp_int32 depth_locked; // depth locked, for nested locks only
128 };
129 
130 typedef struct kmp_base_tas_lock kmp_base_tas_lock_t;
131 
132 union kmp_tas_lock {
133  kmp_base_tas_lock_t lk;
134  kmp_lock_pool_t pool; // make certain struct is large enough
135  double lk_align; // use worst case alignment; no cache line padding
136 };
137 
138 typedef union kmp_tas_lock kmp_tas_lock_t;
139 
140 // Static initializer for test and set lock variables. Usage:
141 // kmp_tas_lock_t xlock = KMP_TAS_LOCK_INITIALIZER( xlock );
142 #define KMP_TAS_LOCK_INITIALIZER(lock) \
143  { \
144  { KMP_LOCK_FREE(tas), 0 } \
145  }
146 
147 extern int __kmp_acquire_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
148 extern int __kmp_test_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
149 extern int __kmp_release_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
150 extern void __kmp_init_tas_lock(kmp_tas_lock_t *lck);
151 extern void __kmp_destroy_tas_lock(kmp_tas_lock_t *lck);
152 
153 extern int __kmp_acquire_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
154 extern int __kmp_test_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
155 extern int __kmp_release_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);
156 extern void __kmp_init_nested_tas_lock(kmp_tas_lock_t *lck);
157 extern void __kmp_destroy_nested_tas_lock(kmp_tas_lock_t *lck);
158 
159 #define KMP_LOCK_RELEASED 1
160 #define KMP_LOCK_STILL_HELD 0
161 #define KMP_LOCK_ACQUIRED_FIRST 1
162 #define KMP_LOCK_ACQUIRED_NEXT 0
163 
164 #define KMP_USE_FUTEX \
165  (KMP_OS_LINUX && !KMP_OS_CNK && \
166  (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64))
167 
168 #if KMP_USE_FUTEX
169 
170 // ----------------------------------------------------------------------------
171 // futex locks. futex locks are only available on Linux* OS.
172 //
173 // Like non-nested test and set lock, non-nested futex locks use the memory
174 // allocated by the compiler for the lock, rather than a pointer to it.
175 //
176 // Information normally available to the tools, such as lock location, lock
177 // usage (normal lock vs. critical section), etc. is not available with test and
178 // set locks. With non-nested futex locks, the lock owner is not even available.
179 // ----------------------------------------------------------------------------
180 
181 struct kmp_base_futex_lock {
182  volatile kmp_int32 poll; // KMP_LOCK_FREE(futex) => unlocked
183  // 2*(gtid+1) of owning thread, 0 if unlocked
184  // locked: (gtid+1) of owning thread
185  kmp_int32 depth_locked; // depth locked, for nested locks only
186 };
187 
188 typedef struct kmp_base_futex_lock kmp_base_futex_lock_t;
189 
190 union kmp_futex_lock {
191  kmp_base_futex_lock_t lk;
192  kmp_lock_pool_t pool; // make certain struct is large enough
193  double lk_align; // use worst case alignment
194  // no cache line padding
195 };
196 
197 typedef union kmp_futex_lock kmp_futex_lock_t;
198 
199 // Static initializer for futex lock variables. Usage:
200 // kmp_futex_lock_t xlock = KMP_FUTEX_LOCK_INITIALIZER( xlock );
201 #define KMP_FUTEX_LOCK_INITIALIZER(lock) \
202  { \
203  { KMP_LOCK_FREE(futex), 0 } \
204  }
205 
206 extern int __kmp_acquire_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);
207 extern int __kmp_test_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);
208 extern int __kmp_release_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);
209 extern void __kmp_init_futex_lock(kmp_futex_lock_t *lck);
210 extern void __kmp_destroy_futex_lock(kmp_futex_lock_t *lck);
211 
212 extern int __kmp_acquire_nested_futex_lock(kmp_futex_lock_t *lck,
213  kmp_int32 gtid);
214 extern int __kmp_test_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);
215 extern int __kmp_release_nested_futex_lock(kmp_futex_lock_t *lck,
216  kmp_int32 gtid);
217 extern void __kmp_init_nested_futex_lock(kmp_futex_lock_t *lck);
218 extern void __kmp_destroy_nested_futex_lock(kmp_futex_lock_t *lck);
219 
220 #endif // KMP_USE_FUTEX
221 
222 // ----------------------------------------------------------------------------
223 // Ticket locks.
224 
225 #ifdef __cplusplus
226 
227 #ifdef _MSC_VER
228 // MSVC won't allow use of std::atomic<> in a union since it has non-trivial
229 // copy constructor.
230 
231 struct kmp_base_ticket_lock {
232  // `initialized' must be the first entry in the lock data structure!
233  std::atomic_bool initialized;
234  volatile union kmp_ticket_lock *self; // points to the lock union
235  ident_t const *location; // Source code location of omp_init_lock().
236  std::atomic_uint
237  next_ticket; // ticket number to give to next thread which acquires
238  std::atomic_uint now_serving; // ticket number for thread which holds the lock
239  std::atomic_int owner_id; // (gtid+1) of owning thread, 0 if unlocked
240  std::atomic_int depth_locked; // depth locked, for nested locks only
241  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
242 };
243 #else
244 struct kmp_base_ticket_lock {
245  // `initialized' must be the first entry in the lock data structure!
246  std::atomic<bool> initialized;
247  volatile union kmp_ticket_lock *self; // points to the lock union
248  ident_t const *location; // Source code location of omp_init_lock().
249  std::atomic<unsigned>
250  next_ticket; // ticket number to give to next thread which acquires
251  std::atomic<unsigned>
252  now_serving; // ticket number for thread which holds the lock
253  std::atomic<int> owner_id; // (gtid+1) of owning thread, 0 if unlocked
254  std::atomic<int> depth_locked; // depth locked, for nested locks only
255  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
256 };
257 #endif
258 
259 #else // __cplusplus
260 
261 struct kmp_base_ticket_lock;
262 
263 #endif // !__cplusplus
264 
265 typedef struct kmp_base_ticket_lock kmp_base_ticket_lock_t;
266 
267 union KMP_ALIGN_CACHE kmp_ticket_lock {
268  kmp_base_ticket_lock_t
269  lk; // This field must be first to allow static initializing.
270  kmp_lock_pool_t pool;
271  double lk_align; // use worst case alignment
272  char lk_pad[KMP_PAD(kmp_base_ticket_lock_t, CACHE_LINE)];
273 };
274 
275 typedef union kmp_ticket_lock kmp_ticket_lock_t;
276 
277 // Static initializer for simple ticket lock variables. Usage:
278 // kmp_ticket_lock_t xlock = KMP_TICKET_LOCK_INITIALIZER( xlock );
279 // Note the macro argument. It is important to make var properly initialized.
280 #define KMP_TICKET_LOCK_INITIALIZER(lock) \
281  { \
282  { \
283  ATOMIC_VAR_INIT(true) \
284  , &(lock), NULL, ATOMIC_VAR_INIT(0U), ATOMIC_VAR_INIT(0U), \
285  ATOMIC_VAR_INIT(0), ATOMIC_VAR_INIT(-1) \
286  } \
287  }
288 
289 extern int __kmp_acquire_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid);
290 extern int __kmp_test_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid);
291 extern int __kmp_test_ticket_lock_with_cheks(kmp_ticket_lock_t *lck,
292  kmp_int32 gtid);
293 extern int __kmp_release_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid);
294 extern void __kmp_init_ticket_lock(kmp_ticket_lock_t *lck);
295 extern void __kmp_destroy_ticket_lock(kmp_ticket_lock_t *lck);
296 
297 extern int __kmp_acquire_nested_ticket_lock(kmp_ticket_lock_t *lck,
298  kmp_int32 gtid);
299 extern int __kmp_test_nested_ticket_lock(kmp_ticket_lock_t *lck,
300  kmp_int32 gtid);
301 extern int __kmp_release_nested_ticket_lock(kmp_ticket_lock_t *lck,
302  kmp_int32 gtid);
303 extern void __kmp_init_nested_ticket_lock(kmp_ticket_lock_t *lck);
304 extern void __kmp_destroy_nested_ticket_lock(kmp_ticket_lock_t *lck);
305 
306 // ----------------------------------------------------------------------------
307 // Queuing locks.
308 
309 #if KMP_USE_ADAPTIVE_LOCKS
310 
311 struct kmp_adaptive_lock_info;
312 
313 typedef struct kmp_adaptive_lock_info kmp_adaptive_lock_info_t;
314 
315 #if KMP_DEBUG_ADAPTIVE_LOCKS
316 
317 struct kmp_adaptive_lock_statistics {
318  /* So we can get stats from locks that haven't been destroyed. */
319  kmp_adaptive_lock_info_t *next;
320  kmp_adaptive_lock_info_t *prev;
321 
322  /* Other statistics */
323  kmp_uint32 successfulSpeculations;
324  kmp_uint32 hardFailedSpeculations;
325  kmp_uint32 softFailedSpeculations;
326  kmp_uint32 nonSpeculativeAcquires;
327  kmp_uint32 nonSpeculativeAcquireAttempts;
328  kmp_uint32 lemmingYields;
329 };
330 
331 typedef struct kmp_adaptive_lock_statistics kmp_adaptive_lock_statistics_t;
332 
333 extern void __kmp_print_speculative_stats();
334 extern void __kmp_init_speculative_stats();
335 
336 #endif // KMP_DEBUG_ADAPTIVE_LOCKS
337 
338 struct kmp_adaptive_lock_info {
339  /* Values used for adaptivity.
340  Although these are accessed from multiple threads we don't access them
341  atomically, because if we miss updates it probably doesn't matter much. (It
342  just affects our decision about whether to try speculation on the lock). */
343  kmp_uint32 volatile badness;
344  kmp_uint32 volatile acquire_attempts;
345  /* Parameters of the lock. */
346  kmp_uint32 max_badness;
347  kmp_uint32 max_soft_retries;
348 
349 #if KMP_DEBUG_ADAPTIVE_LOCKS
350  kmp_adaptive_lock_statistics_t volatile stats;
351 #endif
352 };
353 
354 #endif // KMP_USE_ADAPTIVE_LOCKS
355 
356 struct kmp_base_queuing_lock {
357 
358  // `initialized' must be the first entry in the lock data structure!
359  volatile union kmp_queuing_lock
360  *initialized; // Points to the lock union if in initialized state.
361 
362  ident_t const *location; // Source code location of omp_init_lock().
363 
364  KMP_ALIGN(8) // tail_id must be 8-byte aligned!
365 
366  volatile kmp_int32
367  tail_id; // (gtid+1) of thread at tail of wait queue, 0 if empty
368  // Must be no padding here since head/tail used in 8-byte CAS
369  volatile kmp_int32
370  head_id; // (gtid+1) of thread at head of wait queue, 0 if empty
371  // Decl order assumes little endian
372  // bakery-style lock
373  volatile kmp_uint32
374  next_ticket; // ticket number to give to next thread which acquires
375  volatile kmp_uint32
376  now_serving; // ticket number for thread which holds the lock
377  volatile kmp_int32 owner_id; // (gtid+1) of owning thread, 0 if unlocked
378  kmp_int32 depth_locked; // depth locked, for nested locks only
379 
380  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
381 };
382 
383 typedef struct kmp_base_queuing_lock kmp_base_queuing_lock_t;
384 
385 KMP_BUILD_ASSERT(offsetof(kmp_base_queuing_lock_t, tail_id) % 8 == 0);
386 
387 union KMP_ALIGN_CACHE kmp_queuing_lock {
388  kmp_base_queuing_lock_t
389  lk; // This field must be first to allow static initializing.
390  kmp_lock_pool_t pool;
391  double lk_align; // use worst case alignment
392  char lk_pad[KMP_PAD(kmp_base_queuing_lock_t, CACHE_LINE)];
393 };
394 
395 typedef union kmp_queuing_lock kmp_queuing_lock_t;
396 
397 extern int __kmp_acquire_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid);
398 extern int __kmp_test_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid);
399 extern int __kmp_release_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid);
400 extern void __kmp_init_queuing_lock(kmp_queuing_lock_t *lck);
401 extern void __kmp_destroy_queuing_lock(kmp_queuing_lock_t *lck);
402 
403 extern int __kmp_acquire_nested_queuing_lock(kmp_queuing_lock_t *lck,
404  kmp_int32 gtid);
405 extern int __kmp_test_nested_queuing_lock(kmp_queuing_lock_t *lck,
406  kmp_int32 gtid);
407 extern int __kmp_release_nested_queuing_lock(kmp_queuing_lock_t *lck,
408  kmp_int32 gtid);
409 extern void __kmp_init_nested_queuing_lock(kmp_queuing_lock_t *lck);
410 extern void __kmp_destroy_nested_queuing_lock(kmp_queuing_lock_t *lck);
411 
412 #if KMP_USE_ADAPTIVE_LOCKS
413 
414 // ----------------------------------------------------------------------------
415 // Adaptive locks.
416 struct kmp_base_adaptive_lock {
417  kmp_base_queuing_lock qlk;
418  KMP_ALIGN(CACHE_LINE)
419  kmp_adaptive_lock_info_t
420  adaptive; // Information for the speculative adaptive lock
421 };
422 
423 typedef struct kmp_base_adaptive_lock kmp_base_adaptive_lock_t;
424 
425 union KMP_ALIGN_CACHE kmp_adaptive_lock {
426  kmp_base_adaptive_lock_t lk;
427  kmp_lock_pool_t pool;
428  double lk_align;
429  char lk_pad[KMP_PAD(kmp_base_adaptive_lock_t, CACHE_LINE)];
430 };
431 typedef union kmp_adaptive_lock kmp_adaptive_lock_t;
432 
433 #define GET_QLK_PTR(l) ((kmp_queuing_lock_t *)&(l)->lk.qlk)
434 
435 #endif // KMP_USE_ADAPTIVE_LOCKS
436 
437 // ----------------------------------------------------------------------------
438 // DRDPA ticket locks.
439 struct kmp_base_drdpa_lock {
440  // All of the fields on the first cache line are only written when
441  // initializing or reconfiguring the lock. These are relatively rare
442  // operations, so data from the first cache line will usually stay resident in
443  // the cache of each thread trying to acquire the lock.
444  //
445  // initialized must be the first entry in the lock data structure!
446  KMP_ALIGN_CACHE
447 
448  volatile union kmp_drdpa_lock
449  *initialized; // points to the lock union if in initialized state
450  ident_t const *location; // Source code location of omp_init_lock().
451  volatile struct kmp_lock_poll { kmp_uint64 poll; } * volatile polls;
452  volatile kmp_uint64 mask; // is 2**num_polls-1 for mod op
453  kmp_uint64 cleanup_ticket; // thread with cleanup ticket
454  volatile struct kmp_lock_poll *old_polls; // will deallocate old_polls
455  kmp_uint32 num_polls; // must be power of 2
456 
457  // next_ticket it needs to exist in a separate cache line, as it is
458  // invalidated every time a thread takes a new ticket.
459  KMP_ALIGN_CACHE
460 
461  volatile kmp_uint64 next_ticket;
462 
463  // now_serving is used to store our ticket value while we hold the lock. It
464  // has a slightly different meaning in the DRDPA ticket locks (where it is
465  // written by the acquiring thread) than it does in the simple ticket locks
466  // (where it is written by the releasing thread).
467  //
468  // Since now_serving is only read an written in the critical section,
469  // it is non-volatile, but it needs to exist on a separate cache line,
470  // as it is invalidated at every lock acquire.
471  //
472  // Likewise, the vars used for nested locks (owner_id and depth_locked) are
473  // only written by the thread owning the lock, so they are put in this cache
474  // line. owner_id is read by other threads, so it must be declared volatile.
475  KMP_ALIGN_CACHE
476  kmp_uint64 now_serving; // doesn't have to be volatile
477  volatile kmp_uint32 owner_id; // (gtid+1) of owning thread, 0 if unlocked
478  kmp_int32 depth_locked; // depth locked
479  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
480 };
481 
482 typedef struct kmp_base_drdpa_lock kmp_base_drdpa_lock_t;
483 
484 union KMP_ALIGN_CACHE kmp_drdpa_lock {
485  kmp_base_drdpa_lock_t
486  lk; // This field must be first to allow static initializing. */
487  kmp_lock_pool_t pool;
488  double lk_align; // use worst case alignment
489  char lk_pad[KMP_PAD(kmp_base_drdpa_lock_t, CACHE_LINE)];
490 };
491 
492 typedef union kmp_drdpa_lock kmp_drdpa_lock_t;
493 
494 extern int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);
495 extern int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);
496 extern int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);
497 extern void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck);
498 extern void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck);
499 
500 extern int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck,
501  kmp_int32 gtid);
502 extern int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);
503 extern int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck,
504  kmp_int32 gtid);
505 extern void __kmp_init_nested_drdpa_lock(kmp_drdpa_lock_t *lck);
506 extern void __kmp_destroy_nested_drdpa_lock(kmp_drdpa_lock_t *lck);
507 
508 // ============================================================================
509 // Lock purposes.
510 // ============================================================================
511 
512 // Bootstrap locks.
513 //
514 // Bootstrap locks -- very few locks used at library initialization time.
515 // Bootstrap locks are currently implemented as ticket locks.
516 // They could also be implemented as test and set lock, but cannot be
517 // implemented with other lock kinds as they require gtids which are not
518 // available at initialization time.
519 
520 typedef kmp_ticket_lock_t kmp_bootstrap_lock_t;
521 
522 #define KMP_BOOTSTRAP_LOCK_INITIALIZER(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 static inline int __kmp_acquire_lock(kmp_lock_t *lck, kmp_int32 gtid) {
555  return __kmp_acquire_ticket_lock(lck, gtid);
556 }
557 
558 static inline int __kmp_test_lock(kmp_lock_t *lck, kmp_int32 gtid) {
559  return __kmp_test_ticket_lock(lck, gtid);
560 }
561 
562 static inline void __kmp_release_lock(kmp_lock_t *lck, kmp_int32 gtid) {
563  __kmp_release_ticket_lock(lck, gtid);
564 }
565 
566 static inline void __kmp_init_lock(kmp_lock_t *lck) {
567  __kmp_init_ticket_lock(lck);
568 }
569 
570 static inline void __kmp_destroy_lock(kmp_lock_t *lck) {
571  __kmp_destroy_ticket_lock(lck);
572 }
573 
574 // User locks.
575 //
576 // Do not allocate objects of type union kmp_user_lock!!! This will waste space
577 // unless __kmp_user_lock_kind == lk_drdpa. Instead, check the value of
578 // __kmp_user_lock_kind and allocate objects of the type of the appropriate
579 // union member, and cast their addresses to kmp_user_lock_p.
580 
581 enum kmp_lock_kind {
582  lk_default = 0,
583  lk_tas,
584 #if KMP_USE_FUTEX
585  lk_futex,
586 #endif
587 #if KMP_USE_DYNAMIC_LOCK && KMP_USE_TSX
588  lk_hle,
589  lk_rtm,
590 #endif
591  lk_ticket,
592  lk_queuing,
593  lk_drdpa,
594 #if KMP_USE_ADAPTIVE_LOCKS
595  lk_adaptive
596 #endif // KMP_USE_ADAPTIVE_LOCKS
597 };
598 
599 typedef enum kmp_lock_kind kmp_lock_kind_t;
600 
601 extern kmp_lock_kind_t __kmp_user_lock_kind;
602 
603 union kmp_user_lock {
604  kmp_tas_lock_t tas;
605 #if KMP_USE_FUTEX
606  kmp_futex_lock_t futex;
607 #endif
608  kmp_ticket_lock_t ticket;
609  kmp_queuing_lock_t queuing;
610  kmp_drdpa_lock_t drdpa;
611 #if KMP_USE_ADAPTIVE_LOCKS
612  kmp_adaptive_lock_t adaptive;
613 #endif // KMP_USE_ADAPTIVE_LOCKS
614  kmp_lock_pool_t pool;
615 };
616 
617 typedef union kmp_user_lock *kmp_user_lock_p;
618 
619 #if !KMP_USE_DYNAMIC_LOCK
620 
621 extern size_t __kmp_base_user_lock_size;
622 extern size_t __kmp_user_lock_size;
623 
624 extern kmp_int32 (*__kmp_get_user_lock_owner_)(kmp_user_lock_p lck);
625 
626 static inline kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p lck) {
627  KMP_DEBUG_ASSERT(__kmp_get_user_lock_owner_ != NULL);
628  return (*__kmp_get_user_lock_owner_)(lck);
629 }
630 
631 extern int (*__kmp_acquire_user_lock_with_checks_)(kmp_user_lock_p lck,
632  kmp_int32 gtid);
633 
634 #if KMP_OS_LINUX && \
635  (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
636 
637 #define __kmp_acquire_user_lock_with_checks(lck, gtid) \
638  if (__kmp_user_lock_kind == lk_tas) { \
639  if (__kmp_env_consistency_check) { \
640  char const *const func = "omp_set_lock"; \
641  if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) && \
642  lck->tas.lk.depth_locked != -1) { \
643  KMP_FATAL(LockNestableUsedAsSimple, func); \
644  } \
645  if ((gtid >= 0) && (lck->tas.lk.poll - 1 == gtid)) { \
646  KMP_FATAL(LockIsAlreadyOwned, func); \
647  } \
648  } \
649  if ((lck->tas.lk.poll != 0) || \
650  (!KMP_COMPARE_AND_STORE_ACQ32(&(lck->tas.lk.poll), 0, gtid + 1))) { \
651  kmp_uint32 spins; \
652  KMP_FSYNC_PREPARE(lck); \
653  KMP_INIT_YIELD(spins); \
654  if (TCR_4(__kmp_nth) > \
655  (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) { \
656  KMP_YIELD(TRUE); \
657  } else { \
658  KMP_YIELD_SPIN(spins); \
659  } \
660  while ( \
661  (lck->tas.lk.poll != 0) || \
662  (!KMP_COMPARE_AND_STORE_ACQ32(&(lck->tas.lk.poll), 0, gtid + 1))) { \
663  if (TCR_4(__kmp_nth) > \
664  (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) { \
665  KMP_YIELD(TRUE); \
666  } else { \
667  KMP_YIELD_SPIN(spins); \
668  } \
669  } \
670  } \
671  KMP_FSYNC_ACQUIRED(lck); \
672  } else { \
673  KMP_DEBUG_ASSERT(__kmp_acquire_user_lock_with_checks_ != NULL); \
674  (*__kmp_acquire_user_lock_with_checks_)(lck, gtid); \
675  }
676 
677 #else
678 static inline int __kmp_acquire_user_lock_with_checks(kmp_user_lock_p lck,
679  kmp_int32 gtid) {
680  KMP_DEBUG_ASSERT(__kmp_acquire_user_lock_with_checks_ != NULL);
681  return (*__kmp_acquire_user_lock_with_checks_)(lck, gtid);
682 }
683 #endif
684 
685 extern int (*__kmp_test_user_lock_with_checks_)(kmp_user_lock_p lck,
686  kmp_int32 gtid);
687 
688 #if KMP_OS_LINUX && \
689  (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
690 
691 #include "kmp_i18n.h" /* AC: KMP_FATAL definition */
692 extern int __kmp_env_consistency_check; /* AC: copy from kmp.h here */
693 static inline int __kmp_test_user_lock_with_checks(kmp_user_lock_p lck,
694  kmp_int32 gtid) {
695  if (__kmp_user_lock_kind == lk_tas) {
696  if (__kmp_env_consistency_check) {
697  char const *const func = "omp_test_lock";
698  if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
699  lck->tas.lk.depth_locked != -1) {
700  KMP_FATAL(LockNestableUsedAsSimple, func);
701  }
702  }
703  return ((lck->tas.lk.poll == 0) &&
704  KMP_COMPARE_AND_STORE_ACQ32(&(lck->tas.lk.poll), 0, gtid + 1));
705  } else {
706  KMP_DEBUG_ASSERT(__kmp_test_user_lock_with_checks_ != NULL);
707  return (*__kmp_test_user_lock_with_checks_)(lck, gtid);
708  }
709 }
710 #else
711 static inline int __kmp_test_user_lock_with_checks(kmp_user_lock_p lck,
712  kmp_int32 gtid) {
713  KMP_DEBUG_ASSERT(__kmp_test_user_lock_with_checks_ != NULL);
714  return (*__kmp_test_user_lock_with_checks_)(lck, gtid);
715 }
716 #endif
717 
718 extern int (*__kmp_release_user_lock_with_checks_)(kmp_user_lock_p lck,
719  kmp_int32 gtid);
720 
721 static inline void __kmp_release_user_lock_with_checks(kmp_user_lock_p lck,
722  kmp_int32 gtid) {
723  KMP_DEBUG_ASSERT(__kmp_release_user_lock_with_checks_ != NULL);
724  (*__kmp_release_user_lock_with_checks_)(lck, gtid);
725 }
726 
727 extern void (*__kmp_init_user_lock_with_checks_)(kmp_user_lock_p lck);
728 
729 static inline void __kmp_init_user_lock_with_checks(kmp_user_lock_p lck) {
730  KMP_DEBUG_ASSERT(__kmp_init_user_lock_with_checks_ != NULL);
731  (*__kmp_init_user_lock_with_checks_)(lck);
732 }
733 
734 // We need a non-checking version of destroy lock for when the RTL is
735 // doing the cleanup as it can't always tell if the lock is nested or not.
736 extern void (*__kmp_destroy_user_lock_)(kmp_user_lock_p lck);
737 
738 static inline void __kmp_destroy_user_lock(kmp_user_lock_p lck) {
739  KMP_DEBUG_ASSERT(__kmp_destroy_user_lock_ != NULL);
740  (*__kmp_destroy_user_lock_)(lck);
741 }
742 
743 extern void (*__kmp_destroy_user_lock_with_checks_)(kmp_user_lock_p lck);
744 
745 static inline void __kmp_destroy_user_lock_with_checks(kmp_user_lock_p lck) {
746  KMP_DEBUG_ASSERT(__kmp_destroy_user_lock_with_checks_ != NULL);
747  (*__kmp_destroy_user_lock_with_checks_)(lck);
748 }
749 
750 extern int (*__kmp_acquire_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
751  kmp_int32 gtid);
752 
753 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)
754 
755 #define __kmp_acquire_nested_user_lock_with_checks(lck, gtid, depth) \
756  if (__kmp_user_lock_kind == lk_tas) { \
757  if (__kmp_env_consistency_check) { \
758  char const *const func = "omp_set_nest_lock"; \
759  if ((sizeof(kmp_tas_lock_t) <= OMP_NEST_LOCK_T_SIZE) && \
760  lck->tas.lk.depth_locked == -1) { \
761  KMP_FATAL(LockSimpleUsedAsNestable, func); \
762  } \
763  } \
764  if (lck->tas.lk.poll - 1 == gtid) { \
765  lck->tas.lk.depth_locked += 1; \
766  *depth = KMP_LOCK_ACQUIRED_NEXT; \
767  } else { \
768  if ((lck->tas.lk.poll != 0) || \
769  (!KMP_COMPARE_AND_STORE_ACQ32(&(lck->tas.lk.poll), 0, gtid + 1))) { \
770  kmp_uint32 spins; \
771  KMP_FSYNC_PREPARE(lck); \
772  KMP_INIT_YIELD(spins); \
773  if (TCR_4(__kmp_nth) > \
774  (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) { \
775  KMP_YIELD(TRUE); \
776  } else { \
777  KMP_YIELD_SPIN(spins); \
778  } \
779  while ((lck->tas.lk.poll != 0) || \
780  (!KMP_COMPARE_AND_STORE_ACQ32(&(lck->tas.lk.poll), 0, \
781  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_ACQ32(&(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 value. If this is the case, no get function wrapper exists.
886 
887 extern int (*__kmp_is_user_lock_initialized_)(kmp_user_lock_p lck);
888 
889 // no set function; fields set durining local allocation
890 
891 extern const ident_t *(*__kmp_get_user_lock_location_)(kmp_user_lock_p lck);
892 
893 static inline const ident_t *__kmp_get_user_lock_location(kmp_user_lock_p lck) {
894  if (__kmp_get_user_lock_location_ != NULL) {
895  return (*__kmp_get_user_lock_location_)(lck);
896  } else {
897  return NULL;
898  }
899 }
900 
901 extern void (*__kmp_set_user_lock_location_)(kmp_user_lock_p lck,
902  const ident_t *loc);
903 
904 static inline void __kmp_set_user_lock_location(kmp_user_lock_p lck,
905  const ident_t *loc) {
906  if (__kmp_set_user_lock_location_ != NULL) {
907  (*__kmp_set_user_lock_location_)(lck, loc);
908  }
909 }
910 
911 extern kmp_lock_flags_t (*__kmp_get_user_lock_flags_)(kmp_user_lock_p lck);
912 
913 extern void (*__kmp_set_user_lock_flags_)(kmp_user_lock_p lck,
914  kmp_lock_flags_t flags);
915 
916 static inline void __kmp_set_user_lock_flags(kmp_user_lock_p lck,
917  kmp_lock_flags_t flags) {
918  if (__kmp_set_user_lock_flags_ != NULL) {
919  (*__kmp_set_user_lock_flags_)(lck, flags);
920  }
921 }
922 
923 // The fuction which sets up all of the vtbl pointers for kmp_user_lock_t.
924 extern void __kmp_set_user_lock_vptrs(kmp_lock_kind_t user_lock_kind);
925 
926 // Macros for binding user lock functions.
927 #define KMP_BIND_USER_LOCK_TEMPLATE(nest, kind, suffix) \
928  { \
929  __kmp_acquire##nest##user_lock_with_checks_ = (int (*)( \
930  kmp_user_lock_p, kmp_int32))__kmp_acquire##nest##kind##_##suffix; \
931  __kmp_release##nest##user_lock_with_checks_ = (int (*)( \
932  kmp_user_lock_p, kmp_int32))__kmp_release##nest##kind##_##suffix; \
933  __kmp_test##nest##user_lock_with_checks_ = (int (*)( \
934  kmp_user_lock_p, kmp_int32))__kmp_test##nest##kind##_##suffix; \
935  __kmp_init##nest##user_lock_with_checks_ = \
936  (void (*)(kmp_user_lock_p))__kmp_init##nest##kind##_##suffix; \
937  __kmp_destroy##nest##user_lock_with_checks_ = \
938  (void (*)(kmp_user_lock_p))__kmp_destroy##nest##kind##_##suffix; \
939  }
940 
941 #define KMP_BIND_USER_LOCK(kind) KMP_BIND_USER_LOCK_TEMPLATE(_, kind, lock)
942 #define KMP_BIND_USER_LOCK_WITH_CHECKS(kind) \
943  KMP_BIND_USER_LOCK_TEMPLATE(_, kind, lock_with_checks)
944 #define KMP_BIND_NESTED_USER_LOCK(kind) \
945  KMP_BIND_USER_LOCK_TEMPLATE(_nested_, kind, lock)
946 #define KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(kind) \
947  KMP_BIND_USER_LOCK_TEMPLATE(_nested_, kind, lock_with_checks)
948 
949 // User lock table & lock allocation
950 /* On 64-bit Linux* OS (and OS X*) GNU compiler allocates only 4 bytems memory
951  for lock variable, which is not enough to store a pointer, so we have to use
952  lock indexes instead of pointers and maintain lock table to map indexes to
953  pointers.
954 
955 
956  Note: The first element of the table is not a pointer to lock! It is a
957  pointer to previously allocated table (or NULL if it is the first table).
958 
959  Usage:
960 
961  if ( OMP_LOCK_T_SIZE < sizeof( <lock> ) ) { // or OMP_NEST_LOCK_T_SIZE
962  Lock table is fully utilized. User locks are indexes, so table is used on
963  user lock operation.
964  Note: it may be the case (lin_32) that we don't need to use a lock
965  table for regular locks, but do need the table for nested locks.
966  }
967  else {
968  Lock table initialized but not actually used.
969  }
970 */
971 
972 struct kmp_lock_table {
973  kmp_lock_index_t used; // Number of used elements
974  kmp_lock_index_t allocated; // Number of allocated elements
975  kmp_user_lock_p *table; // Lock table.
976 };
977 
978 typedef struct kmp_lock_table kmp_lock_table_t;
979 
980 extern kmp_lock_table_t __kmp_user_lock_table;
981 extern kmp_user_lock_p __kmp_lock_pool;
982 
983 struct kmp_block_of_locks {
984  struct kmp_block_of_locks *next_block;
985  void *locks;
986 };
987 
988 typedef struct kmp_block_of_locks kmp_block_of_locks_t;
989 
990 extern kmp_block_of_locks_t *__kmp_lock_blocks;
991 extern int __kmp_num_locks_in_block;
992 
993 extern kmp_user_lock_p __kmp_user_lock_allocate(void **user_lock,
994  kmp_int32 gtid,
995  kmp_lock_flags_t flags);
996 extern void __kmp_user_lock_free(void **user_lock, kmp_int32 gtid,
997  kmp_user_lock_p lck);
998 extern kmp_user_lock_p __kmp_lookup_user_lock(void **user_lock,
999  char const *func);
1000 extern void __kmp_cleanup_user_locks();
1001 
1002 #define KMP_CHECK_USER_LOCK_INIT() \
1003  { \
1004  if (!TCR_4(__kmp_init_user_locks)) { \
1005  __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); \
1006  if (!TCR_4(__kmp_init_user_locks)) { \
1007  TCW_4(__kmp_init_user_locks, TRUE); \
1008  } \
1009  __kmp_release_bootstrap_lock(&__kmp_initz_lock); \
1010  } \
1011  }
1012 
1013 #endif // KMP_USE_DYNAMIC_LOCK
1014 
1015 #undef KMP_PAD
1016 #undef KMP_GTID_DNE
1017 
1018 #if KMP_USE_DYNAMIC_LOCK
1019 // KMP_USE_DYNAMIC_LOCK enables dynamic dispatch of lock functions without
1020 // breaking the current compatibility. Essential functionality of this new code
1021 // is dynamic dispatch, but it also implements (or enables implementation of)
1022 // hinted user lock and critical section which will be part of OMP 4.5 soon.
1023 //
1024 // Lock type can be decided at creation time (i.e., lock initialization), and
1025 // subsequent lock function call on the created lock object requires type
1026 // extraction and call through jump table using the extracted type. This type
1027 // information is stored in two different ways depending on the size of the lock
1028 // object, and we differentiate lock types by this size requirement - direct and
1029 // indirect locks.
1030 //
1031 // Direct locks:
1032 // A direct lock object fits into the space created by the compiler for an
1033 // omp_lock_t object, and TAS/Futex lock falls into this category. We use low
1034 // one byte of the lock object as the storage for the lock type, and appropriate
1035 // bit operation is required to access the data meaningful to the lock
1036 // algorithms. Also, to differentiate direct lock from indirect lock, 1 is
1037 // written to LSB of the lock object. The newly introduced "hle" lock is also a
1038 // direct lock.
1039 //
1040 // Indirect locks:
1041 // An indirect lock object requires more space than the compiler-generated
1042 // space, and it should be allocated from heap. Depending on the size of the
1043 // compiler-generated space for the lock (i.e., size of omp_lock_t), this
1044 // omp_lock_t object stores either the address of the heap-allocated indirect
1045 // lock (void * fits in the object) or an index to the indirect lock table entry
1046 // that holds the address. Ticket/Queuing/DRDPA/Adaptive lock falls into this
1047 // category, and the newly introduced "rtm" lock is also an indirect lock which
1048 // was implemented on top of the Queuing lock. When the omp_lock_t object holds
1049 // an index (not lock address), 0 is written to LSB to differentiate the lock
1050 // from a direct lock, and the remaining part is the actual index to the
1051 // indirect lock table.
1052 
1053 #include <stdint.h> // for uintptr_t
1054 
1055 // Shortcuts
1056 #define KMP_USE_INLINED_TAS \
1057  (KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM)) && 1
1058 #define KMP_USE_INLINED_FUTEX KMP_USE_FUTEX && 0
1059 
1060 // List of lock definitions; all nested locks are indirect locks.
1061 // hle lock is xchg lock prefixed with XACQUIRE/XRELEASE.
1062 // All nested locks are indirect lock types.
1063 #if KMP_USE_TSX
1064 #if KMP_USE_FUTEX
1065 #define KMP_FOREACH_D_LOCK(m, a) m(tas, a) m(futex, a) m(hle, a)
1066 #define KMP_FOREACH_I_LOCK(m, a) \
1067  m(ticket, a) m(queuing, a) m(adaptive, a) m(drdpa, a) m(rtm, a) \
1068  m(nested_tas, a) m(nested_futex, a) m(nested_ticket, a) \
1069  m(nested_queuing, a) m(nested_drdpa, a)
1070 #else
1071 #define KMP_FOREACH_D_LOCK(m, a) m(tas, a) m(hle, a)
1072 #define KMP_FOREACH_I_LOCK(m, a) \
1073  m(ticket, a) m(queuing, a) m(adaptive, a) m(drdpa, a) m(rtm, a) \
1074  m(nested_tas, a) m(nested_ticket, a) m(nested_queuing, a) \
1075  m(nested_drdpa, a)
1076 #endif // KMP_USE_FUTEX
1077 #define KMP_LAST_D_LOCK lockseq_hle
1078 #else
1079 #if KMP_USE_FUTEX
1080 #define KMP_FOREACH_D_LOCK(m, a) m(tas, a) m(futex, a)
1081 #define KMP_FOREACH_I_LOCK(m, a) \
1082  m(ticket, a) m(queuing, a) m(drdpa, a) m(nested_tas, a) m(nested_futex, a) \
1083  m(nested_ticket, a) m(nested_queuing, a) m(nested_drdpa, a)
1084 #define KMP_LAST_D_LOCK lockseq_futex
1085 #else
1086 #define KMP_FOREACH_D_LOCK(m, a) m(tas, a)
1087 #define KMP_FOREACH_I_LOCK(m, a) \
1088  m(ticket, a) m(queuing, a) m(drdpa, a) m(nested_tas, a) m(nested_ticket, a) \
1089  m(nested_queuing, a) m(nested_drdpa, a)
1090 #define KMP_LAST_D_LOCK lockseq_tas
1091 #endif // KMP_USE_FUTEX
1092 #endif // KMP_USE_TSX
1093 
1094 // Information used in dynamic dispatch
1095 #define KMP_LOCK_SHIFT \
1096  8 // number of low bits to be used as tag for direct locks
1097 #define KMP_FIRST_D_LOCK lockseq_tas
1098 #define KMP_FIRST_I_LOCK lockseq_ticket
1099 #define KMP_LAST_I_LOCK lockseq_nested_drdpa
1100 #define KMP_NUM_I_LOCKS \
1101  (locktag_nested_drdpa + 1) // number of indirect lock types
1102 
1103 // Base type for dynamic locks.
1104 typedef kmp_uint32 kmp_dyna_lock_t;
1105 
1106 // Lock sequence that enumerates all lock kinds. Always make this enumeration
1107 // consistent with kmp_lockseq_t in the include directory.
1108 typedef enum {
1109  lockseq_indirect = 0,
1110 #define expand_seq(l, a) lockseq_##l,
1111  KMP_FOREACH_D_LOCK(expand_seq, 0) KMP_FOREACH_I_LOCK(expand_seq, 0)
1112 #undef expand_seq
1113 } kmp_dyna_lockseq_t;
1114 
1115 // Enumerates indirect lock tags.
1116 typedef enum {
1117 #define expand_tag(l, a) locktag_##l,
1118  KMP_FOREACH_I_LOCK(expand_tag, 0)
1119 #undef expand_tag
1120 } kmp_indirect_locktag_t;
1121 
1122 // Utility macros that extract information from lock sequences.
1123 #define KMP_IS_D_LOCK(seq) \
1124  ((seq) >= KMP_FIRST_D_LOCK && (seq) <= KMP_LAST_D_LOCK)
1125 #define KMP_IS_I_LOCK(seq) \
1126  ((seq) >= KMP_FIRST_I_LOCK && (seq) <= KMP_LAST_I_LOCK)
1127 #define KMP_GET_I_TAG(seq) (kmp_indirect_locktag_t)((seq)-KMP_FIRST_I_LOCK)
1128 #define KMP_GET_D_TAG(seq) ((seq) << 1 | 1)
1129 
1130 // Enumerates direct lock tags starting from indirect tag.
1131 typedef enum {
1132 #define expand_tag(l, a) locktag_##l = KMP_GET_D_TAG(lockseq_##l),
1133  KMP_FOREACH_D_LOCK(expand_tag, 0)
1134 #undef expand_tag
1135 } kmp_direct_locktag_t;
1136 
1137 // Indirect lock type
1138 typedef struct {
1139  kmp_user_lock_p lock;
1140  kmp_indirect_locktag_t type;
1141 } kmp_indirect_lock_t;
1142 
1143 // Function tables for direct locks. Set/unset/test differentiate functions
1144 // with/without consistency checking.
1145 extern void (*__kmp_direct_init[])(kmp_dyna_lock_t *, kmp_dyna_lockseq_t);
1146 extern void (*__kmp_direct_destroy[])(kmp_dyna_lock_t *);
1147 extern void (*(*__kmp_direct_set))(kmp_dyna_lock_t *, kmp_int32);
1148 extern int (*(*__kmp_direct_unset))(kmp_dyna_lock_t *, kmp_int32);
1149 extern int (*(*__kmp_direct_test))(kmp_dyna_lock_t *, kmp_int32);
1150 
1151 // Function tables for indirect locks. Set/unset/test differentiate functions
1152 // with/withuot consistency checking.
1153 extern void (*__kmp_indirect_init[])(kmp_user_lock_p);
1154 extern void (*__kmp_indirect_destroy[])(kmp_user_lock_p);
1155 extern void (*(*__kmp_indirect_set))(kmp_user_lock_p, kmp_int32);
1156 extern int (*(*__kmp_indirect_unset))(kmp_user_lock_p, kmp_int32);
1157 extern int (*(*__kmp_indirect_test))(kmp_user_lock_p, kmp_int32);
1158 
1159 // Extracts direct lock tag from a user lock pointer
1160 #define KMP_EXTRACT_D_TAG(l) \
1161  (*((kmp_dyna_lock_t *)(l)) & ((1 << KMP_LOCK_SHIFT) - 1) & \
1162  -(*((kmp_dyna_lock_t *)(l)) & 1))
1163 
1164 // Extracts indirect lock index from a user lock pointer
1165 #define KMP_EXTRACT_I_INDEX(l) (*(kmp_lock_index_t *)(l) >> 1)
1166 
1167 // Returns function pointer to the direct lock function with l (kmp_dyna_lock_t
1168 // *) and op (operation type).
1169 #define KMP_D_LOCK_FUNC(l, op) __kmp_direct_##op[KMP_EXTRACT_D_TAG(l)]
1170 
1171 // Returns function pointer to the indirect lock function with l
1172 // (kmp_indirect_lock_t *) and op (operation type).
1173 #define KMP_I_LOCK_FUNC(l, op) \
1174  __kmp_indirect_##op[((kmp_indirect_lock_t *)(l))->type]
1175 
1176 // Initializes a direct lock with the given lock pointer and lock sequence.
1177 #define KMP_INIT_D_LOCK(l, seq) \
1178  __kmp_direct_init[KMP_GET_D_TAG(seq)]((kmp_dyna_lock_t *)l, seq)
1179 
1180 // Initializes an indirect lock with the given lock pointer and lock sequence.
1181 #define KMP_INIT_I_LOCK(l, seq) \
1182  __kmp_direct_init[0]((kmp_dyna_lock_t *)(l), seq)
1183 
1184 // Returns "free" lock value for the given lock type.
1185 #define KMP_LOCK_FREE(type) (locktag_##type)
1186 
1187 // Returns "busy" lock value for the given lock teyp.
1188 #define KMP_LOCK_BUSY(v, type) ((v) << KMP_LOCK_SHIFT | locktag_##type)
1189 
1190 // Returns lock value after removing (shifting) lock tag.
1191 #define KMP_LOCK_STRIP(v) ((v) >> KMP_LOCK_SHIFT)
1192 
1193 // Initializes global states and data structures for managing dynamic user
1194 // locks.
1195 extern void __kmp_init_dynamic_user_locks();
1196 
1197 // Allocates and returns an indirect lock with the given indirect lock tag.
1198 extern kmp_indirect_lock_t *
1199 __kmp_allocate_indirect_lock(void **, kmp_int32, kmp_indirect_locktag_t);
1200 
1201 // Cleans up global states and data structures for managing dynamic user locks.
1202 extern void __kmp_cleanup_indirect_user_locks();
1203 
1204 // Default user lock sequence when not using hinted locks.
1205 extern kmp_dyna_lockseq_t __kmp_user_lock_seq;
1206 
1207 // Jump table for "set lock location", available only for indirect locks.
1208 extern void (*__kmp_indirect_set_location[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
1209  const ident_t *);
1210 #define KMP_SET_I_LOCK_LOCATION(lck, loc) \
1211  { \
1212  if (__kmp_indirect_set_location[(lck)->type] != NULL) \
1213  __kmp_indirect_set_location[(lck)->type]((lck)->lock, loc); \
1214  }
1215 
1216 // Jump table for "set lock flags", available only for indirect locks.
1217 extern void (*__kmp_indirect_set_flags[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
1218  kmp_lock_flags_t);
1219 #define KMP_SET_I_LOCK_FLAGS(lck, flag) \
1220  { \
1221  if (__kmp_indirect_set_flags[(lck)->type] != NULL) \
1222  __kmp_indirect_set_flags[(lck)->type]((lck)->lock, flag); \
1223  }
1224 
1225 // Jump table for "get lock location", available only for indirect locks.
1226 extern const ident_t *(*__kmp_indirect_get_location[KMP_NUM_I_LOCKS])(
1227  kmp_user_lock_p);
1228 #define KMP_GET_I_LOCK_LOCATION(lck) \
1229  (__kmp_indirect_get_location[(lck)->type] != NULL \
1230  ? __kmp_indirect_get_location[(lck)->type]((lck)->lock) \
1231  : NULL)
1232 
1233 // Jump table for "get lock flags", available only for indirect locks.
1234 extern kmp_lock_flags_t (*__kmp_indirect_get_flags[KMP_NUM_I_LOCKS])(
1235  kmp_user_lock_p);
1236 #define KMP_GET_I_LOCK_FLAGS(lck) \
1237  (__kmp_indirect_get_flags[(lck)->type] != NULL \
1238  ? __kmp_indirect_get_flags[(lck)->type]((lck)->lock) \
1239  : NULL)
1240 
1241 #define KMP_I_LOCK_CHUNK \
1242  1024 // number of kmp_indirect_lock_t objects to be allocated together
1243 
1244 // Lock table for indirect locks.
1245 typedef struct kmp_indirect_lock_table {
1246  kmp_indirect_lock_t **table; // blocks of indirect locks allocated
1247  kmp_lock_index_t size; // size of the indirect lock table
1248  kmp_lock_index_t next; // index to the next lock to be allocated
1249 } kmp_indirect_lock_table_t;
1250 
1251 extern kmp_indirect_lock_table_t __kmp_i_lock_table;
1252 
1253 // Returns the indirect lock associated with the given index.
1254 #define KMP_GET_I_LOCK(index) \
1255  (*(__kmp_i_lock_table.table + (index) / KMP_I_LOCK_CHUNK) + \
1256  (index) % KMP_I_LOCK_CHUNK)
1257 
1258 // Number of locks in a lock block, which is fixed to "1" now.
1259 // TODO: No lock block implementation now. If we do support, we need to manage
1260 // lock block data structure for each indirect lock type.
1261 extern int __kmp_num_locks_in_block;
1262 
1263 // Fast lock table lookup without consistency checking
1264 #define KMP_LOOKUP_I_LOCK(l) \
1265  ((OMP_LOCK_T_SIZE < sizeof(void *)) ? KMP_GET_I_LOCK(KMP_EXTRACT_I_INDEX(l)) \
1266  : *((kmp_indirect_lock_t **)(l)))
1267 
1268 // Used once in kmp_error.cpp
1269 extern kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p, kmp_uint32);
1270 
1271 #else // KMP_USE_DYNAMIC_LOCK
1272 
1273 #define KMP_LOCK_BUSY(v, type) (v)
1274 #define KMP_LOCK_FREE(type) 0
1275 #define KMP_LOCK_STRIP(v) (v)
1276 
1277 #endif // KMP_USE_DYNAMIC_LOCK
1278 
1279 // data structure for using backoff within spin locks.
1280 typedef struct {
1281  kmp_uint32 step; // current step
1282  kmp_uint32 max_backoff; // upper bound of outer delay loop
1283  kmp_uint32 min_tick; // size of inner delay loop in ticks (machine-dependent)
1284 } kmp_backoff_t;
1285 
1286 // Runtime's default backoff parameters
1287 extern kmp_backoff_t __kmp_spin_backoff_params;
1288 
1289 // Backoff function
1290 extern void __kmp_spin_backoff(kmp_backoff_t *);
1291 
1292 #ifdef __cplusplus
1293 } // extern "C"
1294 #endif // __cplusplus
1295 
1296 #endif /* KMP_LOCK_H */
Definition: kmp.h:208