20 #include "kmp_wait_release.h" 29 enum SYSTEM_INFORMATION_CLASS {
30 SystemProcessInformation = 5
50 SIZE_T PeakVirtualSize;
53 SIZE_T PeakWorkingSetSize;
54 SIZE_T WorkingSetSize;
55 SIZE_T QuotaPeakPagedPoolUsage;
56 SIZE_T QuotaPagedPoolUsage;
57 SIZE_T QuotaPeakNonPagedPoolUsage;
58 SIZE_T QuotaNonPagedPoolUsage;
60 SIZE_T PeakPagefileUsage;
61 SIZE_T PrivatePageCount;
64 struct SYSTEM_THREAD {
65 LARGE_INTEGER KernelTime;
66 LARGE_INTEGER UserTime;
67 LARGE_INTEGER CreateTime;
73 ULONG ContextSwitchCount;
78 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, KernelTime ) == 0 );
80 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, StartAddress ) == 28 );
81 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, State ) == 52 );
83 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, StartAddress ) == 32 );
84 KMP_BUILD_ASSERT( offsetof( SYSTEM_THREAD, State ) == 68 );
87 struct SYSTEM_PROCESS_INFORMATION {
88 ULONG NextEntryOffset;
89 ULONG NumberOfThreads;
90 LARGE_INTEGER Reserved[ 3 ];
91 LARGE_INTEGER CreateTime;
92 LARGE_INTEGER UserTime;
93 LARGE_INTEGER KernelTime;
94 UNICODE_STRING ImageName;
97 HANDLE ParentProcessId;
100 VM_COUNTERS VMCounters;
101 IO_COUNTERS IOCounters;
102 SYSTEM_THREAD Threads[ 1 ];
104 typedef SYSTEM_PROCESS_INFORMATION * PSYSTEM_PROCESS_INFORMATION;
106 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, NextEntryOffset ) == 0 );
107 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, CreateTime ) == 32 );
108 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, ImageName ) == 56 );
110 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, ProcessId ) == 68 );
111 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, HandleCount ) == 76 );
112 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, VMCounters ) == 88 );
113 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, IOCounters ) == 136 );
114 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, Threads ) == 184 );
116 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, ProcessId ) == 80 );
117 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, HandleCount ) == 96 );
118 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, VMCounters ) == 112 );
119 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, IOCounters ) == 208 );
120 KMP_BUILD_ASSERT( offsetof( SYSTEM_PROCESS_INFORMATION, Threads ) == 256 );
123 typedef NTSTATUS (NTAPI *NtQuerySystemInformation_t)( SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG );
124 NtQuerySystemInformation_t NtQuerySystemInformation = NULL;
126 HMODULE ntdll = NULL;
130 #if KMP_GROUP_AFFINITY 131 static HMODULE kernel32 = NULL;
137 #if KMP_HANDLE_SIGNALS 138 typedef void (* sig_func_t )( int );
139 static sig_func_t __kmp_sighldrs[ NSIG ];
140 static int __kmp_siginstalled[ NSIG ];
143 static HANDLE __kmp_monitor_ev;
144 static kmp_int64 __kmp_win32_time;
145 double __kmp_win32_tick;
147 int __kmp_init_runtime = FALSE;
148 CRITICAL_SECTION __kmp_win32_section;
151 __kmp_win32_mutex_init( kmp_win32_mutex_t *mx )
153 InitializeCriticalSection( & mx->cs );
155 __kmp_itt_system_object_created( & mx->cs,
"Critical Section" );
160 __kmp_win32_mutex_destroy( kmp_win32_mutex_t *mx )
162 DeleteCriticalSection( & mx->cs );
166 __kmp_win32_mutex_lock( kmp_win32_mutex_t *mx )
168 EnterCriticalSection( & mx->cs );
172 __kmp_win32_mutex_unlock( kmp_win32_mutex_t *mx )
174 LeaveCriticalSection( & mx->cs );
178 __kmp_win32_cond_init( kmp_win32_cond_t *cv )
180 cv->waiters_count_ = 0;
181 cv->wait_generation_count_ = 0;
182 cv->release_count_ = 0;
185 __kmp_win32_mutex_init( & cv->waiters_count_lock_ );
188 cv->event_ = CreateEvent( NULL,
193 __kmp_itt_system_object_created( cv->event_,
"Event" );
198 __kmp_win32_cond_destroy( kmp_win32_cond_t *cv )
200 __kmp_win32_mutex_destroy( & cv->waiters_count_lock_ );
201 __kmp_free_handle( cv->event_ );
202 memset( cv,
'\0',
sizeof( *cv ) );
209 __kmp_win32_cond_wait( kmp_win32_cond_t *cv, kmp_win32_mutex_t *mx, kmp_info_t *th,
int need_decrease_load )
215 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
218 cv->waiters_count_++;
221 my_generation = cv->wait_generation_count_;
223 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_ );
224 __kmp_win32_mutex_unlock( mx );
230 WaitForSingleObject( cv->event_, INFINITE );
232 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
237 wait_done = ( cv->release_count_ > 0 ) &&
238 ( cv->wait_generation_count_ != my_generation );
240 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_);
248 __kmp_win32_mutex_lock( mx );
249 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
251 cv->waiters_count_--;
252 cv->release_count_--;
254 last_waiter = ( cv->release_count_ == 0 );
256 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_ );
260 ResetEvent( cv->event_ );
265 __kmp_win32_cond_broadcast( kmp_win32_cond_t *cv )
267 __kmp_win32_mutex_lock( &cv->waiters_count_lock_ );
269 if( cv->waiters_count_ > 0 ) {
270 SetEvent( cv->event_ );
273 cv->release_count_ = cv->waiters_count_;
276 cv->wait_generation_count_++;
279 __kmp_win32_mutex_unlock( &cv->waiters_count_lock_ );
283 __kmp_win32_cond_signal( kmp_win32_cond_t *cv )
285 __kmp_win32_cond_broadcast( cv );
292 __kmp_enable(
int new_state )
294 if (__kmp_init_runtime)
295 LeaveCriticalSection( & __kmp_win32_section );
299 __kmp_disable(
int *old_state )
303 if (__kmp_init_runtime)
304 EnterCriticalSection( & __kmp_win32_section );
308 __kmp_suspend_initialize(
void )
314 __kmp_suspend_initialize_thread( kmp_info_t *th )
316 if ( ! TCR_4( th->th.th_suspend_init ) ) {
319 __kmp_win32_cond_init( &th->th.th_suspend_cv );
320 __kmp_win32_mutex_init( &th->th.th_suspend_mx );
321 TCW_4( th->th.th_suspend_init, TRUE );
326 __kmp_suspend_uninitialize_thread( kmp_info_t *th )
328 if ( TCR_4( th->th.th_suspend_init ) ) {
331 __kmp_win32_cond_destroy( & th->th.th_suspend_cv );
332 __kmp_win32_mutex_destroy( & th->th.th_suspend_mx );
333 TCW_4( th->th.th_suspend_init, FALSE );
341 static inline void __kmp_suspend_template(
int th_gtid, C *flag )
343 kmp_info_t *th = __kmp_threads[th_gtid];
345 typename C::flag_t old_spin;
347 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d enter for flag's loc(%p)\n", th_gtid, flag->get() ) );
349 __kmp_suspend_initialize_thread( th );
350 __kmp_win32_mutex_lock( &th->th.th_suspend_mx );
352 KF_TRACE( 10, (
"__kmp_suspend_template: T#%d setting sleep bit for flag's loc(%p)\n",
353 th_gtid, flag->get() ) );
358 old_spin = flag->set_sleeping();
360 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d set sleep bit for flag's loc(%p)==%d\n",
361 th_gtid, flag->get(), *(flag->get()) ) );
363 if ( flag->done_check_val(old_spin) ) {
364 old_spin = flag->unset_sleeping();
365 KF_TRACE( 5, (
"__kmp_suspend_template: T#%d false alarm, reset sleep bit for flag's loc(%p)\n",
366 th_gtid, flag->get()) );
369 __kmp_suspend_count++;
375 int deactivated = FALSE;
376 TCW_PTR(th->th.th_sleep_loc, (
void *)flag);
377 while ( flag->is_sleeping() ) {
378 KF_TRACE( 15, (
"__kmp_suspend_template: T#%d about to perform kmp_win32_cond_wait()\n",
381 if ( ! deactivated ) {
382 th->th.th_active = FALSE;
383 if ( th->th.th_active_in_pool ) {
384 th->th.th_active_in_pool = FALSE;
386 (kmp_int32 *) &__kmp_thread_pool_active_nth );
387 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
391 __kmp_win32_cond_wait( &th->th.th_suspend_cv, &th->th.th_suspend_mx, 0, 0 );
394 __kmp_win32_cond_wait( &th->th.th_suspend_cv, &th->th.th_suspend_mx, 0, 0 );
398 if( flag->is_sleeping() ) {
399 KF_TRACE( 100, (
"__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ));
407 th->th.th_active = TRUE;
408 if ( TCR_4(th->th.th_in_pool) ) {
410 (kmp_int32 *) &__kmp_thread_pool_active_nth );
411 th->th.th_active_in_pool = TRUE;
416 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
418 KF_TRACE( 30, (
"__kmp_suspend_template: T#%d exit\n", th_gtid ) );
421 void __kmp_suspend_32(
int th_gtid, kmp_flag_32 *flag) {
422 __kmp_suspend_template(th_gtid, flag);
424 void __kmp_suspend_64(
int th_gtid, kmp_flag_64 *flag) {
425 __kmp_suspend_template(th_gtid, flag);
427 void __kmp_suspend_oncore(
int th_gtid, kmp_flag_oncore *flag) {
428 __kmp_suspend_template(th_gtid, flag);
436 static inline void __kmp_resume_template(
int target_gtid, C *flag )
438 kmp_info_t *th = __kmp_threads[target_gtid];
442 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
445 KF_TRACE( 30, (
"__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", gtid, target_gtid ) );
447 __kmp_suspend_initialize_thread( th );
448 __kmp_win32_mutex_lock( &th->th.th_suspend_mx );
451 flag = (C *)th->th.th_sleep_loc;
455 if (!flag || flag->get_type() != flag->get_ptr_type()) {
456 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p)\n",
457 gtid, target_gtid, NULL ) );
458 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
462 typename C::flag_t old_spin = flag->unset_sleeping();
463 if ( !flag->is_sleeping_val(old_spin) ) {
464 KF_TRACE( 5, (
"__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag's loc(%p): " 466 gtid, target_gtid, flag->get(), old_spin, *(flag->get()) ) );
467 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
471 TCW_PTR(th->th.th_sleep_loc, NULL);
473 KF_TRACE( 5, (
"__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p)\n",
474 gtid, target_gtid, flag->get() ) );
476 __kmp_win32_cond_signal( &th->th.th_suspend_cv );
477 __kmp_win32_mutex_unlock( &th->th.th_suspend_mx );
479 KF_TRACE( 30, (
"__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
480 gtid, target_gtid ) );
483 void __kmp_resume_32(
int target_gtid, kmp_flag_32 *flag) {
484 __kmp_resume_template(target_gtid, flag);
486 void __kmp_resume_64(
int target_gtid, kmp_flag_64 *flag) {
487 __kmp_resume_template(target_gtid, flag);
489 void __kmp_resume_oncore(
int target_gtid, kmp_flag_oncore *flag) {
490 __kmp_resume_template(target_gtid, flag);
498 __kmp_yield(
int cond )
508 __kmp_gtid_set_specific(
int gtid )
510 if( __kmp_init_gtid ) {
511 KA_TRACE( 50, (
"__kmp_gtid_set_specific: T#%d key:%d\n",
512 gtid, __kmp_gtid_threadprivate_key ));
513 if( ! TlsSetValue( __kmp_gtid_threadprivate_key, (LPVOID)(gtid+1)) )
514 KMP_FATAL( TLSSetValueFailed );
516 KA_TRACE( 50, (
"__kmp_gtid_set_specific: runtime shutdown, returning\n" ) );
521 __kmp_gtid_get_specific()
524 if( !__kmp_init_gtid ) {
525 KA_TRACE( 50, (
"__kmp_gtid_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
526 return KMP_GTID_SHUTDOWN;
528 gtid = (int)(kmp_intptr_t)TlsGetValue( __kmp_gtid_threadprivate_key );
535 KA_TRACE( 50, (
"__kmp_gtid_get_specific: key:%d gtid:%d\n",
536 __kmp_gtid_threadprivate_key, gtid ));
543 #if KMP_GROUP_AFFINITY 550 __kmp_get_proc_group( kmp_affin_mask_t
const *mask )
554 for (i = 0; i < __kmp_num_proc_groups; i++) {
557 unsigned long first_32_bits = hwloc_bitmap_to_ith_ulong((hwloc_const_bitmap_t)mask, i*2);
558 unsigned long second_32_bits = hwloc_bitmap_to_ith_ulong((hwloc_const_bitmap_t)mask, i*2+1);
559 if (first_32_bits == 0 && second_32_bits == 0) {
578 __kmp_set_system_affinity( kmp_affin_mask_t
const *mask,
int abort_on_error )
581 int retval = hwloc_set_cpubind(__kmp_hwloc_topology, (hwloc_cpuset_t)mask, HWLOC_CPUBIND_THREAD);
586 if (abort_on_error) {
589 KMP_MSG( FatalSysError ),
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 ),
656 __kmp_get_system_affinity( kmp_affin_mask_t *mask,
int abort_on_error )
659 int retval = hwloc_get_cpubind(__kmp_hwloc_topology, (hwloc_cpuset_t)mask, HWLOC_CPUBIND_THREAD);
664 if (abort_on_error) {
667 KMP_MSG( FatalSysError ),
674 # if KMP_GROUP_AFFINITY 676 if (__kmp_num_proc_groups > 1) {
679 KMP_DEBUG_ASSERT(__kmp_GetThreadGroupAffinity != NULL);
681 if (__kmp_GetThreadGroupAffinity(GetCurrentThread(), &ga) == 0) {
682 DWORD error = GetLastError();
683 if (abort_on_error) {
686 KMP_MSG(FunctionError,
"GetThreadGroupAffinity()"),
694 if ((ga.Group < 0) || (ga.Group > __kmp_num_proc_groups)
699 mask[ga.Group] = ga.Mask;
706 kmp_affin_mask_t newMask, sysMask, retval;
708 if (!GetProcessAffinityMask(GetCurrentProcess(), &newMask, &sysMask)) {
709 DWORD error = GetLastError();
710 if (abort_on_error) {
713 KMP_MSG(FunctionError,
"GetProcessAffinityMask()"),
720 retval = SetThreadAffinityMask(GetCurrentThread(), newMask);
722 DWORD error = GetLastError();
723 if (abort_on_error) {
726 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
733 newMask = SetThreadAffinityMask(GetCurrentThread(), retval);
735 DWORD error = GetLastError();
736 if (abort_on_error) {
739 KMP_MSG(FunctionError,
"SetThreadAffinityMask()"),
752 __kmp_affinity_bind_thread(
int proc )
755 kmp_affin_mask_t *mask;
756 KMP_CPU_ALLOC_ON_STACK(mask);
758 KMP_CPU_SET(proc, mask);
759 __kmp_set_system_affinity(mask, TRUE);
760 KMP_CPU_FREE_FROM_STACK(mask);
762 # if KMP_GROUP_AFFINITY 764 if (__kmp_num_proc_groups > 1) {
770 KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups
771 * CHAR_BIT *
sizeof(DWORD_PTR))));
772 ga.Group = proc / (CHAR_BIT *
sizeof(DWORD_PTR));
773 ga.Mask = (
unsigned long long)1 << (proc % (CHAR_BIT *
sizeof(DWORD_PTR)));
774 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
776 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
777 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
778 DWORD error = GetLastError();
779 if (__kmp_affinity_verbose) {
782 KMP_MSG( CantSetThreadAffMask ),
794 kmp_affin_mask_t mask;
796 KMP_CPU_SET(proc, &mask);
797 __kmp_set_system_affinity(&mask, TRUE);
803 __kmp_affinity_determine_capable(
const char *env_var )
809 #if KMP_GROUP_AFFINITY 810 KMP_AFFINITY_ENABLE(__kmp_num_proc_groups*
sizeof(kmp_affin_mask_t));
812 KMP_AFFINITY_ENABLE(
sizeof(kmp_affin_mask_t));
816 "__kmp_affinity_determine_capable: " 817 "Windows* OS affinity interface functional (mask size = %" KMP_SIZE_T_SPEC
").\n",
818 __kmp_affin_mask_size
823 __kmp_read_cpu_time(
void )
825 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
831 status = GetProcessTimes( GetCurrentProcess(), &CreationTime,
832 &ExitTime, &KernelTime, &UserTime );
837 sec += KernelTime.dwHighDateTime;
838 sec += UserTime.dwHighDateTime;
841 sec *= (double) (1 << 16) * (double) (1 << 16);
843 sec += KernelTime.dwLowDateTime;
844 sec += UserTime.dwLowDateTime;
846 cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC;
853 __kmp_read_system_info(
struct kmp_sys_info *info )
872 __kmp_runtime_initialize(
void )
878 if ( __kmp_init_runtime ) {
886 UINT err_mode = SetErrorMode (SEM_FAILCRITICALERRORS);
888 BOOL ret = GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
889 |GET_MODULE_HANDLE_EX_FLAG_PIN,
890 (LPCTSTR)&__kmp_serial_initialize, &h);
891 KMP_DEBUG_ASSERT2(h && ret,
"OpenMP RTL cannot find itself loaded");
892 SetErrorMode (err_mode);
893 KA_TRACE( 10, (
"__kmp_runtime_initialize: dynamic library pinned\n") );
897 InitializeCriticalSection( & __kmp_win32_section );
899 __kmp_itt_system_object_created( & __kmp_win32_section,
"Critical Section" );
901 __kmp_initialize_system_tick();
903 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) 904 if ( ! __kmp_cpuinfo.initialized ) {
905 __kmp_query_cpuid( & __kmp_cpuinfo );
910 #if KMP_OS_WINDOWS && ! defined KMP_DYNAMIC_LIB 925 __kmp_tls_gtid_min = 0;
927 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
931 if ( !__kmp_gtid_threadprivate_key ) {
932 __kmp_gtid_threadprivate_key = TlsAlloc();
933 if( __kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES ) {
934 KMP_FATAL( TLSOutOfIndexes );
949 __kmp_str_buf_init( & path );
950 path_size = GetSystemDirectory( path.str, path.size );
951 KMP_DEBUG_ASSERT( path_size > 0 );
952 if ( path_size >= path.size ) {
956 __kmp_str_buf_reserve( & path, path_size );
957 path_size = GetSystemDirectory( path.str, path.size );
958 KMP_DEBUG_ASSERT( path_size > 0 );
960 if ( path_size > 0 && path_size < path.size ) {
965 path.used = path_size;
966 __kmp_str_buf_print( & path,
"\\%s",
"ntdll.dll" );
971 ntdll = GetModuleHandle( path.str );
974 KMP_DEBUG_ASSERT( ntdll != NULL );
975 if ( ntdll != NULL ) {
976 NtQuerySystemInformation = (NtQuerySystemInformation_t) GetProcAddress( ntdll,
"NtQuerySystemInformation" );
978 KMP_DEBUG_ASSERT( NtQuerySystemInformation != NULL );
980 #if KMP_GROUP_AFFINITY 985 if ( path_size > 0 && path_size < path.size ) {
990 path.used = path_size;
991 __kmp_str_buf_print( & path,
"\\%s",
"kernel32.dll" );
996 kernel32 = GetModuleHandle( path.str );
997 KA_TRACE( 10, (
"__kmp_runtime_initialize: kernel32.dll = %s\n", path.str ) );
1003 if ( kernel32 != NULL ) {
1004 __kmp_GetActiveProcessorCount = (kmp_GetActiveProcessorCount_t) GetProcAddress( kernel32,
"GetActiveProcessorCount" );
1005 __kmp_GetActiveProcessorGroupCount = (kmp_GetActiveProcessorGroupCount_t) GetProcAddress( kernel32,
"GetActiveProcessorGroupCount" );
1006 __kmp_GetThreadGroupAffinity = (kmp_GetThreadGroupAffinity_t) GetProcAddress( kernel32,
"GetThreadGroupAffinity" );
1007 __kmp_SetThreadGroupAffinity = (kmp_SetThreadGroupAffinity_t) GetProcAddress( kernel32,
"SetThreadGroupAffinity" );
1009 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorCount = %p\n", __kmp_GetActiveProcessorCount ) );
1010 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_GetActiveProcessorGroupCount = %p\n", __kmp_GetActiveProcessorGroupCount ) );
1011 KA_TRACE( 10, (
"__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity = %p\n", __kmp_GetThreadGroupAffinity ) );
1012 KA_TRACE( 10, (
"__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity = %p\n", __kmp_SetThreadGroupAffinity ) );
1013 KA_TRACE( 10, (
"__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
sizeof(kmp_affin_mask_t) ) );
1022 if ( ( __kmp_GetActiveProcessorCount != NULL )
1023 && ( __kmp_GetActiveProcessorGroupCount != NULL )
1024 && ( __kmp_GetThreadGroupAffinity != NULL )
1025 && ( __kmp_SetThreadGroupAffinity != NULL )
1026 && ( ( __kmp_num_proc_groups
1027 = __kmp_GetActiveProcessorGroupCount() ) > 1 ) ) {
1033 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
1037 for ( i = 0; i < __kmp_num_proc_groups; i++ ) {
1038 DWORD size = __kmp_GetActiveProcessorCount( i );
1039 __kmp_xproc += size;
1040 KA_TRACE( 10, (
"__kmp_runtime_initialize: proc group %d size = %d\n", i, size ) );
1044 KA_TRACE( 10, (
"__kmp_runtime_initialize: %d processor groups detected\n", __kmp_num_proc_groups ) );
1048 if ( __kmp_num_proc_groups <= 1 ) {
1049 GetSystemInfo( & info );
1050 __kmp_xproc = info.dwNumberOfProcessors;
1053 GetSystemInfo( & info );
1054 __kmp_xproc = info.dwNumberOfProcessors;
1061 if ( __kmp_xproc <= 0 ) {
1065 KA_TRACE( 5, (
"__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc) );
1067 __kmp_str_buf_free( & path );
1070 __kmp_itt_initialize();
1073 __kmp_init_runtime = TRUE;
1077 __kmp_runtime_destroy(
void )
1079 if ( ! __kmp_init_runtime ) {
1084 __kmp_itt_destroy();
1089 KA_TRACE( 40, (
"__kmp_runtime_destroy\n" ));
1091 if( __kmp_gtid_threadprivate_key ) {
1092 TlsFree( __kmp_gtid_threadprivate_key );
1093 __kmp_gtid_threadprivate_key = 0;
1096 __kmp_affinity_uninitialize();
1097 DeleteCriticalSection( & __kmp_win32_section );
1100 NtQuerySystemInformation = NULL;
1104 __kmp_GetActiveProcessorCount = NULL;
1105 __kmp_GetActiveProcessorGroupCount = NULL;
1106 __kmp_GetThreadGroupAffinity = NULL;
1107 __kmp_SetThreadGroupAffinity = NULL;
1108 #endif // KMP_ARCH_X86_64 1110 __kmp_init_runtime = FALSE;
1115 __kmp_terminate_thread(
int gtid )
1117 kmp_info_t *th = __kmp_threads[ gtid ];
1121 KA_TRACE( 10, (
"__kmp_terminate_thread: kill (%d)\n", gtid ) );
1123 if (TerminateThread( th->th.th_info.ds.ds_thread, (DWORD) -1) == FALSE) {
1126 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1133 __kmp_clear_system_time(
void )
1137 status = QueryPerformanceCounter( & time );
1138 __kmp_win32_time = (kmp_int64) time.QuadPart;
1142 __kmp_initialize_system_tick(
void )
1148 status = QueryPerformanceFrequency( & freq );
1150 DWORD error = GetLastError();
1153 KMP_MSG( FunctionError,
"QueryPerformanceFrequency()" ),
1160 __kmp_win32_tick = ((double) 1.0) / (double) freq.QuadPart;
1168 __kmp_elapsed(
double *t )
1172 status = QueryPerformanceCounter( & now );
1173 *t = ((double) now.QuadPart) * __kmp_win32_tick;
1179 __kmp_elapsed_tick(
double *t )
1181 *t = __kmp_win32_tick;
1185 __kmp_read_system_time(
double *delta )
1187 if (delta != NULL) {
1191 status = QueryPerformanceCounter( & now );
1193 *delta = ((double) (((kmp_int64) now.QuadPart) - __kmp_win32_time))
1202 __kmp_launch_worker(
void *arg )
1204 volatile void *stack_data;
1207 kmp_info_t *this_thr = (kmp_info_t *) arg;
1210 gtid = this_thr->th.th_info.ds.ds_gtid;
1211 __kmp_gtid_set_specific( gtid );
1212 #ifdef KMP_TDATA_GTID 1213 #error "This define causes problems with LoadLibrary() + declspec(thread) " \ 1214 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \ 1215 "reference: http://support.microsoft.com/kb/118816" 1220 __kmp_itt_thread_name( gtid );
1223 __kmp_affinity_set_init_mask( gtid, FALSE );
1225 #if KMP_ARCH_X86 || KMP_ARCH_X86_64 1230 __kmp_clear_x87_fpu_status_word();
1231 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
1232 __kmp_load_mxcsr( &__kmp_init_mxcsr );
1235 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
1236 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
1239 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1240 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1241 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1243 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1244 TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
1245 KMP_ASSERT( this_thr -> th.th_info.ds.ds_stackgrow == FALSE );
1246 __kmp_check_stack_overlap( this_thr );
1249 exit_val = __kmp_launch_thread( this_thr );
1250 KMP_FSYNC_RELEASING( &this_thr -> th.th_info.ds.ds_alive );
1251 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1259 __kmp_launch_monitor(
void *arg )
1262 kmp_thread_t monitor;
1265 kmp_info_t *this_thr = (kmp_info_t *) arg;
1267 KMP_DEBUG_ASSERT(__kmp_init_monitor);
1268 TCW_4( __kmp_init_monitor, 2 );
1270 this_thr -> th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1271 TCW_4( this_thr -> th.th_info.ds.ds_alive, TRUE );
1274 KA_TRACE( 10, (
"__kmp_launch_monitor: launched\n" ) );
1276 monitor = GetCurrentThread();
1279 status = SetThreadPriority( monitor, THREAD_PRIORITY_HIGHEST );
1281 DWORD error = GetLastError();
1284 KMP_MSG( CantSetThreadPriority ),
1291 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
1292 #ifdef KMP_TDATA_GTID 1293 #error "This define causes problems with LoadLibrary() + declspec(thread) " \ 1294 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \ 1295 "reference: http://support.microsoft.com/kb/118816" 1300 __kmp_itt_thread_ignore();
1305 interval = ( 1000 / __kmp_monitor_wakeups );
1307 while (! TCR_4(__kmp_global.g.g_done)) {
1310 KA_TRACE( 15, (
"__kmp_launch_monitor: update\n" ) );
1312 wait_status = WaitForSingleObject( __kmp_monitor_ev, interval );
1314 if (wait_status == WAIT_TIMEOUT) {
1315 TCW_4( __kmp_global.g.g_time.dt.t_value,
1316 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
1322 KA_TRACE( 10, (
"__kmp_launch_monitor: finished\n" ) );
1324 status = SetThreadPriority( monitor, THREAD_PRIORITY_NORMAL );
1326 DWORD error = GetLastError();
1329 KMP_MSG( CantSetThreadPriority ),
1335 if (__kmp_global.g.g_abort != 0) {
1341 KA_TRACE( 10, (
"__kmp_launch_monitor: terminate sig=%d\n", (__kmp_global.g.g_abort) ) );
1346 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1347 __kmp_terminate_thread( gtid );
1353 KA_TRACE( 10, (
"__kmp_launch_monitor: raise sig=%d\n", (__kmp_global.g.g_abort) ) );
1355 if (__kmp_global.g.g_abort > 0) {
1356 raise( __kmp_global.g.g_abort );
1360 TCW_4( this_thr -> th.th_info.ds.ds_alive, FALSE );
1367 __kmp_create_worker(
int gtid, kmp_info_t *th,
size_t stack_size )
1369 kmp_thread_t handle;
1372 KA_TRACE( 10, (
"__kmp_create_worker: try to create thread (%d)\n", gtid ) );
1374 th->th.th_info.ds.ds_gtid = gtid;
1376 if ( KMP_UBER_GTID(gtid) ) {
1385 rc = DuplicateHandle(
1386 GetCurrentProcess(),
1388 GetCurrentProcess(),
1389 &th->th.th_info.ds.ds_thread,
1392 DUPLICATE_SAME_ACCESS
1395 KA_TRACE( 10, (
" __kmp_create_worker: ROOT Handle duplicated, th = %p, handle = %" KMP_UINTPTR_SPEC
"\n",
1397 th->th.th_info.ds.ds_thread ) );
1398 th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1400 if ( TCR_4(__kmp_gtid_mode) < 2 ) {
1402 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1403 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1404 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1405 __kmp_check_stack_overlap( th );
1412 KA_TRACE( 10, (
"__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC
1413 " bytes\n", stack_size ) );
1415 stack_size += gtid * __kmp_stkoffset;
1417 TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1418 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
1420 KA_TRACE( 10, (
"__kmp_create_worker: (before) stack_size = %" 1422 " bytes, &__kmp_launch_worker = %p, th = %p, " 1424 (SIZE_T) stack_size,
1425 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1426 (LPVOID) th, &idThread ) );
1428 handle = CreateThread( NULL, (SIZE_T) stack_size,
1429 (LPTHREAD_START_ROUTINE) __kmp_launch_worker,
1430 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1432 KA_TRACE( 10, (
"__kmp_create_worker: (after) stack_size = %" 1434 " bytes, &__kmp_launch_worker = %p, th = %p, " 1435 "idThread = %u, handle = %" KMP_UINTPTR_SPEC
"\n",
1436 (SIZE_T) stack_size,
1437 (LPTHREAD_START_ROUTINE) & __kmp_launch_worker,
1438 (LPVOID) th, idThread, handle ) );
1440 if ( handle == 0 ) {
1441 DWORD error = GetLastError();
1442 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantCreateThread ), KMP_ERR( error ), __kmp_msg_null);
1444 th->th.th_info.ds.ds_thread = handle;
1450 KA_TRACE( 10, (
"__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1454 __kmp_still_running(kmp_info_t *th) {
1455 return (WAIT_TIMEOUT == WaitForSingleObject( th->th.th_info.ds.ds_thread, 0));
1459 __kmp_create_monitor( kmp_info_t *th )
1461 kmp_thread_t handle;
1463 int ideal, new_ideal;
1465 if( __kmp_dflt_blocktime == KMP_MAX_BLOCKTIME ) {
1467 KA_TRACE( 10, (
"__kmp_create_monitor: skipping monitor thread because of MAX blocktime\n" ) );
1468 th->th.th_info.ds.ds_tid = 0;
1469 th->th.th_info.ds.ds_gtid = 0;
1470 TCW_4( __kmp_init_monitor, 2 );
1473 KA_TRACE( 10, (
"__kmp_create_monitor: try to create monitor\n" ) );
1477 __kmp_monitor_ev = CreateEvent( NULL, TRUE, FALSE, NULL );
1478 if ( __kmp_monitor_ev == NULL ) {
1479 DWORD error = GetLastError();
1482 KMP_MSG( CantCreateEvent ),
1488 __kmp_itt_system_object_created( __kmp_monitor_ev,
"Event" );
1491 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1492 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1496 if ( __kmp_monitor_stksize == 0 ) {
1497 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1499 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1500 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1503 KA_TRACE( 10, (
"__kmp_create_monitor: requested stacksize = %d bytes\n",
1504 (
int) __kmp_monitor_stksize ) );
1506 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
1508 handle = CreateThread( NULL, (SIZE_T) __kmp_monitor_stksize,
1509 (LPTHREAD_START_ROUTINE) __kmp_launch_monitor,
1510 (LPVOID) th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread );
1512 DWORD error = GetLastError();
1515 KMP_MSG( CantCreateThread ),
1521 th->th.th_info.ds.ds_thread = handle;
1525 KA_TRACE( 10, (
"__kmp_create_monitor: monitor created %p\n",
1526 (
void *) th->th.th_info.ds.ds_thread ) );
1539 __kmp_is_thread_alive( kmp_info_t * th, DWORD *exit_val )
1542 rc = GetExitCodeThread( th->th.th_info.ds.ds_thread, exit_val );
1544 DWORD error = GetLastError();
1547 KMP_MSG( FunctionError,
"GetExitCodeThread()" ),
1552 return ( *exit_val == STILL_ACTIVE );
1560 ExitThread( exit_status );
1567 __kmp_reap_common( kmp_info_t * th )
1573 KA_TRACE( 10, (
"__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid ) );
1593 register kmp_uint32 spins;
1595 KMP_FSYNC_SPIN_INIT( obj, (
void*) & th->th.th_info.ds.ds_alive );
1597 KMP_INIT_YIELD( spins );
1600 KMP_FSYNC_SPIN_PREPARE( obj );
1602 __kmp_is_thread_alive( th, &exit_val );
1603 KMP_YIELD( TCR_4(__kmp_nth) > __kmp_avail_proc );
1604 KMP_YIELD_SPIN( spins );
1605 }
while ( exit_val == STILL_ACTIVE && TCR_4( th->th.th_info.ds.ds_alive ) );
1607 if ( exit_val == STILL_ACTIVE ) {
1608 KMP_FSYNC_CANCEL( obj );
1610 KMP_FSYNC_SPIN_ACQUIRED( obj );
1615 __kmp_free_handle( th->th.th_info.ds.ds_thread );
1622 if ( exit_val == STILL_ACTIVE ) {
1623 KA_TRACE( 1, (
"__kmp_reap_common: thread still active.\n" ) );
1624 }
else if ( (
void *) exit_val != (
void *) th) {
1625 KA_TRACE( 1, (
"__kmp_reap_common: ExitProcess / TerminateThread used?\n" ) );
1630 "__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
"\n",
1631 th->th.th_info.ds.ds_gtid,
1632 th->th.th_info.ds.ds_thread
1636 th->th.th_info.ds.ds_thread = 0;
1637 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1638 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1639 th->th.th_info.ds.ds_thread_id = 0;
1645 __kmp_reap_monitor( kmp_info_t *th )
1649 KA_TRACE( 10, (
"__kmp_reap_monitor: try to reap %p\n",
1650 (
void *) th->th.th_info.ds.ds_thread ) );
1655 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1656 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
1657 KA_TRACE( 10, (
"__kmp_reap_monitor: monitor did not start, returning\n") );
1663 status = SetEvent( __kmp_monitor_ev );
1664 if ( status == FALSE ) {
1665 DWORD error = GetLastError();
1668 KMP_MSG( CantSetEvent ),
1673 KA_TRACE( 10, (
"__kmp_reap_monitor: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1674 __kmp_reap_common( th );
1676 __kmp_free_handle( __kmp_monitor_ev );
1682 __kmp_reap_worker( kmp_info_t * th )
1684 KA_TRACE( 10, (
"__kmp_reap_worker: reaping thread (%d)\n", th->th.th_info.ds.ds_gtid ) );
1685 __kmp_reap_common( th );
1691 #if KMP_HANDLE_SIGNALS 1695 __kmp_team_handler(
int signo )
1697 if ( __kmp_global.g.g_abort == 0 ) {
1699 if ( __kmp_debug_buf ) {
1700 __kmp_dump_debug_buffer();
1703 TCW_4( __kmp_global.g.g_abort, signo );
1705 TCW_4( __kmp_global.g.g_done, TRUE );
1713 sig_func_t __kmp_signal(
int signum, sig_func_t handler ) {
1714 sig_func_t old = signal( signum, handler );
1715 if ( old == SIG_ERR ) {
1717 __kmp_msg( kmp_ms_fatal, KMP_MSG( FunctionError,
"signal" ), KMP_ERR( error ), __kmp_msg_null );
1723 __kmp_install_one_handler(
1730 KB_TRACE( 60, (
"__kmp_install_one_handler: called: sig=%d\n", sig ) );
1731 if ( parallel_init ) {
1732 old = __kmp_signal( sig, handler );
1734 if ( old == __kmp_sighldrs[ sig ] ) {
1735 __kmp_siginstalled[ sig ] = 1;
1738 old = __kmp_signal( sig, old );
1744 old = __kmp_signal( sig, SIG_DFL );
1745 __kmp_sighldrs[ sig ] = old;
1746 __kmp_signal( sig, old );
1752 __kmp_remove_one_handler(
int sig ) {
1753 if ( __kmp_siginstalled[ sig ] ) {
1756 KB_TRACE( 60, (
"__kmp_remove_one_handler: called: sig=%d\n", sig ) );
1757 old = __kmp_signal( sig, __kmp_sighldrs[ sig ] );
1758 if ( old != __kmp_team_handler ) {
1759 KB_TRACE( 10, (
"__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1760 old = __kmp_signal( sig, old );
1762 __kmp_sighldrs[ sig ] = NULL;
1763 __kmp_siginstalled[ sig ] = 0;
1770 __kmp_install_signals(
int parallel_init )
1772 KB_TRACE( 10, (
"__kmp_install_signals: called\n" ) );
1773 if ( ! __kmp_handle_signals ) {
1774 KB_TRACE( 10, (
"__kmp_install_signals: KMP_HANDLE_SIGNALS is false - handlers not installed\n" ) );
1777 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1778 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1779 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1780 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1781 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1782 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1787 __kmp_remove_signals(
void )
1790 KB_TRACE( 10, (
"__kmp_remove_signals: called\n" ) );
1791 for ( sig = 1; sig < NSIG; ++ sig ) {
1792 __kmp_remove_one_handler( sig );
1797 #endif // KMP_HANDLE_SIGNALS 1801 __kmp_thread_sleep(
int millis )
1805 status = SleepEx( (DWORD) millis, FALSE );
1807 DWORD error = GetLastError();
1810 KMP_MSG( FunctionError,
"SleepEx()" ),
1819 __kmp_is_address_mapped(
void * addr )
1822 MEMORY_BASIC_INFORMATION lpBuffer;
1825 dwLength =
sizeof(MEMORY_BASIC_INFORMATION);
1827 status = VirtualQuery( addr, &lpBuffer, dwLength );
1829 return !((( lpBuffer.State == MEM_RESERVE) || ( lpBuffer.State == MEM_FREE )) ||
1830 (( lpBuffer.Protect == PAGE_NOACCESS ) || ( lpBuffer.Protect == PAGE_EXECUTE )));
1834 __kmp_hardware_timestamp(
void)
1838 QueryPerformanceCounter((LARGE_INTEGER*) &r);
1844 __kmp_free_handle( kmp_thread_t tHandle )
1848 rc = CloseHandle( tHandle );
1850 DWORD error = GetLastError();
1853 KMP_MSG( CantCloseHandle ),
1861 __kmp_get_load_balance(
int max ) {
1863 static ULONG glb_buff_size = 100 * 1024;
1865 static int glb_running_threads = 0;
1866 static double glb_call_time = 0;
1868 int running_threads = 0;
1869 NTSTATUS status = 0;
1870 ULONG buff_size = 0;
1871 ULONG info_size = 0;
1872 void * buffer = NULL;
1873 PSYSTEM_PROCESS_INFORMATION spi = NULL;
1876 double call_time = 0.0;
1878 __kmp_elapsed( & call_time );
1880 if ( glb_call_time &&
1881 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
1882 running_threads = glb_running_threads;
1885 glb_call_time = call_time;
1888 if ( NtQuerySystemInformation == NULL ) {
1889 running_threads = -1;
1900 buff_size = glb_buff_size;
1902 buff_size = 2 * buff_size;
1905 buffer = KMP_INTERNAL_REALLOC( buffer, buff_size );
1906 if ( buffer == NULL ) {
1907 running_threads = -1;
1910 status = NtQuerySystemInformation( SystemProcessInformation, buffer, buff_size, & info_size );
1913 }
while ( status == STATUS_INFO_LENGTH_MISMATCH );
1914 glb_buff_size = buff_size;
1916 #define CHECK( cond ) \ 1918 KMP_DEBUG_ASSERT( cond ); \ 1919 if ( ! ( cond ) ) { \ 1920 running_threads = -1; \ 1925 CHECK( buff_size >= info_size );
1926 spi = PSYSTEM_PROCESS_INFORMATION( buffer );
1928 ptrdiff_t offset = uintptr_t( spi ) - uintptr_t( buffer );
1929 CHECK( 0 <= offset && offset +
sizeof( SYSTEM_PROCESS_INFORMATION ) < info_size );
1930 HANDLE pid = spi->ProcessId;
1931 ULONG num = spi->NumberOfThreads;
1933 size_t spi_size =
sizeof( SYSTEM_PROCESS_INFORMATION ) +
sizeof( SYSTEM_THREAD ) * ( num - 1 );
1934 CHECK( offset + spi_size < info_size );
1935 if ( spi->NextEntryOffset != 0 ) {
1936 CHECK( spi_size <= spi->NextEntryOffset );
1941 for (
int i = 0; i < num; ++ i ) {
1942 THREAD_STATE state = spi->Threads[ i ].State;
1945 if ( state == StateRunning ) {
1949 if ( running_threads >= max ) {
1955 if ( spi->NextEntryOffset == 0 ) {
1958 spi = PSYSTEM_PROCESS_INFORMATION( uintptr_t( spi ) + spi->NextEntryOffset );
1965 if ( buffer != NULL ) {
1966 KMP_INTERNAL_FREE( buffer );
1969 glb_running_threads = running_threads;
1971 return running_threads;