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