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