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);
462 static inline void __kmp_resume_template(
int target_gtid, C *flag )
464 kmp_info_t *th = __kmp_threads[target_gtid];
468 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
471 KF_TRACE( 30, (
"__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", gtid, target_gtid ) );
473 __kmp_suspend_initialize_thread( th );
474 __kmp_win32_mutex_lock( &th->th.th_suspend_mx );
477 flag = (C *)th->th.th_sleep_loc;
481 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p)\n",
482 gtid, target_gtid, NULL ) );
483 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
487 typename C::flag_t old_spin = flag->unset_sleeping();
488 if ( !flag->is_sleeping_val(old_spin) ) {
489 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p): "
491 gtid, target_gtid, flag->get(), old_spin, *(flag->get()) ) );
492 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
496 TCW_PTR(th->th.th_sleep_loc, NULL);
497 KF_TRACE( 5, (
"__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p)\n",
498 gtid, target_gtid, flag->get() ) );
501 __kmp_win32_cond_signal( &th->th.th_suspend_cv );
502 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
504 KF_TRACE( 30, (
"__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
505 gtid, target_gtid ) );
508 void __kmp_resume_32(
int target_gtid, kmp_flag_32 *flag) {
509 __kmp_resume_template(target_gtid, flag);
511 void __kmp_resume_64(
int target_gtid, kmp_flag_64 *flag) {
512 __kmp_resume_template(target_gtid, flag);
514 void __kmp_resume_oncore(
int target_gtid, kmp_flag_oncore *flag) {
515 __kmp_resume_template(target_gtid, flag);
522 __kmp_yield(
int cond )
532 __kmp_gtid_set_specific(
int gtid )
534 KA_TRACE( 50, (
"__kmp_gtid_set_specific: T#%d key:%d\n",
535 gtid, __kmp_gtid_threadprivate_key ));
536 KMP_ASSERT( __kmp_init_runtime );
537 if( ! TlsSetValue( __kmp_gtid_threadprivate_key, (LPVOID)(gtid+1)) )
538 KMP_FATAL( TLSSetValueFailed );
542 __kmp_gtid_get_specific()
545 if( !__kmp_init_runtime ) {
546 KA_TRACE( 50, (
"__kmp_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
547 return KMP_GTID_SHUTDOWN;
549 gtid = (int)(kmp_intptr_t)TlsGetValue( __kmp_gtid_threadprivate_key );
556 KA_TRACE( 50, (
"__kmp_gtid_get_specific: key:%d gtid:%d\n",
557 __kmp_gtid_threadprivate_key, gtid ));
564 #if KMP_GROUP_AFFINITY
571 __kmp_get_proc_group( kmp_affin_mask_t
const *mask )
575 for (i = 0; i < __kmp_num_proc_groups; i++) {
590 __kmp_set_system_affinity( kmp_affin_mask_t
const *mask,
int abort_on_error )
593 #if KMP_GROUP_AFFINITY
595 if (__kmp_num_proc_groups > 1) {
600 int group = __kmp_get_proc_group( mask );
602 if (abort_on_error) {
603 KMP_FATAL(AffinityInvalidMask,
"kmp_set_affinity");
613 ga.Mask = mask[group];
614 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
616 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
617 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
618 DWORD error = GetLastError();
619 if (abort_on_error) {
622 KMP_MSG( CantSetThreadAffMask ),
635 if (!SetThreadAffinityMask( GetCurrentThread(), *mask )) {
636 DWORD error = GetLastError();
637 if (abort_on_error) {
640 KMP_MSG( CantSetThreadAffMask ),
652 __kmp_get_system_affinity( kmp_affin_mask_t *mask,
int abort_on_error )
655 #if KMP_GROUP_AFFINITY
657 if (__kmp_num_proc_groups > 1) {
660 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
662 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
663 DWORD error = GetLastError();
664 if (abort_on_error) {
667 KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
675 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups)
680 mask[ga.Group] = ga.Mask;
687 kmp_affin_mask_t newMask, sysMask, retval;
689 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
690 DWORD error = GetLastError();
691 if (abort_on_error) {
694 KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
701 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
703 DWORD error = GetLastError();
704 if (abort_on_error) {
707 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
714 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
716 DWORD error = GetLastError();
717 if (abort_on_error) {
720 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
732 __kmp_affinity_bind_thread(
int proc )
735 #if KMP_GROUP_AFFINITY
737 if (__kmp_num_proc_groups > 1) {
743 KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups
744 * CHAR_BIT *
sizeof(DWORD_PTR))));
745 ga.Group = proc / (CHAR_BIT *
sizeof(DWORD_PTR));
746 ga.Mask = (
unsigned long long)1 << (proc % (CHAR_BIT *
sizeof(DWORD_PTR)));
747 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
749 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
750 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
751 DWORD error = GetLastError();
752 if (__kmp_affinity_verbose) {
755 KMP_MSG( CantSetThreadAffMask ),
767 kmp_affin_mask_t mask;
769 KMP_CPU_SET(proc, &mask);
770 __kmp_set_system_affinity(&mask, TRUE);
775 __kmp_affinity_determine_capable(
const char *env_var )
781 #if KMP_GROUP_AFFINITY
782 KMP_AFFINITY_ENABLE(__kmp_num_proc_groups*
sizeof(kmp_affin_mask_t));
784 KMP_AFFINITY_ENABLE(
sizeof(kmp_affin_mask_t));
788 "__kmp_affinity_determine_capable: "
789 "Windows* OS affinity interface functional (mask size = %" KMP_SIZE_T_SPEC
").\n",
790 __kmp_affin_mask_size
795 __kmp_read_cpu_time(
void )
797 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
803 status = GetProcessTimes( GetCurrentProcess(), &CreationTime,
804 &ExitTime, &KernelTime, &UserTime );
809 sec += KernelTime.dwHighDateTime;
810 sec += UserTime.dwHighDateTime;
813 sec *= (double) (1 << 16) * (double) (1 << 16);
815 sec += KernelTime.dwLowDateTime;
816 sec += UserTime.dwLowDateTime;
818 cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC;
825 __kmp_read_system_info(
struct kmp_sys_info *info )
844 __kmp_runtime_initialize(
void )
850 if ( __kmp_init_runtime ) {
858 UINT err_mode = SetErrorMode (SEM_FAILCRITICALERRORS);
860 BOOL ret = GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
861 |GET_MODULE_HANDLE_EX_FLAG_PIN,
862 (LPCTSTR)&__kmp_serial_initialize, &h);
863 KMP_DEBUG_ASSERT2(h && ret,
"OpenMP RTL cannot find itself loaded");
864 SetErrorMode (err_mode);
865 KA_TRACE( 10, (
"__kmp_runtime_initialize: dynamic library pinned\n") );
869 InitializeCriticalSection( & __kmp_win32_section );
871 __kmp_itt_system_object_created( & __kmp_win32_section,
"Critical Section" );
873 __kmp_initialize_system_tick();
875 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
876 if ( ! __kmp_cpuinfo.initialized ) {
877 __kmp_query_cpuid( & __kmp_cpuinfo );
882 #if KMP_OS_WINDOWS && ! defined KMP_DYNAMIC_LIB
897 __kmp_tls_gtid_min = 0;
899 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
903 if ( !__kmp_gtid_threadprivate_key ) {
904 __kmp_gtid_threadprivate_key = TlsAlloc();
905 if( __kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES ) {
906 KMP_FATAL( TLSOutOfIndexes );
921 __kmp_str_buf_init( & path );
922 path_size = GetSystemDirectory( path.str, path.size );
923 KMP_DEBUG_ASSERT( path_size > 0 );
924 if ( path_size >= path.size ) {
928 __kmp_str_buf_reserve( & path, path_size );
929 path_size = GetSystemDirectory( path.str, path.size );
930 KMP_DEBUG_ASSERT( path_size > 0 );
932 if ( path_size > 0 && path_size < path.size ) {
937 path.used = path_size;
938 __kmp_str_buf_print( & path,
"\\%s",
"ntdll.dll" );
943 ntdll = GetModuleHandle( path.str );
946 KMP_DEBUG_ASSERT( ntdll != NULL );
947 if ( ntdll != NULL ) {
948 NtQuerySystemInformation = (NtQuerySystemInformation_t) GetProcAddress( ntdll,
"NtQuerySystemInformation" );
950 KMP_DEBUG_ASSERT( NtQuerySystemInformation != NULL );
952 #if KMP_GROUP_AFFINITY
957 if ( path_size > 0 && path_size < path.size ) {
962 path.used = path_size;
963 __kmp_str_buf_print( & path,
"\\%s",
"kernel32.dll" );
968 kernel32 = GetModuleHandle( path.str );
969 KA_TRACE( 10, (
"__kmp_runtime_initialize: kernel32.dll = %s\n", path.str ) );
975 if ( kernel32 != NULL ) {
976 __kmp_GetActiveProcessorCount = (kmp_GetActiveProcessorCount_t) GetProcAddress( kernel32,
"GetActiveProcessorCount" );
977 __kmp_GetActiveProcessorGroupCount = (kmp_GetActiveProcessorGroupCount_t) GetProcAddress( kernel32,
"GetActiveProcessorGroupCount" );
978 __kmp_GetThreadGroupAffinity = (kmp_GetThreadGroupAffinity_t) GetProcAddress( kernel32,
"GetThreadGroupAffinity" );
979 __kmp_SetThreadGroupAffinity = (kmp_SetThreadGroupAffinity_t) GetProcAddress( kernel32,
"SetThreadGroupAffinity" );
981 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorCount = %p\n", __kmp_GetActiveProcessorCount ) );
982 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorGroupCount = %p\n", __kmp_GetActiveProcessorGroupCount ) );
983 KA_TRACE( 10, (
"__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity = %p\n", __kmp_GetThreadGroupAffinity ) );
984 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity = %p\n", __kmp_SetThreadGroupAffinity ) );
985 KA_TRACE( 10, (
"__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
sizeof(kmp_affin_mask_t) ) );
994 if ( ( __kmp_GetActiveProcessorCount != NULL )
995 && ( __kmp_GetActiveProcessorGroupCount != NULL )
996 && ( __kmp_GetThreadGroupAffinity != NULL )
997 && ( __kmp_SetThreadGroupAffinity != NULL )
998 && ( ( __kmp_num_proc_groups
999 = __kmp_GetActiveProcessorGroupCount() ) > 1 ) ) {
1005 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
1009 for ( i = 0; i < __kmp_num_proc_groups; i++ ) {
1010 DWORD size = __kmp_GetActiveProcessorCount( i );
1011 __kmp_xproc += size;
1012 KA_TRACE( 10, (
"__kmp_runtime_initialize: proc group %d size = %d\n", i, size ) );
1016 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
1020 if ( __kmp_num_proc_groups <= 1 ) {
1021 GetSystemInfo( & info );
1022 __kmp_xproc = info.dwNumberOfProcessors;
1025 GetSystemInfo( & info );
1026 __kmp_xproc = info.dwNumberOfProcessors;
1033 if ( __kmp_xproc <= 0 ) {
1037 KA_TRACE( 5, (
"__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc) );
1039 __kmp_str_buf_free( & path );
1042 __kmp_itt_initialize();
1045 __kmp_init_runtime = TRUE;
1049 __kmp_runtime_destroy(
void )
1051 if ( ! __kmp_init_runtime ) {
1056 __kmp_itt_destroy();
1061 KA_TRACE( 40, (
"__kmp_runtime_destroy\n" ));
1063 if( __kmp_gtid_threadprivate_key ) {
1064 TlsFree( __kmp_gtid_threadprivate_key );
1065 __kmp_gtid_threadprivate_key = 0;
1068 __kmp_affinity_uninitialize();
1069 DeleteCriticalSection( & __kmp_win32_section );
1072 NtQuerySystemInformation = NULL;
1076 __kmp_GetActiveProcessorCount = NULL;
1077 __kmp_GetActiveProcessorGroupCount = NULL;
1078 __kmp_GetThreadGroupAffinity = NULL;
1079 __kmp_SetThreadGroupAffinity = NULL;
1080 #endif // KMP_ARCH_X86_64
1082 __kmp_init_runtime = FALSE;
1087 __kmp_terminate_thread(
int gtid )
1089 kmp_info_t *th = __kmp_threads[ gtid ];
1093 KA_TRACE( 10, (
"__kmp_terminate_thread: kill (%d)\n", gtid ) );
1095 if (TerminateThread( th->th.th_info.ds.ds_thread, (DWORD) -1) == FALSE) {
1098 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1105 __kmp_clear_system_time(
void )
1109 status = QueryPerformanceCounter( & time );
1110 __kmp_win32_time = (kmp_int64) time.QuadPart;
1114 __kmp_initialize_system_tick(
void )
1120 status = QueryPerformanceFrequency( & freq );
1122 DWORD error = GetLastError();
1125 KMP_MSG( FunctionError,
"QueryPerformanceFrequency()" ),
1132 __kmp_win32_tick = ((double) 1.0) / (double) freq.QuadPart;
1140 __kmp_elapsed(
double *t )
1144 status = QueryPerformanceCounter( & now );
1145 *t = ((double) now.QuadPart) * __kmp_win32_tick;
1151 __kmp_elapsed_tick(
double *t )
1153 *t = __kmp_win32_tick;
1157 __kmp_read_system_time(
double *delta )
1160 if (delta != NULL) {
1164 status = QueryPerformanceCounter( & now );
1166 *delta = ((double) (((kmp_int64) now.QuadPart) - __kmp_win32_time))
1175 __kmp_launch_worker(
void *arg )
1177 volatile void *stack_data;
1180 kmp_info_t *this_thr = (kmp_info_t *) arg;
1183 gtid = this_thr->th.th_info.ds.ds_gtid;
1184 __kmp_gtid_set_specific( gtid );
1185 #ifdef KMP_TDATA_GTID
1186 #error "This define causes problems with LoadLibrary() + declspec(thread) " \
1187 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
1188 "reference: http://support.microsoft.com/kb/118816"
1193 __kmp_itt_thread_name( gtid );
1196 __kmp_affinity_set_init_mask( gtid, FALSE );
1198 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
1203 __kmp_clear_x87_fpu_status_word();
1204 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
1205 __kmp_load_mxcsr( &__kmp_init_mxcsr );
1208 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
1209 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
1212 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1213 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1214 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1216 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1217 TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
1218 KMP_ASSERT( this_thr -> th.th_info.ds.ds_stackgrow == FALSE );
1219 __kmp_check_stack_overlap( this_thr );
1222 exit_val = __kmp_launch_thread( this_thr );
1223 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1224 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1233 __kmp_launch_monitor(
void *arg )
1236 kmp_thread_t monitor;
1239 kmp_info_t *this_thr = (kmp_info_t *) arg;
1241 KMP_DEBUG_ASSERT(__kmp_init_monitor);
1242 TCW_4( __kmp_init_monitor, 2 );
1244 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1245 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1248 KA_TRACE( 10, (
"__kmp_launch_monitor: launched\n" ) );
1250 monitor = GetCurrentThread();
1253 status = SetThreadPriority( monitor, THREAD_PRIORITY_HIGHEST );
1255 DWORD error = GetLastError();
1258 KMP_MSG( CantSetThreadPriority ),
1265 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
1266 #ifdef KMP_TDATA_GTID
1267 #error "This define causes problems with LoadLibrary() + declspec(thread) " \
1268 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
1269 "reference: http://support.microsoft.com/kb/118816"
1274 __kmp_itt_thread_ignore();
1279 interval = ( 1000 / __kmp_monitor_wakeups );
1281 while (! TCR_4(__kmp_global.g.g_done)) {
1284 KA_TRACE( 15, (
"__kmp_launch_monitor: update\n" ) );
1286 wait_status = WaitForSingleObject( __kmp_monitor_ev, interval );
1288 if (wait_status == WAIT_TIMEOUT) {
1289 TCW_4( __kmp_global.g.g_time.dt.t_value,
1290 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
1296 KA_TRACE( 10, (
"__kmp_launch_monitor: finished\n" ) );
1298 status = SetThreadPriority( monitor, THREAD_PRIORITY_NORMAL );
1300 DWORD error = GetLastError();
1303 KMP_MSG( CantSetThreadPriority ),
1309 if (__kmp_global.g.g_abort != 0) {
1315 KA_TRACE( 10, (
"__kmp_launch_monitor: terminate sig=%d\n", (__kmp_global.g.g_abort) ) );
1320 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1321 __kmp_terminate_thread( gtid );
1327 KA_TRACE( 10, (
"__kmp_launch_monitor: raise sig=%d\n", (__kmp_global.g.g_abort) ) );
1329 if (__kmp_global.g.g_abort > 0) {
1330 raise( __kmp_global.g.g_abort );
1334 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1341 __kmp_create_worker(
int gtid, kmp_info_t *th,
size_t stack_size )
1343 kmp_thread_t handle;
1346 KA_TRACE( 10, (
"__kmp_create_worker: try to create thread (%d)\n", gtid ) );
1348 th->th.th_info.ds.ds_gtid = gtid;
1350 if ( KMP_UBER_GTID(gtid) ) {
1359 rc = DuplicateHandle(
1360 GetCurrentProcess(),
1362 GetCurrentProcess(),
1363 &th->th.th_info.ds.ds_thread,
1366 DUPLICATE_SAME_ACCESS
1369 KA_TRACE( 10, (
" __kmp_create_worker: ROOT Handle duplicated, th = %p, handle = %" KMP_UINTPTR_SPEC
"\n",
1371 th->th.th_info.ds.ds_thread ) );
1372 th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1374 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1376 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1377 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1378 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1379 __kmp_check_stack_overlap( th );
1386 KA_TRACE( 10, (
"__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC
1387 " bytes\n", stack_size ) );
1389 stack_size += gtid * __kmp_stkoffset;
1391 TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1392 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
1394 KA_TRACE( 10, (
"__kmp_create_worker: (before) stack_size = %"
1396 " bytes, &__kmp_launch_worker = %p, th = %p, "
1398 (SIZE_T) stack_size,
1399 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1400 (LPVOID) th, &idThread ) );
1403 handle = CreateThread( NULL, (SIZE_T) stack_size,
1404 (LPTHREAD_START_ROUTINE) __kmp_launch_worker,
1405 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1408 KA_TRACE( 10, (
"__kmp_create_worker: (after) stack_size = %"
1410 " bytes, &__kmp_launch_worker = %p, th = %p, "
1411 "idThread = %u, handle = %" KMP_UINTPTR_SPEC
"\n",
1412 (SIZE_T) stack_size,
1413 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1414 (LPVOID) th, idThread, handle ) );
1417 if ( handle == 0 ) {
1418 DWORD error = GetLastError();
1421 KMP_MSG( CantCreateThread ),
1426 th->th.th_info.ds.ds_thread = handle;
1432 KA_TRACE( 10, (
"__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1436 __kmp_still_running(kmp_info_t *th) {
1437 return (WAIT_TIMEOUT == WaitForSingleObject( th->th.th_info.ds.ds_thread, 0));
1441 __kmp_create_monitor( kmp_info_t *th )
1443 kmp_thread_t handle;
1445 int ideal, new_ideal;
1446 int caller_gtid = __kmp_get_gtid();
1448 KA_TRACE( 10, (
"__kmp_create_monitor: try to create monitor\n" ) );
1452 __kmp_monitor_ev = CreateEvent( NULL, TRUE, FALSE, NULL );
1453 if ( __kmp_monitor_ev == NULL ) {
1454 DWORD error = GetLastError();
1457 KMP_MSG( CantCreateEvent ),
1463 __kmp_itt_system_object_created( __kmp_monitor_ev,
"Event" );
1466 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1467 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1471 if ( __kmp_monitor_stksize == 0 ) {
1472 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1474 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1475 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1478 KA_TRACE( 10, (
"__kmp_create_monitor: requested stacksize = %d bytes\n",
1479 (
int) __kmp_monitor_stksize ) );
1481 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
1483 handle = CreateThread( NULL, (SIZE_T) __kmp_monitor_stksize,
1484 (LPTHREAD_START_ROUTINE) __kmp_launch_monitor,
1485 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1487 DWORD error = GetLastError();
1490 KMP_MSG( CantCreateThread ),
1496 th->th.th_info.ds.ds_thread = handle;
1500 KA_TRACE( 10, (
"__kmp_create_monitor: monitor created %p\n",
1501 (
void *) th->th.th_info.ds.ds_thread ) );
1514 __kmp_is_thread_alive( kmp_info_t * th, DWORD *exit_val )
1517 rc = GetExitCodeThread( th->th.th_info.ds.ds_thread, exit_val );
1519 DWORD error = GetLastError();
1522 KMP_MSG( FunctionError,
"GetExitCodeThread()" ),
1527 return ( *exit_val == STILL_ACTIVE );
1535 ExitThread( exit_status );
1542 __kmp_reap_common( kmp_info_t * th )
1548 KA_TRACE( 10, (
"__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid ) );
1568 register kmp_uint32 spins;
1570 KMP_FSYNC_SPIN_INIT( obj, (
void*) & th->th.th_info.ds.ds_alive );
1572 KMP_INIT_YIELD( spins );
1575 KMP_FSYNC_SPIN_PREPARE( obj );
1577 __kmp_is_thread_alive( th, &exit_val );
1578 KMP_YIELD( TCR_4(__kmp_nth) > __kmp_avail_proc );
1579 KMP_YIELD_SPIN( spins );
1580 }
while ( exit_val == STILL_ACTIVE && TCR_4( th->th.th_info.ds.ds_alive ) );
1582 if ( exit_val == STILL_ACTIVE ) {
1583 KMP_FSYNC_CANCEL( obj );
1585 KMP_FSYNC_SPIN_ACQUIRED( obj );
1590 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1597 if ( exit_val == STILL_ACTIVE ) {
1598 KA_TRACE( 1, (
"__kmp_reap_common: thread still active.\n" ) );
1599 }
else if ( (
void *) exit_val != (
void *) th) {
1600 KA_TRACE( 1, (
"__kmp_reap_common: ExitProcess / TerminateThread used?\n" ) );
1605 "__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
"\n",
1606 th->th.th_info.ds.ds_gtid,
1607 th->th.th_info.ds.ds_thread
1611 th->th.th_info.ds.ds_thread = 0;
1612 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1613 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1614 th->th.th_info.ds.ds_thread_id = 0;
1620 __kmp_reap_monitor( kmp_info_t *th )
1624 KA_TRACE( 10, (
"__kmp_reap_monitor: try to reap %p\n",
1625 (
void *) th->th.th_info.ds.ds_thread ) );
1630 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1631 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
1637 status = SetEvent( __kmp_monitor_ev );
1638 if ( status == FALSE ) {
1639 DWORD error = GetLastError();
1642 KMP_MSG( CantSetEvent ),
1647 KA_TRACE( 10, (
"__kmp_reap_monitor: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1648 __kmp_reap_common( th );
1650 __kmp_free_handle( __kmp_monitor_ev );
1656 __kmp_reap_worker( kmp_info_t * th )
1658 KA_TRACE( 10, (
"__kmp_reap_worker: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1659 __kmp_reap_common( th );
1665 #if KMP_HANDLE_SIGNALS
1669 __kmp_team_handler(
int signo )
1671 if ( __kmp_global.g.g_abort == 0 ) {
1673 if ( __kmp_debug_buf ) {
1674 __kmp_dump_debug_buffer();
1677 TCW_4( __kmp_global.g.g_abort, signo );
1679 TCW_4( __kmp_global.g.g_done, TRUE );
1687 sig_func_t __kmp_signal(
int signum, sig_func_t handler ) {
1688 sig_func_t old = signal( signum, handler );
1689 if ( old == SIG_ERR ) {
1691 __kmp_msg( kmp_ms_fatal, KMP_MSG( FunctionError,
"signal" ), KMP_ERR( error ), __kmp_msg_null );
1697 __kmp_install_one_handler(
1704 KB_TRACE( 60, (
"__kmp_install_one_handler: called: sig=%d\n", sig ) );
1705 if ( parallel_init ) {
1706 old = __kmp_signal( sig, handler );
1708 if ( old == __kmp_sighldrs[ sig ] ) {
1709 __kmp_siginstalled[ sig ] = 1;
1712 old = __kmp_signal( sig, old );
1718 old = __kmp_signal( sig, SIG_DFL );
1719 __kmp_sighldrs[ sig ] = old;
1720 __kmp_signal( sig, old );
1726 __kmp_remove_one_handler(
int sig ) {
1727 if ( __kmp_siginstalled[ sig ] ) {
1730 KB_TRACE( 60, (
"__kmp_remove_one_handler: called: sig=%d\n", sig ) );
1731 old = __kmp_signal( sig, __kmp_sighldrs[ sig ] );
1732 if ( old != __kmp_team_handler ) {
1733 KB_TRACE( 10, (
"__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1734 old = __kmp_signal( sig, old );
1736 __kmp_sighldrs[ sig ] = NULL;
1737 __kmp_siginstalled[ sig ] = 0;
1744 __kmp_install_signals(
int parallel_init )
1746 KB_TRACE( 10, (
"__kmp_install_signals: called\n" ) );
1747 if ( ! __kmp_handle_signals ) {
1748 KB_TRACE( 10, (
"__kmp_install_signals: KMP_HANDLE_SIGNALS is false - handlers not installed\n" ) );
1751 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1752 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1753 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1754 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1755 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1756 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1761 __kmp_remove_signals(
void )
1764 KB_TRACE( 10, (
"__kmp_remove_signals: called\n" ) );
1765 for ( sig = 1; sig < NSIG; ++ sig ) {
1766 __kmp_remove_one_handler( sig );
1771 #endif // KMP_HANDLE_SIGNALS
1775 __kmp_thread_sleep(
int millis )
1779 status = SleepEx( (DWORD) millis, FALSE );
1781 DWORD error = GetLastError();
1784 KMP_MSG( FunctionError,
"SleepEx()" ),
1793 __kmp_is_address_mapped(
void * addr )
1796 MEMORY_BASIC_INFORMATION lpBuffer;
1799 dwLength =
sizeof(MEMORY_BASIC_INFORMATION);
1801 status = VirtualQuery( addr, &lpBuffer, dwLength );
1803 return !((( lpBuffer.State == MEM_RESERVE) || ( lpBuffer.State == MEM_FREE )) ||
1804 (( lpBuffer.Protect == PAGE_NOACCESS ) || ( lpBuffer.Protect == PAGE_EXECUTE )));
1808 __kmp_hardware_timestamp(
void)
1812 QueryPerformanceCounter((LARGE_INTEGER*) &r);
1818 __kmp_free_handle( kmp_thread_t tHandle )
1822 rc = CloseHandle( tHandle );
1824 DWORD error = GetLastError();
1827 KMP_MSG( CantCloseHandle ),
1835 __kmp_get_load_balance(
int max ) {
1837 static ULONG glb_buff_size = 100 * 1024;
1839 static int glb_running_threads = 0;
1840 static double glb_call_time = 0;
1842 int running_threads = 0;
1843 NTSTATUS status = 0;
1844 ULONG buff_size = 0;
1845 ULONG info_size = 0;
1846 void * buffer = NULL;
1847 PSYSTEM_PROCESS_INFORMATION spi = NULL;
1850 double call_time = 0.0;
1852 __kmp_elapsed( & call_time );
1854 if ( glb_call_time &&
1855 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
1856 running_threads = glb_running_threads;
1859 glb_call_time = call_time;
1862 if ( NtQuerySystemInformation == NULL ) {
1863 running_threads = -1;
1874 buff_size = glb_buff_size;
1876 buff_size = 2 * buff_size;
1879 buffer = KMP_INTERNAL_REALLOC( buffer, buff_size );
1880 if ( buffer == NULL ) {
1881 running_threads = -1;
1884 status = NtQuerySystemInformation( SystemProcessInformation, buffer, buff_size, & info_size );
1887 }
while ( status == STATUS_INFO_LENGTH_MISMATCH );
1888 glb_buff_size = buff_size;
1890 #define CHECK( cond ) \
1892 KMP_DEBUG_ASSERT( cond ); \
1893 if ( ! ( cond ) ) { \
1894 running_threads = -1; \
1899 CHECK( buff_size >= info_size );
1900 spi = PSYSTEM_PROCESS_INFORMATION( buffer );
1902 ptrdiff_t offset = uintptr_t( spi ) - uintptr_t( buffer );
1903 CHECK( 0 <= offset && offset +
sizeof( SYSTEM_PROCESS_INFORMATION ) < info_size );
1904 HANDLE pid = spi->ProcessId;
1905 ULONG num = spi->NumberOfThreads;
1907 size_t spi_size =
sizeof( SYSTEM_PROCESS_INFORMATION ) +
sizeof( SYSTEM_THREAD ) * ( num - 1 );
1908 CHECK( offset + spi_size < info_size );
1909 if ( spi->NextEntryOffset != 0 ) {
1910 CHECK( spi_size <= spi->NextEntryOffset );
1915 for (
int i = 0; i < num; ++ i ) {
1916 THREAD_STATE state = spi->Threads[ i ].State;
1919 if ( state == StateRunning ) {
1923 if ( running_threads >= max ) {
1929 if ( spi->NextEntryOffset == 0 ) {
1932 spi = PSYSTEM_PROCESS_INFORMATION( uintptr_t( spi ) + spi->NextEntryOffset );
1939 if ( buffer != NULL ) {
1940 KMP_INTERNAL_FREE( buffer );
1943 glb_running_threads = running_threads;
1945 return running_threads;