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  * $Revision: 42810 $
4  * $Date: 2013-11-07 12:06:33 -0600 (Thu, 07 Nov 2013) $
5  */
6 
7 /* <copyright>
8  Copyright (c) 1997-2013 Intel Corporation. All Rights Reserved.
9 
10  Redistribution and use in source and binary forms, with or without
11  modification, are permitted provided that the following conditions
12  are met:
13 
14  * Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in the
18  documentation and/or other materials provided with the distribution.
19  * Neither the name of Intel Corporation nor the names of its
20  contributors may be used to endorse or promote products derived
21  from this software without specific prior written permission.
22 
23  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 
35 </copyright> */
36 
37 #ifndef KMP_LOCK_H
38 #define KMP_LOCK_H
39 
40 #include <limits.h> // CHAR_BIT
41 #include <stddef.h> // offsetof
42 
43 #include "kmp_os.h"
44 #include "kmp_debug.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif // __cplusplus
49 
50 // ----------------------------------------------------------------------------
51 // Have to copy these definitions from kmp.h because kmp.h cannot be included
52 // due to circular dependencies. Will undef these at end of file.
53 
54 #define KMP_PAD(type, sz) (sizeof(type) + (sz - ((sizeof(type) - 1) % (sz)) - 1))
55 #define KMP_GTID_DNE (-2)
56 
57 // Forward declaration of ident and ident_t
58 
59 struct ident;
60 typedef struct ident ident_t;
61 
62 // End of copied code.
63 // ----------------------------------------------------------------------------
64 
65 //
66 // We need to know the size of the area we can assume that the compiler(s)
67 // allocated for obects of type omp_lock_t and omp_nest_lock_t. The Intel
68 // compiler always allocates a pointer-sized area, as does visual studio.
69 //
70 // gcc however, only allocates 4 bytes for regular locks, even on 64-bit
71 // intel archs. It allocates at least 8 bytes for nested lock (more on
72 // recent versions), but we are bounded by the pointer-sized chunks that
73 // the Intel compiler allocates.
74 //
75 
76 #if KMP_OS_LINUX && defined(KMP_GOMP_COMPAT)
77 # define OMP_LOCK_T_SIZE sizeof(int)
78 # define OMP_NEST_LOCK_T_SIZE sizeof(void *)
79 #else
80 # define OMP_LOCK_T_SIZE sizeof(void *)
81 # define OMP_NEST_LOCK_T_SIZE sizeof(void *)
82 #endif
83 
84 //
85 // The Intel compiler allocates a 32-byte chunk for a critical section.
86 // Both gcc and visual studio only allocate enough space for a pointer.
87 // Sometimes we know that the space was allocated by the Intel compiler.
88 //
89 #define OMP_CRITICAL_SIZE sizeof(void *)
90 #define INTEL_CRITICAL_SIZE 32
91 
92 //
93 // lock flags
94 //
95 typedef kmp_uint32 kmp_lock_flags_t;
96 
97 #define kmp_lf_critical_section 1
98 
99 //
100 // When a lock table is used, the indices are of kmp_lock_index_t
101 //
102 typedef kmp_uint32 kmp_lock_index_t;
103 
104 //
105 // When memory allocated for locks are on the lock pool (free list),
106 // it is treated as structs of this type.
107 //
108 struct kmp_lock_pool {
109  union kmp_user_lock *next;
110  kmp_lock_index_t index;
111 };
112 
113 typedef struct kmp_lock_pool kmp_lock_pool_t;
114 
115 
116 extern void __kmp_validate_locks( void );
117 
118 
119 // ----------------------------------------------------------------------------
120 //
121 // There are 5 lock implementations:
122 //
123 // 1. Test and set locks.
124 // 2. futex locks (Linux* OS on x86 and Intel(R) Many Integrated Core architecture)
125 // 3. Ticket (Lamport bakery) locks.
126 // 4. Queuing locks (with separate spin fields).
127 // 5. DRPA (Dynamically Reconfigurable Distributed Polling Area) locks
128 //
129 // and 3 lock purposes:
130 //
131 // 1. Bootstrap locks -- Used for a few locks available at library startup-shutdown time.
132 // These do not require non-negative global thread ID's.
133 // 2. Internal RTL locks -- Used everywhere else in the RTL
134 // 3. User locks (includes critical sections)
135 //
136 // ----------------------------------------------------------------------------
137 
138 
139 // ============================================================================
140 // Lock implementations.
141 // ============================================================================
142 
143 
144 // ----------------------------------------------------------------------------
145 // Test and set locks.
146 //
147 // Non-nested test and set locks differ from the other lock kinds (except
148 // futex) in that we use the memory allocated by the compiler for the lock,
149 // rather than a pointer to it.
150 //
151 // On lin32, lin_32e, and win_32, the space allocated may be as small as 4
152 // bytes, so we have to use a lock table for nested locks, and avoid accessing
153 // the depth_locked field for non-nested locks.
154 //
155 // Information normally available to the tools, such as lock location,
156 // lock usage (normal lock vs. critical section), etc. is not available with
157 // test and set locks.
158 // ----------------------------------------------------------------------------
159 
160 struct kmp_base_tas_lock {
161  volatile kmp_int32 poll; // 0 => unlocked
162  // locked: (gtid+1) of owning thread
163  kmp_int32 depth_locked; // depth locked, for nested locks only
164 };
165 
166 typedef struct kmp_base_tas_lock kmp_base_tas_lock_t;
167 
168 union kmp_tas_lock {
169  kmp_base_tas_lock_t lk;
170  kmp_lock_pool_t pool; // make certain struct is large enough
171  double lk_align; // use worst case alignment
172  // no cache line padding
173 };
174 
175 typedef union kmp_tas_lock kmp_tas_lock_t;
176 
177 //
178 // Static initializer for test and set lock variables. Usage:
179 // kmp_tas_lock_t xlock = KMP_TAS_LOCK_INITIALIZER( xlock );
180 //
181 #define KMP_TAS_LOCK_INITIALIZER( lock ) { { 0, 0 } }
182 
183 extern void __kmp_acquire_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
184 extern int __kmp_test_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
185 extern void __kmp_release_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
186 extern void __kmp_init_tas_lock( kmp_tas_lock_t *lck );
187 extern void __kmp_destroy_tas_lock( kmp_tas_lock_t *lck );
188 
189 extern void __kmp_acquire_nested_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
190 extern int __kmp_test_nested_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
191 extern void __kmp_release_nested_tas_lock( kmp_tas_lock_t *lck, kmp_int32 gtid );
192 extern void __kmp_init_nested_tas_lock( kmp_tas_lock_t *lck );
193 extern void __kmp_destroy_nested_tas_lock( kmp_tas_lock_t *lck );
194 
195 
196 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM)
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 void __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 void __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)
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 void __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 void __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;
303 
304 typedef struct kmp_adaptive_lock kmp_adaptive_lock_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_t * next;
311  kmp_adaptive_lock_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
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 #if KMP_USE_ADAPTIVE_LOCKS
371  KMP_ALIGN(CACHE_LINE)
372  kmp_adaptive_lock_t adaptive; // Information for the speculative adaptive lock
373 #endif
374 };
375 
376 typedef struct kmp_base_queuing_lock kmp_base_queuing_lock_t;
377 
378 KMP_BUILD_ASSERT( offsetof( kmp_base_queuing_lock_t, tail_id ) % 8 == 0 );
379 
380 union KMP_ALIGN_CACHE kmp_queuing_lock {
381  kmp_base_queuing_lock_t lk; // This field must be first to allow static initializing.
382  kmp_lock_pool_t pool;
383  double lk_align; // use worst case alignment
384  char lk_pad[ KMP_PAD( kmp_base_queuing_lock_t, CACHE_LINE ) ];
385 };
386 
387 typedef union kmp_queuing_lock kmp_queuing_lock_t;
388 
389 extern void __kmp_acquire_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
390 extern int __kmp_test_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
391 extern void __kmp_release_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
392 extern void __kmp_init_queuing_lock( kmp_queuing_lock_t *lck );
393 extern void __kmp_destroy_queuing_lock( kmp_queuing_lock_t *lck );
394 
395 extern void __kmp_acquire_nested_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
396 extern int __kmp_test_nested_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
397 extern void __kmp_release_nested_queuing_lock( kmp_queuing_lock_t *lck, kmp_int32 gtid );
398 extern void __kmp_init_nested_queuing_lock( kmp_queuing_lock_t *lck );
399 extern void __kmp_destroy_nested_queuing_lock( kmp_queuing_lock_t *lck );
400 
401 
402 // ----------------------------------------------------------------------------
403 // DRDPA ticket locks.
404 // ----------------------------------------------------------------------------
405 
406 struct kmp_base_drdpa_lock {
407  //
408  // All of the fields on the first cache line are only written when
409  // initializing or reconfiguring the lock. These are relatively rare
410  // operations, so data from the first cache line will usually stay
411  // resident in the cache of each thread trying to acquire the lock.
412  //
413  // initialized must be the first entry in the lock data structure!
414  //
415  KMP_ALIGN_CACHE
416 
417  volatile union kmp_drdpa_lock * initialized; // points to the lock union if in initialized state
418  ident_t const * location; // Source code location of omp_init_lock().
419  volatile struct kmp_lock_poll {
420  kmp_uint64 poll;
421  } * volatile polls;
422  volatile kmp_uint64 mask; // is 2**num_polls-1 for mod op
423  kmp_uint64 cleanup_ticket; // thread with cleanup ticket
424  volatile struct kmp_lock_poll * old_polls; // will deallocate old_polls
425  kmp_uint32 num_polls; // must be power of 2
426 
427  //
428  // next_ticket it needs to exist in a separate cache line, as it is
429  // invalidated every time a thread takes a new ticket.
430  //
431  KMP_ALIGN_CACHE
432 
433  volatile kmp_uint64 next_ticket;
434 
435  //
436  // now_serving is used to store our ticket value while we hold the lock.
437  // It has a slighly different meaning in the DRDPA ticket locks (where
438  // it is written by the acquiring thread) than it does in the simple
439  // ticket locks (where it is written by the releasing thread).
440  //
441  // Since now_serving is only read an written in the critical section,
442  // it is non-volatile, but it needs to exist on a separate cache line,
443  // as it is invalidated at every lock acquire.
444  //
445  // Likewise, the vars used for nested locks (owner_id and depth_locked)
446  // are only written by the thread owning the lock, so they are put in
447  // this cache line. owner_id is read by other threads, so it must be
448  // declared volatile.
449  //
450  KMP_ALIGN_CACHE
451 
452  kmp_uint64 now_serving; // doesn't have to be volatile
453  volatile kmp_uint32 owner_id; // (gtid+1) of owning thread, 0 if unlocked
454  kmp_int32 depth_locked; // depth locked
455  kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock
456 };
457 
458 typedef struct kmp_base_drdpa_lock kmp_base_drdpa_lock_t;
459 
460 union KMP_ALIGN_CACHE kmp_drdpa_lock {
461  kmp_base_drdpa_lock_t lk; // This field must be first to allow static initializing. */
462  kmp_lock_pool_t pool;
463  double lk_align; // use worst case alignment
464  char lk_pad[ KMP_PAD( kmp_base_drdpa_lock_t, CACHE_LINE ) ];
465 };
466 
467 typedef union kmp_drdpa_lock kmp_drdpa_lock_t;
468 
469 extern void __kmp_acquire_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
470 extern int __kmp_test_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
471 extern void __kmp_release_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
472 extern void __kmp_init_drdpa_lock( kmp_drdpa_lock_t *lck );
473 extern void __kmp_destroy_drdpa_lock( kmp_drdpa_lock_t *lck );
474 
475 extern void __kmp_acquire_nested_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
476 extern int __kmp_test_nested_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
477 extern void __kmp_release_nested_drdpa_lock( kmp_drdpa_lock_t *lck, kmp_int32 gtid );
478 extern void __kmp_init_nested_drdpa_lock( kmp_drdpa_lock_t *lck );
479 extern void __kmp_destroy_nested_drdpa_lock( kmp_drdpa_lock_t *lck );
480 
481 
482 // ============================================================================
483 // Lock purposes.
484 // ============================================================================
485 
486 
487 // ----------------------------------------------------------------------------
488 // Bootstrap locks.
489 // ----------------------------------------------------------------------------
490 
491 // Bootstrap locks -- very few locks used at library initialization time.
492 // Bootstrap locks are currently implemented as ticket locks.
493 // They could also be implemented as test and set lock, but cannot be
494 // implemented with other lock kinds as they require gtids which are not
495 // available at initialization time.
496 
497 typedef kmp_ticket_lock_t kmp_bootstrap_lock_t;
498 
499 #define KMP_BOOTSTRAP_LOCK_INITIALIZER( lock ) KMP_TICKET_LOCK_INITIALIZER( (lock) )
500 
501 static inline void
502 __kmp_acquire_bootstrap_lock( kmp_bootstrap_lock_t *lck )
503 {
504  __kmp_acquire_ticket_lock( lck, KMP_GTID_DNE );
505 }
506 
507 static inline int
508 __kmp_test_bootstrap_lock( kmp_bootstrap_lock_t *lck )
509 {
510  return __kmp_test_ticket_lock( lck, KMP_GTID_DNE );
511 }
512 
513 static inline void
514 __kmp_release_bootstrap_lock( kmp_bootstrap_lock_t *lck )
515 {
516  __kmp_release_ticket_lock( lck, KMP_GTID_DNE );
517 }
518 
519 static inline void
520 __kmp_init_bootstrap_lock( kmp_bootstrap_lock_t *lck )
521 {
522  __kmp_init_ticket_lock( lck );
523 }
524 
525 static inline void
526 __kmp_destroy_bootstrap_lock( kmp_bootstrap_lock_t *lck )
527 {
528  __kmp_destroy_ticket_lock( lck );
529 }
530 
531 
532 // ----------------------------------------------------------------------------
533 // Internal RTL locks.
534 // ----------------------------------------------------------------------------
535 
536 //
537 // Internal RTL locks are also implemented as ticket locks, for now.
538 //
539 // FIXME - We should go through and figure out which lock kind works best for
540 // each internal lock, and use the type deeclaration and function calls for
541 // that explicit lock kind (and get rid of this section).
542 //
543 
544 typedef kmp_ticket_lock_t kmp_lock_t;
545 
546 static inline void
547 __kmp_acquire_lock( kmp_lock_t *lck, kmp_int32 gtid )
548 {
549  __kmp_acquire_ticket_lock( lck, gtid );
550 }
551 
552 static inline int
553 __kmp_test_lock( kmp_lock_t *lck, kmp_int32 gtid )
554 {
555  return __kmp_test_ticket_lock( lck, gtid );
556 }
557 
558 static inline void
559 __kmp_release_lock( kmp_lock_t *lck, kmp_int32 gtid )
560 {
561  __kmp_release_ticket_lock( lck, gtid );
562 }
563 
564 static inline void
565 __kmp_init_lock( kmp_lock_t *lck )
566 {
567  __kmp_init_ticket_lock( lck );
568 }
569 
570 static inline void
571 __kmp_destroy_lock( kmp_lock_t *lck )
572 {
573  __kmp_destroy_ticket_lock( lck );
574 }
575 
576 
577 // ----------------------------------------------------------------------------
578 // User locks.
579 // ----------------------------------------------------------------------------
580 
581 //
582 // Do not allocate objects of type union kmp_user_lock!!!
583 // This will waste space unless __kmp_user_lock_kind == lk_drdpa.
584 // Instead, check the value of __kmp_user_lock_kind and allocate objects of
585 // the type of the appropriate union member, and cast their addresses to
586 // kmp_user_lock_p.
587 //
588 
589 enum kmp_lock_kind {
590  lk_default = 0,
591  lk_tas,
592 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM)
593  lk_futex,
594 #endif
595  lk_ticket,
596  lk_queuing,
597  lk_drdpa,
598 #if KMP_USE_ADAPTIVE_LOCKS
599  lk_adaptive
600 #endif // KMP_USE_ADAPTIVE_LOCKS
601 };
602 
603 typedef enum kmp_lock_kind kmp_lock_kind_t;
604 
605 extern kmp_lock_kind_t __kmp_user_lock_kind;
606 
607 union kmp_user_lock {
608  kmp_tas_lock_t tas;
609 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM)
610  kmp_futex_lock_t futex;
611 #endif
612  kmp_ticket_lock_t ticket;
613  kmp_queuing_lock_t queuing;
614  kmp_drdpa_lock_t drdpa;
615 #if KMP_USE_ADAPTIVE_LOCKS
616  kmp_adaptive_lock_t adaptive;
617 #endif // KMP_USE_ADAPTIVE_LOCKS
618  kmp_lock_pool_t pool;
619 };
620 
621 typedef union kmp_user_lock *kmp_user_lock_p;
622 
623 extern size_t __kmp_base_user_lock_size;
624 extern size_t __kmp_user_lock_size;
625 
626 extern kmp_int32 ( *__kmp_get_user_lock_owner_ )( kmp_user_lock_p lck );
627 
628 static inline kmp_int32
629 __kmp_get_user_lock_owner( kmp_user_lock_p lck )
630 {
631  KMP_DEBUG_ASSERT( __kmp_get_user_lock_owner_ != NULL );
632  return ( *__kmp_get_user_lock_owner_ )( lck );
633 }
634 
635 extern void ( *__kmp_acquire_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
636 
637 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM)
638 
639 #define __kmp_acquire_user_lock_with_checks(lck,gtid) \
640  if (__kmp_user_lock_kind == lk_tas) { \
641  if ( __kmp_env_consistency_check ) { \
642  char const * const func = "omp_set_lock"; \
643  if ( ( sizeof ( kmp_tas_lock_t ) <= OMP_LOCK_T_SIZE ) \
644  && lck->tas.lk.depth_locked != -1 ) { \
645  KMP_FATAL( LockNestableUsedAsSimple, func ); \
646  } \
647  if ( ( gtid >= 0 ) && ( lck->tas.lk.poll - 1 == gtid ) ) { \
648  KMP_FATAL( LockIsAlreadyOwned, func ); \
649  } \
650  } \
651  if ( ( lck->tas.lk.poll != 0 ) || \
652  ( ! KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) ) ) { \
653  kmp_uint32 spins; \
654  KMP_FSYNC_PREPARE( lck ); \
655  KMP_INIT_YIELD( spins ); \
656  if ( TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc) ) { \
657  KMP_YIELD( TRUE ); \
658  } else { \
659  KMP_YIELD_SPIN( spins ); \
660  } \
661  while ( ( lck->tas.lk.poll != 0 ) || \
662  ( ! KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) ) ) { \
663  if ( TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc) ) { \
664  KMP_YIELD( TRUE ); \
665  } else { \
666  KMP_YIELD_SPIN( spins ); \
667  } \
668  } \
669  } \
670  KMP_FSYNC_ACQUIRED( lck ); \
671  } else { \
672  KMP_DEBUG_ASSERT( __kmp_acquire_user_lock_with_checks_ != NULL ); \
673  ( *__kmp_acquire_user_lock_with_checks_ )( lck, gtid ); \
674  }
675 
676 #else
677 static inline void
678 __kmp_acquire_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
679 {
680  KMP_DEBUG_ASSERT( __kmp_acquire_user_lock_with_checks_ != NULL );
681  ( *__kmp_acquire_user_lock_with_checks_ )( lck, gtid );
682 }
683 #endif
684 
685 extern int ( *__kmp_test_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
686 
687 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM)
688 
689 #include "kmp_i18n.h" /* AC: KMP_FATAL definition */
690 extern int __kmp_env_consistency_check; /* AC: copy from kmp.h here */
691 static inline int
692 __kmp_test_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
693 {
694  if ( __kmp_user_lock_kind == lk_tas ) {
695  if ( __kmp_env_consistency_check ) {
696  char const * const func = "omp_test_lock";
697  if ( ( sizeof ( kmp_tas_lock_t ) <= OMP_LOCK_T_SIZE )
698  && lck->tas.lk.depth_locked != -1 ) {
699  KMP_FATAL( LockNestableUsedAsSimple, func );
700  }
701  }
702  return ( ( lck->tas.lk.poll == 0 ) &&
703  KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) );
704  } else {
705  KMP_DEBUG_ASSERT( __kmp_test_user_lock_with_checks_ != NULL );
706  return ( *__kmp_test_user_lock_with_checks_ )( lck, gtid );
707  }
708 }
709 #else
710 static inline int
711 __kmp_test_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
712 {
713  KMP_DEBUG_ASSERT( __kmp_test_user_lock_with_checks_ != NULL );
714  return ( *__kmp_test_user_lock_with_checks_ )( lck, gtid );
715 }
716 #endif
717 
718 extern void ( *__kmp_release_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
719 
720 static inline void
721 __kmp_release_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
722 {
723  KMP_DEBUG_ASSERT( __kmp_release_user_lock_with_checks_ != NULL );
724  ( *__kmp_release_user_lock_with_checks_ ) ( lck, gtid );
725 }
726 
727 extern void ( *__kmp_init_user_lock_with_checks_ )( kmp_user_lock_p lck );
728 
729 static inline void
730 __kmp_init_user_lock_with_checks( kmp_user_lock_p lck )
731 {
732  KMP_DEBUG_ASSERT( __kmp_init_user_lock_with_checks_ != NULL );
733  ( *__kmp_init_user_lock_with_checks_ )( lck );
734 }
735 
736 //
737 // We need a non-checking version of destroy lock for when the RTL is
738 // doing the cleanup as it can't always tell if the lock is nested or not.
739 //
740 extern void ( *__kmp_destroy_user_lock_ )( kmp_user_lock_p lck );
741 
742 static inline void
743 __kmp_destroy_user_lock( kmp_user_lock_p lck )
744 {
745  KMP_DEBUG_ASSERT( __kmp_destroy_user_lock_ != NULL );
746  ( *__kmp_destroy_user_lock_ )( lck );
747 }
748 
749 extern void ( *__kmp_destroy_user_lock_with_checks_ )( kmp_user_lock_p lck );
750 
751 static inline void
752 __kmp_destroy_user_lock_with_checks( kmp_user_lock_p lck )
753 {
754  KMP_DEBUG_ASSERT( __kmp_destroy_user_lock_with_checks_ != NULL );
755  ( *__kmp_destroy_user_lock_with_checks_ )( lck );
756 }
757 
758 extern void ( *__kmp_acquire_nested_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
759 
760 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)
761 
762 #define __kmp_acquire_nested_user_lock_with_checks(lck,gtid) \
763  if (__kmp_user_lock_kind == lk_tas) { \
764  if ( __kmp_env_consistency_check ) { \
765  char const * const func = "omp_set_nest_lock"; \
766  if ( ( sizeof ( kmp_tas_lock_t ) <= OMP_NEST_LOCK_T_SIZE ) \
767  && lck->tas.lk.depth_locked == -1 ) { \
768  KMP_FATAL( LockSimpleUsedAsNestable, func ); \
769  } \
770  } \
771  if ( lck->tas.lk.poll - 1 == gtid ) { \
772  lck->tas.lk.depth_locked += 1; \
773  } else { \
774  if ( ( lck->tas.lk.poll != 0 ) || \
775  ( ! KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) ) ) { \
776  kmp_uint32 spins; \
777  KMP_FSYNC_PREPARE( lck ); \
778  KMP_INIT_YIELD( spins ); \
779  if ( TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc) ) { \
780  KMP_YIELD( TRUE ); \
781  } else { \
782  KMP_YIELD_SPIN( spins ); \
783  } \
784  while ( ( lck->tas.lk.poll != 0 ) || \
785  ( ! KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) ) ) { \
786  if ( TCR_4(__kmp_nth) > (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc) ) { \
787  KMP_YIELD( TRUE ); \
788  } else { \
789  KMP_YIELD_SPIN( spins ); \
790  } \
791  } \
792  } \
793  lck->tas.lk.depth_locked = 1; \
794  } \
795  KMP_FSYNC_ACQUIRED( lck ); \
796  } else { \
797  KMP_DEBUG_ASSERT( __kmp_acquire_nested_user_lock_with_checks_ != NULL ); \
798  ( *__kmp_acquire_nested_user_lock_with_checks_ )( lck, gtid ); \
799  }
800 
801 #else
802 static inline void
803 __kmp_acquire_nested_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
804 {
805  KMP_DEBUG_ASSERT( __kmp_acquire_nested_user_lock_with_checks_ != NULL );
806  ( *__kmp_acquire_nested_user_lock_with_checks_ )( lck, gtid );
807 }
808 #endif
809 
810 extern int ( *__kmp_test_nested_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
811 
812 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)
813 static inline int
814 __kmp_test_nested_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
815 {
816  if ( __kmp_user_lock_kind == lk_tas ) {
817  int retval;
818  if ( __kmp_env_consistency_check ) {
819  char const * const func = "omp_test_nest_lock";
820  if ( ( sizeof ( kmp_tas_lock_t ) <= OMP_NEST_LOCK_T_SIZE )
821  && lck->tas.lk.depth_locked == -1 ) {
822  KMP_FATAL( LockSimpleUsedAsNestable, func );
823  }
824  }
825  KMP_DEBUG_ASSERT( gtid >= 0 );
826  if ( lck->tas.lk.poll - 1 == gtid ) { /* __kmp_get_tas_lock_owner( lck ) == gtid */
827  return ++lck->tas.lk.depth_locked; /* same owner, depth increased */
828  }
829  retval = ( ( lck->tas.lk.poll == 0 ) &&
830  KMP_COMPARE_AND_STORE_ACQ32( &(lck->tas.lk.poll), 0, gtid + 1 ) );
831  if ( retval ) {
832  KMP_MB();
833  lck->tas.lk.depth_locked = 1;
834  }
835  return retval;
836  } else {
837  KMP_DEBUG_ASSERT( __kmp_test_nested_user_lock_with_checks_ != NULL );
838  return ( *__kmp_test_nested_user_lock_with_checks_ )( lck, gtid );
839  }
840 }
841 #else
842 static inline int
843 __kmp_test_nested_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
844 {
845  KMP_DEBUG_ASSERT( __kmp_test_nested_user_lock_with_checks_ != NULL );
846  return ( *__kmp_test_nested_user_lock_with_checks_ )( lck, gtid );
847 }
848 #endif
849 
850 extern void ( *__kmp_release_nested_user_lock_with_checks_ )( kmp_user_lock_p lck, kmp_int32 gtid );
851 
852 static inline void
853 __kmp_release_nested_user_lock_with_checks( kmp_user_lock_p lck, kmp_int32 gtid )
854 {
855  KMP_DEBUG_ASSERT( __kmp_release_nested_user_lock_with_checks_ != NULL );
856  ( *__kmp_release_nested_user_lock_with_checks_ )( lck, gtid );
857 }
858 
859 extern void ( *__kmp_init_nested_user_lock_with_checks_ )( kmp_user_lock_p lck );
860 
861 static inline void __kmp_init_nested_user_lock_with_checks( kmp_user_lock_p lck )
862 {
863  KMP_DEBUG_ASSERT( __kmp_init_nested_user_lock_with_checks_ != NULL );
864  ( *__kmp_init_nested_user_lock_with_checks_ )( lck );
865 }
866 
867 extern void ( *__kmp_destroy_nested_user_lock_with_checks_ )( kmp_user_lock_p lck );
868 
869 static inline void
870 __kmp_destroy_nested_user_lock_with_checks( kmp_user_lock_p lck )
871 {
872  KMP_DEBUG_ASSERT( __kmp_destroy_nested_user_lock_with_checks_ != NULL );
873  ( *__kmp_destroy_nested_user_lock_with_checks_ )( lck );
874 }
875 
876 //
877 // user lock functions which do not necessarily exist for all lock kinds.
878 //
879 // The "set" functions usually have wrapper routines that check for a NULL set
880 // function pointer and call it if non-NULL.
881 //
882 // In some cases, it makes sense to have a "get" wrapper function check for a
883 // NULL get function pointer and return NULL / invalid value / error code if
884 // the function pointer is NULL.
885 //
886 // In other cases, the calling code really should differentiate between an
887 // unimplemented function and one that is implemented but returning NULL /
888 // invalied value. If this is the case, no get function wrapper exists.
889 //
890 
891 extern int ( *__kmp_is_user_lock_initialized_ )( kmp_user_lock_p lck );
892 
893 // no set function; fields set durining local allocation
894 
895 extern const ident_t * ( *__kmp_get_user_lock_location_ )( kmp_user_lock_p lck );
896 
897 static inline const ident_t *
898 __kmp_get_user_lock_location( kmp_user_lock_p lck )
899 {
900  if ( __kmp_get_user_lock_location_ != NULL ) {
901  return ( *__kmp_get_user_lock_location_ )( lck );
902  }
903  else {
904  return NULL;
905  }
906 }
907 
908 extern void ( *__kmp_set_user_lock_location_ )( kmp_user_lock_p lck, const ident_t *loc );
909 
910 static inline void
911 __kmp_set_user_lock_location( kmp_user_lock_p lck, const ident_t *loc )
912 {
913  if ( __kmp_set_user_lock_location_ != NULL ) {
914  ( *__kmp_set_user_lock_location_ )( lck, loc );
915  }
916 }
917 
918 extern kmp_lock_flags_t ( *__kmp_get_user_lock_flags_ )( kmp_user_lock_p lck );
919 
920 extern void ( *__kmp_set_user_lock_flags_ )( kmp_user_lock_p lck, kmp_lock_flags_t flags );
921 
922 static inline void
923 __kmp_set_user_lock_flags( kmp_user_lock_p lck, kmp_lock_flags_t flags )
924 {
925  if ( __kmp_set_user_lock_flags_ != NULL ) {
926  ( *__kmp_set_user_lock_flags_ )( lck, flags );
927  }
928 }
929 
930 //
931 // The fuction which sets up all of the vtbl pointers for kmp_user_lock_t.
932 //
933 extern void __kmp_set_user_lock_vptrs( kmp_lock_kind_t user_lock_kind );
934 
935 
936 
937 // ----------------------------------------------------------------------------
938 // User lock table & lock allocation
939 // ----------------------------------------------------------------------------
940 
941 /*
942  On 64-bit Linux* OS (and OS X*) GNU compiler allocates only 4 bytems memory for lock variable, which
943  is not enough to store a pointer, so we have to use lock indexes instead of pointers and
944  maintain lock table to map indexes to pointers.
945 
946 
947  Note: The first element of the table is not a pointer to lock! It is a pointer to previously
948  allocated table (or NULL if it is the first table).
949 
950  Usage:
951 
952  if ( OMP_LOCK_T_SIZE < sizeof( <lock> ) ) { // or OMP_NEST_LOCK_T_SIZE
953  Lock table is fully utilized. User locks are indexes, so table is
954  used on user lock operation.
955  Note: it may be the case (lin_32) that we don't need to use a lock
956  table for regular locks, but do need the table for nested locks.
957  }
958  else {
959  Lock table initialized but not actually used.
960  }
961 */
962 
963 struct kmp_lock_table {
964  kmp_lock_index_t used; // Number of used elements
965  kmp_lock_index_t allocated; // Number of allocated elements
966  kmp_user_lock_p * table; // Lock table.
967 };
968 
969 typedef struct kmp_lock_table kmp_lock_table_t;
970 
971 extern kmp_lock_table_t __kmp_user_lock_table;
972 extern kmp_user_lock_p __kmp_lock_pool;
973 
974 struct kmp_block_of_locks {
975  struct kmp_block_of_locks * next_block;
976  void * locks;
977 };
978 
979 typedef struct kmp_block_of_locks kmp_block_of_locks_t;
980 
981 extern kmp_block_of_locks_t *__kmp_lock_blocks;
982 extern int __kmp_num_locks_in_block;
983 
984 extern kmp_user_lock_p __kmp_user_lock_allocate( void **user_lock, kmp_int32 gtid, kmp_lock_flags_t flags );
985 extern void __kmp_user_lock_free( void **user_lock, kmp_int32 gtid, kmp_user_lock_p lck );
986 extern kmp_user_lock_p __kmp_lookup_user_lock( void **user_lock, char const *func );
987 extern void __kmp_cleanup_user_locks();
988 
989 #define KMP_CHECK_USER_LOCK_INIT() \
990  { \
991  if ( ! TCR_4( __kmp_init_user_locks ) ) { \
992  __kmp_acquire_bootstrap_lock( &__kmp_initz_lock ); \
993  if ( ! TCR_4( __kmp_init_user_locks ) ) { \
994  TCW_4( __kmp_init_user_locks, TRUE ); \
995  } \
996  __kmp_release_bootstrap_lock( &__kmp_initz_lock ); \
997  } \
998  }
999 
1000 #undef KMP_PAD
1001 #undef KMP_GTID_DNE
1002 
1003 #ifdef __cplusplus
1004 } // extern "C"
1005 #endif // __cplusplus
1006 
1007 #endif /* KMP_LOCK_H */
1008 
Definition: kmp.h:200