kmp_wait_release.h 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * kmp_wait_release.h -- Wait/Release implementation
  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_WAIT_RELEASE_H
  12. #define KMP_WAIT_RELEASE_H
  13. #include "kmp.h"
  14. #include "kmp_itt.h"
  15. #include "kmp_stats.h"
  16. #if OMPT_SUPPORT
  17. #include "ompt-specific.h"
  18. #endif
  19. /*!
  20. @defgroup WAIT_RELEASE Wait/Release operations
  21. The definitions and functions here implement the lowest level thread
  22. synchronizations of suspending a thread and awaking it. They are used to build
  23. higher level operations such as barriers and fork/join.
  24. */
  25. /*!
  26. @ingroup WAIT_RELEASE
  27. @{
  28. */
  29. struct flag_properties {
  30. unsigned int type : 16;
  31. unsigned int reserved : 16;
  32. };
  33. template <enum flag_type FlagType> struct flag_traits {};
  34. template <> struct flag_traits<flag32> {
  35. typedef kmp_uint32 flag_t;
  36. static const flag_type t = flag32;
  37. static inline flag_t tcr(flag_t f) { return TCR_4(f); }
  38. static inline flag_t test_then_add4(volatile flag_t *f) {
  39. return KMP_TEST_THEN_ADD4_32(RCAST(volatile kmp_int32 *, f));
  40. }
  41. static inline flag_t test_then_or(volatile flag_t *f, flag_t v) {
  42. return KMP_TEST_THEN_OR32(f, v);
  43. }
  44. static inline flag_t test_then_and(volatile flag_t *f, flag_t v) {
  45. return KMP_TEST_THEN_AND32(f, v);
  46. }
  47. };
  48. template <> struct flag_traits<atomic_flag64> {
  49. typedef kmp_uint64 flag_t;
  50. static const flag_type t = atomic_flag64;
  51. static inline flag_t tcr(flag_t f) { return TCR_8(f); }
  52. static inline flag_t test_then_add4(volatile flag_t *f) {
  53. return KMP_TEST_THEN_ADD4_64(RCAST(volatile kmp_int64 *, f));
  54. }
  55. static inline flag_t test_then_or(volatile flag_t *f, flag_t v) {
  56. return KMP_TEST_THEN_OR64(f, v);
  57. }
  58. static inline flag_t test_then_and(volatile flag_t *f, flag_t v) {
  59. return KMP_TEST_THEN_AND64(f, v);
  60. }
  61. };
  62. template <> struct flag_traits<flag64> {
  63. typedef kmp_uint64 flag_t;
  64. static const flag_type t = flag64;
  65. static inline flag_t tcr(flag_t f) { return TCR_8(f); }
  66. static inline flag_t test_then_add4(volatile flag_t *f) {
  67. return KMP_TEST_THEN_ADD4_64(RCAST(volatile kmp_int64 *, f));
  68. }
  69. static inline flag_t test_then_or(volatile flag_t *f, flag_t v) {
  70. return KMP_TEST_THEN_OR64(f, v);
  71. }
  72. static inline flag_t test_then_and(volatile flag_t *f, flag_t v) {
  73. return KMP_TEST_THEN_AND64(f, v);
  74. }
  75. };
  76. template <> struct flag_traits<flag_oncore> {
  77. typedef kmp_uint64 flag_t;
  78. static const flag_type t = flag_oncore;
  79. static inline flag_t tcr(flag_t f) { return TCR_8(f); }
  80. static inline flag_t test_then_add4(volatile flag_t *f) {
  81. return KMP_TEST_THEN_ADD4_64(RCAST(volatile kmp_int64 *, f));
  82. }
  83. static inline flag_t test_then_or(volatile flag_t *f, flag_t v) {
  84. return KMP_TEST_THEN_OR64(f, v);
  85. }
  86. static inline flag_t test_then_and(volatile flag_t *f, flag_t v) {
  87. return KMP_TEST_THEN_AND64(f, v);
  88. }
  89. };
  90. /*! Base class for all flags */
  91. template <flag_type FlagType> class kmp_flag {
  92. protected:
  93. flag_properties t; /**< "Type" of the flag in loc */
  94. kmp_info_t *waiting_threads[1]; /**< Threads sleeping on this thread. */
  95. kmp_uint32 num_waiting_threads; /**< Num threads sleeping on this thread. */
  96. std::atomic<bool> *sleepLoc;
  97. public:
  98. typedef flag_traits<FlagType> traits_type;
  99. kmp_flag() : t({FlagType, 0U}), num_waiting_threads(0), sleepLoc(nullptr) {}
  100. kmp_flag(int nwaiters)
  101. : t({FlagType, 0U}), num_waiting_threads(nwaiters), sleepLoc(nullptr) {}
  102. kmp_flag(std::atomic<bool> *sloc)
  103. : t({FlagType, 0U}), num_waiting_threads(0), sleepLoc(sloc) {}
  104. /*! @result the flag_type */
  105. flag_type get_type() { return (flag_type)(t.type); }
  106. /*! param i in index into waiting_threads
  107. * @result the thread that is waiting at index i */
  108. kmp_info_t *get_waiter(kmp_uint32 i) {
  109. KMP_DEBUG_ASSERT(i < num_waiting_threads);
  110. return waiting_threads[i];
  111. }
  112. /*! @result num_waiting_threads */
  113. kmp_uint32 get_num_waiters() { return num_waiting_threads; }
  114. /*! @param thr in the thread which is now waiting
  115. * Insert a waiting thread at index 0. */
  116. void set_waiter(kmp_info_t *thr) {
  117. waiting_threads[0] = thr;
  118. num_waiting_threads = 1;
  119. }
  120. enum barrier_type get_bt() { return bs_last_barrier; }
  121. };
  122. /*! Base class for wait/release volatile flag */
  123. template <typename PtrType, flag_type FlagType, bool Sleepable>
  124. class kmp_flag_native : public kmp_flag<FlagType> {
  125. protected:
  126. volatile PtrType *loc;
  127. PtrType checker; /**< When flag==checker, it has been released. */
  128. typedef flag_traits<FlagType> traits_type;
  129. public:
  130. typedef PtrType flag_t;
  131. kmp_flag_native(volatile PtrType *p) : kmp_flag<FlagType>(), loc(p) {}
  132. kmp_flag_native(volatile PtrType *p, kmp_info_t *thr)
  133. : kmp_flag<FlagType>(1), loc(p) {
  134. this->waiting_threads[0] = thr;
  135. }
  136. kmp_flag_native(volatile PtrType *p, PtrType c)
  137. : kmp_flag<FlagType>(), loc(p), checker(c) {}
  138. kmp_flag_native(volatile PtrType *p, PtrType c, std::atomic<bool> *sloc)
  139. : kmp_flag<FlagType>(sloc), loc(p), checker(c) {}
  140. virtual ~kmp_flag_native() {}
  141. void *operator new(size_t size) { return __kmp_allocate(size); }
  142. void operator delete(void *p) { __kmp_free(p); }
  143. volatile PtrType *get() { return loc; }
  144. void *get_void_p() { return RCAST(void *, CCAST(PtrType *, loc)); }
  145. void set(volatile PtrType *new_loc) { loc = new_loc; }
  146. PtrType load() { return *loc; }
  147. void store(PtrType val) { *loc = val; }
  148. /*! @result true if the flag object has been released. */
  149. virtual bool done_check() {
  150. if (Sleepable && !(this->sleepLoc))
  151. return (traits_type::tcr(*(this->get())) & ~KMP_BARRIER_SLEEP_STATE) ==
  152. checker;
  153. else
  154. return traits_type::tcr(*(this->get())) == checker;
  155. }
  156. /*! @param old_loc in old value of flag
  157. * @result true if the flag's old value indicates it was released. */
  158. virtual bool done_check_val(PtrType old_loc) { return old_loc == checker; }
  159. /*! @result true if the flag object is not yet released.
  160. * Used in __kmp_wait_template like:
  161. * @code
  162. * while (flag.notdone_check()) { pause(); }
  163. * @endcode */
  164. virtual bool notdone_check() {
  165. return traits_type::tcr(*(this->get())) != checker;
  166. }
  167. /*! @result Actual flag value before release was applied.
  168. * Trigger all waiting threads to run by modifying flag to release state. */
  169. void internal_release() {
  170. (void)traits_type::test_then_add4((volatile PtrType *)this->get());
  171. }
  172. /*! @result Actual flag value before sleep bit(s) set.
  173. * Notes that there is at least one thread sleeping on the flag by setting
  174. * sleep bit(s). */
  175. PtrType set_sleeping() {
  176. if (this->sleepLoc) {
  177. this->sleepLoc->store(true);
  178. return *(this->get());
  179. }
  180. return traits_type::test_then_or((volatile PtrType *)this->get(),
  181. KMP_BARRIER_SLEEP_STATE);
  182. }
  183. /*! @result Actual flag value before sleep bit(s) cleared.
  184. * Notes that there are no longer threads sleeping on the flag by clearing
  185. * sleep bit(s). */
  186. void unset_sleeping() {
  187. if (this->sleepLoc) {
  188. this->sleepLoc->store(false);
  189. return;
  190. }
  191. traits_type::test_then_and((volatile PtrType *)this->get(),
  192. ~KMP_BARRIER_SLEEP_STATE);
  193. }
  194. /*! @param old_loc in old value of flag
  195. * Test if there are threads sleeping on the flag's old value in old_loc. */
  196. bool is_sleeping_val(PtrType old_loc) {
  197. if (this->sleepLoc)
  198. return this->sleepLoc->load();
  199. return old_loc & KMP_BARRIER_SLEEP_STATE;
  200. }
  201. /*! Test whether there are threads sleeping on the flag. */
  202. bool is_sleeping() {
  203. if (this->sleepLoc)
  204. return this->sleepLoc->load();
  205. return is_sleeping_val(*(this->get()));
  206. }
  207. bool is_any_sleeping() {
  208. if (this->sleepLoc)
  209. return this->sleepLoc->load();
  210. return is_sleeping_val(*(this->get()));
  211. }
  212. kmp_uint8 *get_stolen() { return NULL; }
  213. };
  214. /*! Base class for wait/release atomic flag */
  215. template <typename PtrType, flag_type FlagType, bool Sleepable>
  216. class kmp_flag_atomic : public kmp_flag<FlagType> {
  217. protected:
  218. std::atomic<PtrType> *loc; /**< Pointer to flag location to wait on */
  219. PtrType checker; /**< Flag == checker means it has been released. */
  220. public:
  221. typedef flag_traits<FlagType> traits_type;
  222. typedef PtrType flag_t;
  223. kmp_flag_atomic(std::atomic<PtrType> *p) : kmp_flag<FlagType>(), loc(p) {}
  224. kmp_flag_atomic(std::atomic<PtrType> *p, kmp_info_t *thr)
  225. : kmp_flag<FlagType>(1), loc(p) {
  226. this->waiting_threads[0] = thr;
  227. }
  228. kmp_flag_atomic(std::atomic<PtrType> *p, PtrType c)
  229. : kmp_flag<FlagType>(), loc(p), checker(c) {}
  230. kmp_flag_atomic(std::atomic<PtrType> *p, PtrType c, std::atomic<bool> *sloc)
  231. : kmp_flag<FlagType>(sloc), loc(p), checker(c) {}
  232. /*! @result the pointer to the actual flag */
  233. std::atomic<PtrType> *get() { return loc; }
  234. /*! @result void* pointer to the actual flag */
  235. void *get_void_p() { return RCAST(void *, loc); }
  236. /*! @param new_loc in set loc to point at new_loc */
  237. void set(std::atomic<PtrType> *new_loc) { loc = new_loc; }
  238. /*! @result flag value */
  239. PtrType load() { return loc->load(std::memory_order_acquire); }
  240. /*! @param val the new flag value to be stored */
  241. void store(PtrType val) { loc->store(val, std::memory_order_release); }
  242. /*! @result true if the flag object has been released. */
  243. bool done_check() {
  244. if (Sleepable && !(this->sleepLoc))
  245. return (this->load() & ~KMP_BARRIER_SLEEP_STATE) == checker;
  246. else
  247. return this->load() == checker;
  248. }
  249. /*! @param old_loc in old value of flag
  250. * @result true if the flag's old value indicates it was released. */
  251. bool done_check_val(PtrType old_loc) { return old_loc == checker; }
  252. /*! @result true if the flag object is not yet released.
  253. * Used in __kmp_wait_template like:
  254. * @code
  255. * while (flag.notdone_check()) { pause(); }
  256. * @endcode */
  257. bool notdone_check() { return this->load() != checker; }
  258. /*! @result Actual flag value before release was applied.
  259. * Trigger all waiting threads to run by modifying flag to release state. */
  260. void internal_release() { KMP_ATOMIC_ADD(this->get(), 4); }
  261. /*! @result Actual flag value before sleep bit(s) set.
  262. * Notes that there is at least one thread sleeping on the flag by setting
  263. * sleep bit(s). */
  264. PtrType set_sleeping() {
  265. if (this->sleepLoc) {
  266. this->sleepLoc->store(true);
  267. return *(this->get());
  268. }
  269. return KMP_ATOMIC_OR(this->get(), KMP_BARRIER_SLEEP_STATE);
  270. }
  271. /*! @result Actual flag value before sleep bit(s) cleared.
  272. * Notes that there are no longer threads sleeping on the flag by clearing
  273. * sleep bit(s). */
  274. void unset_sleeping() {
  275. if (this->sleepLoc) {
  276. this->sleepLoc->store(false);
  277. return;
  278. }
  279. KMP_ATOMIC_AND(this->get(), ~KMP_BARRIER_SLEEP_STATE);
  280. }
  281. /*! @param old_loc in old value of flag
  282. * Test whether there are threads sleeping on flag's old value in old_loc. */
  283. bool is_sleeping_val(PtrType old_loc) {
  284. if (this->sleepLoc)
  285. return this->sleepLoc->load();
  286. return old_loc & KMP_BARRIER_SLEEP_STATE;
  287. }
  288. /*! Test whether there are threads sleeping on the flag. */
  289. bool is_sleeping() {
  290. if (this->sleepLoc)
  291. return this->sleepLoc->load();
  292. return is_sleeping_val(this->load());
  293. }
  294. bool is_any_sleeping() {
  295. if (this->sleepLoc)
  296. return this->sleepLoc->load();
  297. return is_sleeping_val(this->load());
  298. }
  299. kmp_uint8 *get_stolen() { return NULL; }
  300. };
  301. #if OMPT_SUPPORT
  302. OMPT_NOINLINE
  303. static void __ompt_implicit_task_end(kmp_info_t *this_thr,
  304. ompt_state_t ompt_state,
  305. ompt_data_t *tId) {
  306. int ds_tid = this_thr->th.th_info.ds.ds_tid;
  307. if (ompt_state == ompt_state_wait_barrier_implicit) {
  308. this_thr->th.ompt_thread_info.state = ompt_state_overhead;
  309. #if OMPT_OPTIONAL
  310. void *codeptr = NULL;
  311. if (ompt_enabled.ompt_callback_sync_region_wait) {
  312. ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
  313. ompt_sync_region_barrier_implicit, ompt_scope_end, NULL, tId,
  314. codeptr);
  315. }
  316. if (ompt_enabled.ompt_callback_sync_region) {
  317. ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
  318. ompt_sync_region_barrier_implicit, ompt_scope_end, NULL, tId,
  319. codeptr);
  320. }
  321. #endif
  322. if (!KMP_MASTER_TID(ds_tid)) {
  323. if (ompt_enabled.ompt_callback_implicit_task) {
  324. int flags = this_thr->th.ompt_thread_info.parallel_flags;
  325. flags = (flags & ompt_parallel_league) ? ompt_task_initial
  326. : ompt_task_implicit;
  327. ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
  328. ompt_scope_end, NULL, tId, 0, ds_tid, flags);
  329. }
  330. // return to idle state
  331. this_thr->th.ompt_thread_info.state = ompt_state_idle;
  332. } else {
  333. this_thr->th.ompt_thread_info.state = ompt_state_overhead;
  334. }
  335. }
  336. }
  337. #endif
  338. /* Spin wait loop that first does pause/yield, then sleep. A thread that calls
  339. __kmp_wait_* must make certain that another thread calls __kmp_release
  340. to wake it back up to prevent deadlocks!
  341. NOTE: We may not belong to a team at this point. */
  342. template <class C, bool final_spin, bool Cancellable = false,
  343. bool Sleepable = true>
  344. static inline bool
  345. __kmp_wait_template(kmp_info_t *this_thr,
  346. C *flag USE_ITT_BUILD_ARG(void *itt_sync_obj)) {
  347. #if USE_ITT_BUILD && USE_ITT_NOTIFY
  348. volatile void *spin = flag->get();
  349. #endif
  350. kmp_uint32 spins;
  351. int th_gtid;
  352. int tasks_completed = FALSE;
  353. #if !KMP_USE_MONITOR
  354. kmp_uint64 poll_count;
  355. kmp_uint64 hibernate_goal;
  356. #else
  357. kmp_uint32 hibernate;
  358. #endif
  359. kmp_uint64 time;
  360. KMP_FSYNC_SPIN_INIT(spin, NULL);
  361. if (flag->done_check()) {
  362. KMP_FSYNC_SPIN_ACQUIRED(CCAST(void *, spin));
  363. return false;
  364. }
  365. th_gtid = this_thr->th.th_info.ds.ds_gtid;
  366. if (Cancellable) {
  367. kmp_team_t *team = this_thr->th.th_team;
  368. if (team && team->t.t_cancel_request == cancel_parallel)
  369. return true;
  370. }
  371. #if KMP_OS_UNIX
  372. if (final_spin)
  373. KMP_ATOMIC_ST_REL(&this_thr->th.th_blocking, true);
  374. #endif
  375. KA_TRACE(20,
  376. ("__kmp_wait_sleep: T#%d waiting for flag(%p)\n", th_gtid, flag));
  377. #if KMP_STATS_ENABLED
  378. stats_state_e thread_state = KMP_GET_THREAD_STATE();
  379. #endif
  380. /* OMPT Behavior:
  381. THIS function is called from
  382. __kmp_barrier (2 times) (implicit or explicit barrier in parallel regions)
  383. these have join / fork behavior
  384. In these cases, we don't change the state or trigger events in THIS
  385. function.
  386. Events are triggered in the calling code (__kmp_barrier):
  387. state := ompt_state_overhead
  388. barrier-begin
  389. barrier-wait-begin
  390. state := ompt_state_wait_barrier
  391. call join-barrier-implementation (finally arrive here)
  392. {}
  393. call fork-barrier-implementation (finally arrive here)
  394. {}
  395. state := ompt_state_overhead
  396. barrier-wait-end
  397. barrier-end
  398. state := ompt_state_work_parallel
  399. __kmp_fork_barrier (after thread creation, before executing implicit task)
  400. call fork-barrier-implementation (finally arrive here)
  401. {} // worker arrive here with state = ompt_state_idle
  402. __kmp_join_barrier (implicit barrier at end of parallel region)
  403. state := ompt_state_barrier_implicit
  404. barrier-begin
  405. barrier-wait-begin
  406. call join-barrier-implementation (finally arrive here
  407. final_spin=FALSE)
  408. {
  409. }
  410. __kmp_fork_barrier (implicit barrier at end of parallel region)
  411. call fork-barrier-implementation (finally arrive here final_spin=TRUE)
  412. Worker after task-team is finished:
  413. barrier-wait-end
  414. barrier-end
  415. implicit-task-end
  416. idle-begin
  417. state := ompt_state_idle
  418. Before leaving, if state = ompt_state_idle
  419. idle-end
  420. state := ompt_state_overhead
  421. */
  422. #if OMPT_SUPPORT
  423. ompt_state_t ompt_entry_state;
  424. ompt_data_t *tId;
  425. if (ompt_enabled.enabled) {
  426. ompt_entry_state = this_thr->th.ompt_thread_info.state;
  427. if (!final_spin || ompt_entry_state != ompt_state_wait_barrier_implicit ||
  428. KMP_MASTER_TID(this_thr->th.th_info.ds.ds_tid)) {
  429. ompt_lw_taskteam_t *team = NULL;
  430. if (this_thr->th.th_team)
  431. team = this_thr->th.th_team->t.ompt_serialized_team_info;
  432. if (team) {
  433. tId = &(team->ompt_task_info.task_data);
  434. } else {
  435. tId = OMPT_CUR_TASK_DATA(this_thr);
  436. }
  437. } else {
  438. tId = &(this_thr->th.ompt_thread_info.task_data);
  439. }
  440. if (final_spin && (__kmp_tasking_mode == tskm_immediate_exec ||
  441. this_thr->th.th_task_team == NULL)) {
  442. // implicit task is done. Either no taskqueue, or task-team finished
  443. __ompt_implicit_task_end(this_thr, ompt_entry_state, tId);
  444. }
  445. }
  446. #endif
  447. KMP_INIT_YIELD(spins); // Setup for waiting
  448. KMP_INIT_BACKOFF(time);
  449. if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME ||
  450. __kmp_pause_status == kmp_soft_paused) {
  451. #if KMP_USE_MONITOR
  452. // The worker threads cannot rely on the team struct existing at this point.
  453. // Use the bt values cached in the thread struct instead.
  454. #ifdef KMP_ADJUST_BLOCKTIME
  455. if (__kmp_pause_status == kmp_soft_paused ||
  456. (__kmp_zero_bt && !this_thr->th.th_team_bt_set))
  457. // Force immediate suspend if not set by user and more threads than
  458. // available procs
  459. hibernate = 0;
  460. else
  461. hibernate = this_thr->th.th_team_bt_intervals;
  462. #else
  463. hibernate = this_thr->th.th_team_bt_intervals;
  464. #endif /* KMP_ADJUST_BLOCKTIME */
  465. /* If the blocktime is nonzero, we want to make sure that we spin wait for
  466. the entirety of the specified #intervals, plus up to one interval more.
  467. This increment make certain that this thread doesn't go to sleep too
  468. soon. */
  469. if (hibernate != 0)
  470. hibernate++;
  471. // Add in the current time value.
  472. hibernate += TCR_4(__kmp_global.g.g_time.dt.t_value);
  473. KF_TRACE(20, ("__kmp_wait_sleep: T#%d now=%d, hibernate=%d, intervals=%d\n",
  474. th_gtid, __kmp_global.g.g_time.dt.t_value, hibernate,
  475. hibernate - __kmp_global.g.g_time.dt.t_value));
  476. #else
  477. if (__kmp_pause_status == kmp_soft_paused) {
  478. // Force immediate suspend
  479. hibernate_goal = KMP_NOW();
  480. } else
  481. hibernate_goal = KMP_NOW() + this_thr->th.th_team_bt_intervals;
  482. poll_count = 0;
  483. (void)poll_count;
  484. #endif // KMP_USE_MONITOR
  485. }
  486. KMP_MB();
  487. // Main wait spin loop
  488. while (flag->notdone_check()) {
  489. kmp_task_team_t *task_team = NULL;
  490. if (__kmp_tasking_mode != tskm_immediate_exec) {
  491. task_team = this_thr->th.th_task_team;
  492. /* If the thread's task team pointer is NULL, it means one of 3 things:
  493. 1) A newly-created thread is first being released by
  494. __kmp_fork_barrier(), and its task team has not been set up yet.
  495. 2) All tasks have been executed to completion.
  496. 3) Tasking is off for this region. This could be because we are in a
  497. serialized region (perhaps the outer one), or else tasking was manually
  498. disabled (KMP_TASKING=0). */
  499. if (task_team != NULL) {
  500. if (TCR_SYNC_4(task_team->tt.tt_active)) {
  501. if (KMP_TASKING_ENABLED(task_team)) {
  502. flag->execute_tasks(
  503. this_thr, th_gtid, final_spin,
  504. &tasks_completed USE_ITT_BUILD_ARG(itt_sync_obj), 0);
  505. } else
  506. this_thr->th.th_reap_state = KMP_SAFE_TO_REAP;
  507. } else {
  508. KMP_DEBUG_ASSERT(!KMP_MASTER_TID(this_thr->th.th_info.ds.ds_tid));
  509. #if OMPT_SUPPORT
  510. // task-team is done now, other cases should be catched above
  511. if (final_spin && ompt_enabled.enabled)
  512. __ompt_implicit_task_end(this_thr, ompt_entry_state, tId);
  513. #endif
  514. this_thr->th.th_task_team = NULL;
  515. this_thr->th.th_reap_state = KMP_SAFE_TO_REAP;
  516. }
  517. } else {
  518. this_thr->th.th_reap_state = KMP_SAFE_TO_REAP;
  519. } // if
  520. } // if
  521. KMP_FSYNC_SPIN_PREPARE(CCAST(void *, spin));
  522. if (TCR_4(__kmp_global.g.g_done)) {
  523. if (__kmp_global.g.g_abort)
  524. __kmp_abort_thread();
  525. break;
  526. }
  527. // If we are oversubscribed, or have waited a bit (and
  528. // KMP_LIBRARY=throughput), then yield
  529. KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
  530. #if KMP_STATS_ENABLED
  531. // Check if thread has been signalled to idle state
  532. // This indicates that the logical "join-barrier" has finished
  533. if (this_thr->th.th_stats->isIdle() &&
  534. KMP_GET_THREAD_STATE() == FORK_JOIN_BARRIER) {
  535. KMP_SET_THREAD_STATE(IDLE);
  536. KMP_PUSH_PARTITIONED_TIMER(OMP_idle);
  537. }
  538. #endif
  539. // Check if the barrier surrounding this wait loop has been cancelled
  540. if (Cancellable) {
  541. kmp_team_t *team = this_thr->th.th_team;
  542. if (team && team->t.t_cancel_request == cancel_parallel)
  543. break;
  544. }
  545. // For hidden helper thread, if task_team is nullptr, it means the main
  546. // thread has not released the barrier. We cannot wait here because once the
  547. // main thread releases all children barriers, all hidden helper threads are
  548. // still sleeping. This leads to a problem that following configuration,
  549. // such as task team sync, will not be performed such that this thread does
  550. // not have task team. Usually it is not bad. However, a corner case is,
  551. // when the first task encountered is an untied task, the check in
  552. // __kmp_task_alloc will crash because it uses the task team pointer without
  553. // checking whether it is nullptr. It is probably under some kind of
  554. // assumption.
  555. if (task_team && KMP_HIDDEN_HELPER_WORKER_THREAD(th_gtid) &&
  556. !TCR_4(__kmp_hidden_helper_team_done)) {
  557. // If there is still hidden helper tasks to be executed, the hidden helper
  558. // thread will not enter a waiting status.
  559. if (KMP_ATOMIC_LD_ACQ(&__kmp_unexecuted_hidden_helper_tasks) == 0) {
  560. __kmp_hidden_helper_worker_thread_wait();
  561. }
  562. continue;
  563. }
  564. // Don't suspend if KMP_BLOCKTIME is set to "infinite"
  565. if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME &&
  566. __kmp_pause_status != kmp_soft_paused)
  567. continue;
  568. // Don't suspend if there is a likelihood of new tasks being spawned.
  569. if (task_team != NULL && TCR_4(task_team->tt.tt_found_tasks) &&
  570. !__kmp_wpolicy_passive)
  571. continue;
  572. #if KMP_USE_MONITOR
  573. // If we have waited a bit more, fall asleep
  574. if (TCR_4(__kmp_global.g.g_time.dt.t_value) < hibernate)
  575. continue;
  576. #else
  577. if (KMP_BLOCKING(hibernate_goal, poll_count++))
  578. continue;
  579. #endif
  580. // Don't suspend if wait loop designated non-sleepable
  581. // in template parameters
  582. if (!Sleepable)
  583. continue;
  584. #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  585. if (__kmp_mwait_enabled || __kmp_umwait_enabled) {
  586. KF_TRACE(50, ("__kmp_wait_sleep: T#%d using monitor/mwait\n", th_gtid));
  587. flag->mwait(th_gtid);
  588. } else {
  589. #endif
  590. KF_TRACE(50, ("__kmp_wait_sleep: T#%d suspend time reached\n", th_gtid));
  591. #if KMP_OS_UNIX
  592. if (final_spin)
  593. KMP_ATOMIC_ST_REL(&this_thr->th.th_blocking, false);
  594. #endif
  595. flag->suspend(th_gtid);
  596. #if KMP_OS_UNIX
  597. if (final_spin)
  598. KMP_ATOMIC_ST_REL(&this_thr->th.th_blocking, true);
  599. #endif
  600. #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  601. }
  602. #endif
  603. if (TCR_4(__kmp_global.g.g_done)) {
  604. if (__kmp_global.g.g_abort)
  605. __kmp_abort_thread();
  606. break;
  607. } else if (__kmp_tasking_mode != tskm_immediate_exec &&
  608. this_thr->th.th_reap_state == KMP_SAFE_TO_REAP) {
  609. this_thr->th.th_reap_state = KMP_NOT_SAFE_TO_REAP;
  610. }
  611. // TODO: If thread is done with work and times out, disband/free
  612. }
  613. #if OMPT_SUPPORT
  614. ompt_state_t ompt_exit_state = this_thr->th.ompt_thread_info.state;
  615. if (ompt_enabled.enabled && ompt_exit_state != ompt_state_undefined) {
  616. #if OMPT_OPTIONAL
  617. if (final_spin) {
  618. __ompt_implicit_task_end(this_thr, ompt_exit_state, tId);
  619. ompt_exit_state = this_thr->th.ompt_thread_info.state;
  620. }
  621. #endif
  622. if (ompt_exit_state == ompt_state_idle) {
  623. this_thr->th.ompt_thread_info.state = ompt_state_overhead;
  624. }
  625. }
  626. #endif
  627. #if KMP_STATS_ENABLED
  628. // If we were put into idle state, pop that off the state stack
  629. if (KMP_GET_THREAD_STATE() == IDLE) {
  630. KMP_POP_PARTITIONED_TIMER();
  631. KMP_SET_THREAD_STATE(thread_state);
  632. this_thr->th.th_stats->resetIdleFlag();
  633. }
  634. #endif
  635. #if KMP_OS_UNIX
  636. if (final_spin)
  637. KMP_ATOMIC_ST_REL(&this_thr->th.th_blocking, false);
  638. #endif
  639. KMP_FSYNC_SPIN_ACQUIRED(CCAST(void *, spin));
  640. if (Cancellable) {
  641. kmp_team_t *team = this_thr->th.th_team;
  642. if (team && team->t.t_cancel_request == cancel_parallel) {
  643. if (tasks_completed) {
  644. // undo the previous decrement of unfinished_threads so that the
  645. // thread can decrement at the join barrier with no problem
  646. kmp_task_team_t *task_team = this_thr->th.th_task_team;
  647. std::atomic<kmp_int32> *unfinished_threads =
  648. &(task_team->tt.tt_unfinished_threads);
  649. KMP_ATOMIC_INC(unfinished_threads);
  650. }
  651. return true;
  652. }
  653. }
  654. return false;
  655. }
  656. #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  657. // Set up a monitor on the flag variable causing the calling thread to wait in
  658. // a less active state until the flag variable is modified.
  659. template <class C>
  660. static inline void __kmp_mwait_template(int th_gtid, C *flag) {
  661. KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_mwait);
  662. kmp_info_t *th = __kmp_threads[th_gtid];
  663. KF_TRACE(30, ("__kmp_mwait_template: T#%d enter for flag = %p\n", th_gtid,
  664. flag->get()));
  665. // User-level mwait is available
  666. KMP_DEBUG_ASSERT(__kmp_mwait_enabled || __kmp_umwait_enabled);
  667. __kmp_suspend_initialize_thread(th);
  668. __kmp_lock_suspend_mx(th);
  669. volatile void *spin = flag->get();
  670. void *cacheline = (void *)(kmp_uintptr_t(spin) & ~(CACHE_LINE - 1));
  671. if (!flag->done_check()) {
  672. // Mark thread as no longer active
  673. th->th.th_active = FALSE;
  674. if (th->th.th_active_in_pool) {
  675. th->th.th_active_in_pool = FALSE;
  676. KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
  677. KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
  678. }
  679. flag->set_sleeping();
  680. KF_TRACE(50, ("__kmp_mwait_template: T#%d calling monitor\n", th_gtid));
  681. #if KMP_HAVE_UMWAIT
  682. if (__kmp_umwait_enabled) {
  683. __kmp_umonitor(cacheline);
  684. }
  685. #elif KMP_HAVE_MWAIT
  686. if (__kmp_mwait_enabled) {
  687. __kmp_mm_monitor(cacheline, 0, 0);
  688. }
  689. #endif
  690. // To avoid a race, check flag between 'monitor' and 'mwait'. A write to
  691. // the address could happen after the last time we checked and before
  692. // monitoring started, in which case monitor can't detect the change.
  693. if (flag->done_check())
  694. flag->unset_sleeping();
  695. else {
  696. // if flag changes here, wake-up happens immediately
  697. TCW_PTR(th->th.th_sleep_loc, (void *)flag);
  698. th->th.th_sleep_loc_type = flag->get_type();
  699. __kmp_unlock_suspend_mx(th);
  700. KF_TRACE(50, ("__kmp_mwait_template: T#%d calling mwait\n", th_gtid));
  701. #if KMP_HAVE_UMWAIT
  702. if (__kmp_umwait_enabled) {
  703. __kmp_umwait(1, 100); // to do: enable ctrl via hints, backoff counter
  704. }
  705. #elif KMP_HAVE_MWAIT
  706. if (__kmp_mwait_enabled) {
  707. __kmp_mm_mwait(0, __kmp_mwait_hints);
  708. }
  709. #endif
  710. KF_TRACE(50, ("__kmp_mwait_template: T#%d mwait done\n", th_gtid));
  711. __kmp_lock_suspend_mx(th);
  712. // Clean up sleep info; doesn't matter how/why this thread stopped waiting
  713. if (flag->is_sleeping())
  714. flag->unset_sleeping();
  715. TCW_PTR(th->th.th_sleep_loc, NULL);
  716. th->th.th_sleep_loc_type = flag_unset;
  717. }
  718. // Mark thread as active again
  719. th->th.th_active = TRUE;
  720. if (TCR_4(th->th.th_in_pool)) {
  721. KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
  722. th->th.th_active_in_pool = TRUE;
  723. }
  724. } // Drop out to main wait loop to check flag, handle tasks, etc.
  725. __kmp_unlock_suspend_mx(th);
  726. KF_TRACE(30, ("__kmp_mwait_template: T#%d exit\n", th_gtid));
  727. }
  728. #endif // KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  729. /* Release any threads specified as waiting on the flag by releasing the flag
  730. and resume the waiting thread if indicated by the sleep bit(s). A thread that
  731. calls __kmp_wait_template must call this function to wake up the potentially
  732. sleeping thread and prevent deadlocks! */
  733. template <class C> static inline void __kmp_release_template(C *flag) {
  734. #ifdef KMP_DEBUG
  735. int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
  736. #endif
  737. KF_TRACE(20, ("__kmp_release: T#%d releasing flag(%x)\n", gtid, flag->get()));
  738. KMP_DEBUG_ASSERT(flag->get());
  739. KMP_FSYNC_RELEASING(flag->get_void_p());
  740. flag->internal_release();
  741. KF_TRACE(100, ("__kmp_release: T#%d set new spin=%d\n", gtid, flag->get(),
  742. flag->load()));
  743. if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
  744. // Only need to check sleep stuff if infinite block time not set.
  745. // Are *any* threads waiting on flag sleeping?
  746. if (flag->is_any_sleeping()) {
  747. for (unsigned int i = 0; i < flag->get_num_waiters(); ++i) {
  748. // if sleeping waiter exists at i, sets current_waiter to i inside flag
  749. kmp_info_t *waiter = flag->get_waiter(i);
  750. if (waiter) {
  751. int wait_gtid = waiter->th.th_info.ds.ds_gtid;
  752. // Wake up thread if needed
  753. KF_TRACE(50, ("__kmp_release: T#%d waking up thread T#%d since sleep "
  754. "flag(%p) set\n",
  755. gtid, wait_gtid, flag->get()));
  756. flag->resume(wait_gtid); // unsets flag's current_waiter when done
  757. }
  758. }
  759. }
  760. }
  761. }
  762. template <bool Cancellable, bool Sleepable>
  763. class kmp_flag_32 : public kmp_flag_atomic<kmp_uint32, flag32, Sleepable> {
  764. public:
  765. kmp_flag_32(std::atomic<kmp_uint32> *p)
  766. : kmp_flag_atomic<kmp_uint32, flag32, Sleepable>(p) {}
  767. kmp_flag_32(std::atomic<kmp_uint32> *p, kmp_info_t *thr)
  768. : kmp_flag_atomic<kmp_uint32, flag32, Sleepable>(p, thr) {}
  769. kmp_flag_32(std::atomic<kmp_uint32> *p, kmp_uint32 c)
  770. : kmp_flag_atomic<kmp_uint32, flag32, Sleepable>(p, c) {}
  771. void suspend(int th_gtid) { __kmp_suspend_32(th_gtid, this); }
  772. #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  773. void mwait(int th_gtid) { __kmp_mwait_32(th_gtid, this); }
  774. #endif
  775. void resume(int th_gtid) { __kmp_resume_32(th_gtid, this); }
  776. int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid, int final_spin,
  777. int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
  778. kmp_int32 is_constrained) {
  779. return __kmp_execute_tasks_32(
  780. this_thr, gtid, this, final_spin,
  781. thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
  782. }
  783. bool wait(kmp_info_t *this_thr,
  784. int final_spin USE_ITT_BUILD_ARG(void *itt_sync_obj)) {
  785. if (final_spin)
  786. return __kmp_wait_template<kmp_flag_32, TRUE, Cancellable, Sleepable>(
  787. this_thr, this USE_ITT_BUILD_ARG(itt_sync_obj));
  788. else
  789. return __kmp_wait_template<kmp_flag_32, FALSE, Cancellable, Sleepable>(
  790. this_thr, this USE_ITT_BUILD_ARG(itt_sync_obj));
  791. }
  792. void release() { __kmp_release_template(this); }
  793. flag_type get_ptr_type() { return flag32; }
  794. };
  795. template <bool Cancellable, bool Sleepable>
  796. class kmp_flag_64 : public kmp_flag_native<kmp_uint64, flag64, Sleepable> {
  797. public:
  798. kmp_flag_64(volatile kmp_uint64 *p)
  799. : kmp_flag_native<kmp_uint64, flag64, Sleepable>(p) {}
  800. kmp_flag_64(volatile kmp_uint64 *p, kmp_info_t *thr)
  801. : kmp_flag_native<kmp_uint64, flag64, Sleepable>(p, thr) {}
  802. kmp_flag_64(volatile kmp_uint64 *p, kmp_uint64 c)
  803. : kmp_flag_native<kmp_uint64, flag64, Sleepable>(p, c) {}
  804. kmp_flag_64(volatile kmp_uint64 *p, kmp_uint64 c, std::atomic<bool> *loc)
  805. : kmp_flag_native<kmp_uint64, flag64, Sleepable>(p, c, loc) {}
  806. void suspend(int th_gtid) { __kmp_suspend_64(th_gtid, this); }
  807. #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  808. void mwait(int th_gtid) { __kmp_mwait_64(th_gtid, this); }
  809. #endif
  810. void resume(int th_gtid) { __kmp_resume_64(th_gtid, this); }
  811. int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid, int final_spin,
  812. int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
  813. kmp_int32 is_constrained) {
  814. return __kmp_execute_tasks_64(
  815. this_thr, gtid, this, final_spin,
  816. thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
  817. }
  818. bool wait(kmp_info_t *this_thr,
  819. int final_spin USE_ITT_BUILD_ARG(void *itt_sync_obj)) {
  820. if (final_spin)
  821. return __kmp_wait_template<kmp_flag_64, TRUE, Cancellable, Sleepable>(
  822. this_thr, this USE_ITT_BUILD_ARG(itt_sync_obj));
  823. else
  824. return __kmp_wait_template<kmp_flag_64, FALSE, Cancellable, Sleepable>(
  825. this_thr, this USE_ITT_BUILD_ARG(itt_sync_obj));
  826. }
  827. void release() { __kmp_release_template(this); }
  828. flag_type get_ptr_type() { return flag64; }
  829. };
  830. template <bool Cancellable, bool Sleepable>
  831. class kmp_atomic_flag_64
  832. : public kmp_flag_atomic<kmp_uint64, atomic_flag64, Sleepable> {
  833. public:
  834. kmp_atomic_flag_64(std::atomic<kmp_uint64> *p)
  835. : kmp_flag_atomic<kmp_uint64, atomic_flag64, Sleepable>(p) {}
  836. kmp_atomic_flag_64(std::atomic<kmp_uint64> *p, kmp_info_t *thr)
  837. : kmp_flag_atomic<kmp_uint64, atomic_flag64, Sleepable>(p, thr) {}
  838. kmp_atomic_flag_64(std::atomic<kmp_uint64> *p, kmp_uint64 c)
  839. : kmp_flag_atomic<kmp_uint64, atomic_flag64, Sleepable>(p, c) {}
  840. kmp_atomic_flag_64(std::atomic<kmp_uint64> *p, kmp_uint64 c,
  841. std::atomic<bool> *loc)
  842. : kmp_flag_atomic<kmp_uint64, atomic_flag64, Sleepable>(p, c, loc) {}
  843. void suspend(int th_gtid) { __kmp_atomic_suspend_64(th_gtid, this); }
  844. void mwait(int th_gtid) { __kmp_atomic_mwait_64(th_gtid, this); }
  845. void resume(int th_gtid) { __kmp_atomic_resume_64(th_gtid, this); }
  846. int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid, int final_spin,
  847. int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
  848. kmp_int32 is_constrained) {
  849. return __kmp_atomic_execute_tasks_64(
  850. this_thr, gtid, this, final_spin,
  851. thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
  852. }
  853. bool wait(kmp_info_t *this_thr,
  854. int final_spin USE_ITT_BUILD_ARG(void *itt_sync_obj)) {
  855. if (final_spin)
  856. return __kmp_wait_template<kmp_atomic_flag_64, TRUE, Cancellable,
  857. Sleepable>(
  858. this_thr, this USE_ITT_BUILD_ARG(itt_sync_obj));
  859. else
  860. return __kmp_wait_template<kmp_atomic_flag_64, FALSE, Cancellable,
  861. Sleepable>(
  862. this_thr, this USE_ITT_BUILD_ARG(itt_sync_obj));
  863. }
  864. void release() { __kmp_release_template(this); }
  865. flag_type get_ptr_type() { return atomic_flag64; }
  866. };
  867. // Hierarchical 64-bit on-core barrier instantiation
  868. class kmp_flag_oncore : public kmp_flag_native<kmp_uint64, flag_oncore, false> {
  869. kmp_uint32 offset; /**< Portion of flag of interest for an operation. */
  870. bool flag_switch; /**< Indicates a switch in flag location. */
  871. enum barrier_type bt; /**< Barrier type. */
  872. kmp_info_t *this_thr; /**< Thread to redirect to different flag location. */
  873. #if USE_ITT_BUILD
  874. void *itt_sync_obj; /**< ITT object to pass to new flag location. */
  875. #endif
  876. unsigned char &byteref(volatile kmp_uint64 *loc, size_t offset) {
  877. return (RCAST(unsigned char *, CCAST(kmp_uint64 *, loc)))[offset];
  878. }
  879. public:
  880. kmp_flag_oncore(volatile kmp_uint64 *p)
  881. : kmp_flag_native<kmp_uint64, flag_oncore, false>(p), flag_switch(false) {
  882. }
  883. kmp_flag_oncore(volatile kmp_uint64 *p, kmp_uint32 idx)
  884. : kmp_flag_native<kmp_uint64, flag_oncore, false>(p), offset(idx),
  885. flag_switch(false),
  886. bt(bs_last_barrier) USE_ITT_BUILD_ARG(itt_sync_obj(nullptr)) {}
  887. kmp_flag_oncore(volatile kmp_uint64 *p, kmp_uint64 c, kmp_uint32 idx,
  888. enum barrier_type bar_t,
  889. kmp_info_t *thr USE_ITT_BUILD_ARG(void *itt))
  890. : kmp_flag_native<kmp_uint64, flag_oncore, false>(p, c), offset(idx),
  891. flag_switch(false), bt(bar_t),
  892. this_thr(thr) USE_ITT_BUILD_ARG(itt_sync_obj(itt)) {}
  893. virtual ~kmp_flag_oncore() override {}
  894. void *operator new(size_t size) { return __kmp_allocate(size); }
  895. void operator delete(void *p) { __kmp_free(p); }
  896. bool done_check_val(kmp_uint64 old_loc) override {
  897. return byteref(&old_loc, offset) == checker;
  898. }
  899. bool done_check() override { return done_check_val(*get()); }
  900. bool notdone_check() override {
  901. // Calculate flag_switch
  902. if (this_thr->th.th_bar[bt].bb.wait_flag == KMP_BARRIER_SWITCH_TO_OWN_FLAG)
  903. flag_switch = true;
  904. if (byteref(get(), offset) != 1 && !flag_switch)
  905. return true;
  906. else if (flag_switch) {
  907. this_thr->th.th_bar[bt].bb.wait_flag = KMP_BARRIER_SWITCHING;
  908. kmp_flag_64<> flag(&this_thr->th.th_bar[bt].bb.b_go,
  909. (kmp_uint64)KMP_BARRIER_STATE_BUMP);
  910. __kmp_wait_64(this_thr, &flag, TRUE USE_ITT_BUILD_ARG(itt_sync_obj));
  911. }
  912. return false;
  913. }
  914. void internal_release() {
  915. // Other threads can write their own bytes simultaneously.
  916. if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
  917. byteref(get(), offset) = 1;
  918. } else {
  919. kmp_uint64 mask = 0;
  920. byteref(&mask, offset) = 1;
  921. KMP_TEST_THEN_OR64(get(), mask);
  922. }
  923. }
  924. void wait(kmp_info_t *this_thr, int final_spin) {
  925. if (final_spin)
  926. __kmp_wait_template<kmp_flag_oncore, TRUE>(
  927. this_thr, this USE_ITT_BUILD_ARG(itt_sync_obj));
  928. else
  929. __kmp_wait_template<kmp_flag_oncore, FALSE>(
  930. this_thr, this USE_ITT_BUILD_ARG(itt_sync_obj));
  931. }
  932. void release() { __kmp_release_template(this); }
  933. void suspend(int th_gtid) { __kmp_suspend_oncore(th_gtid, this); }
  934. #if KMP_HAVE_MWAIT || KMP_HAVE_UMWAIT
  935. void mwait(int th_gtid) { __kmp_mwait_oncore(th_gtid, this); }
  936. #endif
  937. void resume(int th_gtid) { __kmp_resume_oncore(th_gtid, this); }
  938. int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid, int final_spin,
  939. int *thread_finished USE_ITT_BUILD_ARG(void *itt_sync_obj),
  940. kmp_int32 is_constrained) {
  941. #if OMPD_SUPPORT
  942. int ret = __kmp_execute_tasks_oncore(
  943. this_thr, gtid, this, final_spin,
  944. thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
  945. if (ompd_state & OMPD_ENABLE_BP)
  946. ompd_bp_task_end();
  947. return ret;
  948. #else
  949. return __kmp_execute_tasks_oncore(
  950. this_thr, gtid, this, final_spin,
  951. thread_finished USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
  952. #endif
  953. }
  954. enum barrier_type get_bt() { return bt; }
  955. flag_type get_ptr_type() { return flag_oncore; }
  956. };
  957. static inline void __kmp_null_resume_wrapper(kmp_info_t *thr) {
  958. int gtid = __kmp_gtid_from_thread(thr);
  959. void *flag = CCAST(void *, thr->th.th_sleep_loc);
  960. flag_type type = thr->th.th_sleep_loc_type;
  961. if (!flag)
  962. return;
  963. // Attempt to wake up a thread: examine its type and call appropriate template
  964. switch (type) {
  965. case flag32:
  966. __kmp_resume_32(gtid, RCAST(kmp_flag_32<> *, flag));
  967. break;
  968. case flag64:
  969. __kmp_resume_64(gtid, RCAST(kmp_flag_64<> *, flag));
  970. break;
  971. case atomic_flag64:
  972. __kmp_atomic_resume_64(gtid, RCAST(kmp_atomic_flag_64<> *, flag));
  973. break;
  974. case flag_oncore:
  975. __kmp_resume_oncore(gtid, RCAST(kmp_flag_oncore *, flag));
  976. break;
  977. #ifdef KMP_DEBUG
  978. case flag_unset:
  979. KF_TRACE(100, ("__kmp_null_resume_wrapper: flag type %d is unset\n", type));
  980. break;
  981. default:
  982. KF_TRACE(100, ("__kmp_null_resume_wrapper: flag type %d does not match any "
  983. "known flag type\n",
  984. type));
  985. #endif
  986. }
  987. }
  988. /*!
  989. @}
  990. */
  991. #endif // KMP_WAIT_RELEASE_H