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