kmp_lock.h 54 KB

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