LLVM OpenMP* Runtime Library
kmp_wait_release.h
1/*
2 * kmp_wait_release.h -- Wait/Release implementation
3 */
4
5//===----------------------------------------------------------------------===//
6//
7// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8// See https://llvm.org/LICENSE.txt for license information.
9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef KMP_WAIT_RELEASE_H
14#define KMP_WAIT_RELEASE_H
15
16#include "kmp.h"
17#include "kmp_itt.h"
18#include "kmp_stats.h"
19#if OMPT_SUPPORT
20#include "ompt-specific.h"
21#endif
22
36struct flag_properties {
37 unsigned int type : 16;
38 unsigned int reserved : 16;
39};
40
41template <enum flag_type FlagType> struct flag_traits {};
42
43template <> struct flag_traits<flag32> {
44 typedef kmp_uint32 flag_t;
45 static const flag_type t = flag32;
46 static inline flag_t tcr(flag_t f) { return TCR_4(f); }
47 static inline flag_t test_then_add4(volatile flag_t *f) {
48 return KMP_TEST_THEN_ADD4_32(RCAST(volatile kmp_int32 *, f));
49 }
50 static inline flag_t test_then_or(volatile flag_t *f, flag_t v) {
51 return KMP_TEST_THEN_OR32(f, v);
52 }
53 static inline flag_t test_then_and(volatile flag_t *f, flag_t v) {
54 return KMP_TEST_THEN_AND32(f, v);
55 }
56};
57
58template <> struct flag_traits<atomic_flag64> {
59 typedef kmp_uint64 flag_t;
60 static const flag_type t = atomic_flag64;
61 static inline flag_t tcr(flag_t f) { return TCR_8(f); }
62 static inline flag_t test_then_add4(volatile flag_t *f) {
63 return KMP_TEST_THEN_ADD4_64(RCAST(volatile kmp_int64 *, f));
64 }
65 static inline flag_t test_then_or(volatile flag_t *f, flag_t v) {
66 return KMP_TEST_THEN_OR64(f, v);
67 }
68 static inline flag_t test_then_and(volatile flag_t *f, flag_t v) {
69 return KMP_TEST_THEN_AND64(f, v);
70 }
71};
72
73template <> struct flag_traits<flag64> {
74 typedef kmp_uint64 flag_t;
75 static const flag_type t = flag64;
76 static inline flag_t tcr(flag_t f) { return TCR_8(f); }
77 static inline flag_t test_then_add4(volatile flag_t *f) {
78 return KMP_TEST_THEN_ADD4_64(RCAST(volatile kmp_int64 *, f));
79 }
80 static inline flag_t test_then_or(volatile flag_t *f, flag_t v) {
81 return KMP_TEST_THEN_OR64(f, v);
82 }
83 static inline flag_t test_then_and(volatile flag_t *f, flag_t v) {
84 return KMP_TEST_THEN_AND64(f, v);
85 }
86};
87
88template <> struct flag_traits<flag_oncore> {
89 typedef kmp_uint64 flag_t;
90 static const flag_type t = flag_oncore;
91 static inline flag_t tcr(flag_t f) { return TCR_8(f); }
92 static inline flag_t test_then_add4(volatile flag_t *f) {
93 return KMP_TEST_THEN_ADD4_64(RCAST(volatile kmp_int64 *, f));
94 }
95 static inline flag_t test_then_or(volatile flag_t *f, flag_t v) {
96 return KMP_TEST_THEN_OR64(f, v);
97 }
98 static inline flag_t test_then_and(volatile flag_t *f, flag_t v) {
99 return KMP_TEST_THEN_AND64(f, v);
100 }
101};
102
104template <flag_type FlagType> class kmp_flag {
105protected:
106 flag_properties t;
107 kmp_info_t *waiting_threads[1];
109 std::atomic<bool> *sleepLoc;
110
111public:
112 typedef flag_traits<FlagType> traits_type;
113 kmp_flag() : t({FlagType, 0U}), num_waiting_threads(0), sleepLoc(nullptr) {}
114 kmp_flag(int nwaiters)
115 : t({FlagType, 0U}), num_waiting_threads(nwaiters), sleepLoc(nullptr) {}
116 kmp_flag(std::atomic<bool> *sloc)
117 : t({FlagType, 0U}), num_waiting_threads(0), sleepLoc(sloc) {}
119 flag_type get_type() { return (flag_type)(t.type); }
120
123 kmp_info_t *get_waiter(kmp_uint32 i) {
124 KMP_DEBUG_ASSERT(i < num_waiting_threads);
125 return waiting_threads[i];
126 }
128 kmp_uint32 get_num_waiters() { return num_waiting_threads; }
131 void set_waiter(kmp_info_t *thr) {
132 waiting_threads[0] = thr;
134 }
135 enum barrier_type get_bt() { return bs_last_barrier; }
136};
137
139template <typename PtrType, flag_type FlagType, bool Sleepable>
140class kmp_flag_native : public kmp_flag<FlagType> {
141protected:
142 volatile PtrType *loc;
143 PtrType checker;
144 typedef flag_traits<FlagType> traits_type;
145
146public:
147 typedef PtrType flag_t;
148 kmp_flag_native(volatile PtrType *p) : kmp_flag<FlagType>(), loc(p) {}
149 kmp_flag_native(volatile PtrType *p, kmp_info_t *thr)
150 : kmp_flag<FlagType>(1), loc(p) {
151 this->waiting_threads[0] = thr;
152 }
153 kmp_flag_native(volatile PtrType *p, PtrType c)
154 : kmp_flag<FlagType>(), loc(p), checker(c) {}
155 kmp_flag_native(volatile PtrType *p, PtrType c, std::atomic<bool> *sloc)
156 : kmp_flag<FlagType>(sloc), loc(p), checker(c) {}
157 virtual ~kmp_flag_native() {}
158 void *operator new(size_t size) { return __kmp_allocate(size); }
159 void operator delete(void *p) { __kmp_free(p); }
160 volatile PtrType *get() { return loc; }
161 void *get_void_p() { return RCAST(void *, CCAST(PtrType *, loc)); }
162 void set(volatile PtrType *new_loc) { loc = new_loc; }
163 PtrType load() { return *loc; }
164 void store(PtrType val) { *loc = val; }
166 virtual bool done_check() {
167 if (Sleepable && !(this->sleepLoc))
168 return (traits_type::tcr(*(this->get())) & ~KMP_BARRIER_SLEEP_STATE) ==
169 checker;
170 else
171 return traits_type::tcr(*(this->get())) == checker;
172 }
175 virtual bool done_check_val(PtrType old_loc) { return old_loc == checker; }
181 virtual bool notdone_check() {
182 return traits_type::tcr(*(this->get())) != checker;
183 }
187 (void)traits_type::test_then_add4((volatile PtrType *)this->get());
188 }
192 PtrType set_sleeping() {
193 if (this->sleepLoc) {
194 this->sleepLoc->store(true);
195 return *(this->get());
196 }
197 return traits_type::test_then_or((volatile PtrType *)this->get(),
198 KMP_BARRIER_SLEEP_STATE);
199 }
204 if (this->sleepLoc) {
205 this->sleepLoc->store(false);
206 return;
207 }
208 traits_type::test_then_and((volatile PtrType *)this->get(),
209 ~KMP_BARRIER_SLEEP_STATE);
210 }
213 bool is_sleeping_val(PtrType old_loc) {
214 if (this->sleepLoc)
215 return this->sleepLoc->load();
216 return old_loc & KMP_BARRIER_SLEEP_STATE;
217 }
219 bool is_sleeping() {
220 if (this->sleepLoc)
221 return this->sleepLoc->load();
222 return is_sleeping_val(*(this->get()));
223 }
224 bool is_any_sleeping() {
225 if (this->sleepLoc)
226 return this->sleepLoc->load();
227 return is_sleeping_val(*(this->get()));
228 }
229 kmp_uint8 *get_stolen() { return NULL; }
230};
231
233template <typename PtrType, flag_type FlagType, bool Sleepable>
234class kmp_flag_atomic : public kmp_flag<FlagType> {
235protected:
236 std::atomic<PtrType> *loc;
237 PtrType checker;
238public:
239 typedef flag_traits<FlagType> traits_type;
240 typedef PtrType flag_t;
241 kmp_flag_atomic(std::atomic<PtrType> *p) : kmp_flag<FlagType>(), loc(p) {}
242 kmp_flag_atomic(std::atomic<PtrType> *p, kmp_info_t *thr)
243 : kmp_flag<FlagType>(1), loc(p) {
244 this->waiting_threads[0] = thr;
245 }
246 kmp_flag_atomic(std::atomic<PtrType> *p, PtrType c)
247 : kmp_flag<FlagType>(), loc(p), checker(c) {}
248 kmp_flag_atomic(std::atomic<PtrType> *p, PtrType c, std::atomic<bool> *sloc)
249 : kmp_flag<FlagType>(sloc), loc(p), checker(c) {}
251 std::atomic<PtrType> *get() { return loc; }
253 void *get_void_p() { return RCAST(void *, loc); }
255 void set(std::atomic<PtrType> *new_loc) { loc = new_loc; }
257 PtrType load() { return loc->load(std::memory_order_acquire); }
259 void store(PtrType val) { loc->store(val, std::memory_order_release); }
261 bool done_check() {
262 if (Sleepable && !(this->sleepLoc))
263 return (this->load() & ~KMP_BARRIER_SLEEP_STATE) == checker;
264 else
265 return this->load() == checker;
266 }
269 bool done_check_val(PtrType old_loc) { return old_loc == checker; }
275 bool notdone_check() { return this->load() != checker; }
278 void internal_release() { KMP_ATOMIC_ADD(this->get(), 4); }
282 PtrType set_sleeping() {
283 if (this->sleepLoc) {
284 this->sleepLoc->store(true);
285 return *(this->get());
286 }
287 return KMP_ATOMIC_OR(this->get(), KMP_BARRIER_SLEEP_STATE);
288 }
293 if (this->sleepLoc) {
294 this->sleepLoc->store(false);
295 return;
296 }
297 KMP_ATOMIC_AND(this->get(), ~KMP_BARRIER_SLEEP_STATE);
298 }
301 bool is_sleeping_val(PtrType old_loc) {
302 if (this->sleepLoc)
303 return this->sleepLoc->load();
304 return old_loc & KMP_BARRIER_SLEEP_STATE;
305 }
307 bool is_sleeping() {
308 if (this->sleepLoc)
309 return this->sleepLoc->load();
310 return is_sleeping_val(this->load());
311 }
312 bool is_any_sleeping() {
313 if (this->sleepLoc)
314 return this->sleepLoc->load();
315 return is_sleeping_val(this->load());
316 }
317 kmp_uint8 *get_stolen() { return NULL; }
318};
319
320#if OMPT_SUPPORT
321OMPT_NOINLINE
322static void __ompt_implicit_task_end(kmp_info_t *this_thr,
323 ompt_state_t ompt_state,
324 ompt_data_t *tId) {
325 int ds_tid = this_thr->th.th_info.ds.ds_tid;
326 if (ompt_state == ompt_state_wait_barrier_implicit) {
327 this_thr->th.ompt_thread_info.state = ompt_state_overhead;
328#if OMPT_OPTIONAL
329 void *codeptr = NULL;
330 if (ompt_enabled.ompt_callback_sync_region_wait) {
331 ompt_callbacks.ompt_callback(ompt_callback_sync_region_wait)(
332 ompt_sync_region_barrier_implicit, ompt_scope_end, NULL, tId,
333 codeptr);
334 }
335 if (ompt_enabled.ompt_callback_sync_region) {
336 ompt_callbacks.ompt_callback(ompt_callback_sync_region)(
337 ompt_sync_region_barrier_implicit, ompt_scope_end, NULL, tId,
338 codeptr);
339 }
340#endif
341 if (!KMP_MASTER_TID(ds_tid)) {
342 if (ompt_enabled.ompt_callback_implicit_task) {
343 int flags = this_thr->th.ompt_thread_info.parallel_flags;
344 flags = (flags & ompt_parallel_league) ? ompt_task_initial
345 : ompt_task_implicit;
346 ompt_callbacks.ompt_callback(ompt_callback_implicit_task)(
347 ompt_scope_end, NULL, tId, 0, ds_tid, flags);
348 }
349 // return to idle state
350 this_thr->th.ompt_thread_info.state = ompt_state_idle;
351 } else {
352 this_thr->th.ompt_thread_info.state = ompt_state_overhead;
353 }
354 }
355}
356#endif
357
358/* Spin wait loop that first does pause/yield, then sleep. A thread that calls
359 __kmp_wait_* must make certain that another thread calls __kmp_release
360 to wake it back up to prevent deadlocks!
361
362 NOTE: We may not belong to a team at this point. */
363template <class C, bool final_spin, bool Cancellable = false,
364 bool Sleepable = true>
365static inline bool
366__kmp_wait_template(kmp_info_t *this_thr,
367 C *flag USE_ITT_BUILD_ARG(void *itt_sync_obj)) {
368#if USE_ITT_BUILD && USE_ITT_NOTIFY
369 volatile void *spin = flag->get();
370#endif
371 kmp_uint32 spins;
372 int th_gtid;
373 int tasks_completed = FALSE;
374#if !KMP_USE_MONITOR
375 kmp_uint64 poll_count;
376 kmp_uint64 hibernate_goal;
377#else
378 kmp_uint32 hibernate;
379#endif
380 kmp_uint64 time;
381
382 KMP_FSYNC_SPIN_INIT(spin, NULL);
383 if (flag->done_check()) {
384 KMP_FSYNC_SPIN_ACQUIRED(CCAST(void *, spin));
385 return false;
386 }
387 th_gtid = this_thr->th.th_info.ds.ds_gtid;
388 if (Cancellable) {
389 kmp_team_t *team = this_thr->th.th_team;
390 if (team && team->t.t_cancel_request == cancel_parallel)
391 return true;
392 }
393#if KMP_OS_UNIX
394 if (final_spin)
395 KMP_ATOMIC_ST_REL(&this_thr->th.th_blocking, true);
396#endif
397 KA_TRACE(20,
398 ("__kmp_wait_sleep: T#%d waiting for flag(%p)\n", th_gtid, flag));
399#if KMP_STATS_ENABLED
400 stats_state_e thread_state = KMP_GET_THREAD_STATE();
401#endif
402
403/* OMPT Behavior:
404THIS function is called from
405 __kmp_barrier (2 times) (implicit or explicit barrier in parallel regions)
406 these have join / fork behavior
407
408 In these cases, we don't change the state or trigger events in THIS
409function.
410 Events are triggered in the calling code (__kmp_barrier):
411
412 state := ompt_state_overhead
413 barrier-begin
414 barrier-wait-begin
415 state := ompt_state_wait_barrier
416 call join-barrier-implementation (finally arrive here)
417 {}
418 call fork-barrier-implementation (finally arrive here)
419 {}
420 state := ompt_state_overhead
421 barrier-wait-end
422 barrier-end
423 state := ompt_state_work_parallel
424
425
426 __kmp_fork_barrier (after thread creation, before executing implicit task)
427 call fork-barrier-implementation (finally arrive here)
428 {} // worker arrive here with state = ompt_state_idle
429
430
431 __kmp_join_barrier (implicit barrier at end of parallel region)
432 state := ompt_state_barrier_implicit
433 barrier-begin
434 barrier-wait-begin
435 call join-barrier-implementation (finally arrive here
436final_spin=FALSE)
437 {
438 }
439 __kmp_fork_barrier (implicit barrier at end of parallel region)
440 call fork-barrier-implementation (finally arrive here final_spin=TRUE)
441
442 Worker after task-team is finished:
443 barrier-wait-end
444 barrier-end
445 implicit-task-end
446 idle-begin
447 state := ompt_state_idle
448
449 Before leaving, if state = ompt_state_idle
450 idle-end
451 state := ompt_state_overhead
452*/
453#if OMPT_SUPPORT
454 ompt_state_t ompt_entry_state;
455 ompt_data_t *tId;
456 if (ompt_enabled.enabled) {
457 ompt_entry_state = this_thr->th.ompt_thread_info.state;
458 if (!final_spin || ompt_entry_state != ompt_state_wait_barrier_implicit ||
459 KMP_MASTER_TID(this_thr->th.th_info.ds.ds_tid)) {
460 ompt_lw_taskteam_t *team = NULL;
461 if (this_thr->th.th_team)
462 team = this_thr->th.th_team->t.ompt_serialized_team_info;
463 if (team) {
464 tId = &(team->ompt_task_info.task_data);
465 } else {
466 tId = OMPT_CUR_TASK_DATA(this_thr);
467 }
468 } else {
469 tId = &(this_thr->th.ompt_thread_info.task_data);
470 }
471 if (final_spin && (__kmp_tasking_mode == tskm_immediate_exec ||
472 this_thr->th.th_task_team == NULL)) {
473 // implicit task is done. Either no taskqueue, or task-team finished
474 __ompt_implicit_task_end(this_thr, ompt_entry_state, tId);
475 }
476 }
477#endif
478
479 KMP_INIT_YIELD(spins); // Setup for waiting
480 KMP_INIT_BACKOFF(time);
481
482 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME ||
483 __kmp_pause_status == kmp_soft_paused) {
484#if KMP_USE_MONITOR
485// The worker threads cannot rely on the team struct existing at this point.
486// Use the bt values cached in the thread struct instead.
487#ifdef KMP_ADJUST_BLOCKTIME
488 if (__kmp_pause_status == kmp_soft_paused ||
489 (__kmp_zero_bt && !this_thr->th.th_team_bt_set))
490 // Force immediate suspend if not set by user and more threads than
491 // available procs
492 hibernate = 0;
493 else
494 hibernate = this_thr->th.th_team_bt_intervals;
495#else
496 hibernate = this_thr->th.th_team_bt_intervals;
497#endif /* KMP_ADJUST_BLOCKTIME */
498
499 /* If the blocktime is nonzero, we want to make sure that we spin wait for
500 the entirety of the specified #intervals, plus up to one interval more.
501 This increment make certain that this thread doesn't go to sleep too
502 soon. */
503 if (hibernate != 0)
504 hibernate++;
505
506 // Add in the current time value.
507 hibernate += TCR_4(__kmp_global.g.g_time.dt.t_value);
508 KF_TRACE(20, ("__kmp_wait_sleep: T#%d now=%d, hibernate=%d, intervals=%d\n",
509 th_gtid, __kmp_global.g.g_time.dt.t_value, hibernate,
510 hibernate - __kmp_global.g.g_time.dt.t_value));
511#else
512 if (__kmp_pause_status == kmp_soft_paused) {
513 // Force immediate suspend
514 hibernate_goal = KMP_NOW();
515 } else
516 hibernate_goal = KMP_NOW() + this_thr->th.th_team_bt_intervals;
517 poll_count = 0;
518 (void)poll_count;
519#endif // KMP_USE_MONITOR
520 }
521
522 KMP_MB();
523
524 // Main wait spin loop
525 while (flag->notdone_check()) {
526 kmp_task_team_t *task_team = NULL;
527 if (__kmp_tasking_mode != tskm_immediate_exec) {
528 task_team = this_thr->th.th_task_team;
529 /* If the thread's task team pointer is NULL, it means one of 3 things:
530 1) A newly-created thread is first being released by
531 __kmp_fork_barrier(), and its task team has not been set up yet.
532 2) All tasks have been executed to completion.
533 3) Tasking is off for this region. This could be because we are in a
534 serialized region (perhaps the outer one), or else tasking was manually
535 disabled (KMP_TASKING=0). */
536