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 if (!flag || flag->get_type() != flag->get_ptr_type()) {
464 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p)\n",
465 gtid, target_gtid, NULL ) );
466 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
470 typename C::flag_t old_spin = flag->unset_sleeping();
471 if ( !flag->is_sleeping_val(old_spin) ) {
472 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p): " 474 gtid, target_gtid, flag->get(), old_spin, *(flag->get()) ) );
475 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
479 TCW_PTR(th->th.th_sleep_loc, NULL);
481 KF_TRACE( 5, (
"__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p)\n",
482 gtid, target_gtid, flag->get() ) );
485 __kmp_win32_cond_signal( &th->th.th_suspend_cv );
486 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
488 KF_TRACE( 30, (
"__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
489 gtid, target_gtid ) );
492 void __kmp_resume_32(
int target_gtid, kmp_flag_32 *flag) {
493 __kmp_resume_template(target_gtid, flag);
495 void __kmp_resume_64(
int target_gtid, kmp_flag_64 *flag) {
496 __kmp_resume_template(target_gtid, flag);
498 void __kmp_resume_oncore(
int target_gtid, kmp_flag_oncore *flag) {
499 __kmp_resume_template(target_gtid, flag);
507 __kmp_yield(
int cond )
517 __kmp_gtid_set_specific(
int gtid )
519 KA_TRACE( 50, (
"__kmp_gtid_set_specific: T#%d key:%d\n",
520 gtid, __kmp_gtid_threadprivate_key ));
521 KMP_ASSERT( __kmp_init_runtime );
522 if( ! TlsSetValue( __kmp_gtid_threadprivate_key, (LPVOID)(gtid+1)) )
523 KMP_FATAL( TLSSetValueFailed );
527 __kmp_gtid_get_specific()
530 if( !__kmp_init_runtime ) {
531 KA_TRACE( 50, (
"__kmp_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
532 return KMP_GTID_SHUTDOWN;
534 gtid = (int)(kmp_intptr_t)TlsGetValue( __kmp_gtid_threadprivate_key );
541 KA_TRACE( 50, (
"__kmp_gtid_get_specific: key:%d gtid:%d\n",
542 __kmp_gtid_threadprivate_key, gtid ));
549 #if KMP_GROUP_AFFINITY 556 __kmp_get_proc_group( kmp_affin_mask_t
const *mask )
560 for (i = 0; i < __kmp_num_proc_groups; i++) {
575 __kmp_set_system_affinity( kmp_affin_mask_t
const *mask,
int abort_on_error )
578 #if KMP_GROUP_AFFINITY 580 if (__kmp_num_proc_groups > 1) {
585 int group = __kmp_get_proc_group( mask );
587 if (abort_on_error) {
588 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
598 ga.Mask = mask[group];
599 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
601 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
602 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
603 DWORD error = GetLastError();
604 if (abort_on_error) {
607 KMP_MSG( CantSetThreadAffMask ),
620 if (!SetThreadAffinityMask( GetCurrentThread(), *mask )) {
621 DWORD error = GetLastError();
622 if (abort_on_error) {
625 KMP_MSG( CantSetThreadAffMask ),
637 __kmp_get_system_affinity( kmp_affin_mask_t *mask,
int abort_on_error )
640 #if KMP_GROUP_AFFINITY 642 if (__kmp_num_proc_groups > 1) {
645 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
647 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
648 DWORD error = GetLastError();
649 if (abort_on_error) {
652 KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
660 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups)
665 mask[ga.Group] = ga.Mask;
672 kmp_affin_mask_t newMask, sysMask, retval;
674 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
675 DWORD error = GetLastError();
676 if (abort_on_error) {
679 KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
686 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
688 DWORD error = GetLastError();
689 if (abort_on_error) {
692 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
699 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
701 DWORD error = GetLastError();
702 if (abort_on_error) {
705 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
717 __kmp_affinity_bind_thread(
int proc )
720 #if KMP_GROUP_AFFINITY 722 if (__kmp_num_proc_groups > 1) {
728 KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups
729 * CHAR_BIT *
sizeof(DWORD_PTR))));
730 ga.Group = proc / (CHAR_BIT *
sizeof(DWORD_PTR));
731 ga.Mask = (
unsigned long long)1 << (proc % (CHAR_BIT *
sizeof(DWORD_PTR)));
732 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
734 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
735 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
736 DWORD error = GetLastError();
737 if (__kmp_affinity_verbose) {
740 KMP_MSG( CantSetThreadAffMask ),
752 kmp_affin_mask_t mask;
754 KMP_CPU_SET(proc, &mask);
755 __kmp_set_system_affinity(&mask, TRUE);
760 __kmp_affinity_determine_capable(
const char *env_var )
766 #if KMP_GROUP_AFFINITY 767 KMP_AFFINITY_ENABLE(__kmp_num_proc_groups*
sizeof(kmp_affin_mask_t));
769 KMP_AFFINITY_ENABLE(
sizeof(kmp_affin_mask_t));
773 "__kmp_affinity_determine_capable: " 774 "Windows* OS affinity interface functional (mask size = %" KMP_SIZE_T_SPEC
").\n",
775 __kmp_affin_mask_size
780 __kmp_read_cpu_time(
void )
782 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
788 status = GetProcessTimes( GetCurrentProcess(), &CreationTime,
789 &ExitTime, &KernelTime, &UserTime );
794 sec += KernelTime.dwHighDateTime;
795 sec += UserTime.dwHighDateTime;
798 sec *= (double) (1 << 16) * (double) (1 << 16);
800 sec += KernelTime.dwLowDateTime;
801 sec += UserTime.dwLowDateTime;
803 cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC;
810 __kmp_read_system_info(
struct kmp_sys_info *info )
829 __kmp_runtime_initialize(
void )
835 if ( __kmp_init_runtime ) {
843 UINT err_mode = SetErrorMode (SEM_FAILCRITICALERRORS);
845 BOOL ret = GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
846 |GET_MODULE_HANDLE_EX_FLAG_PIN,
847 (LPCTSTR)&__kmp_serial_initialize, &h);
848 KMP_DEBUG_ASSERT2(h && ret,
"OpenMP RTL cannot find itself loaded");
849 SetErrorMode (err_mode);
850 KA_TRACE( 10, (
"__kmp_runtime_initialize: dynamic library pinned\n") );
854 InitializeCriticalSection( & __kmp_win32_section );
856 __kmp_itt_system_object_created( & __kmp_win32_section,
"Critical Section" );
858 __kmp_initialize_system_tick();
860 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) 861 if ( ! __kmp_cpuinfo.initialized ) {
862 __kmp_query_cpuid( & __kmp_cpuinfo );
867 #if KMP_OS_WINDOWS && ! defined KMP_DYNAMIC_LIB 882 __kmp_tls_gtid_min = 0;
884 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
888 if ( !__kmp_gtid_threadprivate_key ) {
889 __kmp_gtid_threadprivate_key = TlsAlloc();
890 if( __kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES ) {
891 KMP_FATAL( TLSOutOfIndexes );
906 __kmp_str_buf_init( & path );
907 path_size = GetSystemDirectory( path.str, path.size );
908 KMP_DEBUG_ASSERT( path_size > 0 );
909 if ( path_size >= path.size ) {
913 __kmp_str_buf_reserve( & path, path_size );
914 path_size = GetSystemDirectory( path.str, path.size );
915 KMP_DEBUG_ASSERT( path_size > 0 );
917 if ( path_size > 0 && path_size < path.size ) {
922 path.used = path_size;
923 __kmp_str_buf_print( & path,
"\\%s",
"ntdll.dll" );
928 ntdll = GetModuleHandle( path.str );
931 KMP_DEBUG_ASSERT( ntdll != NULL );
932 if ( ntdll != NULL ) {
933 NtQuerySystemInformation = (NtQuerySystemInformation_t) GetProcAddress( ntdll,
"NtQuerySystemInformation" );
935 KMP_DEBUG_ASSERT( NtQuerySystemInformation != NULL );
937 #if KMP_GROUP_AFFINITY 942 if ( path_size > 0 && path_size < path.size ) {
947 path.used = path_size;
948 __kmp_str_buf_print( & path,
"\\%s",
"kernel32.dll" );
953 kernel32 = GetModuleHandle( path.str );
954 KA_TRACE( 10, (
"__kmp_runtime_initialize: kernel32.dll = %s\n", path.str ) );
960 if ( kernel32 != NULL ) {
961 __kmp_GetActiveProcessorCount = (kmp_GetActiveProcessorCount_t) GetProcAddress( kernel32,
"GetActiveProcessorCount" );
962 __kmp_GetActiveProcessorGroupCount = (kmp_GetActiveProcessorGroupCount_t) GetProcAddress( kernel32,
"GetActiveProcessorGroupCount" );
963 __kmp_GetThreadGroupAffinity = (kmp_GetThreadGroupAffinity_t) GetProcAddress( kernel32,
"GetThreadGroupAffinity" );
964 __kmp_SetThreadGroupAffinity = (kmp_SetThreadGroupAffinity_t) GetProcAddress( kernel32,
"SetThreadGroupAffinity" );
966 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorCount = %p\n", __kmp_GetActiveProcessorCount ) );
967 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorGroupCount = %p\n", __kmp_GetActiveProcessorGroupCount ) );
968 KA_TRACE( 10, (
"__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity = %p\n", __kmp_GetThreadGroupAffinity ) );
969 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity = %p\n", __kmp_SetThreadGroupAffinity ) );
970 KA_TRACE( 10, (
"__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
sizeof(kmp_affin_mask_t) ) );
979 if ( ( __kmp_GetActiveProcessorCount != NULL )
980 && ( __kmp_GetActiveProcessorGroupCount != NULL )
981 && ( __kmp_GetThreadGroupAffinity != NULL )
982 && ( __kmp_SetThreadGroupAffinity != NULL )
983 && ( ( __kmp_num_proc_groups
984 = __kmp_GetActiveProcessorGroupCount() ) > 1 ) ) {
990 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
994 for ( i = 0; i < __kmp_num_proc_groups; i++ ) {
995 DWORD size = __kmp_GetActiveProcessorCount( i );
997 KA_TRACE( 10, (
"__kmp_runtime_initialize: proc group %d size = %d\n", i, size ) );
1001 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
1005 if ( __kmp_num_proc_groups <= 1 ) {
1006 GetSystemInfo( & info );
1007 __kmp_xproc = info.dwNumberOfProcessors;
1010 GetSystemInfo( & info );
1011 __kmp_xproc = info.dwNumberOfProcessors;
1018 if ( __kmp_xproc <= 0 ) {
1022 KA_TRACE( 5, (
"__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc) );
1024 __kmp_str_buf_free( & path );
1027 __kmp_itt_initialize();
1030 __kmp_init_runtime = TRUE;
1034 __kmp_runtime_destroy(
void )
1036 if ( ! __kmp_init_runtime ) {
1041 __kmp_itt_destroy();
1046 KA_TRACE( 40, (
"__kmp_runtime_destroy\n" ));
1048 if( __kmp_gtid_threadprivate_key ) {
1049 TlsFree( __kmp_gtid_threadprivate_key );
1050 __kmp_gtid_threadprivate_key = 0;
1053 __kmp_affinity_uninitialize();
1054 DeleteCriticalSection( & __kmp_win32_section );
1057 NtQuerySystemInformation = NULL;
1061 __kmp_GetActiveProcessorCount = NULL;
1062 __kmp_GetActiveProcessorGroupCount = NULL;
1063 __kmp_GetThreadGroupAffinity = NULL;
1064 __kmp_SetThreadGroupAffinity = NULL;
1065 #endif // KMP_ARCH_X86_64 1067 __kmp_init_runtime = FALSE;
1072 __kmp_terminate_thread(
int gtid )
1074 kmp_info_t *th = __kmp_threads[ gtid ];
1078 KA_TRACE( 10, (
"__kmp_terminate_thread: kill (%d)\n", gtid ) );
1080 if (TerminateThread( th->th.th_info.ds.ds_thread, (DWORD) -1) == FALSE) {
1083 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1090 __kmp_clear_system_time(
void )
1094 status = QueryPerformanceCounter( & time );
1095 __kmp_win32_time = (kmp_int64) time.QuadPart;
1099 __kmp_initialize_system_tick(
void )
1105 status = QueryPerformanceFrequency( & freq );
1107 DWORD error = GetLastError();
1110 KMP_MSG( FunctionError,
"QueryPerformanceFrequency()" ),
1117 __kmp_win32_tick = ((double) 1.0) / (double) freq.QuadPart;
1125 __kmp_elapsed(
double *t )
1129 status = QueryPerformanceCounter( & now );
1130 *t = ((double) now.QuadPart) * __kmp_win32_tick;
1136 __kmp_elapsed_tick(
double *t )
1138 *t = __kmp_win32_tick;
1142 __kmp_read_system_time(
double *delta )
1145 if (delta != NULL) {
1149 status = QueryPerformanceCounter( & now );
1151 *delta = ((double) (((kmp_int64) now.QuadPart) - __kmp_win32_time))
1160 __kmp_launch_worker(
void *arg )
1162 volatile void *stack_data;
1165 kmp_info_t *this_thr = (kmp_info_t *) arg;
1168 gtid = this_thr->th.th_info.ds.ds_gtid;
1169 __kmp_gtid_set_specific( gtid );
1170 #ifdef KMP_TDATA_GTID 1171 #error "This define causes problems with LoadLibrary() + declspec(thread) " \ 1172 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \ 1173 "reference: http://support.microsoft.com/kb/118816" 1178 __kmp_itt_thread_name( gtid );
1181 __kmp_affinity_set_init_mask( gtid, FALSE );
1183 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 1188 __kmp_clear_x87_fpu_status_word();
1189 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
1190 __kmp_load_mxcsr( &__kmp_init_mxcsr );
1193 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
1194 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
1197 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1198 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1199 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1201 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1202 TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
1203 KMP_ASSERT( this_thr -> th.th_info.ds.ds_stackgrow == FALSE );
1204 __kmp_check_stack_overlap( this_thr );
1207 exit_val = __kmp_launch_thread( this_thr );
1208 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1209 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1218 __kmp_launch_monitor(
void *arg )
1221 kmp_thread_t monitor;
1224 kmp_info_t *this_thr = (kmp_info_t *) arg;
1226 KMP_DEBUG_ASSERT(__kmp_init_monitor);
1227 TCW_4( __kmp_init_monitor, 2 );
1229 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1230 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1233 KA_TRACE( 10, (
"__kmp_launch_monitor: launched\n" ) );
1235 monitor = GetCurrentThread();
1238 status = SetThreadPriority( monitor, THREAD_PRIORITY_HIGHEST );
1240 DWORD error = GetLastError();
1243 KMP_MSG( CantSetThreadPriority ),
1250 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
1251 #ifdef KMP_TDATA_GTID 1252 #error "This define causes problems with LoadLibrary() + declspec(thread) " \ 1253 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \ 1254 "reference: http://support.microsoft.com/kb/118816" 1259 __kmp_itt_thread_ignore();
1264 interval = ( 1000 / __kmp_monitor_wakeups );
1266 while (! TCR_4(__kmp_global.g.g_done)) {
1269 KA_TRACE( 15, (
"__kmp_launch_monitor: update\n" ) );
1271 wait_status = WaitForSingleObject( __kmp_monitor_ev, interval );
1273 if (wait_status == WAIT_TIMEOUT) {
1274 TCW_4( __kmp_global.g.g_time.dt.t_value,
1275 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
1281 KA_TRACE( 10, (
"__kmp_launch_monitor: finished\n" ) );
1283 status = SetThreadPriority( monitor, THREAD_PRIORITY_NORMAL );
1285 DWORD error = GetLastError();
1288 KMP_MSG( CantSetThreadPriority ),
1294 if (__kmp_global.g.g_abort != 0) {
1300 KA_TRACE( 10, (
"__kmp_launch_monitor: terminate sig=%d\n", (__kmp_global.g.g_abort) ) );
1305 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1306 __kmp_terminate_thread( gtid );
1312 KA_TRACE( 10, (
"__kmp_launch_monitor: raise sig=%d\n", (__kmp_global.g.g_abort) ) );
1314 if (__kmp_global.g.g_abort > 0) {
1315 raise( __kmp_global.g.g_abort );
1319 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1326 __kmp_create_worker(
int gtid, kmp_info_t *th,
size_t stack_size )
1328 kmp_thread_t handle;
1331 KA_TRACE( 10, (
"__kmp_create_worker: try to create thread (%d)\n", gtid ) );
1333 th->th.th_info.ds.ds_gtid = gtid;
1335 if ( KMP_UBER_GTID(gtid) ) {
1344 rc = DuplicateHandle(
1345 GetCurrentProcess(),
1347 GetCurrentProcess(),
1348 &th->th.th_info.ds.ds_thread,
1351 DUPLICATE_SAME_ACCESS
1354 KA_TRACE( 10, (
" __kmp_create_worker: ROOT Handle duplicated, th = %p, handle = %" KMP_UINTPTR_SPEC
"\n",
1356 th->th.th_info.ds.ds_thread ) );
1357 th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1359 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1361 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1362 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1363 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1364 __kmp_check_stack_overlap( th );
1371 KA_TRACE( 10, (
"__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC
1372 " bytes\n", stack_size ) );
1374 stack_size += gtid * __kmp_stkoffset;
1376 TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1377 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
1379 KA_TRACE( 10, (
"__kmp_create_worker: (before) stack_size = %" 1381 " bytes, &__kmp_launch_worker = %p, th = %p, " 1383 (SIZE_T) stack_size,
1384 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1385 (LPVOID) th, &idThread ) );
1388 handle = CreateThread( NULL, (SIZE_T) stack_size,
1389 (LPTHREAD_START_ROUTINE) __kmp_launch_worker,
1390 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1393 KA_TRACE( 10, (
"__kmp_create_worker: (after) stack_size = %" 1395 " bytes, &__kmp_launch_worker = %p, th = %p, " 1396 "idThread = %u, handle = %" KMP_UINTPTR_SPEC
"\n",
1397 (SIZE_T) stack_size,
1398 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1399 (LPVOID) th, idThread, handle ) );
1402 if ( handle == 0 ) {
1403 DWORD error = GetLastError();
1406 KMP_MSG( CantCreateThread ),
1411 th->th.th_info.ds.ds_thread = handle;
1417 KA_TRACE( 10, (
"__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1421 __kmp_still_running(kmp_info_t *th) {
1422 return (WAIT_TIMEOUT == WaitForSingleObject( th->th.th_info.ds.ds_thread, 0));
1426 __kmp_create_monitor( kmp_info_t *th )
1428 kmp_thread_t handle;
1430 int ideal, new_ideal;
1432 if( __kmp_dflt_blocktime == KMP_MAX_BLOCKTIME ) {
1434 KA_TRACE( 10, (
"__kmp_create_monitor: skipping monitor thread because of MAX blocktime\n" ) );
1435 th->th.th_info.ds.ds_tid = 0;
1436 th->th.th_info.ds.ds_gtid = 0;
1437 TCW_4( __kmp_init_monitor, 2 );
1440 KA_TRACE( 10, (
"__kmp_create_monitor: try to create monitor\n" ) );
1444 __kmp_monitor_ev = CreateEvent( NULL, TRUE, FALSE, NULL );
1445 if ( __kmp_monitor_ev == NULL ) {
1446 DWORD error = GetLastError();
1449 KMP_MSG( CantCreateEvent ),
1455 __kmp_itt_system_object_created( __kmp_monitor_ev,
"Event" );
1458 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1459 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1463 if ( __kmp_monitor_stksize == 0 ) {
1464 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1466 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1467 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1470 KA_TRACE( 10, (
"__kmp_create_monitor: requested stacksize = %d bytes\n",
1471 (
int) __kmp_monitor_stksize ) );
1473 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
1475 handle = CreateThread( NULL, (SIZE_T) __kmp_monitor_stksize,
1476 (LPTHREAD_START_ROUTINE) __kmp_launch_monitor,
1477 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1479 DWORD error = GetLastError();
1482 KMP_MSG( CantCreateThread ),
1488 th->th.th_info.ds.ds_thread = handle;
1492 KA_TRACE( 10, (
"__kmp_create_monitor: monitor created %p\n",
1493 (
void *) th->th.th_info.ds.ds_thread ) );
1506 __kmp_is_thread_alive( kmp_info_t * th, DWORD *exit_val )
1509 rc = GetExitCodeThread( th->th.th_info.ds.ds_thread, exit_val );
1511 DWORD error = GetLastError();
1514 KMP_MSG( FunctionError,
"GetExitCodeThread()" ),
1519 return ( *exit_val == STILL_ACTIVE );
1527 ExitThread( exit_status );
1534 __kmp_reap_common( kmp_info_t * th )
1540 KA_TRACE( 10, (
"__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid ) );
1560 register kmp_uint32 spins;
1562 KMP_FSYNC_SPIN_INIT( obj, (
void*) & th->th.th_info.ds.ds_alive );
1564 KMP_INIT_YIELD( spins );
1567 KMP_FSYNC_SPIN_PREPARE( obj );
1569 __kmp_is_thread_alive( th, &exit_val );
1570 KMP_YIELD( TCR_4(__kmp_nth) > __kmp_avail_proc );
1571 KMP_YIELD_SPIN( spins );
1572 }
while ( exit_val == STILL_ACTIVE && TCR_4( th->th.th_info.ds.ds_alive ) );
1574 if ( exit_val == STILL_ACTIVE ) {
1575 KMP_FSYNC_CANCEL( obj );
1577 KMP_FSYNC_SPIN_ACQUIRED( obj );
1582 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1589 if ( exit_val == STILL_ACTIVE ) {
1590 KA_TRACE( 1, (
"__kmp_reap_common: thread still active.\n" ) );
1591 }
else if ( (
void *) exit_val != (
void *) th) {
1592 KA_TRACE( 1, (
"__kmp_reap_common: ExitProcess / TerminateThread used?\n" ) );
1597 "__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
"\n",
1598 th->th.th_info.ds.ds_gtid,
1599 th->th.th_info.ds.ds_thread
1603 th->th.th_info.ds.ds_thread = 0;
1604 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1605 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1606 th->th.th_info.ds.ds_thread_id = 0;
1612 __kmp_reap_monitor( kmp_info_t *th )
1616 KA_TRACE( 10, (
"__kmp_reap_monitor: try to reap %p\n",
1617 (
void *) th->th.th_info.ds.ds_thread ) );
1622 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1623 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
1624 KA_TRACE( 10, (
"__kmp_reap_monitor: monitor did not start, returning\n") );
1630 status = SetEvent( __kmp_monitor_ev );
1631 if ( status == FALSE ) {
1632 DWORD error = GetLastError();
1635 KMP_MSG( CantSetEvent ),
1640 KA_TRACE( 10, (
"__kmp_reap_monitor: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1641 __kmp_reap_common( th );
1643 __kmp_free_handle( __kmp_monitor_ev );
1649 __kmp_reap_worker( kmp_info_t * th )
1651 KA_TRACE( 10, (
"__kmp_reap_worker: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1652 __kmp_reap_common( th );
1658 #if KMP_HANDLE_SIGNALS 1662 __kmp_team_handler(
int signo )
1664 if ( __kmp_global.g.g_abort == 0 ) {
1666 if ( __kmp_debug_buf ) {
1667 __kmp_dump_debug_buffer();
1670 TCW_4( __kmp_global.g.g_abort, signo );
1672 TCW_4( __kmp_global.g.g_done, TRUE );
1680 sig_func_t __kmp_signal(
int signum, sig_func_t handler ) {
1681 sig_func_t old = signal( signum, handler );
1682 if ( old == SIG_ERR ) {
1684 __kmp_msg( kmp_ms_fatal, KMP_MSG( FunctionError,
"signal" ), KMP_ERR( error ), __kmp_msg_null );
1690 __kmp_install_one_handler(
1697 KB_TRACE( 60, (
"__kmp_install_one_handler: called: sig=%d\n", sig ) );
1698 if ( parallel_init ) {
1699 old = __kmp_signal( sig, handler );
1701 if ( old == __kmp_sighldrs[ sig ] ) {
1702 __kmp_siginstalled[ sig ] = 1;
1705 old = __kmp_signal( sig, old );
1711 old = __kmp_signal( sig, SIG_DFL );
1712 __kmp_sighldrs[ sig ] = old;
1713 __kmp_signal( sig, old );
1719 __kmp_remove_one_handler(
int sig ) {
1720 if ( __kmp_siginstalled[ sig ] ) {
1723 KB_TRACE( 60, (
"__kmp_remove_one_handler: called: sig=%d\n", sig ) );
1724 old = __kmp_signal( sig, __kmp_sighldrs[ sig ] );
1725 if ( old != __kmp_team_handler ) {
1726 KB_TRACE( 10, (
"__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1727 old = __kmp_signal( sig, old );
1729 __kmp_sighldrs[ sig ] = NULL;
1730 __kmp_siginstalled[ sig ] = 0;
1737 __kmp_install_signals(
int parallel_init )
1739 KB_TRACE( 10, (
"__kmp_install_signals: called\n" ) );
1740 if ( ! __kmp_handle_signals ) {
1741 KB_TRACE( 10, (
"__kmp_install_signals: KMP_HANDLE_SIGNALS is false - handlers not installed\n" ) );
1744 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1745 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1746 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1747 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1748 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1749 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1754 __kmp_remove_signals(
void )
1757 KB_TRACE( 10, (
"__kmp_remove_signals: called\n" ) );
1758 for ( sig = 1; sig < NSIG; ++ sig ) {
1759 __kmp_remove_one_handler( sig );
1764 #endif // KMP_HANDLE_SIGNALS 1768 __kmp_thread_sleep(
int millis )
1772 status = SleepEx( (DWORD) millis, FALSE );
1774 DWORD error = GetLastError();
1777 KMP_MSG( FunctionError,
"SleepEx()" ),
1786 __kmp_is_address_mapped(
void * addr )
1789 MEMORY_BASIC_INFORMATION lpBuffer;
1792 dwLength =
sizeof(MEMORY_BASIC_INFORMATION);
1794 status = VirtualQuery( addr, &lpBuffer, dwLength );
1796 return !((( lpBuffer.State == MEM_RESERVE) || ( lpBuffer.State == MEM_FREE )) ||
1797 (( lpBuffer.Protect == PAGE_NOACCESS ) || ( lpBuffer.Protect == PAGE_EXECUTE )));
1801 __kmp_hardware_timestamp(
void)
1805 QueryPerformanceCounter((LARGE_INTEGER*) &r);
1811 __kmp_free_handle( kmp_thread_t tHandle )
1815 rc = CloseHandle( tHandle );
1817 DWORD error = GetLastError();
1820 KMP_MSG( CantCloseHandle ),
1828 __kmp_get_load_balance(
int max ) {
1830 static ULONG glb_buff_size = 100 * 1024;
1832 static int glb_running_threads = 0;
1833 static double glb_call_time = 0;
1835 int running_threads = 0;
1836 NTSTATUS status = 0;
1837 ULONG buff_size = 0;
1838 ULONG info_size = 0;
1839 void * buffer = NULL;
1840 PSYSTEM_PROCESS_INFORMATION spi = NULL;
1843 double call_time = 0.0;
1845 __kmp_elapsed( & call_time );
1847 if ( glb_call_time &&
1848 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
1849 running_threads = glb_running_threads;
1852 glb_call_time = call_time;
1855 if ( NtQuerySystemInformation == NULL ) {
1856 running_threads = -1;
1867 buff_size = glb_buff_size;
1869 buff_size = 2 * buff_size;
1872 buffer = KMP_INTERNAL_REALLOC( buffer, buff_size );
1873 if ( buffer == NULL ) {
1874 running_threads = -1;
1877 status = NtQuerySystemInformation( SystemProcessInformation, buffer, buff_size, & info_size );
1880 }
while ( status == STATUS_INFO_LENGTH_MISMATCH );
1881 glb_buff_size = buff_size;
1883 #define CHECK( cond ) \ 1885 KMP_DEBUG_ASSERT( cond ); \ 1886 if ( ! ( cond ) ) { \ 1887 running_threads = -1; \ 1892 CHECK( buff_size >= info_size );
1893 spi = PSYSTEM_PROCESS_INFORMATION( buffer );
1895 ptrdiff_t offset = uintptr_t( spi ) - uintptr_t( buffer );
1896 CHECK( 0 <= offset && offset +
sizeof( SYSTEM_PROCESS_INFORMATION ) < info_size );
1897 HANDLE pid = spi->ProcessId;
1898 ULONG num = spi->NumberOfThreads;
1900 size_t spi_size =
sizeof( SYSTEM_PROCESS_INFORMATION ) +
sizeof( SYSTEM_THREAD ) * ( num - 1 );
1901 CHECK( offset + spi_size < info_size );
1902 if ( spi->NextEntryOffset != 0 ) {
1903 CHECK( spi_size <= spi->NextEntryOffset );
1908 for (
int i = 0; i < num; ++ i ) {
1909 THREAD_STATE state = spi->Threads[ i ].State;
1912 if ( state == StateRunning ) {
1916 if ( running_threads >= max ) {
1922 if ( spi->NextEntryOffset == 0 ) {
1925 spi = PSYSTEM_PROCESS_INFORMATION( uintptr_t( spi ) + spi->NextEntryOffset );
1932 if ( buffer != NULL ) {
1933 KMP_INTERNAL_FREE( buffer );
1936 glb_running_threads = running_threads;
1938 return running_threads;