39 #include "kmp_wait_release.h"
53 enum SYSTEM_INFORMATION_CLASS {
54 SystemProcessInformation = 5
74 SIZE_T PeakVirtualSize;
77 SIZE_T PeakWorkingSetSize;
78 SIZE_T WorkingSetSize;
79 SIZE_T QuotaPeakPagedPoolUsage;
80 SIZE_T QuotaPagedPoolUsage;
81 SIZE_T QuotaPeakNonPagedPoolUsage;
82 SIZE_T QuotaNonPagedPoolUsage;
84 SIZE_T PeakPagefileUsage;
85 SIZE_T PrivatePageCount;
88 struct SYSTEM_THREAD {
89 LARGE_INTEGER KernelTime;
90 LARGE_INTEGER UserTime;
91 LARGE_INTEGER CreateTime;
97 ULONG ContextSwitchCount;
102 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, KernelTime ) == 0 );
104 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, StartAddress ) == 28 );
105 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, State ) == 52 );
107 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, StartAddress ) == 32 );
108 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, State ) == 68 );
111 struct SYSTEM_PROCESS_INFORMATION {
112 ULONG NextEntryOffset;
113 ULONG NumberOfThreads;
114 LARGE_INTEGER Reserved[ 3 ];
115 LARGE_INTEGER CreateTime;
116 LARGE_INTEGER UserTime;
117 LARGE_INTEGER KernelTime;
118 UNICODE_STRING ImageName;
121 HANDLE ParentProcessId;
123 ULONG Reserved2[ 2 ];
124 VM_COUNTERS VMCounters;
125 IO_COUNTERS IOCounters;
126 SYSTEM_THREAD Threads[ 1 ];
128 typedef SYSTEM_PROCESS_INFORMATION * PSYSTEM_PROCESS_INFORMATION;
130 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, NextEntryOffset ) == 0 );
131 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, CreateTime ) == 32 );
132 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, ImageName ) == 56 );
134 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, ProcessId ) == 68 );
135 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, HandleCount ) == 76 );
136 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, VMCounters ) == 88 );
137 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, IOCounters ) == 136 );
138 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, Threads ) == 184 );
140 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, ProcessId ) == 80 );
141 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, HandleCount ) == 96 );
142 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, VMCounters ) == 112 );
143 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, IOCounters ) == 208 );
144 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, Threads ) == 256 );
147 typedef NTSTATUS (NTAPI *NtQuerySystemInformation_t)( SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG );
148 NtQuerySystemInformation_t NtQuerySystemInformation = NULL;
150 HMODULE ntdll = NULL;
154 #if KMP_GROUP_AFFINITY
155 static HMODULE kernel32 = NULL;
161 #if KMP_HANDLE_SIGNALS
162 typedef void (* sig_func_t )( int );
163 static sig_func_t __kmp_sighldrs[ NSIG ];
164 static int __kmp_siginstalled[ NSIG ];
167 static HANDLE __kmp_monitor_ev;
168 static kmp_int64 __kmp_win32_time;
169 double __kmp_win32_tick;
171 int __kmp_init_runtime = FALSE;
172 CRITICAL_SECTION __kmp_win32_section;
175 __kmp_win32_mutex_init( kmp_win32_mutex_t *mx )
177 InitializeCriticalSection( & mx->cs );
179 __kmp_itt_system_object_created( & mx->cs,
"Critical Section" );
184 __kmp_win32_mutex_destroy( kmp_win32_mutex_t *mx )
186 DeleteCriticalSection( & mx->cs );
190 __kmp_win32_mutex_lock( kmp_win32_mutex_t *mx )
192 EnterCriticalSection( & mx->cs );
196 __kmp_win32_mutex_unlock( kmp_win32_mutex_t *mx )
198 LeaveCriticalSection( & mx->cs );
202 __kmp_win32_cond_init( kmp_win32_cond_t *cv )
204 cv->waiters_count_ = 0;
205 cv->wait_generation_count_ = 0;
206 cv->release_count_ = 0;
209 __kmp_win32_mutex_init( & cv->waiters_count_lock_ );
212 cv->event_ = CreateEvent( NULL,
217 __kmp_itt_system_object_created( cv->event_,
"Event" );
222 __kmp_win32_cond_destroy( kmp_win32_cond_t *cv )
224 __kmp_win32_mutex_destroy( & cv->waiters_count_lock_ );
225 __kmp_free_handle( cv->event_ );
226 memset( cv,
'\0',
sizeof( *cv ) );
233 __kmp_win32_cond_wait( kmp_win32_cond_t *cv, kmp_win32_mutex_t *mx, kmp_info_t *th,
int need_decrease_load )
239 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
242 cv->waiters_count_++;
245 my_generation = cv->wait_generation_count_;
247 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_ );
248 __kmp_win32_mutex_unlock( mx );
255 WaitForSingleObject( cv->event_, INFINITE );
257 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
262 wait_done = ( cv->release_count_ > 0 ) &&
263 ( cv->wait_generation_count_ != my_generation );
265 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_);
273 __kmp_win32_mutex_lock( mx );
274 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
276 cv->waiters_count_--;
277 cv->release_count_--;
279 last_waiter = ( cv->release_count_ == 0 );
281 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_ );
285 ResetEvent( cv->event_ );
290 __kmp_win32_cond_broadcast( kmp_win32_cond_t *cv )
292 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
294 if( cv->waiters_count_ > 0 ) {
295 SetEvent( cv->event_ );
298 cv->release_count_ = cv->waiters_count_;
301 cv->wait_generation_count_++;
304 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_ );
308 __kmp_win32_cond_signal( kmp_win32_cond_t *cv )
310 __kmp_win32_cond_broadcast( cv );
317 __kmp_enable(
int new_state )
319 if (__kmp_init_runtime)
320 LeaveCriticalSection( & __kmp_win32_section );
324 __kmp_disable(
int *old_state )
328 if (__kmp_init_runtime)
329 EnterCriticalSection( & __kmp_win32_section );
333 __kmp_suspend_initialize(
void )
339 __kmp_suspend_initialize_thread( kmp_info_t *th )
341 if ( ! TCR_4( th->th.th_suspend_init ) ) {
344 __kmp_win32_cond_init( &th->th.th_suspend_cv );
345 __kmp_win32_mutex_init( &th->th.th_suspend_mx );
346 TCW_4( th->th.th_suspend_init, TRUE );
351 __kmp_suspend_uninitialize_thread( kmp_info_t *th )
353 if ( TCR_4( th->th.th_suspend_init ) ) {
356 __kmp_win32_cond_destroy( & th->th.th_suspend_cv );
357 __kmp_win32_mutex_destroy( & th->th.th_suspend_mx );
358 TCW_4( th->th.th_suspend_init, FALSE );
366 static inline void __kmp_suspend_template(
int th_gtid, C *flag )
368 kmp_info_t *th = __kmp_threads[th_gtid];
370 typename C::flag_t old_spin;
372 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d enter for flag's loc(%p)\n", th_gtid, flag->get() ) );
374 __kmp_suspend_initialize_thread( th );
375 __kmp_win32_mutex_lock( &th->th.th_suspend_mx );
377 KF_TRACE( 10, (
"__kmp_suspend_template: T#%d setting sleep bit for flag's loc(%p)\n",
378 th_gtid, flag->get() ) );
383 old_spin = flag->set_sleeping();
385 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d set sleep bit for flag's loc(%p)==%d\n",
386 th_gtid, flag->get(), *(flag->get()) ) );
388 if ( flag->done_check_val(old_spin) ) {
389 old_spin = flag->unset_sleeping();
390 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d false alarm, reset sleep bit for flag's loc(%p)\n",
391 th_gtid, flag->get()) );
394 __kmp_suspend_count++;
400 int deactivated = FALSE;
401 TCW_PTR(th->th.th_sleep_loc, (
void *)flag);
402 while ( flag->is_sleeping() ) {
403 KF_TRACE( 15, (
"__kmp_suspend_template: T#%d about to perform kmp_win32_cond_wait()\n",
406 if ( ! deactivated ) {
407 th->th.th_active = FALSE;
408 if ( th->th.th_active_in_pool ) {
409 th->th.th_active_in_pool = FALSE;
411 (kmp_int32 *) &__kmp_thread_pool_active_nth );
412 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
417 __kmp_win32_cond_wait( &th->th.th_suspend_cv, &th->th.th_suspend_mx, 0, 0 );
420 __kmp_win32_cond_wait( &th->th.th_suspend_cv, &th->th.th_suspend_mx, 0, 0 );
424 if( flag->is_sleeping() ) {
425 KF_TRACE( 100, (
"__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ));
433 th->th.th_active = TRUE;
434 if ( TCR_4(th->th.th_in_pool) ) {
436 (kmp_int32 *) &__kmp_thread_pool_active_nth );
437 th->th.th_active_in_pool = TRUE;
443 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
445 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d exit\n", th_gtid ) );
448 void __kmp_suspend_32(
int th_gtid, kmp_flag_32 *flag) {
449 __kmp_suspend_template(th_gtid, flag);
451 void __kmp_suspend_64(
int th_gtid, kmp_flag_64 *flag) {
452 __kmp_suspend_template(th_gtid, flag);
454 void __kmp_suspend_oncore(
int th_gtid, kmp_flag_oncore *flag) {
455 __kmp_suspend_template(th_gtid, flag);
463 static inline void __kmp_resume_template(
int target_gtid, C *flag )
465 kmp_info_t *th = __kmp_threads[target_gtid];
469 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
472 KF_TRACE( 30, (
"__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", gtid, target_gtid ) );
474 __kmp_suspend_initialize_thread( th );
475 __kmp_win32_mutex_lock( &th->th.th_suspend_mx );
478 flag = (C *)th->th.th_sleep_loc;
482 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p)\n",
483 gtid, target_gtid, NULL ) );
484 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
488 typename C::flag_t old_spin = flag->unset_sleeping();
489 if ( !flag->is_sleeping_val(old_spin) ) {
490 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p): "
492 gtid, target_gtid, flag->get(), old_spin, *(flag->get()) ) );
493 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
497 TCW_PTR(th->th.th_sleep_loc, NULL);
499 KF_TRACE( 5, (
"__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p)\n",
500 gtid, target_gtid, flag->get() ) );
503 __kmp_win32_cond_signal( &th->th.th_suspend_cv );
504 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
506 KF_TRACE( 30, (
"__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
507 gtid, target_gtid ) );
510 void __kmp_resume_32(
int target_gtid, kmp_flag_32 *flag) {
511 __kmp_resume_template(target_gtid, flag);
513 void __kmp_resume_64(
int target_gtid, kmp_flag_64 *flag) {
514 __kmp_resume_template(target_gtid, flag);
516 void __kmp_resume_oncore(
int target_gtid, kmp_flag_oncore *flag) {
517 __kmp_resume_template(target_gtid, flag);
525 __kmp_yield(
int cond )
535 __kmp_gtid_set_specific(
int gtid )
537 KA_TRACE( 50, (
"__kmp_gtid_set_specific: T#%d key:%d\n",
538 gtid, __kmp_gtid_threadprivate_key ));
539 KMP_ASSERT( __kmp_init_runtime );
540 if( ! TlsSetValue( __kmp_gtid_threadprivate_key, (LPVOID)(gtid+1)) )
541 KMP_FATAL( TLSSetValueFailed );
545 __kmp_gtid_get_specific()
548 if( !__kmp_init_runtime ) {
549 KA_TRACE( 50, (
"__kmp_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
550 return KMP_GTID_SHUTDOWN;
552 gtid = (int)(kmp_intptr_t)TlsGetValue( __kmp_gtid_threadprivate_key );
559 KA_TRACE( 50, (
"__kmp_gtid_get_specific: key:%d gtid:%d\n",
560 __kmp_gtid_threadprivate_key, gtid ));
567 #if KMP_GROUP_AFFINITY
574 __kmp_get_proc_group( kmp_affin_mask_t
const *mask )
578 for (i = 0; i < __kmp_num_proc_groups; i++) {
593 __kmp_set_system_affinity( kmp_affin_mask_t
const *mask,
int abort_on_error )
596 #if KMP_GROUP_AFFINITY
598 if (__kmp_num_proc_groups > 1) {
603 int group = __kmp_get_proc_group( mask );
605 if (abort_on_error) {
606 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
616 ga.Mask = mask[group];
617 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
619 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
620 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
621 DWORD error = GetLastError();
622 if (abort_on_error) {
625 KMP_MSG( CantSetThreadAffMask ),
638 if (!SetThreadAffinityMask( GetCurrentThread(), *mask )) {
639 DWORD error = GetLastError();
640 if (abort_on_error) {
643 KMP_MSG( CantSetThreadAffMask ),
655 __kmp_get_system_affinity( kmp_affin_mask_t *mask,
int abort_on_error )
658 #if KMP_GROUP_AFFINITY
660 if (__kmp_num_proc_groups > 1) {
663 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
665 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
666 DWORD error = GetLastError();
667 if (abort_on_error) {
670 KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
678 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups)
683 mask[ga.Group] = ga.Mask;
690 kmp_affin_mask_t newMask, sysMask, retval;
692 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
693 DWORD error = GetLastError();
694 if (abort_on_error) {
697 KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
704 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
706 DWORD error = GetLastError();
707 if (abort_on_error) {
710 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
717 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
719 DWORD error = GetLastError();
720 if (abort_on_error) {
723 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
735 __kmp_affinity_bind_thread(
int proc )
738 #if KMP_GROUP_AFFINITY
740 if (__kmp_num_proc_groups > 1) {
746 KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups
747 * CHAR_BIT *
sizeof(DWORD_PTR))));
748 ga.Group = proc / (CHAR_BIT *
sizeof(DWORD_PTR));
749 ga.Mask = (
unsigned long long)1 << (proc % (CHAR_BIT *
sizeof(DWORD_PTR)));
750 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
752 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
753 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
754 DWORD error = GetLastError();
755 if (__kmp_affinity_verbose) {
758 KMP_MSG( CantSetThreadAffMask ),
770 kmp_affin_mask_t mask;
772 KMP_CPU_SET(proc, &mask);
773 __kmp_set_system_affinity(&mask, TRUE);
778 __kmp_affinity_determine_capable(
const char *env_var )
784 #if KMP_GROUP_AFFINITY
785 KMP_AFFINITY_ENABLE(__kmp_num_proc_groups*
sizeof(kmp_affin_mask_t));
787 KMP_AFFINITY_ENABLE(
sizeof(kmp_affin_mask_t));
791 "__kmp_affinity_determine_capable: "
792 "Windows* OS affinity interface functional (mask size = %" KMP_SIZE_T_SPEC
").\n",
793 __kmp_affin_mask_size
798 __kmp_read_cpu_time(
void )
800 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
806 status = GetProcessTimes( GetCurrentProcess(), &CreationTime,
807 &ExitTime, &KernelTime, &UserTime );
812 sec += KernelTime.dwHighDateTime;
813 sec += UserTime.dwHighDateTime;
816 sec *= (double) (1 << 16) * (double) (1 << 16);
818 sec += KernelTime.dwLowDateTime;
819 sec += UserTime.dwLowDateTime;
821 cpu_time += (sec * 100.0) / NSEC_PER_SEC;
828 __kmp_read_system_info(
struct kmp_sys_info *info )
847 __kmp_runtime_initialize(
void )
853 if ( __kmp_init_runtime ) {
861 UINT err_mode = SetErrorMode (SEM_FAILCRITICALERRORS);
863 BOOL ret = GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
864 |GET_MODULE_HANDLE_EX_FLAG_PIN,
865 (LPCTSTR)&__kmp_serial_initialize, &h);
866 KMP_DEBUG_ASSERT2(h && ret,
"OpenMP RTL cannot find itself loaded");
867 SetErrorMode (err_mode);
868 KA_TRACE( 10, (
"__kmp_runtime_initialize: dynamic library pinned\n") );
872 InitializeCriticalSection( & __kmp_win32_section );
874 __kmp_itt_system_object_created( & __kmp_win32_section,
"Critical Section" );
876 __kmp_initialize_system_tick();
878 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
879 if ( ! __kmp_cpuinfo.initialized ) {
880 __kmp_query_cpuid( & __kmp_cpuinfo );
885 #if KMP_OS_WINDOWS && ! defined KMP_DYNAMIC_LIB
900 __kmp_tls_gtid_min = 0;
902 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
906 if ( !__kmp_gtid_threadprivate_key ) {
907 __kmp_gtid_threadprivate_key = TlsAlloc();
908 if( __kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES ) {
909 KMP_FATAL( TLSOutOfIndexes );
924 __kmp_str_buf_init( & path );
925 path_size = GetSystemDirectory( path.str, path.size );
926 KMP_DEBUG_ASSERT( path_size > 0 );
927 if ( path_size >= path.size ) {
931 __kmp_str_buf_reserve( & path, path_size );
932 path_size = GetSystemDirectory( path.str, path.size );
933 KMP_DEBUG_ASSERT( path_size > 0 );
935 if ( path_size > 0 && path_size < path.size ) {
940 path.used = path_size;
941 __kmp_str_buf_print( & path,
"\\%s",
"ntdll.dll" );
946 ntdll = GetModuleHandle( path.str );
949 KMP_DEBUG_ASSERT( ntdll != NULL );
950 if ( ntdll != NULL ) {
951 NtQuerySystemInformation = (NtQuerySystemInformation_t) GetProcAddress( ntdll,
"NtQuerySystemInformation" );
953 KMP_DEBUG_ASSERT( NtQuerySystemInformation != NULL );
955 #if KMP_GROUP_AFFINITY
960 if ( path_size > 0 && path_size < path.size ) {
965 path.used = path_size;
966 __kmp_str_buf_print( & path,
"\\%s",
"kernel32.dll" );
971 kernel32 = GetModuleHandle( path.str );
972 KA_TRACE( 10, (
"__kmp_runtime_initialize: kernel32.dll = %s\n", path.str ) );
978 if ( kernel32 != NULL ) {
979 __kmp_GetActiveProcessorCount = (kmp_GetActiveProcessorCount_t) GetProcAddress( kernel32,
"GetActiveProcessorCount" );
980 __kmp_GetActiveProcessorGroupCount = (kmp_GetActiveProcessorGroupCount_t) GetProcAddress( kernel32,
"GetActiveProcessorGroupCount" );
981 __kmp_GetThreadGroupAffinity = (kmp_GetThreadGroupAffinity_t) GetProcAddress( kernel32,
"GetThreadGroupAffinity" );
982 __kmp_SetThreadGroupAffinity = (kmp_SetThreadGroupAffinity_t) GetProcAddress( kernel32,
"SetThreadGroupAffinity" );
984 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorCount = %p\n", __kmp_GetActiveProcessorCount ) );
985 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorGroupCount = %p\n", __kmp_GetActiveProcessorGroupCount ) );
986 KA_TRACE( 10, (
"__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity = %p\n", __kmp_GetThreadGroupAffinity ) );
987 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity = %p\n", __kmp_SetThreadGroupAffinity ) );
988 KA_TRACE( 10, (
"__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
sizeof(kmp_affin_mask_t) ) );
997 if ( ( __kmp_GetActiveProcessorCount != NULL )
998 && ( __kmp_GetActiveProcessorGroupCount != NULL )
999 && ( __kmp_GetThreadGroupAffinity != NULL )
1000 && ( __kmp_SetThreadGroupAffinity != NULL )
1001 && ( ( __kmp_num_proc_groups
1002 = __kmp_GetActiveProcessorGroupCount() ) > 1 ) ) {
1008 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
1012 for ( i = 0; i < __kmp_num_proc_groups; i++ ) {
1013 DWORD size = __kmp_GetActiveProcessorCount( i );
1014 __kmp_xproc += size;
1015 KA_TRACE( 10, (
"__kmp_runtime_initialize: proc group %d size = %d\n", i, size ) );
1019 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
1023 if ( __kmp_num_proc_groups <= 1 ) {
1024 GetSystemInfo( & info );
1025 __kmp_xproc = info.dwNumberOfProcessors;
1028 GetSystemInfo( & info );
1029 __kmp_xproc = info.dwNumberOfProcessors;
1036 if ( __kmp_xproc <= 0 ) {
1040 KA_TRACE( 5, (
"__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc) );
1042 __kmp_str_buf_free( & path );
1045 __kmp_itt_initialize();
1048 __kmp_init_runtime = TRUE;
1052 __kmp_runtime_destroy(
void )
1054 if ( ! __kmp_init_runtime ) {
1059 __kmp_itt_destroy();
1064 KA_TRACE( 40, (
"__kmp_runtime_destroy\n" ));
1066 if( __kmp_gtid_threadprivate_key ) {
1067 TlsFree( __kmp_gtid_threadprivate_key );
1068 __kmp_gtid_threadprivate_key = 0;
1071 __kmp_affinity_uninitialize();
1072 DeleteCriticalSection( & __kmp_win32_section );
1075 NtQuerySystemInformation = NULL;
1079 __kmp_GetActiveProcessorCount = NULL;
1080 __kmp_GetActiveProcessorGroupCount = NULL;
1081 __kmp_GetThreadGroupAffinity = NULL;
1082 __kmp_SetThreadGroupAffinity = NULL;
1083 #endif // KMP_ARCH_X86_64
1085 __kmp_init_runtime = FALSE;
1090 __kmp_terminate_thread(
int gtid )
1092 kmp_info_t *th = __kmp_threads[ gtid ];
1096 KA_TRACE( 10, (
"__kmp_terminate_thread: kill (%d)\n", gtid ) );
1098 if (TerminateThread( th->th.th_info.ds.ds_thread, (DWORD) -1) == FALSE) {
1101 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1108 __kmp_clear_system_time(
void )
1112 status = QueryPerformanceCounter( & time );
1113 __kmp_win32_time = (kmp_int64) time.QuadPart;
1117 __kmp_initialize_system_tick(
void )
1123 status = QueryPerformanceFrequency( & freq );
1125 DWORD error = GetLastError();
1128 KMP_MSG( FunctionError,
"QueryPerformanceFrequency()" ),
1135 __kmp_win32_tick = ((double) 1.0) / (double) freq.QuadPart;
1143 __kmp_elapsed(
double *t )
1147 status = QueryPerformanceCounter( & now );
1148 *t = ((double) now.QuadPart) * __kmp_win32_tick;
1154 __kmp_elapsed_tick(
double *t )
1156 *t = __kmp_win32_tick;
1160 __kmp_read_system_time(
double *delta )
1163 if (delta != NULL) {
1167 status = QueryPerformanceCounter( & now );
1169 *delta = ((double) (((kmp_int64) now.QuadPart) - __kmp_win32_time))
1178 __kmp_launch_worker(
void *arg )
1180 volatile void *stack_data;
1183 kmp_info_t *this_thr = (kmp_info_t *) arg;
1186 gtid = this_thr->th.th_info.ds.ds_gtid;
1187 __kmp_gtid_set_specific( gtid );
1188 #ifdef KMP_TDATA_GTID
1189 #error "This define causes problems with LoadLibrary() + declspec(thread) " \
1190 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
1191 "reference: http://support.microsoft.com/kb/118816"
1196 __kmp_itt_thread_name( gtid );
1199 __kmp_affinity_set_init_mask( gtid, FALSE );
1201 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1206 __kmp_clear_x87_fpu_status_word();
1207 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
1208 __kmp_load_mxcsr( &__kmp_init_mxcsr );
1211 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
1212 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
1215 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1216 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1217 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1219 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1220 TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
1221 KMP_ASSERT( this_thr -> th.th_info.ds.ds_stackgrow == FALSE );
1222 __kmp_check_stack_overlap( this_thr );
1225 exit_val = __kmp_launch_thread( this_thr );
1226 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1227 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1236 __kmp_launch_monitor(
void *arg )
1239 kmp_thread_t monitor;
1242 kmp_info_t *this_thr = (kmp_info_t *) arg;
1244 KMP_DEBUG_ASSERT(__kmp_init_monitor);
1245 TCW_4( __kmp_init_monitor, 2 );
1247 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1248 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1251 KA_TRACE( 10, (
"__kmp_launch_monitor: launched\n" ) );
1253 monitor = GetCurrentThread();
1256 status = SetThreadPriority( monitor, THREAD_PRIORITY_HIGHEST );
1258 DWORD error = GetLastError();
1261 KMP_MSG( CantSetThreadPriority ),
1268 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
1269 #ifdef KMP_TDATA_GTID
1270 #error "This define causes problems with LoadLibrary() + declspec(thread) " \
1271 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
1272 "reference: http://support.microsoft.com/kb/118816"
1277 __kmp_itt_thread_ignore();
1282 interval = ( 1000 / __kmp_monitor_wakeups );
1284 while (! TCR_4(__kmp_global.g.g_done)) {
1287 KA_TRACE( 15, (
"__kmp_launch_monitor: update\n" ) );
1289 wait_status = WaitForSingleObject( __kmp_monitor_ev, interval );
1291 if (wait_status == WAIT_TIMEOUT) {
1292 TCW_4( __kmp_global.g.g_time.dt.t_value,
1293 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
1299 KA_TRACE( 10, (
"__kmp_launch_monitor: finished\n" ) );
1301 status = SetThreadPriority( monitor, THREAD_PRIORITY_NORMAL );
1303 DWORD error = GetLastError();
1306 KMP_MSG( CantSetThreadPriority ),
1312 if (__kmp_global.g.g_abort != 0) {
1318 KA_TRACE( 10, (
"__kmp_launch_monitor: terminate sig=%d\n", (__kmp_global.g.g_abort) ) );
1323 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1324 __kmp_terminate_thread( gtid );
1330 KA_TRACE( 10, (
"__kmp_launch_monitor: raise sig=%d\n", (__kmp_global.g.g_abort) ) );
1332 if (__kmp_global.g.g_abort > 0) {
1333 raise( __kmp_global.g.g_abort );
1337 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1344 __kmp_create_worker(
int gtid, kmp_info_t *th,
size_t stack_size )
1346 kmp_thread_t handle;
1349 KA_TRACE( 10, (
"__kmp_create_worker: try to create thread (%d)\n", gtid ) );
1351 th->th.th_info.ds.ds_gtid = gtid;
1353 if ( KMP_UBER_GTID(gtid) ) {
1362 rc = DuplicateHandle(
1363 GetCurrentProcess(),
1365 GetCurrentProcess(),
1366 &th->th.th_info.ds.ds_thread,
1369 DUPLICATE_SAME_ACCESS
1372 KA_TRACE( 10, (
" __kmp_create_worker: ROOT Handle duplicated, th = %p, handle = %" KMP_UINTPTR_SPEC
"\n",
1374 th->th.th_info.ds.ds_thread ) );
1375 th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1377 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1379 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1380 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1381 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1382 __kmp_check_stack_overlap( th );
1389 KA_TRACE( 10, (
"__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC
1390 " bytes\n", stack_size ) );
1392 stack_size += gtid * __kmp_stkoffset;
1394 TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1395 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
1397 KA_TRACE( 10, (
"__kmp_create_worker: (before) stack_size = %"
1399 " bytes, &__kmp_launch_worker = %p, th = %p, "
1401 (SIZE_T) stack_size,
1402 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1403 (LPVOID) th, &idThread ) );
1406 handle = CreateThread( NULL, (SIZE_T) stack_size,
1407 (LPTHREAD_START_ROUTINE) __kmp_launch_worker,
1408 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1411 KA_TRACE( 10, (
"__kmp_create_worker: (after) stack_size = %"
1413 " bytes, &__kmp_launch_worker = %p, th = %p, "
1414 "idThread = %u, handle = %" KMP_UINTPTR_SPEC
"\n",
1415 (SIZE_T) stack_size,
1416 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1417 (LPVOID) th, idThread, handle ) );
1420 if ( handle == 0 ) {
1421 DWORD error = GetLastError();
1424 KMP_MSG( CantCreateThread ),
1429 th->th.th_info.ds.ds_thread = handle;
1435 KA_TRACE( 10, (
"__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1439 __kmp_still_running(kmp_info_t *th) {
1440 return (WAIT_TIMEOUT == WaitForSingleObject( th->th.th_info.ds.ds_thread, 0));
1444 __kmp_create_monitor( kmp_info_t *th )
1446 kmp_thread_t handle;
1448 int ideal, new_ideal;
1449 int caller_gtid = __kmp_get_gtid();
1451 KA_TRACE( 10, (
"__kmp_create_monitor: try to create monitor\n" ) );
1455 __kmp_monitor_ev = CreateEvent( NULL, TRUE, FALSE, NULL );
1456 if ( __kmp_monitor_ev == NULL ) {
1457 DWORD error = GetLastError();
1460 KMP_MSG( CantCreateEvent ),
1466 __kmp_itt_system_object_created( __kmp_monitor_ev,
"Event" );
1469 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1470 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1474 if ( __kmp_monitor_stksize == 0 ) {
1475 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1477 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1478 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1481 KA_TRACE( 10, (
"__kmp_create_monitor: requested stacksize = %d bytes\n",
1482 (
int) __kmp_monitor_stksize ) );
1484 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
1486 handle = CreateThread( NULL, (SIZE_T) __kmp_monitor_stksize,
1487 (LPTHREAD_START_ROUTINE) __kmp_launch_monitor,
1488 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1490 DWORD error = GetLastError();
1493 KMP_MSG( CantCreateThread ),
1499 th->th.th_info.ds.ds_thread = handle;
1503 KA_TRACE( 10, (
"__kmp_create_monitor: monitor created %p\n",
1504 (
void *) th->th.th_info.ds.ds_thread ) );
1517 __kmp_is_thread_alive( kmp_info_t * th, DWORD *exit_val )
1520 rc = GetExitCodeThread( th->th.th_info.ds.ds_thread, exit_val );
1522 DWORD error = GetLastError();
1525 KMP_MSG( FunctionError,
"GetExitCodeThread()" ),
1530 return ( *exit_val == STILL_ACTIVE );
1538 ExitThread( exit_status );
1545 __kmp_reap_common( kmp_info_t * th )
1551 KA_TRACE( 10, (
"__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid ) );
1571 register kmp_uint32 spins;
1573 KMP_FSYNC_SPIN_INIT( obj, (
void*) & th->th.th_info.ds.ds_alive );
1575 KMP_INIT_YIELD( spins );
1578 KMP_FSYNC_SPIN_PREPARE( obj );
1580 __kmp_is_thread_alive( th, &exit_val );
1581 KMP_YIELD( TCR_4(__kmp_nth) > __kmp_avail_proc );
1582 KMP_YIELD_SPIN( spins );
1583 }
while ( exit_val == STILL_ACTIVE && TCR_4( th->th.th_info.ds.ds_alive ) );
1585 if ( exit_val == STILL_ACTIVE ) {
1586 KMP_FSYNC_CANCEL( obj );
1588 KMP_FSYNC_SPIN_ACQUIRED( obj );
1593 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1600 if ( exit_val == STILL_ACTIVE ) {
1601 KA_TRACE( 1, (
"__kmp_reap_common: thread still active.\n" ) );
1602 }
else if ( (
void *) exit_val != (
void *) th) {
1603 KA_TRACE( 1, (
"__kmp_reap_common: ExitProcess / TerminateThread used?\n" ) );
1608 "__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
"\n",
1609 th->th.th_info.ds.ds_gtid,
1610 th->th.th_info.ds.ds_thread
1614 th->th.th_info.ds.ds_thread = 0;
1615 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1616 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1617 th->th.th_info.ds.ds_thread_id = 0;
1623 __kmp_reap_monitor( kmp_info_t *th )
1627 KA_TRACE( 10, (
"__kmp_reap_monitor: try to reap %p\n",
1628 (
void *) th->th.th_info.ds.ds_thread ) );
1633 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1634 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
1640 status = SetEvent( __kmp_monitor_ev );
1641 if ( status == FALSE ) {
1642 DWORD error = GetLastError();
1645 KMP_MSG( CantSetEvent ),
1650 KA_TRACE( 10, (
"__kmp_reap_monitor: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1651 __kmp_reap_common( th );
1653 __kmp_free_handle( __kmp_monitor_ev );
1659 __kmp_reap_worker( kmp_info_t * th )
1661 KA_TRACE( 10, (
"__kmp_reap_worker: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1662 __kmp_reap_common( th );
1668 #if KMP_HANDLE_SIGNALS
1672 __kmp_team_handler(
int signo )
1674 if ( __kmp_global.g.g_abort == 0 ) {
1676 if ( __kmp_debug_buf ) {
1677 __kmp_dump_debug_buffer();
1680 TCW_4( __kmp_global.g.g_abort, signo );
1682 TCW_4( __kmp_global.g.g_done, TRUE );
1690 sig_func_t __kmp_signal(
int signum, sig_func_t handler ) {
1691 sig_func_t old = signal( signum, handler );
1692 if ( old == SIG_ERR ) {
1694 __kmp_msg( kmp_ms_fatal, KMP_MSG( FunctionError,
"signal" ), KMP_ERR( error ), __kmp_msg_null );
1700 __kmp_install_one_handler(
1707 KB_TRACE( 60, (
"__kmp_install_one_handler: called: sig=%d\n", sig ) );
1708 if ( parallel_init ) {
1709 old = __kmp_signal( sig, handler );
1711 if ( old == __kmp_sighldrs[ sig ] ) {
1712 __kmp_siginstalled[ sig ] = 1;
1715 old = __kmp_signal( sig, old );
1721 old = __kmp_signal( sig, SIG_DFL );
1722 __kmp_sighldrs[ sig ] = old;
1723 __kmp_signal( sig, old );
1729 __kmp_remove_one_handler(
int sig ) {
1730 if ( __kmp_siginstalled[ sig ] ) {
1733 KB_TRACE( 60, (
"__kmp_remove_one_handler: called: sig=%d\n", sig ) );
1734 old = __kmp_signal( sig, __kmp_sighldrs[ sig ] );
1735 if ( old != __kmp_team_handler ) {
1736 KB_TRACE( 10, (
"__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1737 old = __kmp_signal( sig, old );
1739 __kmp_sighldrs[ sig ] = NULL;
1740 __kmp_siginstalled[ sig ] = 0;
1747 __kmp_install_signals(
int parallel_init )
1749 KB_TRACE( 10, (
"__kmp_install_signals: called\n" ) );
1750 if ( ! __kmp_handle_signals ) {
1751 KB_TRACE( 10, (
"__kmp_install_signals: KMP_HANDLE_SIGNALS is false - handlers not installed\n" ) );
1754 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1755 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1756 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1757 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1758 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1759 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1764 __kmp_remove_signals(
void )
1767 KB_TRACE( 10, (
"__kmp_remove_signals: called\n" ) );
1768 for ( sig = 1; sig < NSIG; ++ sig ) {
1769 __kmp_remove_one_handler( sig );
1774 #endif // KMP_HANDLE_SIGNALS
1778 __kmp_thread_sleep(
int millis )
1782 status = SleepEx( (DWORD) millis, FALSE );
1784 DWORD error = GetLastError();
1787 KMP_MSG( FunctionError,
"SleepEx()" ),
1796 __kmp_is_address_mapped(
void * addr )
1799 MEMORY_BASIC_INFORMATION lpBuffer;
1802 dwLength =
sizeof(MEMORY_BASIC_INFORMATION);
1804 status = VirtualQuery( addr, &lpBuffer, dwLength );
1806 return !((( lpBuffer.State == MEM_RESERVE) || ( lpBuffer.State == MEM_FREE )) ||
1807 (( lpBuffer.Protect == PAGE_NOACCESS ) || ( lpBuffer.Protect == PAGE_EXECUTE )));
1811 __kmp_hardware_timestamp(
void)
1815 QueryPerformanceCounter((LARGE_INTEGER*) &r);
1821 __kmp_free_handle( kmp_thread_t tHandle )
1825 rc = CloseHandle( tHandle );
1827 DWORD error = GetLastError();
1830 KMP_MSG( CantCloseHandle ),
1838 __kmp_get_load_balance(
int max ) {
1840 static ULONG glb_buff_size = 100 * 1024;
1842 static int glb_running_threads = 0;
1843 static double glb_call_time = 0;
1845 int running_threads = 0;
1846 NTSTATUS status = 0;
1847 ULONG buff_size = 0;
1848 ULONG info_size = 0;
1849 void * buffer = NULL;
1850 PSYSTEM_PROCESS_INFORMATION spi = NULL;
1853 double call_time = 0.0;
1855 __kmp_elapsed( & call_time );
1857 if ( glb_call_time &&
1858 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
1859 running_threads = glb_running_threads;
1862 glb_call_time = call_time;
1865 if ( NtQuerySystemInformation == NULL ) {
1866 running_threads = -1;
1877 buff_size = glb_buff_size;
1879 buff_size = 2 * buff_size;
1882 buffer = KMP_INTERNAL_REALLOC( buffer, buff_size );
1883 if ( buffer == NULL ) {
1884 running_threads = -1;
1887 status = NtQuerySystemInformation( SystemProcessInformation, buffer, buff_size, & info_size );
1890 }
while ( status == STATUS_INFO_LENGTH_MISMATCH );
1891 glb_buff_size = buff_size;
1893 #define CHECK( cond ) \
1895 KMP_DEBUG_ASSERT( cond ); \
1896 if ( ! ( cond ) ) { \
1897 running_threads = -1; \
1902 CHECK( buff_size >= info_size );
1903 spi = PSYSTEM_PROCESS_INFORMATION( buffer );
1905 ptrdiff_t offset = uintptr_t( spi ) - uintptr_t( buffer );
1906 CHECK( 0 <= offset && offset +
sizeof( SYSTEM_PROCESS_INFORMATION ) < info_size );
1907 HANDLE pid = spi->ProcessId;
1908 ULONG num = spi->NumberOfThreads;
1910 size_t spi_size =
sizeof( SYSTEM_PROCESS_INFORMATION ) +
sizeof( SYSTEM_THREAD ) * ( num - 1 );
1911 CHECK( offset + spi_size < info_size );
1912 if ( spi->NextEntryOffset != 0 ) {
1913 CHECK( spi_size <= spi->NextEntryOffset );
1918 for (
int i = 0; i < num; ++ i ) {
1919 THREAD_STATE state = spi->Threads[ i ].State;
1922 if ( state == StateRunning ) {
1926 if ( running_threads >= max ) {
1932 if ( spi->NextEntryOffset == 0 ) {
1935 spi = PSYSTEM_PROCESS_INFORMATION( uintptr_t( spi ) + spi->NextEntryOffset );
1942 if ( buffer != NULL ) {
1943 KMP_INTERNAL_FREE( buffer );
1946 glb_running_threads = running_threads;
1948 return running_threads;