blob: 4854d9dbccece632a7a2c6b63ebbe2254121542f [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
5
6//===----------------------------------------------------------------------===//
7//
8// The LLVM Compiler Infrastructure
9//
10// This file is dual licensed under the MIT and the University of Illinois Open
11// Source Licenses. See LICENSE.txt for details.
12//
13//===----------------------------------------------------------------------===//
14
15
16#include "kmp.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000017#include "kmp_affinity.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000018#include "kmp_i18n.h"
19#include "kmp_io.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000020#include "kmp_itt.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000021#include "kmp_wait_release.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000022
Jim Cownie4cc4bb42014-10-07 16:25:50 +000023/* This code is related to NtQuerySystemInformation() function. This function
24 is used in the Load balance algorithm for OMP_DYNAMIC=true to find the
Jim Cownie5e8470a2013-09-27 10:38:44 +000025 number of running threads in the system. */
26
Jonathan Peyton30419822017-05-12 18:01:32 +000027#include <ntsecapi.h> // UNICODE_STRING
Jim Cownie5e8470a2013-09-27 10:38:44 +000028#include <ntstatus.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000029
30enum SYSTEM_INFORMATION_CLASS {
Jonathan Peyton30419822017-05-12 18:01:32 +000031 SystemProcessInformation = 5
Jim Cownie5e8470a2013-09-27 10:38:44 +000032}; // SYSTEM_INFORMATION_CLASS
33
34struct CLIENT_ID {
Jonathan Peyton30419822017-05-12 18:01:32 +000035 HANDLE UniqueProcess;
36 HANDLE UniqueThread;
Jim Cownie5e8470a2013-09-27 10:38:44 +000037}; // struct CLIENT_ID
38
39enum THREAD_STATE {
Jonathan Peyton30419822017-05-12 18:01:32 +000040 StateInitialized,
41 StateReady,
42 StateRunning,
43 StateStandby,
44 StateTerminated,
45 StateWait,
46 StateTransition,
47 StateUnknown
Jim Cownie5e8470a2013-09-27 10:38:44 +000048}; // enum THREAD_STATE
49
50struct VM_COUNTERS {
Jonathan Peyton30419822017-05-12 18:01:32 +000051 SIZE_T PeakVirtualSize;
52 SIZE_T VirtualSize;
53 ULONG PageFaultCount;
54 SIZE_T PeakWorkingSetSize;
55 SIZE_T WorkingSetSize;
56 SIZE_T QuotaPeakPagedPoolUsage;
57 SIZE_T QuotaPagedPoolUsage;
58 SIZE_T QuotaPeakNonPagedPoolUsage;
59 SIZE_T QuotaNonPagedPoolUsage;
60 SIZE_T PagefileUsage;
61 SIZE_T PeakPagefileUsage;
62 SIZE_T PrivatePageCount;
Jim Cownie5e8470a2013-09-27 10:38:44 +000063}; // struct VM_COUNTERS
64
65struct SYSTEM_THREAD {
Jonathan Peyton30419822017-05-12 18:01:32 +000066 LARGE_INTEGER KernelTime;
67 LARGE_INTEGER UserTime;
68 LARGE_INTEGER CreateTime;
69 ULONG WaitTime;
70 LPVOID StartAddress;
71 CLIENT_ID ClientId;
72 DWORD Priority;
73 LONG BasePriority;
74 ULONG ContextSwitchCount;
75 THREAD_STATE State;
76 ULONG WaitReason;
Jim Cownie5e8470a2013-09-27 10:38:44 +000077}; // SYSTEM_THREAD
78
Jonathan Peyton30419822017-05-12 18:01:32 +000079KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, KernelTime) == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +000080#if KMP_ARCH_X86
Jonathan Peyton30419822017-05-12 18:01:32 +000081KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 28);
82KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 52);
Jim Cownie5e8470a2013-09-27 10:38:44 +000083#else
Jonathan Peyton30419822017-05-12 18:01:32 +000084KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 32);
85KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 68);
Jim Cownie5e8470a2013-09-27 10:38:44 +000086#endif
87
88struct SYSTEM_PROCESS_INFORMATION {
Jonathan Peyton30419822017-05-12 18:01:32 +000089 ULONG NextEntryOffset;
90 ULONG NumberOfThreads;
91 LARGE_INTEGER Reserved[3];
92 LARGE_INTEGER CreateTime;
93 LARGE_INTEGER UserTime;
94 LARGE_INTEGER KernelTime;
95 UNICODE_STRING ImageName;
96 DWORD BasePriority;
97 HANDLE ProcessId;
98 HANDLE ParentProcessId;
99 ULONG HandleCount;
100 ULONG Reserved2[2];
101 VM_COUNTERS VMCounters;
102 IO_COUNTERS IOCounters;
103 SYSTEM_THREAD Threads[1];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000104}; // SYSTEM_PROCESS_INFORMATION
Jonathan Peyton30419822017-05-12 18:01:32 +0000105typedef SYSTEM_PROCESS_INFORMATION *PSYSTEM_PROCESS_INFORMATION;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000106
Jonathan Peyton30419822017-05-12 18:01:32 +0000107KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, NextEntryOffset) == 0);
108KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, CreateTime) == 32);
109KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ImageName) == 56);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000110#if KMP_ARCH_X86
Jonathan Peyton30419822017-05-12 18:01:32 +0000111KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 68);
112KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 76);
113KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 88);
114KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 136);
115KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 184);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000116#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000117KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 80);
118KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 96);
119KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 112);
120KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 208);
121KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 256);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000122#endif
123
Jonathan Peyton30419822017-05-12 18:01:32 +0000124typedef NTSTATUS(NTAPI *NtQuerySystemInformation_t)(SYSTEM_INFORMATION_CLASS,
125 PVOID, ULONG, PULONG);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000126NtQuerySystemInformation_t NtQuerySystemInformation = NULL;
127
128HMODULE ntdll = NULL;
129
130/* End of NtQuerySystemInformation()-related code */
131
Jim Cownie5e8470a2013-09-27 10:38:44 +0000132static HMODULE kernel32 = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000133
Jim Cownie5e8470a2013-09-27 10:38:44 +0000134#if KMP_HANDLE_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000135typedef void (*sig_func_t)(int);
136static sig_func_t __kmp_sighldrs[NSIG];
137static int __kmp_siginstalled[NSIG];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000138#endif
139
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000140#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +0000141static HANDLE __kmp_monitor_ev;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000142#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000143static kmp_int64 __kmp_win32_time;
144double __kmp_win32_tick;
145
146int __kmp_init_runtime = FALSE;
147CRITICAL_SECTION __kmp_win32_section;
148
Jonathan Peyton30419822017-05-12 18:01:32 +0000149void __kmp_win32_mutex_init(kmp_win32_mutex_t *mx) {
150 InitializeCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000151#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000152 __kmp_itt_system_object_created(&mx->cs, "Critical Section");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000153#endif /* USE_ITT_BUILD */
154}
155
Jonathan Peyton30419822017-05-12 18:01:32 +0000156void __kmp_win32_mutex_destroy(kmp_win32_mutex_t *mx) {
157 DeleteCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000158}
159
Jonathan Peyton30419822017-05-12 18:01:32 +0000160void __kmp_win32_mutex_lock(kmp_win32_mutex_t *mx) {
161 EnterCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000162}
163
Jonathan Peyton30419822017-05-12 18:01:32 +0000164void __kmp_win32_mutex_unlock(kmp_win32_mutex_t *mx) {
165 LeaveCriticalSection(&mx->cs);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000166}
167
Jonathan Peyton30419822017-05-12 18:01:32 +0000168void __kmp_win32_cond_init(kmp_win32_cond_t *cv) {
169 cv->waiters_count_ = 0;
170 cv->wait_generation_count_ = 0;
171 cv->release_count_ = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000172
Jonathan Peyton30419822017-05-12 18:01:32 +0000173 /* Initialize the critical section */
174 __kmp_win32_mutex_init(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000175
Jonathan Peyton30419822017-05-12 18:01:32 +0000176 /* Create a manual-reset event. */
177 cv->event_ = CreateEvent(NULL, // no security
178 TRUE, // manual-reset
179 FALSE, // non-signaled initially
180 NULL); // unnamed
Jim Cownie5e8470a2013-09-27 10:38:44 +0000181#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000182 __kmp_itt_system_object_created(cv->event_, "Event");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000183#endif /* USE_ITT_BUILD */
184}
185
Jonathan Peyton30419822017-05-12 18:01:32 +0000186void __kmp_win32_cond_destroy(kmp_win32_cond_t *cv) {
187 __kmp_win32_mutex_destroy(&cv->waiters_count_lock_);
188 __kmp_free_handle(cv->event_);
189 memset(cv, '\0', sizeof(*cv));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000190}
191
192/* TODO associate cv with a team instead of a thread so as to optimize
Jonathan Peyton30419822017-05-12 18:01:32 +0000193 the case where we wake up a whole team */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000194
Jonathan Peyton30419822017-05-12 18:01:32 +0000195void __kmp_win32_cond_wait(kmp_win32_cond_t *cv, kmp_win32_mutex_t *mx,
196 kmp_info_t *th, int need_decrease_load) {
197 int my_generation;
198 int last_waiter;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000199
Jonathan Peyton30419822017-05-12 18:01:32 +0000200 /* Avoid race conditions */
201 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000202
Jonathan Peyton30419822017-05-12 18:01:32 +0000203 /* Increment count of waiters */
204 cv->waiters_count_++;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000205
Jonathan Peyton30419822017-05-12 18:01:32 +0000206 /* Store current generation in our activation record. */
207 my_generation = cv->wait_generation_count_;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000208
Jonathan Peyton30419822017-05-12 18:01:32 +0000209 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
210 __kmp_win32_mutex_unlock(mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000211
Jonathan Peyton30419822017-05-12 18:01:32 +0000212 for (;;) {
213 int wait_done;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000214
Jonathan Peyton30419822017-05-12 18:01:32 +0000215 /* Wait until the event is signaled */
216 WaitForSingleObject(cv->event_, INFINITE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000217
Jonathan Peyton30419822017-05-12 18:01:32 +0000218 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000219
Jonathan Peyton30419822017-05-12 18:01:32 +0000220 /* Exit the loop when the <cv->event_> is signaled and there are still
221 waiting threads from this <wait_generation> that haven't been released
222 from this wait yet. */
223 wait_done = (cv->release_count_ > 0) &&
224 (cv->wait_generation_count_ != my_generation);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000225
Jonathan Peyton30419822017-05-12 18:01:32 +0000226 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000227
Jonathan Peyton30419822017-05-12 18:01:32 +0000228 /* there used to be a semicolon after the if statement, it looked like a
229 bug, so i removed it */
230 if (wait_done)
231 break;
232 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000233
Jonathan Peyton30419822017-05-12 18:01:32 +0000234 __kmp_win32_mutex_lock(mx);
235 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000236
Jonathan Peyton30419822017-05-12 18:01:32 +0000237 cv->waiters_count_--;
238 cv->release_count_--;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000239
Jonathan Peyton30419822017-05-12 18:01:32 +0000240 last_waiter = (cv->release_count_ == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000241
Jonathan Peyton30419822017-05-12 18:01:32 +0000242 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000243
Jonathan Peyton30419822017-05-12 18:01:32 +0000244 if (last_waiter) {
245 /* We're the last waiter to be notified, so reset the manual event. */
246 ResetEvent(cv->event_);
247 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000248}
249
Jonathan Peyton30419822017-05-12 18:01:32 +0000250void __kmp_win32_cond_broadcast(kmp_win32_cond_t *cv) {
251 __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000252
Jonathan Peyton30419822017-05-12 18:01:32 +0000253 if (cv->waiters_count_ > 0) {
254 SetEvent(cv->event_);
255 /* Release all the threads in this generation. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000256
Jonathan Peyton30419822017-05-12 18:01:32 +0000257 cv->release_count_ = cv->waiters_count_;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000258
Jonathan Peyton30419822017-05-12 18:01:32 +0000259 /* Start a new generation. */
260 cv->wait_generation_count_++;
261 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000262
Jonathan Peyton30419822017-05-12 18:01:32 +0000263 __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000264}
265
Jonathan Peyton30419822017-05-12 18:01:32 +0000266void __kmp_win32_cond_signal(kmp_win32_cond_t *cv) {
267 __kmp_win32_cond_broadcast(cv);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000268}
269
Jonathan Peyton30419822017-05-12 18:01:32 +0000270void __kmp_enable(int new_state) {
271 if (__kmp_init_runtime)
272 LeaveCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000273}
274
Jonathan Peyton30419822017-05-12 18:01:32 +0000275void __kmp_disable(int *old_state) {
276 *old_state = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000277
Jonathan Peyton30419822017-05-12 18:01:32 +0000278 if (__kmp_init_runtime)
279 EnterCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000280}
281
Jonathan Peyton30419822017-05-12 18:01:32 +0000282void __kmp_suspend_initialize(void) { /* do nothing */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000283}
284
Jonathan Peyton30419822017-05-12 18:01:32 +0000285static void __kmp_suspend_initialize_thread(kmp_info_t *th) {
286 if (!TCR_4(th->th.th_suspend_init)) {
287 /* this means we haven't initialized the suspension pthread objects for this
288 thread in this instance of the process */
289 __kmp_win32_cond_init(&th->th.th_suspend_cv);
290 __kmp_win32_mutex_init(&th->th.th_suspend_mx);
291 TCW_4(th->th.th_suspend_init, TRUE);
292 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000293}
294
Jonathan Peyton30419822017-05-12 18:01:32 +0000295void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
296 if (TCR_4(th->th.th_suspend_init)) {
297 /* this means we have initialize the suspension pthread objects for this
298 thread in this instance of the process */
299 __kmp_win32_cond_destroy(&th->th.th_suspend_cv);
300 __kmp_win32_mutex_destroy(&th->th.th_suspend_mx);
301 TCW_4(th->th.th_suspend_init, FALSE);
302 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000303}
304
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000305/* This routine puts the calling thread to sleep after setting the
Jonathan Peyton30419822017-05-12 18:01:32 +0000306 sleep bit for the indicated flag variable to true. */
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000307template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +0000308static inline void __kmp_suspend_template(int th_gtid, C *flag) {
309 kmp_info_t *th = __kmp_threads[th_gtid];
310 int status;
311 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000312
Jonathan Peyton30419822017-05-12 18:01:32 +0000313 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag's loc(%p)\n",
314 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000315
Jonathan Peyton30419822017-05-12 18:01:32 +0000316 __kmp_suspend_initialize_thread(th);
317 __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000318
Jonathan Peyton30419822017-05-12 18:01:32 +0000319 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for flag's"
320 " loc(%p)\n",
321 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000322
Jonathan Peyton30419822017-05-12 18:01:32 +0000323 /* TODO: shouldn't this use release semantics to ensure that
324 __kmp_suspend_initialize_thread gets called first? */
325 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000326
Jonathan Peyton30419822017-05-12 18:01:32 +0000327 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for flag's"
328 " loc(%p)==%d\n",
329 th_gtid, flag->get(), *(flag->get())));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000330
Jonathan Peyton30419822017-05-12 18:01:32 +0000331 if (flag->done_check_val(old_spin)) {
332 old_spin = flag->unset_sleeping();
333 KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
334 "for flag's loc(%p)\n",
335 th_gtid, flag->get()));
336 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000337#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +0000338 __kmp_suspend_count++;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000339#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000340 /* Encapsulate in a loop as the documentation states that this may "with
341 low probability" return when the condition variable has not been signaled
342 or broadcast */
343 int deactivated = FALSE;
344 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
345 while (flag->is_sleeping()) {
346 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
347 "kmp_win32_cond_wait()\n",
348 th_gtid));
349 // Mark the thread as no longer active (only in the first iteration of the
350 // loop).
351 if (!deactivated) {
352 th->th.th_active = FALSE;
353 if (th->th.th_active_in_pool) {
354 th->th.th_active_in_pool = FALSE;
355 KMP_TEST_THEN_DEC32((kmp_int32 *)&__kmp_thread_pool_active_nth);
356 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
357 }
358 deactivated = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000359
Jonathan Peyton30419822017-05-12 18:01:32 +0000360 __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, 0,
361 0);
362 } else {
363 __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, 0,
364 0);
365 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000366
367#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000368 if (flag->is_sleeping()) {
369 KF_TRACE(100,
370 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
371 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000372#endif /* KMP_DEBUG */
373
Jonathan Peyton30419822017-05-12 18:01:32 +0000374 } // while
Jim Cownie5e8470a2013-09-27 10:38:44 +0000375
Jonathan Peyton30419822017-05-12 18:01:32 +0000376 // Mark the thread as active again (if it was previous marked as inactive)
377 if (deactivated) {
378 th->th.th_active = TRUE;
379 if (TCR_4(th->th.th_in_pool)) {
380 KMP_TEST_THEN_INC32((kmp_int32 *)&__kmp_thread_pool_active_nth);
381 th->th.th_active_in_pool = TRUE;
382 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000383 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000384 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000385
Jonathan Peyton30419822017-05-12 18:01:32 +0000386 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000387
Jonathan Peyton30419822017-05-12 18:01:32 +0000388 KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000389}
390
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000391void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000392 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000393}
394void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000395 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000396}
397void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000398 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000399}
400
Jim Cownie5e8470a2013-09-27 10:38:44 +0000401/* This routine signals the thread specified by target_gtid to wake up
Jonathan Peyton30419822017-05-12 18:01:32 +0000402 after setting the sleep bit indicated by the flag argument to FALSE */
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000403template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +0000404static inline void __kmp_resume_template(int target_gtid, C *flag) {
405 kmp_info_t *th = __kmp_threads[target_gtid];
406 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000407
408#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +0000409 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000410#endif
411
Jonathan Peyton30419822017-05-12 18:01:32 +0000412 KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
413 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000414
Jonathan Peyton30419822017-05-12 18:01:32 +0000415 __kmp_suspend_initialize_thread(th);
416 __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000417
Jonathan Peyton30419822017-05-12 18:01:32 +0000418 if (!flag) { // coming from __kmp_null_resume_wrapper
419 flag = (C *)th->th.th_sleep_loc;
420 }
421
422 // First, check if the flag is null or its type has changed. If so, someone
423 // else woke it up.
424 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type
425 // simply shows what
426 // flag was cast to
427 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
428 "awake: flag's loc(%p)\n",
429 gtid, target_gtid, NULL));
430 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
431 return;
432 } else {
433 typename C::flag_t old_spin = flag->unset_sleeping();
434 if (!flag->is_sleeping_val(old_spin)) {
435 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
436 "awake: flag's loc(%p): %u => %u\n",
437 gtid, target_gtid, flag->get(), old_spin, *(flag->get())));
438 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
439 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000440 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000441 }
442 TCW_PTR(th->th.th_sleep_loc, NULL);
443 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep "
444 "bit for flag's loc(%p)\n",
445 gtid, target_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000446
Jonathan Peyton30419822017-05-12 18:01:32 +0000447 __kmp_win32_cond_signal(&th->th.th_suspend_cv);
448 __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000449
Jonathan Peyton30419822017-05-12 18:01:32 +0000450 KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
451 " for T#%d\n",
452 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000453}
454
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000455void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000456 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000457}
458void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000459 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000460}
461void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000462 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000463}
464
Jonathan Peyton30419822017-05-12 18:01:32 +0000465void __kmp_yield(int cond) {
466 if (cond)
467 Sleep(0);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000468}
469
Jonathan Peyton30419822017-05-12 18:01:32 +0000470void __kmp_gtid_set_specific(int gtid) {
471 if (__kmp_init_gtid) {
472 KA_TRACE(50, ("__kmp_gtid_set_specific: T#%d key:%d\n", gtid,
473 __kmp_gtid_threadprivate_key));
474 if (!TlsSetValue(__kmp_gtid_threadprivate_key, (LPVOID)(gtid + 1)))
475 KMP_FATAL(TLSSetValueFailed);
476 } else {
477 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
478 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000479}
480
Jonathan Peyton30419822017-05-12 18:01:32 +0000481int __kmp_gtid_get_specific() {
482 int gtid;
483 if (!__kmp_init_gtid) {
484 KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
485 "KMP_GTID_SHUTDOWN\n"));
486 return KMP_GTID_SHUTDOWN;
487 }
488 gtid = (int)(kmp_intptr_t)TlsGetValue(__kmp_gtid_threadprivate_key);
489 if (gtid == 0) {
490 gtid = KMP_GTID_DNE;
491 } else {
492 gtid--;
493 }
494 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
495 __kmp_gtid_threadprivate_key, gtid));
496 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000497}
498
Jonathan Peyton30419822017-05-12 18:01:32 +0000499void __kmp_affinity_bind_thread(int proc) {
500 if (__kmp_num_proc_groups > 1) {
501 // Form the GROUP_AFFINITY struct directly, rather than filling
502 // out a bit vector and calling __kmp_set_system_affinity().
503 GROUP_AFFINITY ga;
504 KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups * CHAR_BIT *
505 sizeof(DWORD_PTR))));
506 ga.Group = proc / (CHAR_BIT * sizeof(DWORD_PTR));
507 ga.Mask = (unsigned long long)1 << (proc % (CHAR_BIT * sizeof(DWORD_PTR)));
508 ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000509
Jonathan Peyton30419822017-05-12 18:01:32 +0000510 KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
511 if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
512 DWORD error = GetLastError();
513 if (__kmp_affinity_verbose) { // AC: continue silently if not verbose
514 kmp_msg_t err_code = KMP_ERR(error);
515 __kmp_msg(kmp_ms_warning, KMP_MSG(CantSetThreadAffMask), err_code,
516 __kmp_msg_null);
517 if (__kmp_generate_warnings == kmp_warnings_off) {
518 __kmp_str_free(&err_code.str);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000519 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000520 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000521 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000522 } else {
523 kmp_affin_mask_t *mask;
524 KMP_CPU_ALLOC_ON_STACK(mask);
525 KMP_CPU_ZERO(mask);
526 KMP_CPU_SET(proc, mask);
527 __kmp_set_system_affinity(mask, TRUE);
528 KMP_CPU_FREE_FROM_STACK(mask);
529 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000530}
531
Jonathan Peyton30419822017-05-12 18:01:32 +0000532void __kmp_affinity_determine_capable(const char *env_var) {
533// All versions of Windows* OS (since Win '95) support SetThreadAffinityMask().
Jim Cownie5e8470a2013-09-27 10:38:44 +0000534
Andrey Churbanov7daf9802015-01-27 16:52:57 +0000535#if KMP_GROUP_AFFINITY
Jonathan Peyton30419822017-05-12 18:01:32 +0000536 KMP_AFFINITY_ENABLE(__kmp_num_proc_groups * sizeof(DWORD_PTR));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000537#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000538 KMP_AFFINITY_ENABLE(sizeof(DWORD_PTR));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000539#endif
540
Jonathan Peyton30419822017-05-12 18:01:32 +0000541 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
542 "Windows* OS affinity interface functional (mask size = "
543 "%" KMP_SIZE_T_SPEC ").\n",
544 __kmp_affin_mask_size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000545}
546
Jonathan Peyton30419822017-05-12 18:01:32 +0000547double __kmp_read_cpu_time(void) {
548 FILETIME CreationTime, ExitTime, KernelTime, UserTime;
549 int status;
550 double cpu_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000551
Jonathan Peyton30419822017-05-12 18:01:32 +0000552 cpu_time = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000553
Jonathan Peyton30419822017-05-12 18:01:32 +0000554 status = GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime,
555 &KernelTime, &UserTime);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000556
Jonathan Peyton30419822017-05-12 18:01:32 +0000557 if (status) {
558 double sec = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000559
Jonathan Peyton30419822017-05-12 18:01:32 +0000560 sec += KernelTime.dwHighDateTime;
561 sec += UserTime.dwHighDateTime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000562
Jonathan Peyton30419822017-05-12 18:01:32 +0000563 /* Shift left by 32 bits */
564 sec *= (double)(1 << 16) * (double)(1 << 16);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000565
Jonathan Peyton30419822017-05-12 18:01:32 +0000566 sec += KernelTime.dwLowDateTime;
567 sec += UserTime.dwLowDateTime;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000568
Jonathan Peyton30419822017-05-12 18:01:32 +0000569 cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC;
570 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000571
Jonathan Peyton30419822017-05-12 18:01:32 +0000572 return cpu_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000573}
574
Jonathan Peyton30419822017-05-12 18:01:32 +0000575int __kmp_read_system_info(struct kmp_sys_info *info) {
576 info->maxrss = 0; /* the maximum resident set size utilized (in kilobytes) */
577 info->minflt = 0; /* the number of page faults serviced without any I/O */
578 info->majflt = 0; /* the number of page faults serviced that required I/O */
579 info->nswap = 0; // the number of times a process was "swapped" out of memory
580 info->inblock = 0; // the number of times the file system had to perform input
581 info->oublock = 0; // number of times the file system had to perform output
582 info->nvcsw = 0; /* the number of times a context switch was voluntarily */
583 info->nivcsw = 0; /* the number of times a context switch was forced */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000584
Jonathan Peyton30419822017-05-12 18:01:32 +0000585 return 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000586}
587
Jonathan Peyton30419822017-05-12 18:01:32 +0000588void __kmp_runtime_initialize(void) {
589 SYSTEM_INFO info;
590 kmp_str_buf_t path;
591 UINT path_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000592
Jonathan Peyton30419822017-05-12 18:01:32 +0000593 if (__kmp_init_runtime) {
594 return;
595 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000596
Jonathan Peyton99016992015-05-26 17:32:53 +0000597#if KMP_DYNAMIC_LIB
Jonathan Peyton30419822017-05-12 18:01:32 +0000598 /* Pin dynamic library for the lifetime of application */
599 {
600 // First, turn off error message boxes
601 UINT err_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
602 HMODULE h;
603 BOOL ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
604 GET_MODULE_HANDLE_EX_FLAG_PIN,
605 (LPCTSTR)&__kmp_serial_initialize, &h);
606 KMP_DEBUG_ASSERT2(h && ret, "OpenMP RTL cannot find itself loaded");
607 SetErrorMode(err_mode); // Restore error mode
608 KA_TRACE(10, ("__kmp_runtime_initialize: dynamic library pinned\n"));
609 }
Andrey Churbanov9bf53282015-01-29 17:18:20 +0000610#endif
611
Jonathan Peyton30419822017-05-12 18:01:32 +0000612 InitializeCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000613#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000614 __kmp_itt_system_object_created(&__kmp_win32_section, "Critical Section");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000615#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000616 __kmp_initialize_system_tick();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000617
Jonathan Peyton30419822017-05-12 18:01:32 +0000618#if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
619 if (!__kmp_cpuinfo.initialized) {
620 __kmp_query_cpuid(&__kmp_cpuinfo);
621 }; // if
622#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000623
Jonathan Peyton30419822017-05-12 18:01:32 +0000624/* Set up minimum number of threads to switch to TLS gtid */
625#if KMP_OS_WINDOWS && !defined KMP_DYNAMIC_LIB
626 // Windows* OS, static library.
627 /* New thread may use stack space previously used by another thread,
628 currently terminated. On Windows* OS, in case of static linking, we do not
629 know the moment of thread termination, and our structures (__kmp_threads
630 and __kmp_root arrays) are still keep info about dead threads. This leads
631 to problem in __kmp_get_global_thread_id() function: it wrongly finds gtid
632 (by searching through stack addresses of all known threads) for
633 unregistered foreign tread.
Jim Cownie5e8470a2013-09-27 10:38:44 +0000634
Jonathan Peyton30419822017-05-12 18:01:32 +0000635 Setting __kmp_tls_gtid_min to 0 workarounds this problem:
636 __kmp_get_global_thread_id() does not search through stacks, but get gtid
637 from TLS immediately.
638 --ln
639 */
640 __kmp_tls_gtid_min = 0;
641#else
642 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
643#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000644
Jonathan Peyton30419822017-05-12 18:01:32 +0000645 /* for the static library */
646 if (!__kmp_gtid_threadprivate_key) {
647 __kmp_gtid_threadprivate_key = TlsAlloc();
648 if (__kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES) {
649 KMP_FATAL(TLSOutOfIndexes);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000650 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000651 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000652
Jonathan Peyton30419822017-05-12 18:01:32 +0000653 // Load ntdll.dll.
654 /* Simple GetModuleHandle( "ntdll.dl" ) is not suitable due to security issue
655 (see http://www.microsoft.com/technet/security/advisory/2269637.mspx). We
656 have to specify full path to the library. */
657 __kmp_str_buf_init(&path);
658 path_size = GetSystemDirectory(path.str, path.size);
659 KMP_DEBUG_ASSERT(path_size > 0);
660 if (path_size >= path.size) {
661 // Buffer is too short. Expand the buffer and try again.
662 __kmp_str_buf_reserve(&path, path_size);
663 path_size = GetSystemDirectory(path.str, path.size);
664 KMP_DEBUG_ASSERT(path_size > 0);
665 }; // if
666 if (path_size > 0 && path_size < path.size) {
667 // Now we have system directory name in the buffer.
668 // Append backslash and name of dll to form full path,
669 path.used = path_size;
670 __kmp_str_buf_print(&path, "\\%s", "ntdll.dll");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000671
Jonathan Peyton30419822017-05-12 18:01:32 +0000672 // Now load ntdll using full path.
673 ntdll = GetModuleHandle(path.str);
674 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000675
Jonathan Peyton30419822017-05-12 18:01:32 +0000676 KMP_DEBUG_ASSERT(ntdll != NULL);
677 if (ntdll != NULL) {
678 NtQuerySystemInformation = (NtQuerySystemInformation_t)GetProcAddress(
679 ntdll, "NtQuerySystemInformation");
680 }
681 KMP_DEBUG_ASSERT(NtQuerySystemInformation != NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000682
Andrey Churbanov7daf9802015-01-27 16:52:57 +0000683#if KMP_GROUP_AFFINITY
Jonathan Peyton30419822017-05-12 18:01:32 +0000684 // Load kernel32.dll.
685 // Same caveat - must use full system path name.
686 if (path_size > 0 && path_size < path.size) {
687 // Truncate the buffer back to just the system path length,
688 // discarding "\\ntdll.dll", and replacing it with "kernel32.dll".
689 path.used = path_size;
690 __kmp_str_buf_print(&path, "\\%s", "kernel32.dll");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000691
Jonathan Peyton30419822017-05-12 18:01:32 +0000692 // Load kernel32.dll using full path.
693 kernel32 = GetModuleHandle(path.str);
694 KA_TRACE(10, ("__kmp_runtime_initialize: kernel32.dll = %s\n", path.str));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000695
Jonathan Peyton30419822017-05-12 18:01:32 +0000696 // Load the function pointers to kernel32.dll routines
697 // that may or may not exist on this system.
698 if (kernel32 != NULL) {
699 __kmp_GetActiveProcessorCount =
700 (kmp_GetActiveProcessorCount_t)GetProcAddress(
701 kernel32, "GetActiveProcessorCount");
702 __kmp_GetActiveProcessorGroupCount =
703 (kmp_GetActiveProcessorGroupCount_t)GetProcAddress(
704 kernel32, "GetActiveProcessorGroupCount");
705 __kmp_GetThreadGroupAffinity =
706 (kmp_GetThreadGroupAffinity_t)GetProcAddress(
707 kernel32, "GetThreadGroupAffinity");
708 __kmp_SetThreadGroupAffinity =
709 (kmp_SetThreadGroupAffinity_t)GetProcAddress(
710 kernel32, "SetThreadGroupAffinity");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000711
Jonathan Peyton30419822017-05-12 18:01:32 +0000712 KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_GetActiveProcessorCount"
713 " = %p\n",
714 __kmp_GetActiveProcessorCount));
715 KA_TRACE(10, ("__kmp_runtime_initialize: "
716 "__kmp_GetActiveProcessorGroupCount = %p\n",
717 __kmp_GetActiveProcessorGroupCount));
718 KA_TRACE(10, ("__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity"
719 " = %p\n",
720 __kmp_GetThreadGroupAffinity));
721 KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity"
722 " = %p\n",
723 __kmp_SetThreadGroupAffinity));
724 KA_TRACE(10, ("__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
725 sizeof(kmp_affin_mask_t)));
Andrey Churbanovdf6555b2015-01-27 17:00:03 +0000726
Jonathan Peyton30419822017-05-12 18:01:32 +0000727 // See if group affinity is supported on this system.
728 // If so, calculate the #groups and #procs.
729 //
730 // Group affinity was introduced with Windows* 7 OS and
731 // Windows* Server 2008 R2 OS.
732 if ((__kmp_GetActiveProcessorCount != NULL) &&
733 (__kmp_GetActiveProcessorGroupCount != NULL) &&
734 (__kmp_GetThreadGroupAffinity != NULL) &&
735 (__kmp_SetThreadGroupAffinity != NULL) &&
736 ((__kmp_num_proc_groups = __kmp_GetActiveProcessorGroupCount()) >
737 1)) {
738 // Calculate the total number of active OS procs.
739 int i;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000740
Jonathan Peyton30419822017-05-12 18:01:32 +0000741 KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
742 " detected\n",
743 __kmp_num_proc_groups));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000744
Jonathan Peyton30419822017-05-12 18:01:32 +0000745 __kmp_xproc = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000746
Jonathan Peyton30419822017-05-12 18:01:32 +0000747 for (i = 0; i < __kmp_num_proc_groups; i++) {
748 DWORD size = __kmp_GetActiveProcessorCount(i);
749 __kmp_xproc += size;
750 KA_TRACE(10, ("__kmp_runtime_initialize: proc group %d size = %d\n",
751 i, size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000752 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000753 } else {
754 KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
755 " detected\n",
756 __kmp_num_proc_groups));
757 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000758 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000759 }
760 if (__kmp_num_proc_groups <= 1) {
761 GetSystemInfo(&info);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000762 __kmp_xproc = info.dwNumberOfProcessors;
Jonathan Peyton30419822017-05-12 18:01:32 +0000763 }
764#else
765 GetSystemInfo(&info);
766 __kmp_xproc = info.dwNumberOfProcessors;
Andrey Churbanov7daf9802015-01-27 16:52:57 +0000767#endif /* KMP_GROUP_AFFINITY */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000768
Jonathan Peyton30419822017-05-12 18:01:32 +0000769 // If the OS said there were 0 procs, take a guess and use a value of 2.
770 // This is done for Linux* OS, also. Do we need error / warning?
771 if (__kmp_xproc <= 0) {
772 __kmp_xproc = 2;
773 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000774
Jonathan Peyton30419822017-05-12 18:01:32 +0000775 KA_TRACE(5,
776 ("__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000777
Jonathan Peyton30419822017-05-12 18:01:32 +0000778 __kmp_str_buf_free(&path);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000779
780#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000781 __kmp_itt_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000782#endif /* USE_ITT_BUILD */
783
Jonathan Peyton30419822017-05-12 18:01:32 +0000784 __kmp_init_runtime = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000785} // __kmp_runtime_initialize
786
Jonathan Peyton30419822017-05-12 18:01:32 +0000787void __kmp_runtime_destroy(void) {
788 if (!__kmp_init_runtime) {
789 return;
790 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000791
792#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000793 __kmp_itt_destroy();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000794#endif /* USE_ITT_BUILD */
795
Jonathan Peyton30419822017-05-12 18:01:32 +0000796 /* we can't DeleteCriticalsection( & __kmp_win32_section ); */
797 /* due to the KX_TRACE() commands */
798 KA_TRACE(40, ("__kmp_runtime_destroy\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000799
Jonathan Peyton30419822017-05-12 18:01:32 +0000800 if (__kmp_gtid_threadprivate_key) {
801 TlsFree(__kmp_gtid_threadprivate_key);
802 __kmp_gtid_threadprivate_key = 0;
803 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000804
Jonathan Peyton30419822017-05-12 18:01:32 +0000805 __kmp_affinity_uninitialize();
806 DeleteCriticalSection(&__kmp_win32_section);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000807
Jonathan Peyton30419822017-05-12 18:01:32 +0000808 ntdll = NULL;
809 NtQuerySystemInformation = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000810
811#if KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000812 kernel32 = NULL;
813 __kmp_GetActiveProcessorCount = NULL;
814 __kmp_GetActiveProcessorGroupCount = NULL;
815 __kmp_GetThreadGroupAffinity = NULL;
816 __kmp_SetThreadGroupAffinity = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000817#endif // KMP_ARCH_X86_64
818
Jonathan Peyton30419822017-05-12 18:01:32 +0000819 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000820}
821
Jonathan Peyton30419822017-05-12 18:01:32 +0000822void __kmp_terminate_thread(int gtid) {
823 kmp_info_t *th = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000824
Jonathan Peyton30419822017-05-12 18:01:32 +0000825 if (!th)
826 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000827
Jonathan Peyton30419822017-05-12 18:01:32 +0000828 KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000829
Jonathan Peyton30419822017-05-12 18:01:32 +0000830 if (TerminateThread(th->th.th_info.ds.ds_thread, (DWORD)-1) == FALSE) {
831 /* It's OK, the thread may have exited already */
832 }
833 __kmp_free_handle(th->th.th_info.ds.ds_thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000834}
835
Jonathan Peyton30419822017-05-12 18:01:32 +0000836void __kmp_clear_system_time(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000837 BOOL status;
Jonathan Peyton30419822017-05-12 18:01:32 +0000838 LARGE_INTEGER time;
839 status = QueryPerformanceCounter(&time);
840 __kmp_win32_time = (kmp_int64)time.QuadPart;
841}
Jim Cownie5e8470a2013-09-27 10:38:44 +0000842
Jonathan Peyton30419822017-05-12 18:01:32 +0000843void __kmp_initialize_system_tick(void) {
844 {
845 BOOL status;
846 LARGE_INTEGER freq;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000847
Jonathan Peyton30419822017-05-12 18:01:32 +0000848 status = QueryPerformanceFrequency(&freq);
849 if (!status) {
850 DWORD error = GetLastError();
851 __kmp_msg(kmp_ms_fatal,
852 KMP_MSG(FunctionError, "QueryPerformanceFrequency()"),
853 KMP_ERR(error), __kmp_msg_null);
854
855 } else {
856 __kmp_win32_tick = ((double)1.0) / (double)freq.QuadPart;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000857 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000858 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000859}
860
861/* Calculate the elapsed wall clock time for the user */
862
Jonathan Peyton30419822017-05-12 18:01:32 +0000863void __kmp_elapsed(double *t) {
864 BOOL status;
865 LARGE_INTEGER now;
866 status = QueryPerformanceCounter(&now);
867 *t = ((double)now.QuadPart) * __kmp_win32_tick;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000868}
869
870/* Calculate the elapsed wall clock tick for the user */
871
Jonathan Peyton30419822017-05-12 18:01:32 +0000872void __kmp_elapsed_tick(double *t) { *t = __kmp_win32_tick; }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000873
Jonathan Peyton30419822017-05-12 18:01:32 +0000874void __kmp_read_system_time(double *delta) {
875 if (delta != NULL) {
876 BOOL status;
877 LARGE_INTEGER now;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000878
Jonathan Peyton30419822017-05-12 18:01:32 +0000879 status = QueryPerformanceCounter(&now);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000880
Jonathan Peyton30419822017-05-12 18:01:32 +0000881 *delta = ((double)(((kmp_int64)now.QuadPart) - __kmp_win32_time)) *
882 __kmp_win32_tick;
883 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000884}
885
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000886/* Return the current time stamp in nsec */
Jonathan Peyton30419822017-05-12 18:01:32 +0000887kmp_uint64 __kmp_now_nsec() {
888 LARGE_INTEGER now;
889 QueryPerformanceCounter(&now);
890 return 1e9 * __kmp_win32_tick * now.QuadPart;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000891}
892
Jonathan Peyton30419822017-05-12 18:01:32 +0000893void *__stdcall __kmp_launch_worker(void *arg) {
894 volatile void *stack_data;
895 void *exit_val;
896 void *padding = 0;
897 kmp_info_t *this_thr = (kmp_info_t *)arg;
898 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000899
Jonathan Peyton30419822017-05-12 18:01:32 +0000900 gtid = this_thr->th.th_info.ds.ds_gtid;
901 __kmp_gtid_set_specific(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000902#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000903#error "This define causes problems with LoadLibrary() + declspec(thread) " \
Jim Cownie5e8470a2013-09-27 10:38:44 +0000904 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
905 "reference: http://support.microsoft.com/kb/118816"
Jonathan Peyton30419822017-05-12 18:01:32 +0000906//__kmp_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000907#endif
908
909#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000910 __kmp_itt_thread_name(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000911#endif /* USE_ITT_BUILD */
912
Jonathan Peyton30419822017-05-12 18:01:32 +0000913 __kmp_affinity_set_init_mask(gtid, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000914
915#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000916 // Set FP control regs to be a copy of the parallel initialization thread's.
917 __kmp_clear_x87_fpu_status_word();
918 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
919 __kmp_load_mxcsr(&__kmp_init_mxcsr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000920#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
921
Jonathan Peyton30419822017-05-12 18:01:32 +0000922 if (__kmp_stkoffset > 0 && gtid > 0) {
923 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
924 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000925
Jonathan Peyton30419822017-05-12 18:01:32 +0000926 KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
927 this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
928 TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000929
Jonathan Peyton30419822017-05-12 18:01:32 +0000930 if (TCR_4(__kmp_gtid_mode) <
931 2) { // check stack only if it is used to get gtid
932 TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
933 KMP_ASSERT(this_thr->th.th_info.ds.ds_stackgrow == FALSE);
934 __kmp_check_stack_overlap(this_thr);
935 }
936 KMP_MB();
937 exit_val = __kmp_launch_thread(this_thr);
938 KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
939 TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
940 KMP_MB();
941 return exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000942}
943
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000944#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000945/* The monitor thread controls all of the threads in the complex */
946
Jonathan Peyton30419822017-05-12 18:01:32 +0000947void *__stdcall __kmp_launch_monitor(void *arg) {
948 DWORD wait_status;
949 kmp_thread_t monitor;
950 int status;
951 int interval;
952 kmp_info_t *this_thr = (kmp_info_t *)arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000953
Jonathan Peyton30419822017-05-12 18:01:32 +0000954 KMP_DEBUG_ASSERT(__kmp_init_monitor);
955 TCW_4(__kmp_init_monitor, 2); // AC: Signal library that monitor has started
956 // TODO: hide "2" in enum (like {true,false,started})
957 this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
958 TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000959
Jonathan Peyton30419822017-05-12 18:01:32 +0000960 KMP_MB(); /* Flush all pending memory write invalidates. */
961 KA_TRACE(10, ("__kmp_launch_monitor: launched\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000962
Jonathan Peyton30419822017-05-12 18:01:32 +0000963 monitor = GetCurrentThread();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000964
Jonathan Peyton30419822017-05-12 18:01:32 +0000965 /* set thread priority */
966 status = SetThreadPriority(monitor, THREAD_PRIORITY_HIGHEST);
967 if (!status) {
968 DWORD error = GetLastError();
969 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetThreadPriority), KMP_ERR(error),
970 __kmp_msg_null);
971 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000972
Jonathan Peyton30419822017-05-12 18:01:32 +0000973 /* register us as monitor */
974 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000975#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000976#error "This define causes problems with LoadLibrary() + declspec(thread) " \
Jim Cownie5e8470a2013-09-27 10:38:44 +0000977 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
978 "reference: http://support.microsoft.com/kb/118816"
Jonathan Peyton30419822017-05-12 18:01:32 +0000979//__kmp_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000980#endif
981
982#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000983 __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore
984// monitor thread.
Jim Cownie5e8470a2013-09-27 10:38:44 +0000985#endif /* USE_ITT_BUILD */
986
Jonathan Peyton30419822017-05-12 18:01:32 +0000987 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000988
Jonathan Peyton30419822017-05-12 18:01:32 +0000989 interval = (1000 / __kmp_monitor_wakeups); /* in milliseconds */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000990
Jonathan Peyton30419822017-05-12 18:01:32 +0000991 while (!TCR_4(__kmp_global.g.g_done)) {
992 /* This thread monitors the state of the system */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000993
Jonathan Peyton30419822017-05-12 18:01:32 +0000994 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000995
Jonathan Peyton30419822017-05-12 18:01:32 +0000996 wait_status = WaitForSingleObject(__kmp_monitor_ev, interval);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000997
Jonathan Peyton30419822017-05-12 18:01:32 +0000998 if (wait_status == WAIT_TIMEOUT) {
999 TCW_4(__kmp_global.g.g_time.dt.t_value,
1000 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001001 }
1002
Jonathan Peyton30419822017-05-12 18:01:32 +00001003 KMP_MB(); /* Flush all pending memory write invalidates. */
1004 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001005
Jonathan Peyton30419822017-05-12 18:01:32 +00001006 KA_TRACE(10, ("__kmp_launch_monitor: finished\n"));
1007
1008 status = SetThreadPriority(monitor, THREAD_PRIORITY_NORMAL);
1009 if (!status) {
1010 DWORD error = GetLastError();
1011 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetThreadPriority), KMP_ERR(error),
1012 __kmp_msg_null);
1013 }
1014
1015 if (__kmp_global.g.g_abort != 0) {
1016 /* now we need to terminate the worker threads */
1017 /* the value of t_abort is the signal we caught */
1018 int gtid;
1019
1020 KA_TRACE(10, ("__kmp_launch_monitor: terminate sig=%d\n",
1021 (__kmp_global.g.g_abort)));
1022
1023 /* terminate the OpenMP worker threads */
1024 /* TODO this is not valid for sibling threads!!
1025 * the uber master might not be 0 anymore.. */
1026 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1027 __kmp_terminate_thread(gtid);
1028
1029 __kmp_cleanup();
1030
1031 Sleep(0);
1032
1033 KA_TRACE(10,
1034 ("__kmp_launch_monitor: raise sig=%d\n", __kmp_global.g.g_abort));
1035
1036 if (__kmp_global.g.g_abort > 0) {
1037 raise(__kmp_global.g.g_abort);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001038 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001039 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001040
Jonathan Peyton30419822017-05-12 18:01:32 +00001041 TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001042
Jonathan Peyton30419822017-05-12 18:01:32 +00001043 KMP_MB();
1044 return arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001045}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001046#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001047
Jonathan Peyton30419822017-05-12 18:01:32 +00001048void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
1049 kmp_thread_t handle;
1050 DWORD idThread;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001051
Jonathan Peyton30419822017-05-12 18:01:32 +00001052 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001053
Jonathan Peyton30419822017-05-12 18:01:32 +00001054 th->th.th_info.ds.ds_gtid = gtid;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001055
Jonathan Peyton30419822017-05-12 18:01:32 +00001056 if (KMP_UBER_GTID(gtid)) {
1057 int stack_data;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001058
Jonathan Peyton30419822017-05-12 18:01:32 +00001059 /* TODO: GetCurrentThread() returns a pseudo-handle that is unsuitable for
1060 other threads to use. Is it appropriate to just use GetCurrentThread?
1061 When should we close this handle? When unregistering the root? */
1062 {
1063 BOOL rc;
1064 rc = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
1065 GetCurrentProcess(), &th->th.th_info.ds.ds_thread, 0,
1066 FALSE, DUPLICATE_SAME_ACCESS);
1067 KMP_ASSERT(rc);
1068 KA_TRACE(10, (" __kmp_create_worker: ROOT Handle duplicated, th = %p, "
1069 "handle = %" KMP_UINTPTR_SPEC "\n",
1070 (LPVOID)th, th->th.th_info.ds.ds_thread));
1071 th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001072 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001073 if (TCR_4(__kmp_gtid_mode) < 2) { // check stack only if used to get gtid
1074 /* we will dynamically update the stack range if gtid_mode == 1 */
1075 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1076 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1077 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1078 __kmp_check_stack_overlap(th);
1079 }
1080 } else {
1081 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001082
Jonathan Peyton30419822017-05-12 18:01:32 +00001083 /* Set stack size for this thread now. */
1084 KA_TRACE(10,
1085 ("__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC " bytes\n",
1086 stack_size));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001087
Jonathan Peyton30419822017-05-12 18:01:32 +00001088 stack_size += gtid * __kmp_stkoffset;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001089
Jonathan Peyton30419822017-05-12 18:01:32 +00001090 TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1091 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001092
Jonathan Peyton30419822017-05-12 18:01:32 +00001093 KA_TRACE(10,
1094 ("__kmp_create_worker: (before) stack_size = %" KMP_SIZE_T_SPEC
1095 " bytes, &__kmp_launch_worker = %p, th = %p, &idThread = %p\n",
1096 (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1097 (LPVOID)th, &idThread));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001098
Jonathan Peyton30419822017-05-12 18:01:32 +00001099 handle = CreateThread(
1100 NULL, (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)__kmp_launch_worker,
1101 (LPVOID)th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001102
Jonathan Peyton30419822017-05-12 18:01:32 +00001103 KA_TRACE(10,
1104 ("__kmp_create_worker: (after) stack_size = %" KMP_SIZE_T_SPEC
1105 " bytes, &__kmp_launch_worker = %p, th = %p, "
1106 "idThread = %u, handle = %" KMP_UINTPTR_SPEC "\n",
1107 (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1108 (LPVOID)th, idThread, handle));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001109
Jonathan Peyton30419822017-05-12 18:01:32 +00001110 if (handle == 0) {
1111 DWORD error = GetLastError();
1112 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantCreateThread), KMP_ERR(error),
1113 __kmp_msg_null);
1114 } else {
1115 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001116 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001117
Jonathan Peyton30419822017-05-12 18:01:32 +00001118 KMP_MB(); /* Flush all pending memory write invalidates. */
1119 }
1120
1121 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001122}
1123
Jonathan Peyton30419822017-05-12 18:01:32 +00001124int __kmp_still_running(kmp_info_t *th) {
1125 return (WAIT_TIMEOUT == WaitForSingleObject(th->th.th_info.ds.ds_thread, 0));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001126}
1127
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001128#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001129void __kmp_create_monitor(kmp_info_t *th) {
1130 kmp_thread_t handle;
1131 DWORD idThread;
1132 int ideal, new_ideal;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001133
Jonathan Peyton30419822017-05-12 18:01:32 +00001134 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
1135 // We don't need monitor thread in case of MAX_BLOCKTIME
1136 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
1137 "MAX blocktime\n"));
1138 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
1139 th->th.th_info.ds.ds_gtid = 0;
1140 TCW_4(__kmp_init_monitor, 2); // Signal to stop waiting for monitor creation
1141 return;
1142 }
1143 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001144
Jonathan Peyton30419822017-05-12 18:01:32 +00001145 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001146
Jonathan Peyton30419822017-05-12 18:01:32 +00001147 __kmp_monitor_ev = CreateEvent(NULL, TRUE, FALSE, NULL);
1148 if (__kmp_monitor_ev == NULL) {
1149 DWORD error = GetLastError();
1150 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantCreateEvent), KMP_ERR(error),
1151 __kmp_msg_null);
1152 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001153#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001154 __kmp_itt_system_object_created(__kmp_monitor_ev, "Event");
Jim Cownie5e8470a2013-09-27 10:38:44 +00001155#endif /* USE_ITT_BUILD */
1156
Jonathan Peyton30419822017-05-12 18:01:32 +00001157 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1158 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001159
Jonathan Peyton30419822017-05-12 18:01:32 +00001160 // FIXME - on Windows* OS, if __kmp_monitor_stksize = 0, figure out how
1161 // to automatically expand stacksize based on CreateThread error code.
1162 if (__kmp_monitor_stksize == 0) {
1163 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1164 }
1165 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
1166 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1167 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001168
Jonathan Peyton30419822017-05-12 18:01:32 +00001169 KA_TRACE(10, ("__kmp_create_monitor: requested stacksize = %d bytes\n",
1170 (int)__kmp_monitor_stksize));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001171
Jonathan Peyton30419822017-05-12 18:01:32 +00001172 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001173
Jonathan Peyton30419822017-05-12 18:01:32 +00001174 handle =
1175 CreateThread(NULL, (SIZE_T)__kmp_monitor_stksize,
1176 (LPTHREAD_START_ROUTINE)__kmp_launch_monitor, (LPVOID)th,
1177 STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
1178 if (handle == 0) {
1179 DWORD error = GetLastError();
1180 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantCreateThread), KMP_ERR(error),
1181 __kmp_msg_null);
1182 } else
1183 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001184
Jonathan Peyton30419822017-05-12 18:01:32 +00001185 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001186
Jonathan Peyton30419822017-05-12 18:01:32 +00001187 KA_TRACE(10, ("__kmp_create_monitor: monitor created %p\n",
1188 (void *)th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001189}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001190#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001191
Jonathan Peyton30419822017-05-12 18:01:32 +00001192/* Check to see if thread is still alive.
1193 NOTE: The ExitProcess(code) system call causes all threads to Terminate
1194 with a exit_val = code. Because of this we can not rely on exit_val having
1195 any particular value. So this routine may return STILL_ALIVE in exit_val
1196 even after the thread is dead. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001197
Jonathan Peyton30419822017-05-12 18:01:32 +00001198int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val) {
1199 DWORD rc;
1200 rc = GetExitCodeThread(th->th.th_info.ds.ds_thread, exit_val);
1201 if (rc == 0) {
1202 DWORD error = GetLastError();
1203 __kmp_msg(kmp_ms_fatal, KMP_MSG(FunctionError, "GetExitCodeThread()"),
1204 KMP_ERR(error), __kmp_msg_null);
1205 }; // if
1206 return (*exit_val == STILL_ACTIVE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001207}
1208
Jonathan Peyton30419822017-05-12 18:01:32 +00001209void __kmp_exit_thread(int exit_status) {
1210 ExitThread(exit_status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001211} // __kmp_exit_thread
1212
Jonathan Peyton30419822017-05-12 18:01:32 +00001213// This is a common part for both __kmp_reap_worker() and __kmp_reap_monitor().
1214static void __kmp_reap_common(kmp_info_t *th) {
1215 DWORD exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001216
Jonathan Peyton30419822017-05-12 18:01:32 +00001217 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001218
Jonathan Peyton30419822017-05-12 18:01:32 +00001219 KA_TRACE(
1220 10, ("__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001221
Jonathan Peyton30419822017-05-12 18:01:32 +00001222 /* 2006-10-19:
1223 There are two opposite situations:
1224 1. Windows* OS keep thread alive after it resets ds_alive flag and
1225 exits from thread function. (For example, see C70770/Q394281 "unloading of
1226 dll based on OMP is very slow".)
1227 2. Windows* OS may kill thread before it resets ds_alive flag.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001228
Jonathan Peyton30419822017-05-12 18:01:32 +00001229 Right solution seems to be waiting for *either* thread termination *or*
1230 ds_alive resetting. */
1231 {
1232 // TODO: This code is very similar to KMP_WAIT_YIELD. Need to generalize
1233 // KMP_WAIT_YIELD to cover this usage also.
1234 void *obj = NULL;
1235 register kmp_uint32 spins;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001236#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001237 KMP_FSYNC_SPIN_INIT(obj, (void *)&th->th.th_info.ds.ds_alive);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001238#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00001239 KMP_INIT_YIELD(spins);
1240 do {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001241#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001242 KMP_FSYNC_SPIN_PREPARE(obj);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001243#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00001244 __kmp_is_thread_alive(th, &exit_val);
1245 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
1246 KMP_YIELD_SPIN(spins);
1247 } while (exit_val == STILL_ACTIVE && TCR_4(th->th.th_info.ds.ds_alive));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001248#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001249 if (exit_val == STILL_ACTIVE) {
1250 KMP_FSYNC_CANCEL(obj);
1251 } else {
1252 KMP_FSYNC_SPIN_ACQUIRED(obj);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001253 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +00001254#endif /* USE_ITT_BUILD */
1255 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001256
Jonathan Peyton30419822017-05-12 18:01:32 +00001257 __kmp_free_handle(th->th.th_info.ds.ds_thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001258
Jonathan Peyton30419822017-05-12 18:01:32 +00001259 /* NOTE: The ExitProcess(code) system call causes all threads to Terminate
1260 with a exit_val = code. Because of this we can not rely on exit_val having
1261 any particular value. */
1262 if (exit_val == STILL_ACTIVE) {
1263 KA_TRACE(1, ("__kmp_reap_common: thread still active.\n"));
1264 } else if ((void *)exit_val != (void *)th) {
1265 KA_TRACE(1, ("__kmp_reap_common: ExitProcess / TerminateThread used?\n"));
1266 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001267
Jonathan Peyton30419822017-05-12 18:01:32 +00001268 KA_TRACE(10,
1269 ("__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
1270 "\n",
1271 th->th.th_info.ds.ds_gtid, th->th.th_info.ds.ds_thread));
1272
1273 th->th.th_info.ds.ds_thread = 0;
1274 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1275 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1276 th->th.th_info.ds.ds_thread_id = 0;
1277
1278 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001279}
1280
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001281#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001282void __kmp_reap_monitor(kmp_info_t *th) {
1283 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001284
Jonathan Peyton30419822017-05-12 18:01:32 +00001285 KA_TRACE(10, ("__kmp_reap_monitor: try to reap %p\n",
1286 (void *)th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001287
Jonathan Peyton30419822017-05-12 18:01:32 +00001288 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1289 // If both tid and gtid are 0, it means the monitor did not ever start.
1290 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1291 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1292 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1293 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1294 return;
1295 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001296
Jonathan Peyton30419822017-05-12 18:01:32 +00001297 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001298
Jonathan Peyton30419822017-05-12 18:01:32 +00001299 status = SetEvent(__kmp_monitor_ev);
1300 if (status == FALSE) {
1301 DWORD error = GetLastError();
1302 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetEvent), KMP_ERR(error),
1303 __kmp_msg_null);
1304 }
1305 KA_TRACE(10, ("__kmp_reap_monitor: reaping thread (%d)\n",
1306 th->th.th_info.ds.ds_gtid));
1307 __kmp_reap_common(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001308
Jonathan Peyton30419822017-05-12 18:01:32 +00001309 __kmp_free_handle(__kmp_monitor_ev);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001310
Jonathan Peyton30419822017-05-12 18:01:32 +00001311 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001312}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001313#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001314
Jonathan Peyton30419822017-05-12 18:01:32 +00001315void __kmp_reap_worker(kmp_info_t *th) {
1316 KA_TRACE(10, ("__kmp_reap_worker: reaping thread (%d)\n",
1317 th->th.th_info.ds.ds_gtid));
1318 __kmp_reap_common(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001319}
1320
Jim Cownie5e8470a2013-09-27 10:38:44 +00001321#if KMP_HANDLE_SIGNALS
1322
Jonathan Peyton30419822017-05-12 18:01:32 +00001323static void __kmp_team_handler(int signo) {
1324 if (__kmp_global.g.g_abort == 0) {
1325 // Stage 1 signal handler, let's shut down all of the threads.
1326 if (__kmp_debug_buf) {
1327 __kmp_dump_debug_buffer();
1328 }; // if
1329 KMP_MB(); // Flush all pending memory write invalidates.
1330 TCW_4(__kmp_global.g.g_abort, signo);
1331 KMP_MB(); // Flush all pending memory write invalidates.
1332 TCW_4(__kmp_global.g.g_done, TRUE);
1333 KMP_MB(); // Flush all pending memory write invalidates.
1334 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001335} // __kmp_team_handler
1336
Jonathan Peyton30419822017-05-12 18:01:32 +00001337static sig_func_t __kmp_signal(int signum, sig_func_t handler) {
1338 sig_func_t old = signal(signum, handler);
1339 if (old == SIG_ERR) {
1340 int error = errno;
1341 __kmp_msg(kmp_ms_fatal, KMP_MSG(FunctionError, "signal"), KMP_ERR(error),
1342 __kmp_msg_null);
1343 }; // if
1344 return old;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001345}
1346
Jonathan Peyton30419822017-05-12 18:01:32 +00001347static void __kmp_install_one_handler(int sig, sig_func_t handler,
1348 int parallel_init) {
1349 sig_func_t old;
1350 KMP_MB(); /* Flush all pending memory write invalidates. */
1351 KB_TRACE(60, ("__kmp_install_one_handler: called: sig=%d\n", sig));
1352 if (parallel_init) {
1353 old = __kmp_signal(sig, handler);
1354 // SIG_DFL on Windows* OS in NULL or 0.
1355 if (old == __kmp_sighldrs[sig]) {
1356 __kmp_siginstalled[sig] = 1;
1357 } else { // Restore/keep user's handler if one previously installed.
1358 old = __kmp_signal(sig, old);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001359 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +00001360 } else {
1361 // Save initial/system signal handlers to see if user handlers installed.
1362 // 2009-09-23: It is a dead code. On Windows* OS __kmp_install_signals
1363 // called once with parallel_init == TRUE.
1364 old = __kmp_signal(sig, SIG_DFL);
1365 __kmp_sighldrs[sig] = old;
1366 __kmp_signal(sig, old);
1367 }; // if
1368 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001369} // __kmp_install_one_handler
1370
Jonathan Peyton30419822017-05-12 18:01:32 +00001371static void __kmp_remove_one_handler(int sig) {
1372 if (__kmp_siginstalled[sig]) {
1373 sig_func_t old;
1374 KMP_MB(); // Flush all pending memory write invalidates.
1375 KB_TRACE(60, ("__kmp_remove_one_handler: called: sig=%d\n", sig));
1376 old = __kmp_signal(sig, __kmp_sighldrs[sig]);
1377 if (old != __kmp_team_handler) {
1378 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1379 "restoring: sig=%d\n",
1380 sig));
1381 old = __kmp_signal(sig, old);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001382 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +00001383 __kmp_sighldrs[sig] = NULL;
1384 __kmp_siginstalled[sig] = 0;
1385 KMP_MB(); // Flush all pending memory write invalidates.
1386 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001387} // __kmp_remove_one_handler
1388
Jonathan Peyton30419822017-05-12 18:01:32 +00001389void __kmp_install_signals(int parallel_init) {
1390 KB_TRACE(10, ("__kmp_install_signals: called\n"));
1391 if (!__kmp_handle_signals) {
1392 KB_TRACE(10, ("__kmp_install_signals: KMP_HANDLE_SIGNALS is false - "
1393 "handlers not installed\n"));
1394 return;
1395 }; // if
1396 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1397 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1398 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1399 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1400 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1401 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001402} // __kmp_install_signals
1403
Jonathan Peyton30419822017-05-12 18:01:32 +00001404void __kmp_remove_signals(void) {
1405 int sig;
1406 KB_TRACE(10, ("__kmp_remove_signals: called\n"));
1407 for (sig = 1; sig < NSIG; ++sig) {
1408 __kmp_remove_one_handler(sig);
1409 }; // for sig
Jim Cownie5e8470a2013-09-27 10:38:44 +00001410} // __kmp_remove_signals
1411
Jim Cownie5e8470a2013-09-27 10:38:44 +00001412#endif // KMP_HANDLE_SIGNALS
1413
1414/* Put the thread to sleep for a time period */
Jonathan Peyton30419822017-05-12 18:01:32 +00001415void __kmp_thread_sleep(int millis) {
1416 DWORD status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001417
Jonathan Peyton30419822017-05-12 18:01:32 +00001418 status = SleepEx((DWORD)millis, FALSE);
1419 if (status) {
1420 DWORD error = GetLastError();
1421 __kmp_msg(kmp_ms_fatal, KMP_MSG(FunctionError, "SleepEx()"), KMP_ERR(error),
1422 __kmp_msg_null);
1423 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001424}
1425
Jonathan Peyton30419822017-05-12 18:01:32 +00001426// Determine whether the given address is mapped into the current address space.
1427int __kmp_is_address_mapped(void *addr) {
1428 DWORD status;
1429 MEMORY_BASIC_INFORMATION lpBuffer;
1430 SIZE_T dwLength;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001431
Jonathan Peyton30419822017-05-12 18:01:32 +00001432 dwLength = sizeof(MEMORY_BASIC_INFORMATION);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001433
Jonathan Peyton30419822017-05-12 18:01:32 +00001434 status = VirtualQuery(addr, &lpBuffer, dwLength);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001435
Jonathan Peyton30419822017-05-12 18:01:32 +00001436 return !(((lpBuffer.State == MEM_RESERVE) || (lpBuffer.State == MEM_FREE)) ||
1437 ((lpBuffer.Protect == PAGE_NOACCESS) ||
1438 (lpBuffer.Protect == PAGE_EXECUTE)));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001439}
1440
Jonathan Peyton30419822017-05-12 18:01:32 +00001441kmp_uint64 __kmp_hardware_timestamp(void) {
1442 kmp_uint64 r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001443
Jonathan Peyton30419822017-05-12 18:01:32 +00001444 QueryPerformanceCounter((LARGE_INTEGER *)&r);
1445 return r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001446}
1447
1448/* Free handle and check the error code */
Jonathan Peyton30419822017-05-12 18:01:32 +00001449void __kmp_free_handle(kmp_thread_t tHandle) {
1450 /* called with parameter type HANDLE also, thus suppose kmp_thread_t defined
1451 * as HANDLE */
1452 BOOL rc;
1453 rc = CloseHandle(tHandle);
1454 if (!rc) {
1455 DWORD error = GetLastError();
1456 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantCloseHandle), KMP_ERR(error),
1457 __kmp_msg_null);
1458 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001459}
1460
Jonathan Peyton30419822017-05-12 18:01:32 +00001461int __kmp_get_load_balance(int max) {
1462 static ULONG glb_buff_size = 100 * 1024;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001463
Jonathan Peyton30419822017-05-12 18:01:32 +00001464 // Saved count of the running threads for the thread balance algortihm
1465 static int glb_running_threads = 0;
1466 static double glb_call_time = 0; /* Thread balance algorithm call time */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001467
Jonathan Peyton30419822017-05-12 18:01:32 +00001468 int running_threads = 0; // Number of running threads in the system.
1469 NTSTATUS status = 0;
1470 ULONG buff_size = 0;
1471 ULONG info_size = 0;
1472 void *buffer = NULL;
1473 PSYSTEM_PROCESS_INFORMATION spi = NULL;
1474 int first_time = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001475
Jonathan Peyton30419822017-05-12 18:01:32 +00001476 double call_time = 0.0; // start, finish;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001477
Jonathan Peyton30419822017-05-12 18:01:32 +00001478 __kmp_elapsed(&call_time);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001479
Jonathan Peyton30419822017-05-12 18:01:32 +00001480 if (glb_call_time &&
1481 (call_time - glb_call_time < __kmp_load_balance_interval)) {
1482 running_threads = glb_running_threads;
1483 goto finish;
1484 }
1485 glb_call_time = call_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001486
Jonathan Peyton30419822017-05-12 18:01:32 +00001487 // Do not spend time on running algorithm if we have a permanent error.
1488 if (NtQuerySystemInformation == NULL) {
1489 running_threads = -1;
1490 goto finish;
1491 }; // if
1492
1493 if (max <= 0) {
1494 max = INT_MAX;
1495 }; // if
1496
1497 do {
1498
1499 if (first_time) {
1500 buff_size = glb_buff_size;
1501 } else {
1502 buff_size = 2 * buff_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001503 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001504
Jonathan Peyton30419822017-05-12 18:01:32 +00001505 buffer = KMP_INTERNAL_REALLOC(buffer, buff_size);
1506 if (buffer == NULL) {
1507 running_threads = -1;
1508 goto finish;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001509 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +00001510 status = NtQuerySystemInformation(SystemProcessInformation, buffer,
1511 buff_size, &info_size);
1512 first_time = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001513
Jonathan Peyton30419822017-05-12 18:01:32 +00001514 } while (status == STATUS_INFO_LENGTH_MISMATCH);
1515 glb_buff_size = buff_size;
1516
1517#define CHECK(cond) \
1518 { \
1519 KMP_DEBUG_ASSERT(cond); \
1520 if (!(cond)) { \
1521 running_threads = -1; \
1522 goto finish; \
1523 } \
1524 }
1525
1526 CHECK(buff_size >= info_size);
1527 spi = PSYSTEM_PROCESS_INFORMATION(buffer);
1528 for (;;) {
1529 ptrdiff_t offset = uintptr_t(spi) - uintptr_t(buffer);
1530 CHECK(0 <= offset &&
1531 offset + sizeof(SYSTEM_PROCESS_INFORMATION) < info_size);
1532 HANDLE pid = spi->ProcessId;
1533 ULONG num = spi->NumberOfThreads;
1534 CHECK(num >= 1);
1535 size_t spi_size =
1536 sizeof(SYSTEM_PROCESS_INFORMATION) + sizeof(SYSTEM_THREAD) * (num - 1);
1537 CHECK(offset + spi_size <
1538 info_size); // Make sure process info record fits the buffer.
1539 if (spi->NextEntryOffset != 0) {
1540 CHECK(spi_size <=
1541 spi->NextEntryOffset); // And do not overlap with the next record.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001542 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +00001543 // pid == 0 corresponds to the System Idle Process. It always has running
1544 // threads on all cores. So, we don't consider the running threads of this
1545 // process.
1546 if (pid != 0) {
1547 for (int i = 0; i < num; ++i) {
1548 THREAD_STATE state = spi->Threads[i].State;
1549 // Count threads that have Ready or Running state.
1550 // !!! TODO: Why comment does not match the code???
1551 if (state == StateRunning) {
1552 ++running_threads;
1553 // Stop counting running threads if the number is already greater than
1554 // the number of available cores
1555 if (running_threads >= max) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001556 goto finish;
Jonathan Peyton30419822017-05-12 18:01:32 +00001557 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001558 } // if
Jonathan Peyton30419822017-05-12 18:01:32 +00001559 }; // for i
1560 } // if
1561 if (spi->NextEntryOffset == 0) {
1562 break;
1563 }; // if
1564 spi = PSYSTEM_PROCESS_INFORMATION(uintptr_t(spi) + spi->NextEntryOffset);
1565 }; // forever
Jim Cownie5e8470a2013-09-27 10:38:44 +00001566
Jonathan Peyton30419822017-05-12 18:01:32 +00001567#undef CHECK
Jim Cownie5e8470a2013-09-27 10:38:44 +00001568
Jonathan Peyton30419822017-05-12 18:01:32 +00001569finish: // Clean up and exit.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001570
Jonathan Peyton30419822017-05-12 18:01:32 +00001571 if (buffer != NULL) {
1572 KMP_INTERNAL_FREE(buffer);
1573 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001574
Jonathan Peyton30419822017-05-12 18:01:32 +00001575 glb_running_threads = running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001576
Jonathan Peyton30419822017-05-12 18:01:32 +00001577 return running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001578} //__kmp_get_load_balance()