20 #include "kmp_wait_release.h"
34 enum SYSTEM_INFORMATION_CLASS {
35 SystemProcessInformation = 5
55 SIZE_T PeakVirtualSize;
58 SIZE_T PeakWorkingSetSize;
59 SIZE_T WorkingSetSize;
60 SIZE_T QuotaPeakPagedPoolUsage;
61 SIZE_T QuotaPagedPoolUsage;
62 SIZE_T QuotaPeakNonPagedPoolUsage;
63 SIZE_T QuotaNonPagedPoolUsage;
65 SIZE_T PeakPagefileUsage;
66 SIZE_T PrivatePageCount;
69 struct SYSTEM_THREAD {
70 LARGE_INTEGER KernelTime;
71 LARGE_INTEGER UserTime;
72 LARGE_INTEGER CreateTime;
78 ULONG ContextSwitchCount;
83 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, KernelTime ) == 0 );
85 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, StartAddress ) == 28 );
86 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, State ) == 52 );
88 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, StartAddress ) == 32 );
89 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, State ) == 68 );
92 struct SYSTEM_PROCESS_INFORMATION {
93 ULONG NextEntryOffset;
94 ULONG NumberOfThreads;
95 LARGE_INTEGER Reserved[ 3 ];
96 LARGE_INTEGER CreateTime;
97 LARGE_INTEGER UserTime;
98 LARGE_INTEGER KernelTime;
99 UNICODE_STRING ImageName;
102 HANDLE ParentProcessId;
104 ULONG Reserved2[ 2 ];
105 VM_COUNTERS VMCounters;
106 IO_COUNTERS IOCounters;
107 SYSTEM_THREAD Threads[ 1 ];
109 typedef SYSTEM_PROCESS_INFORMATION * PSYSTEM_PROCESS_INFORMATION;
111 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, NextEntryOffset ) == 0 );
112 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, CreateTime ) == 32 );
113 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, ImageName ) == 56 );
115 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, ProcessId ) == 68 );
116 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, HandleCount ) == 76 );
117 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, VMCounters ) == 88 );
118 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, IOCounters ) == 136 );
119 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, Threads ) == 184 );
121 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, ProcessId ) == 80 );
122 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, HandleCount ) == 96 );
123 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, VMCounters ) == 112 );
124 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, IOCounters ) == 208 );
125 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, Threads ) == 256 );
128 typedef NTSTATUS (NTAPI *NtQuerySystemInformation_t)( SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG );
129 NtQuerySystemInformation_t NtQuerySystemInformation = NULL;
131 HMODULE ntdll = NULL;
135 #if KMP_GROUP_AFFINITY
136 static HMODULE kernel32 = NULL;
142 #if KMP_HANDLE_SIGNALS
143 typedef void (* sig_func_t )( int );
144 static sig_func_t __kmp_sighldrs[ NSIG ];
145 static int __kmp_siginstalled[ NSIG ];
148 static HANDLE __kmp_monitor_ev;
149 static kmp_int64 __kmp_win32_time;
150 double __kmp_win32_tick;
152 int __kmp_init_runtime = FALSE;
153 CRITICAL_SECTION __kmp_win32_section;
156 __kmp_win32_mutex_init( kmp_win32_mutex_t *mx )
158 InitializeCriticalSection( & mx->cs );
160 __kmp_itt_system_object_created( & mx->cs,
"Critical Section" );
165 __kmp_win32_mutex_destroy( kmp_win32_mutex_t *mx )
167 DeleteCriticalSection( & mx->cs );
171 __kmp_win32_mutex_lock( kmp_win32_mutex_t *mx )
173 EnterCriticalSection( & mx->cs );
177 __kmp_win32_mutex_unlock( kmp_win32_mutex_t *mx )
179 LeaveCriticalSection( & mx->cs );
183 __kmp_win32_cond_init( kmp_win32_cond_t *cv )
185 cv->waiters_count_ = 0;
186 cv->wait_generation_count_ = 0;
187 cv->release_count_ = 0;
190 __kmp_win32_mutex_init( & cv->waiters_count_lock_ );
193 cv->event_ = CreateEvent( NULL,
198 __kmp_itt_system_object_created( cv->event_,
"Event" );
203 __kmp_win32_cond_destroy( kmp_win32_cond_t *cv )
205 __kmp_win32_mutex_destroy( & cv->waiters_count_lock_ );
206 __kmp_free_handle( cv->event_ );
207 memset( cv,
'\0',
sizeof( *cv ) );
214 __kmp_win32_cond_wait( kmp_win32_cond_t *cv, kmp_win32_mutex_t *mx, kmp_info_t *th,
int need_decrease_load )
220 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
223 cv->waiters_count_++;
226 my_generation = cv->wait_generation_count_;
228 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_ );
229 __kmp_win32_mutex_unlock( mx );
236 WaitForSingleObject( cv->event_, INFINITE );
238 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
243 wait_done = ( cv->release_count_ > 0 ) &&
244 ( cv->wait_generation_count_ != my_generation );
246 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_);
254 __kmp_win32_mutex_lock( mx );
255 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
257 cv->waiters_count_--;
258 cv->release_count_--;
260 last_waiter = ( cv->release_count_ == 0 );
262 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_ );
266 ResetEvent( cv->event_ );
271 __kmp_win32_cond_broadcast( kmp_win32_cond_t *cv )
273 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
275 if( cv->waiters_count_ > 0 ) {
276 SetEvent( cv->event_ );
279 cv->release_count_ = cv->waiters_count_;
282 cv->wait_generation_count_++;
285 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_ );
289 __kmp_win32_cond_signal( kmp_win32_cond_t *cv )
291 __kmp_win32_cond_broadcast( cv );
298 __kmp_enable(
int new_state )
300 if (__kmp_init_runtime)
301 LeaveCriticalSection( & __kmp_win32_section );
305 __kmp_disable(
int *old_state )
309 if (__kmp_init_runtime)
310 EnterCriticalSection( & __kmp_win32_section );
314 __kmp_suspend_initialize(
void )
320 __kmp_suspend_initialize_thread( kmp_info_t *th )
322 if ( ! TCR_4( th->th.th_suspend_init ) ) {
325 __kmp_win32_cond_init( &th->th.th_suspend_cv );
326 __kmp_win32_mutex_init( &th->th.th_suspend_mx );
327 TCW_4( th->th.th_suspend_init, TRUE );
332 __kmp_suspend_uninitialize_thread( kmp_info_t *th )
334 if ( TCR_4( th->th.th_suspend_init ) ) {
337 __kmp_win32_cond_destroy( & th->th.th_suspend_cv );
338 __kmp_win32_mutex_destroy( & th->th.th_suspend_mx );
339 TCW_4( th->th.th_suspend_init, FALSE );
347 static inline void __kmp_suspend_template(
int th_gtid, C *flag )
349 kmp_info_t *th = __kmp_threads[th_gtid];
351 typename C::flag_t old_spin;
353 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d enter for flag's loc(%p)\n", th_gtid, flag->get() ) );
355 __kmp_suspend_initialize_thread( th );
356 __kmp_win32_mutex_lock( &th->th.th_suspend_mx );
358 KF_TRACE( 10, (
"__kmp_suspend_template: T#%d setting sleep bit for flag's loc(%p)\n",
359 th_gtid, flag->get() ) );
364 old_spin = flag->set_sleeping();
366 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d set sleep bit for flag's loc(%p)==%d\n",
367 th_gtid, flag->get(), *(flag->get()) ) );
369 if ( flag->done_check_val(old_spin) ) {
370 old_spin = flag->unset_sleeping();
371 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d false alarm, reset sleep bit for flag's loc(%p)\n",
372 th_gtid, flag->get()) );
375 __kmp_suspend_count++;
381 int deactivated = FALSE;
382 TCW_PTR(th->th.th_sleep_loc, (
void *)flag);
383 while ( flag->is_sleeping() ) {
384 KF_TRACE( 15, (
"__kmp_suspend_template: T#%d about to perform kmp_win32_cond_wait()\n",
387 if ( ! deactivated ) {
388 th->th.th_active = FALSE;
389 if ( th->th.th_active_in_pool ) {
390 th->th.th_active_in_pool = FALSE;
392 (kmp_int32 *) &__kmp_thread_pool_active_nth );
393 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
398 __kmp_win32_cond_wait( &th->th.th_suspend_cv, &th->th.th_suspend_mx, 0, 0 );
401 __kmp_win32_cond_wait( &th->th.th_suspend_cv, &th->th.th_suspend_mx, 0, 0 );
405 if( flag->is_sleeping() ) {
406 KF_TRACE( 100, (
"__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ));
414 th->th.th_active = TRUE;
415 if ( TCR_4(th->th.th_in_pool) ) {
417 (kmp_int32 *) &__kmp_thread_pool_active_nth );
418 th->th.th_active_in_pool = TRUE;
424 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
426 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d exit\n", th_gtid ) );
429 void __kmp_suspend_32(
int th_gtid, kmp_flag_32 *flag) {
430 __kmp_suspend_template(th_gtid, flag);
432 void __kmp_suspend_64(
int th_gtid, kmp_flag_64 *flag) {
433 __kmp_suspend_template(th_gtid, flag);
435 void __kmp_suspend_oncore(
int th_gtid, kmp_flag_oncore *flag) {
436 __kmp_suspend_template(th_gtid, flag);
444 static inline void __kmp_resume_template(
int target_gtid, C *flag )
446 kmp_info_t *th = __kmp_threads[target_gtid];
450 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
453 KF_TRACE( 30, (
"__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", gtid, target_gtid ) );
455 __kmp_suspend_initialize_thread( th );
456 __kmp_win32_mutex_lock( &th->th.th_suspend_mx );
459 flag = (C *)th->th.th_sleep_loc;
463 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p)\n",
464 gtid, target_gtid, NULL ) );
465 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
469 typename C::flag_t old_spin = flag->unset_sleeping();
470 if ( !flag->is_sleeping_val(old_spin) ) {
471 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p): "
473 gtid, target_gtid, flag->get(), old_spin, *(flag->get()) ) );
474 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
478 TCW_PTR(th->th.th_sleep_loc, NULL);
480 KF_TRACE( 5, (
"__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p)\n",
481 gtid, target_gtid, flag->get() ) );
484 __kmp_win32_cond_signal( &th->th.th_suspend_cv );
485 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
487 KF_TRACE( 30, (
"__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
488 gtid, target_gtid ) );
491 void __kmp_resume_32(
int target_gtid, kmp_flag_32 *flag) {
492 __kmp_resume_template(target_gtid, flag);
494 void __kmp_resume_64(
int target_gtid, kmp_flag_64 *flag) {
495 __kmp_resume_template(target_gtid, flag);
497 void __kmp_resume_oncore(
int target_gtid, kmp_flag_oncore *flag) {
498 __kmp_resume_template(target_gtid, flag);
506 __kmp_yield(
int cond )
516 __kmp_gtid_set_specific(
int gtid )
518 KA_TRACE( 50, (
"__kmp_gtid_set_specific: T#%d key:%d\n",
519 gtid, __kmp_gtid_threadprivate_key ));
520 KMP_ASSERT( __kmp_init_runtime );
521 if( ! TlsSetValue( __kmp_gtid_threadprivate_key, (LPVOID)(gtid+1)) )
522 KMP_FATAL( TLSSetValueFailed );
526 __kmp_gtid_get_specific()
529 if( !__kmp_init_runtime ) {
530 KA_TRACE( 50, (
"__kmp_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
531 return KMP_GTID_SHUTDOWN;
533 gtid = (int)(kmp_intptr_t)TlsGetValue( __kmp_gtid_threadprivate_key );
540 KA_TRACE( 50, (
"__kmp_gtid_get_specific: key:%d gtid:%d\n",
541 __kmp_gtid_threadprivate_key, gtid ));
548 #if KMP_GROUP_AFFINITY
555 __kmp_get_proc_group( kmp_affin_mask_t
const *mask )
559 for (i = 0; i < __kmp_num_proc_groups; i++) {
574 __kmp_set_system_affinity( kmp_affin_mask_t
const *mask,
int abort_on_error )
577 #if KMP_GROUP_AFFINITY
579 if (__kmp_num_proc_groups > 1) {
584 int group = __kmp_get_proc_group( mask );
586 if (abort_on_error) {
587 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
597 ga.Mask = mask[group];
598 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
600 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
601 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
602 DWORD error = GetLastError();
603 if (abort_on_error) {
606 KMP_MSG( CantSetThreadAffMask ),
619 if (!SetThreadAffinityMask( GetCurrentThread(), *mask )) {
620 DWORD error = GetLastError();
621 if (abort_on_error) {
624 KMP_MSG( CantSetThreadAffMask ),
636 __kmp_get_system_affinity( kmp_affin_mask_t *mask,
int abort_on_error )
639 #if KMP_GROUP_AFFINITY
641 if (__kmp_num_proc_groups > 1) {
644 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
646 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
647 DWORD error = GetLastError();
648 if (abort_on_error) {
651 KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
659 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups)
664 mask[ga.Group] = ga.Mask;
671 kmp_affin_mask_t newMask, sysMask, retval;
673 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
674 DWORD error = GetLastError();
675 if (abort_on_error) {
678 KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
685 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
687 DWORD error = GetLastError();
688 if (abort_on_error) {
691 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
698 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
700 DWORD error = GetLastError();
701 if (abort_on_error) {
704 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
716 __kmp_affinity_bind_thread(
int proc )
719 #if KMP_GROUP_AFFINITY
721 if (__kmp_num_proc_groups > 1) {
727 KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups
728 * CHAR_BIT *
sizeof(DWORD_PTR))));
729 ga.Group = proc / (CHAR_BIT *
sizeof(DWORD_PTR));
730 ga.Mask = (
unsigned long long)1 << (proc % (CHAR_BIT *
sizeof(DWORD_PTR)));
731 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
733 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
734 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
735 DWORD error = GetLastError();
736 if (__kmp_affinity_verbose) {
739 KMP_MSG( CantSetThreadAffMask ),
751 kmp_affin_mask_t mask;
753 KMP_CPU_SET(proc, &mask);
754 __kmp_set_system_affinity(&mask, TRUE);
759 __kmp_affinity_determine_capable(
const char *env_var )
765 #if KMP_GROUP_AFFINITY
766 KMP_AFFINITY_ENABLE(__kmp_num_proc_groups*
sizeof(kmp_affin_mask_t));
768 KMP_AFFINITY_ENABLE(
sizeof(kmp_affin_mask_t));
772 "__kmp_affinity_determine_capable: "
773 "Windows* OS affinity interface functional (mask size = %" KMP_SIZE_T_SPEC
").\n",
774 __kmp_affin_mask_size
779 __kmp_read_cpu_time(
void )
781 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
787 status = GetProcessTimes( GetCurrentProcess(), &CreationTime,
788 &ExitTime, &KernelTime, &UserTime );
793 sec += KernelTime.dwHighDateTime;
794 sec += UserTime.dwHighDateTime;
797 sec *= (double) (1 << 16) * (double) (1 << 16);
799 sec += KernelTime.dwLowDateTime;
800 sec += UserTime.dwLowDateTime;
802 cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC;
809 __kmp_read_system_info(
struct kmp_sys_info *info )
828 __kmp_runtime_initialize(
void )
834 if ( __kmp_init_runtime ) {
842 UINT err_mode = SetErrorMode (SEM_FAILCRITICALERRORS);
844 BOOL ret = GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
845 |GET_MODULE_HANDLE_EX_FLAG_PIN,
846 (LPCTSTR)&__kmp_serial_initialize, &h);
847 KMP_DEBUG_ASSERT2(h && ret,
"OpenMP RTL cannot find itself loaded");
848 SetErrorMode (err_mode);
849 KA_TRACE( 10, (
"__kmp_runtime_initialize: dynamic library pinned\n") );
853 InitializeCriticalSection( & __kmp_win32_section );
855 __kmp_itt_system_object_created( & __kmp_win32_section,
"Critical Section" );
857 __kmp_initialize_system_tick();
859 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
860 if ( ! __kmp_cpuinfo.initialized ) {
861 __kmp_query_cpuid( & __kmp_cpuinfo );
866 #if KMP_OS_WINDOWS && ! defined KMP_DYNAMIC_LIB
881 __kmp_tls_gtid_min = 0;
883 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
887 if ( !__kmp_gtid_threadprivate_key ) {
888 __kmp_gtid_threadprivate_key = TlsAlloc();
889 if( __kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES ) {
890 KMP_FATAL( TLSOutOfIndexes );
905 __kmp_str_buf_init( & path );
906 path_size = GetSystemDirectory( path.str, path.size );
907 KMP_DEBUG_ASSERT( path_size > 0 );
908 if ( path_size >= path.size ) {
912 __kmp_str_buf_reserve( & path, path_size );
913 path_size = GetSystemDirectory( path.str, path.size );
914 KMP_DEBUG_ASSERT( path_size > 0 );
916 if ( path_size > 0 && path_size < path.size ) {
921 path.used = path_size;
922 __kmp_str_buf_print( & path,
"\\%s",
"ntdll.dll" );
927 ntdll = GetModuleHandle( path.str );
930 KMP_DEBUG_ASSERT( ntdll != NULL );
931 if ( ntdll != NULL ) {
932 NtQuerySystemInformation = (NtQuerySystemInformation_t) GetProcAddress( ntdll,
"NtQuerySystemInformation" );
934 KMP_DEBUG_ASSERT( NtQuerySystemInformation != NULL );
936 #if KMP_GROUP_AFFINITY
941 if ( path_size > 0 && path_size < path.size ) {
946 path.used = path_size;
947 __kmp_str_buf_print( & path,
"\\%s",
"kernel32.dll" );
952 kernel32 = GetModuleHandle( path.str );
953 KA_TRACE( 10, (
"__kmp_runtime_initialize: kernel32.dll = %s\n", path.str ) );
959 if ( kernel32 != NULL ) {
960 __kmp_GetActiveProcessorCount = (kmp_GetActiveProcessorCount_t) GetProcAddress( kernel32,
"GetActiveProcessorCount" );
961 __kmp_GetActiveProcessorGroupCount = (kmp_GetActiveProcessorGroupCount_t) GetProcAddress( kernel32,
"GetActiveProcessorGroupCount" );
962 __kmp_GetThreadGroupAffinity = (kmp_GetThreadGroupAffinity_t) GetProcAddress( kernel32,
"GetThreadGroupAffinity" );
963 __kmp_SetThreadGroupAffinity = (kmp_SetThreadGroupAffinity_t) GetProcAddress( kernel32,
"SetThreadGroupAffinity" );
965 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorCount = %p\n", __kmp_GetActiveProcessorCount ) );
966 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorGroupCount = %p\n", __kmp_GetActiveProcessorGroupCount ) );
967 KA_TRACE( 10, (
"__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity = %p\n", __kmp_GetThreadGroupAffinity ) );
968 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity = %p\n", __kmp_SetThreadGroupAffinity ) );
969 KA_TRACE( 10, (
"__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
sizeof(kmp_affin_mask_t) ) );
978 if ( ( __kmp_GetActiveProcessorCount != NULL )
979 && ( __kmp_GetActiveProcessorGroupCount != NULL )
980 && ( __kmp_GetThreadGroupAffinity != NULL )
981 && ( __kmp_SetThreadGroupAffinity != NULL )
982 && ( ( __kmp_num_proc_groups
983 = __kmp_GetActiveProcessorGroupCount() ) > 1 ) ) {
989 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
993 for ( i = 0; i < __kmp_num_proc_groups; i++ ) {
994 DWORD size = __kmp_GetActiveProcessorCount( i );
996 KA_TRACE( 10, (
"__kmp_runtime_initialize: proc group %d size = %d\n", i, size ) );
1000 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
1004 if ( __kmp_num_proc_groups <= 1 ) {
1005 GetSystemInfo( & info );
1006 __kmp_xproc = info.dwNumberOfProcessors;
1009 GetSystemInfo( & info );
1010 __kmp_xproc = info.dwNumberOfProcessors;
1017 if ( __kmp_xproc <= 0 ) {
1021 KA_TRACE( 5, (
"__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc) );
1023 __kmp_str_buf_free( & path );
1026 __kmp_itt_initialize();
1029 __kmp_init_runtime = TRUE;
1033 __kmp_runtime_destroy(
void )
1035 if ( ! __kmp_init_runtime ) {
1040 __kmp_itt_destroy();
1045 KA_TRACE( 40, (
"__kmp_runtime_destroy\n" ));
1047 if( __kmp_gtid_threadprivate_key ) {
1048 TlsFree( __kmp_gtid_threadprivate_key );
1049 __kmp_gtid_threadprivate_key = 0;
1052 __kmp_affinity_uninitialize();
1053 DeleteCriticalSection( & __kmp_win32_section );
1056 NtQuerySystemInformation = NULL;
1060 __kmp_GetActiveProcessorCount = NULL;
1061 __kmp_GetActiveProcessorGroupCount = NULL;
1062 __kmp_GetThreadGroupAffinity = NULL;
1063 __kmp_SetThreadGroupAffinity = NULL;
1064 #endif // KMP_ARCH_X86_64
1066 __kmp_init_runtime = FALSE;
1071 __kmp_terminate_thread(
int gtid )
1073 kmp_info_t *th = __kmp_threads[ gtid ];
1077 KA_TRACE( 10, (
"__kmp_terminate_thread: kill (%d)\n", gtid ) );
1079 if (TerminateThread( th->th.th_info.ds.ds_thread, (DWORD) -1) == FALSE) {
1082 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1089 __kmp_clear_system_time(
void )
1093 status = QueryPerformanceCounter( & time );
1094 __kmp_win32_time = (kmp_int64) time.QuadPart;
1098 __kmp_initialize_system_tick(
void )
1104 status = QueryPerformanceFrequency( & freq );
1106 DWORD error = GetLastError();
1109 KMP_MSG( FunctionError,
"QueryPerformanceFrequency()" ),
1116 __kmp_win32_tick = ((double) 1.0) / (double) freq.QuadPart;
1124 __kmp_elapsed(
double *t )
1128 status = QueryPerformanceCounter( & now );
1129 *t = ((double) now.QuadPart) * __kmp_win32_tick;
1135 __kmp_elapsed_tick(
double *t )
1137 *t = __kmp_win32_tick;
1141 __kmp_read_system_time(
double *delta )
1144 if (delta != NULL) {
1148 status = QueryPerformanceCounter( & now );
1150 *delta = ((double) (((kmp_int64) now.QuadPart) - __kmp_win32_time))
1159 __kmp_launch_worker(
void *arg )
1161 volatile void *stack_data;
1164 kmp_info_t *this_thr = (kmp_info_t *) arg;
1167 gtid = this_thr->th.th_info.ds.ds_gtid;
1168 __kmp_gtid_set_specific( gtid );
1169 #ifdef KMP_TDATA_GTID
1170 #error "This define causes problems with LoadLibrary() + declspec(thread) " \
1171 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
1172 "reference: http://support.microsoft.com/kb/118816"
1177 __kmp_itt_thread_name( gtid );
1180 __kmp_affinity_set_init_mask( gtid, FALSE );
1182 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1187 __kmp_clear_x87_fpu_status_word();
1188 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
1189 __kmp_load_mxcsr( &__kmp_init_mxcsr );
1192 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
1193 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
1196 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1197 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1198 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1200 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1201 TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
1202 KMP_ASSERT( this_thr -> th.th_info.ds.ds_stackgrow == FALSE );
1203 __kmp_check_stack_overlap( this_thr );
1206 exit_val = __kmp_launch_thread( this_thr );
1207 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1208 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1217 __kmp_launch_monitor(
void *arg )
1220 kmp_thread_t monitor;
1223 kmp_info_t *this_thr = (kmp_info_t *) arg;
1225 KMP_DEBUG_ASSERT(__kmp_init_monitor);
1226 TCW_4( __kmp_init_monitor, 2 );
1228 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1229 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1232 KA_TRACE( 10, (
"__kmp_launch_monitor: launched\n" ) );
1234 monitor = GetCurrentThread();
1237 status = SetThreadPriority( monitor, THREAD_PRIORITY_HIGHEST );
1239 DWORD error = GetLastError();
1242 KMP_MSG( CantSetThreadPriority ),
1249 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
1250 #ifdef KMP_TDATA_GTID
1251 #error "This define causes problems with LoadLibrary() + declspec(thread) " \
1252 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
1253 "reference: http://support.microsoft.com/kb/118816"
1258 __kmp_itt_thread_ignore();
1263 interval = ( 1000 / __kmp_monitor_wakeups );
1265 while (! TCR_4(__kmp_global.g.g_done)) {
1268 KA_TRACE( 15, (
"__kmp_launch_monitor: update\n" ) );
1270 wait_status = WaitForSingleObject( __kmp_monitor_ev, interval );
1272 if (wait_status == WAIT_TIMEOUT) {
1273 TCW_4( __kmp_global.g.g_time.dt.t_value,
1274 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
1280 KA_TRACE( 10, (
"__kmp_launch_monitor: finished\n" ) );
1282 status = SetThreadPriority( monitor, THREAD_PRIORITY_NORMAL );
1284 DWORD error = GetLastError();
1287 KMP_MSG( CantSetThreadPriority ),
1293 if (__kmp_global.g.g_abort != 0) {
1299 KA_TRACE( 10, (
"__kmp_launch_monitor: terminate sig=%d\n", (__kmp_global.g.g_abort) ) );
1304 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1305 __kmp_terminate_thread( gtid );
1311 KA_TRACE( 10, (
"__kmp_launch_monitor: raise sig=%d\n", (__kmp_global.g.g_abort) ) );
1313 if (__kmp_global.g.g_abort > 0) {
1314 raise( __kmp_global.g.g_abort );
1318 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1325 __kmp_create_worker(
int gtid, kmp_info_t *th,
size_t stack_size )
1327 kmp_thread_t handle;
1330 KA_TRACE( 10, (
"__kmp_create_worker: try to create thread (%d)\n", gtid ) );
1332 th->th.th_info.ds.ds_gtid = gtid;
1334 if ( KMP_UBER_GTID(gtid) ) {
1343 rc = DuplicateHandle(
1344 GetCurrentProcess(),
1346 GetCurrentProcess(),
1347 &th->th.th_info.ds.ds_thread,
1350 DUPLICATE_SAME_ACCESS
1353 KA_TRACE( 10, (
" __kmp_create_worker: ROOT Handle duplicated, th = %p, handle = %" KMP_UINTPTR_SPEC
"\n",
1355 th->th.th_info.ds.ds_thread ) );
1356 th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1358 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1360 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1361 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1362 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1363 __kmp_check_stack_overlap( th );
1370 KA_TRACE( 10, (
"__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC
1371 " bytes\n", stack_size ) );
1373 stack_size += gtid * __kmp_stkoffset;
1375 TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1376 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
1378 KA_TRACE( 10, (
"__kmp_create_worker: (before) stack_size = %"
1380 " bytes, &__kmp_launch_worker = %p, th = %p, "
1382 (SIZE_T) stack_size,
1383 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1384 (LPVOID) th, &idThread ) );
1387 handle = CreateThread( NULL, (SIZE_T) stack_size,
1388 (LPTHREAD_START_ROUTINE) __kmp_launch_worker,
1389 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1392 KA_TRACE( 10, (
"__kmp_create_worker: (after) stack_size = %"
1394 " bytes, &__kmp_launch_worker = %p, th = %p, "
1395 "idThread = %u, handle = %" KMP_UINTPTR_SPEC
"\n",
1396 (SIZE_T) stack_size,
1397 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1398 (LPVOID) th, idThread, handle ) );
1401 if ( handle == 0 ) {
1402 DWORD error = GetLastError();
1405 KMP_MSG( CantCreateThread ),
1410 th->th.th_info.ds.ds_thread = handle;
1416 KA_TRACE( 10, (
"__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1420 __kmp_still_running(kmp_info_t *th) {
1421 return (WAIT_TIMEOUT == WaitForSingleObject( th->th.th_info.ds.ds_thread, 0));
1425 __kmp_create_monitor( kmp_info_t *th )
1427 kmp_thread_t handle;
1429 int ideal, new_ideal;
1430 int caller_gtid = __kmp_get_gtid();
1432 KA_TRACE( 10, (
"__kmp_create_monitor: try to create monitor\n" ) );
1436 __kmp_monitor_ev = CreateEvent( NULL, TRUE, FALSE, NULL );
1437 if ( __kmp_monitor_ev == NULL ) {
1438 DWORD error = GetLastError();
1441 KMP_MSG( CantCreateEvent ),
1447 __kmp_itt_system_object_created( __kmp_monitor_ev,
"Event" );
1450 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1451 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1455 if ( __kmp_monitor_stksize == 0 ) {
1456 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1458 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1459 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1462 KA_TRACE( 10, (
"__kmp_create_monitor: requested stacksize = %d bytes\n",
1463 (
int) __kmp_monitor_stksize ) );
1465 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
1467 handle = CreateThread( NULL, (SIZE_T) __kmp_monitor_stksize,
1468 (LPTHREAD_START_ROUTINE) __kmp_launch_monitor,
1469 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1471 DWORD error = GetLastError();
1474 KMP_MSG( CantCreateThread ),
1480 th->th.th_info.ds.ds_thread = handle;
1484 KA_TRACE( 10, (
"__kmp_create_monitor: monitor created %p\n",
1485 (
void *) th->th.th_info.ds.ds_thread ) );
1498 __kmp_is_thread_alive( kmp_info_t * th, DWORD *exit_val )
1501 rc = GetExitCodeThread( th->th.th_info.ds.ds_thread, exit_val );
1503 DWORD error = GetLastError();
1506 KMP_MSG( FunctionError,
"GetExitCodeThread()" ),
1511 return ( *exit_val == STILL_ACTIVE );
1519 ExitThread( exit_status );
1526 __kmp_reap_common( kmp_info_t * th )
1532 KA_TRACE( 10, (
"__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid ) );
1552 register kmp_uint32 spins;
1554 KMP_FSYNC_SPIN_INIT( obj, (
void*) & th->th.th_info.ds.ds_alive );
1556 KMP_INIT_YIELD( spins );
1559 KMP_FSYNC_SPIN_PREPARE( obj );
1561 __kmp_is_thread_alive( th, &exit_val );
1562 KMP_YIELD( TCR_4(__kmp_nth) > __kmp_avail_proc );
1563 KMP_YIELD_SPIN( spins );
1564 }
while ( exit_val == STILL_ACTIVE && TCR_4( th->th.th_info.ds.ds_alive ) );
1566 if ( exit_val == STILL_ACTIVE ) {
1567 KMP_FSYNC_CANCEL( obj );
1569 KMP_FSYNC_SPIN_ACQUIRED( obj );
1574 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1581 if ( exit_val == STILL_ACTIVE ) {
1582 KA_TRACE( 1, (
"__kmp_reap_common: thread still active.\n" ) );
1583 }
else if ( (
void *) exit_val != (
void *) th) {
1584 KA_TRACE( 1, (
"__kmp_reap_common: ExitProcess / TerminateThread used?\n" ) );
1589 "__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
"\n",
1590 th->th.th_info.ds.ds_gtid,
1591 th->th.th_info.ds.ds_thread
1595 th->th.th_info.ds.ds_thread = 0;
1596 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1597 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1598 th->th.th_info.ds.ds_thread_id = 0;
1604 __kmp_reap_monitor( kmp_info_t *th )
1608 KA_TRACE( 10, (
"__kmp_reap_monitor: try to reap %p\n",
1609 (
void *) th->th.th_info.ds.ds_thread ) );
1614 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1615 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
1621 status = SetEvent( __kmp_monitor_ev );
1622 if ( status == FALSE ) {
1623 DWORD error = GetLastError();
1626 KMP_MSG( CantSetEvent ),
1631 KA_TRACE( 10, (
"__kmp_reap_monitor: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1632 __kmp_reap_common( th );
1634 __kmp_free_handle( __kmp_monitor_ev );
1640 __kmp_reap_worker( kmp_info_t * th )
1642 KA_TRACE( 10, (
"__kmp_reap_worker: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1643 __kmp_reap_common( th );
1649 #if KMP_HANDLE_SIGNALS
1653 __kmp_team_handler(
int signo )
1655 if ( __kmp_global.g.g_abort == 0 ) {
1657 if ( __kmp_debug_buf ) {
1658 __kmp_dump_debug_buffer();
1661 TCW_4( __kmp_global.g.g_abort, signo );
1663 TCW_4( __kmp_global.g.g_done, TRUE );
1671 sig_func_t __kmp_signal(
int signum, sig_func_t handler ) {
1672 sig_func_t old = signal( signum, handler );
1673 if ( old == SIG_ERR ) {
1675 __kmp_msg( kmp_ms_fatal, KMP_MSG( FunctionError,
"signal" ), KMP_ERR( error ), __kmp_msg_null );
1681 __kmp_install_one_handler(
1688 KB_TRACE( 60, (
"__kmp_install_one_handler: called: sig=%d\n", sig ) );
1689 if ( parallel_init ) {
1690 old = __kmp_signal( sig, handler );
1692 if ( old == __kmp_sighldrs[ sig ] ) {
1693 __kmp_siginstalled[ sig ] = 1;
1696 old = __kmp_signal( sig, old );
1702 old = __kmp_signal( sig, SIG_DFL );
1703 __kmp_sighldrs[ sig ] = old;
1704 __kmp_signal( sig, old );
1710 __kmp_remove_one_handler(
int sig ) {
1711 if ( __kmp_siginstalled[ sig ] ) {
1714 KB_TRACE( 60, (
"__kmp_remove_one_handler: called: sig=%d\n", sig ) );
1715 old = __kmp_signal( sig, __kmp_sighldrs[ sig ] );
1716 if ( old != __kmp_team_handler ) {
1717 KB_TRACE( 10, (
"__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1718 old = __kmp_signal( sig, old );
1720 __kmp_sighldrs[ sig ] = NULL;
1721 __kmp_siginstalled[ sig ] = 0;
1728 __kmp_install_signals(
int parallel_init )
1730 KB_TRACE( 10, (
"__kmp_install_signals: called\n" ) );
1731 if ( ! __kmp_handle_signals ) {
1732 KB_TRACE( 10, (
"__kmp_install_signals: KMP_HANDLE_SIGNALS is false - handlers not installed\n" ) );
1735 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1736 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1737 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1738 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1739 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1740 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1745 __kmp_remove_signals(
void )
1748 KB_TRACE( 10, (
"__kmp_remove_signals: called\n" ) );
1749 for ( sig = 1; sig < NSIG; ++ sig ) {
1750 __kmp_remove_one_handler( sig );
1755 #endif // KMP_HANDLE_SIGNALS
1759 __kmp_thread_sleep(
int millis )
1763 status = SleepEx( (DWORD) millis, FALSE );
1765 DWORD error = GetLastError();
1768 KMP_MSG( FunctionError,
"SleepEx()" ),
1777 __kmp_is_address_mapped(
void * addr )
1780 MEMORY_BASIC_INFORMATION lpBuffer;
1783 dwLength =
sizeof(MEMORY_BASIC_INFORMATION);
1785 status = VirtualQuery( addr, &lpBuffer, dwLength );
1787 return !((( lpBuffer.State == MEM_RESERVE) || ( lpBuffer.State == MEM_FREE )) ||
1788 (( lpBuffer.Protect == PAGE_NOACCESS ) || ( lpBuffer.Protect == PAGE_EXECUTE )));
1792 __kmp_hardware_timestamp(
void)
1796 QueryPerformanceCounter((LARGE_INTEGER*) &r);
1802 __kmp_free_handle( kmp_thread_t tHandle )
1806 rc = CloseHandle( tHandle );
1808 DWORD error = GetLastError();
1811 KMP_MSG( CantCloseHandle ),
1819 __kmp_get_load_balance(
int max ) {
1821 static ULONG glb_buff_size = 100 * 1024;
1823 static int glb_running_threads = 0;
1824 static double glb_call_time = 0;
1826 int running_threads = 0;
1827 NTSTATUS status = 0;
1828 ULONG buff_size = 0;
1829 ULONG info_size = 0;
1830 void * buffer = NULL;
1831 PSYSTEM_PROCESS_INFORMATION spi = NULL;
1834 double call_time = 0.0;
1836 __kmp_elapsed( & call_time );
1838 if ( glb_call_time &&
1839 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
1840 running_threads = glb_running_threads;
1843 glb_call_time = call_time;
1846 if ( NtQuerySystemInformation == NULL ) {
1847 running_threads = -1;
1858 buff_size = glb_buff_size;
1860 buff_size = 2 * buff_size;
1863 buffer = KMP_INTERNAL_REALLOC( buffer, buff_size );
1864 if ( buffer == NULL ) {
1865 running_threads = -1;
1868 status = NtQuerySystemInformation( SystemProcessInformation, buffer, buff_size, & info_size );
1871 }
while ( status == STATUS_INFO_LENGTH_MISMATCH );
1872 glb_buff_size = buff_size;
1874 #define CHECK( cond ) \
1876 KMP_DEBUG_ASSERT( cond ); \
1877 if ( ! ( cond ) ) { \
1878 running_threads = -1; \
1883 CHECK( buff_size >= info_size );
1884 spi = PSYSTEM_PROCESS_INFORMATION( buffer );
1886 ptrdiff_t offset = uintptr_t( spi ) - uintptr_t( buffer );
1887 CHECK( 0 <= offset && offset +
sizeof( SYSTEM_PROCESS_INFORMATION ) < info_size );
1888 HANDLE pid = spi->ProcessId;
1889 ULONG num = spi->NumberOfThreads;
1891 size_t spi_size =
sizeof( SYSTEM_PROCESS_INFORMATION ) +
sizeof( SYSTEM_THREAD ) * ( num - 1 );
1892 CHECK( offset + spi_size < info_size );
1893 if ( spi->NextEntryOffset != 0 ) {
1894 CHECK( spi_size <= spi->NextEntryOffset );
1899 for (
int i = 0; i < num; ++ i ) {
1900 THREAD_STATE state = spi->Threads[ i ].State;
1903 if ( state == StateRunning ) {
1907 if ( running_threads >= max ) {
1913 if ( spi->NextEntryOffset == 0 ) {
1916 spi = PSYSTEM_PROCESS_INFORMATION( uintptr_t( spi ) + spi->NextEntryOffset );
1923 if ( buffer != NULL ) {
1924 KMP_INTERNAL_FREE( buffer );
1927 glb_running_threads = running_threads;
1929 return running_threads;