Intel® OpenMP* Runtime Library
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups Pages
kmp_lock.h
1 /*
2  * kmp_lock.h -- lock header file
3  */
4 
5 /* <copyright>
6  Copyright (c) 1997-2015 Intel Corporation. All Rights Reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11 
12  * Redistributions of source code must retain the above copyright
13  notice, this list of conditions and the following disclaimer.
14  * Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions and the following disclaimer in the
16  documentation and/or other materials provided with the distribution.
17  * Neither the name of Intel Corporation nor the names of its
18  contributors may be used to endorse or promote products derived
19  from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 </copyright> */
34 
35 #ifndef KMP_LOCK_H
36 #define KMP_LOCK_H
37 
38 #include <limits.h> // CHAR_BIT
39 #include <stddef.h> // offsetof
40 
41 #include "kmp_os.h"
42 #include "kmp_debug.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif // __cplusplus
47 
48 // ----------------------------------------------------------------------------
49 // Have to copy these definitions from kmp.h because kmp.h cannot be included
50 // due to circular dependencies. Will undef these at end of file.
51 
52 #define KMP_PAD(type, sz) (sizeof(type) + (sz - ((sizeof(type) - 1) % (sz)) - 1))
53 #define KMP_GTID_DNE (-2)
54 
55 // Forward declaration of ident and ident_t
56 
57 struct ident;
58 typedef struct ident ident_t;
59 
60 // End of copied code.
61 // ----------------------------------------------------------------------------
62 
63 //
64 // We need to know the size of the area we can assume that the compiler(s)
65 // allocated for obects of type omp_lock_t and omp_nest_lock_t. The Intel
66 // compiler always allocates a pointer-sized area, as does visual studio.
67 //
68 // gcc however, only allocates 4 bytes for regular locks, even on 64-bit
69 // intel archs. It allocates at least 8 bytes for nested lock (more on
70 // recent versions), but we are bounded by the pointer-sized chunks that
71 // the Intel compiler allocates.
72 //
73 
74 #if KMP_OS_LINUX && defined(KMP_GOMP_COMPAT)
75 # define OMP_LOCK_T_SIZE sizeof(int)
76 # define OMP_NEST_LOCK_T_SIZE sizeof(void *)
77 #else
78 # define OMP_LOCK_T_SIZE sizeof(void *)
79 # define OMP_NEST_LOCK_T_SIZE sizeof(void *)
80 #endif
81 
82 //
83 // The Intel compiler allocates a 32-byte chunk for a critical section.
84 // Both gcc and visual studio only allocate enough space for a pointer.
85 // Sometimes we know that the space was allocated by the Intel compiler.
86 //
87 #define OMP_CRITICAL_SIZE sizeof(void *)
88 #define INTEL_CRITICAL_SIZE 32
89 
90 //
91 // lock flags
92 //
93 typedef kmp_uint32 kmp_lock_flags_t;
94 
95 #define kmp_lf_critical_section 1
96 
97 //
98 // When a lock table is used, the indices are of kmp_lock_index_t
99 //
100 typedef kmp_uint32 kmp_lock_index_t;
101 
102 //
103 // When memory allocated for locks are on the lock pool (free list),
104 // it is treated as structs of this type.
105 //
106 struct kmp_lock_pool {
107  union kmp_user_lock *next;
108  kmp_lock_index_t index;
109 };
110 
111 typedef struct kmp_lock_pool kmp_lock_pool_t;
112 
113 
114 extern void __kmp_validate_locks( void );
115 
116 
117 // ----------------------------------------------------------------------------
118 //
119 // There are 5 lock implementations:
120 //
121 // 1. Test and set locks.
122 // 2. futex locks (Linux* OS on x86 and Intel(R) Many Integrated Core architecture)
123 // 3. Ticket (Lamport bakery) locks.
124 // 4. Queuing locks (with separate spin fields).
125 // 5. DRPA (Dynamically Reconfigurable Distributed Polling Area) locks
126 //
127 // and 3 lock purposes:
128 //
129 // 1. Bootstrap locks -- Used for a few locks available at library startup-shutdown time.
130 // These do not require non-negative global thread ID's.
131 // 2. Internal RTL locks -- Used everywhere else in the RTL
132 // 3. User locks (includes critical sections)
133 //
134 // ----------------------------------------------------------------------------
135 
136 
137 // ============================================================================
138 // Lock implementations.
139 // ============================================================================
140 
141 
142 // ----------------------------------------------------------------------------
143 // Test and set locks.
144 //
145 // Non-nested test and set locks differ from the other lock kinds (except
146 // futex) in that we use the memory allocated by the compiler for the lock,
147 // rather than a pointer to it.
148 //
149 // On lin32, lin_32e, and win_32, the space allocated may be as small as 4
150 // bytes, so we have to use a lock table for nested locks, and avoid accessing
151 // the depth_locked field for non-nested locks.
152 //
153 // Information normally available to the tools, such as lock location,
154 // lock usage (normal lock vs. critical section), etc. is not available with
155 // test and set locks.
156 // ----------------------------------------------------------------------------
157 
158 struct kmp_base_tas_lock {
159  volatile kmp_int32 poll; // 0 => unlocked
160  // locked: (gtid+1) of owning thread
161  kmp_int32 depth_locked; // depth locked, for nested locks only
162 };
163 
164 typedef struct kmp_base_tas_lock kmp_base_tas_lock_t;
165 
166 union kmp_tas_lock {
167  kmp_base_tas_lock_t lk;
168  kmp_lock_pool_t pool; // make certain struct is large enough
169  double lk_align; // use worst case alignment
170  // no cache line padding
171 };
172 
173 typedef union kmp_tas_lock kmp_tas_lock_t;
174 
175 //
176 // Static initializer for test and set lock variables. Usage:
177 // kmp_tas_lock_t xlock = KMP_TAS_LOCK_INITIALIZER( xlock );
178 //
179 #define KMP_TAS_LOCK_INITIALIZER( lock ) { { 0, 0 } }
180 
181 extern void __kmp_acquire_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
182 extern int __kmp_test_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
183 extern int __kmp_release_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
184 extern void __kmp_init_tas_lock( kmp_tas_lock_t *lck );
185 extern void __kmp_destroy_tas_lock( kmp_tas_lock_t *lck );
186 
187 extern void __kmp_acquire_nested_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
188 extern int __kmp_test_nested_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
189 extern int __kmp_release_nested_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
190 extern void __kmp_init_nested_tas_lock( kmp_tas_lock_t *lck );
191 extern void __kmp_destroy_nested_tas_lock( kmp_tas_lock_t *lck );
192 
193 #define KMP_LOCK_RELEASED 1
194 #define KMP_LOCK_STILL_HELD 0
195 
196 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
197 
198 // ----------------------------------------------------------------------------
199 // futex locks. futex locks are only available on Linux* OS.
200 //
201 // Like non-nested test and set lock, non-nested futex locks use the memory
202 // allocated by the compiler for the lock, rather than a pointer to it.
203 //
204 // Information normally available to the tools, such as lock location,
205 // lock usage (normal lock vs. critical section), etc. is not available with
206 // test and set locks. With non-nested futex locks, the lock owner is not
207 // even available.
208 // ----------------------------------------------------------------------------
209 
210 struct kmp_base_futex_lock {
211  volatile kmp_int32 poll; // 0 => unlocked
212  // 2*(gtid+1) of owning thread, 0 if unlocked
213  // locked: (gtid+1) of owning thread
214  kmp_int32 depth_locked; // depth locked, for nested locks only
215 };
216 
217 typedef struct kmp_base_futex_lock kmp_base_futex_lock_t;
218 
219 union kmp_futex_lock {
220  kmp_base_futex_lock_t lk;
221  kmp_lock_pool_t pool; // make certain struct is large enough
222  double lk_align; // use worst case alignment
223  // no cache line padding
224 };
225 
226 typedef union kmp_futex_lock kmp_futex_lock_t;
227 
228 //
229 // Static initializer for futex lock variables. Usage:
230 // kmp_futex_lock_t xlock = KMP_FUTEX_LOCK_INITIALIZER( xlock );
231 //
232 #define KMP_FUTEX_LOCK_INITIALIZER( lock ) { { 0, 0 } }
233 
234 extern void __kmp_acquire_futex_lock( kmp_futex_lock_t *lck, kmp_int32 gtid );
235 extern int __kmp_test_futex_lock( kmp_futex_lock_t *lck, kmp_int32 gtid );
236 extern int __kmp_release_futex_lock( kmp_futex_lock_t *lck, kmp_int32 gtid );
237 extern void __kmp_init_futex_lock( kmp_futex_lock_t *lck );
238 extern void __kmp_destroy_futex_lock( kmp_futex_lock_t *lck );
239 
240 extern void __kmp_acquire_nested_futex_lock( kmp_futex_lock_t *lck, kmp_int32 gtid );
241 extern int __kmp_test_nested_futex_lock( kmp_futex_lock_t *lck, kmp_int32 gtid );
242 extern int __kmp_release_nested_futex_lock( kmp_futex_lock_t *lck, kmp_int32 gtid );
243 extern void __kmp_init_nested_futex_lock( kmp_futex_lock_t *lck );
244 extern void __kmp_destroy_nested_futex_lock( kmp_futex_lock_t *lck );
245 
246 #endif // KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
247 
248 
249 // ----------------------------------------------------------------------------
250 // Ticket locks.
251 // ----------------------------------------------------------------------------
252 
253 struct kmp_base_ticket_lock {
254  // `initialized' must be the first entry in the lock data structure!
255  volatile union kmp_ticket_lock * initialized; // points to the lock union if in initialized state
256  ident_t const * location; // Source code location of omp_init_lock().
257  volatile kmp_uint32 next_ticket; // ticket number to give to next thread which acquires
258  volatile kmp_uint32 now_serving; // ticket number for thread which holds the lock
259  volatile kmp_int32 owner_id; // (gtid+1) of owning thread, 0 if unlocked
260  kmp_int32 depth_locked; // depth locked, for nested locks only
261  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
262 };
263 
264 typedef struct kmp_base_ticket_lock kmp_base_ticket_lock_t;
265 
266 union KMP_ALIGN_CACHE kmp_ticket_lock {
267  kmp_base_ticket_lock_t 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 //
276 // Static initializer for simple ticket lock variables. Usage:
277 // kmp_ticket_lock_t xlock = KMP_TICKET_LOCK_INITIALIZER( xlock );
278 // Note the macro argument. It is important to make var properly initialized.
279 //
280 #define KMP_TICKET_LOCK_INITIALIZER( lock ) { { (kmp_ticket_lock_t *) & (lock), NULL, 0, 0, 0, -1 } }
281 
282 extern void __kmp_acquire_ticket_lock( kmp_ticket_lock_t *lck, kmp_int32 gtid );
283 extern int __kmp_test_ticket_lock( kmp_ticket_lock_t *lck, kmp_int32 gtid );
284 extern int __kmp_test_ticket_lock_with_cheks( kmp_ticket_lock_t *lck, kmp_int32 gtid );
285 extern int __kmp_release_ticket_lock( kmp_ticket_lock_t *lck, kmp_int32 gtid );
286 extern void __kmp_init_ticket_lock( kmp_ticket_lock_t *lck );
287 extern void __kmp_destroy_ticket_lock( kmp_ticket_lock_t *lck );
288 
289 extern void __kmp_acquire_nested_ticket_lock( kmp_ticket_lock_t *lck, kmp_int32 gtid );
290 extern int __kmp_test_nested_ticket_lock( kmp_ticket_lock_t *lck, kmp_int32 gtid );
291 extern int __kmp_release_nested_ticket_lock( kmp_ticket_lock_t *lck, kmp_int32 gtid );
292 extern void __kmp_init_nested_ticket_lock( kmp_ticket_lock_t *lck );
293 extern void __kmp_destroy_nested_ticket_lock( kmp_ticket_lock_t *lck );
294 
295 
296 // ----------------------------------------------------------------------------
297 // Queuing locks.
298 // ----------------------------------------------------------------------------
299 
300 #if KMP_USE_ADAPTIVE_LOCKS
301 
302 struct kmp_adaptive_lock_info;
303 
304 typedef struct kmp_adaptive_lock_info kmp_adaptive_lock_info_t;
305 
306 #if KMP_DEBUG_ADAPTIVE_LOCKS
307 
308 struct kmp_adaptive_lock_statistics {
309  /* So we can get stats from locks that haven't been destroyed. */
310  kmp_adaptive_lock_info_t * next;
311  kmp_adaptive_lock_info_t * prev;
312 
313  /* Other statistics */
314  kmp_uint32 successfulSpeculations;
315  kmp_uint32 hardFailedSpeculations;
316  kmp_uint32 softFailedSpeculations;
317  kmp_uint32 nonSpeculativeAcquires;
318  kmp_uint32 nonSpeculativeAcquireAttempts;
319  kmp_uint32 lemmingYields;
320 };
321 
322 typedef struct kmp_adaptive_lock_statistics kmp_adaptive_lock_statistics_t;
323 
324 extern void __kmp_print_speculative_stats();
325 extern void __kmp_init_speculative_stats();
326 
327 #endif // KMP_DEBUG_ADAPTIVE_LOCKS
328 
329 struct kmp_adaptive_lock_info
330 {
331  /* Values used for adaptivity.
332  * Although these are accessed from multiple threads we don't access them atomically,
333  * because if we miss updates it probably doesn't matter much. (It just affects our
334  * decision about whether to try speculation on the lock).
335  */
336  kmp_uint32 volatile badness;
337  kmp_uint32 volatile acquire_attempts;
338  /* Parameters of the lock. */
339  kmp_uint32 max_badness;
340  kmp_uint32 max_soft_retries;
341 
342 #if KMP_DEBUG_ADAPTIVE_LOCKS
343  kmp_adaptive_lock_statistics_t volatile stats;
344 #endif
345 };
346 
347 #endif // KMP_USE_ADAPTIVE_LOCKS
348 
349 
350 struct kmp_base_queuing_lock {
351 
352  // `initialized' must be the first entry in the lock data structure!
353  volatile union kmp_queuing_lock *initialized; // Points to the lock union if in initialized state.
354 
355  ident_t const * location; // Source code location of omp_init_lock().
356 
357  KMP_ALIGN( 8 ) // tail_id must be 8-byte aligned!
358 
359  volatile kmp_int32 tail_id; // (gtid+1) of thread at tail of wait queue, 0 if empty
360  // Must be no padding here since head/tail used in 8-byte CAS
361  volatile kmp_int32 head_id; // (gtid+1) of thread at head of wait queue, 0 if empty
362  // Decl order assumes little endian
363  // bakery-style lock
364  volatile kmp_uint32 next_ticket; // ticket number to give to next thread which acquires
365  volatile kmp_uint32 now_serving; // ticket number for thread which holds the lock
366  volatile kmp_int32 owner_id; // (gtid+1) of owning thread, 0 if unlocked
367  kmp_int32 depth_locked; // depth locked, for nested locks only
368 
369  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
370 };
371 
372 typedef struct kmp_base_queuing_lock kmp_base_queuing_lock_t;
373 
374 KMP_BUILD_ASSERT( offsetof( kmp_base_queuing_lock_t, tail_id ) % 8 == 0 );
375 
376 union KMP_ALIGN_CACHE kmp_queuing_lock {
377  kmp_base_queuing_lock_t lk; // This field must be first to allow static initializing.
378  kmp_lock_pool_t pool;
379  double lk_align; // use worst case alignment
380  char lk_pad[ KMP_PAD( kmp_base_queuing_lock_t, CACHE_LINE ) ];
381 };
382 
383 typedef union kmp_queuing_lock kmp_queuing_lock_t;
384 
385 extern void __kmp_acquire_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
386 extern int __kmp_test_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
387 extern int __kmp_release_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
388 extern void __kmp_init_queuing_lock( kmp_queuing_lock_t *lck );
389 extern void __kmp_destroy_queuing_lock( kmp_queuing_lock_t *lck );
390 
391 extern void __kmp_acquire_nested_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
392 extern int __kmp_test_nested_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
393 extern int __kmp_release_nested_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
394 extern void __kmp_init_nested_queuing_lock( kmp_queuing_lock_t *lck );
395 extern void __kmp_destroy_nested_queuing_lock( kmp_queuing_lock_t *lck );
396 
397 #if KMP_USE_ADAPTIVE_LOCKS
398 
399 // ----------------------------------------------------------------------------
400 // Adaptive locks.
401 // ----------------------------------------------------------------------------
402 struct kmp_base_adaptive_lock {
403  kmp_base_queuing_lock qlk;
404  KMP_ALIGN(CACHE_LINE)
405  kmp_adaptive_lock_info_t adaptive; // Information for the speculative adaptive lock
406 };
407 
408 typedef struct kmp_base_adaptive_lock kmp_base_adaptive_lock_t;
409 
410 union KMP_ALIGN_CACHE kmp_adaptive_lock {
411  kmp_base_adaptive_lock_t lk;
412  kmp_lock_pool_t pool;
413  double lk_align;
414  char lk_pad[ KMP_PAD(kmp_base_adaptive_lock_t, CACHE_LINE) ];
415 };
416 typedef union kmp_adaptive_lock kmp_adaptive_lock_t;
417 
418 # define GET_QLK_PTR(l) ((kmp_queuing_lock_t *) & (l)->lk.qlk)
419 
420 #endif // KMP_USE_ADAPTIVE_LOCKS
421 
422 // ----------------------------------------------------------------------------
423 // DRDPA ticket locks.
424 // ----------------------------------------------------------------------------
425 
426 struct kmp_base_drdpa_lock {
427  //
428  // All of the fields on the first cache line are only written when
429  // initializing or reconfiguring the lock. These are relatively rare
430  // operations, so data from the first cache line will usually stay
431  // resident in the cache of each thread trying to acquire the lock.
432  //
433  // initialized must be the first entry in the lock data structure!
434  //
435  KMP_ALIGN_CACHE
436 
437  volatile union kmp_drdpa_lock * initialized; // points to the lock union if in initialized state
438  ident_t const * location; // Source code location of omp_init_lock().
439  volatile struct kmp_lock_poll {
440  kmp_uint64 poll;
441  } * volatile polls;
442  volatile kmp_uint64 mask; // is 2**num_polls-1 for mod op
443  kmp_uint64 cleanup_ticket; // thread with cleanup ticket
444  volatile struct kmp_lock_poll * old_polls; // will deallocate old_polls
445  kmp_uint32 num_polls; // must be power of 2
446 
447  //
448  // next_ticket it needs to exist in a separate cache line, as it is
449  // invalidated every time a thread takes a new ticket.
450  //
451  KMP_ALIGN_CACHE
452 
453  volatile kmp_uint64 next_ticket;
454 
455  //
456  // now_serving is used to store our ticket value while we hold the lock.
457  // It has a slightly different meaning in the DRDPA ticket locks (where
458  // it is written by the acquiring thread) than it does in the simple
459  // ticket locks (where it is written by the releasing thread).
460  //
461  // Since now_serving is only read an written in the critical section,
462  // it is non-volatile, but it needs to exist on a separate cache line,
463  // as it is invalidated at every lock acquire.
464  //
465  // Likewise, the vars used for nested locks (owner_id and depth_locked)
466  // are only written by the thread owning the lock, so they are put in
467  // this cache line. owner_id is read by other threads, so it must be
468  // declared volatile.
469  //
470  KMP_ALIGN_CACHE
471 
472  kmp_uint64 now_serving; // doesn't have to be volatile
473  volatile kmp_uint32 owner_id; // (gtid+1) of owning thread, 0 if unlocked
474  kmp_int32 depth_locked; // depth locked
475  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
476 };
477 
478 typedef struct kmp_base_drdpa_lock kmp_base_drdpa_lock_t;
479 
480 union KMP_ALIGN_CACHE kmp_drdpa_lock {
481  kmp_base_drdpa_lock_t lk; // This field must be first to allow static initializing. */
482  kmp_lock_pool_t pool;
483  double lk_align; // use worst case alignment
484  char lk_pad[ KMP_PAD( kmp_base_drdpa_lock_t, CACHE_LINE ) ];
485 };
486 
487 typedef union kmp_drdpa_lock kmp_drdpa_lock_t;
488 
489 extern void __kmp_acquire_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
490 extern int __kmp_test_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
491 extern int __kmp_release_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
492 extern void __kmp_init_drdpa_lock( kmp_drdpa_lock_t *lck );
493 extern void __kmp_destroy_drdpa_lock( kmp_drdpa_lock_t *lck );
494 
495 extern void __kmp_acquire_nested_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
496 extern int __kmp_test_nested_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
497 extern int __kmp_release_nested_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
498 extern void __kmp_init_nested_drdpa_lock( kmp_drdpa_lock_t *lck );
499 extern void __kmp_destroy_nested_drdpa_lock( kmp_drdpa_lock_t *lck );
500 
501 
502 // ============================================================================
503 // Lock purposes.
504 // ============================================================================
505 
506 
507 // ----------------------------------------------------------------------------
508 // Bootstrap locks.
509 // ----------------------------------------------------------------------------
510 
511 // Bootstrap locks -- very few locks used at library initialization time.
512 // Bootstrap locks are currently implemented as ticket locks.
513 // They could also be implemented as test and set lock, but cannot be
514 // implemented with other lock kinds as they require gtids which are not
515 // available at initialization time.
516 
517 typedef kmp_ticket_lock_t kmp_bootstrap_lock_t;
518 
519 #define KMP_BOOTSTRAP_LOCK_INITIALIZER( lock ) KMP_TICKET_LOCK_INITIALIZER( (lock) )
520 
521 static inline void
522 __kmp_acquire_bootstrap_lock( kmp_bootstrap_lock_t *lck )
523 {
524  __kmp_acquire_ticket_lock( lck, KMP_GTID_DNE );
525 }
526 
527 static inline int
528 __kmp_test_bootstrap_lock( kmp_bootstrap_lock_t *lck )
529 {
530  return __kmp_test_ticket_lock( lck, KMP_GTID_DNE );
531 }
532 
533 static inline void
534 __kmp_release_bootstrap_lock( kmp_bootstrap_lock_t *lck )
535 {
536  __kmp_release_ticket_lock( lck, KMP_GTID_DNE );
537 }
538 
539 static inline void
540 __kmp_init_bootstrap_lock( kmp_bootstrap_lock_t *lck )
541 {
542  __kmp_init_ticket_lock( lck );
543 }
544 
545 static inline void
546 __kmp_destroy_bootstrap_lock( kmp_bootstrap_lock_t *lck )
547 {
548  __kmp_destroy_ticket_lock( lck );
549 }
550 
551 
552 // ----------------------------------------------------------------------------
553 // Internal RTL locks.
554 // ----------------------------------------------------------------------------
555 
556 //
557 // Internal RTL locks are also implemented as ticket locks, for now.
558 //
559 // FIXME - We should go through and figure out which lock kind works best for
560 // each internal lock, and use the type declaration and function calls for
561 // that explicit lock kind (and get rid of this section).
562 //
563 
564 typedef kmp_ticket_lock_t kmp_lock_t;
565 
566 static inline void
567 __kmp_acquire_lock( kmp_lock_t *lck, kmp_int32 gtid )
568 {
569  __kmp_acquire_ticket_lock( lck, gtid );
570 }
571 
572 static inline int
573 __kmp_test_lock( kmp_lock_t *lck, kmp_int32 gtid )
574 {
575  return __kmp_test_ticket_lock( lck, gtid );
576 }
577 
578 static inline void
579 __kmp_release_lock( kmp_lock_t *lck, kmp_int32 gtid )
580 {
581  __kmp_release_ticket_lock( lck, gtid );
582 }
583 
584 static inline void
585 __kmp_init_lock( kmp_lock_t *lck )
586 {
587  __kmp_init_ticket_lock( lck );
588 }
589 
590 static inline void
591 __kmp_destroy_lock( kmp_lock_t *lck )
592 {
593  __kmp_destroy_ticket_lock( lck );
594 }
595 
596 
597 // ----------------------------------------------------------------------------
598 // User locks.
599 // ----------------------------------------------------------------------------
600 
601 //
602 // Do not allocate objects of type union kmp_user_lock!!!
603 // This will waste space unless __kmp_user_lock_kind == lk_drdpa.
604 // Instead, check the value of __kmp_user_lock_kind and allocate objects of
605 // the type of the appropriate union member, and cast their addresses to
606 // kmp_user_lock_p.
607 //
608 
609 enum kmp_lock_kind {
610  lk_default = 0,
611  lk_tas,
612 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
613  lk_futex,
614 #endif
615  lk_ticket,
616  lk_queuing,
617  lk_drdpa,
618 #if KMP_USE_ADAPTIVE_LOCKS
619  lk_adaptive
620 #endif // KMP_USE_ADAPTIVE_LOCKS
621 };
622 
623 typedef enum kmp_lock_kind kmp_lock_kind_t;
624 
625 extern kmp_lock_kind_t __kmp_user_lock_kind;
626 
627 union kmp_user_lock {
628  kmp_tas_lock_t tas;
629 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
630  kmp_futex_lock_t futex;
631 #endif
632  kmp_ticket_lock_t ticket;
633  kmp_queuing_lock_t queuing;
634  kmp_drdpa_lock_t drdpa;
635 #if KMP_USE_ADAPTIVE_LOCKS
636  kmp_adaptive_lock_t adaptive;
637 #endif // KMP_USE_ADAPTIVE_LOCKS
638  kmp_lock_pool_t pool;
639 };
640 
641 typedef union kmp_user_lock *kmp_user_lock_p;
642 
643 #if ! KMP_USE_DYNAMIC_LOCK
644 
645 extern size_t __kmp_base_user_lock_size;
646 extern size_t __kmp_user_lock_size;
647 
648 extern kmp_int32 ( *__kmp_get_user_lock_owner_ )( kmp_user_lock_p lck );
649 
650 static inline kmp_int32
651 __kmp_get_user_lock_owner( kmp_user_lock_p lck )
652 {
653  KMP_DEBUG_ASSERT( __kmp_get_user_lock_owner_ != NULL );
654  return ( *__kmp_get_user_lock_owner_ )( lck );
655 }
656 
657 extern void ( *__kmp_acquire_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
658 
659 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
660 
661 #define __kmp_acquire_user_lock_with_checks(lck,gtid) \
662  if (__kmp_user_lock_kind == lk_tas) { \
663  if ( __kmp_env_consistency_check ) { \
664  char const * const func = "omp_set_lock"; \
665  if ( ( sizeof ( kmp_tas_lock_t ) <= OMP_LOCK_T_SIZE ) \
666  && lck->tas.lk.depth_locked != -1 ) { \
667  KMP_FATAL( LockNestableUsedAsSimple, func ); \
668  } \
669  if ( ( gtid >= 0 ) && ( lck->tas.lk.poll - 1 == gtid ) ) { \
670  KMP_FATAL( LockIsAlreadyOwned, func ); \
671  } \
672  } \
673  if ( ( lck->tas.lk.poll != 0 ) || \
674  ( ! KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) ) ) { \
675  kmp_uint32 spins; \
676  KMP_FSYNC_PREPARE( lck ); \
677  KMP_INIT_YIELD( spins ); \
678  if ( TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc) ) { \
679  KMP_YIELD( TRUE ); \
680  } else { \
681  KMP_YIELD_SPIN( spins ); \
682  } \
683  while ( ( lck->tas.lk.poll != 0 ) || \
684  ( ! KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) ) ) { \
685  if ( TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc) ) { \
686  KMP_YIELD( TRUE ); \
687  } else { \
688  KMP_YIELD_SPIN( spins ); \
689  } \
690  } \
691  } \
692  KMP_FSYNC_ACQUIRED( lck ); \
693  } else { \
694  KMP_DEBUG_ASSERT( __kmp_acquire_user_lock_with_checks_ != NULL ); \
695  ( *__kmp_acquire_user_lock_with_checks_ )( lck, gtid ); \
696  }
697 
698 #else
699 static inline void
700 __kmp_acquire_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
701 {
702  KMP_DEBUG_ASSERT( __kmp_acquire_user_lock_with_checks_ != NULL );
703  ( *__kmp_acquire_user_lock_with_checks_ )( lck, gtid );
704 }
705 #endif
706 
707 extern int ( *__kmp_test_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
708 
709 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
710 
711 #include "kmp_i18n.h" /* AC: KMP_FATAL definition */
712 extern int __kmp_env_consistency_check; /* AC: copy from kmp.h here */
713 static inline int
714 __kmp_test_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
715 {
716  if ( __kmp_user_lock_kind == lk_tas ) {
717  if ( __kmp_env_consistency_check ) {
718  char const * const func = "omp_test_lock";
719  if ( ( sizeof ( kmp_tas_lock_t ) <= OMP_LOCK_T_SIZE )
720  && lck->tas.lk.depth_locked != -1 ) {
721  KMP_FATAL( LockNestableUsedAsSimple, func );
722  }
723  }
724  return ( ( lck->tas.lk.poll == 0 ) &&
725  KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) );
726  } else {
727  KMP_DEBUG_ASSERT( __kmp_test_user_lock_with_checks_ != NULL );
728  return ( *__kmp_test_user_lock_with_checks_ )( lck, gtid );
729  }
730 }
731 #else
732 static inline int
733 __kmp_test_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
734 {
735  KMP_DEBUG_ASSERT( __kmp_test_user_lock_with_checks_ != NULL );
736  return ( *__kmp_test_user_lock_with_checks_ )( lck, gtid );
737 }
738 #endif
739 
740 extern int ( *__kmp_release_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
741 
742 static inline void
743 __kmp_release_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
744 {
745  KMP_DEBUG_ASSERT( __kmp_release_user_lock_with_checks_ != NULL );
746  ( *__kmp_release_user_lock_with_checks_ ) ( lck, gtid );
747 }
748 
749 extern void ( *__kmp_init_user_lock_with_checks_ )( kmp_user_lock_p lck );
750 
751 static inline void
752 __kmp_init_user_lock_with_checks( kmp_user_lock_p lck )
753 {
754  KMP_DEBUG_ASSERT( __kmp_init_user_lock_with_checks_ != NULL );
755  ( *__kmp_init_user_lock_with_checks_ )( lck );
756 }
757 
758 //
759 // We need a non-checking version of destroy lock for when the RTL is
760 // doing the cleanup as it can't always tell if the lock is nested or not.
761 //
762 extern void ( *__kmp_destroy_user_lock_ )( kmp_user_lock_p lck );
763 
764 static inline void
765 __kmp_destroy_user_lock( kmp_user_lock_p lck )
766 {
767  KMP_DEBUG_ASSERT( __kmp_destroy_user_lock_ != NULL );
768  ( *__kmp_destroy_user_lock_ )( lck );
769 }
770 
771 extern void ( *__kmp_destroy_user_lock_with_checks_ )( kmp_user_lock_p lck );
772 
773 static inline void
774 __kmp_destroy_user_lock_with_checks( kmp_user_lock_p lck )
775 {
776  KMP_DEBUG_ASSERT( __kmp_destroy_user_lock_with_checks_ != NULL );
777  ( *__kmp_destroy_user_lock_with_checks_ )( lck );
778 }
779 
780 extern void ( *__kmp_acquire_nested_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
781 
782 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)
783 
784 #define __kmp_acquire_nested_user_lock_with_checks(lck,gtid) \
785  if (__kmp_user_lock_kind == lk_tas) { \
786  if ( __kmp_env_consistency_check ) { \
787  char const * const func = "omp_set_nest_lock"; \
788  if ( ( sizeof ( kmp_tas_lock_t ) <= OMP_NEST_LOCK_T_SIZE ) \
789  && lck->tas.lk.depth_locked == -1 ) { \
790  KMP_FATAL( LockSimpleUsedAsNestable, func ); \
791  } \
792  } \
793  if ( lck->tas.lk.poll - 1 == gtid ) { \
794  lck->tas.lk.depth_locked += 1; \
795  } else { \
796  if ( ( lck->tas.lk.poll != 0 ) || \
797  ( ! KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) ) ) { \
798  kmp_uint32 spins; \
799  KMP_FSYNC_PREPARE( lck ); \
800  KMP_INIT_YIELD( spins ); \
801  if ( TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc) ) { \
802  KMP_YIELD( TRUE ); \
803  } else { \
804  KMP_YIELD_SPIN( spins ); \
805  } \
806  while ( ( lck->tas.lk.poll != 0 ) || \
807  ( ! KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) ) ) { \
808  if ( TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc) ) { \
809  KMP_YIELD( TRUE ); \
810  } else { \
811  KMP_YIELD_SPIN( spins ); \
812  } \
813  } \
814  } \
815  lck->tas.lk.depth_locked = 1; \
816  } \
817  KMP_FSYNC_ACQUIRED( lck ); \
818  } else { \
819  KMP_DEBUG_ASSERT( __kmp_acquire_nested_user_lock_with_checks_ != NULL ); \
820  ( *__kmp_acquire_nested_user_lock_with_checks_ )( lck, gtid ); \
821  }
822 
823 #else
824 static inline void
825 __kmp_acquire_nested_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
826 {
827  KMP_DEBUG_ASSERT( __kmp_acquire_nested_user_lock_with_checks_ != NULL );
828  ( *__kmp_acquire_nested_user_lock_with_checks_ )( lck, gtid );
829 }
830 #endif
831 
832 extern int ( *__kmp_test_nested_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
833 
834 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)
835 static inline int
836 __kmp_test_nested_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
837 {
838  if ( __kmp_user_lock_kind == lk_tas ) {
839  int retval;
840  if ( __kmp_env_consistency_check ) {
841  char const * const func = "omp_test_nest_lock";
842  if ( ( sizeof ( kmp_tas_lock_t ) <= OMP_NEST_LOCK_T_SIZE )
843  && lck->tas.lk.depth_locked == -1 ) {
844  KMP_FATAL( LockSimpleUsedAsNestable, func );
845  }
846  }
847  KMP_DEBUG_ASSERT( gtid >= 0 );
848  if ( lck->tas.lk.poll - 1 == gtid ) { /* __kmp_get_tas_lock_owner( lck ) == gtid */
849  return ++lck->tas.lk.depth_locked; /* same owner, depth increased */
850  }
851  retval = ( ( lck->tas.lk.poll == 0 ) &&
852  KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) );
853  if ( retval ) {
854  KMP_MB();
855  lck->tas.lk.depth_locked = 1;
856  }
857  return retval;
858  } else {
859  KMP_DEBUG_ASSERT( __kmp_test_nested_user_lock_with_checks_ != NULL );
860  return ( *__kmp_test_nested_user_lock_with_checks_ )( lck, gtid );
861  }
862 }
863 #else
864 static inline int
865 __kmp_test_nested_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
866 {
867  KMP_DEBUG_ASSERT( __kmp_test_nested_user_lock_with_checks_ != NULL );
868  return ( *__kmp_test_nested_user_lock_with_checks_ )( lck, gtid );
869 }
870 #endif
871 
872 extern int ( *__kmp_release_nested_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
873 
874 static inline int
875 __kmp_release_nested_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
876 {
877  KMP_DEBUG_ASSERT( __kmp_release_nested_user_lock_with_checks_ != NULL );
878  return ( *__kmp_release_nested_user_lock_with_checks_ )( lck, gtid );
879 }
880 
881 extern void ( *__kmp_init_nested_user_lock_with_checks_ )( kmp_user_lock_p lck );
882 
883 static inline void __kmp_init_nested_user_lock_with_checks( kmp_user_lock_p lck )
884 {
885  KMP_DEBUG_ASSERT( __kmp_init_nested_user_lock_with_checks_ != NULL );
886  ( *__kmp_init_nested_user_lock_with_checks_ )( lck );
887 }
888 
889 extern void ( *__kmp_destroy_nested_user_lock_with_checks_ )( kmp_user_lock_p lck );
890 
891 static inline void
892 __kmp_destroy_nested_user_lock_with_checks( kmp_user_lock_p lck )
893 {
894  KMP_DEBUG_ASSERT( __kmp_destroy_nested_user_lock_with_checks_ != NULL );
895  ( *__kmp_destroy_nested_user_lock_with_checks_ )( lck );
896 }
897 
898 //
899 // user lock functions which do not necessarily exist for all lock kinds.
900 //
901 // The "set" functions usually have wrapper routines that check for a NULL set
902 // function pointer and call it if non-NULL.
903 //
904 // In some cases, it makes sense to have a "get" wrapper function check for a
905 // NULL get function pointer and return NULL / invalid value / error code if
906 // the function pointer is NULL.
907 //
908 // In other cases, the calling code really should differentiate between an
909 // unimplemented function and one that is implemented but returning NULL /
910 // invalied value. If this is the case, no get function wrapper exists.
911 //
912 
913 extern int ( *__kmp_is_user_lock_initialized_ )( kmp_user_lock_p lck );
914 
915 // no set function; fields set durining local allocation
916 
917 extern const ident_t * ( *__kmp_get_user_lock_location_ )( kmp_user_lock_p lck );
918 
919 static inline const ident_t *
920 __kmp_get_user_lock_location( kmp_user_lock_p lck )
921 {
922  if ( __kmp_get_user_lock_location_ != NULL ) {
923  return ( *__kmp_get_user_lock_location_ )( lck );
924  }
925  else {
926  return NULL;
927  }
928 }
929 
930 extern void ( *__kmp_set_user_lock_location_ )( kmp_user_lock_p lck, const ident_t *loc );
931 
932 static inline void
933 __kmp_set_user_lock_location( kmp_user_lock_p lck, const ident_t *loc )
934 {
935  if ( __kmp_set_user_lock_location_ != NULL ) {
936  ( *__kmp_set_user_lock_location_ )( lck, loc );
937  }
938 }
939 
940 extern kmp_lock_flags_t ( *__kmp_get_user_lock_flags_ )( kmp_user_lock_p lck );
941 
942 extern void ( *__kmp_set_user_lock_flags_ )( kmp_user_lock_p lck, kmp_lock_flags_t flags );
943 
944 static inline void
945 __kmp_set_user_lock_flags( kmp_user_lock_p lck, kmp_lock_flags_t flags )
946 {
947  if ( __kmp_set_user_lock_flags_ != NULL ) {
948  ( *__kmp_set_user_lock_flags_ )( lck, flags );
949  }
950 }
951 
952 //
953 // The fuction which sets up all of the vtbl pointers for kmp_user_lock_t.
954 //
955 extern void __kmp_set_user_lock_vptrs( kmp_lock_kind_t user_lock_kind );
956 
957 //
958 // Macros for binding user lock functions.
959 //
960 #define KMP_BIND_USER_LOCK_TEMPLATE(nest, kind, suffix) { \
961  __kmp_acquire##nest##user_lock_with_checks_ = ( void (*)( kmp_user_lock_p, kmp_int32 ) ) \
962  __kmp_acquire##nest##kind##_##suffix; \
963  __kmp_release##nest##user_lock_with_checks_ = ( int (*)( kmp_user_lock_p, kmp_int32 ) ) \
964  __kmp_release##nest##kind##_##suffix; \
965  __kmp_test##nest##user_lock_with_checks_ = ( int (*)( kmp_user_lock_p, kmp_int32 ) ) \
966  __kmp_test##nest##kind##_##suffix; \
967  __kmp_init##nest##user_lock_with_checks_ = ( void (*)( kmp_user_lock_p ) ) \
968  __kmp_init##nest##kind##_##suffix; \
969  __kmp_destroy##nest##user_lock_with_checks_ = ( void (*)( kmp_user_lock_p ) ) \
970  __kmp_destroy##nest##kind##_##suffix; \
971 }
972 
973 #define KMP_BIND_USER_LOCK(kind) KMP_BIND_USER_LOCK_TEMPLATE(_, kind, lock)
974 #define KMP_BIND_USER_LOCK_WITH_CHECKS(kind) KMP_BIND_USER_LOCK_TEMPLATE(_, kind, lock_with_checks)
975 #define KMP_BIND_NESTED_USER_LOCK(kind) KMP_BIND_USER_LOCK_TEMPLATE(_nested_, kind, lock)
976 #define KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(kind) KMP_BIND_USER_LOCK_TEMPLATE(_nested_, kind, lock_with_checks)
977 
978 // ----------------------------------------------------------------------------
979 // User lock table & lock allocation
980 // ----------------------------------------------------------------------------
981 
982 /*
983  On 64-bit Linux* OS (and OS X*) GNU compiler allocates only 4 bytems memory for lock variable, which
984  is not enough to store a pointer, so we have to use lock indexes instead of pointers and
985  maintain lock table to map indexes to pointers.
986 
987 
988  Note: The first element of the table is not a pointer to lock! It is a pointer to previously
989  allocated table (or NULL if it is the first table).
990 
991  Usage:
992 
993  if ( OMP_LOCK_T_SIZE < sizeof( <lock> ) ) { // or OMP_NEST_LOCK_T_SIZE
994  Lock table is fully utilized. User locks are indexes, so table is
995  used on user lock operation.
996  Note: it may be the case (lin_32) that we don't need to use a lock
997  table for regular locks, but do need the table for nested locks.
998  }
999  else {
1000  Lock table initialized but not actually used.
1001  }
1002 */
1003 
1004 struct kmp_lock_table {
1005  kmp_lock_index_t used; // Number of used elements
1006  kmp_lock_index_t allocated; // Number of allocated elements
1007  kmp_user_lock_p * table; // Lock table.
1008 };
1009 
1010 typedef struct kmp_lock_table kmp_lock_table_t;
1011 
1012 extern kmp_lock_table_t __kmp_user_lock_table;
1013 extern kmp_user_lock_p __kmp_lock_pool;
1014 
1015 struct kmp_block_of_locks {
1016  struct kmp_block_of_locks * next_block;
1017  void * locks;
1018 };
1019 
1020 typedef struct kmp_block_of_locks kmp_block_of_locks_t;
1021 
1022 extern kmp_block_of_locks_t *__kmp_lock_blocks;
1023 extern int __kmp_num_locks_in_block;
1024 
1025 extern kmp_user_lock_p __kmp_user_lock_allocate( void **user_lock, kmp_int32 gtid, kmp_lock_flags_t flags );
1026 extern void __kmp_user_lock_free( void **user_lock, kmp_int32 gtid, kmp_user_lock_p lck );
1027 extern kmp_user_lock_p __kmp_lookup_user_lock( void **user_lock, char const *func );
1028 extern void __kmp_cleanup_user_locks();
1029 
1030 #define KMP_CHECK_USER_LOCK_INIT() \
1031  { \
1032  if ( ! TCR_4( __kmp_init_user_locks ) ) { \
1033  __kmp_acquire_bootstrap_lock( &__kmp_initz_lock ); \
1034  if ( ! TCR_4( __kmp_init_user_locks ) ) { \
1035  TCW_4( __kmp_init_user_locks, TRUE ); \
1036  } \
1037  __kmp_release_bootstrap_lock( &__kmp_initz_lock ); \
1038  } \
1039  }
1040 
1041 #endif // KMP_USE_DYNAMIC_LOCK
1042 
1043 #undef KMP_PAD
1044 #undef KMP_GTID_DNE
1045 
1046 #if KMP_USE_DYNAMIC_LOCK
1047 
1048 #define DYNA_HAS_FUTEX (KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM))
1049 #define DYNA_HAS_HLE (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC)
1050 #define DYNA_USE_FAST_FUTEX 0 && DYNA_HAS_FUTEX
1051 #define DYNA_USE_FAST_TAS 1 && DYNA_HAS_FUTEX
1052 
1053 // List of lock definitions; all nested locks are indirect locks.
1054 // hle lock is xchg lock prefixed with XACQUIRE/XRELEASE.
1055 // All nested locks are indirect lock types.
1056 #if DYNA_HAS_FUTEX
1057 # if DYNA_HAS_HLE
1058 # define FOREACH_D_LOCK(m, a) m(tas, a) m(futex, a) m(hle, a)
1059 # define DYNA_LAST_D_LOCK_SEQ lockseq_hle
1060 # else
1061 # define FOREACH_D_LOCK(m, a) m(tas, a) m(futex, a)
1062 # define DYNA_LAST_D_LOCK_SEQ lockseq_futex
1063 # endif // DYNA_HAS_HLE
1064 # if KMP_USE_ADAPTIVE_LOCKS
1065 # define FOREACH_I_LOCK(m, a) m(ticket, a) m(queuing, a) m(adaptive, a) m(drdpa, a) \
1066  m(nested_tas, a) m(nested_futex, a) m(nested_ticket, a) \
1067  m(nested_queuing, a) m(nested_drdpa, a)
1068 # else
1069 # define FOREACH_I_LOCK(m, a) m(ticket, a) m(queuing, a) m(drdpa, a) \
1070  m(nested_tas, a) m(nested_futex, a) m(nested_ticket, a) \
1071  m(nested_queuing, a) m(nested_drdpa, a)
1072 # endif // KMP_USE_ADAPTIVE_LOCKS
1073 #else
1074 # if DYNA_HAS_HLE
1075 # define FOREACH_D_LOCK(m, a) m(tas, a) m(hle, a)
1076 # define DYNA_LAST_D_LOCK_SEQ lockseq_hle
1077 # else
1078 # define FOREACH_D_LOCK(m, a) m(tas, a)
1079 # define DYNA_LAST_D_LOCK_SEQ lockseq_tas
1080 # endif // DYNA_HAS_HLE
1081 # if KMP_USE_ADAPTIVE_LOCKS
1082 # define FOREACH_I_LOCK(m, a) m(ticket, a) m(queuing, a) m(adaptive, a) m(drdpa, a) \
1083  m(nested_tas, a) m(nested_ticket, a) \
1084  m(nested_queuing, a) m(nested_drdpa, a)
1085 # else
1086 # define FOREACH_I_LOCK(m, a) m(ticket, a) m(queuing, a) m(drdpa, a) \
1087  m(nested_tas, a) m(nested_ticket, a) \
1088  m(nested_queuing, a) m(nested_drdpa, a)
1089 # endif // KMP_USE_ADAPTIVE_LOCKS
1090 #endif // DYNA_HAS_FUTEX
1091 
1092 // Information used in dynamic dispatch
1093 #define DYNA_LOCK_VALUE_SHIFT 8
1094 #define DYNA_LOCK_TYPE_MASK ((1<<DYNA_LOCK_VALUE_SHIFT)-1)
1095 #define DYNA_NUM_D_LOCKS DYNA_LAST_D_LOCK_SEQ
1096 #define DYNA_NUM_I_LOCKS (locktag_nested_drdpa+1)
1097 
1098 // Base type for dynamic locks.
1099 typedef kmp_uint32 kmp_dyna_lock_t;
1100 
1101 // Lock sequence that enumerates all lock kinds.
1102 // Always make this enumeration consistent with kmp_lockseq_t in the include directory.
1103 typedef enum {
1104  lockseq_indirect = 0,
1105 #define expand_seq(l,a) lockseq_##l,
1106  FOREACH_D_LOCK(expand_seq, 0)
1107  FOREACH_I_LOCK(expand_seq, 0)
1108 #undef expand_seq
1109 } kmp_dyna_lockseq_t;
1110 
1111 // Enumerates indirect lock tags.
1112 typedef enum {
1113 #define expand_tag(l,a) locktag_##l,
1114  FOREACH_I_LOCK(expand_tag, 0)
1115 #undef expand_tag
1116 } kmp_indirect_locktag_t;
1117 
1118 // Utility macros that extract information from lock sequences.
1119 #define DYNA_IS_D_LOCK(seq) (seq >= lockseq_tas && seq <= DYNA_LAST_D_LOCK_SEQ)
1120 #define DYNA_IS_I_LOCK(seq) (seq >= lockseq_ticket && seq <= lockseq_nested_drdpa)
1121 #define DYNA_GET_I_TAG(seq) (kmp_indirect_locktag_t)(seq - lockseq_ticket)
1122 #define DYNA_GET_D_TAG(seq) (seq<<1 | 1)
1123 
1124 // Enumerates direct lock tags starting from indirect tag.
1125 typedef enum {
1126 #define expand_tag(l,a) locktag_##l = DYNA_GET_D_TAG(lockseq_##l),
1127  FOREACH_D_LOCK(expand_tag, 0)
1128 #undef expand_tag
1129 } kmp_direct_locktag_t;
1130 
1131 // Indirect lock type
1132 typedef struct {
1133  kmp_user_lock_p lock;
1134  kmp_indirect_locktag_t type;
1135 } kmp_indirect_lock_t;
1136 
1137 // Function tables for direct locks. Set/unset/test differentiate functions with/without consistency checking.
1138 extern void (*__kmp_direct_init_ops[])(kmp_dyna_lock_t *, kmp_dyna_lockseq_t);
1139 extern void (*__kmp_direct_destroy_ops[])(kmp_dyna_lock_t *);
1140 extern void (*(*__kmp_direct_set_ops))(kmp_dyna_lock_t *, kmp_int32);
1141 extern void (*(*__kmp_direct_unset_ops))(kmp_dyna_lock_t *, kmp_int32);
1142 extern int (*(*__kmp_direct_test_ops))(kmp_dyna_lock_t *, kmp_int32);
1143 
1144 // Function tables for indirect locks. Set/unset/test differentiate functions with/withuot consistency checking.
1145 extern void (*__kmp_indirect_init_ops[])(kmp_user_lock_p);
1146 extern void (*__kmp_indirect_destroy_ops[])(kmp_user_lock_p);
1147 extern void (*(*__kmp_indirect_set_ops))(kmp_user_lock_p, kmp_int32);
1148 extern void (*(*__kmp_indirect_unset_ops))(kmp_user_lock_p, kmp_int32);
1149 extern int (*(*__kmp_indirect_test_ops))(kmp_user_lock_p, kmp_int32);
1150 
1151 // Extracts direct lock tag from a user lock pointer
1152 #define DYNA_EXTRACT_D_TAG(l) (*((kmp_dyna_lock_t *)(l)) & DYNA_LOCK_TYPE_MASK & -(*((kmp_dyna_lock_t *)(l)) & 1))
1153 
1154 // Extracts indirect lock index from a user lock pointer
1155 #define DYNA_EXTRACT_I_INDEX(l) (*(kmp_lock_index_t *)(l) >> 1)
1156 
1157 // Returns function pointer to the direct lock function with l (kmp_dyna_lock_t *) and op (operation type).
1158 #define DYNA_D_LOCK_FUNC(l, op) __kmp_direct_##op##_ops[DYNA_EXTRACT_D_TAG(l)]
1159 
1160 // Returns function pointer to the indirect lock function with l (kmp_indirect_lock_t *) and op (operation type).
1161 #define DYNA_I_LOCK_FUNC(l, op) __kmp_indirect_##op##_ops[((kmp_indirect_lock_t *)(l))->type]
1162 
1163 // Initializes a direct lock with the given lock pointer and lock sequence.
1164 #define DYNA_INIT_D_LOCK(l, seq) __kmp_direct_init_ops[DYNA_GET_D_TAG(seq)]((kmp_dyna_lock_t *)l, seq)
1165 
1166 // Initializes an indirect lock with the given lock pointer and lock sequence.
1167 #define DYNA_INIT_I_LOCK(l, seq) __kmp_direct_init_ops[0]((kmp_dyna_lock_t *)(l), seq)
1168 
1169 // Returns "free" lock value for the given lock type.
1170 #define DYNA_LOCK_FREE(type) (locktag_##type)
1171 
1172 // Returns "busy" lock value for the given lock teyp.
1173 #define DYNA_LOCK_BUSY(v, type) ((v)<<DYNA_LOCK_VALUE_SHIFT | locktag_##type)
1174 
1175 // Returns lock value after removing (shifting) lock tag.
1176 #define DYNA_LOCK_STRIP(v) ((v)>>DYNA_LOCK_VALUE_SHIFT)
1177 
1178 // Updates __kmp_user_lock_seq with the give lock type.
1179 #define DYNA_STORE_LOCK_SEQ(type) (__kmp_user_lock_seq = lockseq_##type)
1180 
1181 // Internal entries for hinted lock initializers.
1182 extern void __kmp_init_lock_hinted(void **, int);
1183 extern void __kmp_init_nest_lock_hinted(void **, int);
1184 
1185 // Initializes global states and data structures for managing dynamic user locks.
1186 extern void __kmp_init_dynamic_user_locks();
1187 
1188 // Allocates and returns an indirect lock with the given indirect lock tag.
1189 extern kmp_indirect_lock_t * __kmp_allocate_indirect_lock(void **, kmp_int32, kmp_indirect_locktag_t);
1190 
1191 // Cleans up global states and data structures for managing dynamic user locks.
1192 extern void __kmp_cleanup_indirect_user_locks();
1193 
1194 // Default user lock sequence when not using hinted locks.
1195 extern kmp_dyna_lockseq_t __kmp_user_lock_seq;
1196 
1197 // Jump table for "set lock location", available only for indirect locks.
1198 extern void (*__kmp_indirect_set_location[DYNA_NUM_I_LOCKS])(kmp_user_lock_p, const ident_t *);
1199 #define DYNA_SET_I_LOCK_LOCATION(lck, loc) { \
1200  if (__kmp_indirect_set_location[(lck)->type] != NULL) \
1201  __kmp_indirect_set_location[(lck)->type]((lck)->lock, loc); \
1202 }
1203 
1204 // Jump table for "set lock flags", available only for indirect locks.
1205 extern void (*__kmp_indirect_set_flags[DYNA_NUM_I_LOCKS])(kmp_user_lock_p, kmp_lock_flags_t);
1206 #define DYNA_SET_I_LOCK_FLAGS(lck, flag) { \
1207  if (__kmp_indirect_set_flags[(lck)->type] != NULL) \
1208  __kmp_indirect_set_flags[(lck)->type]((lck)->lock, flag); \
1209 }
1210 
1211 // Jump table for "get lock location", available only for indirect locks.
1212 extern const ident_t * (*__kmp_indirect_get_location[DYNA_NUM_I_LOCKS])(kmp_user_lock_p);
1213 #define DYNA_GET_I_LOCK_LOCATION(lck) ( __kmp_indirect_get_location[(lck)->type] != NULL \
1214  ? __kmp_indirect_get_location[(lck)->type]((lck)->lock) \
1215  : NULL )
1216 
1217 // Jump table for "get lock flags", available only for indirect locks.
1218 extern kmp_lock_flags_t (*__kmp_indirect_get_flags[DYNA_NUM_I_LOCKS])(kmp_user_lock_p);
1219 #define DYNA_GET_I_LOCK_FLAGS(lck) ( __kmp_indirect_get_flags[(lck)->type] != NULL \
1220  ? __kmp_indirect_get_flags[(lck)->type]((lck)->lock) \
1221  : NULL )
1222 
1223 //
1224 // Lock table for indirect locks.
1225 //
1226 // Simple linear structure is used to keep pointers to allocated indirect locks.
1227 extern kmp_indirect_lock_t **__kmp_indirect_lock_table;
1228 // Current size of the lock table; it may increase but never shrink.
1229 extern kmp_lock_index_t __kmp_indirect_lock_table_size;
1230 // Next index to be used for a new indirect lock (= number of indirect locks allocated).
1231 extern kmp_lock_index_t __kmp_indirect_lock_table_next;
1232 // Number of locks in a lock block, which is fixed to "1" now.
1233 // TODO: No lock block implementation now. If we do support, we need to manage lock block data
1234 // structure for each indirect lock type.
1235 extern int __kmp_num_locks_in_block;
1236 
1237 // Fast lock table lookup without consistency checking
1238 #define DYNA_LOOKUP_I_LOCK(l) ( (OMP_LOCK_T_SIZE < sizeof(void *)) \
1239  ? __kmp_indirect_lock_table[DYNA_EXTRACT_I_INDEX(l)] \
1240  : *((kmp_indirect_lock_t **)l) )
1241 
1242 // Used once in kmp_error.c
1243 extern kmp_int32
1244 __kmp_get_user_lock_owner(kmp_user_lock_p, kmp_uint32);
1245 
1246 #else // KMP_USE_DYNAMIC_LOCK
1247 
1248 # define DYNA_LOCK_BUSY(v, type) (v)
1249 # define DYNA_LOCK_FREE(type) 0
1250 # define DYNA_LOCK_STRIP(v) (v)
1251 # define DYNA_STORE_LOCK_SEQ(seq)
1252 
1253 #endif // KMP_USE_DYNAMIC_LOCK
1254 
1255 #ifdef __cplusplus
1256 } // extern "C"
1257 #endif // __cplusplus
1258 
1259 #endif /* KMP_LOCK_H */
1260 
Definition: kmp.h:218