10b57cec5SDimitry Andric /*
20b57cec5SDimitry Andric * z_Windows_NT_util.cpp -- platform specific routines.
30b57cec5SDimitry Andric */
40b57cec5SDimitry Andric
50b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
80b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
90b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "kmp.h"
140b57cec5SDimitry Andric #include "kmp_affinity.h"
150b57cec5SDimitry Andric #include "kmp_i18n.h"
160b57cec5SDimitry Andric #include "kmp_io.h"
170b57cec5SDimitry Andric #include "kmp_itt.h"
180b57cec5SDimitry Andric #include "kmp_wait_release.h"
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric /* This code is related to NtQuerySystemInformation() function. This function
210b57cec5SDimitry Andric is used in the Load balance algorithm for OMP_DYNAMIC=true to find the
220b57cec5SDimitry Andric number of running threads in the system. */
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric #include <ntsecapi.h> // UNICODE_STRING
25*7a6dacacSDimitry Andric #undef WIN32_NO_STATUS
260b57cec5SDimitry Andric #include <ntstatus.h>
27fe6060f1SDimitry Andric #include <psapi.h>
28fe6060f1SDimitry Andric #ifdef _MSC_VER
29fe6060f1SDimitry Andric #pragma comment(lib, "psapi.lib")
30fe6060f1SDimitry Andric #endif
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric enum SYSTEM_INFORMATION_CLASS {
330b57cec5SDimitry Andric SystemProcessInformation = 5
340b57cec5SDimitry Andric }; // SYSTEM_INFORMATION_CLASS
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric struct CLIENT_ID {
370b57cec5SDimitry Andric HANDLE UniqueProcess;
380b57cec5SDimitry Andric HANDLE UniqueThread;
390b57cec5SDimitry Andric }; // struct CLIENT_ID
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric enum THREAD_STATE {
420b57cec5SDimitry Andric StateInitialized,
430b57cec5SDimitry Andric StateReady,
440b57cec5SDimitry Andric StateRunning,
450b57cec5SDimitry Andric StateStandby,
460b57cec5SDimitry Andric StateTerminated,
470b57cec5SDimitry Andric StateWait,
480b57cec5SDimitry Andric StateTransition,
490b57cec5SDimitry Andric StateUnknown
500b57cec5SDimitry Andric }; // enum THREAD_STATE
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric struct VM_COUNTERS {
530b57cec5SDimitry Andric SIZE_T PeakVirtualSize;
540b57cec5SDimitry Andric SIZE_T VirtualSize;
550b57cec5SDimitry Andric ULONG PageFaultCount;
560b57cec5SDimitry Andric SIZE_T PeakWorkingSetSize;
570b57cec5SDimitry Andric SIZE_T WorkingSetSize;
580b57cec5SDimitry Andric SIZE_T QuotaPeakPagedPoolUsage;
590b57cec5SDimitry Andric SIZE_T QuotaPagedPoolUsage;
600b57cec5SDimitry Andric SIZE_T QuotaPeakNonPagedPoolUsage;
610b57cec5SDimitry Andric SIZE_T QuotaNonPagedPoolUsage;
620b57cec5SDimitry Andric SIZE_T PagefileUsage;
630b57cec5SDimitry Andric SIZE_T PeakPagefileUsage;
640b57cec5SDimitry Andric SIZE_T PrivatePageCount;
650b57cec5SDimitry Andric }; // struct VM_COUNTERS
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric struct SYSTEM_THREAD {
680b57cec5SDimitry Andric LARGE_INTEGER KernelTime;
690b57cec5SDimitry Andric LARGE_INTEGER UserTime;
700b57cec5SDimitry Andric LARGE_INTEGER CreateTime;
710b57cec5SDimitry Andric ULONG WaitTime;
720b57cec5SDimitry Andric LPVOID StartAddress;
730b57cec5SDimitry Andric CLIENT_ID ClientId;
740b57cec5SDimitry Andric DWORD Priority;
750b57cec5SDimitry Andric LONG BasePriority;
760b57cec5SDimitry Andric ULONG ContextSwitchCount;
770b57cec5SDimitry Andric THREAD_STATE State;
780b57cec5SDimitry Andric ULONG WaitReason;
790b57cec5SDimitry Andric }; // SYSTEM_THREAD
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, KernelTime) == 0);
82bdd1243dSDimitry Andric #if KMP_ARCH_X86 || KMP_ARCH_ARM
830b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 28);
840b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 52);
850b57cec5SDimitry Andric #else
860b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 32);
870b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 68);
880b57cec5SDimitry Andric #endif
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric struct SYSTEM_PROCESS_INFORMATION {
910b57cec5SDimitry Andric ULONG NextEntryOffset;
920b57cec5SDimitry Andric ULONG NumberOfThreads;
930b57cec5SDimitry Andric LARGE_INTEGER Reserved[3];
940b57cec5SDimitry Andric LARGE_INTEGER CreateTime;
950b57cec5SDimitry Andric LARGE_INTEGER UserTime;
960b57cec5SDimitry Andric LARGE_INTEGER KernelTime;
970b57cec5SDimitry Andric UNICODE_STRING ImageName;
980b57cec5SDimitry Andric DWORD BasePriority;
990b57cec5SDimitry Andric HANDLE ProcessId;
1000b57cec5SDimitry Andric HANDLE ParentProcessId;
1010b57cec5SDimitry Andric ULONG HandleCount;
1020b57cec5SDimitry Andric ULONG Reserved2[2];
1030b57cec5SDimitry Andric VM_COUNTERS VMCounters;
1040b57cec5SDimitry Andric IO_COUNTERS IOCounters;
1050b57cec5SDimitry Andric SYSTEM_THREAD Threads[1];
1060b57cec5SDimitry Andric }; // SYSTEM_PROCESS_INFORMATION
1070b57cec5SDimitry Andric typedef SYSTEM_PROCESS_INFORMATION *PSYSTEM_PROCESS_INFORMATION;
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, NextEntryOffset) == 0);
1100b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, CreateTime) == 32);
1110b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ImageName) == 56);
112bdd1243dSDimitry Andric #if KMP_ARCH_X86 || KMP_ARCH_ARM
1130b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 68);
1140b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 76);
1150b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 88);
1160b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 136);
1170b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 184);
1180b57cec5SDimitry Andric #else
1190b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 80);
1200b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 96);
1210b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 112);
1220b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 208);
1230b57cec5SDimitry Andric KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 256);
1240b57cec5SDimitry Andric #endif
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric typedef NTSTATUS(NTAPI *NtQuerySystemInformation_t)(SYSTEM_INFORMATION_CLASS,
1270b57cec5SDimitry Andric PVOID, ULONG, PULONG);
1280b57cec5SDimitry Andric NtQuerySystemInformation_t NtQuerySystemInformation = NULL;
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric HMODULE ntdll = NULL;
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric /* End of NtQuerySystemInformation()-related code */
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric static HMODULE kernel32 = NULL;
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric #if KMP_HANDLE_SIGNALS
1370b57cec5SDimitry Andric typedef void (*sig_func_t)(int);
1380b57cec5SDimitry Andric static sig_func_t __kmp_sighldrs[NSIG];
1390b57cec5SDimitry Andric static int __kmp_siginstalled[NSIG];
1400b57cec5SDimitry Andric #endif
1410b57cec5SDimitry Andric
1420b57cec5SDimitry Andric #if KMP_USE_MONITOR
1430b57cec5SDimitry Andric static HANDLE __kmp_monitor_ev;
1440b57cec5SDimitry Andric #endif
1450b57cec5SDimitry Andric static kmp_int64 __kmp_win32_time;
1460b57cec5SDimitry Andric double __kmp_win32_tick;
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric int __kmp_init_runtime = FALSE;
1490b57cec5SDimitry Andric CRITICAL_SECTION __kmp_win32_section;
1500b57cec5SDimitry Andric
__kmp_win32_mutex_init(kmp_win32_mutex_t * mx)1510b57cec5SDimitry Andric void __kmp_win32_mutex_init(kmp_win32_mutex_t *mx) {
1520b57cec5SDimitry Andric InitializeCriticalSection(&mx->cs);
1530b57cec5SDimitry Andric #if USE_ITT_BUILD
1540b57cec5SDimitry Andric __kmp_itt_system_object_created(&mx->cs, "Critical Section");
1550b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric
__kmp_win32_mutex_destroy(kmp_win32_mutex_t * mx)1580b57cec5SDimitry Andric void __kmp_win32_mutex_destroy(kmp_win32_mutex_t *mx) {
1590b57cec5SDimitry Andric DeleteCriticalSection(&mx->cs);
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric
__kmp_win32_mutex_lock(kmp_win32_mutex_t * mx)1620b57cec5SDimitry Andric void __kmp_win32_mutex_lock(kmp_win32_mutex_t *mx) {
1630b57cec5SDimitry Andric EnterCriticalSection(&mx->cs);
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric
__kmp_win32_mutex_trylock(kmp_win32_mutex_t * mx)1660b57cec5SDimitry Andric int __kmp_win32_mutex_trylock(kmp_win32_mutex_t *mx) {
1670b57cec5SDimitry Andric return TryEnterCriticalSection(&mx->cs);
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric
__kmp_win32_mutex_unlock(kmp_win32_mutex_t * mx)1700b57cec5SDimitry Andric void __kmp_win32_mutex_unlock(kmp_win32_mutex_t *mx) {
1710b57cec5SDimitry Andric LeaveCriticalSection(&mx->cs);
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric
__kmp_win32_cond_init(kmp_win32_cond_t * cv)1740b57cec5SDimitry Andric void __kmp_win32_cond_init(kmp_win32_cond_t *cv) {
1750b57cec5SDimitry Andric cv->waiters_count_ = 0;
1760b57cec5SDimitry Andric cv->wait_generation_count_ = 0;
1770b57cec5SDimitry Andric cv->release_count_ = 0;
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric /* Initialize the critical section */
1800b57cec5SDimitry Andric __kmp_win32_mutex_init(&cv->waiters_count_lock_);
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric /* Create a manual-reset event. */
1830b57cec5SDimitry Andric cv->event_ = CreateEvent(NULL, // no security
1840b57cec5SDimitry Andric TRUE, // manual-reset
1850b57cec5SDimitry Andric FALSE, // non-signaled initially
1860b57cec5SDimitry Andric NULL); // unnamed
1870b57cec5SDimitry Andric #if USE_ITT_BUILD
1880b57cec5SDimitry Andric __kmp_itt_system_object_created(cv->event_, "Event");
1890b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric
__kmp_win32_cond_destroy(kmp_win32_cond_t * cv)1920b57cec5SDimitry Andric void __kmp_win32_cond_destroy(kmp_win32_cond_t *cv) {
1930b57cec5SDimitry Andric __kmp_win32_mutex_destroy(&cv->waiters_count_lock_);
1940b57cec5SDimitry Andric __kmp_free_handle(cv->event_);
1950b57cec5SDimitry Andric memset(cv, '\0', sizeof(*cv));
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric /* TODO associate cv with a team instead of a thread so as to optimize
1990b57cec5SDimitry Andric the case where we wake up a whole team */
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andric template <class C>
__kmp_win32_cond_wait(kmp_win32_cond_t * cv,kmp_win32_mutex_t * mx,kmp_info_t * th,C * flag)2020b57cec5SDimitry Andric static void __kmp_win32_cond_wait(kmp_win32_cond_t *cv, kmp_win32_mutex_t *mx,
2030b57cec5SDimitry Andric kmp_info_t *th, C *flag) {
2040b57cec5SDimitry Andric int my_generation;
2050b57cec5SDimitry Andric int last_waiter;
2060b57cec5SDimitry Andric
2070b57cec5SDimitry Andric /* Avoid race conditions */
2080b57cec5SDimitry Andric __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
2090b57cec5SDimitry Andric
2100b57cec5SDimitry Andric /* Increment count of waiters */
2110b57cec5SDimitry Andric cv->waiters_count_++;
2120b57cec5SDimitry Andric
2130b57cec5SDimitry Andric /* Store current generation in our activation record. */
2140b57cec5SDimitry Andric my_generation = cv->wait_generation_count_;
2150b57cec5SDimitry Andric
2160b57cec5SDimitry Andric __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
2170b57cec5SDimitry Andric __kmp_win32_mutex_unlock(mx);
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric for (;;) {
2200b57cec5SDimitry Andric int wait_done = 0;
2210b57cec5SDimitry Andric DWORD res, timeout = 5000; // just tried to quess an appropriate number
2220b57cec5SDimitry Andric /* Wait until the event is signaled */
2230b57cec5SDimitry Andric res = WaitForSingleObject(cv->event_, timeout);
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andric if (res == WAIT_OBJECT_0) {
2260b57cec5SDimitry Andric // event signaled
2270b57cec5SDimitry Andric __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
2280b57cec5SDimitry Andric /* Exit the loop when the <cv->event_> is signaled and there are still
2290b57cec5SDimitry Andric waiting threads from this <wait_generation> that haven't been released
2300b57cec5SDimitry Andric from this wait yet. */
2310b57cec5SDimitry Andric wait_done = (cv->release_count_ > 0) &&
2320b57cec5SDimitry Andric (cv->wait_generation_count_ != my_generation);
2330b57cec5SDimitry Andric __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
2340b57cec5SDimitry Andric } else if (res == WAIT_TIMEOUT || res == WAIT_FAILED) {
2350b57cec5SDimitry Andric // check if the flag and cv counters are in consistent state
2360b57cec5SDimitry Andric // as MS sent us debug dump whith inconsistent state of data
2370b57cec5SDimitry Andric __kmp_win32_mutex_lock(mx);
2380b57cec5SDimitry Andric typename C::flag_t old_f = flag->set_sleeping();
2390b57cec5SDimitry Andric if (!flag->done_check_val(old_f & ~KMP_BARRIER_SLEEP_STATE)) {
2400b57cec5SDimitry Andric __kmp_win32_mutex_unlock(mx);
2410b57cec5SDimitry Andric continue;
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric // condition fulfilled, exiting
244349cc55cSDimitry Andric flag->unset_sleeping();
2450b57cec5SDimitry Andric TCW_PTR(th->th.th_sleep_loc, NULL);
246349cc55cSDimitry Andric th->th.th_sleep_loc_type = flag_unset;
247349cc55cSDimitry Andric KF_TRACE(50, ("__kmp_win32_cond_wait: exiting, condition "
248349cc55cSDimitry Andric "fulfilled: flag's loc(%p): %u\n",
249349cc55cSDimitry Andric flag->get(), (unsigned int)flag->load()));
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
2520b57cec5SDimitry Andric KMP_DEBUG_ASSERT(cv->waiters_count_ > 0);
2530b57cec5SDimitry Andric cv->release_count_ = cv->waiters_count_;
2540b57cec5SDimitry Andric cv->wait_generation_count_++;
2550b57cec5SDimitry Andric wait_done = 1;
2560b57cec5SDimitry Andric __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric __kmp_win32_mutex_unlock(mx);
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric /* there used to be a semicolon after the if statement, it looked like a
2610b57cec5SDimitry Andric bug, so i removed it */
2620b57cec5SDimitry Andric if (wait_done)
2630b57cec5SDimitry Andric break;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric __kmp_win32_mutex_lock(mx);
2670b57cec5SDimitry Andric __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric cv->waiters_count_--;
2700b57cec5SDimitry Andric cv->release_count_--;
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric last_waiter = (cv->release_count_ == 0);
2730b57cec5SDimitry Andric
2740b57cec5SDimitry Andric __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andric if (last_waiter) {
2770b57cec5SDimitry Andric /* We're the last waiter to be notified, so reset the manual event. */
2780b57cec5SDimitry Andric ResetEvent(cv->event_);
2790b57cec5SDimitry Andric }
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric
__kmp_win32_cond_broadcast(kmp_win32_cond_t * cv)2820b57cec5SDimitry Andric void __kmp_win32_cond_broadcast(kmp_win32_cond_t *cv) {
2830b57cec5SDimitry Andric __kmp_win32_mutex_lock(&cv->waiters_count_lock_);
2840b57cec5SDimitry Andric
2850b57cec5SDimitry Andric if (cv->waiters_count_ > 0) {
2860b57cec5SDimitry Andric SetEvent(cv->event_);
2870b57cec5SDimitry Andric /* Release all the threads in this generation. */
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andric cv->release_count_ = cv->waiters_count_;
2900b57cec5SDimitry Andric
2910b57cec5SDimitry Andric /* Start a new generation. */
2920b57cec5SDimitry Andric cv->wait_generation_count_++;
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andric __kmp_win32_mutex_unlock(&cv->waiters_count_lock_);
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric
__kmp_win32_cond_signal(kmp_win32_cond_t * cv)2980b57cec5SDimitry Andric void __kmp_win32_cond_signal(kmp_win32_cond_t *cv) {
2990b57cec5SDimitry Andric __kmp_win32_cond_broadcast(cv);
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric
__kmp_enable(int new_state)3020b57cec5SDimitry Andric void __kmp_enable(int new_state) {
3030b57cec5SDimitry Andric if (__kmp_init_runtime)
3040b57cec5SDimitry Andric LeaveCriticalSection(&__kmp_win32_section);
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric
__kmp_disable(int * old_state)3070b57cec5SDimitry Andric void __kmp_disable(int *old_state) {
3080b57cec5SDimitry Andric *old_state = 0;
3090b57cec5SDimitry Andric
3100b57cec5SDimitry Andric if (__kmp_init_runtime)
3110b57cec5SDimitry Andric EnterCriticalSection(&__kmp_win32_section);
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric
__kmp_suspend_initialize(void)3140b57cec5SDimitry Andric void __kmp_suspend_initialize(void) { /* do nothing */
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric
__kmp_suspend_initialize_thread(kmp_info_t * th)3170b57cec5SDimitry Andric void __kmp_suspend_initialize_thread(kmp_info_t *th) {
3180b57cec5SDimitry Andric int old_value = KMP_ATOMIC_LD_RLX(&th->th.th_suspend_init);
3190b57cec5SDimitry Andric int new_value = TRUE;
3200b57cec5SDimitry Andric // Return if already initialized
3210b57cec5SDimitry Andric if (old_value == new_value)
3220b57cec5SDimitry Andric return;
3230b57cec5SDimitry Andric // Wait, then return if being initialized
3240b57cec5SDimitry Andric if (old_value == -1 ||
3250b57cec5SDimitry Andric !__kmp_atomic_compare_store(&th->th.th_suspend_init, old_value, -1)) {
3260b57cec5SDimitry Andric while (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init) != new_value) {
3270b57cec5SDimitry Andric KMP_CPU_PAUSE();
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric } else {
3300b57cec5SDimitry Andric // Claim to be the initializer and do initializations
3310b57cec5SDimitry Andric __kmp_win32_cond_init(&th->th.th_suspend_cv);
3320b57cec5SDimitry Andric __kmp_win32_mutex_init(&th->th.th_suspend_mx);
3330b57cec5SDimitry Andric KMP_ATOMIC_ST_REL(&th->th.th_suspend_init, new_value);
3340b57cec5SDimitry Andric }
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric
__kmp_suspend_uninitialize_thread(kmp_info_t * th)3370b57cec5SDimitry Andric void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
3380b57cec5SDimitry Andric if (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init)) {
3390b57cec5SDimitry Andric /* this means we have initialize the suspension pthread objects for this
3400b57cec5SDimitry Andric thread in this instance of the process */
3410b57cec5SDimitry Andric __kmp_win32_cond_destroy(&th->th.th_suspend_cv);
3420b57cec5SDimitry Andric __kmp_win32_mutex_destroy(&th->th.th_suspend_mx);
3430b57cec5SDimitry Andric KMP_ATOMIC_ST_REL(&th->th.th_suspend_init, FALSE);
3440b57cec5SDimitry Andric }
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric
__kmp_try_suspend_mx(kmp_info_t * th)3470b57cec5SDimitry Andric int __kmp_try_suspend_mx(kmp_info_t *th) {
3480b57cec5SDimitry Andric return __kmp_win32_mutex_trylock(&th->th.th_suspend_mx);
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric
__kmp_lock_suspend_mx(kmp_info_t * th)3510b57cec5SDimitry Andric void __kmp_lock_suspend_mx(kmp_info_t *th) {
3520b57cec5SDimitry Andric __kmp_win32_mutex_lock(&th->th.th_suspend_mx);
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric
__kmp_unlock_suspend_mx(kmp_info_t * th)3550b57cec5SDimitry Andric void __kmp_unlock_suspend_mx(kmp_info_t *th) {
3560b57cec5SDimitry Andric __kmp_win32_mutex_unlock(&th->th.th_suspend_mx);
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andric /* This routine puts the calling thread to sleep after setting the
3600b57cec5SDimitry Andric sleep bit for the indicated flag variable to true. */
3610b57cec5SDimitry Andric template <class C>
__kmp_suspend_template(int th_gtid,C * flag)3620b57cec5SDimitry Andric static inline void __kmp_suspend_template(int th_gtid, C *flag) {
3630b57cec5SDimitry Andric kmp_info_t *th = __kmp_threads[th_gtid];
3640b57cec5SDimitry Andric typename C::flag_t old_spin;
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag's loc(%p)\n",
3670b57cec5SDimitry Andric th_gtid, flag->get()));
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andric __kmp_suspend_initialize_thread(th);
370e8d8bef9SDimitry Andric __kmp_lock_suspend_mx(th);
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andric KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for flag's"
3730b57cec5SDimitry Andric " loc(%p)\n",
3740b57cec5SDimitry Andric th_gtid, flag->get()));
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andric /* TODO: shouldn't this use release semantics to ensure that
3770b57cec5SDimitry Andric __kmp_suspend_initialize_thread gets called first? */
3780b57cec5SDimitry Andric old_spin = flag->set_sleeping();
379349cc55cSDimitry Andric TCW_PTR(th->th.th_sleep_loc, (void *)flag);
380349cc55cSDimitry Andric th->th.th_sleep_loc_type = flag->get_type();
3810b57cec5SDimitry Andric if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME &&
3820b57cec5SDimitry Andric __kmp_pause_status != kmp_soft_paused) {
3830b57cec5SDimitry Andric flag->unset_sleeping();
384349cc55cSDimitry Andric TCW_PTR(th->th.th_sleep_loc, NULL);
385349cc55cSDimitry Andric th->th.th_sleep_loc_type = flag_unset;
386e8d8bef9SDimitry Andric __kmp_unlock_suspend_mx(th);
3870b57cec5SDimitry Andric return;
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric
3900b57cec5SDimitry Andric KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for flag's"
391fe6060f1SDimitry Andric " loc(%p)==%u\n",
392fe6060f1SDimitry Andric th_gtid, flag->get(), (unsigned int)flag->load()));
3930b57cec5SDimitry Andric
394349cc55cSDimitry Andric if (flag->done_check_val(old_spin) || flag->done_check()) {
395349cc55cSDimitry Andric flag->unset_sleeping();
396349cc55cSDimitry Andric TCW_PTR(th->th.th_sleep_loc, NULL);
397349cc55cSDimitry Andric th->th.th_sleep_loc_type = flag_unset;
3980b57cec5SDimitry Andric KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
3990b57cec5SDimitry Andric "for flag's loc(%p)\n",
4000b57cec5SDimitry Andric th_gtid, flag->get()));
4010b57cec5SDimitry Andric } else {
4020b57cec5SDimitry Andric #ifdef DEBUG_SUSPEND
4030b57cec5SDimitry Andric __kmp_suspend_count++;
4040b57cec5SDimitry Andric #endif
4050b57cec5SDimitry Andric /* Encapsulate in a loop as the documentation states that this may "with
4060b57cec5SDimitry Andric low probability" return when the condition variable has not been signaled
4070b57cec5SDimitry Andric or broadcast */
4080b57cec5SDimitry Andric int deactivated = FALSE;
409349cc55cSDimitry Andric
4100b57cec5SDimitry Andric while (flag->is_sleeping()) {
4110b57cec5SDimitry Andric KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
4120b57cec5SDimitry Andric "kmp_win32_cond_wait()\n",
4130b57cec5SDimitry Andric th_gtid));
4140b57cec5SDimitry Andric // Mark the thread as no longer active (only in the first iteration of the
4150b57cec5SDimitry Andric // loop).
4160b57cec5SDimitry Andric if (!deactivated) {
4170b57cec5SDimitry Andric th->th.th_active = FALSE;
4180b57cec5SDimitry Andric if (th->th.th_active_in_pool) {
4190b57cec5SDimitry Andric th->th.th_active_in_pool = FALSE;
4200b57cec5SDimitry Andric KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
4210b57cec5SDimitry Andric KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
4220b57cec5SDimitry Andric }
4230b57cec5SDimitry Andric deactivated = TRUE;
4240b57cec5SDimitry Andric }
4250b57cec5SDimitry Andric
426349cc55cSDimitry Andric KMP_DEBUG_ASSERT(th->th.th_sleep_loc);
427349cc55cSDimitry Andric KMP_DEBUG_ASSERT(th->th.th_sleep_loc_type == flag->get_type());
428349cc55cSDimitry Andric
429349cc55cSDimitry Andric __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, th,
430349cc55cSDimitry Andric flag);
431349cc55cSDimitry Andric
4320b57cec5SDimitry Andric #ifdef KMP_DEBUG
4330b57cec5SDimitry Andric if (flag->is_sleeping()) {
4340b57cec5SDimitry Andric KF_TRACE(100,
4350b57cec5SDimitry Andric ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
4360b57cec5SDimitry Andric }
4370b57cec5SDimitry Andric #endif /* KMP_DEBUG */
4380b57cec5SDimitry Andric
4390b57cec5SDimitry Andric } // while
4400b57cec5SDimitry Andric
441349cc55cSDimitry Andric // We may have had the loop variable set before entering the loop body;
442349cc55cSDimitry Andric // so we need to reset sleep_loc.
443349cc55cSDimitry Andric TCW_PTR(th->th.th_sleep_loc, NULL);
444349cc55cSDimitry Andric th->th.th_sleep_loc_type = flag_unset;
445349cc55cSDimitry Andric
446349cc55cSDimitry Andric KMP_DEBUG_ASSERT(!flag->is_sleeping());
447349cc55cSDimitry Andric KMP_DEBUG_ASSERT(!th->th.th_sleep_loc);
448349cc55cSDimitry Andric
4490b57cec5SDimitry Andric // Mark the thread as active again (if it was previous marked as inactive)
4500b57cec5SDimitry Andric if (deactivated) {
4510b57cec5SDimitry Andric th->th.th_active = TRUE;
4520b57cec5SDimitry Andric if (TCR_4(th->th.th_in_pool)) {
4530b57cec5SDimitry Andric KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
4540b57cec5SDimitry Andric th->th.th_active_in_pool = TRUE;
4550b57cec5SDimitry Andric }
4560b57cec5SDimitry Andric }
4570b57cec5SDimitry Andric }
4580b57cec5SDimitry Andric
459e8d8bef9SDimitry Andric __kmp_unlock_suspend_mx(th);
4600b57cec5SDimitry Andric KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
4610b57cec5SDimitry Andric }
4620b57cec5SDimitry Andric
463e8d8bef9SDimitry Andric template <bool C, bool S>
__kmp_suspend_32(int th_gtid,kmp_flag_32<C,S> * flag)464e8d8bef9SDimitry Andric void __kmp_suspend_32(int th_gtid, kmp_flag_32<C, S> *flag) {
4650b57cec5SDimitry Andric __kmp_suspend_template(th_gtid, flag);
4660b57cec5SDimitry Andric }
467e8d8bef9SDimitry Andric template <bool C, bool S>
__kmp_suspend_64(int th_gtid,kmp_flag_64<C,S> * flag)468e8d8bef9SDimitry Andric void __kmp_suspend_64(int th_gtid, kmp_flag_64<C, S> *flag) {
4690b57cec5SDimitry Andric __kmp_suspend_template(th_gtid, flag);
4700b57cec5SDimitry Andric }
471349cc55cSDimitry Andric template <bool C, bool S>
__kmp_atomic_suspend_64(int th_gtid,kmp_atomic_flag_64<C,S> * flag)472349cc55cSDimitry Andric void __kmp_atomic_suspend_64(int th_gtid, kmp_atomic_flag_64<C, S> *flag) {
473349cc55cSDimitry Andric __kmp_suspend_template(th_gtid, flag);
474349cc55cSDimitry Andric }
__kmp_suspend_oncore(int th_gtid,kmp_flag_oncore * flag)4750b57cec5SDimitry Andric void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
4760b57cec5SDimitry Andric __kmp_suspend_template(th_gtid, flag);
4770b57cec5SDimitry Andric }
4780b57cec5SDimitry Andric
479e8d8bef9SDimitry Andric template void __kmp_suspend_32<false, false>(int, kmp_flag_32<false, false> *);
480e8d8bef9SDimitry Andric template void __kmp_suspend_64<false, true>(int, kmp_flag_64<false, true> *);
481e8d8bef9SDimitry Andric template void __kmp_suspend_64<true, false>(int, kmp_flag_64<true, false> *);
482349cc55cSDimitry Andric template void
483349cc55cSDimitry Andric __kmp_atomic_suspend_64<false, true>(int, kmp_atomic_flag_64<false, true> *);
484349cc55cSDimitry Andric template void
485349cc55cSDimitry Andric __kmp_atomic_suspend_64<true, false>(int, kmp_atomic_flag_64<true, false> *);
486e8d8bef9SDimitry Andric
4870b57cec5SDimitry Andric /* This routine signals the thread specified by target_gtid to wake up
4880b57cec5SDimitry Andric after setting the sleep bit indicated by the flag argument to FALSE */
4890b57cec5SDimitry Andric template <class C>
__kmp_resume_template(int target_gtid,C * flag)4900b57cec5SDimitry Andric static inline void __kmp_resume_template(int target_gtid, C *flag) {
4910b57cec5SDimitry Andric kmp_info_t *th = __kmp_threads[target_gtid];
4920b57cec5SDimitry Andric
4930b57cec5SDimitry Andric #ifdef KMP_DEBUG
4940b57cec5SDimitry Andric int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
4950b57cec5SDimitry Andric #endif
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andric KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
4980b57cec5SDimitry Andric gtid, target_gtid));
4990b57cec5SDimitry Andric
5000b57cec5SDimitry Andric __kmp_suspend_initialize_thread(th);
501e8d8bef9SDimitry Andric __kmp_lock_suspend_mx(th);
5020b57cec5SDimitry Andric
503349cc55cSDimitry Andric if (!flag || flag != th->th.th_sleep_loc) {
504349cc55cSDimitry Andric // coming from __kmp_null_resume_wrapper, or thread is now sleeping on a
505349cc55cSDimitry Andric // different location; wake up at new location
5060b57cec5SDimitry Andric flag = (C *)th->th.th_sleep_loc;
5070b57cec5SDimitry Andric }
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric // First, check if the flag is null or its type has changed. If so, someone
5100b57cec5SDimitry Andric // else woke it up.
511349cc55cSDimitry Andric if (!flag || flag->get_type() != th->th.th_sleep_loc_type) {
512349cc55cSDimitry Andric // simply shows what flag was cast to
5130b57cec5SDimitry Andric KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
5140b57cec5SDimitry Andric "awake: flag's loc(%p)\n",
5150b57cec5SDimitry Andric gtid, target_gtid, NULL));
516e8d8bef9SDimitry Andric __kmp_unlock_suspend_mx(th);
5170b57cec5SDimitry Andric return;
5180b57cec5SDimitry Andric } else {
519349cc55cSDimitry Andric if (!flag->is_sleeping()) {
5200b57cec5SDimitry Andric KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
521349cc55cSDimitry Andric "awake: flag's loc(%p): %u\n",
522349cc55cSDimitry Andric gtid, target_gtid, flag->get(), (unsigned int)flag->load()));
523e8d8bef9SDimitry Andric __kmp_unlock_suspend_mx(th);
5240b57cec5SDimitry Andric return;
5250b57cec5SDimitry Andric }
5260b57cec5SDimitry Andric }
527349cc55cSDimitry Andric KMP_DEBUG_ASSERT(flag);
528349cc55cSDimitry Andric flag->unset_sleeping();
5290b57cec5SDimitry Andric TCW_PTR(th->th.th_sleep_loc, NULL);
530349cc55cSDimitry Andric th->th.th_sleep_loc_type = flag_unset;
531349cc55cSDimitry Andric
5320b57cec5SDimitry Andric KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep "
5330b57cec5SDimitry Andric "bit for flag's loc(%p)\n",
5340b57cec5SDimitry Andric gtid, target_gtid, flag->get()));
5350b57cec5SDimitry Andric
5360b57cec5SDimitry Andric __kmp_win32_cond_signal(&th->th.th_suspend_cv);
537e8d8bef9SDimitry Andric __kmp_unlock_suspend_mx(th);
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
5400b57cec5SDimitry Andric " for T#%d\n",
5410b57cec5SDimitry Andric gtid, target_gtid));
5420b57cec5SDimitry Andric }
5430b57cec5SDimitry Andric
544e8d8bef9SDimitry Andric template <bool C, bool S>
__kmp_resume_32(int target_gtid,kmp_flag_32<C,S> * flag)545e8d8bef9SDimitry Andric void __kmp_resume_32(int target_gtid, kmp_flag_32<C, S> *flag) {
5460b57cec5SDimitry Andric __kmp_resume_template(target_gtid, flag);
5470b57cec5SDimitry Andric }
548e8d8bef9SDimitry Andric template <bool C, bool S>
__kmp_resume_64(int target_gtid,kmp_flag_64<C,S> * flag)549e8d8bef9SDimitry Andric void __kmp_resume_64(int target_gtid, kmp_flag_64<C, S> *flag) {
5500b57cec5SDimitry Andric __kmp_resume_template(target_gtid, flag);
5510b57cec5SDimitry Andric }
552349cc55cSDimitry Andric template <bool C, bool S>
__kmp_atomic_resume_64(int target_gtid,kmp_atomic_flag_64<C,S> * flag)553349cc55cSDimitry Andric void __kmp_atomic_resume_64(int target_gtid, kmp_atomic_flag_64<C, S> *flag) {
554349cc55cSDimitry Andric __kmp_resume_template(target_gtid, flag);
555349cc55cSDimitry Andric }
__kmp_resume_oncore(int target_gtid,kmp_flag_oncore * flag)5560b57cec5SDimitry Andric void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
5570b57cec5SDimitry Andric __kmp_resume_template(target_gtid, flag);
5580b57cec5SDimitry Andric }
5590b57cec5SDimitry Andric
560e8d8bef9SDimitry Andric template void __kmp_resume_32<false, true>(int, kmp_flag_32<false, true> *);
561349cc55cSDimitry Andric template void __kmp_resume_32<false, false>(int, kmp_flag_32<false, false> *);
562e8d8bef9SDimitry Andric template void __kmp_resume_64<false, true>(int, kmp_flag_64<false, true> *);
563349cc55cSDimitry Andric template void
564349cc55cSDimitry Andric __kmp_atomic_resume_64<false, true>(int, kmp_atomic_flag_64<false, true> *);
565e8d8bef9SDimitry Andric
__kmp_yield()5660b57cec5SDimitry Andric void __kmp_yield() { Sleep(0); }
5670b57cec5SDimitry Andric
__kmp_gtid_set_specific(int gtid)5680b57cec5SDimitry Andric void __kmp_gtid_set_specific(int gtid) {
5690b57cec5SDimitry Andric if (__kmp_init_gtid) {
5700b57cec5SDimitry Andric KA_TRACE(50, ("__kmp_gtid_set_specific: T#%d key:%d\n", gtid,
5710b57cec5SDimitry Andric __kmp_gtid_threadprivate_key));
57281ad6265SDimitry Andric kmp_intptr_t g = (kmp_intptr_t)gtid;
57381ad6265SDimitry Andric if (!TlsSetValue(__kmp_gtid_threadprivate_key, (LPVOID)(g + 1)))
5740b57cec5SDimitry Andric KMP_FATAL(TLSSetValueFailed);
5750b57cec5SDimitry Andric } else {
5760b57cec5SDimitry Andric KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
5770b57cec5SDimitry Andric }
5780b57cec5SDimitry Andric }
5790b57cec5SDimitry Andric
__kmp_gtid_get_specific()5800b57cec5SDimitry Andric int __kmp_gtid_get_specific() {
5810b57cec5SDimitry Andric int gtid;
5820b57cec5SDimitry Andric if (!__kmp_init_gtid) {
5830b57cec5SDimitry Andric KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
5840b57cec5SDimitry Andric "KMP_GTID_SHUTDOWN\n"));
5850b57cec5SDimitry Andric return KMP_GTID_SHUTDOWN;
5860b57cec5SDimitry Andric }
5870b57cec5SDimitry Andric gtid = (int)(kmp_intptr_t)TlsGetValue(__kmp_gtid_threadprivate_key);
5880b57cec5SDimitry Andric if (gtid == 0) {
5890b57cec5SDimitry Andric gtid = KMP_GTID_DNE;
5900b57cec5SDimitry Andric } else {
5910b57cec5SDimitry Andric gtid--;
5920b57cec5SDimitry Andric }
5930b57cec5SDimitry Andric KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
5940b57cec5SDimitry Andric __kmp_gtid_threadprivate_key, gtid));
5950b57cec5SDimitry Andric return gtid;
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric
__kmp_affinity_bind_thread(int proc)5980b57cec5SDimitry Andric void __kmp_affinity_bind_thread(int proc) {
5990b57cec5SDimitry Andric if (__kmp_num_proc_groups > 1) {
6000b57cec5SDimitry Andric // Form the GROUP_AFFINITY struct directly, rather than filling
6010b57cec5SDimitry Andric // out a bit vector and calling __kmp_set_system_affinity().
6020b57cec5SDimitry Andric GROUP_AFFINITY ga;
6030b57cec5SDimitry Andric KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups * CHAR_BIT *
6040b57cec5SDimitry Andric sizeof(DWORD_PTR))));
6050b57cec5SDimitry Andric ga.Group = proc / (CHAR_BIT * sizeof(DWORD_PTR));
6060b57cec5SDimitry Andric ga.Mask = (unsigned long long)1 << (proc % (CHAR_BIT * sizeof(DWORD_PTR)));
6070b57cec5SDimitry Andric ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0;
6080b57cec5SDimitry Andric
6090b57cec5SDimitry Andric KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL);
6100b57cec5SDimitry Andric if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) {
6110b57cec5SDimitry Andric DWORD error = GetLastError();
612bdd1243dSDimitry Andric // AC: continue silently if not verbose
613bdd1243dSDimitry Andric if (__kmp_affinity.flags.verbose) {
6140b57cec5SDimitry Andric kmp_msg_t err_code = KMP_ERR(error);
6150b57cec5SDimitry Andric __kmp_msg(kmp_ms_warning, KMP_MSG(CantSetThreadAffMask), err_code,
6160b57cec5SDimitry Andric __kmp_msg_null);
6170b57cec5SDimitry Andric if (__kmp_generate_warnings == kmp_warnings_off) {
6180b57cec5SDimitry Andric __kmp_str_free(&err_code.str);
6190b57cec5SDimitry Andric }
6200b57cec5SDimitry Andric }
6210b57cec5SDimitry Andric }
6220b57cec5SDimitry Andric } else {
6230b57cec5SDimitry Andric kmp_affin_mask_t *mask;
6240b57cec5SDimitry Andric KMP_CPU_ALLOC_ON_STACK(mask);
6250b57cec5SDimitry Andric KMP_CPU_ZERO(mask);
6260b57cec5SDimitry Andric KMP_CPU_SET(proc, mask);
6270b57cec5SDimitry Andric __kmp_set_system_affinity(mask, TRUE);
6280b57cec5SDimitry Andric KMP_CPU_FREE_FROM_STACK(mask);
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric }
6310b57cec5SDimitry Andric
__kmp_affinity_determine_capable(const char * env_var)6320b57cec5SDimitry Andric void __kmp_affinity_determine_capable(const char *env_var) {
633fe6060f1SDimitry Andric // All versions of Windows* OS (since Win '95) support
634fe6060f1SDimitry Andric // SetThreadAffinityMask().
6350b57cec5SDimitry Andric
6360b57cec5SDimitry Andric #if KMP_GROUP_AFFINITY
6370b57cec5SDimitry Andric KMP_AFFINITY_ENABLE(__kmp_num_proc_groups * sizeof(DWORD_PTR));
6380b57cec5SDimitry Andric #else
6390b57cec5SDimitry Andric KMP_AFFINITY_ENABLE(sizeof(DWORD_PTR));
6400b57cec5SDimitry Andric #endif
6410b57cec5SDimitry Andric
6420b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_affinity_determine_capable: "
6430b57cec5SDimitry Andric "Windows* OS affinity interface functional (mask size = "
6440b57cec5SDimitry Andric "%" KMP_SIZE_T_SPEC ").\n",
6450b57cec5SDimitry Andric __kmp_affin_mask_size));
6460b57cec5SDimitry Andric }
6470b57cec5SDimitry Andric
__kmp_read_cpu_time(void)6480b57cec5SDimitry Andric double __kmp_read_cpu_time(void) {
6490b57cec5SDimitry Andric FILETIME CreationTime, ExitTime, KernelTime, UserTime;
6500b57cec5SDimitry Andric int status;
6510b57cec5SDimitry Andric double cpu_time;
6520b57cec5SDimitry Andric
6530b57cec5SDimitry Andric cpu_time = 0;
6540b57cec5SDimitry Andric
6550b57cec5SDimitry Andric status = GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime,
6560b57cec5SDimitry Andric &KernelTime, &UserTime);
6570b57cec5SDimitry Andric
6580b57cec5SDimitry Andric if (status) {
6590b57cec5SDimitry Andric double sec = 0;
6600b57cec5SDimitry Andric
6610b57cec5SDimitry Andric sec += KernelTime.dwHighDateTime;
6620b57cec5SDimitry Andric sec += UserTime.dwHighDateTime;
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric /* Shift left by 32 bits */
6650b57cec5SDimitry Andric sec *= (double)(1 << 16) * (double)(1 << 16);
6660b57cec5SDimitry Andric
6670b57cec5SDimitry Andric sec += KernelTime.dwLowDateTime;
6680b57cec5SDimitry Andric sec += UserTime.dwLowDateTime;
6690b57cec5SDimitry Andric
6700b57cec5SDimitry Andric cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC;
6710b57cec5SDimitry Andric }
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric return cpu_time;
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric
__kmp_read_system_info(struct kmp_sys_info * info)6760b57cec5SDimitry Andric int __kmp_read_system_info(struct kmp_sys_info *info) {
6770b57cec5SDimitry Andric info->maxrss = 0; /* the maximum resident set size utilized (in kilobytes) */
6780b57cec5SDimitry Andric info->minflt = 0; /* the number of page faults serviced without any I/O */
6790b57cec5SDimitry Andric info->majflt = 0; /* the number of page faults serviced that required I/O */
6800b57cec5SDimitry Andric info->nswap = 0; // the number of times a process was "swapped" out of memory
6810b57cec5SDimitry Andric info->inblock = 0; // the number of times the file system had to perform input
6820b57cec5SDimitry Andric info->oublock = 0; // number of times the file system had to perform output
6830b57cec5SDimitry Andric info->nvcsw = 0; /* the number of times a context switch was voluntarily */
6840b57cec5SDimitry Andric info->nivcsw = 0; /* the number of times a context switch was forced */
6850b57cec5SDimitry Andric
6860b57cec5SDimitry Andric return 1;
6870b57cec5SDimitry Andric }
6880b57cec5SDimitry Andric
__kmp_runtime_initialize(void)6890b57cec5SDimitry Andric void __kmp_runtime_initialize(void) {
6900b57cec5SDimitry Andric SYSTEM_INFO info;
6910b57cec5SDimitry Andric kmp_str_buf_t path;
6920b57cec5SDimitry Andric UINT path_size;
6930b57cec5SDimitry Andric
6940b57cec5SDimitry Andric if (__kmp_init_runtime) {
6950b57cec5SDimitry Andric return;
6960b57cec5SDimitry Andric }
6970b57cec5SDimitry Andric
6980b57cec5SDimitry Andric #if KMP_DYNAMIC_LIB
6990b57cec5SDimitry Andric /* Pin dynamic library for the lifetime of application */
7000b57cec5SDimitry Andric {
7010b57cec5SDimitry Andric // First, turn off error message boxes
7020b57cec5SDimitry Andric UINT err_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
7030b57cec5SDimitry Andric HMODULE h;
7040b57cec5SDimitry Andric BOOL ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
7050b57cec5SDimitry Andric GET_MODULE_HANDLE_EX_FLAG_PIN,
7060b57cec5SDimitry Andric (LPCTSTR)&__kmp_serial_initialize, &h);
707fe6060f1SDimitry Andric (void)ret;
7080b57cec5SDimitry Andric KMP_DEBUG_ASSERT2(h && ret, "OpenMP RTL cannot find itself loaded");
7090b57cec5SDimitry Andric SetErrorMode(err_mode); // Restore error mode
7100b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize: dynamic library pinned\n"));
7110b57cec5SDimitry Andric }
7120b57cec5SDimitry Andric #endif
7130b57cec5SDimitry Andric
7140b57cec5SDimitry Andric InitializeCriticalSection(&__kmp_win32_section);
7150b57cec5SDimitry Andric #if USE_ITT_BUILD
7160b57cec5SDimitry Andric __kmp_itt_system_object_created(&__kmp_win32_section, "Critical Section");
7170b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
7180b57cec5SDimitry Andric __kmp_initialize_system_tick();
7190b57cec5SDimitry Andric
7200b57cec5SDimitry Andric #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
7210b57cec5SDimitry Andric if (!__kmp_cpuinfo.initialized) {
7220b57cec5SDimitry Andric __kmp_query_cpuid(&__kmp_cpuinfo);
7230b57cec5SDimitry Andric }
7240b57cec5SDimitry Andric #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
7250b57cec5SDimitry Andric
7260b57cec5SDimitry Andric /* Set up minimum number of threads to switch to TLS gtid */
7270b57cec5SDimitry Andric #if KMP_OS_WINDOWS && !KMP_DYNAMIC_LIB
7280b57cec5SDimitry Andric // Windows* OS, static library.
7290b57cec5SDimitry Andric /* New thread may use stack space previously used by another thread,
7300b57cec5SDimitry Andric currently terminated. On Windows* OS, in case of static linking, we do not
7310b57cec5SDimitry Andric know the moment of thread termination, and our structures (__kmp_threads
7320b57cec5SDimitry Andric and __kmp_root arrays) are still keep info about dead threads. This leads
7330b57cec5SDimitry Andric to problem in __kmp_get_global_thread_id() function: it wrongly finds gtid
7340b57cec5SDimitry Andric (by searching through stack addresses of all known threads) for
7350b57cec5SDimitry Andric unregistered foreign tread.
7360b57cec5SDimitry Andric
7370b57cec5SDimitry Andric Setting __kmp_tls_gtid_min to 0 workarounds this problem:
7380b57cec5SDimitry Andric __kmp_get_global_thread_id() does not search through stacks, but get gtid
7390b57cec5SDimitry Andric from TLS immediately.
7400b57cec5SDimitry Andric --ln
7410b57cec5SDimitry Andric */
7420b57cec5SDimitry Andric __kmp_tls_gtid_min = 0;
7430b57cec5SDimitry Andric #else
7440b57cec5SDimitry Andric __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
7450b57cec5SDimitry Andric #endif
7460b57cec5SDimitry Andric
7470b57cec5SDimitry Andric /* for the static library */
7480b57cec5SDimitry Andric if (!__kmp_gtid_threadprivate_key) {
7490b57cec5SDimitry Andric __kmp_gtid_threadprivate_key = TlsAlloc();
7500b57cec5SDimitry Andric if (__kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES) {
7510b57cec5SDimitry Andric KMP_FATAL(TLSOutOfIndexes);
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric }
7540b57cec5SDimitry Andric
7550b57cec5SDimitry Andric // Load ntdll.dll.
7560b57cec5SDimitry Andric /* Simple GetModuleHandle( "ntdll.dl" ) is not suitable due to security issue
7570b57cec5SDimitry Andric (see http://www.microsoft.com/technet/security/advisory/2269637.mspx). We
7580b57cec5SDimitry Andric have to specify full path to the library. */
7590b57cec5SDimitry Andric __kmp_str_buf_init(&path);
7600b57cec5SDimitry Andric path_size = GetSystemDirectory(path.str, path.size);
7610b57cec5SDimitry Andric KMP_DEBUG_ASSERT(path_size > 0);
7620b57cec5SDimitry Andric if (path_size >= path.size) {
7630b57cec5SDimitry Andric // Buffer is too short. Expand the buffer and try again.
7640b57cec5SDimitry Andric __kmp_str_buf_reserve(&path, path_size);
7650b57cec5SDimitry Andric path_size = GetSystemDirectory(path.str, path.size);
7660b57cec5SDimitry Andric KMP_DEBUG_ASSERT(path_size > 0);
7670b57cec5SDimitry Andric }
7680b57cec5SDimitry Andric if (path_size > 0 && path_size < path.size) {
7690b57cec5SDimitry Andric // Now we have system directory name in the buffer.
7700b57cec5SDimitry Andric // Append backslash and name of dll to form full path,
7710b57cec5SDimitry Andric path.used = path_size;
7720b57cec5SDimitry Andric __kmp_str_buf_print(&path, "\\%s", "ntdll.dll");
7730b57cec5SDimitry Andric
7740b57cec5SDimitry Andric // Now load ntdll using full path.
7750b57cec5SDimitry Andric ntdll = GetModuleHandle(path.str);
7760b57cec5SDimitry Andric }
7770b57cec5SDimitry Andric
7780b57cec5SDimitry Andric KMP_DEBUG_ASSERT(ntdll != NULL);
7790b57cec5SDimitry Andric if (ntdll != NULL) {
7800b57cec5SDimitry Andric NtQuerySystemInformation = (NtQuerySystemInformation_t)GetProcAddress(
7810b57cec5SDimitry Andric ntdll, "NtQuerySystemInformation");
7820b57cec5SDimitry Andric }
7830b57cec5SDimitry Andric KMP_DEBUG_ASSERT(NtQuerySystemInformation != NULL);
7840b57cec5SDimitry Andric
7850b57cec5SDimitry Andric #if KMP_GROUP_AFFINITY
7860b57cec5SDimitry Andric // Load kernel32.dll.
7870b57cec5SDimitry Andric // Same caveat - must use full system path name.
7880b57cec5SDimitry Andric if (path_size > 0 && path_size < path.size) {
7890b57cec5SDimitry Andric // Truncate the buffer back to just the system path length,
7900b57cec5SDimitry Andric // discarding "\\ntdll.dll", and replacing it with "kernel32.dll".
7910b57cec5SDimitry Andric path.used = path_size;
7920b57cec5SDimitry Andric __kmp_str_buf_print(&path, "\\%s", "kernel32.dll");
7930b57cec5SDimitry Andric
7940b57cec5SDimitry Andric // Load kernel32.dll using full path.
7950b57cec5SDimitry Andric kernel32 = GetModuleHandle(path.str);
7960b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize: kernel32.dll = %s\n", path.str));
7970b57cec5SDimitry Andric
7980b57cec5SDimitry Andric // Load the function pointers to kernel32.dll routines
7990b57cec5SDimitry Andric // that may or may not exist on this system.
8000b57cec5SDimitry Andric if (kernel32 != NULL) {
8010b57cec5SDimitry Andric __kmp_GetActiveProcessorCount =
8020b57cec5SDimitry Andric (kmp_GetActiveProcessorCount_t)GetProcAddress(
8030b57cec5SDimitry Andric kernel32, "GetActiveProcessorCount");
8040b57cec5SDimitry Andric __kmp_GetActiveProcessorGroupCount =
8050b57cec5SDimitry Andric (kmp_GetActiveProcessorGroupCount_t)GetProcAddress(
8060b57cec5SDimitry Andric kernel32, "GetActiveProcessorGroupCount");
8070b57cec5SDimitry Andric __kmp_GetThreadGroupAffinity =
8080b57cec5SDimitry Andric (kmp_GetThreadGroupAffinity_t)GetProcAddress(
8090b57cec5SDimitry Andric kernel32, "GetThreadGroupAffinity");
8100b57cec5SDimitry Andric __kmp_SetThreadGroupAffinity =
8110b57cec5SDimitry Andric (kmp_SetThreadGroupAffinity_t)GetProcAddress(
8120b57cec5SDimitry Andric kernel32, "SetThreadGroupAffinity");
8130b57cec5SDimitry Andric
8140b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_GetActiveProcessorCount"
8150b57cec5SDimitry Andric " = %p\n",
8160b57cec5SDimitry Andric __kmp_GetActiveProcessorCount));
8170b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize: "
8180b57cec5SDimitry Andric "__kmp_GetActiveProcessorGroupCount = %p\n",
8190b57cec5SDimitry Andric __kmp_GetActiveProcessorGroupCount));
8200b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity"
8210b57cec5SDimitry Andric " = %p\n",
8220b57cec5SDimitry Andric __kmp_GetThreadGroupAffinity));
8230b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity"
8240b57cec5SDimitry Andric " = %p\n",
8250b57cec5SDimitry Andric __kmp_SetThreadGroupAffinity));
8260b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n",
8270b57cec5SDimitry Andric sizeof(kmp_affin_mask_t)));
8280b57cec5SDimitry Andric
8290b57cec5SDimitry Andric // See if group affinity is supported on this system.
8300b57cec5SDimitry Andric // If so, calculate the #groups and #procs.
8310b57cec5SDimitry Andric //
8320b57cec5SDimitry Andric // Group affinity was introduced with Windows* 7 OS and
8330b57cec5SDimitry Andric // Windows* Server 2008 R2 OS.
8340b57cec5SDimitry Andric if ((__kmp_GetActiveProcessorCount != NULL) &&
8350b57cec5SDimitry Andric (__kmp_GetActiveProcessorGroupCount != NULL) &&
8360b57cec5SDimitry Andric (__kmp_GetThreadGroupAffinity != NULL) &&
8370b57cec5SDimitry Andric (__kmp_SetThreadGroupAffinity != NULL) &&
8380b57cec5SDimitry Andric ((__kmp_num_proc_groups = __kmp_GetActiveProcessorGroupCount()) >
8390b57cec5SDimitry Andric 1)) {
8400b57cec5SDimitry Andric // Calculate the total number of active OS procs.
8410b57cec5SDimitry Andric int i;
8420b57cec5SDimitry Andric
8430b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
8440b57cec5SDimitry Andric " detected\n",
8450b57cec5SDimitry Andric __kmp_num_proc_groups));
8460b57cec5SDimitry Andric
8470b57cec5SDimitry Andric __kmp_xproc = 0;
8480b57cec5SDimitry Andric
8490b57cec5SDimitry Andric for (i = 0; i < __kmp_num_proc_groups; i++) {
8500b57cec5SDimitry Andric DWORD size = __kmp_GetActiveProcessorCount(i);
8510b57cec5SDimitry Andric __kmp_xproc += size;
8520b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize: proc group %d size = %d\n",
8530b57cec5SDimitry Andric i, size));
8540b57cec5SDimitry Andric }
8550b57cec5SDimitry Andric } else {
8560b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups"
8570b57cec5SDimitry Andric " detected\n",
8580b57cec5SDimitry Andric __kmp_num_proc_groups));
8590b57cec5SDimitry Andric }
8600b57cec5SDimitry Andric }
8610b57cec5SDimitry Andric }
8620b57cec5SDimitry Andric if (__kmp_num_proc_groups <= 1) {
8630b57cec5SDimitry Andric GetSystemInfo(&info);
8640b57cec5SDimitry Andric __kmp_xproc = info.dwNumberOfProcessors;
8650b57cec5SDimitry Andric }
8660b57cec5SDimitry Andric #else
867fe6060f1SDimitry Andric (void)kernel32;
8680b57cec5SDimitry Andric GetSystemInfo(&info);
8690b57cec5SDimitry Andric __kmp_xproc = info.dwNumberOfProcessors;
8700b57cec5SDimitry Andric #endif /* KMP_GROUP_AFFINITY */
8710b57cec5SDimitry Andric
8720b57cec5SDimitry Andric // If the OS said there were 0 procs, take a guess and use a value of 2.
8730b57cec5SDimitry Andric // This is done for Linux* OS, also. Do we need error / warning?
8740b57cec5SDimitry Andric if (__kmp_xproc <= 0) {
8750b57cec5SDimitry Andric __kmp_xproc = 2;
8760b57cec5SDimitry Andric }
8770b57cec5SDimitry Andric
8780b57cec5SDimitry Andric KA_TRACE(5,
8790b57cec5SDimitry Andric ("__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc));
8800b57cec5SDimitry Andric
8810b57cec5SDimitry Andric __kmp_str_buf_free(&path);
8820b57cec5SDimitry Andric
8830b57cec5SDimitry Andric #if USE_ITT_BUILD
8840b57cec5SDimitry Andric __kmp_itt_initialize();
8850b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
8860b57cec5SDimitry Andric
8870b57cec5SDimitry Andric __kmp_init_runtime = TRUE;
8880b57cec5SDimitry Andric } // __kmp_runtime_initialize
8890b57cec5SDimitry Andric
__kmp_runtime_destroy(void)8900b57cec5SDimitry Andric void __kmp_runtime_destroy(void) {
8910b57cec5SDimitry Andric if (!__kmp_init_runtime) {
8920b57cec5SDimitry Andric return;
8930b57cec5SDimitry Andric }
8940b57cec5SDimitry Andric
8950b57cec5SDimitry Andric #if USE_ITT_BUILD
8960b57cec5SDimitry Andric __kmp_itt_destroy();
8970b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
8980b57cec5SDimitry Andric
8990b57cec5SDimitry Andric /* we can't DeleteCriticalsection( & __kmp_win32_section ); */
9000b57cec5SDimitry Andric /* due to the KX_TRACE() commands */
9010b57cec5SDimitry Andric KA_TRACE(40, ("__kmp_runtime_destroy\n"));
9020b57cec5SDimitry Andric
9030b57cec5SDimitry Andric if (__kmp_gtid_threadprivate_key) {
9040b57cec5SDimitry Andric TlsFree(__kmp_gtid_threadprivate_key);
9050b57cec5SDimitry Andric __kmp_gtid_threadprivate_key = 0;
9060b57cec5SDimitry Andric }
9070b57cec5SDimitry Andric
9080b57cec5SDimitry Andric __kmp_affinity_uninitialize();
9090b57cec5SDimitry Andric DeleteCriticalSection(&__kmp_win32_section);
9100b57cec5SDimitry Andric
9110b57cec5SDimitry Andric ntdll = NULL;
9120b57cec5SDimitry Andric NtQuerySystemInformation = NULL;
9130b57cec5SDimitry Andric
9140b57cec5SDimitry Andric #if KMP_ARCH_X86_64
9150b57cec5SDimitry Andric kernel32 = NULL;
9160b57cec5SDimitry Andric __kmp_GetActiveProcessorCount = NULL;
9170b57cec5SDimitry Andric __kmp_GetActiveProcessorGroupCount = NULL;
9180b57cec5SDimitry Andric __kmp_GetThreadGroupAffinity = NULL;
9190b57cec5SDimitry Andric __kmp_SetThreadGroupAffinity = NULL;
9200b57cec5SDimitry Andric #endif // KMP_ARCH_X86_64
9210b57cec5SDimitry Andric
9220b57cec5SDimitry Andric __kmp_init_runtime = FALSE;
9230b57cec5SDimitry Andric }
9240b57cec5SDimitry Andric
__kmp_terminate_thread(int gtid)9250b57cec5SDimitry Andric void __kmp_terminate_thread(int gtid) {
9260b57cec5SDimitry Andric kmp_info_t *th = __kmp_threads[gtid];
9270b57cec5SDimitry Andric
9280b57cec5SDimitry Andric if (!th)
9290b57cec5SDimitry Andric return;
9300b57cec5SDimitry Andric
9310b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
9320b57cec5SDimitry Andric
9330b57cec5SDimitry Andric if (TerminateThread(th->th.th_info.ds.ds_thread, (DWORD)-1) == FALSE) {
9340b57cec5SDimitry Andric /* It's OK, the thread may have exited already */
9350b57cec5SDimitry Andric }
9360b57cec5SDimitry Andric __kmp_free_handle(th->th.th_info.ds.ds_thread);
9370b57cec5SDimitry Andric }
9380b57cec5SDimitry Andric
__kmp_clear_system_time(void)9390b57cec5SDimitry Andric void __kmp_clear_system_time(void) {
9400b57cec5SDimitry Andric LARGE_INTEGER time;
94181ad6265SDimitry Andric QueryPerformanceCounter(&time);
9420b57cec5SDimitry Andric __kmp_win32_time = (kmp_int64)time.QuadPart;
9430b57cec5SDimitry Andric }
9440b57cec5SDimitry Andric
__kmp_initialize_system_tick(void)9450b57cec5SDimitry Andric void __kmp_initialize_system_tick(void) {
9460b57cec5SDimitry Andric {
9470b57cec5SDimitry Andric BOOL status;
9480b57cec5SDimitry Andric LARGE_INTEGER freq;
9490b57cec5SDimitry Andric
9500b57cec5SDimitry Andric status = QueryPerformanceFrequency(&freq);
9510b57cec5SDimitry Andric if (!status) {
9520b57cec5SDimitry Andric DWORD error = GetLastError();
9530b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(FunctionError, "QueryPerformanceFrequency()"),
9540b57cec5SDimitry Andric KMP_ERR(error), __kmp_msg_null);
9550b57cec5SDimitry Andric
9560b57cec5SDimitry Andric } else {
9570b57cec5SDimitry Andric __kmp_win32_tick = ((double)1.0) / (double)freq.QuadPart;
9580b57cec5SDimitry Andric }
9590b57cec5SDimitry Andric }
9600b57cec5SDimitry Andric }
9610b57cec5SDimitry Andric
9620b57cec5SDimitry Andric /* Calculate the elapsed wall clock time for the user */
9630b57cec5SDimitry Andric
__kmp_elapsed(double * t)9640b57cec5SDimitry Andric void __kmp_elapsed(double *t) {
9650b57cec5SDimitry Andric LARGE_INTEGER now;
96681ad6265SDimitry Andric QueryPerformanceCounter(&now);
9670b57cec5SDimitry Andric *t = ((double)now.QuadPart) * __kmp_win32_tick;
9680b57cec5SDimitry Andric }
9690b57cec5SDimitry Andric
9700b57cec5SDimitry Andric /* Calculate the elapsed wall clock tick for the user */
9710b57cec5SDimitry Andric
__kmp_elapsed_tick(double * t)9720b57cec5SDimitry Andric void __kmp_elapsed_tick(double *t) { *t = __kmp_win32_tick; }
9730b57cec5SDimitry Andric
__kmp_read_system_time(double * delta)9740b57cec5SDimitry Andric void __kmp_read_system_time(double *delta) {
9750b57cec5SDimitry Andric if (delta != NULL) {
9760b57cec5SDimitry Andric LARGE_INTEGER now;
97781ad6265SDimitry Andric QueryPerformanceCounter(&now);
9780b57cec5SDimitry Andric *delta = ((double)(((kmp_int64)now.QuadPart) - __kmp_win32_time)) *
9790b57cec5SDimitry Andric __kmp_win32_tick;
9800b57cec5SDimitry Andric }
9810b57cec5SDimitry Andric }
9820b57cec5SDimitry Andric
9830b57cec5SDimitry Andric /* Return the current time stamp in nsec */
__kmp_now_nsec()9840b57cec5SDimitry Andric kmp_uint64 __kmp_now_nsec() {
9850b57cec5SDimitry Andric LARGE_INTEGER now;
9860b57cec5SDimitry Andric QueryPerformanceCounter(&now);
9870b57cec5SDimitry Andric return 1e9 * __kmp_win32_tick * now.QuadPart;
9880b57cec5SDimitry Andric }
9890b57cec5SDimitry Andric
__kmp_launch_worker(void * arg)990fe6060f1SDimitry Andric extern "C" void *__stdcall __kmp_launch_worker(void *arg) {
9910b57cec5SDimitry Andric volatile void *stack_data;
9920b57cec5SDimitry Andric void *exit_val;
9930b57cec5SDimitry Andric void *padding = 0;
9940b57cec5SDimitry Andric kmp_info_t *this_thr = (kmp_info_t *)arg;
9950b57cec5SDimitry Andric int gtid;
9960b57cec5SDimitry Andric
9970b57cec5SDimitry Andric gtid = this_thr->th.th_info.ds.ds_gtid;
9980b57cec5SDimitry Andric __kmp_gtid_set_specific(gtid);
9990b57cec5SDimitry Andric #ifdef KMP_TDATA_GTID
10000b57cec5SDimitry Andric #error "This define causes problems with LoadLibrary() + declspec(thread) " \
10010b57cec5SDimitry Andric "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
10020b57cec5SDimitry Andric "reference: http://support.microsoft.com/kb/118816"
10030b57cec5SDimitry Andric //__kmp_gtid = gtid;
10040b57cec5SDimitry Andric #endif
10050b57cec5SDimitry Andric
10060b57cec5SDimitry Andric #if USE_ITT_BUILD
10070b57cec5SDimitry Andric __kmp_itt_thread_name(gtid);
10080b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
10090b57cec5SDimitry Andric
10105f757f3fSDimitry Andric __kmp_affinity_bind_init_mask(gtid);
10110b57cec5SDimitry Andric
10120b57cec5SDimitry Andric #if KMP_ARCH_X86 || KMP_ARCH_X86_64
10130b57cec5SDimitry Andric // Set FP control regs to be a copy of the parallel initialization thread's.
10140b57cec5SDimitry Andric __kmp_clear_x87_fpu_status_word();
10150b57cec5SDimitry Andric __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
10160b57cec5SDimitry Andric __kmp_load_mxcsr(&__kmp_init_mxcsr);
10170b57cec5SDimitry Andric #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
10180b57cec5SDimitry Andric
10190b57cec5SDimitry Andric if (__kmp_stkoffset > 0 && gtid > 0) {
10200b57cec5SDimitry Andric padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
102181ad6265SDimitry Andric (void)padding;
10220b57cec5SDimitry Andric }
10230b57cec5SDimitry Andric
10240b57cec5SDimitry Andric KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
10250b57cec5SDimitry Andric this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
10260b57cec5SDimitry Andric TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
10270b57cec5SDimitry Andric
10280b57cec5SDimitry Andric if (TCR_4(__kmp_gtid_mode) <
10290b57cec5SDimitry Andric 2) { // check stack only if it is used to get gtid
10300b57cec5SDimitry Andric TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data);
10310b57cec5SDimitry Andric KMP_ASSERT(this_thr->th.th_info.ds.ds_stackgrow == FALSE);
10320b57cec5SDimitry Andric __kmp_check_stack_overlap(this_thr);
10330b57cec5SDimitry Andric }
10340b57cec5SDimitry Andric KMP_MB();
10350b57cec5SDimitry Andric exit_val = __kmp_launch_thread(this_thr);
10360b57cec5SDimitry Andric KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive);
10370b57cec5SDimitry Andric TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
10380b57cec5SDimitry Andric KMP_MB();
10390b57cec5SDimitry Andric return exit_val;
10400b57cec5SDimitry Andric }
10410b57cec5SDimitry Andric
10420b57cec5SDimitry Andric #if KMP_USE_MONITOR
10430b57cec5SDimitry Andric /* The monitor thread controls all of the threads in the complex */
10440b57cec5SDimitry Andric
__kmp_launch_monitor(void * arg)10450b57cec5SDimitry Andric void *__stdcall __kmp_launch_monitor(void *arg) {
10460b57cec5SDimitry Andric DWORD wait_status;
10470b57cec5SDimitry Andric kmp_thread_t monitor;
10480b57cec5SDimitry Andric int status;
10490b57cec5SDimitry Andric int interval;
10500b57cec5SDimitry Andric kmp_info_t *this_thr = (kmp_info_t *)arg;
10510b57cec5SDimitry Andric
10520b57cec5SDimitry Andric KMP_DEBUG_ASSERT(__kmp_init_monitor);
10530b57cec5SDimitry Andric TCW_4(__kmp_init_monitor, 2); // AC: Signal library that monitor has started
10540b57cec5SDimitry Andric // TODO: hide "2" in enum (like {true,false,started})
10550b57cec5SDimitry Andric this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
10560b57cec5SDimitry Andric TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE);
10570b57cec5SDimitry Andric
10580b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
10590b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_launch_monitor: launched\n"));
10600b57cec5SDimitry Andric
10610b57cec5SDimitry Andric monitor = GetCurrentThread();
10620b57cec5SDimitry Andric
10630b57cec5SDimitry Andric /* set thread priority */
10640b57cec5SDimitry Andric status = SetThreadPriority(monitor, THREAD_PRIORITY_HIGHEST);
10650b57cec5SDimitry Andric if (!status) {
10660b57cec5SDimitry Andric DWORD error = GetLastError();
10670b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
10680b57cec5SDimitry Andric }
10690b57cec5SDimitry Andric
10700b57cec5SDimitry Andric /* register us as monitor */
10710b57cec5SDimitry Andric __kmp_gtid_set_specific(KMP_GTID_MONITOR);
10720b57cec5SDimitry Andric #ifdef KMP_TDATA_GTID
10730b57cec5SDimitry Andric #error "This define causes problems with LoadLibrary() + declspec(thread) " \
10740b57cec5SDimitry Andric "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \
10750b57cec5SDimitry Andric "reference: http://support.microsoft.com/kb/118816"
10760b57cec5SDimitry Andric //__kmp_gtid = KMP_GTID_MONITOR;
10770b57cec5SDimitry Andric #endif
10780b57cec5SDimitry Andric
10790b57cec5SDimitry Andric #if USE_ITT_BUILD
10800b57cec5SDimitry Andric __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore
10810b57cec5SDimitry Andric // monitor thread.
10820b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
10830b57cec5SDimitry Andric
10840b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
10850b57cec5SDimitry Andric
10860b57cec5SDimitry Andric interval = (1000 / __kmp_monitor_wakeups); /* in milliseconds */
10870b57cec5SDimitry Andric
10880b57cec5SDimitry Andric while (!TCR_4(__kmp_global.g.g_done)) {
10890b57cec5SDimitry Andric /* This thread monitors the state of the system */
10900b57cec5SDimitry Andric
10910b57cec5SDimitry Andric KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
10920b57cec5SDimitry Andric
10930b57cec5SDimitry Andric wait_status = WaitForSingleObject(__kmp_monitor_ev, interval);
10940b57cec5SDimitry Andric
10950b57cec5SDimitry Andric if (wait_status == WAIT_TIMEOUT) {
10960b57cec5SDimitry Andric TCW_4(__kmp_global.g.g_time.dt.t_value,
10970b57cec5SDimitry Andric TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
10980b57cec5SDimitry Andric }
10990b57cec5SDimitry Andric
11000b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
11010b57cec5SDimitry Andric }
11020b57cec5SDimitry Andric
11030b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_launch_monitor: finished\n"));
11040b57cec5SDimitry Andric
11050b57cec5SDimitry Andric status = SetThreadPriority(monitor, THREAD_PRIORITY_NORMAL);
11060b57cec5SDimitry Andric if (!status) {
11070b57cec5SDimitry Andric DWORD error = GetLastError();
11080b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null);
11090b57cec5SDimitry Andric }
11100b57cec5SDimitry Andric
11110b57cec5SDimitry Andric if (__kmp_global.g.g_abort != 0) {
11120b57cec5SDimitry Andric /* now we need to terminate the worker threads */
11130b57cec5SDimitry Andric /* the value of t_abort is the signal we caught */
11140b57cec5SDimitry Andric int gtid;
11150b57cec5SDimitry Andric
11160b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_launch_monitor: terminate sig=%d\n",
11170b57cec5SDimitry Andric (__kmp_global.g.g_abort)));
11180b57cec5SDimitry Andric
11190b57cec5SDimitry Andric /* terminate the OpenMP worker threads */
11200b57cec5SDimitry Andric /* TODO this is not valid for sibling threads!!
11210b57cec5SDimitry Andric * the uber master might not be 0 anymore.. */
11220b57cec5SDimitry Andric for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
11230b57cec5SDimitry Andric __kmp_terminate_thread(gtid);
11240b57cec5SDimitry Andric
11250b57cec5SDimitry Andric __kmp_cleanup();
11260b57cec5SDimitry Andric
11270b57cec5SDimitry Andric Sleep(0);
11280b57cec5SDimitry Andric
11290b57cec5SDimitry Andric KA_TRACE(10,
11300b57cec5SDimitry Andric ("__kmp_launch_monitor: raise sig=%d\n", __kmp_global.g.g_abort));
11310b57cec5SDimitry Andric
11320b57cec5SDimitry Andric if (__kmp_global.g.g_abort > 0) {
11330b57cec5SDimitry Andric raise(__kmp_global.g.g_abort);
11340b57cec5SDimitry Andric }
11350b57cec5SDimitry Andric }
11360b57cec5SDimitry Andric
11370b57cec5SDimitry Andric TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE);
11380b57cec5SDimitry Andric
11390b57cec5SDimitry Andric KMP_MB();
11400b57cec5SDimitry Andric return arg;
11410b57cec5SDimitry Andric }
11420b57cec5SDimitry Andric #endif
11430b57cec5SDimitry Andric
__kmp_create_worker(int gtid,kmp_info_t * th,size_t stack_size)11440b57cec5SDimitry Andric void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
11450b57cec5SDimitry Andric kmp_thread_t handle;
11460b57cec5SDimitry Andric DWORD idThread;
11470b57cec5SDimitry Andric
11480b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
11490b57cec5SDimitry Andric
11500b57cec5SDimitry Andric th->th.th_info.ds.ds_gtid = gtid;
11510b57cec5SDimitry Andric
11520b57cec5SDimitry Andric if (KMP_UBER_GTID(gtid)) {
11530b57cec5SDimitry Andric int stack_data;
11540b57cec5SDimitry Andric
11550b57cec5SDimitry Andric /* TODO: GetCurrentThread() returns a pseudo-handle that is unsuitable for
11560b57cec5SDimitry Andric other threads to use. Is it appropriate to just use GetCurrentThread?
11570b57cec5SDimitry Andric When should we close this handle? When unregistering the root? */
11580b57cec5SDimitry Andric {
11590b57cec5SDimitry Andric BOOL rc;
11600b57cec5SDimitry Andric rc = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
11610b57cec5SDimitry Andric GetCurrentProcess(), &th->th.th_info.ds.ds_thread, 0,
11620b57cec5SDimitry Andric FALSE, DUPLICATE_SAME_ACCESS);
11630b57cec5SDimitry Andric KMP_ASSERT(rc);
11640b57cec5SDimitry Andric KA_TRACE(10, (" __kmp_create_worker: ROOT Handle duplicated, th = %p, "
11650b57cec5SDimitry Andric "handle = %" KMP_UINTPTR_SPEC "\n",
11660b57cec5SDimitry Andric (LPVOID)th, th->th.th_info.ds.ds_thread));
11670b57cec5SDimitry Andric th->th.th_info.ds.ds_thread_id = GetCurrentThreadId();
11680b57cec5SDimitry Andric }
11690b57cec5SDimitry Andric if (TCR_4(__kmp_gtid_mode) < 2) { // check stack only if used to get gtid
11700b57cec5SDimitry Andric /* we will dynamically update the stack range if gtid_mode == 1 */
11710b57cec5SDimitry Andric TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
11720b57cec5SDimitry Andric TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
11730b57cec5SDimitry Andric TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
11740b57cec5SDimitry Andric __kmp_check_stack_overlap(th);
11750b57cec5SDimitry Andric }
11760b57cec5SDimitry Andric } else {
11770b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
11780b57cec5SDimitry Andric
11790b57cec5SDimitry Andric /* Set stack size for this thread now. */
11800b57cec5SDimitry Andric KA_TRACE(10,
11810b57cec5SDimitry Andric ("__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC " bytes\n",
11820b57cec5SDimitry Andric stack_size));
11830b57cec5SDimitry Andric
11840b57cec5SDimitry Andric stack_size += gtid * __kmp_stkoffset;
11850b57cec5SDimitry Andric
11860b57cec5SDimitry Andric TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size);
11870b57cec5SDimitry Andric TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
11880b57cec5SDimitry Andric
11890b57cec5SDimitry Andric KA_TRACE(10,
11900b57cec5SDimitry Andric ("__kmp_create_worker: (before) stack_size = %" KMP_SIZE_T_SPEC
11910b57cec5SDimitry Andric " bytes, &__kmp_launch_worker = %p, th = %p, &idThread = %p\n",
11920b57cec5SDimitry Andric (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
11930b57cec5SDimitry Andric (LPVOID)th, &idThread));
11940b57cec5SDimitry Andric
11950b57cec5SDimitry Andric handle = CreateThread(
11960b57cec5SDimitry Andric NULL, (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)__kmp_launch_worker,
11970b57cec5SDimitry Andric (LPVOID)th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
11980b57cec5SDimitry Andric
11990b57cec5SDimitry Andric KA_TRACE(10,
12000b57cec5SDimitry Andric ("__kmp_create_worker: (after) stack_size = %" KMP_SIZE_T_SPEC
12010b57cec5SDimitry Andric " bytes, &__kmp_launch_worker = %p, th = %p, "
12020b57cec5SDimitry Andric "idThread = %u, handle = %" KMP_UINTPTR_SPEC "\n",
12030b57cec5SDimitry Andric (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker,
12040b57cec5SDimitry Andric (LPVOID)th, idThread, handle));
12050b57cec5SDimitry Andric
12060b57cec5SDimitry Andric if (handle == 0) {
12070b57cec5SDimitry Andric DWORD error = GetLastError();
12080b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
12090b57cec5SDimitry Andric } else {
12100b57cec5SDimitry Andric th->th.th_info.ds.ds_thread = handle;
12110b57cec5SDimitry Andric }
12120b57cec5SDimitry Andric
12130b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
12140b57cec5SDimitry Andric }
12150b57cec5SDimitry Andric
12160b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
12170b57cec5SDimitry Andric }
12180b57cec5SDimitry Andric
__kmp_still_running(kmp_info_t * th)12190b57cec5SDimitry Andric int __kmp_still_running(kmp_info_t *th) {
12200b57cec5SDimitry Andric return (WAIT_TIMEOUT == WaitForSingleObject(th->th.th_info.ds.ds_thread, 0));
12210b57cec5SDimitry Andric }
12220b57cec5SDimitry Andric
12230b57cec5SDimitry Andric #if KMP_USE_MONITOR
__kmp_create_monitor(kmp_info_t * th)12240b57cec5SDimitry Andric void __kmp_create_monitor(kmp_info_t *th) {
12250b57cec5SDimitry Andric kmp_thread_t handle;
12260b57cec5SDimitry Andric DWORD idThread;
12270b57cec5SDimitry Andric int ideal, new_ideal;
12280b57cec5SDimitry Andric
12290b57cec5SDimitry Andric if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
12300b57cec5SDimitry Andric // We don't need monitor thread in case of MAX_BLOCKTIME
12310b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
12320b57cec5SDimitry Andric "MAX blocktime\n"));
12330b57cec5SDimitry Andric th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
12340b57cec5SDimitry Andric th->th.th_info.ds.ds_gtid = 0;
12350b57cec5SDimitry Andric TCW_4(__kmp_init_monitor, 2); // Signal to stop waiting for monitor creation
12360b57cec5SDimitry Andric return;
12370b57cec5SDimitry Andric }
12380b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
12390b57cec5SDimitry Andric
12400b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
12410b57cec5SDimitry Andric
12420b57cec5SDimitry Andric __kmp_monitor_ev = CreateEvent(NULL, TRUE, FALSE, NULL);
12430b57cec5SDimitry Andric if (__kmp_monitor_ev == NULL) {
12440b57cec5SDimitry Andric DWORD error = GetLastError();
12450b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(CantCreateEvent), KMP_ERR(error), __kmp_msg_null);
12460b57cec5SDimitry Andric }
12470b57cec5SDimitry Andric #if USE_ITT_BUILD
12480b57cec5SDimitry Andric __kmp_itt_system_object_created(__kmp_monitor_ev, "Event");
12490b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
12500b57cec5SDimitry Andric
12510b57cec5SDimitry Andric th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
12520b57cec5SDimitry Andric th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
12530b57cec5SDimitry Andric
12540b57cec5SDimitry Andric // FIXME - on Windows* OS, if __kmp_monitor_stksize = 0, figure out how
12550b57cec5SDimitry Andric // to automatically expand stacksize based on CreateThread error code.
12560b57cec5SDimitry Andric if (__kmp_monitor_stksize == 0) {
12570b57cec5SDimitry Andric __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
12580b57cec5SDimitry Andric }
12590b57cec5SDimitry Andric if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
12600b57cec5SDimitry Andric __kmp_monitor_stksize = __kmp_sys_min_stksize;
12610b57cec5SDimitry Andric }
12620b57cec5SDimitry Andric
12630b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_create_monitor: requested stacksize = %d bytes\n",
12640b57cec5SDimitry Andric (int)__kmp_monitor_stksize));
12650b57cec5SDimitry Andric
12660b57cec5SDimitry Andric TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
12670b57cec5SDimitry Andric
12680b57cec5SDimitry Andric handle =
12690b57cec5SDimitry Andric CreateThread(NULL, (SIZE_T)__kmp_monitor_stksize,
12700b57cec5SDimitry Andric (LPTHREAD_START_ROUTINE)__kmp_launch_monitor, (LPVOID)th,
12710b57cec5SDimitry Andric STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread);
12720b57cec5SDimitry Andric if (handle == 0) {
12730b57cec5SDimitry Andric DWORD error = GetLastError();
12740b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null);
12750b57cec5SDimitry Andric } else
12760b57cec5SDimitry Andric th->th.th_info.ds.ds_thread = handle;
12770b57cec5SDimitry Andric
12780b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
12790b57cec5SDimitry Andric
12800b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_create_monitor: monitor created %p\n",
12810b57cec5SDimitry Andric (void *)th->th.th_info.ds.ds_thread));
12820b57cec5SDimitry Andric }
12830b57cec5SDimitry Andric #endif
12840b57cec5SDimitry Andric
12850b57cec5SDimitry Andric /* Check to see if thread is still alive.
12860b57cec5SDimitry Andric NOTE: The ExitProcess(code) system call causes all threads to Terminate
12870b57cec5SDimitry Andric with a exit_val = code. Because of this we can not rely on exit_val having
12880b57cec5SDimitry Andric any particular value. So this routine may return STILL_ALIVE in exit_val
12890b57cec5SDimitry Andric even after the thread is dead. */
12900b57cec5SDimitry Andric
__kmp_is_thread_alive(kmp_info_t * th,DWORD * exit_val)12910b57cec5SDimitry Andric int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val) {
12920b57cec5SDimitry Andric DWORD rc;
12930b57cec5SDimitry Andric rc = GetExitCodeThread(th->th.th_info.ds.ds_thread, exit_val);
12940b57cec5SDimitry Andric if (rc == 0) {
12950b57cec5SDimitry Andric DWORD error = GetLastError();
12960b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(FunctionError, "GetExitCodeThread()"), KMP_ERR(error),
12970b57cec5SDimitry Andric __kmp_msg_null);
12980b57cec5SDimitry Andric }
12990b57cec5SDimitry Andric return (*exit_val == STILL_ACTIVE);
13000b57cec5SDimitry Andric }
13010b57cec5SDimitry Andric
__kmp_exit_thread(int exit_status)13020b57cec5SDimitry Andric void __kmp_exit_thread(int exit_status) {
13030b57cec5SDimitry Andric ExitThread(exit_status);
13040b57cec5SDimitry Andric } // __kmp_exit_thread
13050b57cec5SDimitry Andric
13060b57cec5SDimitry Andric // This is a common part for both __kmp_reap_worker() and __kmp_reap_monitor().
__kmp_reap_common(kmp_info_t * th)13070b57cec5SDimitry Andric static void __kmp_reap_common(kmp_info_t *th) {
13080b57cec5SDimitry Andric DWORD exit_val;
13090b57cec5SDimitry Andric
13100b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
13110b57cec5SDimitry Andric
13120b57cec5SDimitry Andric KA_TRACE(
13130b57cec5SDimitry Andric 10, ("__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid));
13140b57cec5SDimitry Andric
13150b57cec5SDimitry Andric /* 2006-10-19:
13160b57cec5SDimitry Andric There are two opposite situations:
13170b57cec5SDimitry Andric 1. Windows* OS keep thread alive after it resets ds_alive flag and
13180b57cec5SDimitry Andric exits from thread function. (For example, see C70770/Q394281 "unloading of
13190b57cec5SDimitry Andric dll based on OMP is very slow".)
13200b57cec5SDimitry Andric 2. Windows* OS may kill thread before it resets ds_alive flag.
13210b57cec5SDimitry Andric
13220b57cec5SDimitry Andric Right solution seems to be waiting for *either* thread termination *or*
13230b57cec5SDimitry Andric ds_alive resetting. */
13240b57cec5SDimitry Andric {
13250b57cec5SDimitry Andric // TODO: This code is very similar to KMP_WAIT. Need to generalize
13260b57cec5SDimitry Andric // KMP_WAIT to cover this usage also.
13270b57cec5SDimitry Andric void *obj = NULL;
13280b57cec5SDimitry Andric kmp_uint32 spins;
132904eeddc0SDimitry Andric kmp_uint64 time;
13300b57cec5SDimitry Andric #if USE_ITT_BUILD
13310b57cec5SDimitry Andric KMP_FSYNC_SPIN_INIT(obj, (void *)&th->th.th_info.ds.ds_alive);
13320b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
13330b57cec5SDimitry Andric KMP_INIT_YIELD(spins);
133404eeddc0SDimitry Andric KMP_INIT_BACKOFF(time);
13350b57cec5SDimitry Andric do {
13360b57cec5SDimitry Andric #if USE_ITT_BUILD
13370b57cec5SDimitry Andric KMP_FSYNC_SPIN_PREPARE(obj);
13380b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
13390b57cec5SDimitry Andric __kmp_is_thread_alive(th, &exit_val);
134004eeddc0SDimitry Andric KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
13410b57cec5SDimitry Andric } while (exit_val == STILL_ACTIVE && TCR_4(th->th.th_info.ds.ds_alive));
13420b57cec5SDimitry Andric #if USE_ITT_BUILD
13430b57cec5SDimitry Andric if (exit_val == STILL_ACTIVE) {
13440b57cec5SDimitry Andric KMP_FSYNC_CANCEL(obj);
13450b57cec5SDimitry Andric } else {
13460b57cec5SDimitry Andric KMP_FSYNC_SPIN_ACQUIRED(obj);
13470b57cec5SDimitry Andric }
13480b57cec5SDimitry Andric #endif /* USE_ITT_BUILD */
13490b57cec5SDimitry Andric }
13500b57cec5SDimitry Andric
13510b57cec5SDimitry Andric __kmp_free_handle(th->th.th_info.ds.ds_thread);
13520b57cec5SDimitry Andric
13530b57cec5SDimitry Andric /* NOTE: The ExitProcess(code) system call causes all threads to Terminate
13540b57cec5SDimitry Andric with a exit_val = code. Because of this we can not rely on exit_val having
13550b57cec5SDimitry Andric any particular value. */
135681ad6265SDimitry Andric kmp_intptr_t e = (kmp_intptr_t)exit_val;
13570b57cec5SDimitry Andric if (exit_val == STILL_ACTIVE) {
13580b57cec5SDimitry Andric KA_TRACE(1, ("__kmp_reap_common: thread still active.\n"));
135981ad6265SDimitry Andric } else if ((void *)e != (void *)th) {
13600b57cec5SDimitry Andric KA_TRACE(1, ("__kmp_reap_common: ExitProcess / TerminateThread used?\n"));
13610b57cec5SDimitry Andric }
13620b57cec5SDimitry Andric
13630b57cec5SDimitry Andric KA_TRACE(10,
13640b57cec5SDimitry Andric ("__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC
13650b57cec5SDimitry Andric "\n",
13660b57cec5SDimitry Andric th->th.th_info.ds.ds_gtid, th->th.th_info.ds.ds_thread));
13670b57cec5SDimitry Andric
13680b57cec5SDimitry Andric th->th.th_info.ds.ds_thread = 0;
13690b57cec5SDimitry Andric th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
13700b57cec5SDimitry Andric th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
13710b57cec5SDimitry Andric th->th.th_info.ds.ds_thread_id = 0;
13720b57cec5SDimitry Andric
13730b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
13740b57cec5SDimitry Andric }
13750b57cec5SDimitry Andric
13760b57cec5SDimitry Andric #if KMP_USE_MONITOR
__kmp_reap_monitor(kmp_info_t * th)13770b57cec5SDimitry Andric void __kmp_reap_monitor(kmp_info_t *th) {
13780b57cec5SDimitry Andric int status;
13790b57cec5SDimitry Andric
13800b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_reap_monitor: try to reap %p\n",
13810b57cec5SDimitry Andric (void *)th->th.th_info.ds.ds_thread));
13820b57cec5SDimitry Andric
13830b57cec5SDimitry Andric // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
13840b57cec5SDimitry Andric // If both tid and gtid are 0, it means the monitor did not ever start.
13850b57cec5SDimitry Andric // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
13860b57cec5SDimitry Andric KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
13870b57cec5SDimitry Andric if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
13880b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
13890b57cec5SDimitry Andric return;
13900b57cec5SDimitry Andric }
13910b57cec5SDimitry Andric
13920b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
13930b57cec5SDimitry Andric
13940b57cec5SDimitry Andric status = SetEvent(__kmp_monitor_ev);
13950b57cec5SDimitry Andric if (status == FALSE) {
13960b57cec5SDimitry Andric DWORD error = GetLastError();
13970b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(CantSetEvent), KMP_ERR(error), __kmp_msg_null);
13980b57cec5SDimitry Andric }
13990b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_reap_monitor: reaping thread (%d)\n",
14000b57cec5SDimitry Andric th->th.th_info.ds.ds_gtid));
14010b57cec5SDimitry Andric __kmp_reap_common(th);
14020b57cec5SDimitry Andric
14030b57cec5SDimitry Andric __kmp_free_handle(__kmp_monitor_ev);
14040b57cec5SDimitry Andric
14050b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
14060b57cec5SDimitry Andric }
14070b57cec5SDimitry Andric #endif
14080b57cec5SDimitry Andric
__kmp_reap_worker(kmp_info_t * th)14090b57cec5SDimitry Andric void __kmp_reap_worker(kmp_info_t *th) {
14100b57cec5SDimitry Andric KA_TRACE(10, ("__kmp_reap_worker: reaping thread (%d)\n",
14110b57cec5SDimitry Andric th->th.th_info.ds.ds_gtid));
14120b57cec5SDimitry Andric __kmp_reap_common(th);
14130b57cec5SDimitry Andric }
14140b57cec5SDimitry Andric
14150b57cec5SDimitry Andric #if KMP_HANDLE_SIGNALS
14160b57cec5SDimitry Andric
__kmp_team_handler(int signo)14170b57cec5SDimitry Andric static void __kmp_team_handler(int signo) {
14180b57cec5SDimitry Andric if (__kmp_global.g.g_abort == 0) {
14190b57cec5SDimitry Andric // Stage 1 signal handler, let's shut down all of the threads.
14200b57cec5SDimitry Andric if (__kmp_debug_buf) {
14210b57cec5SDimitry Andric __kmp_dump_debug_buffer();
14220b57cec5SDimitry Andric }
14230b57cec5SDimitry Andric KMP_MB(); // Flush all pending memory write invalidates.
14240b57cec5SDimitry Andric TCW_4(__kmp_global.g.g_abort, signo);
14250b57cec5SDimitry Andric KMP_MB(); // Flush all pending memory write invalidates.
14260b57cec5SDimitry Andric TCW_4(__kmp_global.g.g_done, TRUE);
14270b57cec5SDimitry Andric KMP_MB(); // Flush all pending memory write invalidates.
14280b57cec5SDimitry Andric }
14290b57cec5SDimitry Andric } // __kmp_team_handler
14300b57cec5SDimitry Andric
__kmp_signal(int signum,sig_func_t handler)14310b57cec5SDimitry Andric static sig_func_t __kmp_signal(int signum, sig_func_t handler) {
14320b57cec5SDimitry Andric sig_func_t old = signal(signum, handler);
14330b57cec5SDimitry Andric if (old == SIG_ERR) {
14340b57cec5SDimitry Andric int error = errno;
14350b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(FunctionError, "signal"), KMP_ERR(error),
14360b57cec5SDimitry Andric __kmp_msg_null);
14370b57cec5SDimitry Andric }
14380b57cec5SDimitry Andric return old;
14390b57cec5SDimitry Andric }
14400b57cec5SDimitry Andric
__kmp_install_one_handler(int sig,sig_func_t handler,int parallel_init)14410b57cec5SDimitry Andric static void __kmp_install_one_handler(int sig, sig_func_t handler,
14420b57cec5SDimitry Andric int parallel_init) {
14430b57cec5SDimitry Andric sig_func_t old;
14440b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
14450b57cec5SDimitry Andric KB_TRACE(60, ("__kmp_install_one_handler: called: sig=%d\n", sig));
14460b57cec5SDimitry Andric if (parallel_init) {
14470b57cec5SDimitry Andric old = __kmp_signal(sig, handler);
14480b57cec5SDimitry Andric // SIG_DFL on Windows* OS in NULL or 0.
14490b57cec5SDimitry Andric if (old == __kmp_sighldrs[sig]) {
14500b57cec5SDimitry Andric __kmp_siginstalled[sig] = 1;
14510b57cec5SDimitry Andric } else { // Restore/keep user's handler if one previously installed.
14520b57cec5SDimitry Andric old = __kmp_signal(sig, old);
14530b57cec5SDimitry Andric }
14540b57cec5SDimitry Andric } else {
14550b57cec5SDimitry Andric // Save initial/system signal handlers to see if user handlers installed.
14560b57cec5SDimitry Andric // 2009-09-23: It is a dead code. On Windows* OS __kmp_install_signals
14570b57cec5SDimitry Andric // called once with parallel_init == TRUE.
14580b57cec5SDimitry Andric old = __kmp_signal(sig, SIG_DFL);
14590b57cec5SDimitry Andric __kmp_sighldrs[sig] = old;
14600b57cec5SDimitry Andric __kmp_signal(sig, old);
14610b57cec5SDimitry Andric }
14620b57cec5SDimitry Andric KMP_MB(); /* Flush all pending memory write invalidates. */
14630b57cec5SDimitry Andric } // __kmp_install_one_handler
14640b57cec5SDimitry Andric
__kmp_remove_one_handler(int sig)14650b57cec5SDimitry Andric static void __kmp_remove_one_handler(int sig) {
14660b57cec5SDimitry Andric if (__kmp_siginstalled[sig]) {
14670b57cec5SDimitry Andric sig_func_t old;
14680b57cec5SDimitry Andric KMP_MB(); // Flush all pending memory write invalidates.
14690b57cec5SDimitry Andric KB_TRACE(60, ("__kmp_remove_one_handler: called: sig=%d\n", sig));
14700b57cec5SDimitry Andric old = __kmp_signal(sig, __kmp_sighldrs[sig]);
14710b57cec5SDimitry Andric if (old != __kmp_team_handler) {
14720b57cec5SDimitry Andric KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
14730b57cec5SDimitry Andric "restoring: sig=%d\n",
14740b57cec5SDimitry Andric sig));
14750b57cec5SDimitry Andric old = __kmp_signal(sig, old);
14760b57cec5SDimitry Andric }
14770b57cec5SDimitry Andric __kmp_sighldrs[sig] = NULL;
14780b57cec5SDimitry Andric __kmp_siginstalled[sig] = 0;
14790b57cec5SDimitry Andric KMP_MB(); // Flush all pending memory write invalidates.
14800b57cec5SDimitry Andric }
14810b57cec5SDimitry Andric } // __kmp_remove_one_handler
14820b57cec5SDimitry Andric
__kmp_install_signals(int parallel_init)14830b57cec5SDimitry Andric void __kmp_install_signals(int parallel_init) {
14840b57cec5SDimitry Andric KB_TRACE(10, ("__kmp_install_signals: called\n"));
14850b57cec5SDimitry Andric if (!__kmp_handle_signals) {
14860b57cec5SDimitry Andric KB_TRACE(10, ("__kmp_install_signals: KMP_HANDLE_SIGNALS is false - "
14870b57cec5SDimitry Andric "handlers not installed\n"));
14880b57cec5SDimitry Andric return;
14890b57cec5SDimitry Andric }
14900b57cec5SDimitry Andric __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
14910b57cec5SDimitry Andric __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
14920b57cec5SDimitry Andric __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
14930b57cec5SDimitry Andric __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
14940b57cec5SDimitry Andric __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
14950b57cec5SDimitry Andric __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
14960b57cec5SDimitry Andric } // __kmp_install_signals
14970b57cec5SDimitry Andric
__kmp_remove_signals(void)14980b57cec5SDimitry Andric void __kmp_remove_signals(void) {
14990b57cec5SDimitry Andric int sig;
15000b57cec5SDimitry Andric KB_TRACE(10, ("__kmp_remove_signals: called\n"));
15010b57cec5SDimitry Andric for (sig = 1; sig < NSIG; ++sig) {
15020b57cec5SDimitry Andric __kmp_remove_one_handler(sig);
15030b57cec5SDimitry Andric }
15040b57cec5SDimitry Andric } // __kmp_remove_signals
15050b57cec5SDimitry Andric
15060b57cec5SDimitry Andric #endif // KMP_HANDLE_SIGNALS
15070b57cec5SDimitry Andric
15080b57cec5SDimitry Andric /* Put the thread to sleep for a time period */
__kmp_thread_sleep(int millis)15090b57cec5SDimitry Andric void __kmp_thread_sleep(int millis) {
15100b57cec5SDimitry Andric DWORD status;
15110b57cec5SDimitry Andric
15120b57cec5SDimitry Andric status = SleepEx((DWORD)millis, FALSE);
15130b57cec5SDimitry Andric if (status) {
15140b57cec5SDimitry Andric DWORD error = GetLastError();
15150b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(FunctionError, "SleepEx()"), KMP_ERR(error),
15160b57cec5SDimitry Andric __kmp_msg_null);
15170b57cec5SDimitry Andric }
15180b57cec5SDimitry Andric }
15190b57cec5SDimitry Andric
15200b57cec5SDimitry Andric // Determine whether the given address is mapped into the current address space.
__kmp_is_address_mapped(void * addr)15210b57cec5SDimitry Andric int __kmp_is_address_mapped(void *addr) {
15220b57cec5SDimitry Andric MEMORY_BASIC_INFORMATION lpBuffer;
15230b57cec5SDimitry Andric SIZE_T dwLength;
15240b57cec5SDimitry Andric
15250b57cec5SDimitry Andric dwLength = sizeof(MEMORY_BASIC_INFORMATION);
15260b57cec5SDimitry Andric
152781ad6265SDimitry Andric VirtualQuery(addr, &lpBuffer, dwLength);
15280b57cec5SDimitry Andric
15290b57cec5SDimitry Andric return !(((lpBuffer.State == MEM_RESERVE) || (lpBuffer.State == MEM_FREE)) ||
15300b57cec5SDimitry Andric ((lpBuffer.Protect == PAGE_NOACCESS) ||
15310b57cec5SDimitry Andric (lpBuffer.Protect == PAGE_EXECUTE)));
15320b57cec5SDimitry Andric }
15330b57cec5SDimitry Andric
__kmp_hardware_timestamp(void)15340b57cec5SDimitry Andric kmp_uint64 __kmp_hardware_timestamp(void) {
15350b57cec5SDimitry Andric kmp_uint64 r = 0;
15360b57cec5SDimitry Andric
15370b57cec5SDimitry Andric QueryPerformanceCounter((LARGE_INTEGER *)&r);
15380b57cec5SDimitry Andric return r;
15390b57cec5SDimitry Andric }
15400b57cec5SDimitry Andric
15410b57cec5SDimitry Andric /* Free handle and check the error code */
__kmp_free_handle(kmp_thread_t tHandle)15420b57cec5SDimitry Andric void __kmp_free_handle(kmp_thread_t tHandle) {
15430b57cec5SDimitry Andric /* called with parameter type HANDLE also, thus suppose kmp_thread_t defined
15440b57cec5SDimitry Andric * as HANDLE */
15450b57cec5SDimitry Andric BOOL rc;
15460b57cec5SDimitry Andric rc = CloseHandle(tHandle);
15470b57cec5SDimitry Andric if (!rc) {
15480b57cec5SDimitry Andric DWORD error = GetLastError();
15490b57cec5SDimitry Andric __kmp_fatal(KMP_MSG(CantCloseHandle), KMP_ERR(error), __kmp_msg_null);
15500b57cec5SDimitry Andric }
15510b57cec5SDimitry Andric }
15520b57cec5SDimitry Andric
__kmp_get_load_balance(int max)15530b57cec5SDimitry Andric int __kmp_get_load_balance(int max) {
15540b57cec5SDimitry Andric static ULONG glb_buff_size = 100 * 1024;
15550b57cec5SDimitry Andric
1556480093f4SDimitry Andric // Saved count of the running threads for the thread balance algorithm
15570b57cec5SDimitry Andric static int glb_running_threads = 0;
15580b57cec5SDimitry Andric static double glb_call_time = 0; /* Thread balance algorithm call time */
15590b57cec5SDimitry Andric
15600b57cec5SDimitry Andric int running_threads = 0; // Number of running threads in the system.
15610b57cec5SDimitry Andric NTSTATUS status = 0;
15620b57cec5SDimitry Andric ULONG buff_size = 0;
15630b57cec5SDimitry Andric ULONG info_size = 0;
15640b57cec5SDimitry Andric void *buffer = NULL;
15650b57cec5SDimitry Andric PSYSTEM_PROCESS_INFORMATION spi = NULL;
15660b57cec5SDimitry Andric int first_time = 1;
15670b57cec5SDimitry Andric
15680b57cec5SDimitry Andric double call_time = 0.0; // start, finish;
15690b57cec5SDimitry Andric
15700b57cec5SDimitry Andric __kmp_elapsed(&call_time);
15710b57cec5SDimitry Andric
15720b57cec5SDimitry Andric if (glb_call_time &&
15730b57cec5SDimitry Andric (call_time - glb_call_time < __kmp_load_balance_interval)) {
15740b57cec5SDimitry Andric running_threads = glb_running_threads;
15750b57cec5SDimitry Andric goto finish;
15760b57cec5SDimitry Andric }
15770b57cec5SDimitry Andric glb_call_time = call_time;
15780b57cec5SDimitry Andric
15790b57cec5SDimitry Andric // Do not spend time on running algorithm if we have a permanent error.
15800b57cec5SDimitry Andric if (NtQuerySystemInformation == NULL) {
15810b57cec5SDimitry Andric running_threads = -1;
15820b57cec5SDimitry Andric goto finish;
15830b57cec5SDimitry Andric }
15840b57cec5SDimitry Andric
15850b57cec5SDimitry Andric if (max <= 0) {
15860b57cec5SDimitry Andric max = INT_MAX;
15870b57cec5SDimitry Andric }
15880b57cec5SDimitry Andric
15890b57cec5SDimitry Andric do {
15900b57cec5SDimitry Andric
15910b57cec5SDimitry Andric if (first_time) {
15920b57cec5SDimitry Andric buff_size = glb_buff_size;
15930b57cec5SDimitry Andric } else {
15940b57cec5SDimitry Andric buff_size = 2 * buff_size;
15950b57cec5SDimitry Andric }
15960b57cec5SDimitry Andric
15970b57cec5SDimitry Andric buffer = KMP_INTERNAL_REALLOC(buffer, buff_size);
15980b57cec5SDimitry Andric if (buffer == NULL) {
15990b57cec5SDimitry Andric running_threads = -1;
16000b57cec5SDimitry Andric goto finish;
16010b57cec5SDimitry Andric }
16020b57cec5SDimitry Andric status = NtQuerySystemInformation(SystemProcessInformation, buffer,
16030b57cec5SDimitry Andric buff_size, &info_size);
16040b57cec5SDimitry Andric first_time = 0;
16050b57cec5SDimitry Andric
16060b57cec5SDimitry Andric } while (status == STATUS_INFO_LENGTH_MISMATCH);
16070b57cec5SDimitry Andric glb_buff_size = buff_size;
16080b57cec5SDimitry Andric
16090b57cec5SDimitry Andric #define CHECK(cond) \
16100b57cec5SDimitry Andric { \
16110b57cec5SDimitry Andric KMP_DEBUG_ASSERT(cond); \
16120b57cec5SDimitry Andric if (!(cond)) { \
16130b57cec5SDimitry Andric running_threads = -1; \
16140b57cec5SDimitry Andric goto finish; \
16150b57cec5SDimitry Andric } \
16160b57cec5SDimitry Andric }
16170b57cec5SDimitry Andric
16180b57cec5SDimitry Andric CHECK(buff_size >= info_size);
16190b57cec5SDimitry Andric spi = PSYSTEM_PROCESS_INFORMATION(buffer);
16200b57cec5SDimitry Andric for (;;) {
16210b57cec5SDimitry Andric ptrdiff_t offset = uintptr_t(spi) - uintptr_t(buffer);
16220b57cec5SDimitry Andric CHECK(0 <= offset &&
16230b57cec5SDimitry Andric offset + sizeof(SYSTEM_PROCESS_INFORMATION) < info_size);
16240b57cec5SDimitry Andric HANDLE pid = spi->ProcessId;
16250b57cec5SDimitry Andric ULONG num = spi->NumberOfThreads;
16260b57cec5SDimitry Andric CHECK(num >= 1);
16270b57cec5SDimitry Andric size_t spi_size =
16280b57cec5SDimitry Andric sizeof(SYSTEM_PROCESS_INFORMATION) + sizeof(SYSTEM_THREAD) * (num - 1);
16290b57cec5SDimitry Andric CHECK(offset + spi_size <
16300b57cec5SDimitry Andric info_size); // Make sure process info record fits the buffer.
16310b57cec5SDimitry Andric if (spi->NextEntryOffset != 0) {
16320b57cec5SDimitry Andric CHECK(spi_size <=
16330b57cec5SDimitry Andric spi->NextEntryOffset); // And do not overlap with the next record.
16340b57cec5SDimitry Andric }
16350b57cec5SDimitry Andric // pid == 0 corresponds to the System Idle Process. It always has running
16360b57cec5SDimitry Andric // threads on all cores. So, we don't consider the running threads of this
16370b57cec5SDimitry Andric // process.
16380b57cec5SDimitry Andric if (pid != 0) {
1639*7a6dacacSDimitry Andric for (ULONG i = 0; i < num; ++i) {
16400b57cec5SDimitry Andric THREAD_STATE state = spi->Threads[i].State;
16410b57cec5SDimitry Andric // Count threads that have Ready or Running state.
16420b57cec5SDimitry Andric // !!! TODO: Why comment does not match the code???
16430b57cec5SDimitry Andric if (state == StateRunning) {
16440b57cec5SDimitry Andric ++running_threads;
16450b57cec5SDimitry Andric // Stop counting running threads if the number is already greater than
16460b57cec5SDimitry Andric // the number of available cores
16470b57cec5SDimitry Andric if (running_threads >= max) {
16480b57cec5SDimitry Andric goto finish;
16490b57cec5SDimitry Andric }
16500b57cec5SDimitry Andric }
16510b57cec5SDimitry Andric }
16520b57cec5SDimitry Andric }
16530b57cec5SDimitry Andric if (spi->NextEntryOffset == 0) {
16540b57cec5SDimitry Andric break;
16550b57cec5SDimitry Andric }
16560b57cec5SDimitry Andric spi = PSYSTEM_PROCESS_INFORMATION(uintptr_t(spi) + spi->NextEntryOffset);
16570b57cec5SDimitry Andric }
16580b57cec5SDimitry Andric
16590b57cec5SDimitry Andric #undef CHECK
16600b57cec5SDimitry Andric
16610b57cec5SDimitry Andric finish: // Clean up and exit.
16620b57cec5SDimitry Andric
16630b57cec5SDimitry Andric if (buffer != NULL) {
16640b57cec5SDimitry Andric KMP_INTERNAL_FREE(buffer);
16650b57cec5SDimitry Andric }
16660b57cec5SDimitry Andric
16670b57cec5SDimitry Andric glb_running_threads = running_threads;
16680b57cec5SDimitry Andric
16690b57cec5SDimitry Andric return running_threads;
16700b57cec5SDimitry Andric } //__kmp_get_load_balance()
1671e8d8bef9SDimitry Andric
1672fe6060f1SDimitry Andric // Find symbol from the loaded modules
__kmp_lookup_symbol(const char * name,bool next)167306c3fb27SDimitry Andric void *__kmp_lookup_symbol(const char *name, bool next) {
1674fe6060f1SDimitry Andric HANDLE process = GetCurrentProcess();
1675fe6060f1SDimitry Andric DWORD needed;
1676fe6060f1SDimitry Andric HMODULE *modules = nullptr;
1677fe6060f1SDimitry Andric if (!EnumProcessModules(process, modules, 0, &needed))
1678fe6060f1SDimitry Andric return nullptr;
1679fe6060f1SDimitry Andric DWORD num_modules = needed / sizeof(HMODULE);
1680fe6060f1SDimitry Andric modules = (HMODULE *)malloc(num_modules * sizeof(HMODULE));
1681fe6060f1SDimitry Andric if (!EnumProcessModules(process, modules, needed, &needed)) {
1682fe6060f1SDimitry Andric free(modules);
1683fe6060f1SDimitry Andric return nullptr;
1684fe6060f1SDimitry Andric }
168506c3fb27SDimitry Andric HMODULE curr_module = nullptr;
168606c3fb27SDimitry Andric if (next) {
168706c3fb27SDimitry Andric // Current module needs to be skipped if next flag is true
168806c3fb27SDimitry Andric if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
168906c3fb27SDimitry Andric (LPCTSTR)&__kmp_lookup_symbol, &curr_module)) {
169006c3fb27SDimitry Andric free(modules);
169106c3fb27SDimitry Andric return nullptr;
169206c3fb27SDimitry Andric }
169306c3fb27SDimitry Andric }
1694fe6060f1SDimitry Andric void *proc = nullptr;
1695fe6060f1SDimitry Andric for (uint32_t i = 0; i < num_modules; i++) {
169606c3fb27SDimitry Andric if (next && modules[i] == curr_module)
169706c3fb27SDimitry Andric continue;
1698fe6060f1SDimitry Andric proc = (void *)GetProcAddress(modules[i], name);
1699fe6060f1SDimitry Andric if (proc)
1700fe6060f1SDimitry Andric break;
1701fe6060f1SDimitry Andric }
1702fe6060f1SDimitry Andric free(modules);
1703fe6060f1SDimitry Andric return proc;
1704fe6060f1SDimitry Andric }
1705fe6060f1SDimitry Andric
1706e8d8bef9SDimitry Andric // Functions for hidden helper task
__kmp_hidden_helper_worker_thread_wait()1707e8d8bef9SDimitry Andric void __kmp_hidden_helper_worker_thread_wait() {
1708e8d8bef9SDimitry Andric KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1709e8d8bef9SDimitry Andric }
1710e8d8bef9SDimitry Andric
__kmp_do_initialize_hidden_helper_threads()1711e8d8bef9SDimitry Andric void __kmp_do_initialize_hidden_helper_threads() {
1712e8d8bef9SDimitry Andric KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1713e8d8bef9SDimitry Andric }
1714e8d8bef9SDimitry Andric
__kmp_hidden_helper_threads_initz_wait()1715e8d8bef9SDimitry Andric void __kmp_hidden_helper_threads_initz_wait() {
1716e8d8bef9SDimitry Andric KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1717e8d8bef9SDimitry Andric }
1718e8d8bef9SDimitry Andric
__kmp_hidden_helper_initz_release()1719e8d8bef9SDimitry Andric void __kmp_hidden_helper_initz_release() {
1720e8d8bef9SDimitry Andric KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1721e8d8bef9SDimitry Andric }
1722e8d8bef9SDimitry Andric
__kmp_hidden_helper_main_thread_wait()1723e8d8bef9SDimitry Andric void __kmp_hidden_helper_main_thread_wait() {
1724e8d8bef9SDimitry Andric KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1725e8d8bef9SDimitry Andric }
1726e8d8bef9SDimitry Andric
__kmp_hidden_helper_main_thread_release()1727e8d8bef9SDimitry Andric void __kmp_hidden_helper_main_thread_release() {
1728e8d8bef9SDimitry Andric KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1729e8d8bef9SDimitry Andric }
1730e8d8bef9SDimitry Andric
__kmp_hidden_helper_worker_thread_signal()1731e8d8bef9SDimitry Andric void __kmp_hidden_helper_worker_thread_signal() {
1732e8d8bef9SDimitry Andric KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1733e8d8bef9SDimitry Andric }
1734e8d8bef9SDimitry Andric
__kmp_hidden_helper_threads_deinitz_wait()1735e8d8bef9SDimitry Andric void __kmp_hidden_helper_threads_deinitz_wait() {
1736e8d8bef9SDimitry Andric KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1737e8d8bef9SDimitry Andric }
1738e8d8bef9SDimitry Andric
__kmp_hidden_helper_threads_deinitz_release()1739e8d8bef9SDimitry Andric void __kmp_hidden_helper_threads_deinitz_release() {
1740e8d8bef9SDimitry Andric KMP_ASSERT(0 && "Hidden helper task is not supported on Windows");
1741e8d8bef9SDimitry Andric }
1742