blob: c503e9c65c80465c46392be775ff0288af6df228 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
Jonathan Peytonde4749b2016-12-14 23:01:24 +00002 * z_Windows_NT_util.cpp -- platform specific routines.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
Jim Cownie5e8470a2013-09-27 10:38:44 +00005//===----------------------------------------------------------------------===//
6//
Chandler Carruth57b08b02019-01-19 10:56:40 +00007// 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
Jim Cownie5e8470a2013-09-27 10:38:44 +000010//
11//===----------------------------------------------------------------------===//
12
Jim Cownie5e8470a2013-09-27 10:38:44 +000013#include "kmp.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000014#include "kmp_affinity.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000015#include "kmp_i18n.h"
16#include "kmp_io.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000017#include "kmp_itt.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000018#include "kmp_wait_release.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000019
Jim Cownie4cc4bb42014-10-07 16:25:50 +000020/* This code is related to NtQuerySystemInformation() function. This function
21 is used in the Load balance algorithm for OMP_DYNAMIC=true to find the
Jim Cownie5e8470a2013-09-27 10:38:44 +000022 number of running threads in the system. */
23
Jonathan Peyton30419822017-05-12 18:01:32 +000024#include <ntsecapi.h> // UNICODE_STRING
Jim Cownie5e8470a2013-09-27 10:38:44 +000025#include <ntstatus.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000026
27enum SYSTEM_INFORMATION_CLASS {
Jonathan Peyton30419822017-05-12 18:01:32 +000028 SystemProcessInformation = 5
Jim Cownie5e8470a2013-09-27 10:38:44 +000029}; // SYSTEM_INFORMATION_CLASS
30
31struct CLIENT_ID {
Jonathan Peyton30419822017-05-12 18:01:32 +000032 HANDLE UniqueProcess;
33 HANDLE UniqueThread;
Jim Cownie5e8470a2013-09-27 10:38:44 +000034}; // struct CLIENT_ID
35
36enum THREAD_STATE {
Jonathan Peyton30419822017-05-12 18:01:32 +000037 StateInitialized,
38 StateReady,
39 StateRunning,
40 StateStandby,
41 StateTerminated,
42 StateWait,
43 StateTransition,
44 StateUnknown
Jim Cownie5e8470a2013-09-27 10:38:44 +000045}; // enum THREAD_STATE
46
47struct VM_COUNTERS {
Jonathan Peyton30419822017-05-12 18:01:32 +000048 SIZE_T PeakVirtualSize;
49 SIZE_T VirtualSize;
50 ULONG PageFaultCount;
51 SIZE_T PeakWorkingSetSize;
52 SIZE_T WorkingSetSize;
53 SIZE_T QuotaPeakPagedPoolUsage;
54 SIZE_T QuotaPagedPoolUsage;
55 SIZE_T QuotaPeakNonPagedPoolUsage;
56 SIZE_T QuotaNonPagedPoolUsage;
57 SIZE_T PagefileUsage;
58 SIZE_T PeakPagefileUsage;
59 SIZE_T PrivatePageCount;
Jim Cownie5e8470a2013-09-27 10:38:44 +000060}; // struct VM_COUNTERS
61
62struct SYSTEM_THREAD {
Jonathan Peyton30419822017-05-12 18:01:32 +000063 LARGE_INTEGER KernelTime;
64 LARGE_INTEGER UserTime;
65 LARGE_INTEGER CreateTime;
66 ULONG WaitTime;
67 LPVOID StartAddress;
68 CLIENT_ID ClientId;
69 DWORD Priority;
70 LONG BasePriority;
71 ULONG ContextSwitchCount;
72 THREAD_STATE State;
73 ULONG WaitReason;
Jim Cownie5e8470a2013-09-27 10:38:44 +000074}; // SYSTEM_THREAD
75
Jonathan Peyton30419822017-05-12 18:01:32 +000076KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, KernelTime) == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +000077#if KMP_ARCH_X86
Jonathan Peyton30419822017-05-12 18:01:32 +000078KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 28);
79KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 52);
Jim Cownie5e8470a2013-09-27 10:38:44 +000080#else
Jonathan Peyton30419822017-05-12 18:01:32 +000081KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 32);
82KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 68);
Jim Cownie5e8470a2013-09-27 10:38:44 +000083#endif
84
85struct SYSTEM_PROCESS_INFORMATION {
Jonathan Peyton30419822017-05-12 18:01:32 +000086 ULONG NextEntryOffset;
87 ULONG NumberOfThreads;
88 LARGE_INTEGER Reserved[3];
89 LARGE_INTEGER CreateTime;
90 LARGE_INTEGER UserTime;
91 LARGE_INTEGER KernelTime;
92 UNICODE_STRING ImageName;
93 DWORD BasePriority;
94 HANDLE ProcessId;
95 HANDLE ParentProcessId;
96 ULONG HandleCount;
97 ULONG Reserved2[2];
98 VM_COUNTERS VMCounters;
99 IO_COUNTERS IOCounters;
100 SYSTEM_THREAD Threads[1];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000101}; // SYSTEM_PROCESS_INFORMATION
Jonathan Peyton30419822017-05-12 18:01:32 +0000102typedef SYSTEM_PROCESS_INFORMATION *PSYSTEM_PROCESS_INFORMATION;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000103
Jonathan Peyton30419822017-05-12 18:01:32 +0000104KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, NextEntryOffset) == 0);
105KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, CreateTime) == 32);
106KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ImageName) == 56);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000107#if KMP_ARCH_X86
Jonathan Peyton30419822017-05-12 18:01:32 +0000108KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 68);
109KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 76);
110KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 88);
111KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 136);
112KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 184);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000113#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000114KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 80);
115KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 96);
116KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 112);
117KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 208);
118KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 256);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000119#endif
120
Jonathan Peyton30419822017-05-12 18:01:32 +0000121typedef NTSTATUS(NTAPI *NtQuerySystemInformation_t)(SYSTEM_INFORMATION_CLASS,
122 PVOID, ULONG, PULONG);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000123NtQuerySystemInformation_t NtQuerySystemInformation = NULL;
124
125HMODULE ntdll = NULL;
126
127/* End of NtQuerySystemInformation()-related code */
128
Jim Cownie5e8470a2013-09-27 10:38:44 +0000129static HMODULE kernel32 = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000130
Jim Cownie5e8470a2013-09-27 10:38:44 +0000131#if KMP_HANDLE_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000132typedef void (*sig_func_t)(int);
133static sig_func_t __kmp_sighldrs[NSIG];
134static int __kmp_siginstalled[NSIG];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000135#endif
136
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000137#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +0000138static HANDLE __kmp_monitor_ev;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000139#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000140static kmp_int64 __kmp_win32_time;
141double __kmp_win32_tick;
142
143int __kmp_init_runtime = FALSE;
144CRITICAL_SECTION __kmp_win32_section;
145
Jonathan Peyton30419822017-05-12 18:01:32 +0000146void __kmp_win32_mutex_init(kmp_win32_mutex_t *mx) {
147 InitializeCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000148#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000149 __kmp_itt_system_object_created(&mx->cs, "Critical Section");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000150#endif /* USE_ITT_BUILD */
151}
152
Jonathan Peyton30419822017-05-12 18:01:32 +0000153void __kmp_win32_mutex_destroy(kmp_win32_mutex_t *mx) {
154 DeleteCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000155}
156
Jonathan Peyton30419822017-05-12 18:01:32 +0000157void __kmp_win32_mutex_lock(kmp_win32_mutex_t *mx) {
158 EnterCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000159}
160
Jonathan Peyton9b8bb322019-01-16 20:07:39 +0000161int __kmp_win32_mutex_trylock(kmp_win32_mutex_t *mx) {
162 return TryEnterCriticalSection(&mx->cs);
163}
164
Jonathan Peyton30419822017-05-12 18:01:32 +0000165void __kmp_win32_mutex_unlock(kmp_win32_mutex_t *mx) {
166 LeaveCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000167}
168
Jonathan Peyton30419822017-05-12 18:01:32 +0000169void __kmp_win32_cond_init(kmp_win32_cond_t *cv) {
170 cv->waiters_count_ = 0;
171 cv->wait_generation_count_ = 0;
172 cv->release_count_ = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000173
Jonathan Peyton30419822017-05-12 18:01:32 +0000174 /* Initialize the critical section */
175 __kmp_win32_mutex_init(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000176
Jonathan Peyton30419822017-05-12 18:01:32 +0000177 /* Create a manual-reset event. */
178 cv->event_ = CreateEvent(NULL, // no security
179 TRUE, // manual-reset
180 FALSE, // non-signaled initially
181 NULL); // unnamed
Jim Cownie5e8470a2013-09-27 10:38:44 +0000182#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000183 __kmp_itt_system_object_created(cv->event_, "Event");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000184#endif /* USE_ITT_BUILD */
185}
186
Jonathan Peyton30419822017-05-12 18:01:32 +0000187void __kmp_win32_cond_destroy(kmp_win32_cond_t *cv) {
188 __kmp_win32_mutex_destroy(&cv->waiters_count_lock_);
189 __kmp_free_handle(cv->event_);
190 memset(cv, '\0', sizeof(*cv));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000191}
192
193/* TODO associate cv with a team instead of a thread so as to optimize
Jonathan Peyton30419822017-05-12 18:01:32 +0000194 the case where we wake up a whole team */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000195
Jonathan Peytonb727d382019-04-04 20:35:29 +0000196template <class C>
197static void __kmp_win32_cond_wait(kmp_win32_cond_t *cv, kmp_win32_mutex_t *mx,
198 kmp_info_t *th, C *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000199 int my_generation;
200 int last_waiter;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000201
Jonathan Peyton30419822017-05-12 18:01:32 +0000202 /* Avoid race conditions */
203 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000204
Jonathan Peyton30419822017-05-12 18:01:32 +0000205 /* Increment count of waiters */
206 cv->waiters_count_++;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000207
Jonathan Peyton30419822017-05-12 18:01:32 +0000208 /* Store current generation in our activation record. */
209 my_generation = cv->wait_generation_count_;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000210
Jonathan Peyton30419822017-05-12 18:01:32 +0000211 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
212 __kmp_win32_mutex_unlock(mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000213
Jonathan Peyton30419822017-05-12 18:01:32 +0000214 for (;;) {
Jonathan Peytonb727d382019-04-04 20:35:29 +0000215 int wait_done = 0;
216 DWORD res, timeout = 5000; // just tried to quess an appropriate number
Jonathan Peyton30419822017-05-12 18:01:32 +0000217 /* Wait until the event is signaled */
Jonathan Peytonb727d382019-04-04 20:35:29 +0000218 res = WaitForSingleObject(cv->event_, timeout);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000219
Jonathan Peytonb727d382019-04-04 20:35:29 +0000220 if (res == WAIT_OBJECT_0) {
221 // event signaled
222 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
223 /* Exit the loop when the <cv->event_> is signaled and there are still
224 waiting threads from this <wait_generation> that haven't been released
225 from this wait yet. */
226 wait_done = (cv->release_count_ > 0) &&
227 (cv->wait_generation_count_ != my_generation);
228 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
229 } else if (res == WAIT_TIMEOUT || res == WAIT_FAILED) {
230 // check if the flag and cv counters are in consistent state
231 // as MS sent us debug dump whith inconsistent state of data
232 __kmp_win32_mutex_lock(mx);
233 typename C::flag_t old_f = flag->set_sleeping();
234 if (!flag->done_check_val(old_f & ~KMP_BARRIER_SLEEP_STATE)) {
235 __kmp_win32_mutex_unlock(mx);
236 continue;
237 }
238 // condition fulfilled, exiting
239 old_f = flag->unset_sleeping();
240 KMP_DEBUG_ASSERT(old_f & KMP_BARRIER_SLEEP_STATE);
241 TCW_PTR(th->th.th_sleep_loc, NULL);
242 KF_TRACE(50, ("__kmp_win32_cond_wait: exiting, condition "
243 "fulfilled: flag's loc(%p): %u => %u\n",
244 flag->get(), old_f, *(flag->get())));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000245
Jonathan Peytonb727d382019-04-04 20:35:29 +0000246 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
247 KMP_DEBUG_ASSERT(cv->waiters_count_ > 0);
248 cv->release_count_ = cv->waiters_count_;
249 cv->wait_generation_count_++;
250 wait_done = 1;
251 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000252
Jonathan Peytonb727d382019-04-04 20:35:29 +0000253 __kmp_win32_mutex_unlock(mx);
254 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000255 /* there used to be a semicolon after the if statement, it looked like a
256 bug, so i removed it */
257 if (wait_done)
258 break;
259 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000260
Jonathan Peyton30419822017-05-12 18:01:32 +0000261 __kmp_win32_mutex_lock(mx);
262 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000263
Jonathan Peyton30419822017-05-12 18:01:32 +0000264 cv->waiters_count_--;
265 cv->release_count_--;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000266
Jonathan Peyton30419822017-05-12 18:01:32 +0000267 last_waiter = (cv->release_count_ == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000268
Jonathan Peyton30419822017-05-12 18:01:32 +0000269 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000270
Jonathan Peyton30419822017-05-12 18:01:32 +0000271 if (last_waiter) {
272 /* We're the last waiter to be notified, so reset the manual event. */
273 ResetEvent(cv->event_);
274 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000275}
276
Jonathan Peyton30419822017-05-12 18:01:32 +0000277void __kmp_win32_cond_broadcast(kmp_win32_cond_t *cv) {
278 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000279
Jonathan Peyton30419822017-05-12 18:01:32 +0000280 if (cv->waiters_count_ > 0) {
281 SetEvent(cv->event_);
282 /* Release all the threads in this generation. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000283
Jonathan Peyton30419822017-05-12 18:01:32 +0000284 cv->release_count_ = cv->waiters_count_;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000285
Jonathan Peyton30419822017-05-12 18:01:32 +0000286 /* Start a new generation. */
287 cv->wait_generation_count_++;
288 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000289
Jonathan Peyton30419822017-05-12 18:01:32 +0000290 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000291}
292
Jonathan Peyton30419822017-05-12 18:01:32 +0000293void __kmp_win32_cond_signal(kmp_win32_cond_t *cv) {
294 __kmp_win32_cond_broadcast(cv);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000295}
296
Jonathan Peyton30419822017-05-12 18:01:32 +0000297void __kmp_enable(int new_state) {
298 if (__kmp_init_runtime)
299 LeaveCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000300}
301
Jonathan Peyton30419822017-05-12 18:01:32 +0000302void __kmp_disable(int *old_state) {
303 *old_state = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000304
Jonathan Peyton30419822017-05-12 18:01:32 +0000305 if (__kmp_init_runtime)
306 EnterCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000307}
308
Jonathan Peyton30419822017-05-12 18:01:32 +0000309void __kmp_suspend_initialize(void) { /* do nothing */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000310}
311
Jonathan Peytonfeac33e2019-04-08 17:50:02 +0000312void __kmp_suspend_initialize_thread(kmp_info_t *th) {
Andrey Churbanovf8f788b2019-05-16 17:52:53 +0000313 int old_value = KMP_ATOMIC_LD_RLX(&th->th.th_suspend_init);
314 int new_value = TRUE;
315 // Return if already initialized
316 if (old_value == new_value)
317 return;
318 // Wait, then return if being initialized
319 if (old_value == -1 ||
320 !__kmp_atomic_compare_store(&th->th.th_suspend_init, old_value, -1)) {
321 while (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init) != new_value) {
322 KMP_CPU_PAUSE();
323 }
324 } else {
325 // Claim to be the initializer and do initializations
Jonathan Peyton30419822017-05-12 18:01:32 +0000326 __kmp_win32_cond_init(&th->th.th_suspend_cv);
327 __kmp_win32_mutex_init(&th->th.th_suspend_mx);
Andrey Churbanovf8f788b2019-05-16 17:52:53 +0000328 KMP_ATOMIC_ST_REL(&th->th.th_suspend_init, new_value);
Jonathan Peyton30419822017-05-12 18:01:32 +0000329 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000330}
331
Jonathan Peyton30419822017-05-12 18:01:32 +0000332void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
Andrey Churbanovf8f788b2019-05-16 17:52:53 +0000333 if (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init)) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000334 /* this means we have initialize the suspension pthread objects for this
335 thread in this instance of the process */
336 __kmp_win32_cond_destroy(&th->th.th_suspend_cv);
337 __kmp_win32_mutex_destroy(&th->th.th_suspend_mx);
Andrey Churbanovf8f788b2019-05-16 17:52:53 +0000338 KMP_ATOMIC_ST_REL(&th->th.th_suspend_init, FALSE);
Jonathan Peyton30419822017-05-12 18:01:32 +0000339 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000340}
341
Jonathan Peyton9b8bb322019-01-16 20:07:39 +0000342int __kmp_try_suspend_mx(kmp_info_t *th) {
343 return __kmp_win32_mutex_trylock(&th->th.th_suspend_mx);
344}
345
346void __kmp_lock_suspend_mx(kmp_info_t *th) {
347 __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
348}
349
350void __kmp_unlock_suspend_mx(kmp_info_t *th) {
351 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
352}
353
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000354/* This routine puts the calling thread to sleep after setting the
Jonathan Peyton30419822017-05-12 18:01:32 +0000355 sleep bit for the indicated flag variable to true. */
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000356template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +0000357static inline void __kmp_suspend_template(int th_gtid, C *flag) {
358 kmp_info_t *th = __kmp_threads[th_gtid];
359 int status;
360 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000361
Jonathan Peyton30419822017-05-12 18:01:32 +0000362 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag's loc(%p)\n",
363 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000364
Jonathan Peyton30419822017-05-12 18:01:32 +0000365 __kmp_suspend_initialize_thread(th);
366 __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000367
Jonathan Peyton30419822017-05-12 18:01:32 +0000368 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for flag's"
369 " loc(%p)\n",
370 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000371
Jonathan Peyton30419822017-05-12 18:01:32 +0000372 /* TODO: shouldn't this use release semantics to ensure that
373 __kmp_suspend_initialize_thread gets called first? */
374 old_spin = flag->set_sleeping();
Jonathan Peyton9b8bb322019-01-16 20:07:39 +0000375#if OMP_50_ENABLED
376 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME &&
377 __kmp_pause_status != kmp_soft_paused) {
378 flag->unset_sleeping();
379 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
380 return;
381 }
382#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000383
Jonathan Peyton30419822017-05-12 18:01:32 +0000384 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for flag's"
385 " loc(%p)==%d\n",
386 th_gtid, flag->get(), *(flag->get())));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000387
Jonathan Peyton30419822017-05-12 18:01:32 +0000388 if (flag->done_check_val(old_spin)) {
389 old_spin = flag->unset_sleeping();
390 KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
391 "for flag's loc(%p)\n",
392 th_gtid, flag->get()));
393 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000394#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +0000395 __kmp_suspend_count++;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000396#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000397 /* Encapsulate in a loop as the documentation states that this may "with
398 low probability" return when the condition variable has not been signaled
399 or broadcast */
400 int deactivated = FALSE;
401 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
402 while (flag->is_sleeping()) {
403 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
404 "kmp_win32_cond_wait()\n",
405 th_gtid));
406 // Mark the thread as no longer active (only in the first iteration of the
407 // loop).
408 if (!deactivated) {
409 th->th.th_active = FALSE;
410 if (th->th.th_active_in_pool) {
411 th->th.th_active_in_pool = FALSE;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +0000412 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +0000413 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
414 }
415 deactivated = TRUE;
Jonathan Peytonb727d382019-04-04 20:35:29 +0000416 __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, th,
417 flag);
Jonathan Peyton30419822017-05-12 18:01:32 +0000418 } else {
Jonathan Peytonb727d382019-04-04 20:35:29 +0000419 __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, th,
420 flag);
Jonathan Peyton30419822017-05-12 18:01:32 +0000421 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000422
423#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000424 if (flag->is_sleeping()) {
425 KF_TRACE(100,
426 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
427 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000428#endif /* KMP_DEBUG */
429
Jonathan Peyton30419822017-05-12 18:01:32 +0000430 } // while
Jim Cownie5e8470a2013-09-27 10:38:44 +0000431
Jonathan Peyton30419822017-05-12 18:01:32 +0000432 // Mark the thread as active again (if it was previous marked as inactive)
433 if (deactivated) {
434 th->th.th_active = TRUE;
435 if (TCR_4(th->th.th_in_pool)) {
Jonathan Peyton37e2ef52018-07-09 17:36:22 +0000436 KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +0000437 th->th.th_active_in_pool = TRUE;
438 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000439 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000440 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000441
Jonathan Peyton30419822017-05-12 18:01:32 +0000442 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000443
Jonathan Peyton30419822017-05-12 18:01:32 +0000444 KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000445}
446
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000447void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000448 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000449}
450void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000451 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000452}
453void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000454 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000455}
456
Jim Cownie5e8470a2013-09-27 10:38:44 +0000457/* This routine signals the thread specified by target_gtid to wake up
Jonathan Peyton30419822017-05-12 18:01:32 +0000458 after setting the sleep bit indicated by the flag argument to FALSE */
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000459template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +0000460static inline void __kmp_resume_template(int target_gtid, C *flag) {
461 kmp_info_t *th = __kmp_threads[target_gtid];
462 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000463
464#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000465 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000466#endif
467
Jonathan Peyton30419822017-05-12 18:01:32 +0000468 KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
469 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000470
Jonathan Peyton30419822017-05-12 18:01:32 +0000471 __kmp_suspend_initialize_thread(th);
472 __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000473
Jonathan Peyton30419822017-05-12 18:01:32 +0000474 if (!flag) { // coming from __kmp_null_resume_wrapper
475 flag = (C *)th->th.th_sleep_loc;
476 }
477
478 // First, check if the flag is null or its type has changed. If so, someone
479 // else woke it up.
480 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type
481 // simply shows what
482 // flag was cast to
483 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
484 "awake: flag's loc(%p)\n",
485 gtid, target_gtid, NULL));
486 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
487 return;
488 } else {
489 typename C::flag_t old_spin = flag->unset_sleeping();
490 if (!flag->is_sleeping_val(old_spin)) {
491 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
492 "awake: flag's loc(%p): %u => %u\n",
493 gtid, target_gtid, flag->get(), old_spin, *(flag->get())));
494 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
495 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000496 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000497 }
498 TCW_PTR(th->th.th_sleep_loc, NULL);
499 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep "
500 "bit for flag's loc(%p)\n",
501 gtid, target_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000502
Jonathan Peyton30419822017-05-12 18:01:32 +0000503 __kmp_win32_cond_signal(&th->th.th_suspend_cv);
504 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000505
Jonathan Peyton30419822017-05-12 18:01:32 +0000506 KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
507 " for T#%d\n",
508 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000509}
510
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000511void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000512 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000513}
514void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000515 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000516}
517void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000518 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000519}
520
Jonathan Peytone47d32f2019-02-28 19:11:29 +0000521void __kmp_yield() { Sleep(0); }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000522
Jonathan Peyton30419822017-05-12 18:01:32 +0000523void __kmp_gtid_set_specific(int gtid) {
524 if (__kmp_init_gtid) {
525 KA_TRACE(50, ("__kmp_gtid_set_specific: T#%d key:%d\n", gtid,
526 __kmp_gtid_threadprivate_key));
527 if (!TlsSetValue(__kmp_gtid_threadprivate_key, (LPVOID)(gtid + 1)))
528 KMP_FATAL(TLSSetValueFailed);
529 } else {
530 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
531 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000532}
533
Jonathan Peyton30419822017-05-12 18:01:32 +0000534int __kmp_gtid_get_specific() {
535 int gtid;
536 if (!__kmp_init_gtid) {
537 KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
538 "KMP_GTID_SHUTDOWN\n"));
539 return KMP_GTID_SHUTDOWN;
540 }
541 gtid = (int)(kmp_intptr_t)TlsGetValue(__kmp_gtid_threadprivate_key);
542 if (gtid == 0) {
543 gtid = KMP_GTID_DNE;
544 } else {
545 gtid--;
546 }
547 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
548 __kmp_gtid_threadprivate_key, gtid));
549 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000550}
551
Jonathan Peyton30419822017-05-12 18:01:32 +0000552void __kmp_affinity_bind_thread(int proc) {
553 if (__kmp_num_proc_groups > 1) {
554 // Form the GROUP_AFFINITY struct directly, rather than filling
555 // out a bit vector and calling __kmp_set_system_affinity().
556 GROUP_AFFINITY ga;
557 KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups * CHAR_BIT *
558 sizeof(DWORD_PTR))));
559 ga.Group = proc / (CHAR_BIT * sizeof(DWORD_PTR));
560 ga.Mask = (unsigned long long)1 << (proc % (CHAR_BIT * sizeof(DWORD_PTR)));
561 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000562
Jonathan Peyton30419822017-05-12 18:01:32 +0000563 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
564 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
565 DWORD error = GetLastError();
566 if (__kmp_affinity_verbose) { // AC: continue silently if not verbose
567 kmp_msg_t err_code = KMP_ERR(error);
568 __kmp_msg(kmp_ms_warning, KMP_MSG(CantSetThreadAffMask), err_code,
569 __kmp_msg_null);
570 if (__kmp_generate_warnings == kmp_warnings_off) {
571 __kmp_str_free(&err_code.str);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000572 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000573 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000574 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000575 } else {
576 kmp_affin_mask_t *mask;
577 KMP_CPU_ALLOC_ON_STACK(mask);
578 KMP_CPU_ZERO(mask);
579 KMP_CPU_SET(proc, mask);
580 __kmp_set_system_affinity(mask, TRUE);
581 KMP_CPU_FREE_FROM_STACK(mask);
582 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000583}
584
Jonathan Peyton30419822017-05-12 18:01:32 +0000585void __kmp_affinity_determine_capable(const char *env_var) {
586// All versions of Windows* OS (since Win '95) support SetThreadAffinityMask().
Jim Cownie5e8470a2013-09-27 10:38:44 +0000587
Andrey Churbanov7daf9802015-01-27 16:52:57 +0000588#if KMP_GROUP_AFFINITY
Jonathan Peyton30419822017-05-12 18:01:32 +0000589 KMP_AFFINITY_ENABLE(__kmp_num_proc_groups * sizeof(DWORD_PTR));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000590#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000591 KMP_AFFINITY_ENABLE(sizeof(DWORD_PTR));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000592#endif
593
Jonathan Peyton30419822017-05-12 18:01:32 +0000594 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
595 "Windows* OS affinity interface functional (mask size = "
596 "%" KMP_SIZE_T_SPEC ").\n",
597 __kmp_affin_mask_size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000598}
599
Jonathan Peyton30419822017-05-12 18:01:32 +0000600double __kmp_read_cpu_time(void) {
601 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
602 int status;
603 double cpu_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000604
Jonathan Peyton30419822017-05-12 18:01:32 +0000605 cpu_time = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000606
Jonathan Peyton30419822017-05-12 18:01:32 +0000607 status = GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime,
608 &KernelTime, &UserTime);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000609
Jonathan Peyton30419822017-05-12 18:01:32 +0000610 if (status) {
611 double sec = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000612
Jonathan Peyton30419822017-05-12 18:01:32 +0000613 sec += KernelTime.dwHighDateTime;
614 sec += UserTime.dwHighDateTime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000615
Jonathan Peyton30419822017-05-12 18:01:32 +0000616 /* Shift left by 32 bits */
617 sec *= (double)(1 << 16) * (double)(1 << 16);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000618
Jonathan Peyton30419822017-05-12 18:01:32 +0000619 sec += KernelTime.dwLowDateTime;
620 sec += UserTime.dwLowDateTime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000621
Jonathan Peyton30419822017-05-12 18:01:32 +0000622 cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC;
623 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000624
Jonathan Peyton30419822017-05-12 18:01:32 +0000625 return cpu_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000626}
627
Jonathan Peyton30419822017-05-12 18:01:32 +0000628int __kmp_read_system_info(struct kmp_sys_info *info) {
629 info->maxrss = 0; /* the maximum resident set size utilized (in kilobytes) */
630 info->minflt = 0; /* the number of page faults serviced without any I/O */
631 info->majflt = 0; /* the number of page faults serviced that required I/O */
632 info->nswap = 0; // the number of times a process was "swapped" out of memory
633 info->inblock = 0; // the number of times the file system had to perform input
634 info->oublock = 0; // number of times the file system had to perform output
635 info->nvcsw = 0; /* the number of times a context switch was voluntarily */
636 info->nivcsw = 0; /* the number of times a context switch was forced */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000637
Jonathan Peyton30419822017-05-12 18:01:32 +0000638 return 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000639}
640
Jonathan Peyton30419822017-05-12 18:01:32 +0000641void __kmp_runtime_initialize(void) {
642 SYSTEM_INFO info;
643 kmp_str_buf_t path;
644 UINT path_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000645
Jonathan Peyton30419822017-05-12 18:01:32 +0000646 if (__kmp_init_runtime) {
647 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000648 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000649
Jonathan Peyton99016992015-05-26 17:32:53 +0000650#if KMP_DYNAMIC_LIB
Jonathan Peyton30419822017-05-12 18:01:32 +0000651 /* Pin dynamic library for the lifetime of application */
652 {
653 // First, turn off error message boxes
654 UINT err_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
655 HMODULE h;
656 BOOL ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
657 GET_MODULE_HANDLE_EX_FLAG_PIN,
658 (LPCTSTR)&__kmp_serial_initialize, &h);
659 KMP_DEBUG_ASSERT2(h && ret, "OpenMP RTL cannot find itself loaded");
660 SetErrorMode(err_mode); // Restore error mode
661 KA_TRACE(10, ("__kmp_runtime_initialize: dynamic library pinned\n"));
662 }
Andrey Churbanov9bf53282015-01-29 17:18:20 +0000663#endif
664
Jonathan Peyton30419822017-05-12 18:01:32 +0000665 InitializeCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000666#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000667 __kmp_itt_system_object_created(&__kmp_win32_section, "Critical Section");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000668#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000669 __kmp_initialize_system_tick();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000670
Jonathan Peyton30419822017-05-12 18:01:32 +0000671#if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
672 if (!__kmp_cpuinfo.initialized) {
673 __kmp_query_cpuid(&__kmp_cpuinfo);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000674 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000675#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000676
Jonathan Peyton30419822017-05-12 18:01:32 +0000677/* Set up minimum number of threads to switch to TLS gtid */
Jonathan Peyton8b3842f2018-10-05 17:59:39 +0000678#if KMP_OS_WINDOWS && !KMP_DYNAMIC_LIB
Jonathan Peyton30419822017-05-12 18:01:32 +0000679 // Windows* OS, static library.
680 /* New thread may use stack space previously used by another thread,
681 currently terminated. On Windows* OS, in case of static linking, we do not
682 know the moment of thread termination, and our structures (__kmp_threads
683 and __kmp_root arrays) are still keep info about dead threads. This leads
684 to problem in __kmp_get_global_thread_id() function: it wrongly finds gtid
685 (by searching through stack addresses of all known threads) for
686 unregistered foreign tread.
Jim Cownie5e8470a2013-09-27 10:38:44 +0000687
Jonathan Peyton30419822017-05-12 18:01:32 +0000688 Setting __kmp_tls_gtid_min to 0 workarounds this problem:
689 __kmp_get_global_thread_id() does not search through stacks, but get gtid
690 from TLS immediately.
691 --ln
692 */
693 __kmp_tls_gtid_min = 0;
694#else
695 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
696#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000697
Jonathan Peyton30419822017-05-12 18:01:32 +0000698 /* for the static library */
699 if (!__kmp_gtid_threadprivate_key) {
700 __kmp_gtid_threadprivate_key = TlsAlloc();
701 if (__kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES) {
702 KMP_FATAL(TLSOutOfIndexes);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000703 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000704 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000705
Jonathan Peyton30419822017-05-12 18:01:32 +0000706 // Load ntdll.dll.
707 /* Simple GetModuleHandle( "ntdll.dl" ) is not suitable due to security issue
708 (see http://www.microsoft.com/technet/security/advisory/2269637.mspx). We
709 have to specify full path to the library. */
710 __kmp_str_buf_init(&path);
711 path_size = GetSystemDirectory(path.str, path.size);
712 KMP_DEBUG_ASSERT(path_size > 0);
713 if (path_size >= path.size) {
714 // Buffer is too short. Expand the buffer and try again.
715 __kmp_str_buf_reserve(&path, path_size);
716 path_size = GetSystemDirectory(path.str, path.size);
717 KMP_DEBUG_ASSERT(path_size > 0);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000718 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000719 if (path_size > 0 && path_size < path.size) {
720 // Now we have system directory name in the buffer.
721 // Append backslash and name of dll to form full path,
722 path.used = path_size;
723 __kmp_str_buf_print(&path, "\\%s", "ntdll.dll");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000724
Jonathan Peyton30419822017-05-12 18:01:32 +0000725 // Now load ntdll using full path.
726 ntdll = GetModuleHandle(path.str);
727 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000728
Jonathan Peyton30419822017-05-12 18:01:32 +0000729 KMP_DEBUG_ASSERT(ntdll != NULL);
730 if (ntdll != NULL) {
731 NtQuerySystemInformation = (NtQuerySystemInformation_t)GetProcAddress(
732 ntdll, "NtQuerySystemInformation");
733 }
734 KMP_DEBUG_ASSERT(NtQuerySystemInformation != NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000735
Andrey Churbanov7daf9802015-01-27 16:52:57 +0000736#if KMP_GROUP_AFFINITY
Jonathan Peyton30419822017-05-12 18:01:32 +0000737 // Load kernel32.dll.
738 // Same caveat - must use full system path name.
739 if (path_size > 0 && path_size < path.size) {
740 // Truncate the buffer back to just the system path length,
741 // discarding "\\ntdll.dll", and replacing it with "kernel32.dll".
742 path.used = path_size;
743 __kmp_str_buf_print(&path, "\\%s", "kernel32.dll");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000744
Jonathan Peyton30419822017-05-12 18:01:32 +0000745 // Load kernel32.dll using full path.
746 kernel32 = GetModuleHandle(path.str);
747 KA_TRACE(10, ("__kmp_runtime_initialize: kernel32.dll = %s\n", path.str));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000748
Jonathan Peyton30419822017-05-12 18:01:32 +0000749 // Load the function pointers to kernel32.dll routines
750 // that may or may not exist on this system.
751 if (kernel32 != NULL) {
752 __kmp_GetActiveProcessorCount =
753 (kmp_GetActiveProcessorCount_t)GetProcAddress(
754 kernel32, "GetActiveProcessorCount");
755 __kmp_GetActiveProcessorGroupCount =
756 (kmp_GetActiveProcessorGroupCount_t)GetProcAddress(
757 kernel32, "GetActiveProcessorGroupCount");
758 __kmp_GetThreadGroupAffinity =
759 (kmp_GetThreadGroupAffinity_t)GetProcAddress(
760 kernel32, "GetThreadGroupAffinity");
761 __kmp_SetThreadGroupAffinity =
762 (kmp_SetThreadGroupAffinity_t)GetProcAddress(
763 kernel32, "SetThreadGroupAffinity");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000764
Jonathan Peyton30419822017-05-12 18:01:32 +0000765 KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_GetActiveProcessorCount"
766 " = %p\n",
767 __kmp_GetActiveProcessorCount));
768 KA_TRACE(10, ("__kmp_runtime_initialize: "
769 "__kmp_GetActiveProcessorGroupCount = %p\n",
770 __kmp_GetActiveProcessorGroupCount));
771 KA_TRACE(10, ("__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity"
772 " = %p\n",
773 __kmp_GetThreadGroupAffinity));
774 KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity"
775 " = %p\n",
776 __kmp_SetThreadGroupAffinity));
777 KA_TRACE(10, ("__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
778 sizeof(kmp_affin_mask_t)));
Andrey Churbanovdf6555b2015-01-27 17:00:03 +0000779
Jonathan Peyton30419822017-05-12 18:01:32 +0000780 // See if group affinity is supported on this system.
781 // If so, calculate the #groups and #procs.
782 //
783 // Group affinity was introduced with Windows* 7 OS and
784 // Windows* Server 2008 R2 OS.
785 if ((__kmp_GetActiveProcessorCount != NULL) &&
786 (__kmp_GetActiveProcessorGroupCount != NULL) &&
787 (__kmp_GetThreadGroupAffinity != NULL) &&
788 (__kmp_SetThreadGroupAffinity != NULL) &&
789 ((__kmp_num_proc_groups = __kmp_GetActiveProcessorGroupCount()) >
790 1)) {
791 // Calculate the total number of active OS procs.
792 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000793
Jonathan Peyton30419822017-05-12 18:01:32 +0000794 KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
795 " detected\n",
796 __kmp_num_proc_groups));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000797
Jonathan Peyton30419822017-05-12 18:01:32 +0000798 __kmp_xproc = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000799
Jonathan Peyton30419822017-05-12 18:01:32 +0000800 for (i = 0; i < __kmp_num_proc_groups; i++) {
801 DWORD size = __kmp_GetActiveProcessorCount(i);
802 __kmp_xproc += size;
803 KA_TRACE(10, ("__kmp_runtime_initialize: proc group %d size = %d\n",
804 i, size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000805 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000806 } else {
807 KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
808 " detected\n",
809 __kmp_num_proc_groups));
810 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000811 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000812 }
813 if (__kmp_num_proc_groups <= 1) {
814 GetSystemInfo(&info);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000815 __kmp_xproc = info.dwNumberOfProcessors;
Jonathan Peyton30419822017-05-12 18:01:32 +0000816 }
817#else
818 GetSystemInfo(&info);
819 __kmp_xproc = info.dwNumberOfProcessors;
Andrey Churbanov7daf9802015-01-27 16:52:57 +0000820#endif /* KMP_GROUP_AFFINITY */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000821
Jonathan Peyton30419822017-05-12 18:01:32 +0000822 // If the OS said there were 0 procs, take a guess and use a value of 2.
823 // This is done for Linux* OS, also. Do we need error / warning?
824 if (__kmp_xproc <= 0) {
825 __kmp_xproc = 2;
826 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000827
Jonathan Peyton30419822017-05-12 18:01:32 +0000828 KA_TRACE(5,
829 ("__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000830
Jonathan Peyton30419822017-05-12 18:01:32 +0000831 __kmp_str_buf_free(&path);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000832
833#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000834 __kmp_itt_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000835#endif /* USE_ITT_BUILD */
836
Jonathan Peyton30419822017-05-12 18:01:32 +0000837 __kmp_init_runtime = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000838} // __kmp_runtime_initialize
839
Jonathan Peyton30419822017-05-12 18:01:32 +0000840void __kmp_runtime_destroy(void) {
841 if (!__kmp_init_runtime) {
842 return;
843 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000844
845#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000846 __kmp_itt_destroy();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000847#endif /* USE_ITT_BUILD */
848
Jonathan Peyton30419822017-05-12 18:01:32 +0000849 /* we can't DeleteCriticalsection( & __kmp_win32_section ); */
850 /* due to the KX_TRACE() commands */
851 KA_TRACE(40, ("__kmp_runtime_destroy\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000852
Jonathan Peyton30419822017-05-12 18:01:32 +0000853 if (__kmp_gtid_threadprivate_key) {
854 TlsFree(__kmp_gtid_threadprivate_key);
855 __kmp_gtid_threadprivate_key = 0;
856 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000857
Jonathan Peyton30419822017-05-12 18:01:32 +0000858 __kmp_affinity_uninitialize();
859 DeleteCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000860
Jonathan Peyton30419822017-05-12 18:01:32 +0000861 ntdll = NULL;
862 NtQuerySystemInformation = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000863
864#if KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000865 kernel32 = NULL;
866 __kmp_GetActiveProcessorCount = NULL;
867 __kmp_GetActiveProcessorGroupCount = NULL;
868 __kmp_GetThreadGroupAffinity = NULL;
869 __kmp_SetThreadGroupAffinity = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000870#endif // KMP_ARCH_X86_64
871
Jonathan Peyton30419822017-05-12 18:01:32 +0000872 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000873}
874
Jonathan Peyton30419822017-05-12 18:01:32 +0000875void __kmp_terminate_thread(int gtid) {
876 kmp_info_t *th = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000877
Jonathan Peyton30419822017-05-12 18:01:32 +0000878 if (!th)
879 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000880
Jonathan Peyton30419822017-05-12 18:01:32 +0000881 KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000882
Jonathan Peyton30419822017-05-12 18:01:32 +0000883 if (TerminateThread(th->th.th_info.ds.ds_thread, (DWORD)-1) == FALSE) {
884 /* It's OK, the thread may have exited already */
885 }
886 __kmp_free_handle(th->th.th_info.ds.ds_thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000887}
888
Jonathan Peyton30419822017-05-12 18:01:32 +0000889void __kmp_clear_system_time(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000890 BOOL status;
Jonathan Peyton30419822017-05-12 18:01:32 +0000891 LARGE_INTEGER time;
892 status = QueryPerformanceCounter(&time);
893 __kmp_win32_time = (kmp_int64)time.QuadPart;
894}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000895
Jonathan Peyton30419822017-05-12 18:01:32 +0000896void __kmp_initialize_system_tick(void) {
897 {
898 BOOL status;
899 LARGE_INTEGER freq;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000900
Jonathan Peyton30419822017-05-12 18:01:32 +0000901 status = QueryPerformanceFrequency(&freq);
902 if (!status) {
903 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000904 __kmp_fatal(KMP_MSG(FunctionError, "QueryPerformanceFrequency()"),
905 KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +0000906
907 } else {
908 __kmp_win32_tick = ((double)1.0) / (double)freq.QuadPart;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000909 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000910 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000911}
912
913/* Calculate the elapsed wall clock time for the user */
914
Jonathan Peyton30419822017-05-12 18:01:32 +0000915void __kmp_elapsed(double *t) {
916 BOOL status;
917 LARGE_INTEGER now;
918 status = QueryPerformanceCounter(&now);
919 *t = ((double)now.QuadPart) * __kmp_win32_tick;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000920}
921
922/* Calculate the elapsed wall clock tick for the user */
923
Jonathan Peyton30419822017-05-12 18:01:32 +0000924void __kmp_elapsed_tick(double *t) { *t = __kmp_win32_tick; }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000925
Jonathan Peyton30419822017-05-12 18:01:32 +0000926void __kmp_read_system_time(double *delta) {
927 if (delta != NULL) {
928 BOOL status;
929 LARGE_INTEGER now;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000930
Jonathan Peyton30419822017-05-12 18:01:32 +0000931 status = QueryPerformanceCounter(&now);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000932
Jonathan Peyton30419822017-05-12 18:01:32 +0000933 *delta = ((double)(((kmp_int64)now.QuadPart) - __kmp_win32_time)) *
934 __kmp_win32_tick;
935 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000936}
937
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000938/* Return the current time stamp in nsec */
Jonathan Peyton30419822017-05-12 18:01:32 +0000939kmp_uint64 __kmp_now_nsec() {
940 LARGE_INTEGER now;
941 QueryPerformanceCounter(&now);
942 return 1e9 * __kmp_win32_tick * now.QuadPart;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000943}
944
Andrey Churbanovf700e9e2018-12-10 13:45:00 +0000945extern "C"
Jonathan Peyton30419822017-05-12 18:01:32 +0000946void *__stdcall __kmp_launch_worker(void *arg) {
947 volatile void *stack_data;
948 void *exit_val;
949 void *padding = 0;
950 kmp_info_t *this_thr = (kmp_info_t *)arg;
951 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000952
Jonathan Peyton30419822017-05-12 18:01:32 +0000953 gtid = this_thr->th.th_info.ds.ds_gtid;
954 __kmp_gtid_set_specific(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000955#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000956#error "This define causes problems with LoadLibrary() + declspec(thread) " \
Jim Cownie5e8470a2013-09-27 10:38:44 +0000957 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
958 "reference: http://support.microsoft.com/kb/118816"
Jonathan Peyton30419822017-05-12 18:01:32 +0000959//__kmp_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000960#endif
961
962#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000963 __kmp_itt_thread_name(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000964#endif /* USE_ITT_BUILD */
965
Jonathan Peyton30419822017-05-12 18:01:32 +0000966 __kmp_affinity_set_init_mask(gtid, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000967
968#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000969 // Set FP control regs to be a copy of the parallel initialization thread's.
970 __kmp_clear_x87_fpu_status_word();
971 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
972 __kmp_load_mxcsr(&__kmp_init_mxcsr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000973#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
974
Jonathan Peyton30419822017-05-12 18:01:32 +0000975 if (__kmp_stkoffset > 0 && gtid > 0) {
976 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
977 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000978
Jonathan Peyton30419822017-05-12 18:01:32 +0000979 KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
980 this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
981 TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000982
Jonathan Peyton30419822017-05-12 18:01:32 +0000983 if (TCR_4(__kmp_gtid_mode) <
984 2) { // check stack only if it is used to get gtid
985 TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
986 KMP_ASSERT(this_thr->th.th_info.ds.ds_stackgrow == FALSE);
987 __kmp_check_stack_overlap(this_thr);
988 }
989 KMP_MB();
990 exit_val = __kmp_launch_thread(this_thr);
991 KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
992 TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
993 KMP_MB();
994 return exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000995}
996
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000997#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000998/* The monitor thread controls all of the threads in the complex */
999
Jonathan Peyton30419822017-05-12 18:01:32 +00001000void *__stdcall __kmp_launch_monitor(void *arg) {
1001 DWORD wait_status;
1002 kmp_thread_t monitor;
1003 int status;
1004 int interval;
1005 kmp_info_t *this_thr = (kmp_info_t *)arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001006
Jonathan Peyton30419822017-05-12 18:01:32 +00001007 KMP_DEBUG_ASSERT(__kmp_init_monitor);
1008 TCW_4(__kmp_init_monitor, 2); // AC: Signal library that monitor has started
1009 // TODO: hide "2" in enum (like {true,false,started})
1010 this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
1011 TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001012
Jonathan Peyton30419822017-05-12 18:01:32 +00001013 KMP_MB(); /* Flush all pending memory write invalidates. */
1014 KA_TRACE(10, ("__kmp_launch_monitor: launched\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001015
Jonathan Peyton30419822017-05-12 18:01:32 +00001016 monitor = GetCurrentThread();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001017
Jonathan Peyton30419822017-05-12 18:01:32 +00001018 /* set thread priority */
1019 status = SetThreadPriority(monitor, THREAD_PRIORITY_HIGHEST);
1020 if (!status) {
1021 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001022 __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001023 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001024
Jonathan Peyton30419822017-05-12 18:01:32 +00001025 /* register us as monitor */
1026 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001027#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +00001028#error "This define causes problems with LoadLibrary() + declspec(thread) " \
Jim Cownie5e8470a2013-09-27 10:38:44 +00001029 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
1030 "reference: http://support.microsoft.com/kb/118816"
Jonathan Peyton30419822017-05-12 18:01:32 +00001031//__kmp_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001032#endif
1033
1034#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001035 __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore
1036// monitor thread.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001037#endif /* USE_ITT_BUILD */
1038
Jonathan Peyton30419822017-05-12 18:01:32 +00001039 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001040
Jonathan Peyton30419822017-05-12 18:01:32 +00001041 interval = (1000 / __kmp_monitor_wakeups); /* in milliseconds */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001042
Jonathan Peyton30419822017-05-12 18:01:32 +00001043 while (!TCR_4(__kmp_global.g.g_done)) {
1044 /* This thread monitors the state of the system */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001045
Jonathan Peyton30419822017-05-12 18:01:32 +00001046 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001047
Jonathan Peyton30419822017-05-12 18:01:32 +00001048 wait_status = WaitForSingleObject(__kmp_monitor_ev, interval);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001049
Jonathan Peyton30419822017-05-12 18:01:32 +00001050 if (wait_status == WAIT_TIMEOUT) {
1051 TCW_4(__kmp_global.g.g_time.dt.t_value,
1052 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001053 }
1054
Jonathan Peyton30419822017-05-12 18:01:32 +00001055 KMP_MB(); /* Flush all pending memory write invalidates. */
1056 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001057
Jonathan Peyton30419822017-05-12 18:01:32 +00001058 KA_TRACE(10, ("__kmp_launch_monitor: finished\n"));
1059
1060 status = SetThreadPriority(monitor, THREAD_PRIORITY_NORMAL);
1061 if (!status) {
1062 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001063 __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001064 }
1065
1066 if (__kmp_global.g.g_abort != 0) {
1067 /* now we need to terminate the worker threads */
1068 /* the value of t_abort is the signal we caught */
1069 int gtid;
1070
1071 KA_TRACE(10, ("__kmp_launch_monitor: terminate sig=%d\n",
1072 (__kmp_global.g.g_abort)));
1073
1074 /* terminate the OpenMP worker threads */
1075 /* TODO this is not valid for sibling threads!!
1076 * the uber master might not be 0 anymore.. */
1077 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1078 __kmp_terminate_thread(gtid);
1079
1080 __kmp_cleanup();
1081
1082 Sleep(0);
1083
1084 KA_TRACE(10,
1085 ("__kmp_launch_monitor: raise sig=%d\n", __kmp_global.g.g_abort));
1086
1087 if (__kmp_global.g.g_abort > 0) {
1088 raise(__kmp_global.g.g_abort);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001089 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001090 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001091
Jonathan Peyton30419822017-05-12 18:01:32 +00001092 TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001093
Jonathan Peyton30419822017-05-12 18:01:32 +00001094 KMP_MB();
1095 return arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001096}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001097#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001098
Jonathan Peyton30419822017-05-12 18:01:32 +00001099void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
1100 kmp_thread_t handle;
1101 DWORD idThread;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001102
Jonathan Peyton30419822017-05-12 18:01:32 +00001103 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001104
Jonathan Peyton30419822017-05-12 18:01:32 +00001105 th->th.th_info.ds.ds_gtid = gtid;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001106
Jonathan Peyton30419822017-05-12 18:01:32 +00001107 if (KMP_UBER_GTID(gtid)) {
1108 int stack_data;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001109
Jonathan Peyton30419822017-05-12 18:01:32 +00001110 /* TODO: GetCurrentThread() returns a pseudo-handle that is unsuitable for
1111 other threads to use. Is it appropriate to just use GetCurrentThread?
1112 When should we close this handle? When unregistering the root? */
1113 {
1114 BOOL rc;
1115 rc = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
1116 GetCurrentProcess(), &th->th.th_info.ds.ds_thread, 0,
1117 FALSE, DUPLICATE_SAME_ACCESS);
1118 KMP_ASSERT(rc);
1119 KA_TRACE(10, (" __kmp_create_worker: ROOT Handle duplicated, th = %p, "
1120 "handle = %" KMP_UINTPTR_SPEC "\n",
1121 (LPVOID)th, th->th.th_info.ds.ds_thread));
1122 th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001123 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001124 if (TCR_4(__kmp_gtid_mode) < 2) { // check stack only if used to get gtid
1125 /* we will dynamically update the stack range if gtid_mode == 1 */
1126 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1127 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1128 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1129 __kmp_check_stack_overlap(th);
1130 }
1131 } else {
1132 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001133
Jonathan Peyton30419822017-05-12 18:01:32 +00001134 /* Set stack size for this thread now. */
1135 KA_TRACE(10,
1136 ("__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC " bytes\n",
1137 stack_size));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001138
Jonathan Peyton30419822017-05-12 18:01:32 +00001139 stack_size += gtid * __kmp_stkoffset;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001140
Jonathan Peyton30419822017-05-12 18:01:32 +00001141 TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1142 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001143
Jonathan Peyton30419822017-05-12 18:01:32 +00001144 KA_TRACE(10,
1145 ("__kmp_create_worker: (before) stack_size = %" KMP_SIZE_T_SPEC
1146 " bytes, &__kmp_launch_worker = %p, th = %p, &idThread = %p\n",
1147 (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1148 (LPVOID)th, &idThread));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001149
Jonathan Peyton30419822017-05-12 18:01:32 +00001150 handle = CreateThread(
1151 NULL, (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)__kmp_launch_worker,
1152 (LPVOID)th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001153
Jonathan Peyton30419822017-05-12 18:01:32 +00001154 KA_TRACE(10,
1155 ("__kmp_create_worker: (after) stack_size = %" KMP_SIZE_T_SPEC
1156 " bytes, &__kmp_launch_worker = %p, th = %p, "
1157 "idThread = %u, handle = %" KMP_UINTPTR_SPEC "\n",
1158 (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1159 (LPVOID)th, idThread, handle));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001160
Jonathan Peyton30419822017-05-12 18:01:32 +00001161 if (handle == 0) {
1162 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001163 __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001164 } else {
1165 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001166 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001167
Jonathan Peyton30419822017-05-12 18:01:32 +00001168 KMP_MB(); /* Flush all pending memory write invalidates. */
1169 }
1170
1171 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001172}
1173
Jonathan Peyton30419822017-05-12 18:01:32 +00001174int __kmp_still_running(kmp_info_t *th) {
1175 return (WAIT_TIMEOUT == WaitForSingleObject(th->th.th_info.ds.ds_thread, 0));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001176}
1177
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001178#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001179void __kmp_create_monitor(kmp_info_t *th) {
1180 kmp_thread_t handle;
1181 DWORD idThread;
1182 int ideal, new_ideal;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001183
Jonathan Peyton30419822017-05-12 18:01:32 +00001184 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
1185 // We don't need monitor thread in case of MAX_BLOCKTIME
1186 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
1187 "MAX blocktime\n"));
1188 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
1189 th->th.th_info.ds.ds_gtid = 0;
1190 TCW_4(__kmp_init_monitor, 2); // Signal to stop waiting for monitor creation
1191 return;
1192 }
1193 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001194
Jonathan Peyton30419822017-05-12 18:01:32 +00001195 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001196
Jonathan Peyton30419822017-05-12 18:01:32 +00001197 __kmp_monitor_ev = CreateEvent(NULL, TRUE, FALSE, NULL);
1198 if (__kmp_monitor_ev == NULL) {
1199 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001200 __kmp_fatal(KMP_MSG(CantCreateEvent), KMP_ERR(error), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001201 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001202#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001203 __kmp_itt_system_object_created(__kmp_monitor_ev, "Event");
Jim Cownie5e8470a2013-09-27 10:38:44 +00001204#endif /* USE_ITT_BUILD */
1205
Jonathan Peyton30419822017-05-12 18:01:32 +00001206 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1207 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001208
Jonathan Peyton30419822017-05-12 18:01:32 +00001209 // FIXME - on Windows* OS, if __kmp_monitor_stksize = 0, figure out how
1210 // to automatically expand stacksize based on CreateThread error code.
1211 if (__kmp_monitor_stksize == 0) {
1212 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1213 }
1214 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
1215 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1216 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001217
Jonathan Peyton30419822017-05-12 18:01:32 +00001218 KA_TRACE(10, ("__kmp_create_monitor: requested stacksize = %d bytes\n",
1219 (int)__kmp_monitor_stksize));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001220
Jonathan Peyton30419822017-05-12 18:01:32 +00001221 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001222
Jonathan Peyton30419822017-05-12 18:01:32 +00001223 handle =
1224 CreateThread(NULL, (SIZE_T)__kmp_monitor_stksize,
1225 (LPTHREAD_START_ROUTINE)__kmp_launch_monitor, (LPVOID)th,
1226 STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
1227 if (handle == 0) {
1228 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001229 __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001230 } else
1231 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001232
Jonathan Peyton30419822017-05-12 18:01:32 +00001233 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001234
Jonathan Peyton30419822017-05-12 18:01:32 +00001235 KA_TRACE(10, ("__kmp_create_monitor: monitor created %p\n",
1236 (void *)th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001237}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001238#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001239
Jonathan Peyton30419822017-05-12 18:01:32 +00001240/* Check to see if thread is still alive.
1241 NOTE: The ExitProcess(code) system call causes all threads to Terminate
1242 with a exit_val = code. Because of this we can not rely on exit_val having
1243 any particular value. So this routine may return STILL_ALIVE in exit_val
1244 even after the thread is dead. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001245
Jonathan Peyton30419822017-05-12 18:01:32 +00001246int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val) {
1247 DWORD rc;
1248 rc = GetExitCodeThread(th->th.th_info.ds.ds_thread, exit_val);
1249 if (rc == 0) {
1250 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001251 __kmp_fatal(KMP_MSG(FunctionError, "GetExitCodeThread()"), KMP_ERR(error),
1252 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001253 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001254 return (*exit_val == STILL_ACTIVE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001255}
1256
Jonathan Peyton30419822017-05-12 18:01:32 +00001257void __kmp_exit_thread(int exit_status) {
1258 ExitThread(exit_status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001259} // __kmp_exit_thread
1260
Jonathan Peyton30419822017-05-12 18:01:32 +00001261// This is a common part for both __kmp_reap_worker() and __kmp_reap_monitor().
1262static void __kmp_reap_common(kmp_info_t *th) {
1263 DWORD exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001264
Jonathan Peyton30419822017-05-12 18:01:32 +00001265 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001266
Jonathan Peyton30419822017-05-12 18:01:32 +00001267 KA_TRACE(
1268 10, ("__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001269
Jonathan Peyton30419822017-05-12 18:01:32 +00001270 /* 2006-10-19:
1271 There are two opposite situations:
1272 1. Windows* OS keep thread alive after it resets ds_alive flag and
1273 exits from thread function. (For example, see C70770/Q394281 "unloading of
1274 dll based on OMP is very slow".)
1275 2. Windows* OS may kill thread before it resets ds_alive flag.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001276
Jonathan Peyton30419822017-05-12 18:01:32 +00001277 Right solution seems to be waiting for *either* thread termination *or*
1278 ds_alive resetting. */
1279 {
Jonathan Peytone47d32f2019-02-28 19:11:29 +00001280 // TODO: This code is very similar to KMP_WAIT. Need to generalize
1281 // KMP_WAIT to cover this usage also.
Jonathan Peyton30419822017-05-12 18:01:32 +00001282 void *obj = NULL;
Ed Maste414544c2017-07-07 21:06:05 +00001283 kmp_uint32 spins;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001284#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001285 KMP_FSYNC_SPIN_INIT(obj, (void *)&th->th.th_info.ds.ds_alive);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001286#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00001287 KMP_INIT_YIELD(spins);
1288 do {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001289#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001290 KMP_FSYNC_SPIN_PREPARE(obj);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001291#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00001292 __kmp_is_thread_alive(th, &exit_val);
Jonathan Peytone47d32f2019-02-28 19:11:29 +00001293 KMP_YIELD_OVERSUB_ELSE_SPIN(spins);
Jonathan Peyton30419822017-05-12 18:01:32 +00001294 } while (exit_val == STILL_ACTIVE && TCR_4(th->th.th_info.ds.ds_alive));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001295#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001296 if (exit_val == STILL_ACTIVE) {
1297 KMP_FSYNC_CANCEL(obj);
1298 } else {
1299 KMP_FSYNC_SPIN_ACQUIRED(obj);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001300 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001301#endif /* USE_ITT_BUILD */
1302 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001303
Jonathan Peyton30419822017-05-12 18:01:32 +00001304 __kmp_free_handle(th->th.th_info.ds.ds_thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001305
Jonathan Peyton30419822017-05-12 18:01:32 +00001306 /* NOTE: The ExitProcess(code) system call causes all threads to Terminate
1307 with a exit_val = code. Because of this we can not rely on exit_val having
1308 any particular value. */
1309 if (exit_val == STILL_ACTIVE) {
1310 KA_TRACE(1, ("__kmp_reap_common: thread still active.\n"));
1311 } else if ((void *)exit_val != (void *)th) {
1312 KA_TRACE(1, ("__kmp_reap_common: ExitProcess / TerminateThread used?\n"));
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001313 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001314
Jonathan Peyton30419822017-05-12 18:01:32 +00001315 KA_TRACE(10,
1316 ("__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
1317 "\n",
1318 th->th.th_info.ds.ds_gtid, th->th.th_info.ds.ds_thread));
1319
1320 th->th.th_info.ds.ds_thread = 0;
1321 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1322 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1323 th->th.th_info.ds.ds_thread_id = 0;
1324
1325 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001326}
1327
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001328#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001329void __kmp_reap_monitor(kmp_info_t *th) {
1330 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001331
Jonathan Peyton30419822017-05-12 18:01:32 +00001332 KA_TRACE(10, ("__kmp_reap_monitor: try to reap %p\n",
1333 (void *)th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001334
Jonathan Peyton30419822017-05-12 18:01:32 +00001335 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1336 // If both tid and gtid are 0, it means the monitor did not ever start.
1337 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1338 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1339 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1340 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1341 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001342 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001343
Jonathan Peyton30419822017-05-12 18:01:32 +00001344 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001345
Jonathan Peyton30419822017-05-12 18:01:32 +00001346 status = SetEvent(__kmp_monitor_ev);
1347 if (status == FALSE) {
1348 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001349 __kmp_fatal(KMP_MSG(CantSetEvent), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001350 }
1351 KA_TRACE(10, ("__kmp_reap_monitor: reaping thread (%d)\n",
1352 th->th.th_info.ds.ds_gtid));
1353 __kmp_reap_common(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001354
Jonathan Peyton30419822017-05-12 18:01:32 +00001355 __kmp_free_handle(__kmp_monitor_ev);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001356
Jonathan Peyton30419822017-05-12 18:01:32 +00001357 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001358}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001359#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001360
Jonathan Peyton30419822017-05-12 18:01:32 +00001361void __kmp_reap_worker(kmp_info_t *th) {
1362 KA_TRACE(10, ("__kmp_reap_worker: reaping thread (%d)\n",
1363 th->th.th_info.ds.ds_gtid));
1364 __kmp_reap_common(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001365}
1366
Jim Cownie5e8470a2013-09-27 10:38:44 +00001367#if KMP_HANDLE_SIGNALS
1368
Jonathan Peyton30419822017-05-12 18:01:32 +00001369static void __kmp_team_handler(int signo) {
1370 if (__kmp_global.g.g_abort == 0) {
1371 // Stage 1 signal handler, let's shut down all of the threads.
1372 if (__kmp_debug_buf) {
1373 __kmp_dump_debug_buffer();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001374 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001375 KMP_MB(); // Flush all pending memory write invalidates.
1376 TCW_4(__kmp_global.g.g_abort, signo);
1377 KMP_MB(); // Flush all pending memory write invalidates.
1378 TCW_4(__kmp_global.g.g_done, TRUE);
1379 KMP_MB(); // Flush all pending memory write invalidates.
1380 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001381} // __kmp_team_handler
1382
Jonathan Peyton30419822017-05-12 18:01:32 +00001383static sig_func_t __kmp_signal(int signum, sig_func_t handler) {
1384 sig_func_t old = signal(signum, handler);
1385 if (old == SIG_ERR) {
1386 int error = errno;
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001387 __kmp_fatal(KMP_MSG(FunctionError, "signal"), KMP_ERR(error),
1388 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001389 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001390 return old;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001391}
1392
Jonathan Peyton30419822017-05-12 18:01:32 +00001393static void __kmp_install_one_handler(int sig, sig_func_t handler,
1394 int parallel_init) {
1395 sig_func_t old;
1396 KMP_MB(); /* Flush all pending memory write invalidates. */
1397 KB_TRACE(60, ("__kmp_install_one_handler: called: sig=%d\n", sig));
1398 if (parallel_init) {
1399 old = __kmp_signal(sig, handler);
1400 // SIG_DFL on Windows* OS in NULL or 0.
1401 if (old == __kmp_sighldrs[sig]) {
1402 __kmp_siginstalled[sig] = 1;
1403 } else { // Restore/keep user's handler if one previously installed.
1404 old = __kmp_signal(sig, old);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001405 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001406 } else {
1407 // Save initial/system signal handlers to see if user handlers installed.
1408 // 2009-09-23: It is a dead code. On Windows* OS __kmp_install_signals
1409 // called once with parallel_init == TRUE.
1410 old = __kmp_signal(sig, SIG_DFL);
1411 __kmp_sighldrs[sig] = old;
1412 __kmp_signal(sig, old);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001413 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001414 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001415} // __kmp_install_one_handler
1416
Jonathan Peyton30419822017-05-12 18:01:32 +00001417static void __kmp_remove_one_handler(int sig) {
1418 if (__kmp_siginstalled[sig]) {
1419 sig_func_t old;
1420 KMP_MB(); // Flush all pending memory write invalidates.
1421 KB_TRACE(60, ("__kmp_remove_one_handler: called: sig=%d\n", sig));
1422 old = __kmp_signal(sig, __kmp_sighldrs[sig]);
1423 if (old != __kmp_team_handler) {
1424 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1425 "restoring: sig=%d\n",
1426 sig));
1427 old = __kmp_signal(sig, old);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001428 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001429 __kmp_sighldrs[sig] = NULL;
1430 __kmp_siginstalled[sig] = 0;
1431 KMP_MB(); // Flush all pending memory write invalidates.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001432 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001433} // __kmp_remove_one_handler
1434
Jonathan Peyton30419822017-05-12 18:01:32 +00001435void __kmp_install_signals(int parallel_init) {
1436 KB_TRACE(10, ("__kmp_install_signals: called\n"));
1437 if (!__kmp_handle_signals) {
1438 KB_TRACE(10, ("__kmp_install_signals: KMP_HANDLE_SIGNALS is false - "
1439 "handlers not installed\n"));
1440 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001441 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001442 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1443 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1444 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1445 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1446 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1447 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001448} // __kmp_install_signals
1449
Jonathan Peyton30419822017-05-12 18:01:32 +00001450void __kmp_remove_signals(void) {
1451 int sig;
1452 KB_TRACE(10, ("__kmp_remove_signals: called\n"));
1453 for (sig = 1; sig < NSIG; ++sig) {
1454 __kmp_remove_one_handler(sig);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001455 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001456} // __kmp_remove_signals
1457
Jim Cownie5e8470a2013-09-27 10:38:44 +00001458#endif // KMP_HANDLE_SIGNALS
1459
1460/* Put the thread to sleep for a time period */
Jonathan Peyton30419822017-05-12 18:01:32 +00001461void __kmp_thread_sleep(int millis) {
1462 DWORD status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001463
Jonathan Peyton30419822017-05-12 18:01:32 +00001464 status = SleepEx((DWORD)millis, FALSE);
1465 if (status) {
1466 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001467 __kmp_fatal(KMP_MSG(FunctionError, "SleepEx()"), KMP_ERR(error),
1468 __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001469 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001470}
1471
Jonathan Peyton30419822017-05-12 18:01:32 +00001472// Determine whether the given address is mapped into the current address space.
1473int __kmp_is_address_mapped(void *addr) {
1474 DWORD status;
1475 MEMORY_BASIC_INFORMATION lpBuffer;
1476 SIZE_T dwLength;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001477
Jonathan Peyton30419822017-05-12 18:01:32 +00001478 dwLength = sizeof(MEMORY_BASIC_INFORMATION);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001479
Jonathan Peyton30419822017-05-12 18:01:32 +00001480 status = VirtualQuery(addr, &lpBuffer, dwLength);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001481
Jonathan Peyton30419822017-05-12 18:01:32 +00001482 return !(((lpBuffer.State == MEM_RESERVE) || (lpBuffer.State == MEM_FREE)) ||
1483 ((lpBuffer.Protect == PAGE_NOACCESS) ||
1484 (lpBuffer.Protect == PAGE_EXECUTE)));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001485}
1486
Jonathan Peyton30419822017-05-12 18:01:32 +00001487kmp_uint64 __kmp_hardware_timestamp(void) {
1488 kmp_uint64 r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001489
Jonathan Peyton30419822017-05-12 18:01:32 +00001490 QueryPerformanceCounter((LARGE_INTEGER *)&r);
1491 return r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001492}
1493
1494/* Free handle and check the error code */
Jonathan Peyton30419822017-05-12 18:01:32 +00001495void __kmp_free_handle(kmp_thread_t tHandle) {
1496 /* called with parameter type HANDLE also, thus suppose kmp_thread_t defined
1497 * as HANDLE */
1498 BOOL rc;
1499 rc = CloseHandle(tHandle);
1500 if (!rc) {
1501 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001502 __kmp_fatal(KMP_MSG(CantCloseHandle), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001503 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001504}
1505
Jonathan Peyton30419822017-05-12 18:01:32 +00001506int __kmp_get_load_balance(int max) {
1507 static ULONG glb_buff_size = 100 * 1024;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001508
Jonathan Peyton30419822017-05-12 18:01:32 +00001509 // Saved count of the running threads for the thread balance algortihm
1510 static int glb_running_threads = 0;
1511 static double glb_call_time = 0; /* Thread balance algorithm call time */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001512
Jonathan Peyton30419822017-05-12 18:01:32 +00001513 int running_threads = 0; // Number of running threads in the system.
1514 NTSTATUS status = 0;
1515 ULONG buff_size = 0;
1516 ULONG info_size = 0;
1517 void *buffer = NULL;
1518 PSYSTEM_PROCESS_INFORMATION spi = NULL;
1519 int first_time = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001520
Jonathan Peyton30419822017-05-12 18:01:32 +00001521 double call_time = 0.0; // start, finish;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001522
Jonathan Peyton30419822017-05-12 18:01:32 +00001523 __kmp_elapsed(&call_time);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001524
Jonathan Peyton30419822017-05-12 18:01:32 +00001525 if (glb_call_time &&
1526 (call_time - glb_call_time < __kmp_load_balance_interval)) {
1527 running_threads = glb_running_threads;
1528 goto finish;
1529 }
1530 glb_call_time = call_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001531
Jonathan Peyton30419822017-05-12 18:01:32 +00001532 // Do not spend time on running algorithm if we have a permanent error.
1533 if (NtQuerySystemInformation == NULL) {
1534 running_threads = -1;
1535 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001536 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001537
1538 if (max <= 0) {
1539 max = INT_MAX;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001540 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001541
1542 do {
1543
1544 if (first_time) {
1545 buff_size = glb_buff_size;
1546 } else {
1547 buff_size = 2 * buff_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001548 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001549
Jonathan Peyton30419822017-05-12 18:01:32 +00001550 buffer = KMP_INTERNAL_REALLOC(buffer, buff_size);
1551 if (buffer == NULL) {
1552 running_threads = -1;
1553 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001554 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001555 status = NtQuerySystemInformation(SystemProcessInformation, buffer,
1556 buff_size, &info_size);
1557 first_time = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001558
Jonathan Peyton30419822017-05-12 18:01:32 +00001559 } while (status == STATUS_INFO_LENGTH_MISMATCH);
1560 glb_buff_size = buff_size;
1561
1562#define CHECK(cond) \
1563 { \
1564 KMP_DEBUG_ASSERT(cond); \
1565 if (!(cond)) { \
1566 running_threads = -1; \
1567 goto finish; \
1568 } \
1569 }
1570
1571 CHECK(buff_size >= info_size);
1572 spi = PSYSTEM_PROCESS_INFORMATION(buffer);
1573 for (;;) {
1574 ptrdiff_t offset = uintptr_t(spi) - uintptr_t(buffer);
1575 CHECK(0 <= offset &&
1576 offset + sizeof(SYSTEM_PROCESS_INFORMATION) < info_size);
1577 HANDLE pid = spi->ProcessId;
1578 ULONG num = spi->NumberOfThreads;
1579 CHECK(num >= 1);
1580 size_t spi_size =
1581 sizeof(SYSTEM_PROCESS_INFORMATION) + sizeof(SYSTEM_THREAD) * (num - 1);
1582 CHECK(offset + spi_size <
1583 info_size); // Make sure process info record fits the buffer.
1584 if (spi->NextEntryOffset != 0) {
1585 CHECK(spi_size <=
1586 spi->NextEntryOffset); // And do not overlap with the next record.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001587 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001588 // pid == 0 corresponds to the System Idle Process. It always has running
1589 // threads on all cores. So, we don't consider the running threads of this
1590 // process.
1591 if (pid != 0) {
1592 for (int i = 0; i < num; ++i) {
1593 THREAD_STATE state = spi->Threads[i].State;
1594 // Count threads that have Ready or Running state.
1595 // !!! TODO: Why comment does not match the code???
1596 if (state == StateRunning) {
1597 ++running_threads;
1598 // Stop counting running threads if the number is already greater than
1599 // the number of available cores
1600 if (running_threads >= max) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001601 goto finish;
Jonathan Peyton30419822017-05-12 18:01:32 +00001602 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001603 }
1604 }
1605 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001606 if (spi->NextEntryOffset == 0) {
1607 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001608 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001609 spi = PSYSTEM_PROCESS_INFORMATION(uintptr_t(spi) + spi->NextEntryOffset);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001610 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001611
Jonathan Peyton30419822017-05-12 18:01:32 +00001612#undef CHECK
Jim Cownie5e8470a2013-09-27 10:38:44 +00001613
Jonathan Peyton30419822017-05-12 18:01:32 +00001614finish: // Clean up and exit.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001615
Jonathan Peyton30419822017-05-12 18:01:32 +00001616 if (buffer != NULL) {
1617 KMP_INTERNAL_FREE(buffer);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001618 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001619
Jonathan Peyton30419822017-05-12 18:01:32 +00001620 glb_running_threads = running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001621
Jonathan Peyton30419822017-05-12 18:01:32 +00001622 return running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001623} //__kmp_get_load_balance()