17 #include "kmp_wrapper_getpid.h"
22 #include "kmp_stats.h"
23 #include "kmp_wait_release.h"
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 <sys/sysctl.h>
55 # include <pthread_np.h>
64 #if KMP_COMPILER_GCC && !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64)
72 struct kmp_sys_timer {
73 struct timespec start;
77 #define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec)
79 static struct kmp_sys_timer __kmp_sys_timer_data;
81 #if KMP_HANDLE_SIGNALS
82 typedef void (* sig_func_t )( int );
83 STATIC_EFI2_WORKAROUND
struct sigaction __kmp_sighldrs[ NSIG ];
84 static sigset_t __kmp_sigset;
87 static int __kmp_init_runtime = FALSE;
89 static int __kmp_fork_count = 0;
91 static pthread_condattr_t __kmp_suspend_cond_attr;
92 static pthread_mutexattr_t __kmp_suspend_mutex_attr;
94 static kmp_cond_align_t __kmp_wait_cv;
95 static kmp_mutex_align_t __kmp_wait_mx;
102 __kmp_print_cond(
char *buffer, kmp_cond_align_t *cond )
104 KMP_SNPRINTF( buffer, 128,
"(cond (lock (%ld, %d)), (descr (%p)))",
105 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
106 cond->c_cond.__c_waiting );
113 #if ( KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
126 # if KMP_ARCH_X86 || KMP_ARCH_ARM
127 # ifndef __NR_sched_setaffinity
128 # define __NR_sched_setaffinity 241
129 # elif __NR_sched_setaffinity != 241
130 # error Wrong code for setaffinity system call.
132 # ifndef __NR_sched_getaffinity
133 # define __NR_sched_getaffinity 242
134 # elif __NR_sched_getaffinity != 242
135 # error Wrong code for getaffinity system call.
138 # elif KMP_ARCH_AARCH64
139 # ifndef __NR_sched_setaffinity
140 # define __NR_sched_setaffinity 122
141 # elif __NR_sched_setaffinity != 122
142 # error Wrong code for setaffinity system call.
144 # ifndef __NR_sched_getaffinity
145 # define __NR_sched_getaffinity 123
146 # elif __NR_sched_getaffinity != 123
147 # error Wrong code for getaffinity system call.
150 # elif KMP_ARCH_X86_64
151 # ifndef __NR_sched_setaffinity
152 # define __NR_sched_setaffinity 203
153 # elif __NR_sched_setaffinity != 203
154 # error Wrong code for setaffinity system call.
156 # ifndef __NR_sched_getaffinity
157 # define __NR_sched_getaffinity 204
158 # elif __NR_sched_getaffinity != 204
159 # error Wrong code for getaffinity system call.
162 # elif KMP_ARCH_PPC64
163 # ifndef __NR_sched_setaffinity
164 # define __NR_sched_setaffinity 222
165 # elif __NR_sched_setaffinity != 222
166 # error Wrong code for setaffinity system call.
168 # ifndef __NR_sched_getaffinity
169 # define __NR_sched_getaffinity 223
170 # elif __NR_sched_getaffinity != 223
171 # error Wrong code for getaffinity system call.
175 # ifndef __NR_sched_setaffinity
176 # define __NR_sched_setaffinity 4239
177 # elif __NR_sched_setaffinity != 4239
178 # error Wrong code for setaffinity system call.
180 # ifndef __NR_sched_getaffinity
181 # define __NR_sched_getaffinity 4240
182 # elif __NR_sched_getaffinity != 4240
183 # error Wrong code for getaffinity system call.
186 # elif KMP_ARCH_MIPS64
187 # ifndef __NR_sched_setaffinity
188 # define __NR_sched_setaffinity 5195
189 # elif __NR_sched_setaffinity != 5195
190 # error Wrong code for setaffinity system call.
192 # ifndef __NR_sched_getaffinity
193 # define __NR_sched_getaffinity 5196
194 # elif __NR_sched_getaffinity != 5196
195 # error Wrong code for getaffinity system call.
199 # error Unknown or unsupported architecture
204 __kmp_set_system_affinity( kmp_affin_mask_t
const *mask,
int abort_on_error )
206 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
207 "Illegal set affinity operation when not capable");
209 int retval = syscall( __NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask );
214 if (abort_on_error) {
217 KMP_MSG( FatalSysError ),
226 __kmp_get_system_affinity( kmp_affin_mask_t *mask,
int abort_on_error )
228 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
229 "Illegal get affinity operation when not capable");
231 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 = (kmp_affin_mask_t *)KMP_ALLOCA(__kmp_affin_mask_size);
255 KMP_CPU_SET(which, mask);
256 __kmp_set_system_affinity(mask, TRUE);
265 __kmp_affinity_determine_capable(
const char *env_var)
271 # define KMP_CPU_SET_SIZE_LIMIT (1024*1024)
275 kmp_affin_mask_t *buf;
276 buf = ( kmp_affin_mask_t * ) KMP_INTERNAL_MALLOC( KMP_CPU_SET_SIZE_LIMIT );
281 gCode = syscall( __NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf );
282 KA_TRACE(30, (
"__kmp_affinity_determine_capable: "
283 "initial getaffinity call returned %d errno = %d\n",
291 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
292 && (__kmp_affinity_type != affinity_none)
293 && (__kmp_affinity_type != affinity_default)
294 && (__kmp_affinity_type != affinity_disabled))) {
298 KMP_MSG( GetAffSysCallNotSupported, env_var ),
303 KMP_AFFINITY_DISABLE();
304 KMP_INTERNAL_FREE(buf);
313 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
314 KA_TRACE(30, (
"__kmp_affinity_determine_capable: "
315 "setaffinity for mask size %d returned %d errno = %d\n",
316 gCode, sCode, errno));
318 if (errno == ENOSYS) {
319 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
320 && (__kmp_affinity_type != affinity_none)
321 && (__kmp_affinity_type != affinity_default)
322 && (__kmp_affinity_type != affinity_disabled))) {
326 KMP_MSG( SetAffSysCallNotSupported, env_var ),
331 KMP_AFFINITY_DISABLE();
332 KMP_INTERNAL_FREE(buf);
334 if (errno == EFAULT) {
335 KMP_AFFINITY_ENABLE(gCode);
336 KA_TRACE(10, (
"__kmp_affinity_determine_capable: "
337 "affinity supported (mask size %d)\n",
338 (
int)__kmp_affin_mask_size));
339 KMP_INTERNAL_FREE(buf);
349 KA_TRACE(30, (
"__kmp_affinity_determine_capable: "
350 "searching for proper set size\n"));
352 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
353 gCode = syscall( __NR_sched_getaffinity, 0, size, buf );
354 KA_TRACE(30, (
"__kmp_affinity_determine_capable: "
355 "getaffinity for mask size %d returned %d errno = %d\n", size,
359 if ( errno == ENOSYS )
364 KA_TRACE(30, (
"__kmp_affinity_determine_capable: "
365 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
367 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
368 && (__kmp_affinity_type != affinity_none)
369 && (__kmp_affinity_type != affinity_default)
370 && (__kmp_affinity_type != affinity_disabled))) {
374 KMP_MSG( GetAffSysCallNotSupported, env_var ),
379 KMP_AFFINITY_DISABLE();
380 KMP_INTERNAL_FREE(buf);
386 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
387 KA_TRACE(30, (
"__kmp_affinity_determine_capable: "
388 "setaffinity for mask size %d returned %d errno = %d\n",
389 gCode, sCode, errno));
391 if (errno == ENOSYS) {
395 KA_TRACE(30, (
"__kmp_affinity_determine_capable: "
396 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
398 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
399 && (__kmp_affinity_type != affinity_none)
400 && (__kmp_affinity_type != affinity_default)
401 && (__kmp_affinity_type != affinity_disabled))) {
405 KMP_MSG( SetAffSysCallNotSupported, env_var ),
410 KMP_AFFINITY_DISABLE();
411 KMP_INTERNAL_FREE(buf);
414 if (errno == EFAULT) {
415 KMP_AFFINITY_ENABLE(gCode);
416 KA_TRACE(10, (
"__kmp_affinity_determine_capable: "
417 "affinity supported (mask size %d)\n",
418 (
int)__kmp_affin_mask_size));
419 KMP_INTERNAL_FREE(buf);
425 KMP_INTERNAL_FREE(buf);
431 KMP_AFFINITY_DISABLE();
432 KA_TRACE(10, (
"__kmp_affinity_determine_capable: "
433 "cannot determine mask size - affinity not supported\n"));
434 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
435 && (__kmp_affinity_type != affinity_none)
436 && (__kmp_affinity_type != affinity_default)
437 && (__kmp_affinity_type != affinity_disabled))) {
438 KMP_WARNING( AffCantGetMaskSize, env_var );
442 #endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
447 #if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && !KMP_OS_CNK
450 __kmp_futex_determine_capable()
453 int rc = syscall( __NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0 );
454 int retval = ( rc == 0 ) || ( errno != ENOSYS );
456 KA_TRACE(10, (
"__kmp_futex_determine_capable: rc = %d errno = %d\n", rc,
458 KA_TRACE(10, (
"__kmp_futex_determine_capable: futex syscall%s supported\n",
459 retval ?
"" :
" not" ) );
464 #endif // KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM) && !KMP_OS_CNK
469 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS)
476 __kmp_test_then_or8(
volatile kmp_int8 *p, kmp_int8 d )
478 kmp_int8 old_value, new_value;
480 old_value = TCR_1( *p );
481 new_value = old_value | d;
483 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
486 old_value = TCR_1( *p );
487 new_value = old_value | d;
493 __kmp_test_then_and8(
volatile kmp_int8 *p, kmp_int8 d )
495 kmp_int8 old_value, new_value;
497 old_value = TCR_1( *p );
498 new_value = old_value & d;
500 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
503 old_value = TCR_1( *p );
504 new_value = old_value & d;
510 __kmp_test_then_or32(
volatile kmp_int32 *p, kmp_int32 d )
512 kmp_int32 old_value, new_value;
514 old_value = TCR_4( *p );
515 new_value = old_value | d;
517 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
520 old_value = TCR_4( *p );
521 new_value = old_value | d;
527 __kmp_test_then_and32(
volatile kmp_int32 *p, kmp_int32 d )
529 kmp_int32 old_value, new_value;
531 old_value = TCR_4( *p );
532 new_value = old_value & d;
534 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
537 old_value = TCR_4( *p );
538 new_value = old_value & d;
543 # if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64
545 __kmp_test_then_add8(
volatile kmp_int8 *p, kmp_int8 d )
547 kmp_int8 old_value, new_value;
549 old_value = TCR_1( *p );
550 new_value = old_value + d;
552 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
555 old_value = TCR_1( *p );
556 new_value = old_value + d;
562 __kmp_test_then_add64(
volatile kmp_int64 *p, kmp_int64 d )
564 kmp_int64 old_value, new_value;
566 old_value = TCR_8( *p );
567 new_value = old_value + d;
569 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
572 old_value = TCR_8( *p );
573 new_value = old_value + d;
580 __kmp_test_then_or64(
volatile kmp_int64 *p, kmp_int64 d )
582 kmp_int64 old_value, new_value;
584 old_value = TCR_8( *p );
585 new_value = old_value | d;
586 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
589 old_value = TCR_8( *p );
590 new_value = old_value | d;
596 __kmp_test_then_and64(
volatile kmp_int64 *p, kmp_int64 d )
598 kmp_int64 old_value, new_value;
600 old_value = TCR_8( *p );
601 new_value = old_value & d;
602 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
605 old_value = TCR_8( *p );
606 new_value = old_value & d;
614 __kmp_terminate_thread(
int gtid )
617 kmp_info_t *th = __kmp_threads[ gtid ];
621 #ifdef KMP_CANCEL_THREADS
622 KA_TRACE( 10, (
"__kmp_terminate_thread: kill (%d)\n", gtid ) );
623 status = pthread_cancel( th->th.th_info.ds.ds_thread );
624 if ( status != 0 && status != ESRCH ) {
627 KMP_MSG( CantTerminateWorkerThread ),
651 __kmp_set_stack_info(
int gtid, kmp_info_t *th )
654 #if KMP_OS_LINUX || KMP_OS_FREEBSD
664 if ( ! KMP_UBER_GTID(gtid) ) {
667 status = pthread_attr_init( &attr );
668 KMP_CHECK_SYSFAIL(
"pthread_attr_init", status );
670 status = pthread_attr_get_np( pthread_self(), &attr );
671 KMP_CHECK_SYSFAIL(
"pthread_attr_get_np", status );
673 status = pthread_getattr_np( pthread_self(), &attr );
674 KMP_CHECK_SYSFAIL(
"pthread_getattr_np", status );
676 status = pthread_attr_getstack( &attr, &addr, &size );
677 KMP_CHECK_SYSFAIL(
"pthread_attr_getstack", status );
678 KA_TRACE( 60, (
"__kmp_set_stack_info: T#%d pthread_attr_getstack returned size: %lu, "
682 status = pthread_attr_destroy( &attr );
683 KMP_CHECK_SYSFAIL(
"pthread_attr_destroy", status );
686 if ( size != 0 && addr != 0 ) {
688 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((
char *)addr) + size));
689 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
690 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
695 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
696 TCW_PTR(th -> th.th_info.ds.ds_stackbase, &stack_data);
697 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
702 __kmp_launch_worker(
void *thr )
704 int status, old_type, old_state;
705 #ifdef KMP_BLOCK_SIGNALS
706 sigset_t new_set, old_set;
709 #if KMP_OS_LINUX || KMP_OS_FREEBSD
714 gtid = ((kmp_info_t*)thr) -> th.th_info.ds.ds_gtid;
715 __kmp_gtid_set_specific( gtid );
716 #ifdef KMP_TDATA_GTID
719 #if KMP_STATS_ENABLED
721 __kmp_stats_thread_ptr = ((kmp_info_t*)thr)->th.th_stats;
725 __kmp_itt_thread_name( gtid );
728 #if KMP_AFFINITY_SUPPORTED
729 __kmp_affinity_set_init_mask( gtid, FALSE );
732 #ifdef KMP_CANCEL_THREADS
733 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
734 KMP_CHECK_SYSFAIL(
"pthread_setcanceltype", status );
736 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
737 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status );
740 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
745 __kmp_clear_x87_fpu_status_word();
746 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
747 __kmp_load_mxcsr( &__kmp_init_mxcsr );
750 #ifdef KMP_BLOCK_SIGNALS
751 status = sigfillset( & new_set );
752 KMP_CHECK_SYSFAIL_ERRNO(
"sigfillset", status );
753 status = pthread_sigmask( SIG_BLOCK, & new_set, & old_set );
754 KMP_CHECK_SYSFAIL(
"pthread_sigmask", status );
757 #if KMP_OS_LINUX || KMP_OS_FREEBSD
758 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
759 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
764 __kmp_set_stack_info( gtid, (kmp_info_t*)thr );
766 __kmp_check_stack_overlap( (kmp_info_t*)thr );
768 exit_val = __kmp_launch_thread( (kmp_info_t *) thr );
770 #ifdef KMP_BLOCK_SIGNALS
771 status = pthread_sigmask( SIG_SETMASK, & old_set, NULL );
772 KMP_CHECK_SYSFAIL(
"pthread_sigmask", status );
782 __kmp_launch_monitor(
void *thr )
784 int status, old_type, old_state;
785 #ifdef KMP_BLOCK_SIGNALS
788 struct timespec interval;
790 int yield_cycles = 0;
794 KA_TRACE( 10, (
"__kmp_launch_monitor: #1 launched\n" ) );
797 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
798 #ifdef KMP_TDATA_GTID
799 __kmp_gtid = KMP_GTID_MONITOR;
805 __kmp_itt_thread_ignore();
808 __kmp_set_stack_info( ((kmp_info_t*)thr)->th.th_info.ds.ds_gtid, (kmp_info_t*)thr );
810 __kmp_check_stack_overlap( (kmp_info_t*)thr );
812 #ifdef KMP_CANCEL_THREADS
813 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
814 KMP_CHECK_SYSFAIL(
"pthread_setcanceltype", status );
816 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
817 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status );
820 #if KMP_REAL_TIME_FIX
824 int sched = sched_getscheduler( 0 );
825 if ( sched == SCHED_FIFO || sched == SCHED_RR ) {
828 struct sched_param param;
829 int max_priority = sched_get_priority_max( sched );
831 KMP_WARNING( RealTimeSchedNotSupported );
832 sched_getparam( 0, & param );
833 if ( param.sched_priority < max_priority ) {
834 param.sched_priority += 1;
835 rc = sched_setscheduler( 0, sched, & param );
840 KMP_MSG( CantChangeMonitorPriority ),
842 KMP_MSG( MonitorWillStarve ),
851 KMP_MSG( RunningAtMaxPriority ),
852 KMP_MSG( MonitorWillStarve ),
853 KMP_HNT( RunningAtMaxPriority ),
858 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
860 #endif // KMP_REAL_TIME_FIX
864 if ( __kmp_monitor_wakeups == 1 ) {
866 interval.tv_nsec = 0;
869 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
872 KA_TRACE( 10, (
"__kmp_launch_monitor: #2 monitor\n" ) );
874 if (__kmp_yield_cycle) {
875 __kmp_yielding_on = 0;
876 yield_count = __kmp_yield_off_count;
878 __kmp_yielding_on = 1;
881 while( ! TCR_4( __kmp_global.g.g_done ) ) {
887 KA_TRACE( 15, (
"__kmp_launch_monitor: update\n" ) );
889 status = gettimeofday( &tval, NULL );
890 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status );
891 TIMEVAL_TO_TIMESPEC( &tval, &now );
893 now.tv_sec += interval.tv_sec;
894 now.tv_nsec += interval.tv_nsec;
896 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
898 now.tv_nsec -= KMP_NSEC_PER_SEC;
901 status = pthread_mutex_lock( & __kmp_wait_mx.m_mutex );
902 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status );
904 if ( !TCR_4(__kmp_global.g.g_done) ) {
905 status = pthread_cond_timedwait( &__kmp_wait_cv.c_cond, &__kmp_wait_mx.m_mutex, &now );
907 if ( status != ETIMEDOUT && status != EINTR ) {
908 KMP_SYSFAIL(
"pthread_cond_timedwait", status );
912 status = pthread_mutex_unlock( & __kmp_wait_mx.m_mutex );
913 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
915 if (__kmp_yield_cycle) {
917 if ( (yield_cycles % yield_count) == 0 ) {
918 if (__kmp_yielding_on) {
919 __kmp_yielding_on = 0;
920 yield_count = __kmp_yield_off_count;
922 __kmp_yielding_on = 1;
923 yield_count = __kmp_yield_on_count;
928 __kmp_yielding_on = 1;
931 TCW_4( __kmp_global.g.g_time.dt.t_value,
932 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
937 KA_TRACE( 10, (
"__kmp_launch_monitor: #3 cleanup\n" ) );
939 #ifdef KMP_BLOCK_SIGNALS
940 status = sigfillset( & new_set );
941 KMP_CHECK_SYSFAIL_ERRNO(
"sigfillset", status );
942 status = pthread_sigmask( SIG_UNBLOCK, & new_set, NULL );
943 KMP_CHECK_SYSFAIL(
"pthread_sigmask", status );
946 KA_TRACE( 10, (
"__kmp_launch_monitor: #4 finished\n" ) );
948 if( __kmp_global.g.g_abort != 0 ) {
954 KA_TRACE( 10, (
"__kmp_launch_monitor: #5 terminate sig=%d\n", __kmp_global.g.g_abort ) );
959 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
960 __kmp_terminate_thread( gtid );
964 KA_TRACE( 10, (
"__kmp_launch_monitor: #6 raise sig=%d\n", __kmp_global.g.g_abort ) );
966 if (__kmp_global.g.g_abort > 0)
967 raise( __kmp_global.g.g_abort );
971 KA_TRACE( 10, (
"__kmp_launch_monitor: #7 exit\n" ) );
977 __kmp_create_worker(
int gtid, kmp_info_t *th,
size_t stack_size )
980 pthread_attr_t thread_attr;
984 th->th.th_info.ds.ds_gtid = gtid;
986 #if KMP_STATS_ENABLED
988 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
993 th->th.th_stats = __kmp_stats_list.push_back(gtid);
994 if(KMP_UBER_GTID(gtid)) {
995 __kmp_stats_start_time = tsc_tick_count::now();
996 __kmp_stats_thread_ptr = th->th.th_stats;
1001 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
1003 #endif // KMP_STATS_ENABLED
1005 if ( KMP_UBER_GTID(gtid) ) {
1006 KA_TRACE( 10, (
"__kmp_create_worker: uber thread (%d)\n", gtid ) );
1007 th -> th.th_info.ds.ds_thread = pthread_self();
1008 __kmp_set_stack_info( gtid, th );
1009 __kmp_check_stack_overlap( th );
1013 KA_TRACE( 10, (
"__kmp_create_worker: try to create thread (%d)\n", gtid ) );
1017 #ifdef KMP_THREAD_ATTR
1019 status = pthread_attr_init( &thread_attr );
1020 if ( status != 0 ) {
1023 KMP_MSG( CantInitThreadAttrs ),
1028 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1029 if ( status != 0 ) {
1032 KMP_MSG( CantSetWorkerState ),
1039 stack_size += gtid * __kmp_stkoffset;
1041 KA_TRACE( 10, (
"__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
1042 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
1043 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size ) );
1045 # ifdef _POSIX_THREAD_ATTR_STACKSIZE
1046 status = pthread_attr_setstacksize( & thread_attr, stack_size );
1047 # ifdef KMP_BACKUP_STKSIZE
1048 if ( status != 0 ) {
1049 if ( ! __kmp_env_stksize ) {
1050 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
1051 __kmp_stksize = KMP_BACKUP_STKSIZE;
1052 KA_TRACE( 10, (
"__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
1053 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
1055 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size )
1057 status = pthread_attr_setstacksize( &thread_attr, stack_size );
1061 if ( status != 0 ) {
1064 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1066 KMP_HNT( ChangeWorkerStackSize ),
1075 status = pthread_create( & handle, & thread_attr, __kmp_launch_worker, (
void *) th );
1076 if ( status != 0 || ! handle ) {
1077 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1078 if ( status == EINVAL ) {
1081 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1083 KMP_HNT( IncreaseWorkerStackSize ),
1087 if ( status == ENOMEM ) {
1090 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1092 KMP_HNT( DecreaseWorkerStackSize ),
1097 if ( status == EAGAIN ) {
1100 KMP_MSG( NoResourcesForWorkerThread ),
1102 KMP_HNT( Decrease_NUM_THREADS ),
1106 KMP_SYSFAIL(
"pthread_create", status );
1109 th->th.th_info.ds.ds_thread = handle;
1112 #ifdef KMP_THREAD_ATTR
1114 status = pthread_attr_destroy( & thread_attr );
1118 KMP_MSG( CantDestroyThreadAttrs ),
1128 KA_TRACE( 10, (
"__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1134 __kmp_create_monitor( kmp_info_t *th )
1137 pthread_attr_t thread_attr;
1141 int auto_adj_size = FALSE;
1143 caller_gtid = __kmp_get_gtid();
1145 KA_TRACE( 10, (
"__kmp_create_monitor: try to create monitor\n" ) );
1149 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1150 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1151 #if KMP_REAL_TIME_FIX
1152 TCW_4( __kmp_global.g.g_time.dt.t_value, -1 );
1154 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
1155 #endif // KMP_REAL_TIME_FIX
1157 #ifdef KMP_THREAD_ATTR
1158 if ( __kmp_monitor_stksize == 0 ) {
1159 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1160 auto_adj_size = TRUE;
1162 status = pthread_attr_init( &thread_attr );
1163 if ( status != 0 ) {
1166 KMP_MSG( CantInitThreadAttrs ),
1171 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1172 if ( status != 0 ) {
1175 KMP_MSG( CantSetMonitorState ),
1181 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1182 status = pthread_attr_getstacksize( & thread_attr, & size );
1183 KMP_CHECK_SYSFAIL(
"pthread_attr_getstacksize", status );
1185 size = __kmp_sys_min_stksize;
1189 if ( __kmp_monitor_stksize == 0 ) {
1190 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1192 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1193 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1196 KA_TRACE( 10, (
"__kmp_create_monitor: default stacksize = %lu bytes,"
1197 "requested stacksize = %lu bytes\n",
1198 size, __kmp_monitor_stksize ) );
1204 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1205 KA_TRACE( 10, (
"__kmp_create_monitor: setting stacksize = %lu bytes,",
1206 __kmp_monitor_stksize ) );
1207 status = pthread_attr_setstacksize( & thread_attr, __kmp_monitor_stksize );
1208 if ( status != 0 ) {
1209 if ( auto_adj_size ) {
1210 __kmp_monitor_stksize *= 2;
1215 KMP_MSG( CantSetMonitorStackSize, (
long int) __kmp_monitor_stksize ),
1217 KMP_HNT( ChangeMonitorStackSize ),
1223 status = pthread_create( &handle, & thread_attr, __kmp_launch_monitor, (
void *) th );
1225 if ( status != 0 ) {
1226 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1227 if ( status == EINVAL ) {
1228 if ( auto_adj_size && ( __kmp_monitor_stksize < (
size_t)0x40000000 ) ) {
1229 __kmp_monitor_stksize *= 2;
1234 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1236 KMP_HNT( IncreaseMonitorStackSize ),
1240 if ( status == ENOMEM ) {
1243 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1245 KMP_HNT( DecreaseMonitorStackSize ),
1250 if ( status == EAGAIN ) {
1253 KMP_MSG( NoResourcesForMonitorThread ),
1255 KMP_HNT( DecreaseNumberOfThreadsInUse ),
1259 KMP_SYSFAIL(
"pthread_create", status );
1262 th->th.th_info.ds.ds_thread = handle;
1264 #if KMP_REAL_TIME_FIX
1266 KMP_DEBUG_ASSERT(
sizeof( kmp_uint32 ) ==
sizeof( __kmp_global.g.g_time.dt.t_value ) );
1268 (kmp_uint32
volatile *) & __kmp_global.g.g_time.dt.t_value, -1, & __kmp_neq_4, NULL
1270 #endif // KMP_REAL_TIME_FIX
1272 #ifdef KMP_THREAD_ATTR
1273 status = pthread_attr_destroy( & thread_attr );
1274 if ( status != 0 ) {
1277 KMP_MSG( CantDestroyThreadAttrs ),
1286 KA_TRACE( 10, (
"__kmp_create_monitor: monitor created %#.8lx\n", th->th.th_info.ds.ds_thread ) );
1294 pthread_exit( (
void *)(intptr_t) exit_status );
1297 void __kmp_resume_monitor();
1300 __kmp_reap_monitor( kmp_info_t *th )
1305 KA_TRACE( 10, (
"__kmp_reap_monitor: try to reap monitor thread with handle %#.8lx\n",
1306 th->th.th_info.ds.ds_thread ) );
1311 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1312 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
1323 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1324 if (status == ESRCH) {
1326 KA_TRACE( 10, (
"__kmp_reap_monitor: monitor does not exist, returning\n") );
1330 __kmp_resume_monitor();
1331 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1332 if (exit_val != th) {
1335 KMP_MSG( ReapMonitorError ),
1342 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1343 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1345 KA_TRACE( 10, (
"__kmp_reap_monitor: done reaping monitor thread with handle %#.8lx\n",
1346 th->th.th_info.ds.ds_thread ) );
1353 __kmp_reap_worker( kmp_info_t *th )
1360 KA_TRACE( 10, (
"__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid ) );
1367 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1368 if (status == ESRCH) {
1369 KA_TRACE( 10, (
"__kmp_reap_worker: worker T#%d does not exist, returning\n",
1370 th->th.th_info.ds.ds_gtid ) );
1373 KA_TRACE( 10, (
"__kmp_reap_worker: try to join with worker T#%d\n",
1374 th->th.th_info.ds.ds_gtid ) );
1376 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1379 if ( status != 0 ) {
1382 KMP_MSG( ReapWorkerError ),
1387 if ( exit_val != th ) {
1388 KA_TRACE( 10, (
"__kmp_reap_worker: worker T#%d did not reap properly, "
1390 th->th.th_info.ds.ds_gtid, exit_val ) );
1396 KA_TRACE( 10, (
"__kmp_reap_worker: done reaping T#%d\n", th->th.th_info.ds.ds_gtid ) );
1405 #if KMP_HANDLE_SIGNALS
1409 __kmp_null_handler(
int signo )
1416 __kmp_team_handler(
int signo )
1418 if ( __kmp_global.g.g_abort == 0 ) {
1421 __kmp_debug_printf(
"__kmp_team_handler: caught signal = %d\n", signo );
1436 if ( __kmp_debug_buf ) {
1437 __kmp_dump_debug_buffer( );
1440 TCW_4( __kmp_global.g.g_abort, signo );
1442 TCW_4( __kmp_global.g.g_done, TRUE );
1447 __kmp_debug_printf(
"__kmp_team_handler: unknown signal type" );
1456 void __kmp_sigaction(
int signum,
const struct sigaction * act,
struct sigaction * oldact ) {
1457 int rc = sigaction( signum, act, oldact );
1458 KMP_CHECK_SYSFAIL_ERRNO(
"sigaction", rc );
1463 __kmp_install_one_handler(
int sig, sig_func_t handler_func,
int parallel_init )
1466 KB_TRACE( 60, (
"__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init ) );
1467 if ( parallel_init ) {
1468 struct sigaction new_action;
1469 struct sigaction old_action;
1470 new_action.sa_handler = handler_func;
1471 new_action.sa_flags = 0;
1472 sigfillset( & new_action.sa_mask );
1473 __kmp_sigaction( sig, & new_action, & old_action );
1474 if ( old_action.sa_handler == __kmp_sighldrs[ sig ].sa_handler ) {
1475 sigaddset( & __kmp_sigset, sig );
1478 __kmp_sigaction( sig, & old_action, NULL );
1482 __kmp_sigaction( sig, NULL, & __kmp_sighldrs[ sig ] );
1489 __kmp_remove_one_handler(
int sig )
1491 KB_TRACE( 60, (
"__kmp_remove_one_handler( %d )\n", sig ) );
1492 if ( sigismember( & __kmp_sigset, sig ) ) {
1493 struct sigaction old;
1495 __kmp_sigaction( sig, & __kmp_sighldrs[ sig ], & old );
1496 if ( ( old.sa_handler != __kmp_team_handler ) && ( old.sa_handler != __kmp_null_handler ) ) {
1498 KB_TRACE( 10, (
"__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1499 __kmp_sigaction( sig, & old, NULL );
1501 sigdelset( & __kmp_sigset, sig );
1508 __kmp_install_signals(
int parallel_init )
1510 KB_TRACE( 10, (
"__kmp_install_signals( %d )\n", parallel_init ) );
1511 if ( __kmp_handle_signals || ! parallel_init ) {
1514 sigemptyset( & __kmp_sigset );
1515 __kmp_install_one_handler( SIGHUP, __kmp_team_handler, parallel_init );
1516 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1517 __kmp_install_one_handler( SIGQUIT, __kmp_team_handler, parallel_init );
1518 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1519 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1520 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1521 __kmp_install_one_handler( SIGBUS, __kmp_team_handler, parallel_init );
1522 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1524 __kmp_install_one_handler( SIGSYS, __kmp_team_handler, parallel_init );
1526 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1528 __kmp_install_one_handler( SIGPIPE, __kmp_team_handler, parallel_init );
1535 __kmp_remove_signals(
void )
1538 KB_TRACE( 10, (
"__kmp_remove_signals()\n" ) );
1539 for ( sig = 1; sig < NSIG; ++ sig ) {
1540 __kmp_remove_one_handler( sig );
1545 #endif // KMP_HANDLE_SIGNALS
1551 __kmp_enable(
int new_state )
1553 #ifdef KMP_CANCEL_THREADS
1554 int status, old_state;
1555 status = pthread_setcancelstate( new_state, & old_state );
1556 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status );
1557 KMP_DEBUG_ASSERT( old_state == PTHREAD_CANCEL_DISABLE );
1562 __kmp_disable(
int * old_state )
1564 #ifdef KMP_CANCEL_THREADS
1566 status = pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, old_state );
1567 KMP_CHECK_SYSFAIL(
"pthread_setcancelstate", status );
1575 __kmp_atfork_prepare (
void)
1581 __kmp_atfork_parent (
void)
1592 __kmp_atfork_child (
void)
1600 __kmp_init_runtime = FALSE;
1601 __kmp_init_monitor = 0;
1602 __kmp_init_parallel = FALSE;
1603 __kmp_init_middle = FALSE;
1604 __kmp_init_serial = FALSE;
1605 TCW_4(__kmp_init_gtid, FALSE);
1606 __kmp_init_common = FALSE;
1608 TCW_4(__kmp_init_user_locks, FALSE);
1609 #if ! KMP_USE_DYNAMIC_LOCK
1610 __kmp_user_lock_table.used = 1;
1611 __kmp_user_lock_table.allocated = 0;
1612 __kmp_user_lock_table.table = NULL;
1613 __kmp_lock_blocks = NULL;
1617 TCW_4(__kmp_nth, 0);
1621 KA_TRACE( 10, (
"__kmp_atfork_child: checking cache address list %p\n",
1622 __kmp_threadpriv_cache_list ) );
1624 while ( __kmp_threadpriv_cache_list != NULL ) {
1626 if ( *__kmp_threadpriv_cache_list -> addr != NULL ) {
1627 KC_TRACE( 50, (
"__kmp_atfork_child: zeroing cache at address %p\n",
1628 &(*__kmp_threadpriv_cache_list -> addr) ) );
1630 *__kmp_threadpriv_cache_list -> addr = NULL;
1632 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list -> next;
1635 __kmp_init_runtime = FALSE;
1638 __kmp_init_bootstrap_lock( &__kmp_initz_lock );
1639 __kmp_init_bootstrap_lock( &__kmp_stdio_lock );
1640 __kmp_init_bootstrap_lock( &__kmp_console_lock );
1655 __kmp_register_atfork(
void) {
1656 if ( __kmp_need_register_atfork ) {
1657 int status = pthread_atfork( __kmp_atfork_prepare, __kmp_atfork_parent, __kmp_atfork_child );
1658 KMP_CHECK_SYSFAIL(
"pthread_atfork", status );
1659 __kmp_need_register_atfork = FALSE;
1664 __kmp_suspend_initialize(
void )
1667 status = pthread_mutexattr_init( &__kmp_suspend_mutex_attr );
1668 KMP_CHECK_SYSFAIL(
"pthread_mutexattr_init", status );
1669 status = pthread_condattr_init( &__kmp_suspend_cond_attr );
1670 KMP_CHECK_SYSFAIL(
"pthread_condattr_init", status );
1674 __kmp_suspend_initialize_thread( kmp_info_t *th )
1676 if ( th->th.th_suspend_init_count <= __kmp_fork_count ) {
1680 status = pthread_cond_init( &th->th.th_suspend_cv.c_cond, &__kmp_suspend_cond_attr );
1681 KMP_CHECK_SYSFAIL(
"pthread_cond_init", status );
1682 status = pthread_mutex_init( &th->th.th_suspend_mx.m_mutex, & __kmp_suspend_mutex_attr );
1683 KMP_CHECK_SYSFAIL(
"pthread_mutex_init", status );
1684 *(
volatile int*)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1689 __kmp_suspend_uninitialize_thread( kmp_info_t *th )
1691 if(th->th.th_suspend_init_count > __kmp_fork_count) {
1696 status = pthread_cond_destroy( &th->th.th_suspend_cv.c_cond );
1697 if ( status != 0 && status != EBUSY ) {
1698 KMP_SYSFAIL(
"pthread_cond_destroy", status );
1700 status = pthread_mutex_destroy( &th->th.th_suspend_mx.m_mutex );
1701 if ( status != 0 && status != EBUSY ) {
1702 KMP_SYSFAIL(
"pthread_mutex_destroy", status );
1704 --th->th.th_suspend_init_count;
1705 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1713 static inline void __kmp_suspend_template(
int th_gtid, C *flag )
1716 kmp_info_t *th = __kmp_threads[th_gtid];
1718 typename C::flag_t old_spin;
1720 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, flag->get() ) );
1722 __kmp_suspend_initialize_thread( th );
1724 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1725 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status );
1727 KF_TRACE( 10, (
"__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1728 th_gtid, flag->get() ) );
1733 old_spin = flag->set_sleeping();
1735 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%d\n",
1736 th_gtid, flag->get(), *(flag->get()) ) );
1738 if ( flag->done_check_val(old_spin) ) {
1739 old_spin = flag->unset_sleeping();
1740 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d false alarm, reset sleep bit for spin(%p)\n",
1741 th_gtid, flag->get()) );
1747 int deactivated = FALSE;
1748 TCW_PTR(th->th.th_sleep_loc, (
void *)flag);
1749 while ( flag->is_sleeping() ) {
1750 #ifdef DEBUG_SUSPEND
1752 __kmp_suspend_count++;
1753 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
1754 __kmp_printf(
"__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, buffer );
1757 if ( ! deactivated ) {
1758 th->th.th_active = FALSE;
1759 if ( th->th.th_active_in_pool ) {
1760 th->th.th_active_in_pool = FALSE;
1761 KMP_TEST_THEN_DEC32(
1762 (kmp_int32 *) &__kmp_thread_pool_active_nth );
1763 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
1770 #if USE_SUSPEND_TIMEOUT
1771 struct timespec now;
1772 struct timeval tval;
1775 status = gettimeofday( &tval, NULL );
1776 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status );
1777 TIMEVAL_TO_TIMESPEC( &tval, &now );
1779 msecs = (4*__kmp_dflt_blocktime) + 200;
1780 now.tv_sec += msecs / 1000;
1781 now.tv_nsec += (msecs % 1000)*1000;
1783 KF_TRACE( 15, (
"__kmp_suspend_template: T#%d about to perform pthread_cond_timedwait\n",
1785 status = pthread_cond_timedwait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex, & now );
1787 KF_TRACE( 15, (
"__kmp_suspend_template: T#%d about to perform pthread_cond_wait\n",
1790 status = pthread_cond_wait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex );
1793 if ( (status != 0) && (status != EINTR) && (status != ETIMEDOUT) ) {
1794 KMP_SYSFAIL(
"pthread_cond_wait", status );
1797 if (status == ETIMEDOUT) {
1798 if ( flag->is_sleeping() ) {
1799 KF_TRACE( 100, (
"__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid ) );
1801 KF_TRACE( 2, (
"__kmp_suspend_template: T#%d timeout wakeup, sleep bit not set!\n",
1804 }
else if ( flag->is_sleeping() ) {
1805 KF_TRACE( 100, (
"__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ) );
1811 if ( deactivated ) {
1812 th->th.th_active = TRUE;
1813 if ( TCR_4(th->th.th_in_pool) ) {
1814 KMP_TEST_THEN_INC32( (kmp_int32 *) &__kmp_thread_pool_active_nth );
1815 th->th.th_active_in_pool = TRUE;
1820 #ifdef DEBUG_SUSPEND
1823 __kmp_print_cond( buffer, &th->th.th_suspend_cv);
1824 __kmp_printf(
"__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, buffer );
1829 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1830 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1832 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d exit\n", th_gtid ) );
1835 void __kmp_suspend_32(
int th_gtid, kmp_flag_32 *flag) {
1836 __kmp_suspend_template(th_gtid, flag);
1838 void __kmp_suspend_64(
int th_gtid, kmp_flag_64 *flag) {
1839 __kmp_suspend_template(th_gtid, flag);
1841 void __kmp_suspend_oncore(
int th_gtid, kmp_flag_oncore *flag) {
1842 __kmp_suspend_template(th_gtid, flag);
1851 static inline void __kmp_resume_template(
int target_gtid, C *flag )
1853 kmp_info_t *th = __kmp_threads[target_gtid];
1857 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1860 KF_TRACE( 30, (
"__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", gtid, target_gtid ) );
1861 KMP_DEBUG_ASSERT( gtid != target_gtid );
1863 __kmp_suspend_initialize_thread( th );
1865 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1866 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status );
1869 flag = (C *)th->th.th_sleep_loc;
1873 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p)\n",
1874 gtid, target_gtid, NULL ) );
1875 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1876 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1880 typename C::flag_t old_spin = flag->unset_sleeping();
1881 if ( ! flag->is_sleeping_val(old_spin) ) {
1882 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p): "
1884 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
1886 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1887 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1890 KF_TRACE( 5, (
"__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p): "
1892 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
1894 TCW_PTR(th->th.th_sleep_loc, NULL);
1897 #ifdef DEBUG_SUSPEND
1900 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
1901 __kmp_printf(
"__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, target_gtid, buffer );
1906 status = pthread_cond_signal( &th->th.th_suspend_cv.c_cond );
1907 KMP_CHECK_SYSFAIL(
"pthread_cond_signal", status );
1908 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1909 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1910 KF_TRACE( 30, (
"__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
1911 gtid, target_gtid ) );
1914 void __kmp_resume_32(
int target_gtid, kmp_flag_32 *flag) {
1915 __kmp_resume_template(target_gtid, flag);
1917 void __kmp_resume_64(
int target_gtid, kmp_flag_64 *flag) {
1918 __kmp_resume_template(target_gtid, flag);
1920 void __kmp_resume_oncore(
int target_gtid, kmp_flag_oncore *flag) {
1921 __kmp_resume_template(target_gtid, flag);
1925 __kmp_resume_monitor()
1930 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1931 KF_TRACE( 30, (
"__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n",
1932 gtid, KMP_GTID_MONITOR ) );
1933 KMP_DEBUG_ASSERT( gtid != KMP_GTID_MONITOR );
1935 status = pthread_mutex_lock( &__kmp_wait_mx.m_mutex );
1936 KMP_CHECK_SYSFAIL(
"pthread_mutex_lock", status );
1937 #ifdef DEBUG_SUSPEND
1940 __kmp_print_cond( buffer, &__kmp_wait_cv.c_cond );
1941 __kmp_printf(
"__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid, KMP_GTID_MONITOR, buffer );
1944 status = pthread_cond_signal( &__kmp_wait_cv.c_cond );
1945 KMP_CHECK_SYSFAIL(
"pthread_cond_signal", status );
1946 status = pthread_mutex_unlock( &__kmp_wait_mx.m_mutex );
1947 KMP_CHECK_SYSFAIL(
"pthread_mutex_unlock", status );
1948 KF_TRACE( 30, (
"__kmp_resume_monitor: T#%d exiting after signaling wake up for T#%d\n",
1949 gtid, KMP_GTID_MONITOR ) );
1956 __kmp_yield(
int cond )
1958 if (cond && __kmp_yielding_on) {
1967 __kmp_gtid_set_specific(
int gtid )
1970 KMP_ASSERT( __kmp_init_runtime );
1971 status = pthread_setspecific( __kmp_gtid_threadprivate_key, (
void*)(intptr_t)(gtid+1) );
1972 KMP_CHECK_SYSFAIL(
"pthread_setspecific", status );
1976 __kmp_gtid_get_specific()
1979 if ( !__kmp_init_runtime ) {
1980 KA_TRACE( 50, (
"__kmp_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
1981 return KMP_GTID_SHUTDOWN;
1983 gtid = (int)(
size_t)pthread_getspecific( __kmp_gtid_threadprivate_key );
1985 gtid = KMP_GTID_DNE;
1990 KA_TRACE( 50, (
"__kmp_gtid_get_specific: key:%d gtid:%d\n",
1991 __kmp_gtid_threadprivate_key, gtid ));
1999 __kmp_read_cpu_time(
void )
2006 return (buffer.tms_utime + buffer.tms_cutime) / (double) CLOCKS_PER_SEC;
2010 __kmp_read_system_info(
struct kmp_sys_info *info )
2013 struct rusage r_usage;
2015 memset( info, 0,
sizeof( *info ) );
2017 status = getrusage( RUSAGE_SELF, &r_usage);
2018 KMP_CHECK_SYSFAIL_ERRNO(
"getrusage", status );
2020 info->maxrss = r_usage.ru_maxrss;
2021 info->minflt = r_usage.ru_minflt;
2022 info->majflt = r_usage.ru_majflt;
2023 info->nswap = r_usage.ru_nswap;
2024 info->inblock = r_usage.ru_inblock;
2025 info->oublock = r_usage.ru_oublock;
2026 info->nvcsw = r_usage.ru_nvcsw;
2027 info->nivcsw = r_usage.ru_nivcsw;
2029 return (status != 0);
2037 __kmp_read_system_time(
double *delta )
2040 struct timeval tval;
2041 struct timespec stop;
2044 status = gettimeofday( &tval, NULL );
2045 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status );
2046 TIMEVAL_TO_TIMESPEC( &tval, &stop );
2047 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
2048 *delta = (t_ns * 1e-9);
2052 __kmp_clear_system_time(
void )
2054 struct timeval tval;
2056 status = gettimeofday( &tval, NULL );
2057 KMP_CHECK_SYSFAIL_ERRNO(
"gettimeofday", status );
2058 TIMEVAL_TO_TIMESPEC( &tval, &__kmp_sys_timer_data.start );
2067 __kmp_tv_threadprivate_store( kmp_info_t *th,
void *global_addr,
void *thread_addr )
2071 p = (
struct tv_data *) __kmp_allocate(
sizeof( *p ) );
2073 p->u.tp.global_addr = global_addr;
2074 p->u.tp.thread_addr = thread_addr;
2076 p->type = (
void *) 1;
2078 p->next = th->th.th_local.tv_data;
2079 th->th.th_local.tv_data = p;
2081 if ( p->next == 0 ) {
2082 int rc = pthread_setspecific( __kmp_tv_key, p );
2083 KMP_CHECK_SYSFAIL(
"pthread_setspecific", rc );
2093 __kmp_get_xproc(
void ) {
2099 r = sysconf( _SC_NPROCESSORS_ONLN );
2107 host_basic_info_data_t info;
2108 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
2109 rc = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) & info, & num );
2110 if ( rc == 0 && num == HOST_BASIC_INFO_COUNT ) {
2113 r = info.avail_cpus;
2115 KMP_WARNING( CantGetNumAvailCPU );
2116 KMP_INFORM( AssumedNumCPU );
2119 #elif KMP_OS_FREEBSD
2121 int mib[] = { CTL_HW, HW_NCPU };
2122 size_t len =
sizeof( r );
2123 if ( sysctl( mib, 2, &r, &len, NULL, 0 ) < 0 ) {
2125 KMP_WARNING( CantGetNumAvailCPU );
2126 KMP_INFORM( AssumedNumCPU );
2131 #error "Unknown or unsupported OS."
2135 return r > 0 ? r : 2;
2140 __kmp_read_from_file(
char const *path,
char const *format, ... )
2145 va_start(args, format);
2146 FILE *f = fopen(path,
"rb");
2149 result = vfscanf(f, format, args);
2156 __kmp_runtime_initialize(
void )
2159 pthread_mutexattr_t mutex_attr;
2160 pthread_condattr_t cond_attr;
2162 if ( __kmp_init_runtime ) {
2166 #if ( KMP_ARCH_X86 || KMP_ARCH_X86_64 )
2167 if ( ! __kmp_cpuinfo.initialized ) {
2168 __kmp_query_cpuid( &__kmp_cpuinfo );
2172 __kmp_xproc = __kmp_get_xproc();
2174 if ( sysconf( _SC_THREADS ) ) {
2177 __kmp_sys_max_nth = sysconf( _SC_THREAD_THREADS_MAX );
2178 if ( __kmp_sys_max_nth == -1 ) {
2180 __kmp_sys_max_nth = INT_MAX;
2182 else if ( __kmp_sys_max_nth <= 1 ) {
2184 __kmp_sys_max_nth = KMP_MAX_NTH;
2188 __kmp_sys_min_stksize = sysconf( _SC_THREAD_STACK_MIN );
2189 if ( __kmp_sys_min_stksize <= 1 ) {
2190 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
2195 __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 ) {
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
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
2642 #if KMP_COMPILER_GCC && !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64)
2644 int __kmp_invoke_microtask( microtask_t pkfn,
int gtid,
int tid,
int argc,
2647 ,
void **exit_frame_ptr
2651 int argc_full = argc + 2;
2654 ffi_type *types[argc_full];
2655 void *args[argc_full];
2659 *exit_frame_ptr = __builtin_frame_address(0);
2662 for (i = 0; i < argc_full; i++)
2663 types[i] = &ffi_type_pointer;
2671 for (i = 0; i < argc; i++)
2672 args[2 + i] = &p_argv[i];
2674 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argc_full,
2675 &ffi_type_void, types) != FFI_OK)
2678 ffi_call(&cif, (
void (*)(
void))pkfn, NULL, args);
2681 *exit_frame_ptr = 0;
2687 #endif // KMP_COMPILER_GCC && !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_PPC64)
2689 #if KMP_ARCH_PPC64 || KMP_ARCH_AARCH64
2694 __kmp_invoke_microtask( microtask_t pkfn,
2696 int argc,
void *p_argv[]
2698 ,
void **exit_frame_ptr
2703 *exit_frame_ptr = __builtin_frame_address(0);
2708 fprintf(stderr,
"Too many args to microtask: %d!\n", argc);
2712 (*pkfn)(>id, &tid);
2715 (*pkfn)(>id, &tid, p_argv[0]);
2718 (*pkfn)(>id, &tid, p_argv[0], p_argv[1]);
2721 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2]);
2724 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2727 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2730 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2734 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2735 p_argv[5], p_argv[6]);
2738 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2739 p_argv[5], p_argv[6], p_argv[7]);
2742 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2743 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2746 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2747 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2750 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2751 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2754 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2755 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2759 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2760 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2761 p_argv[11], p_argv[12]);
2764 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2765 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2766 p_argv[11], p_argv[12], p_argv[13]);
2769 (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2770 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2771 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2776 *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_TIME_BLOCK(name)
Uses specified timer (name) to time code block.