17 #include "kmp_affinity.h" 22 #include "kmp_stats.h" 24 #include "kmp_wait_release.h" 25 #include "kmp_wrapper_getpid.h" 27 #if !KMP_OS_FREEBSD && !KMP_OS_NETBSD 31 #include <sys/resource.h> 32 #include <sys/syscall.h> 34 #include <sys/times.h> 37 #if KMP_OS_LINUX && !KMP_OS_CNK 38 #include <sys/sysinfo.h> 53 #include <mach/mach.h> 54 #include <sys/sysctl.h> 56 #include <pthread_np.h> 63 #include "tsan_annotations.h" 65 struct kmp_sys_timer {
66 struct timespec start;
70 #define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec) 72 static struct kmp_sys_timer __kmp_sys_timer_data;
74 #if KMP_HANDLE_SIGNALS 75 typedef void (*sig_func_t)(int);
76 STATIC_EFI2_WORKAROUND
struct sigaction __kmp_sighldrs[NSIG];
77 static sigset_t __kmp_sigset;
80 static int __kmp_init_runtime = FALSE;
82 static int __kmp_fork_count = 0;
84 static pthread_condattr_t __kmp_suspend_cond_attr;
85 static pthread_mutexattr_t __kmp_suspend_mutex_attr;
87 static kmp_cond_align_t __kmp_wait_cv;
88 static kmp_mutex_align_t __kmp_wait_mx;
90 kmp_uint64 __kmp_ticks_per_msec = 1000000;
93 static void __kmp_print_cond(
char *buffer, kmp_cond_align_t *cond) {
94 KMP_SNPRINTF(buffer, 128,
"(cond (lock (%ld, %d)), (descr (%p)))",
95 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
96 cond->c_cond.__c_waiting);
100 #if (KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED) 104 void __kmp_affinity_bind_thread(
int which) {
105 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
106 "Illegal set affinity operation when not capable");
108 kmp_affin_mask_t *mask;
109 KMP_CPU_ALLOC_ON_STACK(mask);
111 KMP_CPU_SET(which, mask);
112 __kmp_set_system_affinity(mask, TRUE);
113 KMP_CPU_FREE_FROM_STACK(mask);
119 void __kmp_affinity_determine_capable(
const char *env_var) {
122 #define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024) 127 buf = (
unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
132 gCode = syscall(__NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf);
133 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 134 "initial getaffinity call returned %d errno = %d\n",
140 if (__kmp_affinity_verbose ||
141 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
142 (__kmp_affinity_type != affinity_default) &&
143 (__kmp_affinity_type != affinity_disabled))) {
145 kmp_msg_t err_code = KMP_ERR(error);
146 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
147 err_code, __kmp_msg_null);
148 if (__kmp_generate_warnings == kmp_warnings_off) {
149 __kmp_str_free(&err_code.str);
152 KMP_AFFINITY_DISABLE();
153 KMP_INTERNAL_FREE(buf);
161 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL);
162 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 163 "setaffinity for mask size %d returned %d errno = %d\n",
164 gCode, sCode, errno));
166 if (errno == ENOSYS) {
167 if (__kmp_affinity_verbose ||
168 (__kmp_affinity_warnings &&
169 (__kmp_affinity_type != affinity_none) &&
170 (__kmp_affinity_type != affinity_default) &&
171 (__kmp_affinity_type != affinity_disabled))) {
173 kmp_msg_t err_code = KMP_ERR(error);
174 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var),
175 err_code, __kmp_msg_null);
176 if (__kmp_generate_warnings == kmp_warnings_off) {
177 __kmp_str_free(&err_code.str);
180 KMP_AFFINITY_DISABLE();
181 KMP_INTERNAL_FREE(buf);
183 if (errno == EFAULT) {
184 KMP_AFFINITY_ENABLE(gCode);
185 KA_TRACE(10, (
"__kmp_affinity_determine_capable: " 186 "affinity supported (mask size %d)\n",
187 (
int)__kmp_affin_mask_size));
188 KMP_INTERNAL_FREE(buf);
196 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 197 "searching for proper set size\n"));
199 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
200 gCode = syscall(__NR_sched_getaffinity, 0, size, buf);
201 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 202 "getaffinity for mask size %d returned %d errno = %d\n",
203 size, gCode, errno));
206 if (errno == ENOSYS) {
208 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 209 "inconsistent OS call behavior: errno == ENOSYS for mask " 212 if (__kmp_affinity_verbose ||
213 (__kmp_affinity_warnings &&
214 (__kmp_affinity_type != affinity_none) &&
215 (__kmp_affinity_type != affinity_default) &&
216 (__kmp_affinity_type != affinity_disabled))) {
218 kmp_msg_t err_code = KMP_ERR(error);
219 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
220 err_code, __kmp_msg_null);
221 if (__kmp_generate_warnings == kmp_warnings_off) {
222 __kmp_str_free(&err_code.str);
225 KMP_AFFINITY_DISABLE();
226 KMP_INTERNAL_FREE(buf);
232 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL);
233 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 234 "setaffinity for mask size %d returned %d errno = %d\n",
235 gCode, sCode, errno));
237 if (errno == ENOSYS) {
239 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 240 "inconsistent OS call behavior: errno == ENOSYS for mask " 243 if (__kmp_affinity_verbose ||
244 (__kmp_affinity_warnings &&
245 (__kmp_affinity_type != affinity_none) &&
246 (__kmp_affinity_type != affinity_default) &&
247 (__kmp_affinity_type != affinity_disabled))) {
249 kmp_msg_t err_code = KMP_ERR(error);
250 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var),
251 err_code, __kmp_msg_null);
252 if (__kmp_generate_warnings == kmp_warnings_off) {
253 __kmp_str_free(&err_code.str);
256 KMP_AFFINITY_DISABLE();
257 KMP_INTERNAL_FREE(buf);
260 if (errno == EFAULT) {
261 KMP_AFFINITY_ENABLE(gCode);
262 KA_TRACE(10, (
"__kmp_affinity_determine_capable: " 263 "affinity supported (mask size %d)\n",
264 (
int)__kmp_affin_mask_size));
265 KMP_INTERNAL_FREE(buf);
272 KMP_INTERNAL_FREE(buf);
277 KMP_AFFINITY_DISABLE();
278 KA_TRACE(10, (
"__kmp_affinity_determine_capable: " 279 "cannot determine mask size - affinity not supported\n"));
280 if (__kmp_affinity_verbose ||
281 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
282 (__kmp_affinity_type != affinity_default) &&
283 (__kmp_affinity_type != affinity_disabled))) {
284 KMP_WARNING(AffCantGetMaskSize, env_var);
288 #endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED 292 int __kmp_futex_determine_capable() {
294 int rc = syscall(__NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0);
295 int retval = (rc == 0) || (errno != ENOSYS);
298 (
"__kmp_futex_determine_capable: rc = %d errno = %d\n", rc, errno));
299 KA_TRACE(10, (
"__kmp_futex_determine_capable: futex syscall%s supported\n",
300 retval ?
"" :
" not"));
305 #endif // KMP_USE_FUTEX 307 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (!KMP_ASM_INTRINS) 311 kmp_int8 __kmp_test_then_or8(
volatile kmp_int8 *p, kmp_int8 d) {
312 kmp_int8 old_value, new_value;
314 old_value = TCR_1(*p);
315 new_value = old_value | d;
317 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
319 old_value = TCR_1(*p);
320 new_value = old_value | d;
325 kmp_int8 __kmp_test_then_and8(
volatile kmp_int8 *p, kmp_int8 d) {
326 kmp_int8 old_value, new_value;
328 old_value = TCR_1(*p);
329 new_value = old_value & d;
331 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
333 old_value = TCR_1(*p);
334 new_value = old_value & d;
339 kmp_uint32 __kmp_test_then_or32(
volatile kmp_uint32 *p, kmp_uint32 d) {
340 kmp_uint32 old_value, new_value;
342 old_value = TCR_4(*p);
343 new_value = old_value | d;
345 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
347 old_value = TCR_4(*p);
348 new_value = old_value | d;
353 kmp_uint32 __kmp_test_then_and32(
volatile kmp_uint32 *p, kmp_uint32 d) {
354 kmp_uint32 old_value, new_value;
356 old_value = TCR_4(*p);
357 new_value = old_value & d;
359 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
361 old_value = TCR_4(*p);
362 new_value = old_value & d;
368 kmp_int8 __kmp_test_then_add8(
volatile kmp_int8 *p, kmp_int8 d) {
369 kmp_int8 old_value, new_value;
371 old_value = TCR_1(*p);
372 new_value = old_value + d;
374 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
376 old_value = TCR_1(*p);
377 new_value = old_value + d;
382 kmp_int64 __kmp_test_then_add64(
volatile kmp_int64 *p, kmp_int64 d) {
383 kmp_int64 old_value, new_value;
385 old_value = TCR_8(*p);
386 new_value = old_value + d;
388 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
390 old_value = TCR_8(*p);
391 new_value = old_value + d;
397 kmp_uint64 __kmp_test_then_or64(
volatile kmp_uint64 *p, kmp_uint64 d) {
398 kmp_uint64 old_value, new_value;
400 old_value = TCR_8(*p);
401 new_value = old_value | d;
402 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
404 old_value = TCR_8(*p);
405 new_value = old_value | d;
410 kmp_uint64 __kmp_test_then_and64(
volatile kmp_uint64 *p, kmp_uint64 d) {
411 kmp_uint64 old_value, new_value;
413 old_value = TCR_8(*p);
414 new_value = old_value & d;
415 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
417 old_value = TCR_8(*p);
418 new_value = old_value & d;
425 void __kmp_terminate_thread(
int gtid) {
427 kmp_info_t *th = __kmp_threads[gtid];
432 #ifdef KMP_CANCEL_THREADS 433 KA_TRACE(10, (
"__kmp_terminate_thread: kill (%d)\n", gtid));
434 status = pthread_cancel(th->th.th_info.ds.ds_thread);
435 if (status != 0 && status != ESRCH) {
436 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantTerminateWorkerThread), KMP_ERR(status),
447 static kmp_int32 __kmp_set_stack_info(
int gtid, kmp_info_t *th) {
449 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD 459 if (!KMP_UBER_GTID(gtid)) {
462 status = pthread_attr_init(&attr);
463 KMP_CHECK_SYSFAIL(
"pthread_attr_init", status);
464 #if KMP_OS_FREEBSD || KMP_OS_NETBSD 465 status = pthread_attr_get_np(pthread_self(), &attr);
466 KMP_CHECK_SYSFAIL(
"pthread_attr_get_np", status);
468 status = pthread_getattr_np(pthread_self(), &attr);
469 KMP_CHECK_SYSFAIL(
"pthread_getattr_np", status);
471 status = pthread_attr_getstack(&attr, &addr, &size);
472 KMP_CHECK_SYSFAIL(
"pthread_attr_getstack", status);
474 (
"__kmp_set_stack_info: T#%d pthread_attr_getstack returned size:" 475 " %lu, low addr: %p\n",
477 status = pthread_attr_destroy(&attr);
478 KMP_CHECK_SYSFAIL(
"pthread_attr_destroy", status);
481 if (size != 0 && addr != 0) {
483 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((
char *)addr) + size));
484 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
485 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
490 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
491 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
492 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
496 static void *__kmp_launch_worker(
void *thr) {
497 int status, old_type, old_state;
498 #ifdef KMP_BLOCK_SIGNALS 499 sigset_t new_set, old_set;
502 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD 503 void *
volatile padding = 0;
507 gtid = ((kmp_info_t *)thr)->th.th_info.ds.ds_gtid;
508 __kmp_gtid_set_specific(gtid);
509 #ifdef KMP_TDATA_GTID 512 #if KMP_STATS_ENABLED 514 __kmp_stats_thread_ptr = ((kmp_info_t *)thr)->th.th_stats;
516 KMP_SET_THREAD_STATE(IDLE);
521 __kmp_itt_thread_name(gtid);
524 #if KMP_AFFINITY_SUPPORTED 525 __kmp_affinity_set_init_mask(gtid, FALSE);
528 #ifdef KMP_CANCEL_THREADS 529 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
530 KMP_CHECK_SYSFAIL(
"pthread_setcanceltype", status);
532 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
533 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status);
536 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 538 __kmp_clear_x87_fpu_status_word();
539 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
540 __kmp_load_mxcsr(&__kmp_init_mxcsr);
543 #ifdef KMP_BLOCK_SIGNALS 544 status = sigfillset(&new_set);
545 KMP_CHECK_SYSFAIL_ERRNO(
"sigfillset", status);
546 status = pthread_sigmask(SIG_BLOCK, &new_set, &old_set);
547 KMP_CHECK_SYSFAIL(
"pthread_sigmask", status);
550 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD 551 if (__kmp_stkoffset > 0 && gtid > 0) {
552 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
557 __kmp_set_stack_info(gtid, (kmp_info_t *)thr);
559 __kmp_check_stack_overlap((kmp_info_t *)thr);
561 exit_val = __kmp_launch_thread((kmp_info_t *)thr);
563 #ifdef KMP_BLOCK_SIGNALS 564 status = pthread_sigmask(SIG_SETMASK, &old_set, NULL);
565 KMP_CHECK_SYSFAIL(
"pthread_sigmask", status);
574 static void *__kmp_launch_monitor(
void *thr) {
575 int status, old_type, old_state;
576 #ifdef KMP_BLOCK_SIGNALS 579 struct timespec interval;
581 int yield_cycles = 0;
585 KA_TRACE(10, (
"__kmp_launch_monitor: #1 launched\n"));
588 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
589 #ifdef KMP_TDATA_GTID 590 __kmp_gtid = KMP_GTID_MONITOR;
597 __kmp_itt_thread_ignore();
600 __kmp_set_stack_info(((kmp_info_t *)thr)->th.th_info.ds.ds_gtid,
603 __kmp_check_stack_overlap((kmp_info_t *)thr);
605 #ifdef KMP_CANCEL_THREADS 606 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
607 KMP_CHECK_SYSFAIL(
"pthread_setcanceltype", status);
609 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
610 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status);
613 #if KMP_REAL_TIME_FIX 618 int sched = sched_getscheduler(0);
619 if (sched == SCHED_FIFO || sched == SCHED_RR) {
622 struct sched_param param;
623 int max_priority = sched_get_priority_max(sched);
625 KMP_WARNING(RealTimeSchedNotSupported);
626 sched_getparam(0, ¶m);
627 if (param.sched_priority < max_priority) {
628 param.sched_priority += 1;
629 rc = sched_setscheduler(0, sched, ¶m);
632 kmp_msg_t err_code = KMP_ERR(error);
633 __kmp_msg(kmp_ms_warning, KMP_MSG(CantChangeMonitorPriority),
634 err_code, KMP_MSG(MonitorWillStarve), __kmp_msg_null);
635 if (__kmp_generate_warnings == kmp_warnings_off) {
636 __kmp_str_free(&err_code.str);
643 __kmp_msg(kmp_ms_warning, KMP_MSG(RunningAtMaxPriority),
644 KMP_MSG(MonitorWillStarve), KMP_HNT(RunningAtMaxPriority),
649 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
651 #endif // KMP_REAL_TIME_FIX 655 if (__kmp_monitor_wakeups == 1) {
657 interval.tv_nsec = 0;
660 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
663 KA_TRACE(10, (
"__kmp_launch_monitor: #2 monitor\n"));
665 if (__kmp_yield_cycle) {
666 __kmp_yielding_on = 0;
667 yield_count = __kmp_yield_off_count;
669 __kmp_yielding_on = 1;
672 while (!TCR_4(__kmp_global.g.g_done)) {
678 KA_TRACE(15, (
"__kmp_launch_monitor: update\n"));
680 status = gettimeofday(&tval, NULL);
681 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status);
682 TIMEVAL_TO_TIMESPEC(&tval, &now);
684 now.tv_sec += interval.tv_sec;
685 now.tv_nsec += interval.tv_nsec;
687 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
689 now.tv_nsec -= KMP_NSEC_PER_SEC;
692 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
693 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status);
695 if (!TCR_4(__kmp_global.g.g_done)) {
696 status = pthread_cond_timedwait(&__kmp_wait_cv.c_cond,
697 &__kmp_wait_mx.m_mutex, &now);
699 if (status != ETIMEDOUT && status != EINTR) {
700 KMP_SYSFAIL(
"pthread_cond_timedwait", status);
704 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
705 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status);
707 if (__kmp_yield_cycle) {
709 if ((yield_cycles % yield_count) == 0) {
710 if (__kmp_yielding_on) {
711 __kmp_yielding_on = 0;
712 yield_count = __kmp_yield_off_count;
714 __kmp_yielding_on = 1;
715 yield_count = __kmp_yield_on_count;
720 __kmp_yielding_on = 1;
723 TCW_4(__kmp_global.g.g_time.dt.t_value,
724 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
729 KA_TRACE(10, (
"__kmp_launch_monitor: #3 cleanup\n"));
731 #ifdef KMP_BLOCK_SIGNALS 732 status = sigfillset(&new_set);
733 KMP_CHECK_SYSFAIL_ERRNO(
"sigfillset", status);
734 status = pthread_sigmask(SIG_UNBLOCK, &new_set, NULL);
735 KMP_CHECK_SYSFAIL(
"pthread_sigmask", status);
738 KA_TRACE(10, (
"__kmp_launch_monitor: #4 finished\n"));
740 if (__kmp_global.g.g_abort != 0) {
746 KA_TRACE(10, (
"__kmp_launch_monitor: #5 terminate sig=%d\n",
747 __kmp_global.g.g_abort));
752 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
753 __kmp_terminate_thread(gtid);
757 KA_TRACE(10, (
"__kmp_launch_monitor: #6 raise sig=%d\n",
758 __kmp_global.g.g_abort));
760 if (__kmp_global.g.g_abort > 0)
761 raise(__kmp_global.g.g_abort);
764 KA_TRACE(10, (
"__kmp_launch_monitor: #7 exit\n"));
768 #endif // KMP_USE_MONITOR 770 void __kmp_create_worker(
int gtid, kmp_info_t *th,
size_t stack_size) {
772 pthread_attr_t thread_attr;
775 th->th.th_info.ds.ds_gtid = gtid;
777 #if KMP_STATS_ENABLED 779 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
785 if (!KMP_UBER_GTID(gtid)) {
786 th->th.th_stats = __kmp_stats_list->push_back(gtid);
790 th->th.th_stats = __kmp_stats_thread_ptr;
792 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
794 #endif // KMP_STATS_ENABLED 796 if (KMP_UBER_GTID(gtid)) {
797 KA_TRACE(10, (
"__kmp_create_worker: uber thread (%d)\n", gtid));
798 th->th.th_info.ds.ds_thread = pthread_self();
799 __kmp_set_stack_info(gtid, th);
800 __kmp_check_stack_overlap(th);
804 KA_TRACE(10, (
"__kmp_create_worker: try to create thread (%d)\n", gtid));
808 #ifdef KMP_THREAD_ATTR 809 status = pthread_attr_init(&thread_attr);
811 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantInitThreadAttrs), KMP_ERR(status),
814 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
816 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetWorkerState), KMP_ERR(status),
827 stack_size += gtid * __kmp_stkoffset * 2;
829 KA_TRACE(10, (
"__kmp_create_worker: T#%d, default stacksize = %lu bytes, " 830 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
831 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
833 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 834 status = pthread_attr_setstacksize(&thread_attr, stack_size);
835 #ifdef KMP_BACKUP_STKSIZE 837 if (!__kmp_env_stksize) {
838 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
839 __kmp_stksize = KMP_BACKUP_STKSIZE;
840 KA_TRACE(10, (
"__kmp_create_worker: T#%d, default stacksize = %lu bytes, " 841 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu " 843 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
844 status = pthread_attr_setstacksize(&thread_attr, stack_size);
849 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetWorkerStackSize, stack_size),
850 KMP_ERR(status), KMP_HNT(ChangeWorkerStackSize), __kmp_msg_null);
857 pthread_create(&handle, &thread_attr, __kmp_launch_worker, (
void *)th);
858 if (status != 0 || !handle) {
859 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 860 if (status == EINVAL) {
861 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetWorkerStackSize, stack_size),
862 KMP_ERR(status), KMP_HNT(IncreaseWorkerStackSize),
865 if (status == ENOMEM) {
866 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetWorkerStackSize, stack_size),
867 KMP_ERR(status), KMP_HNT(DecreaseWorkerStackSize),
871 if (status == EAGAIN) {
872 __kmp_msg(kmp_ms_fatal, KMP_MSG(NoResourcesForWorkerThread),
873 KMP_ERR(status), KMP_HNT(Decrease_NUM_THREADS), __kmp_msg_null);
875 KMP_SYSFAIL(
"pthread_create", status);
878 th->th.th_info.ds.ds_thread = handle;
880 #ifdef KMP_THREAD_ATTR 881 status = pthread_attr_destroy(&thread_attr);
883 kmp_msg_t err_code = KMP_ERR(status);
884 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
886 if (__kmp_generate_warnings == kmp_warnings_off) {
887 __kmp_str_free(&err_code.str);
894 KA_TRACE(10, (
"__kmp_create_worker: done creating thread (%d)\n", gtid));
899 void __kmp_create_monitor(kmp_info_t *th) {
901 pthread_attr_t thread_attr;
904 int auto_adj_size = FALSE;
906 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
908 KA_TRACE(10, (
"__kmp_create_monitor: skipping monitor thread because of " 910 th->th.th_info.ds.ds_tid = 0;
911 th->th.th_info.ds.ds_gtid = 0;
914 KA_TRACE(10, (
"__kmp_create_monitor: try to create monitor\n"));
918 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
919 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
920 #if KMP_REAL_TIME_FIX 921 TCW_4(__kmp_global.g.g_time.dt.t_value,
924 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
925 #endif // KMP_REAL_TIME_FIX 927 #ifdef KMP_THREAD_ATTR 928 if (__kmp_monitor_stksize == 0) {
929 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
930 auto_adj_size = TRUE;
932 status = pthread_attr_init(&thread_attr);
934 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantInitThreadAttrs), KMP_ERR(status),
937 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
939 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetMonitorState), KMP_ERR(status),
943 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 944 status = pthread_attr_getstacksize(&thread_attr, &size);
945 KMP_CHECK_SYSFAIL(
"pthread_attr_getstacksize", status);
947 size = __kmp_sys_min_stksize;
951 if (__kmp_monitor_stksize == 0) {
952 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
954 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
955 __kmp_monitor_stksize = __kmp_sys_min_stksize;
958 KA_TRACE(10, (
"__kmp_create_monitor: default stacksize = %lu bytes," 959 "requested stacksize = %lu bytes\n",
960 size, __kmp_monitor_stksize));
965 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 966 KA_TRACE(10, (
"__kmp_create_monitor: setting stacksize = %lu bytes,",
967 __kmp_monitor_stksize));
968 status = pthread_attr_setstacksize(&thread_attr, __kmp_monitor_stksize);
971 __kmp_monitor_stksize *= 2;
974 kmp_msg_t err_code = KMP_ERR(status);
975 __kmp_msg(kmp_ms_warning,
976 KMP_MSG(CantSetMonitorStackSize, (
long int)__kmp_monitor_stksize),
977 err_code, KMP_HNT(ChangeMonitorStackSize), __kmp_msg_null);
978 if (__kmp_generate_warnings == kmp_warnings_off) {
979 __kmp_str_free(&err_code.str);
985 pthread_create(&handle, &thread_attr, __kmp_launch_monitor, (
void *)th);
988 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 989 if (status == EINVAL) {
990 if (auto_adj_size && (__kmp_monitor_stksize < (
size_t)0x40000000)) {
991 __kmp_monitor_stksize *= 2;
995 kmp_ms_fatal, KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
996 KMP_ERR(status), KMP_HNT(IncreaseMonitorStackSize), __kmp_msg_null);
998 if (status == ENOMEM) {
1000 kmp_ms_fatal, KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
1001 KMP_ERR(status), KMP_HNT(DecreaseMonitorStackSize), __kmp_msg_null);
1004 if (status == EAGAIN) {
1005 __kmp_msg(kmp_ms_fatal, KMP_MSG(NoResourcesForMonitorThread),
1006 KMP_ERR(status), KMP_HNT(DecreaseNumberOfThreadsInUse),
1009 KMP_SYSFAIL(
"pthread_create", status);
1012 th->th.th_info.ds.ds_thread = handle;
1014 #if KMP_REAL_TIME_FIX 1016 KMP_DEBUG_ASSERT(
sizeof(kmp_uint32) ==
1017 sizeof(__kmp_global.g.g_time.dt.t_value));
1018 __kmp_wait_yield_4((kmp_uint32
volatile *)&__kmp_global.g.g_time.dt.t_value,
1019 -1, &__kmp_neq_4, NULL);
1020 #endif // KMP_REAL_TIME_FIX 1022 #ifdef KMP_THREAD_ATTR 1023 status = pthread_attr_destroy(&thread_attr);
1025 kmp_msg_t err_code = KMP_ERR(status);
1026 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
1028 if (__kmp_generate_warnings == kmp_warnings_off) {
1029 __kmp_str_free(&err_code.str);
1036 KA_TRACE(10, (
"__kmp_create_monitor: monitor created %#.8lx\n",
1037 th->th.th_info.ds.ds_thread));
1040 #endif // KMP_USE_MONITOR 1042 void __kmp_exit_thread(
int exit_status) {
1043 pthread_exit((
void *)(intptr_t)exit_status);
1047 void __kmp_resume_monitor();
1049 void __kmp_reap_monitor(kmp_info_t *th) {
1053 KA_TRACE(10, (
"__kmp_reap_monitor: try to reap monitor thread with handle" 1055 th->th.th_info.ds.ds_thread));
1060 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1061 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1062 KA_TRACE(10, (
"__kmp_reap_monitor: monitor did not start, returning\n"));
1072 status = pthread_kill(th->th.th_info.ds.ds_thread, 0);
1073 if (status != ESRCH) {
1074 __kmp_resume_monitor();
1076 KA_TRACE(10, (
"__kmp_reap_monitor: try to join with monitor\n"));
1077 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
1078 if (exit_val != th) {
1079 __kmp_msg(kmp_ms_fatal, KMP_MSG(ReapMonitorError), KMP_ERR(status),
1083 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1084 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1086 KA_TRACE(10, (
"__kmp_reap_monitor: done reaping monitor thread with handle" 1088 th->th.th_info.ds.ds_thread));
1092 #endif // KMP_USE_MONITOR 1094 void __kmp_reap_worker(kmp_info_t *th) {
1101 10, (
"__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid));
1103 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
1107 __kmp_msg(kmp_ms_fatal, KMP_MSG(ReapWorkerError), KMP_ERR(status),
1110 if (exit_val != th) {
1111 KA_TRACE(10, (
"__kmp_reap_worker: worker T#%d did not reap properly, " 1113 th->th.th_info.ds.ds_gtid, exit_val));
1117 KA_TRACE(10, (
"__kmp_reap_worker: done reaping T#%d\n",
1118 th->th.th_info.ds.ds_gtid));
1123 #if KMP_HANDLE_SIGNALS 1125 static void __kmp_null_handler(
int signo) {
1129 static void __kmp_team_handler(
int signo) {
1130 if (__kmp_global.g.g_abort == 0) {
1133 __kmp_debug_printf(
"__kmp_team_handler: caught signal = %d\n", signo);
1148 if (__kmp_debug_buf) {
1149 __kmp_dump_debug_buffer();
1152 TCW_4(__kmp_global.g.g_abort, signo);
1154 TCW_4(__kmp_global.g.g_done, TRUE);
1159 __kmp_debug_printf(
"__kmp_team_handler: unknown signal type");
1166 static void __kmp_sigaction(
int signum,
const struct sigaction *act,
1167 struct sigaction *oldact) {
1168 int rc = sigaction(signum, act, oldact);
1169 KMP_CHECK_SYSFAIL_ERRNO(
"sigaction", rc);
1172 static void __kmp_install_one_handler(
int sig, sig_func_t handler_func,
1173 int parallel_init) {
1176 (
"__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init));
1177 if (parallel_init) {
1178 struct sigaction new_action;
1179 struct sigaction old_action;
1180 new_action.sa_handler = handler_func;
1181 new_action.sa_flags = 0;
1182 sigfillset(&new_action.sa_mask);
1183 __kmp_sigaction(sig, &new_action, &old_action);
1184 if (old_action.sa_handler == __kmp_sighldrs[sig].sa_handler) {
1185 sigaddset(&__kmp_sigset, sig);
1188 __kmp_sigaction(sig, &old_action, NULL);
1192 __kmp_sigaction(sig, NULL, &__kmp_sighldrs[sig]);
1197 static void __kmp_remove_one_handler(
int sig) {
1198 KB_TRACE(60, (
"__kmp_remove_one_handler( %d )\n", sig));
1199 if (sigismember(&__kmp_sigset, sig)) {
1200 struct sigaction old;
1202 __kmp_sigaction(sig, &__kmp_sighldrs[sig], &old);
1203 if ((old.sa_handler != __kmp_team_handler) &&
1204 (old.sa_handler != __kmp_null_handler)) {
1206 KB_TRACE(10, (
"__kmp_remove_one_handler: oops, not our handler, " 1207 "restoring: sig=%d\n",
1209 __kmp_sigaction(sig, &old, NULL);
1211 sigdelset(&__kmp_sigset, sig);
1216 void __kmp_install_signals(
int parallel_init) {
1217 KB_TRACE(10, (
"__kmp_install_signals( %d )\n", parallel_init));
1218 if (__kmp_handle_signals || !parallel_init) {
1221 sigemptyset(&__kmp_sigset);
1222 __kmp_install_one_handler(SIGHUP, __kmp_team_handler, parallel_init);
1223 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1224 __kmp_install_one_handler(SIGQUIT, __kmp_team_handler, parallel_init);
1225 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1226 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1227 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1228 __kmp_install_one_handler(SIGBUS, __kmp_team_handler, parallel_init);
1229 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1231 __kmp_install_one_handler(SIGSYS, __kmp_team_handler, parallel_init);
1233 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
1235 __kmp_install_one_handler(SIGPIPE, __kmp_team_handler, parallel_init);
1240 void __kmp_remove_signals(
void) {
1242 KB_TRACE(10, (
"__kmp_remove_signals()\n"));
1243 for (sig = 1; sig < NSIG; ++sig) {
1244 __kmp_remove_one_handler(sig);
1248 #endif // KMP_HANDLE_SIGNALS 1250 void __kmp_enable(
int new_state) {
1251 #ifdef KMP_CANCEL_THREADS 1252 int status, old_state;
1253 status = pthread_setcancelstate(new_state, &old_state);
1254 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status);
1255 KMP_DEBUG_ASSERT(old_state == PTHREAD_CANCEL_DISABLE);
1259 void __kmp_disable(
int *old_state) {
1260 #ifdef KMP_CANCEL_THREADS 1262 status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state);
1263 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status);
1267 static void __kmp_atfork_prepare(
void) {
1270 static void __kmp_atfork_parent(
void) {
1276 static void __kmp_atfork_child(
void) {
1283 #if KMP_AFFINITY_SUPPORTED 1287 kmp_set_thread_affinity_mask_initial();
1292 __kmp_affinity_type = affinity_none;
1294 if (__kmp_nested_proc_bind.bind_types != NULL) {
1295 __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
1297 #endif // OMP_40_ENABLED 1298 #endif // KMP_AFFINITY_SUPPORTED 1300 __kmp_init_runtime = FALSE;
1302 __kmp_init_monitor = 0;
1304 __kmp_init_parallel = FALSE;
1305 __kmp_init_middle = FALSE;
1306 __kmp_init_serial = FALSE;
1307 TCW_4(__kmp_init_gtid, FALSE);
1308 __kmp_init_common = FALSE;
1310 TCW_4(__kmp_init_user_locks, FALSE);
1311 #if !KMP_USE_DYNAMIC_LOCK 1312 __kmp_user_lock_table.used = 1;
1313 __kmp_user_lock_table.allocated = 0;
1314 __kmp_user_lock_table.table = NULL;
1315 __kmp_lock_blocks = NULL;
1319 TCW_4(__kmp_nth, 0);
1323 KA_TRACE(10, (
"__kmp_atfork_child: checking cache address list %p\n",
1324 __kmp_threadpriv_cache_list));
1326 while (__kmp_threadpriv_cache_list != NULL) {
1328 if (*__kmp_threadpriv_cache_list->addr != NULL) {
1329 KC_TRACE(50, (
"__kmp_atfork_child: zeroing cache at address %p\n",
1330 &(*__kmp_threadpriv_cache_list->addr)));
1332 *__kmp_threadpriv_cache_list->addr = NULL;
1334 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list->next;
1337 __kmp_init_runtime = FALSE;
1340 __kmp_init_bootstrap_lock(&__kmp_initz_lock);
1341 __kmp_init_bootstrap_lock(&__kmp_stdio_lock);
1342 __kmp_init_bootstrap_lock(&__kmp_console_lock);
1355 void __kmp_register_atfork(
void) {
1356 if (__kmp_need_register_atfork) {
1357 int status = pthread_atfork(__kmp_atfork_prepare, __kmp_atfork_parent,
1358 __kmp_atfork_child);
1359 KMP_CHECK_SYSFAIL(
"pthread_atfork", status);
1360 __kmp_need_register_atfork = FALSE;
1364 void __kmp_suspend_initialize(
void) {
1366 status = pthread_mutexattr_init(&__kmp_suspend_mutex_attr);
1367 KMP_CHECK_SYSFAIL(
"pthread_mutexattr_init", status);
1368 status = pthread_condattr_init(&__kmp_suspend_cond_attr);
1369 KMP_CHECK_SYSFAIL(
"pthread_condattr_init", status);
1372 static void __kmp_suspend_initialize_thread(kmp_info_t *th) {
1373 ANNOTATE_HAPPENS_AFTER(&th->th.th_suspend_init_count);
1374 if (th->th.th_suspend_init_count <= __kmp_fork_count) {
1378 status = pthread_cond_init(&th->th.th_suspend_cv.c_cond,
1379 &__kmp_suspend_cond_attr);
1380 KMP_CHECK_SYSFAIL(
"pthread_cond_init", status);
1381 status = pthread_mutex_init(&th->th.th_suspend_mx.m_mutex,
1382 &__kmp_suspend_mutex_attr);
1383 KMP_CHECK_SYSFAIL(
"pthread_mutex_init", status);
1384 *(
volatile int *)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1385 ANNOTATE_HAPPENS_BEFORE(&th->th.th_suspend_init_count);
1389 void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
1390 if (th->th.th_suspend_init_count > __kmp_fork_count) {
1395 status = pthread_cond_destroy(&th->th.th_suspend_cv.c_cond);
1396 if (status != 0 && status != EBUSY) {
1397 KMP_SYSFAIL(
"pthread_cond_destroy", status);
1399 status = pthread_mutex_destroy(&th->th.th_suspend_mx.m_mutex);
1400 if (status != 0 && status != EBUSY) {
1401 KMP_SYSFAIL(
"pthread_mutex_destroy", status);
1403 --th->th.th_suspend_init_count;
1404 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1412 static inline void __kmp_suspend_template(
int th_gtid, C *flag) {
1413 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_suspend);
1414 kmp_info_t *th = __kmp_threads[th_gtid];
1416 typename C::flag_t old_spin;
1418 KF_TRACE(30, (
"__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid,
1421 __kmp_suspend_initialize_thread(th);
1423 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1424 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status);
1426 KF_TRACE(10, (
"__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1427 th_gtid, flag->get()));
1431 old_spin = flag->set_sleeping();
1433 KF_TRACE(5, (
"__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x," 1435 th_gtid, flag->get(), *(flag->get()), old_spin));
1437 if (flag->done_check_val(old_spin)) {
1438 old_spin = flag->unset_sleeping();
1439 KF_TRACE(5, (
"__kmp_suspend_template: T#%d false alarm, reset sleep bit " 1441 th_gtid, flag->get()));
1446 int deactivated = FALSE;
1447 TCW_PTR(th->th.th_sleep_loc, (
void *)flag);
1449 while (flag->is_sleeping()) {
1450 #ifdef DEBUG_SUSPEND 1452 __kmp_suspend_count++;
1453 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1454 __kmp_printf(
"__kmp_suspend_template: suspending T#%d: %s\n", th_gtid,
1460 th->th.th_active = FALSE;
1461 if (th->th.th_active_in_pool) {
1462 th->th.th_active_in_pool = FALSE;
1463 KMP_TEST_THEN_DEC32(&__kmp_thread_pool_active_nth);
1464 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
1469 #if USE_SUSPEND_TIMEOUT 1470 struct timespec now;
1471 struct timeval tval;
1474 status = gettimeofday(&tval, NULL);
1475 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status);
1476 TIMEVAL_TO_TIMESPEC(&tval, &now);
1478 msecs = (4 * __kmp_dflt_blocktime) + 200;
1479 now.tv_sec += msecs / 1000;
1480 now.tv_nsec += (msecs % 1000) * 1000;
1482 KF_TRACE(15, (
"__kmp_suspend_template: T#%d about to perform " 1483 "pthread_cond_timedwait\n",
1485 status = pthread_cond_timedwait(&th->th.th_suspend_cv.c_cond,
1486 &th->th.th_suspend_mx.m_mutex, &now);
1488 KF_TRACE(15, (
"__kmp_suspend_template: T#%d about to perform" 1489 " pthread_cond_wait\n",
1491 status = pthread_cond_wait(&th->th.th_suspend_cv.c_cond,
1492 &th->th.th_suspend_mx.m_mutex);
1495 if ((status != 0) && (status != EINTR) && (status != ETIMEDOUT)) {
1496 KMP_SYSFAIL(
"pthread_cond_wait", status);
1499 if (status == ETIMEDOUT) {
1500 if (flag->is_sleeping()) {
1502 (
"__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid));
1504 KF_TRACE(2, (
"__kmp_suspend_template: T#%d timeout wakeup, sleep bit " 1508 }
else if (flag->is_sleeping()) {
1510 (
"__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
1517 th->th.th_active = TRUE;
1518 if (TCR_4(th->th.th_in_pool)) {
1519 KMP_TEST_THEN_INC32(&__kmp_thread_pool_active_nth);
1520 th->th.th_active_in_pool = TRUE;
1524 #ifdef DEBUG_SUSPEND 1527 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1528 __kmp_printf(
"__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid,
1533 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1534 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status);
1535 KF_TRACE(30, (
"__kmp_suspend_template: T#%d exit\n", th_gtid));
1538 void __kmp_suspend_32(
int th_gtid, kmp_flag_32 *flag) {
1539 __kmp_suspend_template(th_gtid, flag);
1541 void __kmp_suspend_64(
int th_gtid, kmp_flag_64 *flag) {
1542 __kmp_suspend_template(th_gtid, flag);
1544 void __kmp_suspend_oncore(
int th_gtid, kmp_flag_oncore *flag) {
1545 __kmp_suspend_template(th_gtid, flag);
1552 static inline void __kmp_resume_template(
int target_gtid, C *flag) {
1553 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1554 kmp_info_t *th = __kmp_threads[target_gtid];
1558 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1561 KF_TRACE(30, (
"__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
1562 gtid, target_gtid));
1563 KMP_DEBUG_ASSERT(gtid != target_gtid);
1565 __kmp_suspend_initialize_thread(th);
1567 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1568 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status);
1571 flag = (C *)CCAST(
void *, th->th.th_sleep_loc);
1576 if (!flag || flag->get_type() != flag->get_ptr_type()) {
1579 KF_TRACE(5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already " 1580 "awake: flag(%p)\n",
1581 gtid, target_gtid, NULL));
1582 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1583 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status);
1587 typename C::flag_t old_spin = flag->unset_sleeping();
1588 if (!flag->is_sleeping_val(old_spin)) {
1589 KF_TRACE(5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already " 1592 gtid, target_gtid, flag->get(), old_spin, *flag->get()));
1593 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1594 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status);
1597 KF_TRACE(5, (
"__kmp_resume_template: T#%d about to wakeup T#%d, reset " 1598 "sleep bit for flag's loc(%p): " 1600 gtid, target_gtid, flag->get(), old_spin, *flag->get()));
1602 TCW_PTR(th->th.th_sleep_loc, NULL);
1604 #ifdef DEBUG_SUSPEND 1607 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1608 __kmp_printf(
"__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid,
1609 target_gtid, buffer);
1612 status = pthread_cond_signal(&th->th.th_suspend_cv.c_cond);
1613 KMP_CHECK_SYSFAIL(
"pthread_cond_signal", status);
1614 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1615 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status);
1616 KF_TRACE(30, (
"__kmp_resume_template: T#%d exiting after signaling wake up" 1618 gtid, target_gtid));
1621 void __kmp_resume_32(
int target_gtid, kmp_flag_32 *flag) {
1622 __kmp_resume_template(target_gtid, flag);
1624 void __kmp_resume_64(
int target_gtid, kmp_flag_64 *flag) {
1625 __kmp_resume_template(target_gtid, flag);
1627 void __kmp_resume_oncore(
int target_gtid, kmp_flag_oncore *flag) {
1628 __kmp_resume_template(target_gtid, flag);
1632 void __kmp_resume_monitor() {
1633 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1636 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1637 KF_TRACE(30, (
"__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n", gtid,
1639 KMP_DEBUG_ASSERT(gtid != KMP_GTID_MONITOR);
1641 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
1642 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status);
1643 #ifdef DEBUG_SUSPEND 1646 __kmp_print_cond(buffer, &__kmp_wait_cv.c_cond);
1647 __kmp_printf(
"__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid,
1648 KMP_GTID_MONITOR, buffer);
1651 status = pthread_cond_signal(&__kmp_wait_cv.c_cond);
1652 KMP_CHECK_SYSFAIL(
"pthread_cond_signal", status);
1653 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
1654 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status);
1655 KF_TRACE(30, (
"__kmp_resume_monitor: T#%d exiting after signaling wake up" 1657 gtid, KMP_GTID_MONITOR));
1659 #endif // KMP_USE_MONITOR 1661 void __kmp_yield(
int cond) {
1665 if (!__kmp_yielding_on)
1668 if (__kmp_yield_cycle && !KMP_YIELD_NOW())
1674 void __kmp_gtid_set_specific(
int gtid) {
1675 if (__kmp_init_gtid) {
1677 status = pthread_setspecific(__kmp_gtid_threadprivate_key,
1678 (
void *)(intptr_t)(gtid + 1));
1679 KMP_CHECK_SYSFAIL(
"pthread_setspecific", status);
1681 KA_TRACE(50, (
"__kmp_gtid_set_specific: runtime shutdown, returning\n"));
1685 int __kmp_gtid_get_specific() {
1687 if (!__kmp_init_gtid) {
1688 KA_TRACE(50, (
"__kmp_gtid_get_specific: runtime shutdown, returning " 1689 "KMP_GTID_SHUTDOWN\n"));
1690 return KMP_GTID_SHUTDOWN;
1692 gtid = (int)(
size_t)pthread_getspecific(__kmp_gtid_threadprivate_key);
1694 gtid = KMP_GTID_DNE;
1698 KA_TRACE(50, (
"__kmp_gtid_get_specific: key:%d gtid:%d\n",
1699 __kmp_gtid_threadprivate_key, gtid));
1703 double __kmp_read_cpu_time(
void) {
1709 return (buffer.tms_utime + buffer.tms_cutime) / (double)CLOCKS_PER_SEC;
1712 int __kmp_read_system_info(
struct kmp_sys_info *info) {
1714 struct rusage r_usage;
1716 memset(info, 0,
sizeof(*info));
1718 status = getrusage(RUSAGE_SELF, &r_usage);
1719 KMP_CHECK_SYSFAIL_ERRNO(
"getrusage", status);
1722 info->maxrss = r_usage.ru_maxrss;
1724 info->minflt = r_usage.ru_minflt;
1726 info->majflt = r_usage.ru_majflt;
1728 info->nswap = r_usage.ru_nswap;
1730 info->inblock = r_usage.ru_inblock;
1732 info->oublock = r_usage.ru_oublock;
1734 info->nvcsw = r_usage.ru_nvcsw;
1736 info->nivcsw = r_usage.ru_nivcsw;
1738 return (status != 0);
1741 void __kmp_read_system_time(
double *delta) {
1743 struct timeval tval;
1744 struct timespec stop;
1747 status = gettimeofday(&tval, NULL);
1748 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status);
1749 TIMEVAL_TO_TIMESPEC(&tval, &stop);
1750 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
1751 *delta = (t_ns * 1e-9);
1754 void __kmp_clear_system_time(
void) {
1755 struct timeval tval;
1757 status = gettimeofday(&tval, NULL);
1758 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status);
1759 TIMEVAL_TO_TIMESPEC(&tval, &__kmp_sys_timer_data.start);
1764 void __kmp_tv_threadprivate_store(kmp_info_t *th,
void *global_addr,
1765 void *thread_addr) {
1768 p = (
struct tv_data *)__kmp_allocate(
sizeof(*p));
1770 p->u.tp.global_addr = global_addr;
1771 p->u.tp.thread_addr = thread_addr;
1773 p->type = (
void *)1;
1775 p->next = th->th.th_local.tv_data;
1776 th->th.th_local.tv_data = p;
1779 int rc = pthread_setspecific(__kmp_tv_key, p);
1780 KMP_CHECK_SYSFAIL(
"pthread_setspecific", rc);
1786 static int __kmp_get_xproc(
void) {
1790 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD 1792 r = sysconf(_SC_NPROCESSORS_ONLN);
1800 host_basic_info_data_t info;
1801 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
1802 rc = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&info, &num);
1803 if (rc == 0 && num == HOST_BASIC_INFO_COUNT) {
1806 r = info.avail_cpus;
1808 KMP_WARNING(CantGetNumAvailCPU);
1809 KMP_INFORM(AssumedNumCPU);
1814 #error "Unknown or unsupported OS." 1818 return r > 0 ? r : 2;
1822 int __kmp_read_from_file(
char const *path,
char const *format, ...) {
1826 va_start(args, format);
1827 FILE *f = fopen(path,
"rb");
1830 result = vfscanf(f, format, args);
1836 void __kmp_runtime_initialize(
void) {
1838 pthread_mutexattr_t mutex_attr;
1839 pthread_condattr_t cond_attr;
1841 if (__kmp_init_runtime) {
1845 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) 1846 if (!__kmp_cpuinfo.initialized) {
1847 __kmp_query_cpuid(&__kmp_cpuinfo);
1851 __kmp_xproc = __kmp_get_xproc();
1853 if (sysconf(_SC_THREADS)) {
1856 __kmp_sys_max_nth = sysconf(_SC_THREAD_THREADS_MAX);
1857 if (__kmp_sys_max_nth == -1) {
1859 __kmp_sys_max_nth = INT_MAX;
1860 }
else if (__kmp_sys_max_nth <= 1) {
1862 __kmp_sys_max_nth = KMP_MAX_NTH;
1866 __kmp_sys_min_stksize = sysconf(_SC_THREAD_STACK_MIN);
1867 if (__kmp_sys_min_stksize <= 1) {
1868 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
1873 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
1877 int rc = pthread_key_create(&__kmp_tv_key, 0);
1878 KMP_CHECK_SYSFAIL(
"pthread_key_create", rc);
1882 status = pthread_key_create(&__kmp_gtid_threadprivate_key,
1883 __kmp_internal_end_dest);
1884 KMP_CHECK_SYSFAIL(
"pthread_key_create", status);
1885 status = pthread_mutexattr_init(&mutex_attr);
1886 KMP_CHECK_SYSFAIL(
"pthread_mutexattr_init", status);
1887 status = pthread_mutex_init(&__kmp_wait_mx.m_mutex, &mutex_attr);
1888 KMP_CHECK_SYSFAIL(
"pthread_mutex_init", status);
1889 status = pthread_condattr_init(&cond_attr);
1890 KMP_CHECK_SYSFAIL(
"pthread_condattr_init", status);
1891 status = pthread_cond_init(&__kmp_wait_cv.c_cond, &cond_attr);
1892 KMP_CHECK_SYSFAIL(
"pthread_cond_init", status);
1894 __kmp_itt_initialize();
1897 __kmp_init_runtime = TRUE;
1900 void __kmp_runtime_destroy(
void) {
1903 if (!__kmp_init_runtime) {
1908 __kmp_itt_destroy();
1911 status = pthread_key_delete(__kmp_gtid_threadprivate_key);
1912 KMP_CHECK_SYSFAIL(
"pthread_key_delete", status);
1914 status = pthread_key_delete(__kmp_tv_key);
1915 KMP_CHECK_SYSFAIL(
"pthread_key_delete", status);
1918 status = pthread_mutex_destroy(&__kmp_wait_mx.m_mutex);
1919 if (status != 0 && status != EBUSY) {
1920 KMP_SYSFAIL(
"pthread_mutex_destroy", status);
1922 status = pthread_cond_destroy(&__kmp_wait_cv.c_cond);
1923 if (status != 0 && status != EBUSY) {
1924 KMP_SYSFAIL(
"pthread_cond_destroy", status);
1926 #if KMP_AFFINITY_SUPPORTED 1927 __kmp_affinity_uninitialize();
1930 __kmp_init_runtime = FALSE;
1935 void __kmp_thread_sleep(
int millis) { sleep((millis + 500) / 1000); }
1938 void __kmp_elapsed(
double *t) {
1940 #ifdef FIX_SGI_CLOCK 1943 status = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
1944 KMP_CHECK_SYSFAIL_ERRNO(
"clock_gettime", status);
1946 (double)ts.tv_nsec * (1.0 / (
double)KMP_NSEC_PER_SEC) + (
double)ts.tv_sec;
1950 status = gettimeofday(&tv, NULL);
1951 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status);
1953 (double)tv.tv_usec * (1.0 / (
double)KMP_USEC_PER_SEC) + (
double)tv.tv_sec;
1958 void __kmp_elapsed_tick(
double *t) { *t = 1 / (double)CLOCKS_PER_SEC; }
1961 kmp_uint64 __kmp_now_nsec() {
1963 gettimeofday(&t, NULL);
1964 return KMP_NSEC_PER_SEC * t.tv_sec + 1000 * t.tv_usec;
1967 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 1969 void __kmp_initialize_system_tick() {
1970 kmp_uint64 delay = 100000;
1971 kmp_uint64 nsec = __kmp_now_nsec();
1972 kmp_uint64 goal = __kmp_hardware_timestamp() + delay;
1974 while ((now = __kmp_hardware_timestamp()) < goal)
1976 __kmp_ticks_per_msec =
1977 (kmp_uint64)(1e6 * (delay + (now - goal)) / (__kmp_now_nsec() - nsec));
1984 int __kmp_is_address_mapped(
void *addr) {
1989 #if KMP_OS_LINUX || KMP_OS_FREEBSD 1994 char *name = __kmp_str_format(
"/proc/%d/maps", getpid());
1997 file = fopen(name,
"r");
1998 KMP_ASSERT(file != NULL);
2002 void *beginning = NULL;
2003 void *ending = NULL;
2006 rc = fscanf(file,
"%p-%p %4s %*[^\n]\n", &beginning, &ending, perms);
2010 KMP_ASSERT(rc == 3 &&
2011 KMP_STRLEN(perms) == 4);
2014 if ((addr >= beginning) && (addr < ending)) {
2016 if (strcmp(perms,
"rw") == 0) {
2027 KMP_INTERNAL_FREE(name);
2036 rc = vm_read_overwrite(
2038 (vm_address_t)(addr),
2040 (vm_address_t)(&buffer),
2048 #elif KMP_OS_FREEBSD || KMP_OS_NETBSD 2055 #error "Unknown or unsupported OS" 2063 #ifdef USE_LOAD_BALANCE 2072 int __kmp_get_load_balance(
int max) {
2076 int res = getloadavg(averages, 3);
2081 if (__kmp_load_balance_interval < 180 && (res >= 1)) {
2082 ret_avg = averages[0];
2083 }
else if ((__kmp_load_balance_interval >= 180 &&
2084 __kmp_load_balance_interval < 600) &&
2086 ret_avg = averages[1];
2087 }
else if ((__kmp_load_balance_interval >= 600) && (res == 3)) {
2088 ret_avg = averages[2];
2102 int __kmp_get_load_balance(
int max) {
2103 static int permanent_error = 0;
2104 static int glb_running_threads = 0;
2106 static double glb_call_time = 0;
2108 int running_threads = 0;
2110 DIR *proc_dir = NULL;
2111 struct dirent *proc_entry = NULL;
2113 kmp_str_buf_t task_path;
2114 DIR *task_dir = NULL;
2115 struct dirent *task_entry = NULL;
2116 int task_path_fixed_len;
2118 kmp_str_buf_t stat_path;
2120 int stat_path_fixed_len;
2122 int total_processes = 0;
2123 int total_threads = 0;
2125 double call_time = 0.0;
2127 __kmp_str_buf_init(&task_path);
2128 __kmp_str_buf_init(&stat_path);
2130 __kmp_elapsed(&call_time);
2132 if (glb_call_time &&
2133 (call_time - glb_call_time < __kmp_load_balance_interval)) {
2134 running_threads = glb_running_threads;
2138 glb_call_time = call_time;
2141 if (permanent_error) {
2142 running_threads = -1;
2151 proc_dir = opendir(
"/proc");
2152 if (proc_dir == NULL) {
2155 running_threads = -1;
2156 permanent_error = 1;
2161 __kmp_str_buf_cat(&task_path,
"/proc/", 6);
2162 task_path_fixed_len = task_path.used;
2164 proc_entry = readdir(proc_dir);
2165 while (proc_entry != NULL) {
2168 if (proc_entry->d_type == DT_DIR && isdigit(proc_entry->d_name[0])) {
2176 KMP_DEBUG_ASSERT(total_processes != 1 ||
2177 strcmp(proc_entry->d_name,
"1") == 0);
2180 task_path.used = task_path_fixed_len;
2181 __kmp_str_buf_cat(&task_path, proc_entry->d_name,
2182 KMP_STRLEN(proc_entry->d_name));
2183 __kmp_str_buf_cat(&task_path,
"/task", 5);
2185 task_dir = opendir(task_path.str);
2186 if (task_dir == NULL) {
2195 if (strcmp(proc_entry->d_name,
"1") == 0) {
2196 running_threads = -1;
2197 permanent_error = 1;
2202 __kmp_str_buf_clear(&stat_path);
2203 __kmp_str_buf_cat(&stat_path, task_path.str, task_path.used);
2204 __kmp_str_buf_cat(&stat_path,
"/", 1);
2205 stat_path_fixed_len = stat_path.used;
2207 task_entry = readdir(task_dir);
2208 while (task_entry != NULL) {
2210 if (proc_entry->d_type == DT_DIR && isdigit(task_entry->d_name[0])) {
2218 stat_path_fixed_len;
2219 __kmp_str_buf_cat(&stat_path, task_entry->d_name,
2220 KMP_STRLEN(task_entry->d_name));
2221 __kmp_str_buf_cat(&stat_path,
"/stat", 5);
2225 stat_file = open(stat_path.str, O_RDONLY);
2226 if (stat_file == -1) {
2256 len = read(stat_file, buffer,
sizeof(buffer) - 1);
2263 char *close_parent = strstr(buffer,
") ");
2264 if (close_parent != NULL) {
2265 char state = *(close_parent + 2);
2268 if (running_threads >= max) {
2278 task_entry = readdir(task_dir);
2284 proc_entry = readdir(proc_dir);
2290 KMP_DEBUG_ASSERT(running_threads > 0);
2291 if (running_threads <= 0) {
2292 running_threads = 1;
2296 if (proc_dir != NULL) {
2299 __kmp_str_buf_free(&task_path);
2300 if (task_dir != NULL) {
2303 __kmp_str_buf_free(&stat_path);
2304 if (stat_file != -1) {
2308 glb_running_threads = running_threads;
2310 return running_threads;
2314 #endif // KMP_OS_DARWIN 2316 #endif // USE_LOAD_BALANCE 2318 #if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \ 2319 ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || KMP_ARCH_PPC64) 2323 int __kmp_invoke_microtask(microtask_t pkfn,
int gtid,
int tid,
int argc,
2327 void **exit_frame_ptr
2331 *exit_frame_ptr = __builtin_frame_address(0);
2336 fprintf(stderr,
"Too many args to microtask: %d!\n", argc);
2340 (*pkfn)(>id, &tid);
2343 (*pkfn)(>id, &tid, p_argv[0]);
2346 (*pkfn)(>id, &tid, p_argv[0], p_argv[1]);
2349 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2]);
2352 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2355 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2358 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2362 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2363 p_argv[5], p_argv[6]);
2366 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2367 p_argv[5], p_argv[6], p_argv[7]);
2370 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2371 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2374 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2375 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2378 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2379 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2382 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2383 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2387 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2388 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2389 p_argv[11], p_argv[12]);
2392 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2393 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2394 p_argv[11], p_argv[12], p_argv[13]);
2397 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2398 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2399 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2404 *exit_frame_ptr = 0;
#define KMP_START_EXPLICIT_TIMER(name)
"Starts" an explicit timer which will need a corresponding KMP_STOP_EXPLICIT_TIMER() macro...
#define KMP_INIT_PARTITIONED_TIMERS(name)
Initializes the paritioned timers to begin with name.