17 #include "kmp_wrapper_getpid.h" 22 #include "kmp_stats.h" 23 #include "kmp_wait_release.h" 25 #if !KMP_OS_FREEBSD && !KMP_OS_NETBSD 31 #include <sys/times.h> 32 #include <sys/resource.h> 33 #include <sys/syscall.h> 35 #if KMP_OS_LINUX && !KMP_OS_CNK 36 # include <sys/sysinfo.h> 37 # if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) 51 # include <sys/sysctl.h> 52 # include <mach/mach.h> 54 # include <pthread_np.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;
95 __kmp_print_cond(
char *buffer, kmp_cond_align_t *cond )
97 KMP_SNPRINTF( buffer, 128,
"(cond (lock (%ld, %d)), (descr (%p)))",
98 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
99 cond->c_cond.__c_waiting );
106 #if ( KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED) 119 # if KMP_ARCH_X86 || KMP_ARCH_ARM 120 # ifndef __NR_sched_setaffinity 121 # define __NR_sched_setaffinity 241 122 # elif __NR_sched_setaffinity != 241 123 # error Wrong code for setaffinity system call. 125 # ifndef __NR_sched_getaffinity 126 # define __NR_sched_getaffinity 242 127 # elif __NR_sched_getaffinity != 242 128 # error Wrong code for getaffinity system call. 131 # elif KMP_ARCH_AARCH64 132 # ifndef __NR_sched_setaffinity 133 # define __NR_sched_setaffinity 122 134 # elif __NR_sched_setaffinity != 122 135 # error Wrong code for setaffinity system call. 137 # ifndef __NR_sched_getaffinity 138 # define __NR_sched_getaffinity 123 139 # elif __NR_sched_getaffinity != 123 140 # error Wrong code for getaffinity system call. 143 # elif KMP_ARCH_X86_64 144 # ifndef __NR_sched_setaffinity 145 # define __NR_sched_setaffinity 203 146 # elif __NR_sched_setaffinity != 203 147 # error Wrong code for setaffinity system call. 149 # ifndef __NR_sched_getaffinity 150 # define __NR_sched_getaffinity 204 151 # elif __NR_sched_getaffinity != 204 152 # error Wrong code for getaffinity system call. 155 # elif KMP_ARCH_PPC64 156 # ifndef __NR_sched_setaffinity 157 # define __NR_sched_setaffinity 222 158 # elif __NR_sched_setaffinity != 222 159 # error Wrong code for setaffinity system call. 161 # ifndef __NR_sched_getaffinity 162 # define __NR_sched_getaffinity 223 163 # elif __NR_sched_getaffinity != 223 164 # error Wrong code for getaffinity system call. 168 # ifndef __NR_sched_setaffinity 169 # define __NR_sched_setaffinity 4239 170 # elif __NR_sched_setaffinity != 4239 171 # error Wrong code for setaffinity system call. 173 # ifndef __NR_sched_getaffinity 174 # define __NR_sched_getaffinity 4240 175 # elif __NR_sched_getaffinity != 4240 176 # error Wrong code for getaffinity system call. 179 # elif KMP_ARCH_MIPS64 180 # ifndef __NR_sched_setaffinity 181 # define __NR_sched_setaffinity 5195 182 # elif __NR_sched_setaffinity != 5195 183 # error Wrong code for setaffinity system call. 185 # ifndef __NR_sched_getaffinity 186 # define __NR_sched_getaffinity 5196 187 # elif __NR_sched_getaffinity != 5196 188 # error Wrong code for getaffinity system call. 192 # error Unknown or unsupported architecture 197 __kmp_set_system_affinity( kmp_affin_mask_t
const *mask,
int abort_on_error )
199 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
200 "Illegal set affinity operation when not capable");
202 int retval = hwloc_set_cpubind(__kmp_hwloc_topology, (hwloc_cpuset_t)mask, HWLOC_CPUBIND_THREAD);
204 int retval = syscall( __NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask );
210 if (abort_on_error) {
213 KMP_MSG( FatalSysError ),
222 __kmp_get_system_affinity( kmp_affin_mask_t *mask,
int abort_on_error )
224 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
225 "Illegal get affinity operation when not capable");
228 int retval = hwloc_get_cpubind(__kmp_hwloc_topology, (hwloc_cpuset_t)mask, HWLOC_CPUBIND_THREAD);
230 int retval = syscall( __NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask );
236 if (abort_on_error) {
239 KMP_MSG( FatalSysError ),
248 __kmp_affinity_bind_thread(
int which )
250 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
251 "Illegal set affinity operation when not capable");
253 kmp_affin_mask_t *mask;
254 KMP_CPU_ALLOC_ON_STACK(mask);
256 KMP_CPU_SET(which, mask);
257 __kmp_set_system_affinity(mask, TRUE);
258 KMP_CPU_FREE_FROM_STACK(mask);
267 __kmp_affinity_determine_capable(
const char *env_var)
273 # define KMP_CPU_SET_SIZE_LIMIT (1024*1024) 277 kmp_affin_mask_t *buf;
278 buf = ( kmp_affin_mask_t * ) KMP_INTERNAL_MALLOC( KMP_CPU_SET_SIZE_LIMIT );
283 gCode = syscall( __NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf );
284 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 285 "initial getaffinity call returned %d errno = %d\n",
293 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
294 && (__kmp_affinity_type != affinity_none)
295 && (__kmp_affinity_type != affinity_default)
296 && (__kmp_affinity_type != affinity_disabled))) {
300 KMP_MSG( GetAffSysCallNotSupported, env_var ),
305 KMP_AFFINITY_DISABLE();
306 KMP_INTERNAL_FREE(buf);
315 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
316 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 317 "setaffinity for mask size %d returned %d errno = %d\n",
318 gCode, sCode, errno));
320 if (errno == ENOSYS) {
321 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
322 && (__kmp_affinity_type != affinity_none)
323 && (__kmp_affinity_type != affinity_default)
324 && (__kmp_affinity_type != affinity_disabled))) {
328 KMP_MSG( SetAffSysCallNotSupported, env_var ),
333 KMP_AFFINITY_DISABLE();
334 KMP_INTERNAL_FREE(buf);
336 if (errno == EFAULT) {
337 KMP_AFFINITY_ENABLE(gCode);
338 KA_TRACE(10, (
"__kmp_affinity_determine_capable: " 339 "affinity supported (mask size %d)\n",
340 (
int)__kmp_affin_mask_size));
341 KMP_INTERNAL_FREE(buf);
351 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 352 "searching for proper set size\n"));
354 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
355 gCode = syscall( __NR_sched_getaffinity, 0, size, buf );
356 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 357 "getaffinity for mask size %d returned %d errno = %d\n", size,
361 if ( errno == ENOSYS )
366 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 367 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
369 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
370 && (__kmp_affinity_type != affinity_none)
371 && (__kmp_affinity_type != affinity_default)
372 && (__kmp_affinity_type != affinity_disabled))) {
376 KMP_MSG( GetAffSysCallNotSupported, env_var ),
381 KMP_AFFINITY_DISABLE();
382 KMP_INTERNAL_FREE(buf);
388 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
389 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 390 "setaffinity for mask size %d returned %d errno = %d\n",
391 gCode, sCode, errno));
393 if (errno == ENOSYS) {
397 KA_TRACE(30, (
"__kmp_affinity_determine_capable: " 398 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
400 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
401 && (__kmp_affinity_type != affinity_none)
402 && (__kmp_affinity_type != affinity_default)
403 && (__kmp_affinity_type != affinity_disabled))) {
407 KMP_MSG( SetAffSysCallNotSupported, env_var ),
412 KMP_AFFINITY_DISABLE();
413 KMP_INTERNAL_FREE(buf);
416 if (errno == EFAULT) {
417 KMP_AFFINITY_ENABLE(gCode);
418 KA_TRACE(10, (
"__kmp_affinity_determine_capable: " 419 "affinity supported (mask size %d)\n",
420 (
int)__kmp_affin_mask_size));
421 KMP_INTERNAL_FREE(buf);
427 KMP_INTERNAL_FREE(buf);
433 KMP_AFFINITY_DISABLE();
434 KA_TRACE(10, (
"__kmp_affinity_determine_capable: " 435 "cannot determine mask size - affinity not supported\n"));
436 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
437 && (__kmp_affinity_type != affinity_none)
438 && (__kmp_affinity_type != affinity_default)
439 && (__kmp_affinity_type != affinity_disabled))) {
440 KMP_WARNING( AffCantGetMaskSize, env_var );
444 #endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED 449 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && !KMP_OS_CNK 452 __kmp_futex_determine_capable()
455 int rc = syscall( __NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0 );
456 int retval = ( rc == 0 ) || ( errno != ENOSYS );
458 KA_TRACE(10, (
"__kmp_futex_determine_capable: rc = %d errno = %d\n", rc,
460 KA_TRACE(10, (
"__kmp_futex_determine_capable: futex syscall%s supported\n",
461 retval ?
"" :
" not" ) );
466 #endif // KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM) && !KMP_OS_CNK 471 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) 478 __kmp_test_then_or8(
volatile kmp_int8 *p, kmp_int8 d )
480 kmp_int8 old_value, new_value;
482 old_value = TCR_1( *p );
483 new_value = old_value | d;
485 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
488 old_value = TCR_1( *p );
489 new_value = old_value | d;
495 __kmp_test_then_and8(
volatile kmp_int8 *p, kmp_int8 d )
497 kmp_int8 old_value, new_value;
499 old_value = TCR_1( *p );
500 new_value = old_value & d;
502 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
505 old_value = TCR_1( *p );
506 new_value = old_value & d;
512 __kmp_test_then_or32(
volatile kmp_int32 *p, kmp_int32 d )
514 kmp_int32 old_value, new_value;
516 old_value = TCR_4( *p );
517 new_value = old_value | d;
519 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
522 old_value = TCR_4( *p );
523 new_value = old_value | d;
529 __kmp_test_then_and32(
volatile kmp_int32 *p, kmp_int32 d )
531 kmp_int32 old_value, new_value;
533 old_value = TCR_4( *p );
534 new_value = old_value & d;
536 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
539 old_value = TCR_4( *p );
540 new_value = old_value & d;
545 # if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64 547 __kmp_test_then_add8(
volatile kmp_int8 *p, kmp_int8 d )
549 kmp_int8 old_value, new_value;
551 old_value = TCR_1( *p );
552 new_value = old_value + d;
554 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
557 old_value = TCR_1( *p );
558 new_value = old_value + d;
564 __kmp_test_then_add64(
volatile kmp_int64 *p, kmp_int64 d )
566 kmp_int64 old_value, new_value;
568 old_value = TCR_8( *p );
569 new_value = old_value + d;
571 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
574 old_value = TCR_8( *p );
575 new_value = old_value + d;
582 __kmp_test_then_or64(
volatile kmp_int64 *p, kmp_int64 d )
584 kmp_int64 old_value, new_value;
586 old_value = TCR_8( *p );
587 new_value = old_value | d;
588 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
591 old_value = TCR_8( *p );
592 new_value = old_value | d;
598 __kmp_test_then_and64(
volatile kmp_int64 *p, kmp_int64 d )
600 kmp_int64 old_value, new_value;
602 old_value = TCR_8( *p );
603 new_value = old_value & d;
604 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
607 old_value = TCR_8( *p );
608 new_value = old_value & d;
616 __kmp_terminate_thread(
int gtid )
619 kmp_info_t *th = __kmp_threads[ gtid ];
623 #ifdef KMP_CANCEL_THREADS 624 KA_TRACE( 10, (
"__kmp_terminate_thread: kill (%d)\n", gtid ) );
625 status = pthread_cancel( th->th.th_info.ds.ds_thread );
626 if ( status != 0 && status != ESRCH ) {
629 KMP_MSG( CantTerminateWorkerThread ),
653 __kmp_set_stack_info(
int gtid, kmp_info_t *th )
656 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD 666 if ( ! KMP_UBER_GTID(gtid) ) {
669 status = pthread_attr_init( &attr );
670 KMP_CHECK_SYSFAIL(
"pthread_attr_init", status );
671 #if KMP_OS_FREEBSD || KMP_OS_NETBSD 672 status = pthread_attr_get_np( pthread_self(), &attr );
673 KMP_CHECK_SYSFAIL(
"pthread_attr_get_np", status );
675 status = pthread_getattr_np( pthread_self(), &attr );
676 KMP_CHECK_SYSFAIL(
"pthread_getattr_np", status );
678 status = pthread_attr_getstack( &attr, &addr, &size );
679 KMP_CHECK_SYSFAIL(
"pthread_attr_getstack", status );
680 KA_TRACE( 60, (
"__kmp_set_stack_info: T#%d pthread_attr_getstack returned size: %lu, " 684 status = pthread_attr_destroy( &attr );
685 KMP_CHECK_SYSFAIL(
"pthread_attr_destroy", status );
688 if ( size != 0 && addr != 0 ) {
690 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((
char *)addr) + size));
691 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
692 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
697 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
698 TCW_PTR(th -> th.th_info.ds.ds_stackbase, &stack_data);
699 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
704 __kmp_launch_worker(
void *thr )
706 int status, old_type, old_state;
707 #ifdef KMP_BLOCK_SIGNALS 708 sigset_t new_set, old_set;
711 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD 712 void *
volatile padding = 0;
716 gtid = ((kmp_info_t*)thr) -> th.th_info.ds.ds_gtid;
717 __kmp_gtid_set_specific( gtid );
718 #ifdef KMP_TDATA_GTID 721 #if KMP_STATS_ENABLED 723 __kmp_stats_thread_ptr = ((kmp_info_t*)thr)->th.th_stats;
727 __kmp_itt_thread_name( gtid );
730 #if KMP_AFFINITY_SUPPORTED 731 __kmp_affinity_set_init_mask( gtid, FALSE );
734 #ifdef KMP_CANCEL_THREADS 735 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
736 KMP_CHECK_SYSFAIL(
"pthread_setcanceltype", status );
738 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
739 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status );
742 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 747 __kmp_clear_x87_fpu_status_word();
748 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
749 __kmp_load_mxcsr( &__kmp_init_mxcsr );
752 #ifdef KMP_BLOCK_SIGNALS 753 status = sigfillset( & new_set );
754 KMP_CHECK_SYSFAIL_ERRNO(
"sigfillset", status );
755 status = pthread_sigmask( SIG_BLOCK, & new_set, & old_set );
756 KMP_CHECK_SYSFAIL(
"pthread_sigmask", status );
759 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD 760 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
761 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
766 __kmp_set_stack_info( gtid, (kmp_info_t*)thr );
768 __kmp_check_stack_overlap( (kmp_info_t*)thr );
770 exit_val = __kmp_launch_thread( (kmp_info_t *) thr );
772 #ifdef KMP_BLOCK_SIGNALS 773 status = pthread_sigmask( SIG_SETMASK, & old_set, NULL );
774 KMP_CHECK_SYSFAIL(
"pthread_sigmask", status );
784 __kmp_launch_monitor(
void *thr )
786 int status, old_type, old_state;
787 #ifdef KMP_BLOCK_SIGNALS 790 struct timespec interval;
792 int yield_cycles = 0;
796 KA_TRACE( 10, (
"__kmp_launch_monitor: #1 launched\n" ) );
799 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
800 #ifdef KMP_TDATA_GTID 801 __kmp_gtid = KMP_GTID_MONITOR;
807 __kmp_itt_thread_ignore();
810 __kmp_set_stack_info( ((kmp_info_t*)thr)->th.th_info.ds.ds_gtid, (kmp_info_t*)thr );
812 __kmp_check_stack_overlap( (kmp_info_t*)thr );
814 #ifdef KMP_CANCEL_THREADS 815 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
816 KMP_CHECK_SYSFAIL(
"pthread_setcanceltype", status );
818 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
819 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status );
822 #if KMP_REAL_TIME_FIX 826 int sched = sched_getscheduler( 0 );
827 if ( sched == SCHED_FIFO || sched == SCHED_RR ) {
830 struct sched_param param;
831 int max_priority = sched_get_priority_max( sched );
833 KMP_WARNING( RealTimeSchedNotSupported );
834 sched_getparam( 0, & param );
835 if ( param.sched_priority < max_priority ) {
836 param.sched_priority += 1;
837 rc = sched_setscheduler( 0, sched, & param );
842 KMP_MSG( CantChangeMonitorPriority ),
844 KMP_MSG( MonitorWillStarve ),
853 KMP_MSG( RunningAtMaxPriority ),
854 KMP_MSG( MonitorWillStarve ),
855 KMP_HNT( RunningAtMaxPriority ),
860 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
862 #endif // KMP_REAL_TIME_FIX 866 if ( __kmp_monitor_wakeups == 1 ) {
868 interval.tv_nsec = 0;
871 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
874 KA_TRACE( 10, (
"__kmp_launch_monitor: #2 monitor\n" ) );
876 if (__kmp_yield_cycle) {
877 __kmp_yielding_on = 0;
878 yield_count = __kmp_yield_off_count;
880 __kmp_yielding_on = 1;
883 while( ! TCR_4( __kmp_global.g.g_done ) ) {
889 KA_TRACE( 15, (
"__kmp_launch_monitor: update\n" ) );
891 status = gettimeofday( &tval, NULL );
892 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status );
893 TIMEVAL_TO_TIMESPEC( &tval, &now );
895 now.tv_sec += interval.tv_sec;
896 now.tv_nsec += interval.tv_nsec;
898 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
900 now.tv_nsec -= KMP_NSEC_PER_SEC;
903 status = pthread_mutex_lock( & __kmp_wait_mx.m_mutex );
904 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status );
906 if ( !TCR_4(__kmp_global.g.g_done) ) {
907 status = pthread_cond_timedwait( &__kmp_wait_cv.c_cond, &__kmp_wait_mx.m_mutex, &now );
909 if ( status != ETIMEDOUT && status != EINTR ) {
910 KMP_SYSFAIL(
"pthread_cond_timedwait", status );
914 status = pthread_mutex_unlock( & __kmp_wait_mx.m_mutex );
915 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
917 if (__kmp_yield_cycle) {
919 if ( (yield_cycles % yield_count) == 0 ) {
920 if (__kmp_yielding_on) {
921 __kmp_yielding_on = 0;
922 yield_count = __kmp_yield_off_count;
924 __kmp_yielding_on = 1;
925 yield_count = __kmp_yield_on_count;
930 __kmp_yielding_on = 1;
933 TCW_4( __kmp_global.g.g_time.dt.t_value,
934 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
939 KA_TRACE( 10, (
"__kmp_launch_monitor: #3 cleanup\n" ) );
941 #ifdef KMP_BLOCK_SIGNALS 942 status = sigfillset( & new_set );
943 KMP_CHECK_SYSFAIL_ERRNO(
"sigfillset", status );
944 status = pthread_sigmask( SIG_UNBLOCK, & new_set, NULL );
945 KMP_CHECK_SYSFAIL(
"pthread_sigmask", status );
948 KA_TRACE( 10, (
"__kmp_launch_monitor: #4 finished\n" ) );
950 if( __kmp_global.g.g_abort != 0 ) {
956 KA_TRACE( 10, (
"__kmp_launch_monitor: #5 terminate sig=%d\n", __kmp_global.g.g_abort ) );
961 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
962 __kmp_terminate_thread( gtid );
966 KA_TRACE( 10, (
"__kmp_launch_monitor: #6 raise sig=%d\n", __kmp_global.g.g_abort ) );
968 if (__kmp_global.g.g_abort > 0)
969 raise( __kmp_global.g.g_abort );
973 KA_TRACE( 10, (
"__kmp_launch_monitor: #7 exit\n" ) );
979 __kmp_create_worker(
int gtid, kmp_info_t *th,
size_t stack_size )
982 pthread_attr_t thread_attr;
986 th->th.th_info.ds.ds_gtid = gtid;
988 #if KMP_STATS_ENABLED 990 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
995 th->th.th_stats = __kmp_stats_list.push_back(gtid);
996 if(KMP_UBER_GTID(gtid)) {
997 __kmp_stats_start_time = tsc_tick_count::now();
998 __kmp_stats_thread_ptr = th->th.th_stats;
1003 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
1005 #endif // KMP_STATS_ENABLED 1007 if ( KMP_UBER_GTID(gtid) ) {
1008 KA_TRACE( 10, (
"__kmp_create_worker: uber thread (%d)\n", gtid ) );
1009 th -> th.th_info.ds.ds_thread = pthread_self();
1010 __kmp_set_stack_info( gtid, th );
1011 __kmp_check_stack_overlap( th );
1015 KA_TRACE( 10, (
"__kmp_create_worker: try to create thread (%d)\n", gtid ) );
1019 #ifdef KMP_THREAD_ATTR 1021 status = pthread_attr_init( &thread_attr );
1022 if ( status != 0 ) {
1025 KMP_MSG( CantInitThreadAttrs ),
1030 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1031 if ( status != 0 ) {
1034 KMP_MSG( CantSetWorkerState ),
1046 stack_size += gtid * __kmp_stkoffset * 2;
1048 KA_TRACE( 10, (
"__kmp_create_worker: T#%d, default stacksize = %lu bytes, " 1049 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
1050 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size ) );
1052 # ifdef _POSIX_THREAD_ATTR_STACKSIZE 1053 status = pthread_attr_setstacksize( & thread_attr, stack_size );
1054 # ifdef KMP_BACKUP_STKSIZE 1055 if ( status != 0 ) {
1056 if ( ! __kmp_env_stksize ) {
1057 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
1058 __kmp_stksize = KMP_BACKUP_STKSIZE;
1059 KA_TRACE( 10, (
"__kmp_create_worker: T#%d, default stacksize = %lu bytes, " 1060 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu " 1062 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size )
1064 status = pthread_attr_setstacksize( &thread_attr, stack_size );
1068 if ( status != 0 ) {
1071 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1073 KMP_HNT( ChangeWorkerStackSize ),
1082 status = pthread_create( & handle, & thread_attr, __kmp_launch_worker, (
void *) th );
1083 if ( status != 0 || ! handle ) {
1084 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 1085 if ( status == EINVAL ) {
1088 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1090 KMP_HNT( IncreaseWorkerStackSize ),
1094 if ( status == ENOMEM ) {
1097 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1099 KMP_HNT( DecreaseWorkerStackSize ),
1104 if ( status == EAGAIN ) {
1107 KMP_MSG( NoResourcesForWorkerThread ),
1109 KMP_HNT( Decrease_NUM_THREADS ),
1113 KMP_SYSFAIL(
"pthread_create", status );
1116 th->th.th_info.ds.ds_thread = handle;
1119 #ifdef KMP_THREAD_ATTR 1121 status = pthread_attr_destroy( & thread_attr );
1125 KMP_MSG( CantDestroyThreadAttrs ),
1135 KA_TRACE( 10, (
"__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1141 __kmp_create_monitor( kmp_info_t *th )
1144 pthread_attr_t thread_attr;
1147 int auto_adj_size = FALSE;
1149 if( __kmp_dflt_blocktime == KMP_MAX_BLOCKTIME ) {
1151 KA_TRACE( 10, (
"__kmp_create_monitor: skipping monitor thread because of MAX blocktime\n" ) );
1152 th->th.th_info.ds.ds_tid = 0;
1153 th->th.th_info.ds.ds_gtid = 0;
1156 KA_TRACE( 10, (
"__kmp_create_monitor: try to create monitor\n" ) );
1160 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1161 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1162 #if KMP_REAL_TIME_FIX 1163 TCW_4( __kmp_global.g.g_time.dt.t_value, -1 );
1165 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
1166 #endif // KMP_REAL_TIME_FIX 1168 #ifdef KMP_THREAD_ATTR 1169 if ( __kmp_monitor_stksize == 0 ) {
1170 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1171 auto_adj_size = TRUE;
1173 status = pthread_attr_init( &thread_attr );
1174 if ( status != 0 ) {
1177 KMP_MSG( CantInitThreadAttrs ),
1182 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1183 if ( status != 0 ) {
1186 KMP_MSG( CantSetMonitorState ),
1192 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 1193 status = pthread_attr_getstacksize( & thread_attr, & size );
1194 KMP_CHECK_SYSFAIL(
"pthread_attr_getstacksize", status );
1196 size = __kmp_sys_min_stksize;
1200 if ( __kmp_monitor_stksize == 0 ) {
1201 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1203 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1204 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1207 KA_TRACE( 10, (
"__kmp_create_monitor: default stacksize = %lu bytes," 1208 "requested stacksize = %lu bytes\n",
1209 size, __kmp_monitor_stksize ) );
1215 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 1216 KA_TRACE( 10, (
"__kmp_create_monitor: setting stacksize = %lu bytes,",
1217 __kmp_monitor_stksize ) );
1218 status = pthread_attr_setstacksize( & thread_attr, __kmp_monitor_stksize );
1219 if ( status != 0 ) {
1220 if ( auto_adj_size ) {
1221 __kmp_monitor_stksize *= 2;
1226 KMP_MSG( CantSetMonitorStackSize, (
long int) __kmp_monitor_stksize ),
1228 KMP_HNT( ChangeMonitorStackSize ),
1234 status = pthread_create( &handle, & thread_attr, __kmp_launch_monitor, (
void *) th );
1236 if ( status != 0 ) {
1237 #ifdef _POSIX_THREAD_ATTR_STACKSIZE 1238 if ( status == EINVAL ) {
1239 if ( auto_adj_size && ( __kmp_monitor_stksize < (
size_t)0x40000000 ) ) {
1240 __kmp_monitor_stksize *= 2;
1245 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1247 KMP_HNT( IncreaseMonitorStackSize ),
1251 if ( status == ENOMEM ) {
1254 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1256 KMP_HNT( DecreaseMonitorStackSize ),
1261 if ( status == EAGAIN ) {
1264 KMP_MSG( NoResourcesForMonitorThread ),
1266 KMP_HNT( DecreaseNumberOfThreadsInUse ),
1270 KMP_SYSFAIL(
"pthread_create", status );
1273 th->th.th_info.ds.ds_thread = handle;
1275 #if KMP_REAL_TIME_FIX 1277 KMP_DEBUG_ASSERT(
sizeof( kmp_uint32 ) ==
sizeof( __kmp_global.g.g_time.dt.t_value ) );
1279 (kmp_uint32
volatile *) & __kmp_global.g.g_time.dt.t_value, -1, & __kmp_neq_4, NULL
1281 #endif // KMP_REAL_TIME_FIX 1283 #ifdef KMP_THREAD_ATTR 1284 status = pthread_attr_destroy( & thread_attr );
1285 if ( status != 0 ) {
1288 KMP_MSG( CantDestroyThreadAttrs ),
1297 KA_TRACE( 10, (
"__kmp_create_monitor: monitor created %#.8lx\n", th->th.th_info.ds.ds_thread ) );
1305 pthread_exit( (
void *)(intptr_t) exit_status );
1308 void __kmp_resume_monitor();
1311 __kmp_reap_monitor( kmp_info_t *th )
1316 KA_TRACE( 10, (
"__kmp_reap_monitor: try to reap monitor thread with handle %#.8lx\n",
1317 th->th.th_info.ds.ds_thread ) );
1322 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1323 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
1324 KA_TRACE( 10, (
"__kmp_reap_monitor: monitor did not start, returning\n") );
1335 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1336 if (status == ESRCH) {
1338 KA_TRACE( 10, (
"__kmp_reap_monitor: monitor does not exist, returning\n") );
1342 __kmp_resume_monitor();
1343 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1344 if (exit_val != th) {
1347 KMP_MSG( ReapMonitorError ),
1354 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1355 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1357 KA_TRACE( 10, (
"__kmp_reap_monitor: done reaping monitor thread with handle %#.8lx\n",
1358 th->th.th_info.ds.ds_thread ) );
1365 __kmp_reap_worker( kmp_info_t *th )
1372 KA_TRACE( 10, (
"__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid ) );
1379 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1380 if (status == ESRCH) {
1381 KA_TRACE( 10, (
"__kmp_reap_worker: worker T#%d does not exist, returning\n",
1382 th->th.th_info.ds.ds_gtid ) );
1385 KA_TRACE( 10, (
"__kmp_reap_worker: try to join with worker T#%d\n",
1386 th->th.th_info.ds.ds_gtid ) );
1388 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1391 if ( status != 0 ) {
1394 KMP_MSG( ReapWorkerError ),
1399 if ( exit_val != th ) {
1400 KA_TRACE( 10, (
"__kmp_reap_worker: worker T#%d did not reap properly, " 1402 th->th.th_info.ds.ds_gtid, exit_val ) );
1408 KA_TRACE( 10, (
"__kmp_reap_worker: done reaping T#%d\n", th->th.th_info.ds.ds_gtid ) );
1417 #if KMP_HANDLE_SIGNALS 1421 __kmp_null_handler(
int signo )
1428 __kmp_team_handler(
int signo )
1430 if ( __kmp_global.g.g_abort == 0 ) {
1433 __kmp_debug_printf(
"__kmp_team_handler: caught signal = %d\n", signo );
1448 if ( __kmp_debug_buf ) {
1449 __kmp_dump_debug_buffer( );
1452 TCW_4( __kmp_global.g.g_abort, signo );
1454 TCW_4( __kmp_global.g.g_done, TRUE );
1459 __kmp_debug_printf(
"__kmp_team_handler: unknown signal type" );
1468 void __kmp_sigaction(
int signum,
const struct sigaction * act,
struct sigaction * oldact ) {
1469 int rc = sigaction( signum, act, oldact );
1470 KMP_CHECK_SYSFAIL_ERRNO(
"sigaction", rc );
1475 __kmp_install_one_handler(
int sig, sig_func_t handler_func,
int parallel_init )
1478 KB_TRACE( 60, (
"__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init ) );
1479 if ( parallel_init ) {
1480 struct sigaction new_action;
1481 struct sigaction old_action;
1482 new_action.sa_handler = handler_func;
1483 new_action.sa_flags = 0;
1484 sigfillset( & new_action.sa_mask );
1485 __kmp_sigaction( sig, & new_action, & old_action );
1486 if ( old_action.sa_handler == __kmp_sighldrs[ sig ].sa_handler ) {
1487 sigaddset( & __kmp_sigset, sig );
1490 __kmp_sigaction( sig, & old_action, NULL );
1494 __kmp_sigaction( sig, NULL, & __kmp_sighldrs[ sig ] );
1501 __kmp_remove_one_handler(
int sig )
1503 KB_TRACE( 60, (
"__kmp_remove_one_handler( %d )\n", sig ) );
1504 if ( sigismember( & __kmp_sigset, sig ) ) {
1505 struct sigaction old;
1507 __kmp_sigaction( sig, & __kmp_sighldrs[ sig ], & old );
1508 if ( ( old.sa_handler != __kmp_team_handler ) && ( old.sa_handler != __kmp_null_handler ) ) {
1510 KB_TRACE( 10, (
"__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1511 __kmp_sigaction( sig, & old, NULL );
1513 sigdelset( & __kmp_sigset, sig );
1520 __kmp_install_signals(
int parallel_init )
1522 KB_TRACE( 10, (
"__kmp_install_signals( %d )\n", parallel_init ) );
1523 if ( __kmp_handle_signals || ! parallel_init ) {
1526 sigemptyset( & __kmp_sigset );
1527 __kmp_install_one_handler( SIGHUP, __kmp_team_handler, parallel_init );
1528 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1529 __kmp_install_one_handler( SIGQUIT, __kmp_team_handler, parallel_init );
1530 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1531 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1532 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1533 __kmp_install_one_handler( SIGBUS, __kmp_team_handler, parallel_init );
1534 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1536 __kmp_install_one_handler( SIGSYS, __kmp_team_handler, parallel_init );
1538 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1540 __kmp_install_one_handler( SIGPIPE, __kmp_team_handler, parallel_init );
1547 __kmp_remove_signals(
void )
1550 KB_TRACE( 10, (
"__kmp_remove_signals()\n" ) );
1551 for ( sig = 1; sig < NSIG; ++ sig ) {
1552 __kmp_remove_one_handler( sig );
1557 #endif // KMP_HANDLE_SIGNALS 1563 __kmp_enable(
int new_state )
1565 #ifdef KMP_CANCEL_THREADS 1566 int status, old_state;
1567 status = pthread_setcancelstate( new_state, & old_state );
1568 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status );
1569 KMP_DEBUG_ASSERT( old_state == PTHREAD_CANCEL_DISABLE );
1574 __kmp_disable(
int * old_state )
1576 #ifdef KMP_CANCEL_THREADS 1578 status = pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, old_state );
1579 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status );
1587 __kmp_atfork_prepare (
void)
1593 __kmp_atfork_parent (
void)
1604 __kmp_atfork_child (
void)
1612 __kmp_init_runtime = FALSE;
1613 __kmp_init_monitor = 0;
1614 __kmp_init_parallel = FALSE;
1615 __kmp_init_middle = FALSE;
1616 __kmp_init_serial = FALSE;
1617 TCW_4(__kmp_init_gtid, FALSE);
1618 __kmp_init_common = FALSE;
1620 TCW_4(__kmp_init_user_locks, FALSE);
1621 #if ! KMP_USE_DYNAMIC_LOCK 1622 __kmp_user_lock_table.used = 1;
1623 __kmp_user_lock_table.allocated = 0;
1624 __kmp_user_lock_table.table = NULL;
1625 __kmp_lock_blocks = NULL;
1629 TCW_4(__kmp_nth, 0);
1633 KA_TRACE( 10, (
"__kmp_atfork_child: checking cache address list %p\n",
1634 __kmp_threadpriv_cache_list ) );
1636 while ( __kmp_threadpriv_cache_list != NULL ) {
1638 if ( *__kmp_threadpriv_cache_list -> addr != NULL ) {
1639 KC_TRACE( 50, (
"__kmp_atfork_child: zeroing cache at address %p\n",
1640 &(*__kmp_threadpriv_cache_list -> addr) ) );
1642 *__kmp_threadpriv_cache_list -> addr = NULL;
1644 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list -> next;
1647 __kmp_init_runtime = FALSE;
1650 __kmp_init_bootstrap_lock( &__kmp_initz_lock );
1651 __kmp_init_bootstrap_lock( &__kmp_stdio_lock );
1652 __kmp_init_bootstrap_lock( &__kmp_console_lock );
1667 __kmp_register_atfork(
void) {
1668 if ( __kmp_need_register_atfork ) {
1669 int status = pthread_atfork( __kmp_atfork_prepare, __kmp_atfork_parent, __kmp_atfork_child );
1670 KMP_CHECK_SYSFAIL(
"pthread_atfork", status );
1671 __kmp_need_register_atfork = FALSE;
1676 __kmp_suspend_initialize(
void )
1679 status = pthread_mutexattr_init( &__kmp_suspend_mutex_attr );
1680 KMP_CHECK_SYSFAIL(
"pthread_mutexattr_init", status );
1681 status = pthread_condattr_init( &__kmp_suspend_cond_attr );
1682 KMP_CHECK_SYSFAIL(
"pthread_condattr_init", status );
1686 __kmp_suspend_initialize_thread( kmp_info_t *th )
1688 if ( th->th.th_suspend_init_count <= __kmp_fork_count ) {
1692 status = pthread_cond_init( &th->th.th_suspend_cv.c_cond, &__kmp_suspend_cond_attr );
1693 KMP_CHECK_SYSFAIL(
"pthread_cond_init", status );
1694 status = pthread_mutex_init( &th->th.th_suspend_mx.m_mutex, & __kmp_suspend_mutex_attr );
1695 KMP_CHECK_SYSFAIL(
"pthread_mutex_init", status );
1696 *(
volatile int*)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1701 __kmp_suspend_uninitialize_thread( kmp_info_t *th )
1703 if(th->th.th_suspend_init_count > __kmp_fork_count) {
1708 status = pthread_cond_destroy( &th->th.th_suspend_cv.c_cond );
1709 if ( status != 0 && status != EBUSY ) {
1710 KMP_SYSFAIL(
"pthread_cond_destroy", status );
1712 status = pthread_mutex_destroy( &th->th.th_suspend_mx.m_mutex );
1713 if ( status != 0 && status != EBUSY ) {
1714 KMP_SYSFAIL(
"pthread_mutex_destroy", status );
1716 --th->th.th_suspend_init_count;
1717 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1725 static inline void __kmp_suspend_template(
int th_gtid, C *flag )
1727 KMP_TIME_DEVELOPER_BLOCK(USER_suspend);
1728 kmp_info_t *th = __kmp_threads[th_gtid];
1730 typename C::flag_t old_spin;
1732 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, flag->get() ) );
1734 __kmp_suspend_initialize_thread( th );
1736 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1737 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status );
1739 KF_TRACE( 10, (
"__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1740 th_gtid, flag->get() ) );
1745 old_spin = flag->set_sleeping();
1747 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x, was %x\n",
1748 th_gtid, flag->get(), *(flag->get()), old_spin ) );
1750 if ( flag->done_check_val(old_spin) ) {
1751 old_spin = flag->unset_sleeping();
1752 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d false alarm, reset sleep bit for spin(%p)\n",
1753 th_gtid, flag->get()) );
1759 int deactivated = FALSE;
1760 TCW_PTR(th->th.th_sleep_loc, (
void *)flag);
1761 while ( flag->is_sleeping() ) {
1762 #ifdef DEBUG_SUSPEND 1764 __kmp_suspend_count++;
1765 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
1766 __kmp_printf(
"__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, buffer );
1769 if ( ! deactivated ) {
1770 th->th.th_active = FALSE;
1771 if ( th->th.th_active_in_pool ) {
1772 th->th.th_active_in_pool = FALSE;
1773 KMP_TEST_THEN_DEC32(
1774 (kmp_int32 *) &__kmp_thread_pool_active_nth );
1775 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
1782 #if USE_SUSPEND_TIMEOUT 1783 struct timespec now;
1784 struct timeval tval;
1787 status = gettimeofday( &tval, NULL );
1788 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status );
1789 TIMEVAL_TO_TIMESPEC( &tval, &now );
1791 msecs = (4*__kmp_dflt_blocktime) + 200;
1792 now.tv_sec += msecs / 1000;
1793 now.tv_nsec += (msecs % 1000)*1000;
1795 KF_TRACE( 15, (
"__kmp_suspend_template: T#%d about to perform pthread_cond_timedwait\n",
1797 status = pthread_cond_timedwait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex, & now );
1799 KF_TRACE( 15, (
"__kmp_suspend_template: T#%d about to perform pthread_cond_wait\n",
1801 status = pthread_cond_wait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex );
1804 if ( (status != 0) && (status != EINTR) && (status != ETIMEDOUT) ) {
1805 KMP_SYSFAIL(
"pthread_cond_wait", status );
1808 if (status == ETIMEDOUT) {
1809 if ( flag->is_sleeping() ) {
1810 KF_TRACE( 100, (
"__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid ) );
1812 KF_TRACE( 2, (
"__kmp_suspend_template: T#%d timeout wakeup, sleep bit not set!\n",
1815 }
else if ( flag->is_sleeping() ) {
1816 KF_TRACE( 100, (
"__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ) );
1822 if ( deactivated ) {
1823 th->th.th_active = TRUE;
1824 if ( TCR_4(th->th.th_in_pool) ) {
1825 KMP_TEST_THEN_INC32( (kmp_int32 *) &__kmp_thread_pool_active_nth );
1826 th->th.th_active_in_pool = TRUE;
1831 #ifdef DEBUG_SUSPEND 1834 __kmp_print_cond( buffer, &th->th.th_suspend_cv);
1835 __kmp_printf(
"__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, buffer );
1840 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1841 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1843 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d exit\n", th_gtid ) );
1846 void __kmp_suspend_32(
int th_gtid, kmp_flag_32 *flag) {
1847 __kmp_suspend_template(th_gtid, flag);
1849 void __kmp_suspend_64(
int th_gtid, kmp_flag_64 *flag) {
1850 __kmp_suspend_template(th_gtid, flag);
1852 void __kmp_suspend_oncore(
int th_gtid, kmp_flag_oncore *flag) {
1853 __kmp_suspend_template(th_gtid, flag);
1862 static inline void __kmp_resume_template(
int target_gtid, C *flag )
1864 KMP_TIME_DEVELOPER_BLOCK(USER_resume);
1865 kmp_info_t *th = __kmp_threads[target_gtid];
1869 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1872 KF_TRACE( 30, (
"__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", gtid, target_gtid ) );
1873 KMP_DEBUG_ASSERT( gtid != target_gtid );
1875 __kmp_suspend_initialize_thread( th );
1877 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1878 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status );
1881 flag = (C *)th->th.th_sleep_loc;
1885 if (!flag || flag->get_type() != flag->get_ptr_type()) {
1886 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p)\n",
1887 gtid, target_gtid, NULL ) );
1888 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1889 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1893 typename C::flag_t old_spin = flag->unset_sleeping();
1894 if ( ! flag->is_sleeping_val(old_spin) ) {
1895 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p): " 1897 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
1899 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1900 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1903 KF_TRACE( 5, (
"__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p): " 1905 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
1907 TCW_PTR(th->th.th_sleep_loc, NULL);
1910 #ifdef DEBUG_SUSPEND 1913 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
1914 __kmp_printf(
"__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, target_gtid, buffer );
1919 status = pthread_cond_signal( &th->th.th_suspend_cv.c_cond );
1920 KMP_CHECK_SYSFAIL(
"pthread_cond_signal", status );
1921 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1922 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1923 KF_TRACE( 30, (
"__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
1924 gtid, target_gtid ) );
1927 void __kmp_resume_32(
int target_gtid, kmp_flag_32 *flag) {
1928 __kmp_resume_template(target_gtid, flag);
1930 void __kmp_resume_64(
int target_gtid, kmp_flag_64 *flag) {
1931 __kmp_resume_template(target_gtid, flag);
1933 void __kmp_resume_oncore(
int target_gtid, kmp_flag_oncore *flag) {
1934 __kmp_resume_template(target_gtid, flag);
1938 __kmp_resume_monitor()
1942 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1943 KF_TRACE( 30, (
"__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n",
1944 gtid, KMP_GTID_MONITOR ) );
1945 KMP_DEBUG_ASSERT( gtid != KMP_GTID_MONITOR );
1947 status = pthread_mutex_lock( &__kmp_wait_mx.m_mutex );
1948 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status );
1949 #ifdef DEBUG_SUSPEND 1952 __kmp_print_cond( buffer, &__kmp_wait_cv.c_cond );
1953 __kmp_printf(
"__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid, KMP_GTID_MONITOR, buffer );
1956 status = pthread_cond_signal( &__kmp_wait_cv.c_cond );
1957 KMP_CHECK_SYSFAIL(
"pthread_cond_signal", status );
1958 status = pthread_mutex_unlock( &__kmp_wait_mx.m_mutex );
1959 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1960 KF_TRACE( 30, (
"__kmp_resume_monitor: T#%d exiting after signaling wake up for T#%d\n",
1961 gtid, KMP_GTID_MONITOR ) );
1968 __kmp_yield(
int cond )
1970 if (cond && __kmp_yielding_on) {
1979 __kmp_gtid_set_specific(
int gtid )
1982 KMP_ASSERT( __kmp_init_runtime );
1983 status = pthread_setspecific( __kmp_gtid_threadprivate_key, (
void*)(intptr_t)(gtid+1) );
1984 KMP_CHECK_SYSFAIL(
"pthread_setspecific", status );
1988 __kmp_gtid_get_specific()
1991 if ( !__kmp_init_runtime ) {
1992 KA_TRACE( 50, (
"__kmp_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
1993 return KMP_GTID_SHUTDOWN;
1995 gtid = (int)(
size_t)pthread_getspecific( __kmp_gtid_threadprivate_key );
1997 gtid = KMP_GTID_DNE;
2002 KA_TRACE( 50, (
"__kmp_gtid_get_specific: key:%d gtid:%d\n",
2003 __kmp_gtid_threadprivate_key, gtid ));
2011 __kmp_read_cpu_time(
void )
2018 return (buffer.tms_utime + buffer.tms_cutime) / (double) CLOCKS_PER_SEC;
2022 __kmp_read_system_info(
struct kmp_sys_info *info )
2025 struct rusage r_usage;
2027 memset( info, 0,
sizeof( *info ) );
2029 status = getrusage( RUSAGE_SELF, &r_usage);
2030 KMP_CHECK_SYSFAIL_ERRNO(
"getrusage", status );
2032 info->maxrss = r_usage.ru_maxrss;
2033 info->minflt = r_usage.ru_minflt;
2034 info->majflt = r_usage.ru_majflt;
2035 info->nswap = r_usage.ru_nswap;
2036 info->inblock = r_usage.ru_inblock;
2037 info->oublock = r_usage.ru_oublock;
2038 info->nvcsw = r_usage.ru_nvcsw;
2039 info->nivcsw = r_usage.ru_nivcsw;
2041 return (status != 0);
2048 __kmp_read_system_time(
double *delta )
2051 struct timeval tval;
2052 struct timespec stop;
2055 status = gettimeofday( &tval, NULL );
2056 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status );
2057 TIMEVAL_TO_TIMESPEC( &tval, &stop );
2058 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
2059 *delta = (t_ns * 1e-9);
2063 __kmp_clear_system_time(
void )
2065 struct timeval tval;
2067 status = gettimeofday( &tval, NULL );
2068 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status );
2069 TIMEVAL_TO_TIMESPEC( &tval, &__kmp_sys_timer_data.start );
2078 __kmp_tv_threadprivate_store( kmp_info_t *th,
void *global_addr,
void *thread_addr )
2082 p = (
struct tv_data *) __kmp_allocate(
sizeof( *p ) );
2084 p->u.tp.global_addr = global_addr;
2085 p->u.tp.thread_addr = thread_addr;
2087 p->type = (
void *) 1;
2089 p->next = th->th.th_local.tv_data;
2090 th->th.th_local.tv_data = p;
2092 if ( p->next == 0 ) {
2093 int rc = pthread_setspecific( __kmp_tv_key, p );
2094 KMP_CHECK_SYSFAIL(
"pthread_setspecific", rc );
2104 __kmp_get_xproc(
void ) {
2108 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD 2110 r = sysconf( _SC_NPROCESSORS_ONLN );
2118 host_basic_info_data_t info;
2119 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
2120 rc = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) & info, & num );
2121 if ( rc == 0 && num == HOST_BASIC_INFO_COUNT ) {
2124 r = info.avail_cpus;
2126 KMP_WARNING( CantGetNumAvailCPU );
2127 KMP_INFORM( AssumedNumCPU );
2132 #error "Unknown or unsupported OS." 2136 return r > 0 ? r : 2;
2141 __kmp_read_from_file(
char const *path,
char const *format, ... )
2146 va_start(args, format);
2147 FILE *f = fopen(path,
"rb");
2150 result = vfscanf(f, format, args);
2157 __kmp_runtime_initialize(
void )
2160 pthread_mutexattr_t mutex_attr;
2161 pthread_condattr_t cond_attr;
2163 if ( __kmp_init_runtime ) {
2167 #if ( KMP_ARCH_X86 || KMP_ARCH_X86_64 ) 2168 if ( ! __kmp_cpuinfo.initialized ) {
2169 __kmp_query_cpuid( &__kmp_cpuinfo );
2173 __kmp_xproc = __kmp_get_xproc();
2175 if ( sysconf( _SC_THREADS ) ) {
2178 __kmp_sys_max_nth = sysconf( _SC_THREAD_THREADS_MAX );
2179 if ( __kmp_sys_max_nth == -1 ) {
2181 __kmp_sys_max_nth = INT_MAX;
2183 else if ( __kmp_sys_max_nth <= 1 ) {
2185 __kmp_sys_max_nth = KMP_MAX_NTH;
2189 __kmp_sys_min_stksize = sysconf( _SC_THREAD_STACK_MIN );
2190 if ( __kmp_sys_min_stksize <= 1 ) {
2191 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
2196 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
2200 int rc = pthread_key_create( & __kmp_tv_key, 0 );
2201 KMP_CHECK_SYSFAIL(
"pthread_key_create", rc );
2205 status = pthread_key_create( &__kmp_gtid_threadprivate_key, __kmp_internal_end_dest );
2206 KMP_CHECK_SYSFAIL(
"pthread_key_create", status );
2207 status = pthread_mutexattr_init( & mutex_attr );
2208 KMP_CHECK_SYSFAIL(
"pthread_mutexattr_init", status );
2209 status = pthread_mutex_init( & __kmp_wait_mx.m_mutex, & mutex_attr );
2210 KMP_CHECK_SYSFAIL(
"pthread_mutex_init", status );
2211 status = pthread_condattr_init( & cond_attr );
2212 KMP_CHECK_SYSFAIL(
"pthread_condattr_init", status );
2213 status = pthread_cond_init( & __kmp_wait_cv.c_cond, & cond_attr );
2214 KMP_CHECK_SYSFAIL(
"pthread_cond_init", status );
2216 __kmp_itt_initialize();
2219 __kmp_init_runtime = TRUE;
2223 __kmp_runtime_destroy(
void )
2227 if ( ! __kmp_init_runtime ) {
2232 __kmp_itt_destroy();
2235 status = pthread_key_delete( __kmp_gtid_threadprivate_key );
2236 KMP_CHECK_SYSFAIL(
"pthread_key_delete", status );
2238 status = pthread_key_delete( __kmp_tv_key );
2239 KMP_CHECK_SYSFAIL(
"pthread_key_delete", status );
2242 status = pthread_mutex_destroy( & __kmp_wait_mx.m_mutex );
2243 if ( status != 0 && status != EBUSY ) {
2244 KMP_SYSFAIL(
"pthread_mutex_destroy", status );
2246 status = pthread_cond_destroy( & __kmp_wait_cv.c_cond );
2247 if ( status != 0 && status != EBUSY ) {
2248 KMP_SYSFAIL(
"pthread_cond_destroy", status );
2250 #if KMP_AFFINITY_SUPPORTED 2251 __kmp_affinity_uninitialize();
2254 __kmp_init_runtime = FALSE;
2261 __kmp_thread_sleep(
int millis )
2263 sleep( ( millis + 500 ) / 1000 );
2268 __kmp_elapsed(
double *t )
2271 # ifdef FIX_SGI_CLOCK 2274 status = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
2275 KMP_CHECK_SYSFAIL_ERRNO(
"clock_gettime", status );
2276 *t = (double) ts.tv_nsec * (1.0 / (
double) KMP_NSEC_PER_SEC) +
2281 status = gettimeofday( & tv, NULL );
2282 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status );
2283 *t = (double) tv.tv_usec * (1.0 / (
double) KMP_USEC_PER_SEC) +
2290 __kmp_elapsed_tick(
double *t )
2292 *t = 1 / (double) CLOCKS_PER_SEC;
2300 __kmp_is_address_mapped(
void * addr ) {
2305 #if KMP_OS_LINUX || KMP_OS_FREEBSD 2312 char * name = __kmp_str_format(
"/proc/%d/maps", getpid() );
2315 file = fopen( name,
"r" );
2316 KMP_ASSERT( file != NULL );
2320 void * beginning = NULL;
2321 void * ending = NULL;
2324 rc = fscanf( file,
"%p-%p %4s %*[^\n]\n", & beginning, & ending, perms );
2328 KMP_ASSERT( rc == 3 && KMP_STRLEN( perms ) == 4 );
2331 if ( ( addr >= beginning ) && ( addr < ending ) ) {
2333 if ( strcmp( perms,
"rw" ) == 0 ) {
2344 KMP_INTERNAL_FREE( name );
2358 (vm_address_t)( addr ),
2360 (vm_address_t)( & buffer ),
2368 #elif KMP_OS_FREEBSD || KMP_OS_NETBSD 2375 #error "Unknown or unsupported OS" 2383 #ifdef USE_LOAD_BALANCE 2394 __kmp_get_load_balance(
int max )
2399 int res = getloadavg( averages, 3 );
2404 if ( __kmp_load_balance_interval < 180 && ( res >= 1 ) ) {
2405 ret_avg = averages[0];
2406 }
else if ( ( __kmp_load_balance_interval >= 180
2407 && __kmp_load_balance_interval < 600 ) && ( res >= 2 ) ) {
2408 ret_avg = averages[1];
2409 }
else if ( ( __kmp_load_balance_interval >= 600 ) && ( res == 3 ) ) {
2410 ret_avg = averages[2];
2424 __kmp_get_load_balance(
int max )
2426 static int permanent_error = 0;
2428 static int glb_running_threads = 0;
2429 static double glb_call_time = 0;
2431 int running_threads = 0;
2433 DIR * proc_dir = NULL;
2434 struct dirent * proc_entry = NULL;
2436 kmp_str_buf_t task_path;
2437 DIR * task_dir = NULL;
2438 struct dirent * task_entry = NULL;
2439 int task_path_fixed_len;
2441 kmp_str_buf_t stat_path;
2443 int stat_path_fixed_len;
2445 int total_processes = 0;
2446 int total_threads = 0;
2448 double call_time = 0.0;
2450 __kmp_str_buf_init( & task_path );
2451 __kmp_str_buf_init( & stat_path );
2453 __kmp_elapsed( & call_time );
2455 if ( glb_call_time &&
2456 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
2457 running_threads = glb_running_threads;
2461 glb_call_time = call_time;
2464 if ( permanent_error ) {
2465 running_threads = -1;
2474 proc_dir = opendir(
"/proc" );
2475 if ( proc_dir == NULL ) {
2478 running_threads = -1;
2479 permanent_error = 1;
2484 __kmp_str_buf_cat( & task_path,
"/proc/", 6 );
2485 task_path_fixed_len = task_path.used;
2487 proc_entry = readdir( proc_dir );
2488 while ( proc_entry != NULL ) {
2491 if ( proc_entry->d_type == DT_DIR && isdigit( proc_entry->d_name[ 0 ] ) ) {
2499 KMP_DEBUG_ASSERT( total_processes != 1 || strcmp( proc_entry->d_name,
"1" ) == 0 );
2502 task_path.used = task_path_fixed_len;
2503 __kmp_str_buf_cat( & task_path, proc_entry->d_name, KMP_STRLEN( proc_entry->d_name ) );
2504 __kmp_str_buf_cat( & task_path,
"/task", 5 );
2506 task_dir = opendir( task_path.str );
2507 if ( task_dir == NULL ) {
2515 if ( strcmp( proc_entry->d_name,
"1" ) == 0 ) {
2516 running_threads = -1;
2517 permanent_error = 1;
2522 __kmp_str_buf_clear( & stat_path );
2523 __kmp_str_buf_cat( & stat_path, task_path.str, task_path.used );
2524 __kmp_str_buf_cat( & stat_path,
"/", 1 );
2525 stat_path_fixed_len = stat_path.used;
2527 task_entry = readdir( task_dir );
2528 while ( task_entry != NULL ) {
2530 if ( proc_entry->d_type == DT_DIR && isdigit( task_entry->d_name[ 0 ] ) ) {
2537 stat_path.used = stat_path_fixed_len;
2538 __kmp_str_buf_cat( & stat_path, task_entry->d_name, KMP_STRLEN( task_entry->d_name ) );
2539 __kmp_str_buf_cat( & stat_path,
"/stat", 5 );
2543 stat_file = open( stat_path.str, O_RDONLY );
2544 if ( stat_file == -1 ) {
2577 len = read( stat_file, buffer,
sizeof( buffer ) - 1 );
2584 char * close_parent = strstr( buffer,
") " );
2585 if ( close_parent != NULL ) {
2586 char state = * ( close_parent + 2 );
2587 if ( state ==
'R' ) {
2589 if ( running_threads >= max ) {
2599 task_entry = readdir( task_dir );
2601 closedir( task_dir );
2605 proc_entry = readdir( proc_dir );
2613 KMP_DEBUG_ASSERT( running_threads > 0 );
2614 if ( running_threads <= 0 ) {
2615 running_threads = 1;
2619 if ( proc_dir != NULL ) {
2620 closedir( proc_dir );
2622 __kmp_str_buf_free( & task_path );
2623 if ( task_dir != NULL ) {
2624 closedir( task_dir );
2626 __kmp_str_buf_free( & stat_path );
2627 if ( stat_file != -1 ) {
2631 glb_running_threads = running_threads;
2633 return running_threads;
2637 # endif // KMP_OS_DARWIN 2639 #endif // USE_LOAD_BALANCE 2641 #if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC) 2646 __kmp_invoke_microtask( microtask_t pkfn,
2648 int argc,
void *p_argv[]
2650 ,
void **exit_frame_ptr
2655 *exit_frame_ptr = __builtin_frame_address(0);
2660 fprintf(stderr,
"Too many args to microtask: %d!\n", argc);
2664 (*pkfn)(>id, &tid);
2667 (*pkfn)(>id, &tid, p_argv[0]);
2670 (*pkfn)(>id, &tid, p_argv[0], p_argv[1]);
2673 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2]);
2676 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2679 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2682 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2686 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2687 p_argv[5], p_argv[6]);
2690 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2691 p_argv[5], p_argv[6], p_argv[7]);
2694 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2695 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2698 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2699 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2702 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2703 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2706 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2707 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2711 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2712 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2713 p_argv[11], p_argv[12]);
2716 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2717 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2718 p_argv[11], p_argv[12], p_argv[13]);
2721 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2722 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2723 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2728 *exit_frame_ptr = 0;
#define KMP_START_EXPLICIT_TIMER(name)
"Starts" an explicit timer which will need a corresponding KMP_STOP_EXPLICIT_TIMER() macro...