blob: 8b8891a60e5b4eab230957dad5dbf34044922683 [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//
7// The LLVM Compiler Infrastructure
8//
9// This file is dual licensed under the MIT and the University of Illinois Open
10// Source Licenses. See LICENSE.txt for details.
11//
12//===----------------------------------------------------------------------===//
13
Jim Cownie5e8470a2013-09-27 10:38:44 +000014#include "kmp.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000015#include "kmp_affinity.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000016#include "kmp_i18n.h"
17#include "kmp_io.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000018#include "kmp_itt.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000019#include "kmp_wait_release.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000020
Jim Cownie4cc4bb42014-10-07 16:25:50 +000021/* This code is related to NtQuerySystemInformation() function. This function
22 is used in the Load balance algorithm for OMP_DYNAMIC=true to find the
Jim Cownie5e8470a2013-09-27 10:38:44 +000023 number of running threads in the system. */
24
Jonathan Peyton30419822017-05-12 18:01:32 +000025#include <ntsecapi.h> // UNICODE_STRING
Jim Cownie5e8470a2013-09-27 10:38:44 +000026#include <ntstatus.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000027
28enum SYSTEM_INFORMATION_CLASS {
Jonathan Peyton30419822017-05-12 18:01:32 +000029 SystemProcessInformation = 5
Jim Cownie5e8470a2013-09-27 10:38:44 +000030}; // SYSTEM_INFORMATION_CLASS
31
32struct CLIENT_ID {
Jonathan Peyton30419822017-05-12 18:01:32 +000033 HANDLE UniqueProcess;
34 HANDLE UniqueThread;
Jim Cownie5e8470a2013-09-27 10:38:44 +000035}; // struct CLIENT_ID
36
37enum THREAD_STATE {
Jonathan Peyton30419822017-05-12 18:01:32 +000038 StateInitialized,
39 StateReady,
40 StateRunning,
41 StateStandby,
42 StateTerminated,
43 StateWait,
44 StateTransition,
45 StateUnknown
Jim Cownie5e8470a2013-09-27 10:38:44 +000046}; // enum THREAD_STATE
47
48struct VM_COUNTERS {
Jonathan Peyton30419822017-05-12 18:01:32 +000049 SIZE_T PeakVirtualSize;
50 SIZE_T VirtualSize;
51 ULONG PageFaultCount;
52 SIZE_T PeakWorkingSetSize;
53 SIZE_T WorkingSetSize;
54 SIZE_T QuotaPeakPagedPoolUsage;
55 SIZE_T QuotaPagedPoolUsage;
56 SIZE_T QuotaPeakNonPagedPoolUsage;
57 SIZE_T QuotaNonPagedPoolUsage;
58 SIZE_T PagefileUsage;
59 SIZE_T PeakPagefileUsage;
60 SIZE_T PrivatePageCount;
Jim Cownie5e8470a2013-09-27 10:38:44 +000061}; // struct VM_COUNTERS
62
63struct SYSTEM_THREAD {
Jonathan Peyton30419822017-05-12 18:01:32 +000064 LARGE_INTEGER KernelTime;
65 LARGE_INTEGER UserTime;
66 LARGE_INTEGER CreateTime;
67 ULONG WaitTime;
68 LPVOID StartAddress;
69 CLIENT_ID ClientId;
70 DWORD Priority;
71 LONG BasePriority;
72 ULONG ContextSwitchCount;
73 THREAD_STATE State;
74 ULONG WaitReason;
Jim Cownie5e8470a2013-09-27 10:38:44 +000075}; // SYSTEM_THREAD
76
Jonathan Peyton30419822017-05-12 18:01:32 +000077KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, KernelTime) == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +000078#if KMP_ARCH_X86
Jonathan Peyton30419822017-05-12 18:01:32 +000079KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 28);
80KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 52);
Jim Cownie5e8470a2013-09-27 10:38:44 +000081#else
Jonathan Peyton30419822017-05-12 18:01:32 +000082KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 32);
83KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 68);
Jim Cownie5e8470a2013-09-27 10:38:44 +000084#endif
85
86struct SYSTEM_PROCESS_INFORMATION {
Jonathan Peyton30419822017-05-12 18:01:32 +000087 ULONG NextEntryOffset;
88 ULONG NumberOfThreads;
89 LARGE_INTEGER Reserved[3];
90 LARGE_INTEGER CreateTime;
91 LARGE_INTEGER UserTime;
92 LARGE_INTEGER KernelTime;
93 UNICODE_STRING ImageName;
94 DWORD BasePriority;
95 HANDLE ProcessId;
96 HANDLE ParentProcessId;
97 ULONG HandleCount;
98 ULONG Reserved2[2];
99 VM_COUNTERS VMCounters;
100 IO_COUNTERS IOCounters;
101 SYSTEM_THREAD Threads[1];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000102}; // SYSTEM_PROCESS_INFORMATION
Jonathan Peyton30419822017-05-12 18:01:32 +0000103typedef SYSTEM_PROCESS_INFORMATION *PSYSTEM_PROCESS_INFORMATION;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000104
Jonathan Peyton30419822017-05-12 18:01:32 +0000105KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, NextEntryOffset) == 0);
106KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, CreateTime) == 32);
107KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ImageName) == 56);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000108#if KMP_ARCH_X86
Jonathan Peyton30419822017-05-12 18:01:32 +0000109KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 68);
110KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 76);
111KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 88);
112KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 136);
113KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 184);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000114#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000115KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 80);
116KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 96);
117KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 112);
118KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 208);
119KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 256);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000120#endif
121
Jonathan Peyton30419822017-05-12 18:01:32 +0000122typedef NTSTATUS(NTAPI *NtQuerySystemInformation_t)(SYSTEM_INFORMATION_CLASS,
123 PVOID, ULONG, PULONG);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000124NtQuerySystemInformation_t NtQuerySystemInformation = NULL;
125
126HMODULE ntdll = NULL;
127
128/* End of NtQuerySystemInformation()-related code */
129
Jim Cownie5e8470a2013-09-27 10:38:44 +0000130static HMODULE kernel32 = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000131
Jim Cownie5e8470a2013-09-27 10:38:44 +0000132#if KMP_HANDLE_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000133typedef void (*sig_func_t)(int);
134static sig_func_t __kmp_sighldrs[NSIG];
135static int __kmp_siginstalled[NSIG];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000136#endif
137
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000138#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +0000139static HANDLE __kmp_monitor_ev;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000140#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000141static kmp_int64 __kmp_win32_time;
142double __kmp_win32_tick;
143
144int __kmp_init_runtime = FALSE;
145CRITICAL_SECTION __kmp_win32_section;
146
Jonathan Peyton30419822017-05-12 18:01:32 +0000147void __kmp_win32_mutex_init(kmp_win32_mutex_t *mx) {
148 InitializeCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000149#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000150 __kmp_itt_system_object_created(&mx->cs, "Critical Section");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000151#endif /* USE_ITT_BUILD */
152}
153
Jonathan Peyton30419822017-05-12 18:01:32 +0000154void __kmp_win32_mutex_destroy(kmp_win32_mutex_t *mx) {
155 DeleteCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000156}
157
Jonathan Peyton30419822017-05-12 18:01:32 +0000158void __kmp_win32_mutex_lock(kmp_win32_mutex_t *mx) {
159 EnterCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000160}
161
Jonathan Peyton9b8bb322019-01-16 20:07:39 +0000162int __kmp_win32_mutex_trylock(kmp_win32_mutex_t *mx) {
163 return TryEnterCriticalSection(&mx->cs);
164}
165
Jonathan Peyton30419822017-05-12 18:01:32 +0000166void __kmp_win32_mutex_unlock(kmp_win32_mutex_t *mx) {
167 LeaveCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000168}
169
Jonathan Peyton30419822017-05-12 18:01:32 +0000170void __kmp_win32_cond_init(kmp_win32_cond_t *cv) {
171 cv->waiters_count_ = 0;
172 cv->wait_generation_count_ = 0;
173 cv->release_count_ = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000174
Jonathan Peyton30419822017-05-12 18:01:32 +0000175 /* Initialize the critical section */
176 __kmp_win32_mutex_init(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000177
Jonathan Peyton30419822017-05-12 18:01:32 +0000178 /* Create a manual-reset event. */
179 cv->event_ = CreateEvent(NULL, // no security
180 TRUE, // manual-reset
181 FALSE, // non-signaled initially
182 NULL); // unnamed
Jim Cownie5e8470a2013-09-27 10:38:44 +0000183#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000184 __kmp_itt_system_object_created(cv->event_, "Event");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000185#endif /* USE_ITT_BUILD */
186}
187
Jonathan Peyton30419822017-05-12 18:01:32 +0000188void __kmp_win32_cond_destroy(kmp_win32_cond_t *cv) {
189 __kmp_win32_mutex_destroy(&cv->waiters_count_lock_);
190 __kmp_free_handle(cv->event_);
191 memset(cv, '\0', sizeof(*cv));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000192}
193
194/* TODO associate cv with a team instead of a thread so as to optimize
Jonathan Peyton30419822017-05-12 18:01:32 +0000195 the case where we wake up a whole team */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000196
Jonathan Peyton30419822017-05-12 18:01:32 +0000197void __kmp_win32_cond_wait(kmp_win32_cond_t *cv, kmp_win32_mutex_t *mx,
198 kmp_info_t *th, int need_decrease_load) {
199 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 (;;) {
215 int wait_done;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000216
Jonathan Peyton30419822017-05-12 18:01:32 +0000217 /* Wait until the event is signaled */
218 WaitForSingleObject(cv->event_, INFINITE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000219
Jonathan Peyton30419822017-05-12 18:01:32 +0000220 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000221
Jonathan Peyton30419822017-05-12 18:01:32 +0000222 /* Exit the loop when the <cv->event_> is signaled and there are still
223 waiting threads from this <wait_generation> that haven't been released
224 from this wait yet. */
225 wait_done = (cv->release_count_ > 0) &&
226 (cv->wait_generation_count_ != my_generation);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000227
Jonathan Peyton30419822017-05-12 18:01:32 +0000228 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000229
Jonathan Peyton30419822017-05-12 18:01:32 +0000230 /* there used to be a semicolon after the if statement, it looked like a
231 bug, so i removed it */
232 if (wait_done)
233 break;
234 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000235
Jonathan Peyton30419822017-05-12 18:01:32 +0000236 __kmp_win32_mutex_lock(mx);
237 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000238
Jonathan Peyton30419822017-05-12 18:01:32 +0000239 cv->waiters_count_--;
240 cv->release_count_--;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000241
Jonathan Peyton30419822017-05-12 18:01:32 +0000242 last_waiter = (cv->release_count_ == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000243
Jonathan Peyton30419822017-05-12 18:01:32 +0000244 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000245
Jonathan Peyton30419822017-05-12 18:01:32 +0000246 if (last_waiter) {
247 /* We're the last waiter to be notified, so reset the manual event. */
248 ResetEvent(cv->event_);
249 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000250}
251
Jonathan Peyton30419822017-05-12 18:01:32 +0000252void __kmp_win32_cond_broadcast(kmp_win32_cond_t *cv) {
253 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000254
Jonathan Peyton30419822017-05-12 18:01:32 +0000255 if (cv->waiters_count_ > 0) {
256 SetEvent(cv->event_);
257 /* Release all the threads in this generation. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000258
Jonathan Peyton30419822017-05-12 18:01:32 +0000259 cv->release_count_ = cv->waiters_count_;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000260
Jonathan Peyton30419822017-05-12 18:01:32 +0000261 /* Start a new generation. */
262 cv->wait_generation_count_++;
263 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000264
Jonathan Peyton30419822017-05-12 18:01:32 +0000265 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000266}
267
Jonathan Peyton30419822017-05-12 18:01:32 +0000268void __kmp_win32_cond_signal(kmp_win32_cond_t *cv) {
269 __kmp_win32_cond_broadcast(cv);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000270}
271
Jonathan Peyton30419822017-05-12 18:01:32 +0000272void __kmp_enable(int new_state) {
273 if (__kmp_init_runtime)
274 LeaveCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000275}
276
Jonathan Peyton30419822017-05-12 18:01:32 +0000277void __kmp_disable(int *old_state) {
278 *old_state = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000279
Jonathan Peyton30419822017-05-12 18:01:32 +0000280 if (__kmp_init_runtime)
281 EnterCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000282}
283
Jonathan Peyton30419822017-05-12 18:01:32 +0000284void __kmp_suspend_initialize(void) { /* do nothing */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000285}
286
Jonathan Peyton30419822017-05-12 18:01:32 +0000287static void __kmp_suspend_initialize_thread(kmp_info_t *th) {
288 if (!TCR_4(th->th.th_suspend_init)) {
289 /* this means we haven't initialized the suspension pthread objects for this
290 thread in this instance of the process */
291 __kmp_win32_cond_init(&th->th.th_suspend_cv);
292 __kmp_win32_mutex_init(&th->th.th_suspend_mx);
293 TCW_4(th->th.th_suspend_init, TRUE);
294 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000295}
296
Jonathan Peyton30419822017-05-12 18:01:32 +0000297void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
298 if (TCR_4(th->th.th_suspend_init)) {
299 /* this means we have initialize the suspension pthread objects for this
300 thread in this instance of the process */
301 __kmp_win32_cond_destroy(&th->th.th_suspend_cv);
302 __kmp_win32_mutex_destroy(&th->th.th_suspend_mx);
303 TCW_4(th->th.th_suspend_init, FALSE);
304 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000305}
306
Jonathan Peyton9b8bb322019-01-16 20:07:39 +0000307int __kmp_try_suspend_mx(kmp_info_t *th) {
308 return __kmp_win32_mutex_trylock(&th->th.th_suspend_mx);
309}
310
311void __kmp_lock_suspend_mx(kmp_info_t *th) {
312 __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
313}
314
315void __kmp_unlock_suspend_mx(kmp_info_t *th) {
316 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
317}
318
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000319/* This routine puts the calling thread to sleep after setting the
Jonathan Peyton30419822017-05-12 18:01:32 +0000320 sleep bit for the indicated flag variable to true. */
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000321template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +0000322static inline void __kmp_suspend_template(int th_gtid, C *flag) {
323 kmp_info_t *th = __kmp_threads[th_gtid];
324 int status;
325 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000326
Jonathan Peyton30419822017-05-12 18:01:32 +0000327 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag's loc(%p)\n",
328 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000329
Jonathan Peyton30419822017-05-12 18:01:32 +0000330 __kmp_suspend_initialize_thread(th);
331 __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000332
Jonathan Peyton30419822017-05-12 18:01:32 +0000333 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for flag's"
334 " loc(%p)\n",
335 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000336
Jonathan Peyton30419822017-05-12 18:01:32 +0000337 /* TODO: shouldn't this use release semantics to ensure that
338 __kmp_suspend_initialize_thread gets called first? */
339 old_spin = flag->set_sleeping();
Jonathan Peyton9b8bb322019-01-16 20:07:39 +0000340#if OMP_50_ENABLED
341 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME &&
342 __kmp_pause_status != kmp_soft_paused) {
343 flag->unset_sleeping();
344 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
345 return;
346 }
347#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000348
Jonathan Peyton30419822017-05-12 18:01:32 +0000349 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for flag's"
350 " loc(%p)==%d\n",
351 th_gtid, flag->get(), *(flag->get())));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000352
Jonathan Peyton30419822017-05-12 18:01:32 +0000353 if (flag->done_check_val(old_spin)) {
354 old_spin = flag->unset_sleeping();
355 KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
356 "for flag's loc(%p)\n",
357 th_gtid, flag->get()));
358 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000359#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +0000360 __kmp_suspend_count++;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000361#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000362 /* Encapsulate in a loop as the documentation states that this may "with
363 low probability" return when the condition variable has not been signaled
364 or broadcast */
365 int deactivated = FALSE;
366 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
367 while (flag->is_sleeping()) {
368 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
369 "kmp_win32_cond_wait()\n",
370 th_gtid));
371 // Mark the thread as no longer active (only in the first iteration of the
372 // loop).
373 if (!deactivated) {
374 th->th.th_active = FALSE;
375 if (th->th.th_active_in_pool) {
376 th->th.th_active_in_pool = FALSE;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +0000377 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +0000378 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
379 }
380 deactivated = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000381
Jonathan Peyton30419822017-05-12 18:01:32 +0000382 __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, 0,
383 0);
384 } else {
385 __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, 0,
386 0);
387 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000388
389#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000390 if (flag->is_sleeping()) {
391 KF_TRACE(100,
392 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
393 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000394#endif /* KMP_DEBUG */
395
Jonathan Peyton30419822017-05-12 18:01:32 +0000396 } // while
Jim Cownie5e8470a2013-09-27 10:38:44 +0000397
Jonathan Peyton30419822017-05-12 18:01:32 +0000398 // Mark the thread as active again (if it was previous marked as inactive)
399 if (deactivated) {
400 th->th.th_active = TRUE;
401 if (TCR_4(th->th.th_in_pool)) {
Jonathan Peyton37e2ef52018-07-09 17:36:22 +0000402 KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +0000403 th->th.th_active_in_pool = TRUE;
404 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000405 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000406 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000407
Jonathan Peyton30419822017-05-12 18:01:32 +0000408 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000409
Jonathan Peyton30419822017-05-12 18:01:32 +0000410 KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000411}
412
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000413void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000414 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000415}
416void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000417 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000418}
419void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000420 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000421}
422
Jim Cownie5e8470a2013-09-27 10:38:44 +0000423/* This routine signals the thread specified by target_gtid to wake up
Jonathan Peyton30419822017-05-12 18:01:32 +0000424 after setting the sleep bit indicated by the flag argument to FALSE */
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000425template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +0000426static inline void __kmp_resume_template(int target_gtid, C *flag) {
427 kmp_info_t *th = __kmp_threads[target_gtid];
428 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000429
430#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000431 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000432#endif
433
Jonathan Peyton30419822017-05-12 18:01:32 +0000434 KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
435 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000436
Jonathan Peyton30419822017-05-12 18:01:32 +0000437 __kmp_suspend_initialize_thread(th);
438 __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000439
Jonathan Peyton30419822017-05-12 18:01:32 +0000440 if (!flag) { // coming from __kmp_null_resume_wrapper
441 flag = (C *)th->th.th_sleep_loc;
442 }
443
444 // First, check if the flag is null or its type has changed. If so, someone
445 // else woke it up.
446 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type
447 // simply shows what
448 // flag was cast to
449 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
450 "awake: flag's loc(%p)\n",
451 gtid, target_gtid, NULL));
452 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
453 return;
454 } else {
455 typename C::flag_t old_spin = flag->unset_sleeping();
456 if (!flag->is_sleeping_val(old_spin)) {
457 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
458 "awake: flag's loc(%p): %u => %u\n",
459 gtid, target_gtid, flag->get(), old_spin, *(flag->get())));
460 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
461 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000462 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000463 }
464 TCW_PTR(th->th.th_sleep_loc, NULL);
465 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep "
466 "bit for flag's loc(%p)\n",
467 gtid, target_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000468
Jonathan Peyton30419822017-05-12 18:01:32 +0000469 __kmp_win32_cond_signal(&th->th.th_suspend_cv);
470 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000471
Jonathan Peyton30419822017-05-12 18:01:32 +0000472 KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
473 " for T#%d\n",
474 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000475}
476
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000477void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000478 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000479}
480void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000481 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000482}
483void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000484 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000485}
486
Jonathan Peyton30419822017-05-12 18:01:32 +0000487void __kmp_yield(int cond) {
488 if (cond)
489 Sleep(0);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000490}
491
Jonathan Peyton30419822017-05-12 18:01:32 +0000492void __kmp_gtid_set_specific(int gtid) {
493 if (__kmp_init_gtid) {
494 KA_TRACE(50, ("__kmp_gtid_set_specific: T#%d key:%d\n", gtid,
495 __kmp_gtid_threadprivate_key));
496 if (!TlsSetValue(__kmp_gtid_threadprivate_key, (LPVOID)(gtid + 1)))
497 KMP_FATAL(TLSSetValueFailed);
498 } else {
499 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
500 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000501}
502
Jonathan Peyton30419822017-05-12 18:01:32 +0000503int __kmp_gtid_get_specific() {
504 int gtid;
505 if (!__kmp_init_gtid) {
506 KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
507 "KMP_GTID_SHUTDOWN\n"));
508 return KMP_GTID_SHUTDOWN;
509 }
510 gtid = (int)(kmp_intptr_t)TlsGetValue(__kmp_gtid_threadprivate_key);
511 if (gtid == 0) {
512 gtid = KMP_GTID_DNE;
513 } else {
514 gtid--;
515 }
516 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
517 __kmp_gtid_threadprivate_key, gtid));
518 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000519}
520
Jonathan Peyton30419822017-05-12 18:01:32 +0000521void __kmp_affinity_bind_thread(int proc) {
522 if (__kmp_num_proc_groups > 1) {
523 // Form the GROUP_AFFINITY struct directly, rather than filling
524 // out a bit vector and calling __kmp_set_system_affinity().
525 GROUP_AFFINITY ga;
526 KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups * CHAR_BIT *
527 sizeof(DWORD_PTR))));
528 ga.Group = proc / (CHAR_BIT * sizeof(DWORD_PTR));
529 ga.Mask = (unsigned long long)1 << (proc % (CHAR_BIT * sizeof(DWORD_PTR)));
530 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000531
Jonathan Peyton30419822017-05-12 18:01:32 +0000532 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
533 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
534 DWORD error = GetLastError();
535 if (__kmp_affinity_verbose) { // AC: continue silently if not verbose
536 kmp_msg_t err_code = KMP_ERR(error);
537 __kmp_msg(kmp_ms_warning, KMP_MSG(CantSetThreadAffMask), err_code,
538 __kmp_msg_null);
539 if (__kmp_generate_warnings == kmp_warnings_off) {
540 __kmp_str_free(&err_code.str);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000541 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000542 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000543 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000544 } else {
545 kmp_affin_mask_t *mask;
546 KMP_CPU_ALLOC_ON_STACK(mask);
547 KMP_CPU_ZERO(mask);
548 KMP_CPU_SET(proc, mask);
549 __kmp_set_system_affinity(mask, TRUE);
550 KMP_CPU_FREE_FROM_STACK(mask);
551 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000552}
553
Jonathan Peyton30419822017-05-12 18:01:32 +0000554void __kmp_affinity_determine_capable(const char *env_var) {
555// All versions of Windows* OS (since Win '95) support SetThreadAffinityMask().
Jim Cownie5e8470a2013-09-27 10:38:44 +0000556
Andrey Churbanov7daf9802015-01-27 16:52:57 +0000557#if KMP_GROUP_AFFINITY
Jonathan Peyton30419822017-05-12 18:01:32 +0000558 KMP_AFFINITY_ENABLE(__kmp_num_proc_groups * sizeof(DWORD_PTR));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000559#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000560 KMP_AFFINITY_ENABLE(sizeof(DWORD_PTR));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000561#endif
562
Jonathan Peyton30419822017-05-12 18:01:32 +0000563 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
564 "Windows* OS affinity interface functional (mask size = "
565 "%" KMP_SIZE_T_SPEC ").\n",
566 __kmp_affin_mask_size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000567}
568
Jonathan Peyton30419822017-05-12 18:01:32 +0000569double __kmp_read_cpu_time(void) {
570 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
571 int status;
572 double cpu_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000573
Jonathan Peyton30419822017-05-12 18:01:32 +0000574 cpu_time = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000575
Jonathan Peyton30419822017-05-12 18:01:32 +0000576 status = GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime,
577 &KernelTime, &UserTime);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000578
Jonathan Peyton30419822017-05-12 18:01:32 +0000579 if (status) {
580 double sec = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000581
Jonathan Peyton30419822017-05-12 18:01:32 +0000582 sec += KernelTime.dwHighDateTime;
583 sec += UserTime.dwHighDateTime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000584
Jonathan Peyton30419822017-05-12 18:01:32 +0000585 /* Shift left by 32 bits */
586 sec *= (double)(1 << 16) * (double)(1 << 16);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000587
Jonathan Peyton30419822017-05-12 18:01:32 +0000588 sec += KernelTime.dwLowDateTime;
589 sec += UserTime.dwLowDateTime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000590
Jonathan Peyton30419822017-05-12 18:01:32 +0000591 cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC;
592 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000593
Jonathan Peyton30419822017-05-12 18:01:32 +0000594 return cpu_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000595}
596
Jonathan Peyton30419822017-05-12 18:01:32 +0000597int __kmp_read_system_info(struct kmp_sys_info *info) {
598 info->maxrss = 0; /* the maximum resident set size utilized (in kilobytes) */
599 info->minflt = 0; /* the number of page faults serviced without any I/O */
600 info->majflt = 0; /* the number of page faults serviced that required I/O */
601 info->nswap = 0; // the number of times a process was "swapped" out of memory
602 info->inblock = 0; // the number of times the file system had to perform input
603 info->oublock = 0; // number of times the file system had to perform output
604 info->nvcsw = 0; /* the number of times a context switch was voluntarily */
605 info->nivcsw = 0; /* the number of times a context switch was forced */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000606
Jonathan Peyton30419822017-05-12 18:01:32 +0000607 return 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000608}
609
Jonathan Peyton30419822017-05-12 18:01:32 +0000610void __kmp_runtime_initialize(void) {
611 SYSTEM_INFO info;
612 kmp_str_buf_t path;
613 UINT path_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000614
Jonathan Peyton30419822017-05-12 18:01:32 +0000615 if (__kmp_init_runtime) {
616 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000617 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000618
Jonathan Peyton99016992015-05-26 17:32:53 +0000619#if KMP_DYNAMIC_LIB
Jonathan Peyton30419822017-05-12 18:01:32 +0000620 /* Pin dynamic library for the lifetime of application */
621 {
622 // First, turn off error message boxes
623 UINT err_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
624 HMODULE h;
625 BOOL ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
626 GET_MODULE_HANDLE_EX_FLAG_PIN,
627 (LPCTSTR)&__kmp_serial_initialize, &h);
628 KMP_DEBUG_ASSERT2(h && ret, "OpenMP RTL cannot find itself loaded");
629 SetErrorMode(err_mode); // Restore error mode
630 KA_TRACE(10, ("__kmp_runtime_initialize: dynamic library pinned\n"));
631 }
Andrey Churbanov9bf53282015-01-29 17:18:20 +0000632#endif
633
Jonathan Peyton30419822017-05-12 18:01:32 +0000634 InitializeCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000635#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000636 __kmp_itt_system_object_created(&__kmp_win32_section, "Critical Section");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000637#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000638 __kmp_initialize_system_tick();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000639
Jonathan Peyton30419822017-05-12 18:01:32 +0000640#if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
641 if (!__kmp_cpuinfo.initialized) {
642 __kmp_query_cpuid(&__kmp_cpuinfo);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000643 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000644#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000645
Jonathan Peyton30419822017-05-12 18:01:32 +0000646/* Set up minimum number of threads to switch to TLS gtid */
Jonathan Peyton8b3842f2018-10-05 17:59:39 +0000647#if KMP_OS_WINDOWS && !KMP_DYNAMIC_LIB
Jonathan Peyton30419822017-05-12 18:01:32 +0000648 // Windows* OS, static library.
649 /* New thread may use stack space previously used by another thread,
650 currently terminated. On Windows* OS, in case of static linking, we do not
651 know the moment of thread termination, and our structures (__kmp_threads
652 and __kmp_root arrays) are still keep info about dead threads. This leads
653 to problem in __kmp_get_global_thread_id() function: it wrongly finds gtid
654 (by searching through stack addresses of all known threads) for
655 unregistered foreign tread.
Jim Cownie5e8470a2013-09-27 10:38:44 +0000656
Jonathan Peyton30419822017-05-12 18:01:32 +0000657 Setting __kmp_tls_gtid_min to 0 workarounds this problem:
658 __kmp_get_global_thread_id() does not search through stacks, but get gtid
659 from TLS immediately.
660 --ln
661 */
662 __kmp_tls_gtid_min = 0;
663#else
664 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
665#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000666
Jonathan Peyton30419822017-05-12 18:01:32 +0000667 /* for the static library */
668 if (!__kmp_gtid_threadprivate_key) {
669 __kmp_gtid_threadprivate_key = TlsAlloc();
670 if (__kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES) {
671 KMP_FATAL(TLSOutOfIndexes);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000672 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000673 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000674
Jonathan Peyton30419822017-05-12 18:01:32 +0000675 // Load ntdll.dll.
676 /* Simple GetModuleHandle( "ntdll.dl" ) is not suitable due to security issue
677 (see http://www.microsoft.com/technet/security/advisory/2269637.mspx). We
678 have to specify full path to the library. */
679 __kmp_str_buf_init(&path);
680 path_size = GetSystemDirectory(path.str, path.size);
681 KMP_DEBUG_ASSERT(path_size > 0);
682 if (path_size >= path.size) {
683 // Buffer is too short. Expand the buffer and try again.
684 __kmp_str_buf_reserve(&path, path_size);
685 path_size = GetSystemDirectory(path.str, path.size);
686 KMP_DEBUG_ASSERT(path_size > 0);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000687 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000688 if (path_size > 0 && path_size < path.size) {
689 // Now we have system directory name in the buffer.
690 // Append backslash and name of dll to form full path,
691 path.used = path_size;
692 __kmp_str_buf_print(&path, "\\%s", "ntdll.dll");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000693
Jonathan Peyton30419822017-05-12 18:01:32 +0000694 // Now load ntdll using full path.
695 ntdll = GetModuleHandle(path.str);
696 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000697
Jonathan Peyton30419822017-05-12 18:01:32 +0000698 KMP_DEBUG_ASSERT(ntdll != NULL);
699 if (ntdll != NULL) {
700 NtQuerySystemInformation = (NtQuerySystemInformation_t)GetProcAddress(
701 ntdll, "NtQuerySystemInformation");
702 }
703 KMP_DEBUG_ASSERT(NtQuerySystemInformation != NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000704
Andrey Churbanov7daf9802015-01-27 16:52:57 +0000705#if KMP_GROUP_AFFINITY
Jonathan Peyton30419822017-05-12 18:01:32 +0000706 // Load kernel32.dll.
707 // Same caveat - must use full system path name.
708 if (path_size > 0 && path_size < path.size) {
709 // Truncate the buffer back to just the system path length,
710 // discarding "\\ntdll.dll", and replacing it with "kernel32.dll".
711 path.used = path_size;
712 __kmp_str_buf_print(&path, "\\%s", "kernel32.dll");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000713
Jonathan Peyton30419822017-05-12 18:01:32 +0000714 // Load kernel32.dll using full path.
715 kernel32 = GetModuleHandle(path.str);
716 KA_TRACE(10, ("__kmp_runtime_initialize: kernel32.dll = %s\n", path.str));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000717
Jonathan Peyton30419822017-05-12 18:01:32 +0000718 // Load the function pointers to kernel32.dll routines
719 // that may or may not exist on this system.
720 if (kernel32 != NULL) {
721 __kmp_GetActiveProcessorCount =
722 (kmp_GetActiveProcessorCount_t)GetProcAddress(
723 kernel32, "GetActiveProcessorCount");
724 __kmp_GetActiveProcessorGroupCount =
725 (kmp_GetActiveProcessorGroupCount_t)GetProcAddress(
726 kernel32, "GetActiveProcessorGroupCount");
727 __kmp_GetThreadGroupAffinity =
728 (kmp_GetThreadGroupAffinity_t)GetProcAddress(
729 kernel32, "GetThreadGroupAffinity");
730 __kmp_SetThreadGroupAffinity =
731 (kmp_SetThreadGroupAffinity_t)GetProcAddress(
732 kernel32, "SetThreadGroupAffinity");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000733
Jonathan Peyton30419822017-05-12 18:01:32 +0000734 KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_GetActiveProcessorCount"
735 " = %p\n",
736 __kmp_GetActiveProcessorCount));
737 KA_TRACE(10, ("__kmp_runtime_initialize: "
738 "__kmp_GetActiveProcessorGroupCount = %p\n",
739 __kmp_GetActiveProcessorGroupCount));
740 KA_TRACE(10, ("__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity"
741 " = %p\n",
742 __kmp_GetThreadGroupAffinity));
743 KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity"
744 " = %p\n",
745 __kmp_SetThreadGroupAffinity));
746 KA_TRACE(10, ("__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
747 sizeof(kmp_affin_mask_t)));
Andrey Churbanovdf6555b2015-01-27 17:00:03 +0000748
Jonathan Peyton30419822017-05-12 18:01:32 +0000749 // See if group affinity is supported on this system.
750 // If so, calculate the #groups and #procs.
751 //
752 // Group affinity was introduced with Windows* 7 OS and
753 // Windows* Server 2008 R2 OS.
754 if ((__kmp_GetActiveProcessorCount != NULL) &&
755 (__kmp_GetActiveProcessorGroupCount != NULL) &&
756 (__kmp_GetThreadGroupAffinity != NULL) &&
757 (__kmp_SetThreadGroupAffinity != NULL) &&
758 ((__kmp_num_proc_groups = __kmp_GetActiveProcessorGroupCount()) >
759 1)) {
760 // Calculate the total number of active OS procs.
761 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000762
Jonathan Peyton30419822017-05-12 18:01:32 +0000763 KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
764 " detected\n",
765 __kmp_num_proc_groups));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000766
Jonathan Peyton30419822017-05-12 18:01:32 +0000767 __kmp_xproc = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000768
Jonathan Peyton30419822017-05-12 18:01:32 +0000769 for (i = 0; i < __kmp_num_proc_groups; i++) {
770 DWORD size = __kmp_GetActiveProcessorCount(i);
771 __kmp_xproc += size;
772 KA_TRACE(10, ("__kmp_runtime_initialize: proc group %d size = %d\n",
773 i, size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000774 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000775 } else {
776 KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
777 " detected\n",
778 __kmp_num_proc_groups));
779 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000780 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000781 }
782 if (__kmp_num_proc_groups <= 1) {
783 GetSystemInfo(&info);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000784 __kmp_xproc = info.dwNumberOfProcessors;
Jonathan Peyton30419822017-05-12 18:01:32 +0000785 }
786#else
787 GetSystemInfo(&info);
788 __kmp_xproc = info.dwNumberOfProcessors;
Andrey Churbanov7daf9802015-01-27 16:52:57 +0000789#endif /* KMP_GROUP_AFFINITY */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000790
Jonathan Peyton30419822017-05-12 18:01:32 +0000791 // If the OS said there were 0 procs, take a guess and use a value of 2.
792 // This is done for Linux* OS, also. Do we need error / warning?
793 if (__kmp_xproc <= 0) {
794 __kmp_xproc = 2;
795 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000796
Jonathan Peyton30419822017-05-12 18:01:32 +0000797 KA_TRACE(5,
798 ("__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000799
Jonathan Peyton30419822017-05-12 18:01:32 +0000800 __kmp_str_buf_free(&path);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000801
802#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000803 __kmp_itt_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000804#endif /* USE_ITT_BUILD */
805
Jonathan Peyton30419822017-05-12 18:01:32 +0000806 __kmp_init_runtime = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000807} // __kmp_runtime_initialize
808
Jonathan Peyton30419822017-05-12 18:01:32 +0000809void __kmp_runtime_destroy(void) {
810 if (!__kmp_init_runtime) {
811 return;
812 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000813
814#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000815 __kmp_itt_destroy();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000816#endif /* USE_ITT_BUILD */
817
Jonathan Peyton30419822017-05-12 18:01:32 +0000818 /* we can't DeleteCriticalsection( & __kmp_win32_section ); */
819 /* due to the KX_TRACE() commands */
820 KA_TRACE(40, ("__kmp_runtime_destroy\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000821
Jonathan Peyton30419822017-05-12 18:01:32 +0000822 if (__kmp_gtid_threadprivate_key) {
823 TlsFree(__kmp_gtid_threadprivate_key);
824 __kmp_gtid_threadprivate_key = 0;
825 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000826
Jonathan Peyton30419822017-05-12 18:01:32 +0000827 __kmp_affinity_uninitialize();
828 DeleteCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000829
Jonathan Peyton30419822017-05-12 18:01:32 +0000830 ntdll = NULL;
831 NtQuerySystemInformation = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000832
833#if KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000834 kernel32 = NULL;
835 __kmp_GetActiveProcessorCount = NULL;
836 __kmp_GetActiveProcessorGroupCount = NULL;
837 __kmp_GetThreadGroupAffinity = NULL;
838 __kmp_SetThreadGroupAffinity = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000839#endif // KMP_ARCH_X86_64
840
Jonathan Peyton30419822017-05-12 18:01:32 +0000841 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000842}
843
Jonathan Peyton30419822017-05-12 18:01:32 +0000844void __kmp_terminate_thread(int gtid) {
845 kmp_info_t *th = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000846
Jonathan Peyton30419822017-05-12 18:01:32 +0000847 if (!th)
848 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000849
Jonathan Peyton30419822017-05-12 18:01:32 +0000850 KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000851
Jonathan Peyton30419822017-05-12 18:01:32 +0000852 if (TerminateThread(th->th.th_info.ds.ds_thread, (DWORD)-1) == FALSE) {
853 /* It's OK, the thread may have exited already */
854 }
855 __kmp_free_handle(th->th.th_info.ds.ds_thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000856}
857
Jonathan Peyton30419822017-05-12 18:01:32 +0000858void __kmp_clear_system_time(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000859 BOOL status;
Jonathan Peyton30419822017-05-12 18:01:32 +0000860 LARGE_INTEGER time;
861 status = QueryPerformanceCounter(&time);
862 __kmp_win32_time = (kmp_int64)time.QuadPart;
863}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000864
Jonathan Peyton30419822017-05-12 18:01:32 +0000865void __kmp_initialize_system_tick(void) {
866 {
867 BOOL status;
868 LARGE_INTEGER freq;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000869
Jonathan Peyton30419822017-05-12 18:01:32 +0000870 status = QueryPerformanceFrequency(&freq);
871 if (!status) {
872 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000873 __kmp_fatal(KMP_MSG(FunctionError, "QueryPerformanceFrequency()"),
874 KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +0000875
876 } else {
877 __kmp_win32_tick = ((double)1.0) / (double)freq.QuadPart;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000878 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000879 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000880}
881
882/* Calculate the elapsed wall clock time for the user */
883
Jonathan Peyton30419822017-05-12 18:01:32 +0000884void __kmp_elapsed(double *t) {
885 BOOL status;
886 LARGE_INTEGER now;
887 status = QueryPerformanceCounter(&now);
888 *t = ((double)now.QuadPart) * __kmp_win32_tick;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000889}
890
891/* Calculate the elapsed wall clock tick for the user */
892
Jonathan Peyton30419822017-05-12 18:01:32 +0000893void __kmp_elapsed_tick(double *t) { *t = __kmp_win32_tick; }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000894
Jonathan Peyton30419822017-05-12 18:01:32 +0000895void __kmp_read_system_time(double *delta) {
896 if (delta != NULL) {
897 BOOL status;
898 LARGE_INTEGER now;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000899
Jonathan Peyton30419822017-05-12 18:01:32 +0000900 status = QueryPerformanceCounter(&now);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000901
Jonathan Peyton30419822017-05-12 18:01:32 +0000902 *delta = ((double)(((kmp_int64)now.QuadPart) - __kmp_win32_time)) *
903 __kmp_win32_tick;
904 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000905}
906
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000907/* Return the current time stamp in nsec */
Jonathan Peyton30419822017-05-12 18:01:32 +0000908kmp_uint64 __kmp_now_nsec() {
909 LARGE_INTEGER now;
910 QueryPerformanceCounter(&now);
911 return 1e9 * __kmp_win32_tick * now.QuadPart;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000912}
913
Andrey Churbanovf700e9e2018-12-10 13:45:00 +0000914extern "C"
Jonathan Peyton30419822017-05-12 18:01:32 +0000915void *__stdcall __kmp_launch_worker(void *arg) {
916 volatile void *stack_data;
917 void *exit_val;
918 void *padding = 0;
919 kmp_info_t *this_thr = (kmp_info_t *)arg;
920 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000921
Jonathan Peyton30419822017-05-12 18:01:32 +0000922 gtid = this_thr->th.th_info.ds.ds_gtid;
923 __kmp_gtid_set_specific(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000924#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000925#error "This define causes problems with LoadLibrary() + declspec(thread) " \
Jim Cownie5e8470a2013-09-27 10:38:44 +0000926 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
927 "reference: http://support.microsoft.com/kb/118816"
Jonathan Peyton30419822017-05-12 18:01:32 +0000928//__kmp_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000929#endif
930
931#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000932 __kmp_itt_thread_name(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000933#endif /* USE_ITT_BUILD */
934
Jonathan Peyton30419822017-05-12 18:01:32 +0000935 __kmp_affinity_set_init_mask(gtid, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000936
937#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000938 // Set FP control regs to be a copy of the parallel initialization thread's.
939 __kmp_clear_x87_fpu_status_word();
940 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
941 __kmp_load_mxcsr(&__kmp_init_mxcsr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000942#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
943
Jonathan Peyton30419822017-05-12 18:01:32 +0000944 if (__kmp_stkoffset > 0 && gtid > 0) {
945 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
946 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000947
Jonathan Peyton30419822017-05-12 18:01:32 +0000948 KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
949 this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
950 TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000951
Jonathan Peyton30419822017-05-12 18:01:32 +0000952 if (TCR_4(__kmp_gtid_mode) <
953 2) { // check stack only if it is used to get gtid
954 TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
955 KMP_ASSERT(this_thr->th.th_info.ds.ds_stackgrow == FALSE);
956 __kmp_check_stack_overlap(this_thr);
957 }
958 KMP_MB();
959 exit_val = __kmp_launch_thread(this_thr);
960 KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
961 TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
962 KMP_MB();
963 return exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000964}
965
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000966#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000967/* The monitor thread controls all of the threads in the complex */
968
Jonathan Peyton30419822017-05-12 18:01:32 +0000969void *__stdcall __kmp_launch_monitor(void *arg) {
970 DWORD wait_status;
971 kmp_thread_t monitor;
972 int status;
973 int interval;
974 kmp_info_t *this_thr = (kmp_info_t *)arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000975
Jonathan Peyton30419822017-05-12 18:01:32 +0000976 KMP_DEBUG_ASSERT(__kmp_init_monitor);
977 TCW_4(__kmp_init_monitor, 2); // AC: Signal library that monitor has started
978 // TODO: hide "2" in enum (like {true,false,started})
979 this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
980 TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000981
Jonathan Peyton30419822017-05-12 18:01:32 +0000982 KMP_MB(); /* Flush all pending memory write invalidates. */
983 KA_TRACE(10, ("__kmp_launch_monitor: launched\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000984
Jonathan Peyton30419822017-05-12 18:01:32 +0000985 monitor = GetCurrentThread();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000986
Jonathan Peyton30419822017-05-12 18:01:32 +0000987 /* set thread priority */
988 status = SetThreadPriority(monitor, THREAD_PRIORITY_HIGHEST);
989 if (!status) {
990 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000991 __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +0000992 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000993
Jonathan Peyton30419822017-05-12 18:01:32 +0000994 /* register us as monitor */
995 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000996#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000997#error "This define causes problems with LoadLibrary() + declspec(thread) " \
Jim Cownie5e8470a2013-09-27 10:38:44 +0000998 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
999 "reference: http://support.microsoft.com/kb/118816"
Jonathan Peyton30419822017-05-12 18:01:32 +00001000//__kmp_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001001#endif
1002
1003#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001004 __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore
1005// monitor thread.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001006#endif /* USE_ITT_BUILD */
1007
Jonathan Peyton30419822017-05-12 18:01:32 +00001008 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001009
Jonathan Peyton30419822017-05-12 18:01:32 +00001010 interval = (1000 / __kmp_monitor_wakeups); /* in milliseconds */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001011
Jonathan Peyton30419822017-05-12 18:01:32 +00001012 while (!TCR_4(__kmp_global.g.g_done)) {
1013 /* This thread monitors the state of the system */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001014
Jonathan Peyton30419822017-05-12 18:01:32 +00001015 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001016
Jonathan Peyton30419822017-05-12 18:01:32 +00001017 wait_status = WaitForSingleObject(__kmp_monitor_ev, interval);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001018
Jonathan Peyton30419822017-05-12 18:01:32 +00001019 if (wait_status == WAIT_TIMEOUT) {
1020 TCW_4(__kmp_global.g.g_time.dt.t_value,
1021 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001022 }
1023
Jonathan Peyton30419822017-05-12 18:01:32 +00001024 KMP_MB(); /* Flush all pending memory write invalidates. */
1025 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001026
Jonathan Peyton30419822017-05-12 18:01:32 +00001027 KA_TRACE(10, ("__kmp_launch_monitor: finished\n"));
1028
1029 status = SetThreadPriority(monitor, THREAD_PRIORITY_NORMAL);
1030 if (!status) {
1031 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001032 __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001033 }
1034
1035 if (__kmp_global.g.g_abort != 0) {
1036 /* now we need to terminate the worker threads */
1037 /* the value of t_abort is the signal we caught */
1038 int gtid;
1039
1040 KA_TRACE(10, ("__kmp_launch_monitor: terminate sig=%d\n",
1041 (__kmp_global.g.g_abort)));
1042
1043 /* terminate the OpenMP worker threads */
1044 /* TODO this is not valid for sibling threads!!
1045 * the uber master might not be 0 anymore.. */
1046 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1047 __kmp_terminate_thread(gtid);
1048
1049 __kmp_cleanup();
1050
1051 Sleep(0);
1052
1053 KA_TRACE(10,
1054 ("__kmp_launch_monitor: raise sig=%d\n", __kmp_global.g.g_abort));
1055
1056 if (__kmp_global.g.g_abort > 0) {
1057 raise(__kmp_global.g.g_abort);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001058 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001059 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001060
Jonathan Peyton30419822017-05-12 18:01:32 +00001061 TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001062
Jonathan Peyton30419822017-05-12 18:01:32 +00001063 KMP_MB();
1064 return arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001065}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001066#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001067
Jonathan Peyton30419822017-05-12 18:01:32 +00001068void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
1069 kmp_thread_t handle;
1070 DWORD idThread;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001071
Jonathan Peyton30419822017-05-12 18:01:32 +00001072 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001073
Jonathan Peyton30419822017-05-12 18:01:32 +00001074 th->th.th_info.ds.ds_gtid = gtid;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001075
Jonathan Peyton30419822017-05-12 18:01:32 +00001076 if (KMP_UBER_GTID(gtid)) {
1077 int stack_data;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001078
Jonathan Peyton30419822017-05-12 18:01:32 +00001079 /* TODO: GetCurrentThread() returns a pseudo-handle that is unsuitable for
1080 other threads to use. Is it appropriate to just use GetCurrentThread?
1081 When should we close this handle? When unregistering the root? */
1082 {
1083 BOOL rc;
1084 rc = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
1085 GetCurrentProcess(), &th->th.th_info.ds.ds_thread, 0,
1086 FALSE, DUPLICATE_SAME_ACCESS);
1087 KMP_ASSERT(rc);
1088 KA_TRACE(10, (" __kmp_create_worker: ROOT Handle duplicated, th = %p, "
1089 "handle = %" KMP_UINTPTR_SPEC "\n",
1090 (LPVOID)th, th->th.th_info.ds.ds_thread));
1091 th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001092 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001093 if (TCR_4(__kmp_gtid_mode) < 2) { // check stack only if used to get gtid
1094 /* we will dynamically update the stack range if gtid_mode == 1 */
1095 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1096 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1097 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1098 __kmp_check_stack_overlap(th);
1099 }
1100 } else {
1101 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001102
Jonathan Peyton30419822017-05-12 18:01:32 +00001103 /* Set stack size for this thread now. */
1104 KA_TRACE(10,
1105 ("__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC " bytes\n",
1106 stack_size));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001107
Jonathan Peyton30419822017-05-12 18:01:32 +00001108 stack_size += gtid * __kmp_stkoffset;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001109
Jonathan Peyton30419822017-05-12 18:01:32 +00001110 TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1111 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001112
Jonathan Peyton30419822017-05-12 18:01:32 +00001113 KA_TRACE(10,
1114 ("__kmp_create_worker: (before) stack_size = %" KMP_SIZE_T_SPEC
1115 " bytes, &__kmp_launch_worker = %p, th = %p, &idThread = %p\n",
1116 (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1117 (LPVOID)th, &idThread));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001118
Jonathan Peyton30419822017-05-12 18:01:32 +00001119 handle = CreateThread(
1120 NULL, (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)__kmp_launch_worker,
1121 (LPVOID)th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001122
Jonathan Peyton30419822017-05-12 18:01:32 +00001123 KA_TRACE(10,
1124 ("__kmp_create_worker: (after) stack_size = %" KMP_SIZE_T_SPEC
1125 " bytes, &__kmp_launch_worker = %p, th = %p, "
1126 "idThread = %u, handle = %" KMP_UINTPTR_SPEC "\n",
1127 (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1128 (LPVOID)th, idThread, handle));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001129
Jonathan Peyton30419822017-05-12 18:01:32 +00001130 if (handle == 0) {
1131 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001132 __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001133 } else {
1134 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001135 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001136
Jonathan Peyton30419822017-05-12 18:01:32 +00001137 KMP_MB(); /* Flush all pending memory write invalidates. */
1138 }
1139
1140 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001141}
1142
Jonathan Peyton30419822017-05-12 18:01:32 +00001143int __kmp_still_running(kmp_info_t *th) {
1144 return (WAIT_TIMEOUT == WaitForSingleObject(th->th.th_info.ds.ds_thread, 0));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001145}
1146
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001147#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001148void __kmp_create_monitor(kmp_info_t *th) {
1149 kmp_thread_t handle;
1150 DWORD idThread;
1151 int ideal, new_ideal;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001152
Jonathan Peyton30419822017-05-12 18:01:32 +00001153 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
1154 // We don't need monitor thread in case of MAX_BLOCKTIME
1155 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
1156 "MAX blocktime\n"));
1157 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
1158 th->th.th_info.ds.ds_gtid = 0;
1159 TCW_4(__kmp_init_monitor, 2); // Signal to stop waiting for monitor creation
1160 return;
1161 }
1162 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001163
Jonathan Peyton30419822017-05-12 18:01:32 +00001164 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001165
Jonathan Peyton30419822017-05-12 18:01:32 +00001166 __kmp_monitor_ev = CreateEvent(NULL, TRUE, FALSE, NULL);
1167 if (__kmp_monitor_ev == NULL) {
1168 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001169 __kmp_fatal(KMP_MSG(CantCreateEvent), KMP_ERR(error), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001170 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001171#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001172 __kmp_itt_system_object_created(__kmp_monitor_ev, "Event");
Jim Cownie5e8470a2013-09-27 10:38:44 +00001173#endif /* USE_ITT_BUILD */
1174
Jonathan Peyton30419822017-05-12 18:01:32 +00001175 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1176 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001177
Jonathan Peyton30419822017-05-12 18:01:32 +00001178 // FIXME - on Windows* OS, if __kmp_monitor_stksize = 0, figure out how
1179 // to automatically expand stacksize based on CreateThread error code.
1180 if (__kmp_monitor_stksize == 0) {
1181 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1182 }
1183 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
1184 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1185 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001186
Jonathan Peyton30419822017-05-12 18:01:32 +00001187 KA_TRACE(10, ("__kmp_create_monitor: requested stacksize = %d bytes\n",
1188 (int)__kmp_monitor_stksize));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001189
Jonathan Peyton30419822017-05-12 18:01:32 +00001190 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001191
Jonathan Peyton30419822017-05-12 18:01:32 +00001192 handle =
1193 CreateThread(NULL, (SIZE_T)__kmp_monitor_stksize,
1194 (LPTHREAD_START_ROUTINE)__kmp_launch_monitor, (LPVOID)th,
1195 STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
1196 if (handle == 0) {
1197 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001198 __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001199 } else
1200 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001201
Jonathan Peyton30419822017-05-12 18:01:32 +00001202 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001203
Jonathan Peyton30419822017-05-12 18:01:32 +00001204 KA_TRACE(10, ("__kmp_create_monitor: monitor created %p\n",
1205 (void *)th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001206}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001207#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001208
Jonathan Peyton30419822017-05-12 18:01:32 +00001209/* Check to see if thread is still alive.
1210 NOTE: The ExitProcess(code) system call causes all threads to Terminate
1211 with a exit_val = code. Because of this we can not rely on exit_val having
1212 any particular value. So this routine may return STILL_ALIVE in exit_val
1213 even after the thread is dead. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001214
Jonathan Peyton30419822017-05-12 18:01:32 +00001215int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val) {
1216 DWORD rc;
1217 rc = GetExitCodeThread(th->th.th_info.ds.ds_thread, exit_val);
1218 if (rc == 0) {
1219 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001220 __kmp_fatal(KMP_MSG(FunctionError, "GetExitCodeThread()"), KMP_ERR(error),
1221 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001222 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001223 return (*exit_val == STILL_ACTIVE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001224}
1225
Jonathan Peyton30419822017-05-12 18:01:32 +00001226void __kmp_exit_thread(int exit_status) {
1227 ExitThread(exit_status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001228} // __kmp_exit_thread
1229
Jonathan Peyton30419822017-05-12 18:01:32 +00001230// This is a common part for both __kmp_reap_worker() and __kmp_reap_monitor().
1231static void __kmp_reap_common(kmp_info_t *th) {
1232 DWORD exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001233
Jonathan Peyton30419822017-05-12 18:01:32 +00001234 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001235
Jonathan Peyton30419822017-05-12 18:01:32 +00001236 KA_TRACE(
1237 10, ("__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001238
Jonathan Peyton30419822017-05-12 18:01:32 +00001239 /* 2006-10-19:
1240 There are two opposite situations:
1241 1. Windows* OS keep thread alive after it resets ds_alive flag and
1242 exits from thread function. (For example, see C70770/Q394281 "unloading of
1243 dll based on OMP is very slow".)
1244 2. Windows* OS may kill thread before it resets ds_alive flag.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001245
Jonathan Peyton30419822017-05-12 18:01:32 +00001246 Right solution seems to be waiting for *either* thread termination *or*
1247 ds_alive resetting. */
1248 {
1249 // TODO: This code is very similar to KMP_WAIT_YIELD. Need to generalize
1250 // KMP_WAIT_YIELD to cover this usage also.
1251 void *obj = NULL;
Ed Maste414544c2017-07-07 21:06:05 +00001252 kmp_uint32 spins;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001253#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001254 KMP_FSYNC_SPIN_INIT(obj, (void *)&th->th.th_info.ds.ds_alive);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001255#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00001256 KMP_INIT_YIELD(spins);
1257 do {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001258#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001259 KMP_FSYNC_SPIN_PREPARE(obj);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001260#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00001261 __kmp_is_thread_alive(th, &exit_val);
1262 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
1263 KMP_YIELD_SPIN(spins);
1264 } while (exit_val == STILL_ACTIVE && TCR_4(th->th.th_info.ds.ds_alive));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001265#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001266 if (exit_val == STILL_ACTIVE) {
1267 KMP_FSYNC_CANCEL(obj);
1268 } else {
1269 KMP_FSYNC_SPIN_ACQUIRED(obj);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001270 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001271#endif /* USE_ITT_BUILD */
1272 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001273
Jonathan Peyton30419822017-05-12 18:01:32 +00001274 __kmp_free_handle(th->th.th_info.ds.ds_thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001275
Jonathan Peyton30419822017-05-12 18:01:32 +00001276 /* NOTE: The ExitProcess(code) system call causes all threads to Terminate
1277 with a exit_val = code. Because of this we can not rely on exit_val having
1278 any particular value. */
1279 if (exit_val == STILL_ACTIVE) {
1280 KA_TRACE(1, ("__kmp_reap_common: thread still active.\n"));
1281 } else if ((void *)exit_val != (void *)th) {
1282 KA_TRACE(1, ("__kmp_reap_common: ExitProcess / TerminateThread used?\n"));
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001283 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001284
Jonathan Peyton30419822017-05-12 18:01:32 +00001285 KA_TRACE(10,
1286 ("__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
1287 "\n",
1288 th->th.th_info.ds.ds_gtid, th->th.th_info.ds.ds_thread));
1289
1290 th->th.th_info.ds.ds_thread = 0;
1291 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1292 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1293 th->th.th_info.ds.ds_thread_id = 0;
1294
1295 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001296}
1297
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001298#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001299void __kmp_reap_monitor(kmp_info_t *th) {
1300 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001301
Jonathan Peyton30419822017-05-12 18:01:32 +00001302 KA_TRACE(10, ("__kmp_reap_monitor: try to reap %p\n",
1303 (void *)th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001304
Jonathan Peyton30419822017-05-12 18:01:32 +00001305 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1306 // If both tid and gtid are 0, it means the monitor did not ever start.
1307 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1308 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1309 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1310 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1311 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001312 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001313
Jonathan Peyton30419822017-05-12 18:01:32 +00001314 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001315
Jonathan Peyton30419822017-05-12 18:01:32 +00001316 status = SetEvent(__kmp_monitor_ev);
1317 if (status == FALSE) {
1318 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001319 __kmp_fatal(KMP_MSG(CantSetEvent), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001320 }
1321 KA_TRACE(10, ("__kmp_reap_monitor: reaping thread (%d)\n",
1322 th->th.th_info.ds.ds_gtid));
1323 __kmp_reap_common(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001324
Jonathan Peyton30419822017-05-12 18:01:32 +00001325 __kmp_free_handle(__kmp_monitor_ev);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001326
Jonathan Peyton30419822017-05-12 18:01:32 +00001327 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001328}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001329#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001330
Jonathan Peyton30419822017-05-12 18:01:32 +00001331void __kmp_reap_worker(kmp_info_t *th) {
1332 KA_TRACE(10, ("__kmp_reap_worker: reaping thread (%d)\n",
1333 th->th.th_info.ds.ds_gtid));
1334 __kmp_reap_common(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001335}
1336
Jim Cownie5e8470a2013-09-27 10:38:44 +00001337#if KMP_HANDLE_SIGNALS
1338
Jonathan Peyton30419822017-05-12 18:01:32 +00001339static void __kmp_team_handler(int signo) {
1340 if (__kmp_global.g.g_abort == 0) {
1341 // Stage 1 signal handler, let's shut down all of the threads.
1342 if (__kmp_debug_buf) {
1343 __kmp_dump_debug_buffer();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001344 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001345 KMP_MB(); // Flush all pending memory write invalidates.
1346 TCW_4(__kmp_global.g.g_abort, signo);
1347 KMP_MB(); // Flush all pending memory write invalidates.
1348 TCW_4(__kmp_global.g.g_done, TRUE);
1349 KMP_MB(); // Flush all pending memory write invalidates.
1350 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001351} // __kmp_team_handler
1352
Jonathan Peyton30419822017-05-12 18:01:32 +00001353static sig_func_t __kmp_signal(int signum, sig_func_t handler) {
1354 sig_func_t old = signal(signum, handler);
1355 if (old == SIG_ERR) {
1356 int error = errno;
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001357 __kmp_fatal(KMP_MSG(FunctionError, "signal"), KMP_ERR(error),
1358 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001359 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001360 return old;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001361}
1362
Jonathan Peyton30419822017-05-12 18:01:32 +00001363static void __kmp_install_one_handler(int sig, sig_func_t handler,
1364 int parallel_init) {
1365 sig_func_t old;
1366 KMP_MB(); /* Flush all pending memory write invalidates. */
1367 KB_TRACE(60, ("__kmp_install_one_handler: called: sig=%d\n", sig));
1368 if (parallel_init) {
1369 old = __kmp_signal(sig, handler);
1370 // SIG_DFL on Windows* OS in NULL or 0.
1371 if (old == __kmp_sighldrs[sig]) {
1372 __kmp_siginstalled[sig] = 1;
1373 } else { // Restore/keep user's handler if one previously installed.
1374 old = __kmp_signal(sig, old);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001375 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001376 } else {
1377 // Save initial/system signal handlers to see if user handlers installed.
1378 // 2009-09-23: It is a dead code. On Windows* OS __kmp_install_signals
1379 // called once with parallel_init == TRUE.
1380 old = __kmp_signal(sig, SIG_DFL);
1381 __kmp_sighldrs[sig] = old;
1382 __kmp_signal(sig, old);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001383 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001384 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001385} // __kmp_install_one_handler
1386
Jonathan Peyton30419822017-05-12 18:01:32 +00001387static void __kmp_remove_one_handler(int sig) {
1388 if (__kmp_siginstalled[sig]) {
1389 sig_func_t old;
1390 KMP_MB(); // Flush all pending memory write invalidates.
1391 KB_TRACE(60, ("__kmp_remove_one_handler: called: sig=%d\n", sig));
1392 old = __kmp_signal(sig, __kmp_sighldrs[sig]);
1393 if (old != __kmp_team_handler) {
1394 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1395 "restoring: sig=%d\n",
1396 sig));
1397 old = __kmp_signal(sig, old);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001398 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001399 __kmp_sighldrs[sig] = NULL;
1400 __kmp_siginstalled[sig] = 0;
1401 KMP_MB(); // Flush all pending memory write invalidates.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001402 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001403} // __kmp_remove_one_handler
1404
Jonathan Peyton30419822017-05-12 18:01:32 +00001405void __kmp_install_signals(int parallel_init) {
1406 KB_TRACE(10, ("__kmp_install_signals: called\n"));
1407 if (!__kmp_handle_signals) {
1408 KB_TRACE(10, ("__kmp_install_signals: KMP_HANDLE_SIGNALS is false - "
1409 "handlers not installed\n"));
1410 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001411 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001412 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1413 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1414 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1415 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1416 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1417 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001418} // __kmp_install_signals
1419
Jonathan Peyton30419822017-05-12 18:01:32 +00001420void __kmp_remove_signals(void) {
1421 int sig;
1422 KB_TRACE(10, ("__kmp_remove_signals: called\n"));
1423 for (sig = 1; sig < NSIG; ++sig) {
1424 __kmp_remove_one_handler(sig);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001425 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001426} // __kmp_remove_signals
1427
Jim Cownie5e8470a2013-09-27 10:38:44 +00001428#endif // KMP_HANDLE_SIGNALS
1429
1430/* Put the thread to sleep for a time period */
Jonathan Peyton30419822017-05-12 18:01:32 +00001431void __kmp_thread_sleep(int millis) {
1432 DWORD status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001433
Jonathan Peyton30419822017-05-12 18:01:32 +00001434 status = SleepEx((DWORD)millis, FALSE);
1435 if (status) {
1436 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001437 __kmp_fatal(KMP_MSG(FunctionError, "SleepEx()"), KMP_ERR(error),
1438 __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001439 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001440}
1441
Jonathan Peyton30419822017-05-12 18:01:32 +00001442// Determine whether the given address is mapped into the current address space.
1443int __kmp_is_address_mapped(void *addr) {
1444 DWORD status;
1445 MEMORY_BASIC_INFORMATION lpBuffer;
1446 SIZE_T dwLength;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001447
Jonathan Peyton30419822017-05-12 18:01:32 +00001448 dwLength = sizeof(MEMORY_BASIC_INFORMATION);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001449
Jonathan Peyton30419822017-05-12 18:01:32 +00001450 status = VirtualQuery(addr, &lpBuffer, dwLength);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001451
Jonathan Peyton30419822017-05-12 18:01:32 +00001452 return !(((lpBuffer.State == MEM_RESERVE) || (lpBuffer.State == MEM_FREE)) ||
1453 ((lpBuffer.Protect == PAGE_NOACCESS) ||
1454 (lpBuffer.Protect == PAGE_EXECUTE)));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001455}
1456
Jonathan Peyton30419822017-05-12 18:01:32 +00001457kmp_uint64 __kmp_hardware_timestamp(void) {
1458 kmp_uint64 r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001459
Jonathan Peyton30419822017-05-12 18:01:32 +00001460 QueryPerformanceCounter((LARGE_INTEGER *)&r);
1461 return r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001462}
1463
1464/* Free handle and check the error code */
Jonathan Peyton30419822017-05-12 18:01:32 +00001465void __kmp_free_handle(kmp_thread_t tHandle) {
1466 /* called with parameter type HANDLE also, thus suppose kmp_thread_t defined
1467 * as HANDLE */
1468 BOOL rc;
1469 rc = CloseHandle(tHandle);
1470 if (!rc) {
1471 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001472 __kmp_fatal(KMP_MSG(CantCloseHandle), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001473 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001474}
1475
Jonathan Peyton30419822017-05-12 18:01:32 +00001476int __kmp_get_load_balance(int max) {
1477 static ULONG glb_buff_size = 100 * 1024;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001478
Jonathan Peyton30419822017-05-12 18:01:32 +00001479 // Saved count of the running threads for the thread balance algortihm
1480 static int glb_running_threads = 0;
1481 static double glb_call_time = 0; /* Thread balance algorithm call time */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001482
Jonathan Peyton30419822017-05-12 18:01:32 +00001483 int running_threads = 0; // Number of running threads in the system.
1484 NTSTATUS status = 0;
1485 ULONG buff_size = 0;
1486 ULONG info_size = 0;
1487 void *buffer = NULL;
1488 PSYSTEM_PROCESS_INFORMATION spi = NULL;
1489 int first_time = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001490
Jonathan Peyton30419822017-05-12 18:01:32 +00001491 double call_time = 0.0; // start, finish;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001492
Jonathan Peyton30419822017-05-12 18:01:32 +00001493 __kmp_elapsed(&call_time);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001494
Jonathan Peyton30419822017-05-12 18:01:32 +00001495 if (glb_call_time &&
1496 (call_time - glb_call_time < __kmp_load_balance_interval)) {
1497 running_threads = glb_running_threads;
1498 goto finish;
1499 }
1500 glb_call_time = call_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001501
Jonathan Peyton30419822017-05-12 18:01:32 +00001502 // Do not spend time on running algorithm if we have a permanent error.
1503 if (NtQuerySystemInformation == NULL) {
1504 running_threads = -1;
1505 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001506 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001507
1508 if (max <= 0) {
1509 max = INT_MAX;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001510 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001511
1512 do {
1513
1514 if (first_time) {
1515 buff_size = glb_buff_size;
1516 } else {
1517 buff_size = 2 * buff_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001518 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001519
Jonathan Peyton30419822017-05-12 18:01:32 +00001520 buffer = KMP_INTERNAL_REALLOC(buffer, buff_size);
1521 if (buffer == NULL) {
1522 running_threads = -1;
1523 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001524 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001525 status = NtQuerySystemInformation(SystemProcessInformation, buffer,
1526 buff_size, &info_size);
1527 first_time = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001528
Jonathan Peyton30419822017-05-12 18:01:32 +00001529 } while (status == STATUS_INFO_LENGTH_MISMATCH);
1530 glb_buff_size = buff_size;
1531
1532#define CHECK(cond) \
1533 { \
1534 KMP_DEBUG_ASSERT(cond); \
1535 if (!(cond)) { \
1536 running_threads = -1; \
1537 goto finish; \
1538 } \
1539 }
1540
1541 CHECK(buff_size >= info_size);
1542 spi = PSYSTEM_PROCESS_INFORMATION(buffer);
1543 for (;;) {
1544 ptrdiff_t offset = uintptr_t(spi) - uintptr_t(buffer);
1545 CHECK(0 <= offset &&
1546 offset + sizeof(SYSTEM_PROCESS_INFORMATION) < info_size);
1547 HANDLE pid = spi->ProcessId;
1548 ULONG num = spi->NumberOfThreads;
1549 CHECK(num >= 1);
1550 size_t spi_size =
1551 sizeof(SYSTEM_PROCESS_INFORMATION) + sizeof(SYSTEM_THREAD) * (num - 1);
1552 CHECK(offset + spi_size <
1553 info_size); // Make sure process info record fits the buffer.
1554 if (spi->NextEntryOffset != 0) {
1555 CHECK(spi_size <=
1556 spi->NextEntryOffset); // And do not overlap with the next record.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001557 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001558 // pid == 0 corresponds to the System Idle Process. It always has running
1559 // threads on all cores. So, we don't consider the running threads of this
1560 // process.
1561 if (pid != 0) {
1562 for (int i = 0; i < num; ++i) {
1563 THREAD_STATE state = spi->Threads[i].State;
1564 // Count threads that have Ready or Running state.
1565 // !!! TODO: Why comment does not match the code???
1566 if (state == StateRunning) {
1567 ++running_threads;
1568 // Stop counting running threads if the number is already greater than
1569 // the number of available cores
1570 if (running_threads >= max) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001571 goto finish;
Jonathan Peyton30419822017-05-12 18:01:32 +00001572 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001573 }
1574 }
1575 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001576 if (spi->NextEntryOffset == 0) {
1577 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001578 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001579 spi = PSYSTEM_PROCESS_INFORMATION(uintptr_t(spi) + spi->NextEntryOffset);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001580 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001581
Jonathan Peyton30419822017-05-12 18:01:32 +00001582#undef CHECK
Jim Cownie5e8470a2013-09-27 10:38:44 +00001583
Jonathan Peyton30419822017-05-12 18:01:32 +00001584finish: // Clean up and exit.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001585
Jonathan Peyton30419822017-05-12 18:01:32 +00001586 if (buffer != NULL) {
1587 KMP_INTERNAL_FREE(buffer);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001588 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001589
Jonathan Peyton30419822017-05-12 18:01:32 +00001590 glb_running_threads = running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001591
Jonathan Peyton30419822017-05-12 18:01:32 +00001592 return running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001593} //__kmp_get_load_balance()