blob: f13ff2925a2e7c86a12a1d5a6ad21aabc295dcfd [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;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000595 }
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);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000621 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000622#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);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000665 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000666 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();
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000851 __kmp_fatal(KMP_MSG(FunctionError, "QueryPerformanceFrequency()"),
852 KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +0000853
854 } else {
855 __kmp_win32_tick = ((double)1.0) / (double)freq.QuadPart;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000856 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000857 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000858}
859
860/* Calculate the elapsed wall clock time for the user */
861
Jonathan Peyton30419822017-05-12 18:01:32 +0000862void __kmp_elapsed(double *t) {
863 BOOL status;
864 LARGE_INTEGER now;
865 status = QueryPerformanceCounter(&now);
866 *t = ((double)now.QuadPart) * __kmp_win32_tick;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000867}
868
869/* Calculate the elapsed wall clock tick for the user */
870
Jonathan Peyton30419822017-05-12 18:01:32 +0000871void __kmp_elapsed_tick(double *t) { *t = __kmp_win32_tick; }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000872
Jonathan Peyton30419822017-05-12 18:01:32 +0000873void __kmp_read_system_time(double *delta) {
874 if (delta != NULL) {
875 BOOL status;
876 LARGE_INTEGER now;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000877
Jonathan Peyton30419822017-05-12 18:01:32 +0000878 status = QueryPerformanceCounter(&now);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000879
Jonathan Peyton30419822017-05-12 18:01:32 +0000880 *delta = ((double)(((kmp_int64)now.QuadPart) - __kmp_win32_time)) *
881 __kmp_win32_tick;
882 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000883}
884
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000885/* Return the current time stamp in nsec */
Jonathan Peyton30419822017-05-12 18:01:32 +0000886kmp_uint64 __kmp_now_nsec() {
887 LARGE_INTEGER now;
888 QueryPerformanceCounter(&now);
889 return 1e9 * __kmp_win32_tick * now.QuadPart;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000890}
891
Jonathan Peyton30419822017-05-12 18:01:32 +0000892void *__stdcall __kmp_launch_worker(void *arg) {
893 volatile void *stack_data;
894 void *exit_val;
895 void *padding = 0;
896 kmp_info_t *this_thr = (kmp_info_t *)arg;
897 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000898
Jonathan Peyton30419822017-05-12 18:01:32 +0000899 gtid = this_thr->th.th_info.ds.ds_gtid;
900 __kmp_gtid_set_specific(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000901#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000902#error "This define causes problems with LoadLibrary() + declspec(thread) " \
Jim Cownie5e8470a2013-09-27 10:38:44 +0000903 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
904 "reference: http://support.microsoft.com/kb/118816"
Jonathan Peyton30419822017-05-12 18:01:32 +0000905//__kmp_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000906#endif
907
908#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000909 __kmp_itt_thread_name(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000910#endif /* USE_ITT_BUILD */
911
Jonathan Peyton30419822017-05-12 18:01:32 +0000912 __kmp_affinity_set_init_mask(gtid, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000913
914#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000915 // Set FP control regs to be a copy of the parallel initialization thread's.
916 __kmp_clear_x87_fpu_status_word();
917 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
918 __kmp_load_mxcsr(&__kmp_init_mxcsr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000919#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
920
Jonathan Peyton30419822017-05-12 18:01:32 +0000921 if (__kmp_stkoffset > 0 && gtid > 0) {
922 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
923 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000924
Jonathan Peyton30419822017-05-12 18:01:32 +0000925 KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
926 this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
927 TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000928
Jonathan Peyton30419822017-05-12 18:01:32 +0000929 if (TCR_4(__kmp_gtid_mode) <
930 2) { // check stack only if it is used to get gtid
931 TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
932 KMP_ASSERT(this_thr->th.th_info.ds.ds_stackgrow == FALSE);
933 __kmp_check_stack_overlap(this_thr);
934 }
935 KMP_MB();
936 exit_val = __kmp_launch_thread(this_thr);
937 KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
938 TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
939 KMP_MB();
940 return exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000941}
942
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000943#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000944/* The monitor thread controls all of the threads in the complex */
945
Jonathan Peyton30419822017-05-12 18:01:32 +0000946void *__stdcall __kmp_launch_monitor(void *arg) {
947 DWORD wait_status;
948 kmp_thread_t monitor;
949 int status;
950 int interval;
951 kmp_info_t *this_thr = (kmp_info_t *)arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000952
Jonathan Peyton30419822017-05-12 18:01:32 +0000953 KMP_DEBUG_ASSERT(__kmp_init_monitor);
954 TCW_4(__kmp_init_monitor, 2); // AC: Signal library that monitor has started
955 // TODO: hide "2" in enum (like {true,false,started})
956 this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
957 TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000958
Jonathan Peyton30419822017-05-12 18:01:32 +0000959 KMP_MB(); /* Flush all pending memory write invalidates. */
960 KA_TRACE(10, ("__kmp_launch_monitor: launched\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000961
Jonathan Peyton30419822017-05-12 18:01:32 +0000962 monitor = GetCurrentThread();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000963
Jonathan Peyton30419822017-05-12 18:01:32 +0000964 /* set thread priority */
965 status = SetThreadPriority(monitor, THREAD_PRIORITY_HIGHEST);
966 if (!status) {
967 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000968 __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +0000969 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000970
Jonathan Peyton30419822017-05-12 18:01:32 +0000971 /* register us as monitor */
972 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000973#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000974#error "This define causes problems with LoadLibrary() + declspec(thread) " \
Jim Cownie5e8470a2013-09-27 10:38:44 +0000975 "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
976 "reference: http://support.microsoft.com/kb/118816"
Jonathan Peyton30419822017-05-12 18:01:32 +0000977//__kmp_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000978#endif
979
980#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000981 __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore
982// monitor thread.
Jim Cownie5e8470a2013-09-27 10:38:44 +0000983#endif /* USE_ITT_BUILD */
984
Jonathan Peyton30419822017-05-12 18:01:32 +0000985 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000986
Jonathan Peyton30419822017-05-12 18:01:32 +0000987 interval = (1000 / __kmp_monitor_wakeups); /* in milliseconds */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000988
Jonathan Peyton30419822017-05-12 18:01:32 +0000989 while (!TCR_4(__kmp_global.g.g_done)) {
990 /* This thread monitors the state of the system */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000991
Jonathan Peyton30419822017-05-12 18:01:32 +0000992 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000993
Jonathan Peyton30419822017-05-12 18:01:32 +0000994 wait_status = WaitForSingleObject(__kmp_monitor_ev, interval);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000995
Jonathan Peyton30419822017-05-12 18:01:32 +0000996 if (wait_status == WAIT_TIMEOUT) {
997 TCW_4(__kmp_global.g.g_time.dt.t_value,
998 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000999 }
1000
Jonathan Peyton30419822017-05-12 18:01:32 +00001001 KMP_MB(); /* Flush all pending memory write invalidates. */
1002 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001003
Jonathan Peyton30419822017-05-12 18:01:32 +00001004 KA_TRACE(10, ("__kmp_launch_monitor: finished\n"));
1005
1006 status = SetThreadPriority(monitor, THREAD_PRIORITY_NORMAL);
1007 if (!status) {
1008 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001009 __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001010 }
1011
1012 if (__kmp_global.g.g_abort != 0) {
1013 /* now we need to terminate the worker threads */
1014 /* the value of t_abort is the signal we caught */
1015 int gtid;
1016
1017 KA_TRACE(10, ("__kmp_launch_monitor: terminate sig=%d\n",
1018 (__kmp_global.g.g_abort)));
1019
1020 /* terminate the OpenMP worker threads */
1021 /* TODO this is not valid for sibling threads!!
1022 * the uber master might not be 0 anymore.. */
1023 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
1024 __kmp_terminate_thread(gtid);
1025
1026 __kmp_cleanup();
1027
1028 Sleep(0);
1029
1030 KA_TRACE(10,
1031 ("__kmp_launch_monitor: raise sig=%d\n", __kmp_global.g.g_abort));
1032
1033 if (__kmp_global.g.g_abort > 0) {
1034 raise(__kmp_global.g.g_abort);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001035 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001036 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001037
Jonathan Peyton30419822017-05-12 18:01:32 +00001038 TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001039
Jonathan Peyton30419822017-05-12 18:01:32 +00001040 KMP_MB();
1041 return arg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001042}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001043#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001044
Jonathan Peyton30419822017-05-12 18:01:32 +00001045void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
1046 kmp_thread_t handle;
1047 DWORD idThread;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001048
Jonathan Peyton30419822017-05-12 18:01:32 +00001049 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001050
Jonathan Peyton30419822017-05-12 18:01:32 +00001051 th->th.th_info.ds.ds_gtid = gtid;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001052
Jonathan Peyton30419822017-05-12 18:01:32 +00001053 if (KMP_UBER_GTID(gtid)) {
1054 int stack_data;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001055
Jonathan Peyton30419822017-05-12 18:01:32 +00001056 /* TODO: GetCurrentThread() returns a pseudo-handle that is unsuitable for
1057 other threads to use. Is it appropriate to just use GetCurrentThread?
1058 When should we close this handle? When unregistering the root? */
1059 {
1060 BOOL rc;
1061 rc = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
1062 GetCurrentProcess(), &th->th.th_info.ds.ds_thread, 0,
1063 FALSE, DUPLICATE_SAME_ACCESS);
1064 KMP_ASSERT(rc);
1065 KA_TRACE(10, (" __kmp_create_worker: ROOT Handle duplicated, th = %p, "
1066 "handle = %" KMP_UINTPTR_SPEC "\n",
1067 (LPVOID)th, th->th.th_info.ds.ds_thread));
1068 th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001069 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001070 if (TCR_4(__kmp_gtid_mode) < 2) { // check stack only if used to get gtid
1071 /* we will dynamically update the stack range if gtid_mode == 1 */
1072 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
1073 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
1074 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
1075 __kmp_check_stack_overlap(th);
1076 }
1077 } else {
1078 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001079
Jonathan Peyton30419822017-05-12 18:01:32 +00001080 /* Set stack size for this thread now. */
1081 KA_TRACE(10,
1082 ("__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC " bytes\n",
1083 stack_size));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001084
Jonathan Peyton30419822017-05-12 18:01:32 +00001085 stack_size += gtid * __kmp_stkoffset;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001086
Jonathan Peyton30419822017-05-12 18:01:32 +00001087 TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
1088 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001089
Jonathan Peyton30419822017-05-12 18:01:32 +00001090 KA_TRACE(10,
1091 ("__kmp_create_worker: (before) stack_size = %" KMP_SIZE_T_SPEC
1092 " bytes, &__kmp_launch_worker = %p, th = %p, &idThread = %p\n",
1093 (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1094 (LPVOID)th, &idThread));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001095
Jonathan Peyton30419822017-05-12 18:01:32 +00001096 handle = CreateThread(
1097 NULL, (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)__kmp_launch_worker,
1098 (LPVOID)th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001099
Jonathan Peyton30419822017-05-12 18:01:32 +00001100 KA_TRACE(10,
1101 ("__kmp_create_worker: (after) stack_size = %" KMP_SIZE_T_SPEC
1102 " bytes, &__kmp_launch_worker = %p, th = %p, "
1103 "idThread = %u, handle = %" KMP_UINTPTR_SPEC "\n",
1104 (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
1105 (LPVOID)th, idThread, handle));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001106
Jonathan Peyton30419822017-05-12 18:01:32 +00001107 if (handle == 0) {
1108 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001109 __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001110 } else {
1111 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001112 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001113
Jonathan Peyton30419822017-05-12 18:01:32 +00001114 KMP_MB(); /* Flush all pending memory write invalidates. */
1115 }
1116
1117 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001118}
1119
Jonathan Peyton30419822017-05-12 18:01:32 +00001120int __kmp_still_running(kmp_info_t *th) {
1121 return (WAIT_TIMEOUT == WaitForSingleObject(th->th.th_info.ds.ds_thread, 0));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001122}
1123
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001124#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001125void __kmp_create_monitor(kmp_info_t *th) {
1126 kmp_thread_t handle;
1127 DWORD idThread;
1128 int ideal, new_ideal;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001129
Jonathan Peyton30419822017-05-12 18:01:32 +00001130 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
1131 // We don't need monitor thread in case of MAX_BLOCKTIME
1132 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
1133 "MAX blocktime\n"));
1134 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
1135 th->th.th_info.ds.ds_gtid = 0;
1136 TCW_4(__kmp_init_monitor, 2); // Signal to stop waiting for monitor creation
1137 return;
1138 }
1139 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001140
Jonathan Peyton30419822017-05-12 18:01:32 +00001141 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001142
Jonathan Peyton30419822017-05-12 18:01:32 +00001143 __kmp_monitor_ev = CreateEvent(NULL, TRUE, FALSE, NULL);
1144 if (__kmp_monitor_ev == NULL) {
1145 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001146 __kmp_fatal(KMP_MSG(CantCreateEvent), KMP_ERR(error), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001147 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001148#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001149 __kmp_itt_system_object_created(__kmp_monitor_ev, "Event");
Jim Cownie5e8470a2013-09-27 10:38:44 +00001150#endif /* USE_ITT_BUILD */
1151
Jonathan Peyton30419822017-05-12 18:01:32 +00001152 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1153 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001154
Jonathan Peyton30419822017-05-12 18:01:32 +00001155 // FIXME - on Windows* OS, if __kmp_monitor_stksize = 0, figure out how
1156 // to automatically expand stacksize based on CreateThread error code.
1157 if (__kmp_monitor_stksize == 0) {
1158 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1159 }
1160 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
1161 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1162 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001163
Jonathan Peyton30419822017-05-12 18:01:32 +00001164 KA_TRACE(10, ("__kmp_create_monitor: requested stacksize = %d bytes\n",
1165 (int)__kmp_monitor_stksize));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001166
Jonathan Peyton30419822017-05-12 18:01:32 +00001167 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001168
Jonathan Peyton30419822017-05-12 18:01:32 +00001169 handle =
1170 CreateThread(NULL, (SIZE_T)__kmp_monitor_stksize,
1171 (LPTHREAD_START_ROUTINE)__kmp_launch_monitor, (LPVOID)th,
1172 STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
1173 if (handle == 0) {
1174 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001175 __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001176 } else
1177 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001178
Jonathan Peyton30419822017-05-12 18:01:32 +00001179 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001180
Jonathan Peyton30419822017-05-12 18:01:32 +00001181 KA_TRACE(10, ("__kmp_create_monitor: monitor created %p\n",
1182 (void *)th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001183}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001184#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001185
Jonathan Peyton30419822017-05-12 18:01:32 +00001186/* Check to see if thread is still alive.
1187 NOTE: The ExitProcess(code) system call causes all threads to Terminate
1188 with a exit_val = code. Because of this we can not rely on exit_val having
1189 any particular value. So this routine may return STILL_ALIVE in exit_val
1190 even after the thread is dead. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001191
Jonathan Peyton30419822017-05-12 18:01:32 +00001192int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val) {
1193 DWORD rc;
1194 rc = GetExitCodeThread(th->th.th_info.ds.ds_thread, exit_val);
1195 if (rc == 0) {
1196 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001197 __kmp_fatal(KMP_MSG(FunctionError, "GetExitCodeThread()"), KMP_ERR(error),
1198 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001199 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001200 return (*exit_val == STILL_ACTIVE);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001201}
1202
Jonathan Peyton30419822017-05-12 18:01:32 +00001203void __kmp_exit_thread(int exit_status) {
1204 ExitThread(exit_status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001205} // __kmp_exit_thread
1206
Jonathan Peyton30419822017-05-12 18:01:32 +00001207// This is a common part for both __kmp_reap_worker() and __kmp_reap_monitor().
1208static void __kmp_reap_common(kmp_info_t *th) {
1209 DWORD exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001210
Jonathan Peyton30419822017-05-12 18:01:32 +00001211 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001212
Jonathan Peyton30419822017-05-12 18:01:32 +00001213 KA_TRACE(
1214 10, ("__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001215
Jonathan Peyton30419822017-05-12 18:01:32 +00001216 /* 2006-10-19:
1217 There are two opposite situations:
1218 1. Windows* OS keep thread alive after it resets ds_alive flag and
1219 exits from thread function. (For example, see C70770/Q394281 "unloading of
1220 dll based on OMP is very slow".)
1221 2. Windows* OS may kill thread before it resets ds_alive flag.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001222
Jonathan Peyton30419822017-05-12 18:01:32 +00001223 Right solution seems to be waiting for *either* thread termination *or*
1224 ds_alive resetting. */
1225 {
1226 // TODO: This code is very similar to KMP_WAIT_YIELD. Need to generalize
1227 // KMP_WAIT_YIELD to cover this usage also.
1228 void *obj = NULL;
Ed Maste414544c2017-07-07 21:06:05 +00001229 kmp_uint32 spins;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001230#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001231 KMP_FSYNC_SPIN_INIT(obj, (void *)&th->th.th_info.ds.ds_alive);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001232#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00001233 KMP_INIT_YIELD(spins);
1234 do {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001235#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001236 KMP_FSYNC_SPIN_PREPARE(obj);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001237#endif /* USE_ITT_BUILD */
Jonathan Peyton30419822017-05-12 18:01:32 +00001238 __kmp_is_thread_alive(th, &exit_val);
1239 KMP_YIELD(TCR_4(__kmp_nth) > __kmp_avail_proc);
1240 KMP_YIELD_SPIN(spins);
1241 } while (exit_val == STILL_ACTIVE && TCR_4(th->th.th_info.ds.ds_alive));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001242#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001243 if (exit_val == STILL_ACTIVE) {
1244 KMP_FSYNC_CANCEL(obj);
1245 } else {
1246 KMP_FSYNC_SPIN_ACQUIRED(obj);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001247 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001248#endif /* USE_ITT_BUILD */
1249 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001250
Jonathan Peyton30419822017-05-12 18:01:32 +00001251 __kmp_free_handle(th->th.th_info.ds.ds_thread);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001252
Jonathan Peyton30419822017-05-12 18:01:32 +00001253 /* NOTE: The ExitProcess(code) system call causes all threads to Terminate
1254 with a exit_val = code. Because of this we can not rely on exit_val having
1255 any particular value. */
1256 if (exit_val == STILL_ACTIVE) {
1257 KA_TRACE(1, ("__kmp_reap_common: thread still active.\n"));
1258 } else if ((void *)exit_val != (void *)th) {
1259 KA_TRACE(1, ("__kmp_reap_common: ExitProcess / TerminateThread used?\n"));
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001260 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001261
Jonathan Peyton30419822017-05-12 18:01:32 +00001262 KA_TRACE(10,
1263 ("__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
1264 "\n",
1265 th->th.th_info.ds.ds_gtid, th->th.th_info.ds.ds_thread));
1266
1267 th->th.th_info.ds.ds_thread = 0;
1268 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1269 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1270 th->th.th_info.ds.ds_thread_id = 0;
1271
1272 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001273}
1274
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001275#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001276void __kmp_reap_monitor(kmp_info_t *th) {
1277 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001278
Jonathan Peyton30419822017-05-12 18:01:32 +00001279 KA_TRACE(10, ("__kmp_reap_monitor: try to reap %p\n",
1280 (void *)th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001281
Jonathan Peyton30419822017-05-12 18:01:32 +00001282 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1283 // If both tid and gtid are 0, it means the monitor did not ever start.
1284 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1285 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1286 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1287 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1288 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001289 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001290
Jonathan Peyton30419822017-05-12 18:01:32 +00001291 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001292
Jonathan Peyton30419822017-05-12 18:01:32 +00001293 status = SetEvent(__kmp_monitor_ev);
1294 if (status == FALSE) {
1295 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001296 __kmp_fatal(KMP_MSG(CantSetEvent), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001297 }
1298 KA_TRACE(10, ("__kmp_reap_monitor: reaping thread (%d)\n",
1299 th->th.th_info.ds.ds_gtid));
1300 __kmp_reap_common(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001301
Jonathan Peyton30419822017-05-12 18:01:32 +00001302 __kmp_free_handle(__kmp_monitor_ev);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001303
Jonathan Peyton30419822017-05-12 18:01:32 +00001304 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001305}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001306#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001307
Jonathan Peyton30419822017-05-12 18:01:32 +00001308void __kmp_reap_worker(kmp_info_t *th) {
1309 KA_TRACE(10, ("__kmp_reap_worker: reaping thread (%d)\n",
1310 th->th.th_info.ds.ds_gtid));
1311 __kmp_reap_common(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001312}
1313
Jim Cownie5e8470a2013-09-27 10:38:44 +00001314#if KMP_HANDLE_SIGNALS
1315
Jonathan Peyton30419822017-05-12 18:01:32 +00001316static void __kmp_team_handler(int signo) {
1317 if (__kmp_global.g.g_abort == 0) {
1318 // Stage 1 signal handler, let's shut down all of the threads.
1319 if (__kmp_debug_buf) {
1320 __kmp_dump_debug_buffer();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001321 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001322 KMP_MB(); // Flush all pending memory write invalidates.
1323 TCW_4(__kmp_global.g.g_abort, signo);
1324 KMP_MB(); // Flush all pending memory write invalidates.
1325 TCW_4(__kmp_global.g.g_done, TRUE);
1326 KMP_MB(); // Flush all pending memory write invalidates.
1327 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001328} // __kmp_team_handler
1329
Jonathan Peyton30419822017-05-12 18:01:32 +00001330static sig_func_t __kmp_signal(int signum, sig_func_t handler) {
1331 sig_func_t old = signal(signum, handler);
1332 if (old == SIG_ERR) {
1333 int error = errno;
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001334 __kmp_fatal(KMP_MSG(FunctionError, "signal"), KMP_ERR(error),
1335 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001336 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001337 return old;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001338}
1339
Jonathan Peyton30419822017-05-12 18:01:32 +00001340static void __kmp_install_one_handler(int sig, sig_func_t handler,
1341 int parallel_init) {
1342 sig_func_t old;
1343 KMP_MB(); /* Flush all pending memory write invalidates. */
1344 KB_TRACE(60, ("__kmp_install_one_handler: called: sig=%d\n", sig));
1345 if (parallel_init) {
1346 old = __kmp_signal(sig, handler);
1347 // SIG_DFL on Windows* OS in NULL or 0.
1348 if (old == __kmp_sighldrs[sig]) {
1349 __kmp_siginstalled[sig] = 1;
1350 } else { // Restore/keep user's handler if one previously installed.
1351 old = __kmp_signal(sig, old);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001352 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001353 } else {
1354 // Save initial/system signal handlers to see if user handlers installed.
1355 // 2009-09-23: It is a dead code. On Windows* OS __kmp_install_signals
1356 // called once with parallel_init == TRUE.
1357 old = __kmp_signal(sig, SIG_DFL);
1358 __kmp_sighldrs[sig] = old;
1359 __kmp_signal(sig, old);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001360 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001361 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001362} // __kmp_install_one_handler
1363
Jonathan Peyton30419822017-05-12 18:01:32 +00001364static void __kmp_remove_one_handler(int sig) {
1365 if (__kmp_siginstalled[sig]) {
1366 sig_func_t old;
1367 KMP_MB(); // Flush all pending memory write invalidates.
1368 KB_TRACE(60, ("__kmp_remove_one_handler: called: sig=%d\n", sig));
1369 old = __kmp_signal(sig, __kmp_sighldrs[sig]);
1370 if (old != __kmp_team_handler) {
1371 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1372 "restoring: sig=%d\n",
1373 sig));
1374 old = __kmp_signal(sig, old);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001375 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001376 __kmp_sighldrs[sig] = NULL;
1377 __kmp_siginstalled[sig] = 0;
1378 KMP_MB(); // Flush all pending memory write invalidates.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001379 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001380} // __kmp_remove_one_handler
1381
Jonathan Peyton30419822017-05-12 18:01:32 +00001382void __kmp_install_signals(int parallel_init) {
1383 KB_TRACE(10, ("__kmp_install_signals: called\n"));
1384 if (!__kmp_handle_signals) {
1385 KB_TRACE(10, ("__kmp_install_signals: KMP_HANDLE_SIGNALS is false - "
1386 "handlers not installed\n"));
1387 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001388 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001389 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1390 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1391 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1392 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1393 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1394 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001395} // __kmp_install_signals
1396
Jonathan Peyton30419822017-05-12 18:01:32 +00001397void __kmp_remove_signals(void) {
1398 int sig;
1399 KB_TRACE(10, ("__kmp_remove_signals: called\n"));
1400 for (sig = 1; sig < NSIG; ++sig) {
1401 __kmp_remove_one_handler(sig);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001402 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001403} // __kmp_remove_signals
1404
Jim Cownie5e8470a2013-09-27 10:38:44 +00001405#endif // KMP_HANDLE_SIGNALS
1406
1407/* Put the thread to sleep for a time period */
Jonathan Peyton30419822017-05-12 18:01:32 +00001408void __kmp_thread_sleep(int millis) {
1409 DWORD status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001410
Jonathan Peyton30419822017-05-12 18:01:32 +00001411 status = SleepEx((DWORD)millis, FALSE);
1412 if (status) {
1413 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001414 __kmp_fatal(KMP_MSG(FunctionError, "SleepEx()"), KMP_ERR(error),
1415 __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001416 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001417}
1418
Jonathan Peyton30419822017-05-12 18:01:32 +00001419// Determine whether the given address is mapped into the current address space.
1420int __kmp_is_address_mapped(void *addr) {
1421 DWORD status;
1422 MEMORY_BASIC_INFORMATION lpBuffer;
1423 SIZE_T dwLength;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001424
Jonathan Peyton30419822017-05-12 18:01:32 +00001425 dwLength = sizeof(MEMORY_BASIC_INFORMATION);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001426
Jonathan Peyton30419822017-05-12 18:01:32 +00001427 status = VirtualQuery(addr, &lpBuffer, dwLength);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001428
Jonathan Peyton30419822017-05-12 18:01:32 +00001429 return !(((lpBuffer.State == MEM_RESERVE) || (lpBuffer.State == MEM_FREE)) ||
1430 ((lpBuffer.Protect == PAGE_NOACCESS) ||
1431 (lpBuffer.Protect == PAGE_EXECUTE)));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001432}
1433
Jonathan Peyton30419822017-05-12 18:01:32 +00001434kmp_uint64 __kmp_hardware_timestamp(void) {
1435 kmp_uint64 r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001436
Jonathan Peyton30419822017-05-12 18:01:32 +00001437 QueryPerformanceCounter((LARGE_INTEGER *)&r);
1438 return r;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001439}
1440
1441/* Free handle and check the error code */
Jonathan Peyton30419822017-05-12 18:01:32 +00001442void __kmp_free_handle(kmp_thread_t tHandle) {
1443 /* called with parameter type HANDLE also, thus suppose kmp_thread_t defined
1444 * as HANDLE */
1445 BOOL rc;
1446 rc = CloseHandle(tHandle);
1447 if (!rc) {
1448 DWORD error = GetLastError();
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001449 __kmp_fatal(KMP_MSG(CantCloseHandle), KMP_ERR(error), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001450 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001451}
1452
Jonathan Peyton30419822017-05-12 18:01:32 +00001453int __kmp_get_load_balance(int max) {
1454 static ULONG glb_buff_size = 100 * 1024;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001455
Jonathan Peyton30419822017-05-12 18:01:32 +00001456 // Saved count of the running threads for the thread balance algortihm
1457 static int glb_running_threads = 0;
1458 static double glb_call_time = 0; /* Thread balance algorithm call time */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001459
Jonathan Peyton30419822017-05-12 18:01:32 +00001460 int running_threads = 0; // Number of running threads in the system.
1461 NTSTATUS status = 0;
1462 ULONG buff_size = 0;
1463 ULONG info_size = 0;
1464 void *buffer = NULL;
1465 PSYSTEM_PROCESS_INFORMATION spi = NULL;
1466 int first_time = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001467
Jonathan Peyton30419822017-05-12 18:01:32 +00001468 double call_time = 0.0; // start, finish;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001469
Jonathan Peyton30419822017-05-12 18:01:32 +00001470 __kmp_elapsed(&call_time);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001471
Jonathan Peyton30419822017-05-12 18:01:32 +00001472 if (glb_call_time &&
1473 (call_time - glb_call_time < __kmp_load_balance_interval)) {
1474 running_threads = glb_running_threads;
1475 goto finish;
1476 }
1477 glb_call_time = call_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001478
Jonathan Peyton30419822017-05-12 18:01:32 +00001479 // Do not spend time on running algorithm if we have a permanent error.
1480 if (NtQuerySystemInformation == NULL) {
1481 running_threads = -1;
1482 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001483 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001484
1485 if (max <= 0) {
1486 max = INT_MAX;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001487 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001488
1489 do {
1490
1491 if (first_time) {
1492 buff_size = glb_buff_size;
1493 } else {
1494 buff_size = 2 * buff_size;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001495 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001496
Jonathan Peyton30419822017-05-12 18:01:32 +00001497 buffer = KMP_INTERNAL_REALLOC(buffer, buff_size);
1498 if (buffer == NULL) {
1499 running_threads = -1;
1500 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001501 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001502 status = NtQuerySystemInformation(SystemProcessInformation, buffer,
1503 buff_size, &info_size);
1504 first_time = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001505
Jonathan Peyton30419822017-05-12 18:01:32 +00001506 } while (status == STATUS_INFO_LENGTH_MISMATCH);
1507 glb_buff_size = buff_size;
1508
1509#define CHECK(cond) \
1510 { \
1511 KMP_DEBUG_ASSERT(cond); \
1512 if (!(cond)) { \
1513 running_threads = -1; \
1514 goto finish; \
1515 } \
1516 }
1517
1518 CHECK(buff_size >= info_size);
1519 spi = PSYSTEM_PROCESS_INFORMATION(buffer);
1520 for (;;) {
1521 ptrdiff_t offset = uintptr_t(spi) - uintptr_t(buffer);
1522 CHECK(0 <= offset &&
1523 offset + sizeof(SYSTEM_PROCESS_INFORMATION) < info_size);
1524 HANDLE pid = spi->ProcessId;
1525 ULONG num = spi->NumberOfThreads;
1526 CHECK(num >= 1);
1527 size_t spi_size =
1528 sizeof(SYSTEM_PROCESS_INFORMATION) + sizeof(SYSTEM_THREAD) * (num - 1);
1529 CHECK(offset + spi_size <
1530 info_size); // Make sure process info record fits the buffer.
1531 if (spi->NextEntryOffset != 0) {
1532 CHECK(spi_size <=
1533 spi->NextEntryOffset); // And do not overlap with the next record.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001534 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001535 // pid == 0 corresponds to the System Idle Process. It always has running
1536 // threads on all cores. So, we don't consider the running threads of this
1537 // process.
1538 if (pid != 0) {
1539 for (int i = 0; i < num; ++i) {
1540 THREAD_STATE state = spi->Threads[i].State;
1541 // Count threads that have Ready or Running state.
1542 // !!! TODO: Why comment does not match the code???
1543 if (state == StateRunning) {
1544 ++running_threads;
1545 // Stop counting running threads if the number is already greater than
1546 // the number of available cores
1547 if (running_threads >= max) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001548 goto finish;
Jonathan Peyton30419822017-05-12 18:01:32 +00001549 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001550 }
1551 }
1552 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001553 if (spi->NextEntryOffset == 0) {
1554 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001555 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001556 spi = PSYSTEM_PROCESS_INFORMATION(uintptr_t(spi) + spi->NextEntryOffset);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001557 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001558
Jonathan Peyton30419822017-05-12 18:01:32 +00001559#undef CHECK
Jim Cownie5e8470a2013-09-27 10:38:44 +00001560
Jonathan Peyton30419822017-05-12 18:01:32 +00001561finish: // Clean up and exit.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001562
Jonathan Peyton30419822017-05-12 18:01:32 +00001563 if (buffer != NULL) {
1564 KMP_INTERNAL_FREE(buffer);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001565 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001566
Jonathan Peyton30419822017-05-12 18:01:32 +00001567 glb_running_threads = running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001568
Jonathan Peyton30419822017-05-12 18:01:32 +00001569 return running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001570} //__kmp_get_load_balance()