1 /*
2  * z_Windows_NT_util.cpp -- platform specific routines.
3  */
4 
5 //===----------------------------------------------------------------------===//
6 //
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8 // See https://llvm.org/LICENSE.txt for license information.
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "kmp.h"
14 #include "kmp_affinity.h"
15 #include "kmp_i18n.h"
16 #include "kmp_io.h"
17 #include "kmp_itt.h"
18 #include "kmp_wait_release.h"
19 
20 /* This code is related to NtQuerySystemInformation() function. This function
21    is used in the Load balance algorithm for OMP_DYNAMIC=true to find the
22    number of running threads in the system. */
23 
24 #include <ntsecapi.h> // UNICODE_STRING
25 #include <ntstatus.h>
26 #include <psapi.h>
27 #ifdef _MSC_VER
28 #pragma comment(lib, "psapi.lib")
29 #endif
30 
31 enum SYSTEM_INFORMATION_CLASS {
32   SystemProcessInformation = 5
33 }; // SYSTEM_INFORMATION_CLASS
34 
35 struct CLIENT_ID {
36   HANDLE UniqueProcess;
37   HANDLE UniqueThread;
38 }; // struct CLIENT_ID
39 
40 enum THREAD_STATE {
41   StateInitialized,
42   StateReady,
43   StateRunning,
44   StateStandby,
45   StateTerminated,
46   StateWait,
47   StateTransition,
48   StateUnknown
49 }; // enum THREAD_STATE
50 
51 struct VM_COUNTERS {
52   SIZE_T PeakVirtualSize;
53   SIZE_T VirtualSize;
54   ULONG PageFaultCount;
55   SIZE_T PeakWorkingSetSize;
56   SIZE_T WorkingSetSize;
57   SIZE_T QuotaPeakPagedPoolUsage;
58   SIZE_T QuotaPagedPoolUsage;
59   SIZE_T QuotaPeakNonPagedPoolUsage;
60   SIZE_T QuotaNonPagedPoolUsage;
61   SIZE_T PagefileUsage;
62   SIZE_T PeakPagefileUsage;
63   SIZE_T PrivatePageCount;
64 }; // struct VM_COUNTERS
65 
66 struct SYSTEM_THREAD {
67   LARGE_INTEGER KernelTime;
68   LARGE_INTEGER UserTime;
69   LARGE_INTEGER CreateTime;
70   ULONG WaitTime;
71   LPVOID StartAddress;
72   CLIENT_ID ClientId;
73   DWORD Priority;
74   LONG BasePriority;
75   ULONG ContextSwitchCount;
76   THREAD_STATE State;
77   ULONG WaitReason;
78 }; // SYSTEM_THREAD
79 
80 KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, KernelTime) == 0);
81 #if KMP_ARCH_X86
82 KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 28);
83 KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 52);
84 #else
85 KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 32);
86 KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 68);
87 #endif
88 
89 struct SYSTEM_PROCESS_INFORMATION {
90   ULONG NextEntryOffset;
91   ULONG NumberOfThreads;
92   LARGE_INTEGER Reserved[3];
93   LARGE_INTEGER CreateTime;
94   LARGE_INTEGER UserTime;
95   LARGE_INTEGER KernelTime;
96   UNICODE_STRING ImageName;
97   DWORD BasePriority;
98   HANDLE ProcessId;
99   HANDLE ParentProcessId;
100   ULONG HandleCount;
101   ULONG Reserved2[2];
102   VM_COUNTERS VMCounters;
103   IO_COUNTERS IOCounters;
104   SYSTEM_THREAD Threads[1];
105 }; // SYSTEM_PROCESS_INFORMATION
106 typedef SYSTEM_PROCESS_INFORMATION *PSYSTEM_PROCESS_INFORMATION;
107 
108 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, NextEntryOffset) == 0);
109 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, CreateTime) == 32);
110 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ImageName) == 56);
111 #if KMP_ARCH_X86
112 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 68);
113 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 76);
114 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 88);
115 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 136);
116 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 184);
117 #else
118 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 80);
119 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 96);
120 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 112);
121 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 208);
122 KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 256);
123 #endif
124 
125 typedef NTSTATUS(NTAPI *NtQuerySystemInformation_t)(SYSTEM_INFORMATION_CLASS,
126                                                     PVOID, ULONG, PULONG);
127 NtQuerySystemInformation_t NtQuerySystemInformation = NULL;
128 
129 HMODULE ntdll = NULL;
130 
131 /* End of NtQuerySystemInformation()-related code */
132 
133 static HMODULE kernel32 = NULL;
134 
135 #if KMP_HANDLE_SIGNALS
136 typedef void (*sig_func_t)(int);
137 static sig_func_t __kmp_sighldrs[NSIG];
138 static int __kmp_siginstalled[NSIG];
139 #endif
140 
141 #if KMP_USE_MONITOR
142 static HANDLE __kmp_monitor_ev;
143 #endif
144 static kmp_int64 __kmp_win32_time;
145 double __kmp_win32_tick;
146 
147 int __kmp_init_runtime = FALSE;
148 CRITICAL_SECTION __kmp_win32_section;
149 
__kmp_win32_mutex_init(kmp_win32_mutex_t * mx)150 void __kmp_win32_mutex_init(kmp_win32_mutex_t *mx) {
151   InitializeCriticalSection(&mx->cs);
152 #if USE_ITT_BUILD
153   __kmp_itt_system_object_created(&mx->cs, "Critical Section");
154 #endif /* USE_ITT_BUILD */
155 }
156 
__kmp_win32_mutex_destroy(kmp_win32_mutex_t * mx)157 void __kmp_win32_mutex_destroy(kmp_win32_mutex_t *mx) {
158   DeleteCriticalSection(&mx->cs);
159 }
160 
__kmp_win32_mutex_lock(kmp_win32_mutex_t * mx)161 void __kmp_win32_mutex_lock(kmp_win32_mutex_t *mx) {
162   EnterCriticalSection(&mx->cs);
163 }
164 
__kmp_win32_mutex_trylock(kmp_win32_mutex_t * mx)165 int __kmp_win32_mutex_trylock(kmp_win32_mutex_t *mx) {
166   return TryEnterCriticalSection(&mx->cs);
167 }
168 
__kmp_win32_mutex_unlock(kmp_win32_mutex_t * mx)169 void __kmp_win32_mutex_unlock(kmp_win32_mutex_t *mx) {
170   LeaveCriticalSection(&mx->cs);
171 }
172 
__kmp_win32_cond_init(kmp_win32_cond_t * cv)173 void __kmp_win32_cond_init(kmp_win32_cond_t *cv) {
174   cv->waiters_count_ = 0;
175   cv->wait_generation_count_ = 0;
176   cv->release_count_ = 0;
177 
178   /* Initialize the critical section */
179   __kmp_win32_mutex_init(&cv->waiters_count_lock_);
180 
181   /* Create a manual-reset event. */
182   cv->event_ = CreateEvent(NULL, // no security
183                            TRUE, // manual-reset
184                            FALSE, // non-signaled initially
185                            NULL); // unnamed
186 #if USE_ITT_BUILD
187   __kmp_itt_system_object_created(cv->event_, "Event");
188 #endif /* USE_ITT_BUILD */
189 }
190 
__kmp_win32_cond_destroy(kmp_win32_cond_t * cv)191 void __kmp_win32_cond_destroy(kmp_win32_cond_t *cv) {
192   __kmp_win32_mutex_destroy(&cv->waiters_count_lock_);
193   __kmp_free_handle(cv->event_);
194   memset(cv, '\0', sizeof(*cv));
195 }
196 
197 /* TODO associate cv with a team instead of a thread so as to optimize
198    the case where we wake up a whole team */
199 
200 template <class C>
__kmp_win32_cond_wait(kmp_win32_cond_t * cv,kmp_win32_mutex_t * mx,kmp_info_t * th,C * flag)201 static void __kmp_win32_cond_wait(kmp_win32_cond_t *cv, kmp_win32_mutex_t *mx,
202                                   kmp_info_t *th, C *flag) {
203   int my_generation;
204   int last_waiter;
205 
206   /* Avoid race conditions */
207   __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
208 
209   /* Increment count of waiters */
210   cv->waiters_count_++;
211 
212   /* Store current generation in our activation record. */
213   my_generation = cv->wait_generation_count_;
214 
215   __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
216   __kmp_win32_mutex_unlock(mx);
217 
218   for (;;) {
219     int wait_done = 0;
220     DWORD res, timeout = 5000; // just tried to quess an appropriate number
221     /* Wait until the event is signaled */
222     res = WaitForSingleObject(cv->event_, timeout);
223 
224     if (res == WAIT_OBJECT_0) {
225       // event signaled
226       __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
227       /* Exit the loop when the <cv->event_> is signaled and there are still
228          waiting threads from this <wait_generation> that haven't been released
229          from this wait yet. */
230       wait_done = (cv->release_count_ > 0) &&
231                   (cv->wait_generation_count_ != my_generation);
232       __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
233     } else if (res == WAIT_TIMEOUT || res == WAIT_FAILED) {
234       // check if the flag and cv counters are in consistent state
235       // as MS sent us debug dump whith inconsistent state of data
236       __kmp_win32_mutex_lock(mx);
237       typename C::flag_t old_f = flag->set_sleeping();
238       if (!flag->done_check_val(old_f & ~KMP_BARRIER_SLEEP_STATE)) {
239         __kmp_win32_mutex_unlock(mx);
240         continue;
241       }
242       // condition fulfilled, exiting
243       old_f = flag->unset_sleeping();
244       KMP_DEBUG_ASSERT(old_f & KMP_BARRIER_SLEEP_STATE);
245       TCW_PTR(th->th.th_sleep_loc, NULL);
246       KF_TRACE(50,
247                ("__kmp_win32_cond_wait: exiting, condition "
248                 "fulfilled: flag's loc(%p): %u => %u\n",
249                 flag->get(), (unsigned int)old_f, (unsigned int)flag->load()));
250 
251       __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
252       KMP_DEBUG_ASSERT(cv->waiters_count_ > 0);
253       cv->release_count_ = cv->waiters_count_;
254       cv->wait_generation_count_++;
255       wait_done = 1;
256       __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
257 
258       __kmp_win32_mutex_unlock(mx);
259     }
260     /* there used to be a semicolon after the if statement, it looked like a
261        bug, so i removed it */
262     if (wait_done)
263       break;
264   }
265 
266   __kmp_win32_mutex_lock(mx);
267   __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
268 
269   cv->waiters_count_--;
270   cv->release_count_--;
271 
272   last_waiter = (cv->release_count_ == 0);
273 
274   __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
275 
276   if (last_waiter) {
277     /* We're the last waiter to be notified, so reset the manual event. */
278     ResetEvent(cv->event_);
279   }
280 }
281 
__kmp_win32_cond_broadcast(kmp_win32_cond_t * cv)282 void __kmp_win32_cond_broadcast(kmp_win32_cond_t *cv) {
283   __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
284 
285   if (cv->waiters_count_ > 0) {
286     SetEvent(cv->event_);
287     /* Release all the threads in this generation. */
288 
289     cv->release_count_ = cv->waiters_count_;
290 
291     /* Start a new generation. */
292     cv->wait_generation_count_++;
293   }
294 
295   __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
296 }
297 
__kmp_win32_cond_signal(kmp_win32_cond_t * cv)298 void __kmp_win32_cond_signal(kmp_win32_cond_t *cv) {
299   __kmp_win32_cond_broadcast(cv);
300 }
301 
__kmp_enable(int new_state)302 void __kmp_enable(int new_state) {
303   if (__kmp_init_runtime)
304     LeaveCriticalSection(&__kmp_win32_section);
305 }
306 
__kmp_disable(int * old_state)307 void __kmp_disable(int *old_state) {
308   *old_state = 0;
309 
310   if (__kmp_init_runtime)
311     EnterCriticalSection(&__kmp_win32_section);
312 }
313 
__kmp_suspend_initialize(void)314 void __kmp_suspend_initialize(void) { /* do nothing */
315 }
316 
__kmp_suspend_initialize_thread(kmp_info_t * th)317 void __kmp_suspend_initialize_thread(kmp_info_t *th) {
318   int old_value = KMP_ATOMIC_LD_RLX(&th->th.th_suspend_init);
319   int new_value = TRUE;
320   // Return if already initialized
321   if (old_value == new_value)
322     return;
323   // Wait, then return if being initialized
324   if (old_value == -1 ||
325       !__kmp_atomic_compare_store(&th->th.th_suspend_init, old_value, -1)) {
326     while (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init) != new_value) {
327       KMP_CPU_PAUSE();
328     }
329   } else {
330     // Claim to be the initializer and do initializations
331     __kmp_win32_cond_init(&th->th.th_suspend_cv);
332     __kmp_win32_mutex_init(&th->th.th_suspend_mx);
333     KMP_ATOMIC_ST_REL(&th->th.th_suspend_init, new_value);
334   }
335 }
336 
__kmp_suspend_uninitialize_thread(kmp_info_t * th)337 void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
338   if (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init)) {
339     /* this means we have initialize the suspension pthread objects for this
340        thread in this instance of the process */
341     __kmp_win32_cond_destroy(&th->th.th_suspend_cv);
342     __kmp_win32_mutex_destroy(&th->th.th_suspend_mx);
343     KMP_ATOMIC_ST_REL(&th->th.th_suspend_init, FALSE);
344   }
345 }
346 
__kmp_try_suspend_mx(kmp_info_t * th)347 int __kmp_try_suspend_mx(kmp_info_t *th) {
348   return __kmp_win32_mutex_trylock(&th->th.th_suspend_mx);
349 }
350 
__kmp_lock_suspend_mx(kmp_info_t * th)351 void __kmp_lock_suspend_mx(kmp_info_t *th) {
352   __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
353 }
354 
__kmp_unlock_suspend_mx(kmp_info_t * th)355 void __kmp_unlock_suspend_mx(kmp_info_t *th) {
356   __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
357 }
358 
359 /* This routine puts the calling thread to sleep after setting the
360    sleep bit for the indicated flag variable to true. */
361 template <class C>
__kmp_suspend_template(int th_gtid,C * flag)362 static inline void __kmp_suspend_template(int th_gtid, C *flag) {
363   kmp_info_t *th = __kmp_threads[th_gtid];
364   typename C::flag_t old_spin;
365 
366   KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag's loc(%p)\n",
367                 th_gtid, flag->get()));
368 
369   __kmp_suspend_initialize_thread(th);
370   __kmp_lock_suspend_mx(th);
371 
372   KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for flag's"
373                 " loc(%p)\n",
374                 th_gtid, flag->get()));
375 
376   /* TODO: shouldn't this use release semantics to ensure that
377      __kmp_suspend_initialize_thread gets called first? */
378   old_spin = flag->set_sleeping();
379   if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME &&
380       __kmp_pause_status != kmp_soft_paused) {
381     flag->unset_sleeping();
382     __kmp_unlock_suspend_mx(th);
383     return;
384   }
385 
386   KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for flag's"
387                " loc(%p)==%u\n",
388                th_gtid, flag->get(), (unsigned int)flag->load()));
389 
390   if (flag->done_check_val(old_spin)) {
391     old_spin = flag->unset_sleeping();
392     KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
393                  "for flag's loc(%p)\n",
394                  th_gtid, flag->get()));
395   } else {
396 #ifdef DEBUG_SUSPEND
397     __kmp_suspend_count++;
398 #endif
399     /* Encapsulate in a loop as the documentation states that this may "with
400        low probability" return when the condition variable has not been signaled
401        or broadcast */
402     int deactivated = FALSE;
403     TCW_PTR(th->th.th_sleep_loc, (void *)flag);
404     while (flag->is_sleeping()) {
405       KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
406                     "kmp_win32_cond_wait()\n",
407                     th_gtid));
408       // Mark the thread as no longer active (only in the first iteration of the
409       // loop).
410       if (!deactivated) {
411         th->th.th_active = FALSE;
412         if (th->th.th_active_in_pool) {
413           th->th.th_active_in_pool = FALSE;
414           KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
415           KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
416         }
417         deactivated = TRUE;
418         __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, th,
419                               flag);
420       } else {
421         __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, th,
422                               flag);
423       }
424 
425 #ifdef KMP_DEBUG
426       if (flag->is_sleeping()) {
427         KF_TRACE(100,
428                  ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
429       }
430 #endif /* KMP_DEBUG */
431 
432     } // while
433 
434     // Mark the thread as active again (if it was previous marked as inactive)
435     if (deactivated) {
436       th->th.th_active = TRUE;
437       if (TCR_4(th->th.th_in_pool)) {
438         KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
439         th->th.th_active_in_pool = TRUE;
440       }
441     }
442   }
443 
444   __kmp_unlock_suspend_mx(th);
445   KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
446 }
447 
448 template <bool C, bool S>
__kmp_suspend_32(int th_gtid,kmp_flag_32<C,S> * flag)449 void __kmp_suspend_32(int th_gtid, kmp_flag_32<C, S> *flag) {
450   __kmp_suspend_template(th_gtid, flag);
451 }
452 template <bool C, bool S>
__kmp_suspend_64(int th_gtid,kmp_flag_64<C,S> * flag)453 void __kmp_suspend_64(int th_gtid, kmp_flag_64<C, S> *flag) {
454   __kmp_suspend_template(th_gtid, flag);
455 }
__kmp_suspend_oncore(int th_gtid,kmp_flag_oncore * flag)456 void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
457   __kmp_suspend_template(th_gtid, flag);
458 }
459 
460 template void __kmp_suspend_32<false, false>(int, kmp_flag_32<false, false> *);
461 template void __kmp_suspend_64<false, true>(int, kmp_flag_64<false, true> *);
462 template void __kmp_suspend_64<true, false>(int, kmp_flag_64<true, false> *);
463 
464 /* This routine signals the thread specified by target_gtid to wake up
465    after setting the sleep bit indicated by the flag argument to FALSE */
466 template <class C>
__kmp_resume_template(int target_gtid,C * flag)467 static inline void __kmp_resume_template(int target_gtid, C *flag) {
468   kmp_info_t *th = __kmp_threads[target_gtid];
469 
470 #ifdef KMP_DEBUG
471   int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
472 #endif
473 
474   KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
475                 gtid, target_gtid));
476 
477   __kmp_suspend_initialize_thread(th);
478   __kmp_lock_suspend_mx(th);
479 
480   if (!flag) { // coming from __kmp_null_resume_wrapper
481     flag = (C *)th->th.th_sleep_loc;
482   }
483 
484   // First, check if the flag is null or its type has changed. If so, someone
485   // else woke it up.
486   if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type
487     // simply shows what
488     // flag was cast to
489     KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
490                  "awake: flag's loc(%p)\n",
491                  gtid, target_gtid, NULL));
492     __kmp_unlock_suspend_mx(th);
493     return;
494   } else {
495     typename C::flag_t old_spin = flag->unset_sleeping();
496     if (!flag->is_sleeping_val(old_spin)) {
497       KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
498                    "awake: flag's loc(%p): %u => %u\n",
499                    gtid, target_gtid, flag->get(), (unsigned int)old_spin,
500                    (unsigned int)flag->load()));
501       __kmp_unlock_suspend_mx(th);
502       return;
503     }
504   }
505   TCW_PTR(th->th.th_sleep_loc, NULL);
506   KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep "
507                "bit for flag's loc(%p)\n",
508                gtid, target_gtid, flag->get()));
509 
510   __kmp_win32_cond_signal(&th->th.th_suspend_cv);
511   __kmp_unlock_suspend_mx(th);
512 
513   KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
514                 " for T#%d\n",
515                 gtid, target_gtid));
516 }
517 
518 template <bool C, bool S>
__kmp_resume_32(int target_gtid,kmp_flag_32<C,S> * flag)519 void __kmp_resume_32(int target_gtid, kmp_flag_32<C, S> *flag) {
520   __kmp_resume_template(target_gtid, flag);
521 }
522 template <bool C, bool S>
__kmp_resume_64(int target_gtid,kmp_flag_64<C,S> * flag)523 void __kmp_resume_64(int target_gtid, kmp_flag_64<C, S> *flag) {
524   __kmp_resume_template(target_gtid, flag);
525 }
__kmp_resume_oncore(int target_gtid,kmp_flag_oncore * flag)526 void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
527   __kmp_resume_template(target_gtid, flag);
528 }
529 
530 template void __kmp_resume_32<false, true>(int, kmp_flag_32<false, true> *);
531 template void __kmp_resume_64<false, true>(int, kmp_flag_64<false, true> *);
532 
__kmp_yield()533 void __kmp_yield() { Sleep(0); }
534 
__kmp_gtid_set_specific(int gtid)535 void __kmp_gtid_set_specific(int gtid) {
536   if (__kmp_init_gtid) {
537     KA_TRACE(50, ("__kmp_gtid_set_specific: T#%d key:%d\n", gtid,
538                   __kmp_gtid_threadprivate_key));
539     if (!TlsSetValue(__kmp_gtid_threadprivate_key, (LPVOID)(gtid + 1)))
540       KMP_FATAL(TLSSetValueFailed);
541   } else {
542     KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
543   }
544 }
545 
__kmp_gtid_get_specific()546 int __kmp_gtid_get_specific() {
547   int gtid;
548   if (!__kmp_init_gtid) {
549     KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
550                   "KMP_GTID_SHUTDOWN\n"));
551     return KMP_GTID_SHUTDOWN;
552   }
553   gtid = (int)(kmp_intptr_t)TlsGetValue(__kmp_gtid_threadprivate_key);
554   if (gtid == 0) {
555     gtid = KMP_GTID_DNE;
556   } else {
557     gtid--;
558   }
559   KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
560                 __kmp_gtid_threadprivate_key, gtid));
561   return gtid;
562 }
563 
__kmp_affinity_bind_thread(int proc)564 void __kmp_affinity_bind_thread(int proc) {
565   if (__kmp_num_proc_groups > 1) {
566     // Form the GROUP_AFFINITY struct directly, rather than filling
567     // out a bit vector and calling __kmp_set_system_affinity().
568     GROUP_AFFINITY ga;
569     KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups * CHAR_BIT *
570                                              sizeof(DWORD_PTR))));
571     ga.Group = proc / (CHAR_BIT * sizeof(DWORD_PTR));
572     ga.Mask = (unsigned long long)1 << (proc % (CHAR_BIT * sizeof(DWORD_PTR)));
573     ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
574 
575     KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
576     if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
577       DWORD error = GetLastError();
578       if (__kmp_affinity_verbose) { // AC: continue silently if not verbose
579         kmp_msg_t err_code = KMP_ERR(error);
580         __kmp_msg(kmp_ms_warning, KMP_MSG(CantSetThreadAffMask), err_code,
581                   __kmp_msg_null);
582         if (__kmp_generate_warnings == kmp_warnings_off) {
583           __kmp_str_free(&err_code.str);
584         }
585       }
586     }
587   } else {
588     kmp_affin_mask_t *mask;
589     KMP_CPU_ALLOC_ON_STACK(mask);
590     KMP_CPU_ZERO(mask);
591     KMP_CPU_SET(proc, mask);
592     __kmp_set_system_affinity(mask, TRUE);
593     KMP_CPU_FREE_FROM_STACK(mask);
594   }
595 }
596 
__kmp_affinity_determine_capable(const char * env_var)597 void __kmp_affinity_determine_capable(const char *env_var) {
598   // All versions of Windows* OS (since Win '95) support
599   // SetThreadAffinityMask().
600 
601 #if KMP_GROUP_AFFINITY
602   KMP_AFFINITY_ENABLE(__kmp_num_proc_groups * sizeof(DWORD_PTR));
603 #else
604   KMP_AFFINITY_ENABLE(sizeof(DWORD_PTR));
605 #endif
606 
607   KA_TRACE(10, ("__kmp_affinity_determine_capable: "
608                 "Windows* OS affinity interface functional (mask size = "
609                 "%" KMP_SIZE_T_SPEC ").\n",
610                 __kmp_affin_mask_size));
611 }
612 
__kmp_read_cpu_time(void)613 double __kmp_read_cpu_time(void) {
614   FILETIME CreationTime, ExitTime, KernelTime, UserTime;
615   int status;
616   double cpu_time;
617 
618   cpu_time = 0;
619 
620   status = GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime,
621                            &KernelTime, &UserTime);
622 
623   if (status) {
624     double sec = 0;
625 
626     sec += KernelTime.dwHighDateTime;
627     sec += UserTime.dwHighDateTime;
628 
629     /* Shift left by 32 bits */
630     sec *= (double)(1 << 16) * (double)(1 << 16);
631 
632     sec += KernelTime.dwLowDateTime;
633     sec += UserTime.dwLowDateTime;
634 
635     cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC;
636   }
637 
638   return cpu_time;
639 }
640 
__kmp_read_system_info(struct kmp_sys_info * info)641 int __kmp_read_system_info(struct kmp_sys_info *info) {
642   info->maxrss = 0; /* the maximum resident set size utilized (in kilobytes) */
643   info->minflt = 0; /* the number of page faults serviced without any I/O */
644   info->majflt = 0; /* the number of page faults serviced that required I/O */
645   info->nswap = 0; // the number of times a process was "swapped" out of memory
646   info->inblock = 0; // the number of times the file system had to perform input
647   info->oublock = 0; // number of times the file system had to perform output
648   info->nvcsw = 0; /* the number of times a context switch was voluntarily */
649   info->nivcsw = 0; /* the number of times a context switch was forced */
650 
651   return 1;
652 }
653 
__kmp_runtime_initialize(void)654 void __kmp_runtime_initialize(void) {
655   SYSTEM_INFO info;
656   kmp_str_buf_t path;
657   UINT path_size;
658 
659   if (__kmp_init_runtime) {
660     return;
661   }
662 
663 #if KMP_DYNAMIC_LIB
664   /* Pin dynamic library for the lifetime of application */
665   {
666     // First, turn off error message boxes
667     UINT err_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
668     HMODULE h;
669     BOOL ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
670                                      GET_MODULE_HANDLE_EX_FLAG_PIN,
671                                  (LPCTSTR)&__kmp_serial_initialize, &h);
672     (void)ret;
673     KMP_DEBUG_ASSERT2(h && ret, "OpenMP RTL cannot find itself loaded");
674     SetErrorMode(err_mode); // Restore error mode
675     KA_TRACE(10, ("__kmp_runtime_initialize: dynamic library pinned\n"));
676   }
677 #endif
678 
679   InitializeCriticalSection(&__kmp_win32_section);
680 #if USE_ITT_BUILD
681   __kmp_itt_system_object_created(&__kmp_win32_section, "Critical Section");
682 #endif /* USE_ITT_BUILD */
683   __kmp_initialize_system_tick();
684 
685 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
686   if (!__kmp_cpuinfo.initialized) {
687     __kmp_query_cpuid(&__kmp_cpuinfo);
688   }
689 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
690 
691 /* Set up minimum number of threads to switch to TLS gtid */
692 #if KMP_OS_WINDOWS && !KMP_DYNAMIC_LIB
693   // Windows* OS, static library.
694   /* New thread may use stack space previously used by another thread,
695      currently terminated. On Windows* OS, in case of static linking, we do not
696      know the moment of thread termination, and our structures (__kmp_threads
697      and __kmp_root arrays) are still keep info about dead threads. This leads
698      to problem in __kmp_get_global_thread_id() function: it wrongly finds gtid
699      (by searching through stack addresses of all known threads) for
700      unregistered foreign tread.
701 
702      Setting __kmp_tls_gtid_min to 0 workarounds this problem:
703      __kmp_get_global_thread_id() does not search through stacks, but get gtid
704      from TLS immediately.
705       --ln
706   */
707   __kmp_tls_gtid_min = 0;
708 #else
709   __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
710 #endif
711 
712   /* for the static library */
713   if (!__kmp_gtid_threadprivate_key) {
714     __kmp_gtid_threadprivate_key = TlsAlloc();
715     if (__kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES) {
716       KMP_FATAL(TLSOutOfIndexes);
717     }
718   }
719 
720   // Load ntdll.dll.
721   /* Simple GetModuleHandle( "ntdll.dl" ) is not suitable due to security issue
722      (see http://www.microsoft.com/technet/security/advisory/2269637.mspx). We
723      have to specify full path to the library. */
724   __kmp_str_buf_init(&path);
725   path_size = GetSystemDirectory(path.str, path.size);
726   KMP_DEBUG_ASSERT(path_size > 0);
727   if (path_size >= path.size) {
728     // Buffer is too short.  Expand the buffer and try again.
729     __kmp_str_buf_reserve(&path, path_size);
730     path_size = GetSystemDirectory(path.str, path.size);
731     KMP_DEBUG_ASSERT(path_size > 0);
732   }
733   if (path_size > 0 && path_size < path.size) {
734     // Now we have system directory name in the buffer.
735     // Append backslash and name of dll to form full path,
736     path.used = path_size;
737     __kmp_str_buf_print(&path, "\\%s", "ntdll.dll");
738 
739     // Now load ntdll using full path.
740     ntdll = GetModuleHandle(path.str);
741   }
742 
743   KMP_DEBUG_ASSERT(ntdll != NULL);
744   if (ntdll != NULL) {
745     NtQuerySystemInformation = (NtQuerySystemInformation_t)GetProcAddress(
746         ntdll, "NtQuerySystemInformation");
747   }
748   KMP_DEBUG_ASSERT(NtQuerySystemInformation != NULL);
749 
750 #if KMP_GROUP_AFFINITY
751   // Load kernel32.dll.
752   // Same caveat - must use full system path name.
753   if (path_size > 0 && path_size < path.size) {
754     // Truncate the buffer back to just the system path length,
755     // discarding "\\ntdll.dll", and replacing it with "kernel32.dll".
756     path.used = path_size;
757     __kmp_str_buf_print(&path, "\\%s", "kernel32.dll");
758 
759     // Load kernel32.dll using full path.
760     kernel32 = GetModuleHandle(path.str);
761     KA_TRACE(10, ("__kmp_runtime_initialize: kernel32.dll = %s\n", path.str));
762 
763     // Load the function pointers to kernel32.dll routines
764     // that may or may not exist on this system.
765     if (kernel32 != NULL) {
766       __kmp_GetActiveProcessorCount =
767           (kmp_GetActiveProcessorCount_t)GetProcAddress(
768               kernel32, "GetActiveProcessorCount");
769       __kmp_GetActiveProcessorGroupCount =
770           (kmp_GetActiveProcessorGroupCount_t)GetProcAddress(
771               kernel32, "GetActiveProcessorGroupCount");
772       __kmp_GetThreadGroupAffinity =
773           (kmp_GetThreadGroupAffinity_t)GetProcAddress(
774               kernel32, "GetThreadGroupAffinity");
775       __kmp_SetThreadGroupAffinity =
776           (kmp_SetThreadGroupAffinity_t)GetProcAddress(
777               kernel32, "SetThreadGroupAffinity");
778 
779       KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_GetActiveProcessorCount"
780                     " = %p\n",
781                     __kmp_GetActiveProcessorCount));
782       KA_TRACE(10, ("__kmp_runtime_initialize: "
783                     "__kmp_GetActiveProcessorGroupCount = %p\n",
784                     __kmp_GetActiveProcessorGroupCount));
785       KA_TRACE(10, ("__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity"
786                     " = %p\n",
787                     __kmp_GetThreadGroupAffinity));
788       KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity"
789                     " = %p\n",
790                     __kmp_SetThreadGroupAffinity));
791       KA_TRACE(10, ("__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
792                     sizeof(kmp_affin_mask_t)));
793 
794       // See if group affinity is supported on this system.
795       // If so, calculate the #groups and #procs.
796       //
797       // Group affinity was introduced with Windows* 7 OS and
798       // Windows* Server 2008 R2 OS.
799       if ((__kmp_GetActiveProcessorCount != NULL) &&
800           (__kmp_GetActiveProcessorGroupCount != NULL) &&
801           (__kmp_GetThreadGroupAffinity != NULL) &&
802           (__kmp_SetThreadGroupAffinity != NULL) &&
803           ((__kmp_num_proc_groups = __kmp_GetActiveProcessorGroupCount()) >
804            1)) {
805         // Calculate the total number of active OS procs.
806         int i;
807 
808         KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
809                       " detected\n",
810                       __kmp_num_proc_groups));
811 
812         __kmp_xproc = 0;
813 
814         for (i = 0; i < __kmp_num_proc_groups; i++) {
815           DWORD size = __kmp_GetActiveProcessorCount(i);
816           __kmp_xproc += size;
817           KA_TRACE(10, ("__kmp_runtime_initialize: proc group %d size = %d\n",
818                         i, size));
819         }
820       } else {
821         KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
822                       " detected\n",
823                       __kmp_num_proc_groups));
824       }
825     }
826   }
827   if (__kmp_num_proc_groups <= 1) {
828     GetSystemInfo(&info);
829     __kmp_xproc = info.dwNumberOfProcessors;
830   }
831 #else
832   (void)kernel32;
833   GetSystemInfo(&info);
834   __kmp_xproc = info.dwNumberOfProcessors;
835 #endif /* KMP_GROUP_AFFINITY */
836 
837   // If the OS said there were 0 procs, take a guess and use a value of 2.
838   // This is done for Linux* OS, also.  Do we need error / warning?
839   if (__kmp_xproc <= 0) {
840     __kmp_xproc = 2;
841   }
842 
843   KA_TRACE(5,
844            ("__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc));
845 
846   __kmp_str_buf_free(&path);
847 
848 #if USE_ITT_BUILD
849   __kmp_itt_initialize();
850 #endif /* USE_ITT_BUILD */
851 
852   __kmp_init_runtime = TRUE;
853 } // __kmp_runtime_initialize
854 
__kmp_runtime_destroy(void)855 void __kmp_runtime_destroy(void) {
856   if (!__kmp_init_runtime) {
857     return;
858   }
859 
860 #if USE_ITT_BUILD
861   __kmp_itt_destroy();
862 #endif /* USE_ITT_BUILD */
863 
864   /* we can't DeleteCriticalsection( & __kmp_win32_section ); */
865   /* due to the KX_TRACE() commands */
866   KA_TRACE(40, ("__kmp_runtime_destroy\n"));
867 
868   if (__kmp_gtid_threadprivate_key) {
869     TlsFree(__kmp_gtid_threadprivate_key);
870     __kmp_gtid_threadprivate_key = 0;
871   }
872 
873   __kmp_affinity_uninitialize();
874   DeleteCriticalSection(&__kmp_win32_section);
875 
876   ntdll = NULL;
877   NtQuerySystemInformation = NULL;
878 
879 #if KMP_ARCH_X86_64
880   kernel32 = NULL;
881   __kmp_GetActiveProcessorCount = NULL;
882   __kmp_GetActiveProcessorGroupCount = NULL;
883   __kmp_GetThreadGroupAffinity = NULL;
884   __kmp_SetThreadGroupAffinity = NULL;
885 #endif // KMP_ARCH_X86_64
886 
887   __kmp_init_runtime = FALSE;
888 }
889 
__kmp_terminate_thread(int gtid)890 void __kmp_terminate_thread(int gtid) {
891   kmp_info_t *th = __kmp_threads[gtid];
892 
893   if (!th)
894     return;
895 
896   KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
897 
898   if (TerminateThread(th->th.th_info.ds.ds_thread, (DWORD)-1) == FALSE) {
899     /* It's OK, the thread may have exited already */
900   }
901   __kmp_free_handle(th->th.th_info.ds.ds_thread);
902 }
903 
__kmp_clear_system_time(void)904 void __kmp_clear_system_time(void) {
905   BOOL status;
906   LARGE_INTEGER time;
907   status = QueryPerformanceCounter(&time);
908   __kmp_win32_time = (kmp_int64)time.QuadPart;
909 }
910 
__kmp_initialize_system_tick(void)911 void __kmp_initialize_system_tick(void) {
912   {
913     BOOL status;
914     LARGE_INTEGER freq;
915 
916     status = QueryPerformanceFrequency(&freq);
917     if (!status) {
918       DWORD error = GetLastError();
919       __kmp_fatal(KMP_MSG(FunctionError, "QueryPerformanceFrequency()"),
920                   KMP_ERR(error), __kmp_msg_null);
921 
922     } else {
923       __kmp_win32_tick = ((double)1.0) / (double)freq.QuadPart;
924     }
925   }
926 }
927 
928 /* Calculate the elapsed wall clock time for the user */
929 
__kmp_elapsed(double * t)930 void __kmp_elapsed(double *t) {
931   BOOL status;
932   LARGE_INTEGER now;
933   status = QueryPerformanceCounter(&now);
934   *t = ((double)now.QuadPart) * __kmp_win32_tick;
935 }
936 
937 /* Calculate the elapsed wall clock tick for the user */
938 
__kmp_elapsed_tick(double * t)939 void __kmp_elapsed_tick(double *t) { *t = __kmp_win32_tick; }
940 
__kmp_read_system_time(double * delta)941 void __kmp_read_system_time(double *delta) {
942   if (delta != NULL) {
943     BOOL status;
944     LARGE_INTEGER now;
945 
946     status = QueryPerformanceCounter(&now);
947 
948     *delta = ((double)(((kmp_int64)now.QuadPart) - __kmp_win32_time)) *
949              __kmp_win32_tick;
950   }
951 }
952 
953 /* Return the current time stamp in nsec */
__kmp_now_nsec()954 kmp_uint64 __kmp_now_nsec() {
955   LARGE_INTEGER now;
956   QueryPerformanceCounter(&now);
957   return 1e9 * __kmp_win32_tick * now.QuadPart;
958 }
959 
__kmp_launch_worker(void * arg)960 extern "C" void *__stdcall __kmp_launch_worker(void *arg) {
961   volatile void *stack_data;
962   void *exit_val;
963   void *padding = 0;
964   kmp_info_t *this_thr = (kmp_info_t *)arg;
965   int gtid;
966 
967   gtid = this_thr->th.th_info.ds.ds_gtid;
968   __kmp_gtid_set_specific(gtid);
969 #ifdef KMP_TDATA_GTID
970 #error "This define causes problems with LoadLibrary() + declspec(thread) " \
971         "on Windows* OS.  See CQ50564, tests kmp_load_library*.c and this MSDN " \
972         "reference: http://support.microsoft.com/kb/118816"
973 //__kmp_gtid = gtid;
974 #endif
975 
976 #if USE_ITT_BUILD
977   __kmp_itt_thread_name(gtid);
978 #endif /* USE_ITT_BUILD */
979 
980   __kmp_affinity_set_init_mask(gtid, FALSE);
981 
982 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
983   // Set FP control regs to be a copy of the parallel initialization thread's.
984   __kmp_clear_x87_fpu_status_word();
985   __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
986   __kmp_load_mxcsr(&__kmp_init_mxcsr);
987 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
988 
989   if (__kmp_stkoffset > 0 && gtid > 0) {
990     padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
991   }
992 
993   KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
994   this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
995   TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
996 
997   if (TCR_4(__kmp_gtid_mode) <
998       2) { // check stack only if it is used to get gtid
999     TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
1000     KMP_ASSERT(this_thr->th.th_info.ds.ds_stackgrow == FALSE);
1001     __kmp_check_stack_overlap(this_thr);
1002   }
1003   KMP_MB();
1004   exit_val = __kmp_launch_thread(this_thr);
1005   KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
1006   TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
1007   KMP_MB();
1008   return exit_val;
1009 }
1010 
1011 #if KMP_USE_MONITOR
1012 /* The monitor thread controls all of the threads in the complex */
1013 
__kmp_launch_monitor(void * arg)1014 void *__stdcall __kmp_launch_monitor(void *arg) {
1015   DWORD wait_status;
1016   kmp_thread_t monitor;
1017   int status;
1018   int interval;
1019   kmp_info_t *this_thr = (kmp_info_t *)arg;
1020 
1021   KMP_DEBUG_ASSERT(__kmp_init_monitor);
1022   TCW_4(__kmp_init_monitor, 2); // AC: Signal library that monitor has started
1023   // TODO: hide "2" in enum (like {true,false,started})
1024   this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1025   TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
1026 
1027   KMP_MB(); /* Flush all pending memory write invalidates.  */
1028   KA_TRACE(10, ("__kmp_launch_monitor: launched\n"));
1029 
1030   monitor = GetCurrentThread();
1031 
1032   /* set thread priority */
1033   status = SetThreadPriority(monitor, THREAD_PRIORITY_HIGHEST);
1034   if (!status) {
1035     DWORD error = GetLastError();
1036     __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
1037   }
1038 
1039   /* register us as monitor */
1040   __kmp_gtid_set_specific(KMP_GTID_MONITOR);
1041 #ifdef KMP_TDATA_GTID
1042 #error "This define causes problems with LoadLibrary() + declspec(thread) " \
1043         "on Windows* OS.  See CQ50564, tests kmp_load_library*.c and this MSDN " \
1044         "reference: http://support.microsoft.com/kb/118816"
1045 //__kmp_gtid = KMP_GTID_MONITOR;
1046 #endif
1047 
1048 #if USE_ITT_BUILD
1049   __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore
1050 // monitor thread.
1051 #endif /* USE_ITT_BUILD */
1052 
1053   KMP_MB(); /* Flush all pending memory write invalidates.  */
1054 
1055   interval = (1000 / __kmp_monitor_wakeups); /* in milliseconds */
1056 
1057   while (!TCR_4(__kmp_global.g.g_done)) {
1058     /*  This thread monitors the state of the system */
1059 
1060     KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
1061 
1062     wait_status = WaitForSingleObject(__kmp_monitor_ev, interval);
1063 
1064     if (wait_status == WAIT_TIMEOUT) {
1065       TCW_4(__kmp_global.g.g_time.dt.t_value,
1066             TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
1067     }
1068 
1069     KMP_MB(); /* Flush all pending memory write invalidates.  */
1070   }
1071 
1072   KA_TRACE(10, ("__kmp_launch_monitor: finished\n"));
1073 
1074   status = SetThreadPriority(monitor, THREAD_PRIORITY_NORMAL);
1075   if (!status) {
1076     DWORD error = GetLastError();
1077     __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
1078   }
1079 
1080   if (__kmp_global.g.g_abort != 0) {
1081     /* now we need to terminate the worker threads   */
1082     /* the value of t_abort is the signal we caught */
1083     int gtid;
1084 
1085     KA_TRACE(10, ("__kmp_launch_monitor: terminate sig=%d\n",
1086                   (__kmp_global.g.g_abort)));
1087 
1088     /* terminate the OpenMP worker threads */
1089     /* TODO this is not valid for sibling threads!!
1090      * the uber master might not be 0 anymore.. */
1091     for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1092       __kmp_terminate_thread(gtid);
1093 
1094     __kmp_cleanup();
1095 
1096     Sleep(0);
1097 
1098     KA_TRACE(10,
1099              ("__kmp_launch_monitor: raise sig=%d\n", __kmp_global.g.g_abort));
1100 
1101     if (__kmp_global.g.g_abort > 0) {
1102       raise(__kmp_global.g.g_abort);
1103     }
1104   }
1105 
1106   TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
1107 
1108   KMP_MB();
1109   return arg;
1110 }
1111 #endif
1112 
__kmp_create_worker(int gtid,kmp_info_t * th,size_t stack_size)1113 void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
1114   kmp_thread_t handle;
1115   DWORD idThread;
1116 
1117   KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
1118 
1119   th->th.th_info.ds.ds_gtid = gtid;
1120 
1121   if (KMP_UBER_GTID(gtid)) {
1122     int stack_data;
1123 
1124     /* TODO: GetCurrentThread() returns a pseudo-handle that is unsuitable for
1125        other threads to use. Is it appropriate to just use GetCurrentThread?
1126        When should we close this handle?  When unregistering the root? */
1127     {
1128       BOOL rc;
1129       rc = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
1130                            GetCurrentProcess(), &th->th.th_info.ds.ds_thread, 0,
1131                            FALSE, DUPLICATE_SAME_ACCESS);
1132       KMP_ASSERT(rc);
1133       KA_TRACE(10, (" __kmp_create_worker: ROOT Handle duplicated, th = %p, "
1134                     "handle = %" KMP_UINTPTR_SPEC "\n",
1135                     (LPVOID)th, th->th.th_info.ds.ds_thread));
1136       th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1137     }
1138     if (TCR_4(__kmp_gtid_mode) < 2) { // check stack only if used to get gtid
1139       /* we will dynamically update the stack range if gtid_mode == 1 */
1140       TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1141       TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1142       TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1143       __kmp_check_stack_overlap(th);
1144     }
1145   } else {
1146     KMP_MB(); /* Flush all pending memory write invalidates.  */
1147 
1148     /* Set stack size for this thread now. */
1149     KA_TRACE(10,
1150              ("__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC " bytes\n",
1151               stack_size));
1152 
1153     stack_size += gtid * __kmp_stkoffset;
1154 
1155     TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1156     TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
1157 
1158     KA_TRACE(10,
1159              ("__kmp_create_worker: (before) stack_size = %" KMP_SIZE_T_SPEC
1160               " bytes, &__kmp_launch_worker = %p, th = %p, &idThread = %p\n",
1161               (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1162               (LPVOID)th, &idThread));
1163 
1164     handle = CreateThread(
1165         NULL, (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)__kmp_launch_worker,
1166         (LPVOID)th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
1167 
1168     KA_TRACE(10,
1169              ("__kmp_create_worker: (after) stack_size = %" KMP_SIZE_T_SPEC
1170               " bytes, &__kmp_launch_worker = %p, th = %p, "
1171               "idThread = %u, handle = %" KMP_UINTPTR_SPEC "\n",
1172               (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1173               (LPVOID)th, idThread, handle));
1174 
1175     if (handle == 0) {
1176       DWORD error = GetLastError();
1177       __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
1178     } else {
1179       th->th.th_info.ds.ds_thread = handle;
1180     }
1181 
1182     KMP_MB(); /* Flush all pending memory write invalidates.  */
1183   }
1184 
1185   KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
1186 }
1187 
__kmp_still_running(kmp_info_t * th)1188 int __kmp_still_running(kmp_info_t *th) {
1189   return (WAIT_TIMEOUT == WaitForSingleObject(th->th.th_info.ds.ds_thread, 0));
1190 }
1191 
1192 #if KMP_USE_MONITOR
__kmp_create_monitor(kmp_info_t * th)1193 void __kmp_create_monitor(kmp_info_t *th) {
1194   kmp_thread_t handle;
1195   DWORD idThread;
1196   int ideal, new_ideal;
1197 
1198   if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
1199     // We don't need monitor thread in case of MAX_BLOCKTIME
1200     KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
1201                   "MAX blocktime\n"));
1202     th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
1203     th->th.th_info.ds.ds_gtid = 0;
1204     TCW_4(__kmp_init_monitor, 2); // Signal to stop waiting for monitor creation
1205     return;
1206   }
1207   KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
1208 
1209   KMP_MB(); /* Flush all pending memory write invalidates.  */
1210 
1211   __kmp_monitor_ev = CreateEvent(NULL, TRUE, FALSE, NULL);
1212   if (__kmp_monitor_ev == NULL) {
1213     DWORD error = GetLastError();
1214     __kmp_fatal(KMP_MSG(CantCreateEvent), KMP_ERR(error), __kmp_msg_null);
1215   }
1216 #if USE_ITT_BUILD
1217   __kmp_itt_system_object_created(__kmp_monitor_ev, "Event");
1218 #endif /* USE_ITT_BUILD */
1219 
1220   th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1221   th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1222 
1223   // FIXME - on Windows* OS, if __kmp_monitor_stksize = 0, figure out how
1224   // to automatically expand stacksize based on CreateThread error code.
1225   if (__kmp_monitor_stksize == 0) {
1226     __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1227   }
1228   if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
1229     __kmp_monitor_stksize = __kmp_sys_min_stksize;
1230   }
1231 
1232   KA_TRACE(10, ("__kmp_create_monitor: requested stacksize = %d bytes\n",
1233                 (int)__kmp_monitor_stksize));
1234 
1235   TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
1236 
1237   handle =
1238       CreateThread(NULL, (SIZE_T)__kmp_monitor_stksize,
1239                    (LPTHREAD_START_ROUTINE)__kmp_launch_monitor, (LPVOID)th,
1240                    STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
1241   if (handle == 0) {
1242     DWORD error = GetLastError();
1243     __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
1244   } else
1245     th->th.th_info.ds.ds_thread = handle;
1246 
1247   KMP_MB(); /* Flush all pending memory write invalidates.  */
1248 
1249   KA_TRACE(10, ("__kmp_create_monitor: monitor created %p\n",
1250                 (void *)th->th.th_info.ds.ds_thread));
1251 }
1252 #endif
1253 
1254 /* Check to see if thread is still alive.
1255    NOTE:  The ExitProcess(code) system call causes all threads to Terminate
1256    with a exit_val = code.  Because of this we can not rely on exit_val having
1257    any particular value.  So this routine may return STILL_ALIVE in exit_val
1258    even after the thread is dead. */
1259 
__kmp_is_thread_alive(kmp_info_t * th,DWORD * exit_val)1260 int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val) {
1261   DWORD rc;
1262   rc = GetExitCodeThread(th->th.th_info.ds.ds_thread, exit_val);
1263   if (rc == 0) {
1264     DWORD error = GetLastError();
1265     __kmp_fatal(KMP_MSG(FunctionError, "GetExitCodeThread()"), KMP_ERR(error),
1266                 __kmp_msg_null);
1267   }
1268   return (*exit_val == STILL_ACTIVE);
1269 }
1270 
__kmp_exit_thread(int exit_status)1271 void __kmp_exit_thread(int exit_status) {
1272   ExitThread(exit_status);
1273 } // __kmp_exit_thread
1274 
1275 // This is a common part for both __kmp_reap_worker() and __kmp_reap_monitor().
__kmp_reap_common(kmp_info_t * th)1276 static void __kmp_reap_common(kmp_info_t *th) {
1277   DWORD exit_val;
1278 
1279   KMP_MB(); /* Flush all pending memory write invalidates.  */
1280 
1281   KA_TRACE(
1282       10, ("__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid));
1283 
1284   /* 2006-10-19:
1285      There are two opposite situations:
1286      1. Windows* OS keep thread alive after it resets ds_alive flag and
1287      exits from thread function. (For example, see C70770/Q394281 "unloading of
1288      dll based on OMP is very slow".)
1289      2. Windows* OS may kill thread before it resets ds_alive flag.
1290 
1291      Right solution seems to be waiting for *either* thread termination *or*
1292      ds_alive resetting. */
1293   {
1294     // TODO: This code is very similar to KMP_WAIT. Need to generalize
1295     // KMP_WAIT to cover this usage also.
1296     void *obj = NULL;
1297     kmp_uint32 spins;
1298 #if USE_ITT_BUILD
1299     KMP_FSYNC_SPIN_INIT(obj, (void *)&th->th.th_info.ds.ds_alive);
1300 #endif /* USE_ITT_BUILD */
1301     KMP_INIT_YIELD(spins);
1302     do {
1303 #if USE_ITT_BUILD
1304       KMP_FSYNC_SPIN_PREPARE(obj);
1305 #endif /* USE_ITT_BUILD */
1306       __kmp_is_thread_alive(th, &exit_val);
1307       KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
1308     } while (exit_val == STILL_ACTIVE && TCR_4(th->th.th_info.ds.ds_alive));
1309 #if USE_ITT_BUILD
1310     if (exit_val == STILL_ACTIVE) {
1311       KMP_FSYNC_CANCEL(obj);
1312     } else {
1313       KMP_FSYNC_SPIN_ACQUIRED(obj);
1314     }
1315 #endif /* USE_ITT_BUILD */
1316   }
1317 
1318   __kmp_free_handle(th->th.th_info.ds.ds_thread);
1319 
1320   /* NOTE:  The ExitProcess(code) system call causes all threads to Terminate
1321      with a exit_val = code.  Because of this we can not rely on exit_val having
1322      any particular value. */
1323   if (exit_val == STILL_ACTIVE) {
1324     KA_TRACE(1, ("__kmp_reap_common: thread still active.\n"));
1325   } else if ((void *)exit_val != (void *)th) {
1326     KA_TRACE(1, ("__kmp_reap_common: ExitProcess / TerminateThread used?\n"));
1327   }
1328 
1329   KA_TRACE(10,
1330            ("__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
1331             "\n",
1332             th->th.th_info.ds.ds_gtid, th->th.th_info.ds.ds_thread));
1333 
1334   th->th.th_info.ds.ds_thread = 0;
1335   th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1336   th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1337   th->th.th_info.ds.ds_thread_id = 0;
1338 
1339   KMP_MB(); /* Flush all pending memory write invalidates.  */
1340 }
1341 
1342 #if KMP_USE_MONITOR
__kmp_reap_monitor(kmp_info_t * th)1343 void __kmp_reap_monitor(kmp_info_t *th) {
1344   int status;
1345 
1346   KA_TRACE(10, ("__kmp_reap_monitor: try to reap %p\n",
1347                 (void *)th->th.th_info.ds.ds_thread));
1348 
1349   // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1350   // If both tid and gtid are 0, it means the monitor did not ever start.
1351   // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1352   KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1353   if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1354     KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1355     return;
1356   }
1357 
1358   KMP_MB(); /* Flush all pending memory write invalidates.  */
1359 
1360   status = SetEvent(__kmp_monitor_ev);
1361   if (status == FALSE) {
1362     DWORD error = GetLastError();
1363     __kmp_fatal(KMP_MSG(CantSetEvent), KMP_ERR(error), __kmp_msg_null);
1364   }
1365   KA_TRACE(10, ("__kmp_reap_monitor: reaping thread (%d)\n",
1366                 th->th.th_info.ds.ds_gtid));
1367   __kmp_reap_common(th);
1368 
1369   __kmp_free_handle(__kmp_monitor_ev);
1370 
1371   KMP_MB(); /* Flush all pending memory write invalidates.  */
1372 }
1373 #endif
1374 
__kmp_reap_worker(kmp_info_t * th)1375 void __kmp_reap_worker(kmp_info_t *th) {
1376   KA_TRACE(10, ("__kmp_reap_worker: reaping thread (%d)\n",
1377                 th->th.th_info.ds.ds_gtid));
1378   __kmp_reap_common(th);
1379 }
1380 
1381 #if KMP_HANDLE_SIGNALS
1382 
__kmp_team_handler(int signo)1383 static void __kmp_team_handler(int signo) {
1384   if (__kmp_global.g.g_abort == 0) {
1385     // Stage 1 signal handler, let's shut down all of the threads.
1386     if (__kmp_debug_buf) {
1387       __kmp_dump_debug_buffer();
1388     }
1389     KMP_MB(); // Flush all pending memory write invalidates.
1390     TCW_4(__kmp_global.g.g_abort, signo);
1391     KMP_MB(); // Flush all pending memory write invalidates.
1392     TCW_4(__kmp_global.g.g_done, TRUE);
1393     KMP_MB(); // Flush all pending memory write invalidates.
1394   }
1395 } // __kmp_team_handler
1396 
__kmp_signal(int signum,sig_func_t handler)1397 static sig_func_t __kmp_signal(int signum, sig_func_t handler) {
1398   sig_func_t old = signal(signum, handler);
1399   if (old == SIG_ERR) {
1400     int error = errno;
1401     __kmp_fatal(KMP_MSG(FunctionError, "signal"), KMP_ERR(error),
1402                 __kmp_msg_null);
1403   }
1404   return old;
1405 }
1406 
__kmp_install_one_handler(int sig,sig_func_t handler,int parallel_init)1407 static void __kmp_install_one_handler(int sig, sig_func_t handler,
1408                                       int parallel_init) {
1409   sig_func_t old;
1410   KMP_MB(); /* Flush all pending memory write invalidates.  */
1411   KB_TRACE(60, ("__kmp_install_one_handler: called: sig=%d\n", sig));
1412   if (parallel_init) {
1413     old = __kmp_signal(sig, handler);
1414     // SIG_DFL on Windows* OS in NULL or 0.
1415     if (old == __kmp_sighldrs[sig]) {
1416       __kmp_siginstalled[sig] = 1;
1417     } else { // Restore/keep user's handler if one previously installed.
1418       old = __kmp_signal(sig, old);
1419     }
1420   } else {
1421     // Save initial/system signal handlers to see if user handlers installed.
1422     // 2009-09-23: It is a dead code. On Windows* OS __kmp_install_signals
1423     // called once with parallel_init == TRUE.
1424     old = __kmp_signal(sig, SIG_DFL);
1425     __kmp_sighldrs[sig] = old;
1426     __kmp_signal(sig, old);
1427   }
1428   KMP_MB(); /* Flush all pending memory write invalidates.  */
1429 } // __kmp_install_one_handler
1430 
__kmp_remove_one_handler(int sig)1431 static void __kmp_remove_one_handler(int sig) {
1432   if (__kmp_siginstalled[sig]) {
1433     sig_func_t old;
1434     KMP_MB(); // Flush all pending memory write invalidates.
1435     KB_TRACE(60, ("__kmp_remove_one_handler: called: sig=%d\n", sig));
1436     old = __kmp_signal(sig, __kmp_sighldrs[sig]);
1437     if (old != __kmp_team_handler) {
1438       KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1439                     "restoring: sig=%d\n",
1440                     sig));
1441       old = __kmp_signal(sig, old);
1442     }
1443     __kmp_sighldrs[sig] = NULL;
1444     __kmp_siginstalled[sig] = 0;
1445     KMP_MB(); // Flush all pending memory write invalidates.
1446   }
1447 } // __kmp_remove_one_handler
1448 
__kmp_install_signals(int parallel_init)1449 void __kmp_install_signals(int parallel_init) {
1450   KB_TRACE(10, ("__kmp_install_signals: called\n"));
1451   if (!__kmp_handle_signals) {
1452     KB_TRACE(10, ("__kmp_install_signals: KMP_HANDLE_SIGNALS is false - "
1453                   "handlers not installed\n"));
1454     return;
1455   }
1456   __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1457   __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1458   __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1459   __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1460   __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1461   __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
1462 } // __kmp_install_signals
1463 
__kmp_remove_signals(void)1464 void __kmp_remove_signals(void) {
1465   int sig;
1466   KB_TRACE(10, ("__kmp_remove_signals: called\n"));
1467   for (sig = 1; sig < NSIG; ++sig) {
1468     __kmp_remove_one_handler(sig);
1469   }
1470 } // __kmp_remove_signals
1471 
1472 #endif // KMP_HANDLE_SIGNALS
1473 
1474 /* Put the thread to sleep for a time period */
__kmp_thread_sleep(int millis)1475 void __kmp_thread_sleep(int millis) {
1476   DWORD status;
1477 
1478   status = SleepEx((DWORD)millis, FALSE);
1479   if (status) {
1480     DWORD error = GetLastError();
1481     __kmp_fatal(KMP_MSG(FunctionError, "SleepEx()"), KMP_ERR(error),
1482                 __kmp_msg_null);
1483   }
1484 }
1485 
1486 // Determine whether the given address is mapped into the current address space.
__kmp_is_address_mapped(void * addr)1487 int __kmp_is_address_mapped(void *addr) {
1488   DWORD status;
1489   MEMORY_BASIC_INFORMATION lpBuffer;
1490   SIZE_T dwLength;
1491 
1492   dwLength = sizeof(MEMORY_BASIC_INFORMATION);
1493 
1494   status = VirtualQuery(addr, &lpBuffer, dwLength);
1495 
1496   return !(((lpBuffer.State == MEM_RESERVE) || (lpBuffer.State == MEM_FREE)) ||
1497            ((lpBuffer.Protect == PAGE_NOACCESS) ||
1498             (lpBuffer.Protect == PAGE_EXECUTE)));
1499 }
1500 
__kmp_hardware_timestamp(void)1501 kmp_uint64 __kmp_hardware_timestamp(void) {
1502   kmp_uint64 r = 0;
1503 
1504   QueryPerformanceCounter((LARGE_INTEGER *)&r);
1505   return r;
1506 }
1507 
1508 /* Free handle and check the error code */
__kmp_free_handle(kmp_thread_t tHandle)1509 void __kmp_free_handle(kmp_thread_t tHandle) {
1510   /* called with parameter type HANDLE also, thus suppose kmp_thread_t defined
1511    * as HANDLE */
1512   BOOL rc;
1513   rc = CloseHandle(tHandle);
1514   if (!rc) {
1515     DWORD error = GetLastError();
1516     __kmp_fatal(KMP_MSG(CantCloseHandle), KMP_ERR(error), __kmp_msg_null);
1517   }
1518 }
1519 
__kmp_get_load_balance(int max)1520 int __kmp_get_load_balance(int max) {
1521   static ULONG glb_buff_size = 100 * 1024;
1522 
1523   // Saved count of the running threads for the thread balance algorithm
1524   static int glb_running_threads = 0;
1525   static double glb_call_time = 0; /* Thread balance algorithm call time */
1526 
1527   int running_threads = 0; // Number of running threads in the system.
1528   NTSTATUS status = 0;
1529   ULONG buff_size = 0;
1530   ULONG info_size = 0;
1531   void *buffer = NULL;
1532   PSYSTEM_PROCESS_INFORMATION spi = NULL;
1533   int first_time = 1;
1534 
1535   double call_time = 0.0; // start, finish;
1536 
1537   __kmp_elapsed(&call_time);
1538 
1539   if (glb_call_time &&
1540       (call_time - glb_call_time < __kmp_load_balance_interval)) {
1541     running_threads = glb_running_threads;
1542     goto finish;
1543   }
1544   glb_call_time = call_time;
1545 
1546   // Do not spend time on running algorithm if we have a permanent error.
1547   if (NtQuerySystemInformation == NULL) {
1548     running_threads = -1;
1549     goto finish;
1550   }
1551 
1552   if (max <= 0) {
1553     max = INT_MAX;
1554   }
1555 
1556   do {
1557 
1558     if (first_time) {
1559       buff_size = glb_buff_size;
1560     } else {
1561       buff_size = 2 * buff_size;
1562     }
1563 
1564     buffer = KMP_INTERNAL_REALLOC(buffer, buff_size);
1565     if (buffer == NULL) {
1566       running_threads = -1;
1567       goto finish;
1568     }
1569     status = NtQuerySystemInformation(SystemProcessInformation, buffer,
1570                                       buff_size, &info_size);
1571     first_time = 0;
1572 
1573   } while (status == STATUS_INFO_LENGTH_MISMATCH);
1574   glb_buff_size = buff_size;
1575 
1576 #define CHECK(cond)                                                            \
1577   {                                                                            \
1578     KMP_DEBUG_ASSERT(cond);                                                    \
1579     if (!(cond)) {                                                             \
1580       running_threads = -1;                                                    \
1581       goto finish;                                                             \
1582     }                                                                          \
1583   }
1584 
1585   CHECK(buff_size >= info_size);
1586   spi = PSYSTEM_PROCESS_INFORMATION(buffer);
1587   for (;;) {
1588     ptrdiff_t offset = uintptr_t(spi) - uintptr_t(buffer);
1589     CHECK(0 <= offset &&
1590           offset + sizeof(SYSTEM_PROCESS_INFORMATION) < info_size);
1591     HANDLE pid = spi->ProcessId;
1592     ULONG num = spi->NumberOfThreads;
1593     CHECK(num >= 1);
1594     size_t spi_size =
1595         sizeof(SYSTEM_PROCESS_INFORMATION) + sizeof(SYSTEM_THREAD) * (num - 1);
1596     CHECK(offset + spi_size <
1597           info_size); // Make sure process info record fits the buffer.
1598     if (spi->NextEntryOffset != 0) {
1599       CHECK(spi_size <=
1600             spi->NextEntryOffset); // And do not overlap with the next record.
1601     }
1602     // pid == 0 corresponds to the System Idle Process. It always has running
1603     // threads on all cores. So, we don't consider the running threads of this
1604     // process.
1605     if (pid != 0) {
1606       for (int i = 0; i < num; ++i) {
1607         THREAD_STATE state = spi->Threads[i].State;
1608         // Count threads that have Ready or Running state.
1609         // !!! TODO: Why comment does not match the code???
1610         if (state == StateRunning) {
1611           ++running_threads;
1612           // Stop counting running threads if the number is already greater than
1613           // the number of available cores
1614           if (running_threads >= max) {
1615             goto finish;
1616           }
1617         }
1618       }
1619     }
1620     if (spi->NextEntryOffset == 0) {
1621       break;
1622     }
1623     spi = PSYSTEM_PROCESS_INFORMATION(uintptr_t(spi) + spi->NextEntryOffset);
1624   }
1625 
1626 #undef CHECK
1627 
1628 finish: // Clean up and exit.
1629 
1630   if (buffer != NULL) {
1631     KMP_INTERNAL_FREE(buffer);
1632   }
1633 
1634   glb_running_threads = running_threads;
1635 
1636   return running_threads;
1637 } //__kmp_get_load_balance()
1638 
1639 // Find symbol from the loaded modules
__kmp_lookup_symbol(const char * name)1640 void *__kmp_lookup_symbol(const char *name) {
1641   HANDLE process = GetCurrentProcess();
1642   DWORD needed;
1643   HMODULE *modules = nullptr;
1644   if (!EnumProcessModules(process, modules, 0, &needed))
1645     return nullptr;
1646   DWORD num_modules = needed / sizeof(HMODULE);
1647   modules = (HMODULE *)malloc(num_modules * sizeof(HMODULE));
1648   if (!EnumProcessModules(process, modules, needed, &needed)) {
1649     free(modules);
1650     return nullptr;
1651   }
1652   void *proc = nullptr;
1653   for (uint32_t i = 0; i < num_modules; i++) {
1654     proc = (void *)GetProcAddress(modules[i], name);
1655     if (proc)
1656       break;
1657   }
1658   free(modules);
1659   return proc;
1660 }
1661 
1662 // Functions for hidden helper task
__kmp_hidden_helper_worker_thread_wait()1663 void __kmp_hidden_helper_worker_thread_wait() {
1664   KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1665 }
1666 
__kmp_do_initialize_hidden_helper_threads()1667 void __kmp_do_initialize_hidden_helper_threads() {
1668   KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1669 }
1670 
__kmp_hidden_helper_threads_initz_wait()1671 void __kmp_hidden_helper_threads_initz_wait() {
1672   KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1673 }
1674 
__kmp_hidden_helper_initz_release()1675 void __kmp_hidden_helper_initz_release() {
1676   KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1677 }
1678 
__kmp_hidden_helper_main_thread_wait()1679 void __kmp_hidden_helper_main_thread_wait() {
1680   KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1681 }
1682 
__kmp_hidden_helper_main_thread_release()1683 void __kmp_hidden_helper_main_thread_release() {
1684   KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1685 }
1686 
__kmp_hidden_helper_worker_thread_signal()1687 void __kmp_hidden_helper_worker_thread_signal() {
1688   KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1689 }
1690 
__kmp_hidden_helper_threads_deinitz_wait()1691 void __kmp_hidden_helper_threads_deinitz_wait() {
1692   KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1693 }
1694 
__kmp_hidden_helper_threads_deinitz_release()1695 void __kmp_hidden_helper_threads_deinitz_release() {
1696   KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1697 }
1698