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