16 #ifndef KMP_WAIT_RELEASE_H 17 #define KMP_WAIT_RELEASE_H 57 volatile P *
get() {
return loc; }
61 void set(
volatile P *new_loc) { loc = new_loc; }
90 __kmp_wait_template(kmp_info_t *this_thr, C *flag,
int final_spin
91 USE_ITT_BUILD_ARG(
void * itt_sync_obj) )
94 volatile typename C::flag_t *spin = flag->get();
98 int tasks_completed = FALSE;
100 KMP_FSYNC_SPIN_INIT(spin, NULL);
101 if (flag->done_check()) {
102 KMP_FSYNC_SPIN_ACQUIRED(spin);
105 th_gtid = this_thr->th.th_info.ds.ds_gtid;
106 KA_TRACE(20, (
"__kmp_wait_sleep: T#%d waiting for flag(%p)\n", th_gtid, flag));
108 #if OMPT_SUPPORT && OMPT_BLAME 109 ompt_state_t ompt_state = this_thr->th.ompt_thread_info.state;
111 ompt_state != ompt_state_undefined) {
112 if (ompt_state == ompt_state_idle) {
113 if (ompt_callbacks.ompt_callback(ompt_event_idle_begin)) {
114 ompt_callbacks.ompt_callback(ompt_event_idle_begin)(th_gtid + 1);
116 }
else if (ompt_callbacks.ompt_callback(ompt_event_wait_barrier_begin)) {
117 KMP_DEBUG_ASSERT(ompt_state == ompt_state_wait_barrier ||
118 ompt_state == ompt_state_wait_barrier_implicit ||
119 ompt_state == ompt_state_wait_barrier_explicit);
121 ompt_lw_taskteam_t* team = this_thr->th.th_team->t.ompt_serialized_team_info;
122 ompt_parallel_id_t pId;
125 pId = team->ompt_team_info.parallel_id;
126 tId = team->ompt_task_info.task_id;
128 pId = this_thr->th.th_team->t.ompt_team_info.parallel_id;
129 tId = this_thr->th.th_current_task->ompt_task_info.task_id;
131 ompt_callbacks.ompt_callback(ompt_event_wait_barrier_begin)(pId, tId);
137 KMP_INIT_YIELD(spins);
139 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
142 #ifdef KMP_ADJUST_BLOCKTIME 143 if (__kmp_zero_bt && !this_thr->th.th_team_bt_set)
147 hibernate = this_thr->th.th_team_bt_intervals;
149 hibernate = this_thr->th.th_team_bt_intervals;
159 hibernate += TCR_4(__kmp_global.g.g_time.dt.t_value);
160 KF_TRACE(20, (
"__kmp_wait_sleep: T#%d now=%d, hibernate=%d, intervals=%d\n",
161 th_gtid, __kmp_global.g.g_time.dt.t_value, hibernate,
162 hibernate - __kmp_global.g.g_time.dt.t_value));
168 while (flag->notdone_check()) {
178 kmp_task_team_t * task_team = NULL;
179 if (__kmp_tasking_mode != tskm_immediate_exec) {
180 task_team = this_thr->th.th_task_team;
181 if (task_team != NULL) {
182 if (TCR_SYNC_4(task_team->tt.tt_active)) {
183 if (KMP_TASKING_ENABLED(task_team))
184 flag->execute_tasks(this_thr, th_gtid, final_spin, &tasks_completed
185 USE_ITT_BUILD_ARG(itt_sync_obj), 0);
188 KMP_DEBUG_ASSERT(!KMP_MASTER_TID(this_thr->th.th_info.ds.ds_tid));
189 this_thr->th.th_task_team = NULL;
194 KMP_FSYNC_SPIN_PREPARE(spin);
195 if (TCR_4(__kmp_global.g.g_done)) {
196 if (__kmp_global.g.g_abort)
197 __kmp_abort_thread();
202 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
206 KMP_YIELD_SPIN(spins);
210 in_pool = !!TCR_4(this_thr->th.th_in_pool);
211 if (in_pool != !!this_thr->th.th_active_in_pool) {
213 KMP_TEST_THEN_INC32((kmp_int32 *)&__kmp_thread_pool_active_nth);
214 this_thr->th.th_active_in_pool = TRUE;
222 KMP_TEST_THEN_DEC32((kmp_int32 *) &__kmp_thread_pool_active_nth);
223 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
224 this_thr->th.th_active_in_pool = FALSE;
229 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME)
233 if ((task_team != NULL) && TCR_4(task_team->tt.tt_found_tasks))
237 if (TCR_4(__kmp_global.g.g_time.dt.t_value) < hibernate)
240 KF_TRACE(50, (
"__kmp_wait_sleep: T#%d suspend time reached\n", th_gtid));
242 flag->suspend(th_gtid);
244 if (TCR_4(__kmp_global.g.g_done)) {
245 if (__kmp_global.g.g_abort)
246 __kmp_abort_thread();
252 #if OMPT_SUPPORT && OMPT_BLAME 254 ompt_state != ompt_state_undefined) {
255 if (ompt_state == ompt_state_idle) {
256 if (ompt_callbacks.ompt_callback(ompt_event_idle_end)) {
257 ompt_callbacks.ompt_callback(ompt_event_idle_end)(th_gtid + 1);
259 }
else if (ompt_callbacks.ompt_callback(ompt_event_wait_barrier_end)) {
260 KMP_DEBUG_ASSERT(ompt_state == ompt_state_wait_barrier ||
261 ompt_state == ompt_state_wait_barrier_implicit ||
262 ompt_state == ompt_state_wait_barrier_explicit);
264 ompt_lw_taskteam_t* team = this_thr->th.th_team->t.ompt_serialized_team_info;
265 ompt_parallel_id_t pId;
268 pId = team->ompt_team_info.parallel_id;
269 tId = team->ompt_task_info.task_id;
271 pId = this_thr->th.th_team->t.ompt_team_info.parallel_id;
272 tId = this_thr->th.th_current_task->ompt_task_info.task_id;
274 ompt_callbacks.ompt_callback(ompt_event_wait_barrier_end)(pId, tId);
279 KMP_FSYNC_SPIN_ACQUIRED(spin);
287 __kmp_release_template(C *flag)
290 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
292 KF_TRACE(20, (
"__kmp_release: T#%d releasing flag(%x)\n", gtid, flag->get()));
293 KMP_DEBUG_ASSERT(flag->get());
294 KMP_FSYNC_RELEASING(flag->get());
296 flag->internal_release();
298 KF_TRACE(100, (
"__kmp_release: T#%d set new spin=%d\n", gtid, flag->get(), *(flag->get())));
300 if (__kmp_dflt_blocktime != KMP_MAX_BLOCKTIME) {
302 if (flag->is_any_sleeping()) {
303 for (
unsigned int i=0; i<flag->get_num_waiters(); ++i) {
304 kmp_info_t * waiter = flag->get_waiter(i);
306 int wait_gtid = waiter->th.th_info.ds.ds_gtid;
308 KF_TRACE(50, (
"__kmp_release: T#%d waking up thread T#%d since sleep flag(%p) set\n",
309 gtid, wait_gtid, flag->get()));
310 flag->resume(wait_gtid);
317 template <
typename FlagType>
318 struct flag_traits {};
321 struct flag_traits<kmp_uint32> {
322 typedef kmp_uint32 flag_t;
324 static inline flag_t tcr(flag_t f) {
return TCR_4(f); }
325 static inline flag_t test_then_add4(
volatile flag_t *f) {
return KMP_TEST_THEN_ADD4_32((
volatile kmp_int32 *)f); }
326 static inline flag_t test_then_or(
volatile flag_t *f, flag_t v) {
return KMP_TEST_THEN_OR32((
volatile kmp_int32 *)f, v); }
327 static inline flag_t test_then_and(
volatile flag_t *f, flag_t v) {
return KMP_TEST_THEN_AND32((
volatile kmp_int32 *)f, v); }
331 struct flag_traits<kmp_uint64> {
332 typedef kmp_uint64 flag_t;
334 static inline flag_t tcr(flag_t f) {
return TCR_8(f); }
335 static inline flag_t test_then_add4(
volatile flag_t *f) {
return KMP_TEST_THEN_ADD4_64((
volatile kmp_int64 *)f); }
336 static inline flag_t test_then_or(
volatile flag_t *f, flag_t v) {
return KMP_TEST_THEN_OR64((
volatile kmp_int64 *)f, v); }
337 static inline flag_t test_then_and(
volatile flag_t *f, flag_t v) {
return KMP_TEST_THEN_AND64((
volatile kmp_int64 *)f, v); }
340 template <
typename FlagType>
341 class kmp_basic_flag :
public kmp_flag<FlagType> {
342 typedef flag_traits<FlagType> traits_type;
344 kmp_info_t * waiting_threads[1];
345 kmp_uint32 num_waiting_threads;
347 kmp_basic_flag(
volatile FlagType *p) :
kmp_flag<FlagType>(p, traits_type::t), num_waiting_threads(0) {}
348 kmp_basic_flag(
volatile FlagType *p, kmp_info_t *thr) :
kmp_flag<FlagType>(p, traits_type::t), num_waiting_threads(1) {
349 waiting_threads[0] = thr;
351 kmp_basic_flag(
volatile FlagType *p, FlagType c) :
kmp_flag<FlagType>(p, traits_type::t), checker(c), num_waiting_threads(0) {}
356 kmp_info_t * get_waiter(kmp_uint32 i) {
357 KMP_DEBUG_ASSERT(i<num_waiting_threads);
358 return waiting_threads[i];
363 kmp_uint32 get_num_waiters() {
return num_waiting_threads; }
369 void set_waiter(kmp_info_t *thr) {
370 waiting_threads[0] = thr;
371 num_waiting_threads = 1;
376 bool done_check() {
return traits_type::tcr(*(this->
get())) == checker; }
381 bool done_check_val(FlagType old_loc) {
return old_loc == checker; }
389 bool notdone_check() {
return traits_type::tcr(*(this->
get())) != checker; }
394 void internal_release() {
395 (void) traits_type::test_then_add4((
volatile FlagType *)this->
get());
401 FlagType set_sleeping() {
402 return traits_type::test_then_or((
volatile FlagType *)this->
get(), KMP_BARRIER_SLEEP_STATE);
408 FlagType unset_sleeping() {
409 return traits_type::test_then_and((
volatile FlagType *)this->
get(), ~KMP_BARRIER_SLEEP_STATE);
415 bool is_sleeping_val(FlagType old_loc) {
return old_loc & KMP_BARRIER_SLEEP_STATE; }
419 bool is_sleeping() {
return is_sleeping_val(*(this->
get())); }
420 bool is_any_sleeping() {
return is_sleeping_val(*(this->
get())); }
421 kmp_uint8 *get_stolen() {
return NULL; }
422 enum barrier_type get_bt() {
return bs_last_barrier; }
425 class kmp_flag_32 :
public kmp_basic_flag<kmp_uint32> {
427 kmp_flag_32(
volatile kmp_uint32 *p) : kmp_basic_flag<kmp_uint32>(p) {}
428 kmp_flag_32(
volatile kmp_uint32 *p, kmp_info_t *thr) : kmp_basic_flag<kmp_uint32>(p, thr) {}
429 kmp_flag_32(
volatile kmp_uint32 *p, kmp_uint32 c) : kmp_basic_flag<kmp_uint32>(p, c) {}
430 void suspend(
int th_gtid) { __kmp_suspend_32(th_gtid,
this); }
431 void resume(
int th_gtid) { __kmp_resume_32(th_gtid,
this); }
432 int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid,
int final_spin,
int *thread_finished
433 USE_ITT_BUILD_ARG(
void * itt_sync_obj), kmp_int32 is_constrained) {
434 return __kmp_execute_tasks_32(this_thr, gtid,
this, final_spin, thread_finished
435 USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
437 void wait(kmp_info_t *this_thr,
int final_spin
438 USE_ITT_BUILD_ARG(
void * itt_sync_obj)) {
439 __kmp_wait_template(this_thr,
this, final_spin
440 USE_ITT_BUILD_ARG(itt_sync_obj));
442 void release() { __kmp_release_template(
this); }
446 class kmp_flag_64 :
public kmp_basic_flag<kmp_uint64> {
448 kmp_flag_64(
volatile kmp_uint64 *p) : kmp_basic_flag<kmp_uint64>(p) {}
449 kmp_flag_64(
volatile kmp_uint64 *p, kmp_info_t *thr) : kmp_basic_flag<kmp_uint64>(p, thr) {}
450 kmp_flag_64(
volatile kmp_uint64 *p, kmp_uint64 c) : kmp_basic_flag<kmp_uint64>(p, c) {}
451 void suspend(
int th_gtid) { __kmp_suspend_64(th_gtid,
this); }
452 void resume(
int th_gtid) { __kmp_resume_64(th_gtid,
this); }
453 int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid,
int final_spin,
int *thread_finished
454 USE_ITT_BUILD_ARG(
void * itt_sync_obj), kmp_int32 is_constrained) {
455 return __kmp_execute_tasks_64(this_thr, gtid,
this, final_spin, thread_finished
456 USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
458 void wait(kmp_info_t *this_thr,
int final_spin
459 USE_ITT_BUILD_ARG(
void * itt_sync_obj)) {
460 __kmp_wait_template(this_thr,
this, final_spin
461 USE_ITT_BUILD_ARG(itt_sync_obj));
463 void release() { __kmp_release_template(
this); }
468 class kmp_flag_oncore :
public kmp_flag<kmp_uint64> {
470 kmp_info_t * waiting_threads[1];
471 kmp_uint32 num_waiting_threads;
474 enum barrier_type bt;
475 kmp_info_t * this_thr;
479 unsigned char& byteref(
volatile kmp_uint64*
loc,
size_t offset) {
return ((
unsigned char *)loc)[offset]; }
481 kmp_flag_oncore(
volatile kmp_uint64 *p)
483 kmp_flag_oncore(
volatile kmp_uint64 *p, kmp_uint32 idx)
485 kmp_flag_oncore(
volatile kmp_uint64 *p, kmp_uint64 c, kmp_uint32 idx,
enum barrier_type bar_t,
492 flag_switch(
false), bt(bar_t), this_thr(thr)
497 kmp_info_t * get_waiter(kmp_uint32 i) {
498 KMP_DEBUG_ASSERT(i<num_waiting_threads);
499 return waiting_threads[i];
501 kmp_uint32 get_num_waiters() {
return num_waiting_threads; }
502 void set_waiter(kmp_info_t *thr) {
503 waiting_threads[0] = thr;
504 num_waiting_threads = 1;
506 bool done_check_val(kmp_uint64 old_loc) {
return byteref(&old_loc,offset) == checker; }
507 bool done_check() {
return done_check_val(*
get()); }
508 bool notdone_check() {
510 if (this_thr->th.th_bar[bt].bb.wait_flag == KMP_BARRIER_SWITCH_TO_OWN_FLAG)
512 if (byteref(
get(),offset) != 1 && !flag_switch)
514 else if (flag_switch) {
515 this_thr->th.th_bar[bt].bb.wait_flag = KMP_BARRIER_SWITCHING;
516 kmp_flag_64 flag(&this_thr->th.th_bar[bt].bb.b_go, (kmp_uint64)KMP_BARRIER_STATE_BUMP);
517 __kmp_wait_64(this_thr, &flag, TRUE
525 void internal_release() {
526 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
527 byteref(
get(),offset) = 1;
531 byteref(&mask,offset) = 1;
532 (void) KMP_TEST_THEN_OR64((
volatile kmp_int64 *)
get(), mask);
535 kmp_uint64 set_sleeping() {
536 return KMP_TEST_THEN_OR64((kmp_int64
volatile *)
get(), KMP_BARRIER_SLEEP_STATE);
538 kmp_uint64 unset_sleeping() {
539 return KMP_TEST_THEN_AND64((kmp_int64
volatile *)
get(), ~KMP_BARRIER_SLEEP_STATE);
541 bool is_sleeping_val(kmp_uint64 old_loc) {
return old_loc & KMP_BARRIER_SLEEP_STATE; }
542 bool is_sleeping() {
return is_sleeping_val(*
get()); }
543 bool is_any_sleeping() {
return is_sleeping_val(*
get()); }
544 void wait(kmp_info_t *this_thr,
int final_spin) {
545 __kmp_wait_template<kmp_flag_oncore>(this_thr,
this, final_spin
546 USE_ITT_BUILD_ARG(itt_sync_obj));
548 void release() { __kmp_release_template(
this); }
549 void suspend(
int th_gtid) { __kmp_suspend_oncore(th_gtid,
this); }
550 void resume(
int th_gtid) { __kmp_resume_oncore(th_gtid,
this); }
551 int execute_tasks(kmp_info_t *this_thr, kmp_int32 gtid,
int final_spin,
int *thread_finished
552 USE_ITT_BUILD_ARG(
void * itt_sync_obj), kmp_int32 is_constrained) {
553 return __kmp_execute_tasks_oncore(this_thr, gtid,
this, final_spin, thread_finished
554 USE_ITT_BUILD_ARG(itt_sync_obj), is_constrained);
556 kmp_uint8 *get_stolen() {
return NULL; }
557 enum barrier_type get_bt() {
return bt; }
566 #endif // KMP_WAIT_RELEASE_H