Path: blob/main/contrib/llvm-project/openmp/runtime/src/kmp_lock.h
35258 views
/*1* kmp_lock.h -- lock header file2*/34//===----------------------------------------------------------------------===//5//6// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.7// See https://llvm.org/LICENSE.txt for license information.8// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception9//10//===----------------------------------------------------------------------===//1112#ifndef KMP_LOCK_H13#define KMP_LOCK_H1415#include <limits.h> // CHAR_BIT16#include <stddef.h> // offsetof1718#include "kmp_debug.h"19#include "kmp_os.h"2021#ifdef __cplusplus22#include <atomic>2324extern "C" {25#endif // __cplusplus2627// ----------------------------------------------------------------------------28// Have to copy these definitions from kmp.h because kmp.h cannot be included29// due to circular dependencies. Will undef these at end of file.3031#define KMP_PAD(type, sz) \32(sizeof(type) + (sz - ((sizeof(type) - 1) % (sz)) - 1))33#define KMP_GTID_DNE (-2)3435// Forward declaration of ident and ident_t3637struct ident;38typedef struct ident ident_t;3940// End of copied code.41// ----------------------------------------------------------------------------4243// We need to know the size of the area we can assume that the compiler(s)44// allocated for objects of type omp_lock_t and omp_nest_lock_t. The Intel45// compiler always allocates a pointer-sized area, as does visual studio.46//47// gcc however, only allocates 4 bytes for regular locks, even on 64-bit48// intel archs. It allocates at least 8 bytes for nested lock (more on49// recent versions), but we are bounded by the pointer-sized chunks that50// the Intel compiler allocates.5152#if (KMP_OS_LINUX || KMP_OS_AIX) && defined(KMP_GOMP_COMPAT)53#define OMP_LOCK_T_SIZE sizeof(int)54#define OMP_NEST_LOCK_T_SIZE sizeof(void *)55#else56#define OMP_LOCK_T_SIZE sizeof(void *)57#define OMP_NEST_LOCK_T_SIZE sizeof(void *)58#endif5960// The Intel compiler allocates a 32-byte chunk for a critical section.61// Both gcc and visual studio only allocate enough space for a pointer.62// Sometimes we know that the space was allocated by the Intel compiler.63#define OMP_CRITICAL_SIZE sizeof(void *)64#define INTEL_CRITICAL_SIZE 326566// lock flags67typedef kmp_uint32 kmp_lock_flags_t;6869#define kmp_lf_critical_section 17071// When a lock table is used, the indices are of kmp_lock_index_t72typedef kmp_uint32 kmp_lock_index_t;7374// When memory allocated for locks are on the lock pool (free list),75// it is treated as structs of this type.76struct kmp_lock_pool {77union kmp_user_lock *next;78kmp_lock_index_t index;79};8081typedef struct kmp_lock_pool kmp_lock_pool_t;8283extern void __kmp_validate_locks(void);8485// ----------------------------------------------------------------------------86// There are 5 lock implementations:87// 1. Test and set locks.88// 2. futex locks (Linux* OS on x86 and89// Intel(R) Many Integrated Core Architecture)90// 3. Ticket (Lamport bakery) locks.91// 4. Queuing locks (with separate spin fields).92// 5. DRPA (Dynamically Reconfigurable Distributed Polling Area) locks93//94// and 3 lock purposes:95// 1. Bootstrap locks -- Used for a few locks available at library96// startup-shutdown time.97// These do not require non-negative global thread ID's.98// 2. Internal RTL locks -- Used everywhere else in the RTL99// 3. User locks (includes critical sections)100// ----------------------------------------------------------------------------101102// ============================================================================103// Lock implementations.104//105// Test and set locks.106//107// Non-nested test and set locks differ from the other lock kinds (except108// futex) in that we use the memory allocated by the compiler for the lock,109// rather than a pointer to it.110//111// On lin32, lin_32e, and win_32, the space allocated may be as small as 4112// bytes, so we have to use a lock table for nested locks, and avoid accessing113// the depth_locked field for non-nested locks.114//115// Information normally available to the tools, such as lock location, lock116// usage (normal lock vs. critical section), etc. is not available with test and117// set locks.118// ----------------------------------------------------------------------------119120struct kmp_base_tas_lock {121// KMP_LOCK_FREE(tas) => unlocked; locked: (gtid+1) of owning thread122#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) && \123__LP64__124// Flip the ordering of the high and low 32-bit member to be consistent125// with the memory layout of the address in 64-bit big-endian.126kmp_int32 depth_locked; // depth locked, for nested locks only127std::atomic<kmp_int32> poll;128#else129std::atomic<kmp_int32> poll;130kmp_int32 depth_locked; // depth locked, for nested locks only131#endif132};133134typedef struct kmp_base_tas_lock kmp_base_tas_lock_t;135136union kmp_tas_lock {137kmp_base_tas_lock_t lk;138kmp_lock_pool_t pool; // make certain struct is large enough139double lk_align; // use worst case alignment; no cache line padding140};141142typedef union kmp_tas_lock kmp_tas_lock_t;143144// Static initializer for test and set lock variables. Usage:145// kmp_tas_lock_t xlock = KMP_TAS_LOCK_INITIALIZER( xlock );146#define KMP_TAS_LOCK_INITIALIZER(lock) \147{ \148{ KMP_LOCK_FREE(tas), 0 } \149}150151extern int __kmp_acquire_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);152extern int __kmp_test_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);153extern int __kmp_release_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);154extern void __kmp_init_tas_lock(kmp_tas_lock_t *lck);155extern void __kmp_destroy_tas_lock(kmp_tas_lock_t *lck);156157extern int __kmp_acquire_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);158extern int __kmp_test_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);159extern int __kmp_release_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid);160extern void __kmp_init_nested_tas_lock(kmp_tas_lock_t *lck);161extern void __kmp_destroy_nested_tas_lock(kmp_tas_lock_t *lck);162163#define KMP_LOCK_RELEASED 1164#define KMP_LOCK_STILL_HELD 0165#define KMP_LOCK_ACQUIRED_FIRST 1166#define KMP_LOCK_ACQUIRED_NEXT 0167#ifndef KMP_USE_FUTEX168#define KMP_USE_FUTEX \169(KMP_OS_LINUX && \170(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64))171#endif172#if KMP_USE_FUTEX173174// ----------------------------------------------------------------------------175// futex locks. futex locks are only available on Linux* OS.176//177// Like non-nested test and set lock, non-nested futex locks use the memory178// allocated by the compiler for the lock, rather than a pointer to it.179//180// Information normally available to the tools, such as lock location, lock181// usage (normal lock vs. critical section), etc. is not available with test and182// set locks. With non-nested futex locks, the lock owner is not even available.183// ----------------------------------------------------------------------------184185struct kmp_base_futex_lock {186volatile kmp_int32 poll; // KMP_LOCK_FREE(futex) => unlocked187// 2*(gtid+1) of owning thread, 0 if unlocked188// locked: (gtid+1) of owning thread189kmp_int32 depth_locked; // depth locked, for nested locks only190};191192typedef struct kmp_base_futex_lock kmp_base_futex_lock_t;193194union kmp_futex_lock {195kmp_base_futex_lock_t lk;196kmp_lock_pool_t pool; // make certain struct is large enough197double lk_align; // use worst case alignment198// no cache line padding199};200201typedef union kmp_futex_lock kmp_futex_lock_t;202203// Static initializer for futex lock variables. Usage:204// kmp_futex_lock_t xlock = KMP_FUTEX_LOCK_INITIALIZER( xlock );205#define KMP_FUTEX_LOCK_INITIALIZER(lock) \206{ \207{ KMP_LOCK_FREE(futex), 0 } \208}209210extern int __kmp_acquire_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);211extern int __kmp_test_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);212extern int __kmp_release_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);213extern void __kmp_init_futex_lock(kmp_futex_lock_t *lck);214extern void __kmp_destroy_futex_lock(kmp_futex_lock_t *lck);215216extern int __kmp_acquire_nested_futex_lock(kmp_futex_lock_t *lck,217kmp_int32 gtid);218extern int __kmp_test_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid);219extern int __kmp_release_nested_futex_lock(kmp_futex_lock_t *lck,220kmp_int32 gtid);221extern void __kmp_init_nested_futex_lock(kmp_futex_lock_t *lck);222extern void __kmp_destroy_nested_futex_lock(kmp_futex_lock_t *lck);223224#endif // KMP_USE_FUTEX225226// ----------------------------------------------------------------------------227// Ticket locks.228229#ifdef __cplusplus230231#ifdef _MSC_VER232// MSVC won't allow use of std::atomic<> in a union since it has non-trivial233// copy constructor.234235struct kmp_base_ticket_lock {236// `initialized' must be the first entry in the lock data structure!237std::atomic_bool initialized;238volatile union kmp_ticket_lock *self; // points to the lock union239ident_t const *location; // Source code location of omp_init_lock().240std::atomic_uint241next_ticket; // ticket number to give to next thread which acquires242std::atomic_uint now_serving; // ticket number for thread which holds the lock243std::atomic_int owner_id; // (gtid+1) of owning thread, 0 if unlocked244std::atomic_int depth_locked; // depth locked, for nested locks only245kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock246};247#else248struct kmp_base_ticket_lock {249// `initialized' must be the first entry in the lock data structure!250std::atomic<bool> initialized;251volatile union kmp_ticket_lock *self; // points to the lock union252ident_t const *location; // Source code location of omp_init_lock().253std::atomic<unsigned>254next_ticket; // ticket number to give to next thread which acquires255std::atomic<unsigned>256now_serving; // ticket number for thread which holds the lock257std::atomic<int> owner_id; // (gtid+1) of owning thread, 0 if unlocked258std::atomic<int> depth_locked; // depth locked, for nested locks only259kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock260};261#endif262263#else // __cplusplus264265struct kmp_base_ticket_lock;266267#endif // !__cplusplus268269typedef struct kmp_base_ticket_lock kmp_base_ticket_lock_t;270271union KMP_ALIGN_CACHE kmp_ticket_lock {272kmp_base_ticket_lock_t273lk; // This field must be first to allow static initializing.274kmp_lock_pool_t pool;275double lk_align; // use worst case alignment276char lk_pad[KMP_PAD(kmp_base_ticket_lock_t, CACHE_LINE)];277};278279typedef union kmp_ticket_lock kmp_ticket_lock_t;280281// Static initializer for simple ticket lock variables. Usage:282// kmp_ticket_lock_t xlock = KMP_TICKET_LOCK_INITIALIZER( xlock );283// Note the macro argument. It is important to make var properly initialized.284#define KMP_TICKET_LOCK_INITIALIZER(lock) \285{ \286{ true, &(lock), NULL, 0U, 0U, 0, -1 } \287}288289extern int __kmp_acquire_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid);290extern int __kmp_test_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid);291extern int __kmp_test_ticket_lock_with_cheks(kmp_ticket_lock_t *lck,292kmp_int32 gtid);293extern int __kmp_release_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid);294extern void __kmp_init_ticket_lock(kmp_ticket_lock_t *lck);295extern void __kmp_destroy_ticket_lock(kmp_ticket_lock_t *lck);296297extern int __kmp_acquire_nested_ticket_lock(kmp_ticket_lock_t *lck,298kmp_int32 gtid);299extern int __kmp_test_nested_ticket_lock(kmp_ticket_lock_t *lck,300kmp_int32 gtid);301extern int __kmp_release_nested_ticket_lock(kmp_ticket_lock_t *lck,302kmp_int32 gtid);303extern void __kmp_init_nested_ticket_lock(kmp_ticket_lock_t *lck);304extern void __kmp_destroy_nested_ticket_lock(kmp_ticket_lock_t *lck);305306// ----------------------------------------------------------------------------307// Queuing locks.308309#if KMP_USE_ADAPTIVE_LOCKS310311struct kmp_adaptive_lock_info;312313typedef struct kmp_adaptive_lock_info kmp_adaptive_lock_info_t;314315#if KMP_DEBUG_ADAPTIVE_LOCKS316317struct kmp_adaptive_lock_statistics {318/* So we can get stats from locks that haven't been destroyed. */319kmp_adaptive_lock_info_t *next;320kmp_adaptive_lock_info_t *prev;321322/* Other statistics */323kmp_uint32 successfulSpeculations;324kmp_uint32 hardFailedSpeculations;325kmp_uint32 softFailedSpeculations;326kmp_uint32 nonSpeculativeAcquires;327kmp_uint32 nonSpeculativeAcquireAttempts;328kmp_uint32 lemmingYields;329};330331typedef struct kmp_adaptive_lock_statistics kmp_adaptive_lock_statistics_t;332333extern void __kmp_print_speculative_stats();334extern void __kmp_init_speculative_stats();335336#endif // KMP_DEBUG_ADAPTIVE_LOCKS337338struct kmp_adaptive_lock_info {339/* Values used for adaptivity.340Although these are accessed from multiple threads we don't access them341atomically, because if we miss updates it probably doesn't matter much. (It342just affects our decision about whether to try speculation on the lock). */343kmp_uint32 volatile badness;344kmp_uint32 volatile acquire_attempts;345/* Parameters of the lock. */346kmp_uint32 max_badness;347kmp_uint32 max_soft_retries;348349#if KMP_DEBUG_ADAPTIVE_LOCKS350kmp_adaptive_lock_statistics_t volatile stats;351#endif352};353354#endif // KMP_USE_ADAPTIVE_LOCKS355356struct kmp_base_queuing_lock {357358// `initialized' must be the first entry in the lock data structure!359volatile union kmp_queuing_lock360*initialized; // Points to the lock union if in initialized state.361362ident_t const *location; // Source code location of omp_init_lock().363364KMP_ALIGN(8) // tail_id must be 8-byte aligned!365366volatile kmp_int32367tail_id; // (gtid+1) of thread at tail of wait queue, 0 if empty368// Must be no padding here since head/tail used in 8-byte CAS369volatile kmp_int32370head_id; // (gtid+1) of thread at head of wait queue, 0 if empty371// Decl order assumes little endian372// bakery-style lock373volatile kmp_uint32374next_ticket; // ticket number to give to next thread which acquires375volatile kmp_uint32376now_serving; // ticket number for thread which holds the lock377volatile kmp_int32 owner_id; // (gtid+1) of owning thread, 0 if unlocked378kmp_int32 depth_locked; // depth locked, for nested locks only379380kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock381};382383typedef struct kmp_base_queuing_lock kmp_base_queuing_lock_t;384385KMP_BUILD_ASSERT(offsetof(kmp_base_queuing_lock_t, tail_id) % 8 == 0);386387union KMP_ALIGN_CACHE kmp_queuing_lock {388kmp_base_queuing_lock_t389lk; // This field must be first to allow static initializing.390kmp_lock_pool_t pool;391double lk_align; // use worst case alignment392char lk_pad[KMP_PAD(kmp_base_queuing_lock_t, CACHE_LINE)];393};394395typedef union kmp_queuing_lock kmp_queuing_lock_t;396397extern int __kmp_acquire_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid);398extern int __kmp_test_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid);399extern int __kmp_release_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid);400extern void __kmp_init_queuing_lock(kmp_queuing_lock_t *lck);401extern void __kmp_destroy_queuing_lock(kmp_queuing_lock_t *lck);402403extern int __kmp_acquire_nested_queuing_lock(kmp_queuing_lock_t *lck,404kmp_int32 gtid);405extern int __kmp_test_nested_queuing_lock(kmp_queuing_lock_t *lck,406kmp_int32 gtid);407extern int __kmp_release_nested_queuing_lock(kmp_queuing_lock_t *lck,408kmp_int32 gtid);409extern void __kmp_init_nested_queuing_lock(kmp_queuing_lock_t *lck);410extern void __kmp_destroy_nested_queuing_lock(kmp_queuing_lock_t *lck);411412#if KMP_USE_ADAPTIVE_LOCKS413414// ----------------------------------------------------------------------------415// Adaptive locks.416struct kmp_base_adaptive_lock {417kmp_base_queuing_lock qlk;418KMP_ALIGN(CACHE_LINE)419kmp_adaptive_lock_info_t420adaptive; // Information for the speculative adaptive lock421};422423typedef struct kmp_base_adaptive_lock kmp_base_adaptive_lock_t;424425union KMP_ALIGN_CACHE kmp_adaptive_lock {426kmp_base_adaptive_lock_t lk;427kmp_lock_pool_t pool;428double lk_align;429char lk_pad[KMP_PAD(kmp_base_adaptive_lock_t, CACHE_LINE)];430};431typedef union kmp_adaptive_lock kmp_adaptive_lock_t;432433#define GET_QLK_PTR(l) ((kmp_queuing_lock_t *)&(l)->lk.qlk)434435#endif // KMP_USE_ADAPTIVE_LOCKS436437// ----------------------------------------------------------------------------438// DRDPA ticket locks.439struct kmp_base_drdpa_lock {440// All of the fields on the first cache line are only written when441// initializing or reconfiguring the lock. These are relatively rare442// operations, so data from the first cache line will usually stay resident in443// the cache of each thread trying to acquire the lock.444//445// initialized must be the first entry in the lock data structure!446KMP_ALIGN_CACHE447448volatile union kmp_drdpa_lock449*initialized; // points to the lock union if in initialized state450ident_t const *location; // Source code location of omp_init_lock().451std::atomic<std::atomic<kmp_uint64> *> polls;452std::atomic<kmp_uint64> mask; // is 2**num_polls-1 for mod op453kmp_uint64 cleanup_ticket; // thread with cleanup ticket454std::atomic<kmp_uint64> *old_polls; // will deallocate old_polls455kmp_uint32 num_polls; // must be power of 2456457// next_ticket it needs to exist in a separate cache line, as it is458// invalidated every time a thread takes a new ticket.459KMP_ALIGN_CACHE460461std::atomic<kmp_uint64> next_ticket;462463// now_serving is used to store our ticket value while we hold the lock. It464// has a slightly different meaning in the DRDPA ticket locks (where it is465// written by the acquiring thread) than it does in the simple ticket locks466// (where it is written by the releasing thread).467//468// Since now_serving is only read and written in the critical section,469// it is non-volatile, but it needs to exist on a separate cache line,470// as it is invalidated at every lock acquire.471//472// Likewise, the vars used for nested locks (owner_id and depth_locked) are473// only written by the thread owning the lock, so they are put in this cache474// line. owner_id is read by other threads, so it must be declared volatile.475KMP_ALIGN_CACHE476kmp_uint64 now_serving; // doesn't have to be volatile477volatile kmp_uint32 owner_id; // (gtid+1) of owning thread, 0 if unlocked478kmp_int32 depth_locked; // depth locked479kmp_lock_flags_t flags; // lock specifics, e.g. critical section lock480};481482typedef struct kmp_base_drdpa_lock kmp_base_drdpa_lock_t;483484union KMP_ALIGN_CACHE kmp_drdpa_lock {485kmp_base_drdpa_lock_t486lk; // This field must be first to allow static initializing. */487kmp_lock_pool_t pool;488double lk_align; // use worst case alignment489char lk_pad[KMP_PAD(kmp_base_drdpa_lock_t, CACHE_LINE)];490};491492typedef union kmp_drdpa_lock kmp_drdpa_lock_t;493494extern int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);495extern int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);496extern int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);497extern void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck);498extern void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck);499500extern int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck,501kmp_int32 gtid);502extern int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid);503extern int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck,504kmp_int32 gtid);505extern void __kmp_init_nested_drdpa_lock(kmp_drdpa_lock_t *lck);506extern void __kmp_destroy_nested_drdpa_lock(kmp_drdpa_lock_t *lck);507508// ============================================================================509// Lock purposes.510// ============================================================================511512// Bootstrap locks.513//514// Bootstrap locks -- very few locks used at library initialization time.515// Bootstrap locks are currently implemented as ticket locks.516// They could also be implemented as test and set lock, but cannot be517// implemented with other lock kinds as they require gtids which are not518// available at initialization time.519520typedef kmp_ticket_lock_t kmp_bootstrap_lock_t;521522#define KMP_BOOTSTRAP_LOCK_INITIALIZER(lock) KMP_TICKET_LOCK_INITIALIZER((lock))523#define KMP_BOOTSTRAP_LOCK_INIT(lock) \524kmp_bootstrap_lock_t lock = KMP_TICKET_LOCK_INITIALIZER(lock)525526static inline int __kmp_acquire_bootstrap_lock(kmp_bootstrap_lock_t *lck) {527return __kmp_acquire_ticket_lock(lck, KMP_GTID_DNE);528}529530static inline int __kmp_test_bootstrap_lock(kmp_bootstrap_lock_t *lck) {531return __kmp_test_ticket_lock(lck, KMP_GTID_DNE);532}533534static inline void __kmp_release_bootstrap_lock(kmp_bootstrap_lock_t *lck) {535__kmp_release_ticket_lock(lck, KMP_GTID_DNE);536}537538static inline void __kmp_init_bootstrap_lock(kmp_bootstrap_lock_t *lck) {539__kmp_init_ticket_lock(lck);540}541542static inline void __kmp_destroy_bootstrap_lock(kmp_bootstrap_lock_t *lck) {543__kmp_destroy_ticket_lock(lck);544}545546// Internal RTL locks.547//548// Internal RTL locks are also implemented as ticket locks, for now.549//550// FIXME - We should go through and figure out which lock kind works best for551// each internal lock, and use the type declaration and function calls for552// that explicit lock kind (and get rid of this section).553554typedef kmp_ticket_lock_t kmp_lock_t;555556#define KMP_LOCK_INIT(lock) kmp_lock_t lock = KMP_TICKET_LOCK_INITIALIZER(lock)557558static inline int __kmp_acquire_lock(kmp_lock_t *lck, kmp_int32 gtid) {559return __kmp_acquire_ticket_lock(lck, gtid);560}561562static inline int __kmp_test_lock(kmp_lock_t *lck, kmp_int32 gtid) {563return __kmp_test_ticket_lock(lck, gtid);564}565566static inline void __kmp_release_lock(kmp_lock_t *lck, kmp_int32 gtid) {567__kmp_release_ticket_lock(lck, gtid);568}569570static inline void __kmp_init_lock(kmp_lock_t *lck) {571__kmp_init_ticket_lock(lck);572}573574static inline void __kmp_destroy_lock(kmp_lock_t *lck) {575__kmp_destroy_ticket_lock(lck);576}577578// User locks.579//580// Do not allocate objects of type union kmp_user_lock!!! This will waste space581// unless __kmp_user_lock_kind == lk_drdpa. Instead, check the value of582// __kmp_user_lock_kind and allocate objects of the type of the appropriate583// union member, and cast their addresses to kmp_user_lock_p.584585enum kmp_lock_kind {586lk_default = 0,587lk_tas,588#if KMP_USE_FUTEX589lk_futex,590#endif591#if KMP_USE_DYNAMIC_LOCK && KMP_USE_TSX592lk_hle,593lk_rtm_queuing,594lk_rtm_spin,595#endif596lk_ticket,597lk_queuing,598lk_drdpa,599#if KMP_USE_ADAPTIVE_LOCKS600lk_adaptive601#endif // KMP_USE_ADAPTIVE_LOCKS602};603604typedef enum kmp_lock_kind kmp_lock_kind_t;605606extern kmp_lock_kind_t __kmp_user_lock_kind;607608union kmp_user_lock {609kmp_tas_lock_t tas;610#if KMP_USE_FUTEX611kmp_futex_lock_t futex;612#endif613kmp_ticket_lock_t ticket;614kmp_queuing_lock_t queuing;615kmp_drdpa_lock_t drdpa;616#if KMP_USE_ADAPTIVE_LOCKS617kmp_adaptive_lock_t adaptive;618#endif // KMP_USE_ADAPTIVE_LOCKS619kmp_lock_pool_t pool;620};621622typedef union kmp_user_lock *kmp_user_lock_p;623624#if !KMP_USE_DYNAMIC_LOCK625626extern size_t __kmp_base_user_lock_size;627extern size_t __kmp_user_lock_size;628629extern kmp_int32 (*__kmp_get_user_lock_owner_)(kmp_user_lock_p lck);630631static inline kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p lck) {632KMP_DEBUG_ASSERT(__kmp_get_user_lock_owner_ != NULL);633return (*__kmp_get_user_lock_owner_)(lck);634}635636extern int (*__kmp_acquire_user_lock_with_checks_)(kmp_user_lock_p lck,637kmp_int32 gtid);638639#if KMP_OS_LINUX && \640(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)641642#define __kmp_acquire_user_lock_with_checks(lck, gtid) \643if (__kmp_user_lock_kind == lk_tas) { \644if (__kmp_env_consistency_check) { \645char const *const func = "omp_set_lock"; \646if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) && \647lck->tas.lk.depth_locked != -1) { \648KMP_FATAL(LockNestableUsedAsSimple, func); \649} \650if ((gtid >= 0) && (lck->tas.lk.poll - 1 == gtid)) { \651KMP_FATAL(LockIsAlreadyOwned, func); \652} \653} \654if (lck->tas.lk.poll != 0 || \655!__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)) { \656kmp_uint32 spins; \657kmp_uint64 time; \658KMP_FSYNC_PREPARE(lck); \659KMP_INIT_YIELD(spins); \660KMP_INIT_BACKOFF(time); \661do { \662KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time); \663} while ( \664lck->tas.lk.poll != 0 || \665!__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)); \666} \667KMP_FSYNC_ACQUIRED(lck); \668} else { \669KMP_DEBUG_ASSERT(__kmp_acquire_user_lock_with_checks_ != NULL); \670(*__kmp_acquire_user_lock_with_checks_)(lck, gtid); \671}672673#else674static inline int __kmp_acquire_user_lock_with_checks(kmp_user_lock_p lck,675kmp_int32 gtid) {676KMP_DEBUG_ASSERT(__kmp_acquire_user_lock_with_checks_ != NULL);677return (*__kmp_acquire_user_lock_with_checks_)(lck, gtid);678}679#endif680681extern int (*__kmp_test_user_lock_with_checks_)(kmp_user_lock_p lck,682kmp_int32 gtid);683684#if KMP_OS_LINUX && \685(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)686687#include "kmp_i18n.h" /* AC: KMP_FATAL definition */688extern int __kmp_env_consistency_check; /* AC: copy from kmp.h here */689static inline int __kmp_test_user_lock_with_checks(kmp_user_lock_p lck,690kmp_int32 gtid) {691if (__kmp_user_lock_kind == lk_tas) {692if (__kmp_env_consistency_check) {693char const *const func = "omp_test_lock";694if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&695lck->tas.lk.depth_locked != -1) {696KMP_FATAL(LockNestableUsedAsSimple, func);697}698}699return ((lck->tas.lk.poll == 0) &&700__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1));701} else {702KMP_DEBUG_ASSERT(__kmp_test_user_lock_with_checks_ != NULL);703return (*__kmp_test_user_lock_with_checks_)(lck, gtid);704}705}706#else707static inline int __kmp_test_user_lock_with_checks(kmp_user_lock_p lck,708kmp_int32 gtid) {709KMP_DEBUG_ASSERT(__kmp_test_user_lock_with_checks_ != NULL);710return (*__kmp_test_user_lock_with_checks_)(lck, gtid);711}712#endif713714extern int (*__kmp_release_user_lock_with_checks_)(kmp_user_lock_p lck,715kmp_int32 gtid);716717static inline void __kmp_release_user_lock_with_checks(kmp_user_lock_p lck,718kmp_int32 gtid) {719KMP_DEBUG_ASSERT(__kmp_release_user_lock_with_checks_ != NULL);720(*__kmp_release_user_lock_with_checks_)(lck, gtid);721}722723extern void (*__kmp_init_user_lock_with_checks_)(kmp_user_lock_p lck);724725static inline void __kmp_init_user_lock_with_checks(kmp_user_lock_p lck) {726KMP_DEBUG_ASSERT(__kmp_init_user_lock_with_checks_ != NULL);727(*__kmp_init_user_lock_with_checks_)(lck);728}729730// We need a non-checking version of destroy lock for when the RTL is731// doing the cleanup as it can't always tell if the lock is nested or not.732extern void (*__kmp_destroy_user_lock_)(kmp_user_lock_p lck);733734static inline void __kmp_destroy_user_lock(kmp_user_lock_p lck) {735KMP_DEBUG_ASSERT(__kmp_destroy_user_lock_ != NULL);736(*__kmp_destroy_user_lock_)(lck);737}738739extern void (*__kmp_destroy_user_lock_with_checks_)(kmp_user_lock_p lck);740741static inline void __kmp_destroy_user_lock_with_checks(kmp_user_lock_p lck) {742KMP_DEBUG_ASSERT(__kmp_destroy_user_lock_with_checks_ != NULL);743(*__kmp_destroy_user_lock_with_checks_)(lck);744}745746extern int (*__kmp_acquire_nested_user_lock_with_checks_)(kmp_user_lock_p lck,747kmp_int32 gtid);748749#if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)750751#define __kmp_acquire_nested_user_lock_with_checks(lck, gtid, depth) \752if (__kmp_user_lock_kind == lk_tas) { \753if (__kmp_env_consistency_check) { \754char const *const func = "omp_set_nest_lock"; \755if ((sizeof(kmp_tas_lock_t) <= OMP_NEST_LOCK_T_SIZE) && \756lck->tas.lk.depth_locked == -1) { \757KMP_FATAL(LockSimpleUsedAsNestable, func); \758} \759} \760if (lck->tas.lk.poll - 1 == gtid) { \761lck->tas.lk.depth_locked += 1; \762*depth = KMP_LOCK_ACQUIRED_NEXT; \763} else { \764if ((lck->tas.lk.poll != 0) || \765!__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)) { \766kmp_uint32 spins; \767kmp_uint64 time; \768KMP_FSYNC_PREPARE(lck); \769KMP_INIT_YIELD(spins); \770KMP_INIT_BACKOFF(time); \771do { \772KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time); \773} while ( \774(lck->tas.lk.poll != 0) || \775!__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1)); \776} \777lck->tas.lk.depth_locked = 1; \778*depth = KMP_LOCK_ACQUIRED_FIRST; \779} \780KMP_FSYNC_ACQUIRED(lck); \781} else { \782KMP_DEBUG_ASSERT(__kmp_acquire_nested_user_lock_with_checks_ != NULL); \783*depth = (*__kmp_acquire_nested_user_lock_with_checks_)(lck, gtid); \784}785786#else787static inline void788__kmp_acquire_nested_user_lock_with_checks(kmp_user_lock_p lck, kmp_int32 gtid,789int *depth) {790KMP_DEBUG_ASSERT(__kmp_acquire_nested_user_lock_with_checks_ != NULL);791*depth = (*__kmp_acquire_nested_user_lock_with_checks_)(lck, gtid);792}793#endif794795extern int (*__kmp_test_nested_user_lock_with_checks_)(kmp_user_lock_p lck,796kmp_int32 gtid);797798#if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64)799static inline int __kmp_test_nested_user_lock_with_checks(kmp_user_lock_p lck,800kmp_int32 gtid) {801if (__kmp_user_lock_kind == lk_tas) {802int retval;803if (__kmp_env_consistency_check) {804char const *const func = "omp_test_nest_lock";805if ((sizeof(kmp_tas_lock_t) <= OMP_NEST_LOCK_T_SIZE) &&806lck->tas.lk.depth_locked == -1) {807KMP_FATAL(LockSimpleUsedAsNestable, func);808}809}810KMP_DEBUG_ASSERT(gtid >= 0);811if (lck->tas.lk.poll - 1 ==812gtid) { /* __kmp_get_tas_lock_owner( lck ) == gtid */813return ++lck->tas.lk.depth_locked; /* same owner, depth increased */814}815retval = ((lck->tas.lk.poll == 0) &&816__kmp_atomic_compare_store_acq(&lck->tas.lk.poll, 0, gtid + 1));817if (retval) {818KMP_MB();819lck->tas.lk.depth_locked = 1;820}821return retval;822} else {823KMP_DEBUG_ASSERT(__kmp_test_nested_user_lock_with_checks_ != NULL);824return (*__kmp_test_nested_user_lock_with_checks_)(lck, gtid);825}826}827#else828static inline int __kmp_test_nested_user_lock_with_checks(kmp_user_lock_p lck,829kmp_int32 gtid) {830KMP_DEBUG_ASSERT(__kmp_test_nested_user_lock_with_checks_ != NULL);831return (*__kmp_test_nested_user_lock_with_checks_)(lck, gtid);832}833#endif834835extern int (*__kmp_release_nested_user_lock_with_checks_)(kmp_user_lock_p lck,836kmp_int32 gtid);837838static inline int839__kmp_release_nested_user_lock_with_checks(kmp_user_lock_p lck,840kmp_int32 gtid) {841KMP_DEBUG_ASSERT(__kmp_release_nested_user_lock_with_checks_ != NULL);842return (*__kmp_release_nested_user_lock_with_checks_)(lck, gtid);843}844845extern void (*__kmp_init_nested_user_lock_with_checks_)(kmp_user_lock_p lck);846847static inline void848__kmp_init_nested_user_lock_with_checks(kmp_user_lock_p lck) {849KMP_DEBUG_ASSERT(__kmp_init_nested_user_lock_with_checks_ != NULL);850(*__kmp_init_nested_user_lock_with_checks_)(lck);851}852853extern void (*__kmp_destroy_nested_user_lock_with_checks_)(kmp_user_lock_p lck);854855static inline void856__kmp_destroy_nested_user_lock_with_checks(kmp_user_lock_p lck) {857KMP_DEBUG_ASSERT(__kmp_destroy_nested_user_lock_with_checks_ != NULL);858(*__kmp_destroy_nested_user_lock_with_checks_)(lck);859}860861// user lock functions which do not necessarily exist for all lock kinds.862//863// The "set" functions usually have wrapper routines that check for a NULL set864// function pointer and call it if non-NULL.865//866// In some cases, it makes sense to have a "get" wrapper function check for a867// NULL get function pointer and return NULL / invalid value / error code if868// the function pointer is NULL.869//870// In other cases, the calling code really should differentiate between an871// unimplemented function and one that is implemented but returning NULL /872// invalid value. If this is the case, no get function wrapper exists.873874extern int (*__kmp_is_user_lock_initialized_)(kmp_user_lock_p lck);875876// no set function; fields set during local allocation877878extern const ident_t *(*__kmp_get_user_lock_location_)(kmp_user_lock_p lck);879880static inline const ident_t *__kmp_get_user_lock_location(kmp_user_lock_p lck) {881if (__kmp_get_user_lock_location_ != NULL) {882return (*__kmp_get_user_lock_location_)(lck);883} else {884return NULL;885}886}887888extern void (*__kmp_set_user_lock_location_)(kmp_user_lock_p lck,889const ident_t *loc);890891static inline void __kmp_set_user_lock_location(kmp_user_lock_p lck,892const ident_t *loc) {893if (__kmp_set_user_lock_location_ != NULL) {894(*__kmp_set_user_lock_location_)(lck, loc);895}896}897898extern kmp_lock_flags_t (*__kmp_get_user_lock_flags_)(kmp_user_lock_p lck);899900extern void (*__kmp_set_user_lock_flags_)(kmp_user_lock_p lck,901kmp_lock_flags_t flags);902903static inline void __kmp_set_user_lock_flags(kmp_user_lock_p lck,904kmp_lock_flags_t flags) {905if (__kmp_set_user_lock_flags_ != NULL) {906(*__kmp_set_user_lock_flags_)(lck, flags);907}908}909910// The function which sets up all of the vtbl pointers for kmp_user_lock_t.911extern void __kmp_set_user_lock_vptrs(kmp_lock_kind_t user_lock_kind);912913// Macros for binding user lock functions.914#define KMP_BIND_USER_LOCK_TEMPLATE(nest, kind, suffix) \915{ \916__kmp_acquire##nest##user_lock_with_checks_ = (int (*)( \917kmp_user_lock_p, kmp_int32))__kmp_acquire##nest##kind##_##suffix; \918__kmp_release##nest##user_lock_with_checks_ = (int (*)( \919kmp_user_lock_p, kmp_int32))__kmp_release##nest##kind##_##suffix; \920__kmp_test##nest##user_lock_with_checks_ = (int (*)( \921kmp_user_lock_p, kmp_int32))__kmp_test##nest##kind##_##suffix; \922__kmp_init##nest##user_lock_with_checks_ = \923(void (*)(kmp_user_lock_p))__kmp_init##nest##kind##_##suffix; \924__kmp_destroy##nest##user_lock_with_checks_ = \925(void (*)(kmp_user_lock_p))__kmp_destroy##nest##kind##_##suffix; \926}927928#define KMP_BIND_USER_LOCK(kind) KMP_BIND_USER_LOCK_TEMPLATE(_, kind, lock)929#define KMP_BIND_USER_LOCK_WITH_CHECKS(kind) \930KMP_BIND_USER_LOCK_TEMPLATE(_, kind, lock_with_checks)931#define KMP_BIND_NESTED_USER_LOCK(kind) \932KMP_BIND_USER_LOCK_TEMPLATE(_nested_, kind, lock)933#define KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(kind) \934KMP_BIND_USER_LOCK_TEMPLATE(_nested_, kind, lock_with_checks)935936// User lock table & lock allocation937/* On 64-bit Linux* OS (and OS X*) GNU compiler allocates only 4 bytems memory938for lock variable, which is not enough to store a pointer, so we have to use939lock indexes instead of pointers and maintain lock table to map indexes to940pointers.941942943Note: The first element of the table is not a pointer to lock! It is a944pointer to previously allocated table (or NULL if it is the first table).945946Usage:947948if ( OMP_LOCK_T_SIZE < sizeof( <lock> ) ) { // or OMP_NEST_LOCK_T_SIZE949Lock table is fully utilized. User locks are indexes, so table is used on950user lock operation.951Note: it may be the case (lin_32) that we don't need to use a lock952table for regular locks, but do need the table for nested locks.953}954else {955Lock table initialized but not actually used.956}957*/958959struct kmp_lock_table {960kmp_lock_index_t used; // Number of used elements961kmp_lock_index_t allocated; // Number of allocated elements962kmp_user_lock_p *table; // Lock table.963};964965typedef struct kmp_lock_table kmp_lock_table_t;966967extern kmp_lock_table_t __kmp_user_lock_table;968extern kmp_user_lock_p __kmp_lock_pool;969970struct kmp_block_of_locks {971struct kmp_block_of_locks *next_block;972void *locks;973};974975typedef struct kmp_block_of_locks kmp_block_of_locks_t;976977extern kmp_block_of_locks_t *__kmp_lock_blocks;978extern int __kmp_num_locks_in_block;979980extern kmp_user_lock_p __kmp_user_lock_allocate(void **user_lock,981kmp_int32 gtid,982kmp_lock_flags_t flags);983extern void __kmp_user_lock_free(void **user_lock, kmp_int32 gtid,984kmp_user_lock_p lck);985extern kmp_user_lock_p __kmp_lookup_user_lock(void **user_lock,986char const *func);987extern void __kmp_cleanup_user_locks();988989#define KMP_CHECK_USER_LOCK_INIT() \990{ \991if (!TCR_4(__kmp_init_user_locks)) { \992__kmp_acquire_bootstrap_lock(&__kmp_initz_lock); \993if (!TCR_4(__kmp_init_user_locks)) { \994TCW_4(__kmp_init_user_locks, TRUE); \995} \996__kmp_release_bootstrap_lock(&__kmp_initz_lock); \997} \998}9991000#endif // KMP_USE_DYNAMIC_LOCK10011002#undef KMP_PAD1003#undef KMP_GTID_DNE10041005#if KMP_USE_DYNAMIC_LOCK1006// KMP_USE_DYNAMIC_LOCK enables dynamic dispatch of lock functions without1007// breaking the current compatibility. Essential functionality of this new code1008// is dynamic dispatch, but it also implements (or enables implementation of)1009// hinted user lock and critical section which will be part of OMP 4.5 soon.1010//1011// Lock type can be decided at creation time (i.e., lock initialization), and1012// subsequent lock function call on the created lock object requires type1013// extraction and call through jump table using the extracted type. This type1014// information is stored in two different ways depending on the size of the lock1015// object, and we differentiate lock types by this size requirement - direct and1016// indirect locks.1017//1018// Direct locks:1019// A direct lock object fits into the space created by the compiler for an1020// omp_lock_t object, and TAS/Futex lock falls into this category. We use low1021// one byte of the lock object as the storage for the lock type, and appropriate1022// bit operation is required to access the data meaningful to the lock1023// algorithms. Also, to differentiate direct lock from indirect lock, 1 is1024// written to LSB of the lock object. The newly introduced "hle" lock is also a1025// direct lock.1026//1027// Indirect locks:1028// An indirect lock object requires more space than the compiler-generated1029// space, and it should be allocated from heap. Depending on the size of the1030// compiler-generated space for the lock (i.e., size of omp_lock_t), this1031// omp_lock_t object stores either the address of the heap-allocated indirect1032// lock (void * fits in the object) or an index to the indirect lock table entry1033// that holds the address. Ticket/Queuing/DRDPA/Adaptive lock falls into this1034// category, and the newly introduced "rtm" lock is also an indirect lock which1035// was implemented on top of the Queuing lock. When the omp_lock_t object holds1036// an index (not lock address), 0 is written to LSB to differentiate the lock1037// from a direct lock, and the remaining part is the actual index to the1038// indirect lock table.10391040#include <stdint.h> // for uintptr_t10411042// Shortcuts1043#define KMP_USE_INLINED_TAS \1044(KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM)) && 11045#define KMP_USE_INLINED_FUTEX KMP_USE_FUTEX && 010461047// List of lock definitions; all nested locks are indirect locks.1048// hle lock is xchg lock prefixed with XACQUIRE/XRELEASE.1049// All nested locks are indirect lock types.1050#if KMP_USE_TSX1051#if KMP_USE_FUTEX1052#define KMP_FOREACH_D_LOCK(m, a) m(tas, a) m(futex, a) m(hle, a) m(rtm_spin, a)1053#define KMP_FOREACH_I_LOCK(m, a) \1054m(ticket, a) m(queuing, a) m(adaptive, a) m(drdpa, a) m(rtm_queuing, a) \1055m(nested_tas, a) m(nested_futex, a) m(nested_ticket, a) \1056m(nested_queuing, a) m(nested_drdpa, a)1057#else1058#define KMP_FOREACH_D_LOCK(m, a) m(tas, a) m(hle, a) m(rtm_spin, a)1059#define KMP_FOREACH_I_LOCK(m, a) \1060m(ticket, a) m(queuing, a) m(adaptive, a) m(drdpa, a) m(rtm_queuing, a) \1061m(nested_tas, a) m(nested_ticket, a) m(nested_queuing, a) \1062m(nested_drdpa, a)1063#endif // KMP_USE_FUTEX1064#define KMP_LAST_D_LOCK lockseq_rtm_spin1065#else1066#if KMP_USE_FUTEX1067#define KMP_FOREACH_D_LOCK(m, a) m(tas, a) m(futex, a)1068#define KMP_FOREACH_I_LOCK(m, a) \1069m(ticket, a) m(queuing, a) m(drdpa, a) m(nested_tas, a) m(nested_futex, a) \1070m(nested_ticket, a) m(nested_queuing, a) m(nested_drdpa, a)1071#define KMP_LAST_D_LOCK lockseq_futex1072#else1073#define KMP_FOREACH_D_LOCK(m, a) m(tas, a)1074#define KMP_FOREACH_I_LOCK(m, a) \1075m(ticket, a) m(queuing, a) m(drdpa, a) m(nested_tas, a) m(nested_ticket, a) \1076m(nested_queuing, a) m(nested_drdpa, a)1077#define KMP_LAST_D_LOCK lockseq_tas1078#endif // KMP_USE_FUTEX1079#endif // KMP_USE_TSX10801081// Information used in dynamic dispatch1082#define KMP_LOCK_SHIFT \10838 // number of low bits to be used as tag for direct locks1084#define KMP_FIRST_D_LOCK lockseq_tas1085#define KMP_FIRST_I_LOCK lockseq_ticket1086#define KMP_LAST_I_LOCK lockseq_nested_drdpa1087#define KMP_NUM_I_LOCKS \1088(locktag_nested_drdpa + 1) // number of indirect lock types10891090// Base type for dynamic locks.1091typedef kmp_uint32 kmp_dyna_lock_t;10921093// Lock sequence that enumerates all lock kinds. Always make this enumeration1094// consistent with kmp_lockseq_t in the include directory.1095typedef enum {1096lockseq_indirect = 0,1097#define expand_seq(l, a) lockseq_##l,1098KMP_FOREACH_D_LOCK(expand_seq, 0) KMP_FOREACH_I_LOCK(expand_seq, 0)1099#undef expand_seq1100} kmp_dyna_lockseq_t;11011102// Enumerates indirect lock tags.1103typedef enum {1104#define expand_tag(l, a) locktag_##l,1105KMP_FOREACH_I_LOCK(expand_tag, 0)1106#undef expand_tag1107} kmp_indirect_locktag_t;11081109// Utility macros that extract information from lock sequences.1110#define KMP_IS_D_LOCK(seq) \1111((seq) >= KMP_FIRST_D_LOCK && (seq) <= KMP_LAST_D_LOCK)1112#define KMP_IS_I_LOCK(seq) \1113((seq) >= KMP_FIRST_I_LOCK && (seq) <= KMP_LAST_I_LOCK)1114#define KMP_GET_I_TAG(seq) (kmp_indirect_locktag_t)((seq)-KMP_FIRST_I_LOCK)1115#define KMP_GET_D_TAG(seq) ((seq) << 1 | 1)11161117// Enumerates direct lock tags starting from indirect tag.1118typedef enum {1119#define expand_tag(l, a) locktag_##l = KMP_GET_D_TAG(lockseq_##l),1120KMP_FOREACH_D_LOCK(expand_tag, 0)1121#undef expand_tag1122} kmp_direct_locktag_t;11231124// Indirect lock type1125typedef struct {1126kmp_user_lock_p lock;1127kmp_indirect_locktag_t type;1128} kmp_indirect_lock_t;11291130// Function tables for direct locks. Set/unset/test differentiate functions1131// with/without consistency checking.1132extern void (*__kmp_direct_init[])(kmp_dyna_lock_t *, kmp_dyna_lockseq_t);1133extern void (**__kmp_direct_destroy)(kmp_dyna_lock_t *);1134extern int (**__kmp_direct_set)(kmp_dyna_lock_t *, kmp_int32);1135extern int (**__kmp_direct_unset)(kmp_dyna_lock_t *, kmp_int32);1136extern int (**__kmp_direct_test)(kmp_dyna_lock_t *, kmp_int32);11371138// Function tables for indirect locks. Set/unset/test differentiate functions1139// with/without consistency checking.1140extern void (*__kmp_indirect_init[])(kmp_user_lock_p);1141extern void (**__kmp_indirect_destroy)(kmp_user_lock_p);1142extern int (**__kmp_indirect_set)(kmp_user_lock_p, kmp_int32);1143extern int (**__kmp_indirect_unset)(kmp_user_lock_p, kmp_int32);1144extern int (**__kmp_indirect_test)(kmp_user_lock_p, kmp_int32);11451146// Extracts direct lock tag from a user lock pointer1147#define KMP_EXTRACT_D_TAG(l) \1148((kmp_dyna_lock_t)((kmp_base_tas_lock_t *)(l))->poll & \1149((1 << KMP_LOCK_SHIFT) - 1) & \1150-((kmp_dyna_lock_t)((kmp_tas_lock_t *)(l))->lk.poll & 1))11511152// Extracts indirect lock index from a user lock pointer1153#define KMP_EXTRACT_I_INDEX(l) \1154((kmp_lock_index_t)((kmp_base_tas_lock_t *)(l))->poll >> 1)11551156// Returns function pointer to the direct lock function with l (kmp_dyna_lock_t1157// *) and op (operation type).1158#define KMP_D_LOCK_FUNC(l, op) __kmp_direct_##op[KMP_EXTRACT_D_TAG(l)]11591160// Returns function pointer to the indirect lock function with l1161// (kmp_indirect_lock_t *) and op (operation type).1162#define KMP_I_LOCK_FUNC(l, op) \1163__kmp_indirect_##op[((kmp_indirect_lock_t *)(l))->type]11641165// Initializes a direct lock with the given lock pointer and lock sequence.1166#define KMP_INIT_D_LOCK(l, seq) \1167__kmp_direct_init[KMP_GET_D_TAG(seq)]((kmp_dyna_lock_t *)l, seq)11681169// Initializes an indirect lock with the given lock pointer and lock sequence.1170#define KMP_INIT_I_LOCK(l, seq) \1171__kmp_direct_init[0]((kmp_dyna_lock_t *)(l), seq)11721173// Returns "free" lock value for the given lock type.1174#define KMP_LOCK_FREE(type) (locktag_##type)11751176// Returns "busy" lock value for the given lock teyp.1177#define KMP_LOCK_BUSY(v, type) ((v) << KMP_LOCK_SHIFT | locktag_##type)11781179// Returns lock value after removing (shifting) lock tag.1180#define KMP_LOCK_STRIP(v) ((v) >> KMP_LOCK_SHIFT)11811182// Initializes global states and data structures for managing dynamic user1183// locks.1184extern void __kmp_init_dynamic_user_locks();11851186// Allocates and returns an indirect lock with the given indirect lock tag.1187extern kmp_indirect_lock_t *1188__kmp_allocate_indirect_lock(void **, kmp_int32, kmp_indirect_locktag_t);11891190// Cleans up global states and data structures for managing dynamic user locks.1191extern void __kmp_cleanup_indirect_user_locks();11921193// Default user lock sequence when not using hinted locks.1194extern kmp_dyna_lockseq_t __kmp_user_lock_seq;11951196// Jump table for "set lock location", available only for indirect locks.1197extern void (*__kmp_indirect_set_location[KMP_NUM_I_LOCKS])(kmp_user_lock_p,1198const ident_t *);1199#define KMP_SET_I_LOCK_LOCATION(lck, loc) \1200{ \1201if (__kmp_indirect_set_location[(lck)->type] != NULL) \1202__kmp_indirect_set_location[(lck)->type]((lck)->lock, loc); \1203}12041205// Jump table for "set lock flags", available only for indirect locks.1206extern void (*__kmp_indirect_set_flags[KMP_NUM_I_LOCKS])(kmp_user_lock_p,1207kmp_lock_flags_t);1208#define KMP_SET_I_LOCK_FLAGS(lck, flag) \1209{ \1210if (__kmp_indirect_set_flags[(lck)->type] != NULL) \1211__kmp_indirect_set_flags[(lck)->type]((lck)->lock, flag); \1212}12131214// Jump table for "get lock location", available only for indirect locks.1215extern const ident_t *(*__kmp_indirect_get_location[KMP_NUM_I_LOCKS])(1216kmp_user_lock_p);1217#define KMP_GET_I_LOCK_LOCATION(lck) \1218(__kmp_indirect_get_location[(lck)->type] != NULL \1219? __kmp_indirect_get_location[(lck)->type]((lck)->lock) \1220: NULL)12211222// Jump table for "get lock flags", available only for indirect locks.1223extern kmp_lock_flags_t (*__kmp_indirect_get_flags[KMP_NUM_I_LOCKS])(1224kmp_user_lock_p);1225#define KMP_GET_I_LOCK_FLAGS(lck) \1226(__kmp_indirect_get_flags[(lck)->type] != NULL \1227? __kmp_indirect_get_flags[(lck)->type]((lck)->lock) \1228: NULL)12291230// number of kmp_indirect_lock_t objects to be allocated together1231#define KMP_I_LOCK_CHUNK 10241232// Keep at a power of 2 since it is used in multiplication & division1233KMP_BUILD_ASSERT(KMP_I_LOCK_CHUNK % 2 == 0);1234// number of row entries in the initial lock table1235#define KMP_I_LOCK_TABLE_INIT_NROW_PTRS 812361237// Lock table for indirect locks.1238typedef struct kmp_indirect_lock_table {1239kmp_indirect_lock_t **table; // blocks of indirect locks allocated1240kmp_uint32 nrow_ptrs; // number *table pointer entries in table1241kmp_lock_index_t next; // index to the next lock to be allocated1242struct kmp_indirect_lock_table *next_table;1243} kmp_indirect_lock_table_t;12441245extern kmp_indirect_lock_table_t __kmp_i_lock_table;12461247// Returns the indirect lock associated with the given index.1248// Returns nullptr if no lock at given index1249static inline kmp_indirect_lock_t *__kmp_get_i_lock(kmp_lock_index_t idx) {1250kmp_indirect_lock_table_t *lock_table = &__kmp_i_lock_table;1251while (lock_table) {1252kmp_lock_index_t max_locks = lock_table->nrow_ptrs * KMP_I_LOCK_CHUNK;1253if (idx < max_locks) {1254kmp_lock_index_t row = idx / KMP_I_LOCK_CHUNK;1255kmp_lock_index_t col = idx % KMP_I_LOCK_CHUNK;1256if (!lock_table->table[row] || idx >= lock_table->next)1257break;1258return &lock_table->table[row][col];1259}1260idx -= max_locks;1261lock_table = lock_table->next_table;1262}1263return nullptr;1264}12651266// Number of locks in a lock block, which is fixed to "1" now.1267// TODO: No lock block implementation now. If we do support, we need to manage1268// lock block data structure for each indirect lock type.1269extern int __kmp_num_locks_in_block;12701271// Fast lock table lookup without consistency checking1272#define KMP_LOOKUP_I_LOCK(l) \1273((OMP_LOCK_T_SIZE < sizeof(void *)) \1274? __kmp_get_i_lock(KMP_EXTRACT_I_INDEX(l)) \1275: *((kmp_indirect_lock_t **)(l)))12761277// Used once in kmp_error.cpp1278extern kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p, kmp_uint32);12791280#else // KMP_USE_DYNAMIC_LOCK12811282#define KMP_LOCK_BUSY(v, type) (v)1283#define KMP_LOCK_FREE(type) 01284#define KMP_LOCK_STRIP(v) (v)12851286#endif // KMP_USE_DYNAMIC_LOCK12871288// data structure for using backoff within spin locks.1289typedef struct {1290kmp_uint32 step; // current step1291kmp_uint32 max_backoff; // upper bound of outer delay loop1292kmp_uint32 min_tick; // size of inner delay loop in ticks (machine-dependent)1293} kmp_backoff_t;12941295// Runtime's default backoff parameters1296extern kmp_backoff_t __kmp_spin_backoff_params;12971298// Backoff function1299extern void __kmp_spin_backoff(kmp_backoff_t *);13001301#ifdef __cplusplus1302} // extern "C"1303#endif // __cplusplus13041305#endif /* KMP_LOCK_H */130613071308