Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1 | /* |
Jonathan Peyton | de4749b | 2016-12-14 23:01:24 +0000 | [diff] [blame] | 2 | * z_Windows_NT_util.cpp -- platform specific routines. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3 | */ |
| 4 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5 | //===----------------------------------------------------------------------===// |
| 6 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 7 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 8 | // See https://llvm.org/LICENSE.txt for license information. |
| 9 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 13 | #include "kmp.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 14 | #include "kmp_affinity.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 15 | #include "kmp_i18n.h" |
| 16 | #include "kmp_io.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 17 | #include "kmp_itt.h" |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 18 | #include "kmp_wait_release.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 19 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 20 | /* This code is related to NtQuerySystemInformation() function. This function |
| 21 | is used in the Load balance algorithm for OMP_DYNAMIC=true to find the |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 22 | number of running threads in the system. */ |
| 23 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 24 | #include <ntsecapi.h> // UNICODE_STRING |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 25 | #include <ntstatus.h> |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 26 | |
| 27 | enum SYSTEM_INFORMATION_CLASS { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 28 | SystemProcessInformation = 5 |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 29 | }; // SYSTEM_INFORMATION_CLASS |
| 30 | |
| 31 | struct CLIENT_ID { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 32 | HANDLE UniqueProcess; |
| 33 | HANDLE UniqueThread; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 34 | }; // struct CLIENT_ID |
| 35 | |
| 36 | enum THREAD_STATE { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 37 | StateInitialized, |
| 38 | StateReady, |
| 39 | StateRunning, |
| 40 | StateStandby, |
| 41 | StateTerminated, |
| 42 | StateWait, |
| 43 | StateTransition, |
| 44 | StateUnknown |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 45 | }; // enum THREAD_STATE |
| 46 | |
| 47 | struct VM_COUNTERS { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 48 | SIZE_T PeakVirtualSize; |
| 49 | SIZE_T VirtualSize; |
| 50 | ULONG PageFaultCount; |
| 51 | SIZE_T PeakWorkingSetSize; |
| 52 | SIZE_T WorkingSetSize; |
| 53 | SIZE_T QuotaPeakPagedPoolUsage; |
| 54 | SIZE_T QuotaPagedPoolUsage; |
| 55 | SIZE_T QuotaPeakNonPagedPoolUsage; |
| 56 | SIZE_T QuotaNonPagedPoolUsage; |
| 57 | SIZE_T PagefileUsage; |
| 58 | SIZE_T PeakPagefileUsage; |
| 59 | SIZE_T PrivatePageCount; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 60 | }; // struct VM_COUNTERS |
| 61 | |
| 62 | struct SYSTEM_THREAD { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 63 | LARGE_INTEGER KernelTime; |
| 64 | LARGE_INTEGER UserTime; |
| 65 | LARGE_INTEGER CreateTime; |
| 66 | ULONG WaitTime; |
| 67 | LPVOID StartAddress; |
| 68 | CLIENT_ID ClientId; |
| 69 | DWORD Priority; |
| 70 | LONG BasePriority; |
| 71 | ULONG ContextSwitchCount; |
| 72 | THREAD_STATE State; |
| 73 | ULONG WaitReason; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 74 | }; // SYSTEM_THREAD |
| 75 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 76 | KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, KernelTime) == 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 77 | #if KMP_ARCH_X86 |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 78 | KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 28); |
| 79 | KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 52); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 80 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 81 | KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, StartAddress) == 32); |
| 82 | KMP_BUILD_ASSERT(offsetof(SYSTEM_THREAD, State) == 68); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | struct SYSTEM_PROCESS_INFORMATION { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 86 | ULONG NextEntryOffset; |
| 87 | ULONG NumberOfThreads; |
| 88 | LARGE_INTEGER Reserved[3]; |
| 89 | LARGE_INTEGER CreateTime; |
| 90 | LARGE_INTEGER UserTime; |
| 91 | LARGE_INTEGER KernelTime; |
| 92 | UNICODE_STRING ImageName; |
| 93 | DWORD BasePriority; |
| 94 | HANDLE ProcessId; |
| 95 | HANDLE ParentProcessId; |
| 96 | ULONG HandleCount; |
| 97 | ULONG Reserved2[2]; |
| 98 | VM_COUNTERS VMCounters; |
| 99 | IO_COUNTERS IOCounters; |
| 100 | SYSTEM_THREAD Threads[1]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 101 | }; // SYSTEM_PROCESS_INFORMATION |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 102 | typedef SYSTEM_PROCESS_INFORMATION *PSYSTEM_PROCESS_INFORMATION; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 103 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 104 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, NextEntryOffset) == 0); |
| 105 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, CreateTime) == 32); |
| 106 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ImageName) == 56); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 107 | #if KMP_ARCH_X86 |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 108 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 68); |
| 109 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 76); |
| 110 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 88); |
| 111 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 136); |
| 112 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 184); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 113 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 114 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, ProcessId) == 80); |
| 115 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, HandleCount) == 96); |
| 116 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, VMCounters) == 112); |
| 117 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, IOCounters) == 208); |
| 118 | KMP_BUILD_ASSERT(offsetof(SYSTEM_PROCESS_INFORMATION, Threads) == 256); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 119 | #endif |
| 120 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 121 | typedef NTSTATUS(NTAPI *NtQuerySystemInformation_t)(SYSTEM_INFORMATION_CLASS, |
| 122 | PVOID, ULONG, PULONG); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 123 | NtQuerySystemInformation_t NtQuerySystemInformation = NULL; |
| 124 | |
| 125 | HMODULE ntdll = NULL; |
| 126 | |
| 127 | /* End of NtQuerySystemInformation()-related code */ |
| 128 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 129 | static HMODULE kernel32 = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 130 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 131 | #if KMP_HANDLE_SIGNALS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 132 | typedef void (*sig_func_t)(int); |
| 133 | static sig_func_t __kmp_sighldrs[NSIG]; |
| 134 | static int __kmp_siginstalled[NSIG]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 137 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 138 | static HANDLE __kmp_monitor_ev; |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 139 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 140 | static kmp_int64 __kmp_win32_time; |
| 141 | double __kmp_win32_tick; |
| 142 | |
| 143 | int __kmp_init_runtime = FALSE; |
| 144 | CRITICAL_SECTION __kmp_win32_section; |
| 145 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 146 | void __kmp_win32_mutex_init(kmp_win32_mutex_t *mx) { |
| 147 | InitializeCriticalSection(&mx->cs); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 148 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 149 | __kmp_itt_system_object_created(&mx->cs, "Critical Section"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 150 | #endif /* USE_ITT_BUILD */ |
| 151 | } |
| 152 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 153 | void __kmp_win32_mutex_destroy(kmp_win32_mutex_t *mx) { |
| 154 | DeleteCriticalSection(&mx->cs); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 157 | void __kmp_win32_mutex_lock(kmp_win32_mutex_t *mx) { |
| 158 | EnterCriticalSection(&mx->cs); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Jonathan Peyton | 9b8bb32 | 2019-01-16 20:07:39 +0000 | [diff] [blame] | 161 | int __kmp_win32_mutex_trylock(kmp_win32_mutex_t *mx) { |
| 162 | return TryEnterCriticalSection(&mx->cs); |
| 163 | } |
| 164 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 165 | void __kmp_win32_mutex_unlock(kmp_win32_mutex_t *mx) { |
| 166 | LeaveCriticalSection(&mx->cs); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 169 | void __kmp_win32_cond_init(kmp_win32_cond_t *cv) { |
| 170 | cv->waiters_count_ = 0; |
| 171 | cv->wait_generation_count_ = 0; |
| 172 | cv->release_count_ = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 173 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 174 | /* Initialize the critical section */ |
| 175 | __kmp_win32_mutex_init(&cv->waiters_count_lock_); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 176 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 177 | /* Create a manual-reset event. */ |
| 178 | cv->event_ = CreateEvent(NULL, // no security |
| 179 | TRUE, // manual-reset |
| 180 | FALSE, // non-signaled initially |
| 181 | NULL); // unnamed |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 182 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 183 | __kmp_itt_system_object_created(cv->event_, "Event"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 184 | #endif /* USE_ITT_BUILD */ |
| 185 | } |
| 186 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 187 | void __kmp_win32_cond_destroy(kmp_win32_cond_t *cv) { |
| 188 | __kmp_win32_mutex_destroy(&cv->waiters_count_lock_); |
| 189 | __kmp_free_handle(cv->event_); |
| 190 | memset(cv, '\0', sizeof(*cv)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* TODO associate cv with a team instead of a thread so as to optimize |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 194 | the case where we wake up a whole team */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 195 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 196 | void __kmp_win32_cond_wait(kmp_win32_cond_t *cv, kmp_win32_mutex_t *mx, |
| 197 | kmp_info_t *th, int need_decrease_load) { |
| 198 | int my_generation; |
| 199 | int last_waiter; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 200 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 201 | /* Avoid race conditions */ |
| 202 | __kmp_win32_mutex_lock(&cv->waiters_count_lock_); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 203 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 204 | /* Increment count of waiters */ |
| 205 | cv->waiters_count_++; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 206 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 207 | /* Store current generation in our activation record. */ |
| 208 | my_generation = cv->wait_generation_count_; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 209 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 210 | __kmp_win32_mutex_unlock(&cv->waiters_count_lock_); |
| 211 | __kmp_win32_mutex_unlock(mx); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 212 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 213 | for (;;) { |
| 214 | int wait_done; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 215 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 216 | /* Wait until the event is signaled */ |
| 217 | WaitForSingleObject(cv->event_, INFINITE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 218 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 219 | __kmp_win32_mutex_lock(&cv->waiters_count_lock_); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 220 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 221 | /* Exit the loop when the <cv->event_> is signaled and there are still |
| 222 | waiting threads from this <wait_generation> that haven't been released |
| 223 | from this wait yet. */ |
| 224 | wait_done = (cv->release_count_ > 0) && |
| 225 | (cv->wait_generation_count_ != my_generation); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 226 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 227 | __kmp_win32_mutex_unlock(&cv->waiters_count_lock_); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 228 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 229 | /* there used to be a semicolon after the if statement, it looked like a |
| 230 | bug, so i removed it */ |
| 231 | if (wait_done) |
| 232 | break; |
| 233 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 234 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 235 | __kmp_win32_mutex_lock(mx); |
| 236 | __kmp_win32_mutex_lock(&cv->waiters_count_lock_); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 237 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 238 | cv->waiters_count_--; |
| 239 | cv->release_count_--; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 240 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 241 | last_waiter = (cv->release_count_ == 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 242 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 243 | __kmp_win32_mutex_unlock(&cv->waiters_count_lock_); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 244 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 245 | if (last_waiter) { |
| 246 | /* We're the last waiter to be notified, so reset the manual event. */ |
| 247 | ResetEvent(cv->event_); |
| 248 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 251 | void __kmp_win32_cond_broadcast(kmp_win32_cond_t *cv) { |
| 252 | __kmp_win32_mutex_lock(&cv->waiters_count_lock_); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 253 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 254 | if (cv->waiters_count_ > 0) { |
| 255 | SetEvent(cv->event_); |
| 256 | /* Release all the threads in this generation. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 257 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 258 | cv->release_count_ = cv->waiters_count_; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 259 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 260 | /* Start a new generation. */ |
| 261 | cv->wait_generation_count_++; |
| 262 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 263 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 264 | __kmp_win32_mutex_unlock(&cv->waiters_count_lock_); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 267 | void __kmp_win32_cond_signal(kmp_win32_cond_t *cv) { |
| 268 | __kmp_win32_cond_broadcast(cv); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 271 | void __kmp_enable(int new_state) { |
| 272 | if (__kmp_init_runtime) |
| 273 | LeaveCriticalSection(&__kmp_win32_section); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 276 | void __kmp_disable(int *old_state) { |
| 277 | *old_state = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 278 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 279 | if (__kmp_init_runtime) |
| 280 | EnterCriticalSection(&__kmp_win32_section); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 283 | void __kmp_suspend_initialize(void) { /* do nothing */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 286 | static void __kmp_suspend_initialize_thread(kmp_info_t *th) { |
| 287 | if (!TCR_4(th->th.th_suspend_init)) { |
| 288 | /* this means we haven't initialized the suspension pthread objects for this |
| 289 | thread in this instance of the process */ |
| 290 | __kmp_win32_cond_init(&th->th.th_suspend_cv); |
| 291 | __kmp_win32_mutex_init(&th->th.th_suspend_mx); |
| 292 | TCW_4(th->th.th_suspend_init, TRUE); |
| 293 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 296 | void __kmp_suspend_uninitialize_thread(kmp_info_t *th) { |
| 297 | if (TCR_4(th->th.th_suspend_init)) { |
| 298 | /* this means we have initialize the suspension pthread objects for this |
| 299 | thread in this instance of the process */ |
| 300 | __kmp_win32_cond_destroy(&th->th.th_suspend_cv); |
| 301 | __kmp_win32_mutex_destroy(&th->th.th_suspend_mx); |
| 302 | TCW_4(th->th.th_suspend_init, FALSE); |
| 303 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Jonathan Peyton | 9b8bb32 | 2019-01-16 20:07:39 +0000 | [diff] [blame] | 306 | int __kmp_try_suspend_mx(kmp_info_t *th) { |
| 307 | return __kmp_win32_mutex_trylock(&th->th.th_suspend_mx); |
| 308 | } |
| 309 | |
| 310 | void __kmp_lock_suspend_mx(kmp_info_t *th) { |
| 311 | __kmp_win32_mutex_lock(&th->th.th_suspend_mx); |
| 312 | } |
| 313 | |
| 314 | void __kmp_unlock_suspend_mx(kmp_info_t *th) { |
| 315 | __kmp_win32_mutex_unlock(&th->th.th_suspend_mx); |
| 316 | } |
| 317 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 318 | /* This routine puts the calling thread to sleep after setting the |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 319 | sleep bit for the indicated flag variable to true. */ |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 320 | template <class C> |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 321 | static inline void __kmp_suspend_template(int th_gtid, C *flag) { |
| 322 | kmp_info_t *th = __kmp_threads[th_gtid]; |
| 323 | int status; |
| 324 | typename C::flag_t old_spin; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 325 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 326 | KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag's loc(%p)\n", |
| 327 | th_gtid, flag->get())); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 328 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 329 | __kmp_suspend_initialize_thread(th); |
| 330 | __kmp_win32_mutex_lock(&th->th.th_suspend_mx); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 331 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 332 | KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for flag's" |
| 333 | " loc(%p)\n", |
| 334 | th_gtid, flag->get())); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 335 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 336 | /* TODO: shouldn't this use release semantics to ensure that |
| 337 | __kmp_suspend_initialize_thread gets called first? */ |
| 338 | old_spin = flag->set_sleeping(); |
Jonathan Peyton | 9b8bb32 | 2019-01-16 20:07:39 +0000 | [diff] [blame] | 339 | #if OMP_50_ENABLED |
| 340 | if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME && |
| 341 | __kmp_pause_status != kmp_soft_paused) { |
| 342 | flag->unset_sleeping(); |
| 343 | __kmp_win32_mutex_unlock(&th->th.th_suspend_mx); |
| 344 | return; |
| 345 | } |
| 346 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 347 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 348 | KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for flag's" |
| 349 | " loc(%p)==%d\n", |
| 350 | th_gtid, flag->get(), *(flag->get()))); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 351 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 352 | if (flag->done_check_val(old_spin)) { |
| 353 | old_spin = flag->unset_sleeping(); |
| 354 | KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit " |
| 355 | "for flag's loc(%p)\n", |
| 356 | th_gtid, flag->get())); |
| 357 | } else { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 358 | #ifdef DEBUG_SUSPEND |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 359 | __kmp_suspend_count++; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 360 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 361 | /* Encapsulate in a loop as the documentation states that this may "with |
| 362 | low probability" return when the condition variable has not been signaled |
| 363 | or broadcast */ |
| 364 | int deactivated = FALSE; |
| 365 | TCW_PTR(th->th.th_sleep_loc, (void *)flag); |
| 366 | while (flag->is_sleeping()) { |
| 367 | KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform " |
| 368 | "kmp_win32_cond_wait()\n", |
| 369 | th_gtid)); |
| 370 | // Mark the thread as no longer active (only in the first iteration of the |
| 371 | // loop). |
| 372 | if (!deactivated) { |
| 373 | th->th.th_active = FALSE; |
| 374 | if (th->th.th_active_in_pool) { |
| 375 | th->th.th_active_in_pool = FALSE; |
Jonathan Peyton | 37e2ef5 | 2018-07-09 17:36:22 +0000 | [diff] [blame] | 376 | KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 377 | KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0); |
| 378 | } |
| 379 | deactivated = TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 380 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 381 | __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, 0, |
| 382 | 0); |
| 383 | } else { |
| 384 | __kmp_win32_cond_wait(&th->th.th_suspend_cv, &th->th.th_suspend_mx, 0, |
| 385 | 0); |
| 386 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 387 | |
| 388 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 389 | if (flag->is_sleeping()) { |
| 390 | KF_TRACE(100, |
| 391 | ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid)); |
| 392 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 393 | #endif /* KMP_DEBUG */ |
| 394 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 395 | } // while |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 396 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 397 | // Mark the thread as active again (if it was previous marked as inactive) |
| 398 | if (deactivated) { |
| 399 | th->th.th_active = TRUE; |
| 400 | if (TCR_4(th->th.th_in_pool)) { |
Jonathan Peyton | 37e2ef5 | 2018-07-09 17:36:22 +0000 | [diff] [blame] | 401 | KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 402 | th->th.th_active_in_pool = TRUE; |
| 403 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 404 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 405 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 406 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 407 | __kmp_win32_mutex_unlock(&th->th.th_suspend_mx); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 408 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 409 | KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 412 | void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 413 | __kmp_suspend_template(th_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 414 | } |
| 415 | void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 416 | __kmp_suspend_template(th_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 417 | } |
| 418 | void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 419 | __kmp_suspend_template(th_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 422 | /* This routine signals the thread specified by target_gtid to wake up |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 423 | after setting the sleep bit indicated by the flag argument to FALSE */ |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 424 | template <class C> |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 425 | static inline void __kmp_resume_template(int target_gtid, C *flag) { |
| 426 | kmp_info_t *th = __kmp_threads[target_gtid]; |
| 427 | int status; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 428 | |
| 429 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 430 | int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 431 | #endif |
| 432 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 433 | KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", |
| 434 | gtid, target_gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 435 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 436 | __kmp_suspend_initialize_thread(th); |
| 437 | __kmp_win32_mutex_lock(&th->th.th_suspend_mx); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 438 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 439 | if (!flag) { // coming from __kmp_null_resume_wrapper |
| 440 | flag = (C *)th->th.th_sleep_loc; |
| 441 | } |
| 442 | |
| 443 | // First, check if the flag is null or its type has changed. If so, someone |
| 444 | // else woke it up. |
| 445 | if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type |
| 446 | // simply shows what |
| 447 | // flag was cast to |
| 448 | KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already " |
| 449 | "awake: flag's loc(%p)\n", |
| 450 | gtid, target_gtid, NULL)); |
| 451 | __kmp_win32_mutex_unlock(&th->th.th_suspend_mx); |
| 452 | return; |
| 453 | } else { |
| 454 | typename C::flag_t old_spin = flag->unset_sleeping(); |
| 455 | if (!flag->is_sleeping_val(old_spin)) { |
| 456 | KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already " |
| 457 | "awake: flag's loc(%p): %u => %u\n", |
| 458 | gtid, target_gtid, flag->get(), old_spin, *(flag->get()))); |
| 459 | __kmp_win32_mutex_unlock(&th->th.th_suspend_mx); |
| 460 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 461 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 462 | } |
| 463 | TCW_PTR(th->th.th_sleep_loc, NULL); |
| 464 | KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep " |
| 465 | "bit for flag's loc(%p)\n", |
| 466 | gtid, target_gtid, flag->get())); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 467 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 468 | __kmp_win32_cond_signal(&th->th.th_suspend_cv); |
| 469 | __kmp_win32_mutex_unlock(&th->th.th_suspend_mx); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 470 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 471 | KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up" |
| 472 | " for T#%d\n", |
| 473 | gtid, target_gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 476 | void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 477 | __kmp_resume_template(target_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 478 | } |
| 479 | void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 480 | __kmp_resume_template(target_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 481 | } |
| 482 | void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 483 | __kmp_resume_template(target_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Jonathan Peyton | e47d32f | 2019-02-28 19:11:29 +0000 | [diff] [blame] | 486 | void __kmp_yield() { Sleep(0); } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 487 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 488 | void __kmp_gtid_set_specific(int gtid) { |
| 489 | if (__kmp_init_gtid) { |
| 490 | KA_TRACE(50, ("__kmp_gtid_set_specific: T#%d key:%d\n", gtid, |
| 491 | __kmp_gtid_threadprivate_key)); |
| 492 | if (!TlsSetValue(__kmp_gtid_threadprivate_key, (LPVOID)(gtid + 1))) |
| 493 | KMP_FATAL(TLSSetValueFailed); |
| 494 | } else { |
| 495 | KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n")); |
| 496 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 499 | int __kmp_gtid_get_specific() { |
| 500 | int gtid; |
| 501 | if (!__kmp_init_gtid) { |
| 502 | KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning " |
| 503 | "KMP_GTID_SHUTDOWN\n")); |
| 504 | return KMP_GTID_SHUTDOWN; |
| 505 | } |
| 506 | gtid = (int)(kmp_intptr_t)TlsGetValue(__kmp_gtid_threadprivate_key); |
| 507 | if (gtid == 0) { |
| 508 | gtid = KMP_GTID_DNE; |
| 509 | } else { |
| 510 | gtid--; |
| 511 | } |
| 512 | KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n", |
| 513 | __kmp_gtid_threadprivate_key, gtid)); |
| 514 | return gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 517 | void __kmp_affinity_bind_thread(int proc) { |
| 518 | if (__kmp_num_proc_groups > 1) { |
| 519 | // Form the GROUP_AFFINITY struct directly, rather than filling |
| 520 | // out a bit vector and calling __kmp_set_system_affinity(). |
| 521 | GROUP_AFFINITY ga; |
| 522 | KMP_DEBUG_ASSERT((proc >= 0) && (proc < (__kmp_num_proc_groups * CHAR_BIT * |
| 523 | sizeof(DWORD_PTR)))); |
| 524 | ga.Group = proc / (CHAR_BIT * sizeof(DWORD_PTR)); |
| 525 | ga.Mask = (unsigned long long)1 << (proc % (CHAR_BIT * sizeof(DWORD_PTR))); |
| 526 | ga.Reserved[0] = ga.Reserved[1] = ga.Reserved[2] = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 527 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 528 | KMP_DEBUG_ASSERT(__kmp_SetThreadGroupAffinity != NULL); |
| 529 | if (__kmp_SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) == 0) { |
| 530 | DWORD error = GetLastError(); |
| 531 | if (__kmp_affinity_verbose) { // AC: continue silently if not verbose |
| 532 | kmp_msg_t err_code = KMP_ERR(error); |
| 533 | __kmp_msg(kmp_ms_warning, KMP_MSG(CantSetThreadAffMask), err_code, |
| 534 | __kmp_msg_null); |
| 535 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 536 | __kmp_str_free(&err_code.str); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 537 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 538 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 539 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 540 | } else { |
| 541 | kmp_affin_mask_t *mask; |
| 542 | KMP_CPU_ALLOC_ON_STACK(mask); |
| 543 | KMP_CPU_ZERO(mask); |
| 544 | KMP_CPU_SET(proc, mask); |
| 545 | __kmp_set_system_affinity(mask, TRUE); |
| 546 | KMP_CPU_FREE_FROM_STACK(mask); |
| 547 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 550 | void __kmp_affinity_determine_capable(const char *env_var) { |
| 551 | // All versions of Windows* OS (since Win '95) support SetThreadAffinityMask(). |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 552 | |
Andrey Churbanov | 7daf980 | 2015-01-27 16:52:57 +0000 | [diff] [blame] | 553 | #if KMP_GROUP_AFFINITY |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 554 | KMP_AFFINITY_ENABLE(__kmp_num_proc_groups * sizeof(DWORD_PTR)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 555 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 556 | KMP_AFFINITY_ENABLE(sizeof(DWORD_PTR)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 557 | #endif |
| 558 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 559 | KA_TRACE(10, ("__kmp_affinity_determine_capable: " |
| 560 | "Windows* OS affinity interface functional (mask size = " |
| 561 | "%" KMP_SIZE_T_SPEC ").\n", |
| 562 | __kmp_affin_mask_size)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 565 | double __kmp_read_cpu_time(void) { |
| 566 | FILETIME CreationTime, ExitTime, KernelTime, UserTime; |
| 567 | int status; |
| 568 | double cpu_time; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 569 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 570 | cpu_time = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 571 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 572 | status = GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime, |
| 573 | &KernelTime, &UserTime); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 574 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 575 | if (status) { |
| 576 | double sec = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 577 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 578 | sec += KernelTime.dwHighDateTime; |
| 579 | sec += UserTime.dwHighDateTime; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 580 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 581 | /* Shift left by 32 bits */ |
| 582 | sec *= (double)(1 << 16) * (double)(1 << 16); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 583 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 584 | sec += KernelTime.dwLowDateTime; |
| 585 | sec += UserTime.dwLowDateTime; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 586 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 587 | cpu_time += (sec * 100.0) / KMP_NSEC_PER_SEC; |
| 588 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 589 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 590 | return cpu_time; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 593 | int __kmp_read_system_info(struct kmp_sys_info *info) { |
| 594 | info->maxrss = 0; /* the maximum resident set size utilized (in kilobytes) */ |
| 595 | info->minflt = 0; /* the number of page faults serviced without any I/O */ |
| 596 | info->majflt = 0; /* the number of page faults serviced that required I/O */ |
| 597 | info->nswap = 0; // the number of times a process was "swapped" out of memory |
| 598 | info->inblock = 0; // the number of times the file system had to perform input |
| 599 | info->oublock = 0; // number of times the file system had to perform output |
| 600 | info->nvcsw = 0; /* the number of times a context switch was voluntarily */ |
| 601 | info->nivcsw = 0; /* the number of times a context switch was forced */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 602 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 603 | return 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 606 | void __kmp_runtime_initialize(void) { |
| 607 | SYSTEM_INFO info; |
| 608 | kmp_str_buf_t path; |
| 609 | UINT path_size; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 610 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 611 | if (__kmp_init_runtime) { |
| 612 | return; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 613 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 614 | |
Jonathan Peyton | 9901699 | 2015-05-26 17:32:53 +0000 | [diff] [blame] | 615 | #if KMP_DYNAMIC_LIB |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 616 | /* Pin dynamic library for the lifetime of application */ |
| 617 | { |
| 618 | // First, turn off error message boxes |
| 619 | UINT err_mode = SetErrorMode(SEM_FAILCRITICALERRORS); |
| 620 | HMODULE h; |
| 621 | BOOL ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 622 | GET_MODULE_HANDLE_EX_FLAG_PIN, |
| 623 | (LPCTSTR)&__kmp_serial_initialize, &h); |
| 624 | KMP_DEBUG_ASSERT2(h && ret, "OpenMP RTL cannot find itself loaded"); |
| 625 | SetErrorMode(err_mode); // Restore error mode |
| 626 | KA_TRACE(10, ("__kmp_runtime_initialize: dynamic library pinned\n")); |
| 627 | } |
Andrey Churbanov | 9bf5328 | 2015-01-29 17:18:20 +0000 | [diff] [blame] | 628 | #endif |
| 629 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 630 | InitializeCriticalSection(&__kmp_win32_section); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 631 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 632 | __kmp_itt_system_object_created(&__kmp_win32_section, "Critical Section"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 633 | #endif /* USE_ITT_BUILD */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 634 | __kmp_initialize_system_tick(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 635 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 636 | #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) |
| 637 | if (!__kmp_cpuinfo.initialized) { |
| 638 | __kmp_query_cpuid(&__kmp_cpuinfo); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 639 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 640 | #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 641 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 642 | /* Set up minimum number of threads to switch to TLS gtid */ |
Jonathan Peyton | 8b3842f | 2018-10-05 17:59:39 +0000 | [diff] [blame] | 643 | #if KMP_OS_WINDOWS && !KMP_DYNAMIC_LIB |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 644 | // Windows* OS, static library. |
| 645 | /* New thread may use stack space previously used by another thread, |
| 646 | currently terminated. On Windows* OS, in case of static linking, we do not |
| 647 | know the moment of thread termination, and our structures (__kmp_threads |
| 648 | and __kmp_root arrays) are still keep info about dead threads. This leads |
| 649 | to problem in __kmp_get_global_thread_id() function: it wrongly finds gtid |
| 650 | (by searching through stack addresses of all known threads) for |
| 651 | unregistered foreign tread. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 652 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 653 | Setting __kmp_tls_gtid_min to 0 workarounds this problem: |
| 654 | __kmp_get_global_thread_id() does not search through stacks, but get gtid |
| 655 | from TLS immediately. |
| 656 | --ln |
| 657 | */ |
| 658 | __kmp_tls_gtid_min = 0; |
| 659 | #else |
| 660 | __kmp_tls_gtid_min = KMP_TLS_GTID_MIN; |
| 661 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 662 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 663 | /* for the static library */ |
| 664 | if (!__kmp_gtid_threadprivate_key) { |
| 665 | __kmp_gtid_threadprivate_key = TlsAlloc(); |
| 666 | if (__kmp_gtid_threadprivate_key == TLS_OUT_OF_INDEXES) { |
| 667 | KMP_FATAL(TLSOutOfIndexes); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 668 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 669 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 670 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 671 | // Load ntdll.dll. |
| 672 | /* Simple GetModuleHandle( "ntdll.dl" ) is not suitable due to security issue |
| 673 | (see http://www.microsoft.com/technet/security/advisory/2269637.mspx). We |
| 674 | have to specify full path to the library. */ |
| 675 | __kmp_str_buf_init(&path); |
| 676 | path_size = GetSystemDirectory(path.str, path.size); |
| 677 | KMP_DEBUG_ASSERT(path_size > 0); |
| 678 | if (path_size >= path.size) { |
| 679 | // Buffer is too short. Expand the buffer and try again. |
| 680 | __kmp_str_buf_reserve(&path, path_size); |
| 681 | path_size = GetSystemDirectory(path.str, path.size); |
| 682 | KMP_DEBUG_ASSERT(path_size > 0); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 683 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 684 | if (path_size > 0 && path_size < path.size) { |
| 685 | // Now we have system directory name in the buffer. |
| 686 | // Append backslash and name of dll to form full path, |
| 687 | path.used = path_size; |
| 688 | __kmp_str_buf_print(&path, "\\%s", "ntdll.dll"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 689 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 690 | // Now load ntdll using full path. |
| 691 | ntdll = GetModuleHandle(path.str); |
| 692 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 693 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 694 | KMP_DEBUG_ASSERT(ntdll != NULL); |
| 695 | if (ntdll != NULL) { |
| 696 | NtQuerySystemInformation = (NtQuerySystemInformation_t)GetProcAddress( |
| 697 | ntdll, "NtQuerySystemInformation"); |
| 698 | } |
| 699 | KMP_DEBUG_ASSERT(NtQuerySystemInformation != NULL); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 700 | |
Andrey Churbanov | 7daf980 | 2015-01-27 16:52:57 +0000 | [diff] [blame] | 701 | #if KMP_GROUP_AFFINITY |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 702 | // Load kernel32.dll. |
| 703 | // Same caveat - must use full system path name. |
| 704 | if (path_size > 0 && path_size < path.size) { |
| 705 | // Truncate the buffer back to just the system path length, |
| 706 | // discarding "\\ntdll.dll", and replacing it with "kernel32.dll". |
| 707 | path.used = path_size; |
| 708 | __kmp_str_buf_print(&path, "\\%s", "kernel32.dll"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 709 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 710 | // Load kernel32.dll using full path. |
| 711 | kernel32 = GetModuleHandle(path.str); |
| 712 | KA_TRACE(10, ("__kmp_runtime_initialize: kernel32.dll = %s\n", path.str)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 713 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 714 | // Load the function pointers to kernel32.dll routines |
| 715 | // that may or may not exist on this system. |
| 716 | if (kernel32 != NULL) { |
| 717 | __kmp_GetActiveProcessorCount = |
| 718 | (kmp_GetActiveProcessorCount_t)GetProcAddress( |
| 719 | kernel32, "GetActiveProcessorCount"); |
| 720 | __kmp_GetActiveProcessorGroupCount = |
| 721 | (kmp_GetActiveProcessorGroupCount_t)GetProcAddress( |
| 722 | kernel32, "GetActiveProcessorGroupCount"); |
| 723 | __kmp_GetThreadGroupAffinity = |
| 724 | (kmp_GetThreadGroupAffinity_t)GetProcAddress( |
| 725 | kernel32, "GetThreadGroupAffinity"); |
| 726 | __kmp_SetThreadGroupAffinity = |
| 727 | (kmp_SetThreadGroupAffinity_t)GetProcAddress( |
| 728 | kernel32, "SetThreadGroupAffinity"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 729 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 730 | KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_GetActiveProcessorCount" |
| 731 | " = %p\n", |
| 732 | __kmp_GetActiveProcessorCount)); |
| 733 | KA_TRACE(10, ("__kmp_runtime_initialize: " |
| 734 | "__kmp_GetActiveProcessorGroupCount = %p\n", |
| 735 | __kmp_GetActiveProcessorGroupCount)); |
| 736 | KA_TRACE(10, ("__kmp_runtime_initialize:__kmp_GetThreadGroupAffinity" |
| 737 | " = %p\n", |
| 738 | __kmp_GetThreadGroupAffinity)); |
| 739 | KA_TRACE(10, ("__kmp_runtime_initialize: __kmp_SetThreadGroupAffinity" |
| 740 | " = %p\n", |
| 741 | __kmp_SetThreadGroupAffinity)); |
| 742 | KA_TRACE(10, ("__kmp_runtime_initialize: sizeof(kmp_affin_mask_t) = %d\n", |
| 743 | sizeof(kmp_affin_mask_t))); |
Andrey Churbanov | df6555b | 2015-01-27 17:00:03 +0000 | [diff] [blame] | 744 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 745 | // See if group affinity is supported on this system. |
| 746 | // If so, calculate the #groups and #procs. |
| 747 | // |
| 748 | // Group affinity was introduced with Windows* 7 OS and |
| 749 | // Windows* Server 2008 R2 OS. |
| 750 | if ((__kmp_GetActiveProcessorCount != NULL) && |
| 751 | (__kmp_GetActiveProcessorGroupCount != NULL) && |
| 752 | (__kmp_GetThreadGroupAffinity != NULL) && |
| 753 | (__kmp_SetThreadGroupAffinity != NULL) && |
| 754 | ((__kmp_num_proc_groups = __kmp_GetActiveProcessorGroupCount()) > |
| 755 | 1)) { |
| 756 | // Calculate the total number of active OS procs. |
| 757 | int i; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 758 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 759 | KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups" |
| 760 | " detected\n", |
| 761 | __kmp_num_proc_groups)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 762 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 763 | __kmp_xproc = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 764 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 765 | for (i = 0; i < __kmp_num_proc_groups; i++) { |
| 766 | DWORD size = __kmp_GetActiveProcessorCount(i); |
| 767 | __kmp_xproc += size; |
| 768 | KA_TRACE(10, ("__kmp_runtime_initialize: proc group %d size = %d\n", |
| 769 | i, size)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 770 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 771 | } else { |
| 772 | KA_TRACE(10, ("__kmp_runtime_initialize: %d processor groups" |
| 773 | " detected\n", |
| 774 | __kmp_num_proc_groups)); |
| 775 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 776 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 777 | } |
| 778 | if (__kmp_num_proc_groups <= 1) { |
| 779 | GetSystemInfo(&info); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 780 | __kmp_xproc = info.dwNumberOfProcessors; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 781 | } |
| 782 | #else |
| 783 | GetSystemInfo(&info); |
| 784 | __kmp_xproc = info.dwNumberOfProcessors; |
Andrey Churbanov | 7daf980 | 2015-01-27 16:52:57 +0000 | [diff] [blame] | 785 | #endif /* KMP_GROUP_AFFINITY */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 786 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 787 | // If the OS said there were 0 procs, take a guess and use a value of 2. |
| 788 | // This is done for Linux* OS, also. Do we need error / warning? |
| 789 | if (__kmp_xproc <= 0) { |
| 790 | __kmp_xproc = 2; |
| 791 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 792 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 793 | KA_TRACE(5, |
| 794 | ("__kmp_runtime_initialize: total processors = %d\n", __kmp_xproc)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 795 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 796 | __kmp_str_buf_free(&path); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 797 | |
| 798 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 799 | __kmp_itt_initialize(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 800 | #endif /* USE_ITT_BUILD */ |
| 801 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 802 | __kmp_init_runtime = TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 803 | } // __kmp_runtime_initialize |
| 804 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 805 | void __kmp_runtime_destroy(void) { |
| 806 | if (!__kmp_init_runtime) { |
| 807 | return; |
| 808 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 809 | |
| 810 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 811 | __kmp_itt_destroy(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 812 | #endif /* USE_ITT_BUILD */ |
| 813 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 814 | /* we can't DeleteCriticalsection( & __kmp_win32_section ); */ |
| 815 | /* due to the KX_TRACE() commands */ |
| 816 | KA_TRACE(40, ("__kmp_runtime_destroy\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 817 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 818 | if (__kmp_gtid_threadprivate_key) { |
| 819 | TlsFree(__kmp_gtid_threadprivate_key); |
| 820 | __kmp_gtid_threadprivate_key = 0; |
| 821 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 822 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 823 | __kmp_affinity_uninitialize(); |
| 824 | DeleteCriticalSection(&__kmp_win32_section); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 825 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 826 | ntdll = NULL; |
| 827 | NtQuerySystemInformation = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 828 | |
| 829 | #if KMP_ARCH_X86_64 |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 830 | kernel32 = NULL; |
| 831 | __kmp_GetActiveProcessorCount = NULL; |
| 832 | __kmp_GetActiveProcessorGroupCount = NULL; |
| 833 | __kmp_GetThreadGroupAffinity = NULL; |
| 834 | __kmp_SetThreadGroupAffinity = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 835 | #endif // KMP_ARCH_X86_64 |
| 836 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 837 | __kmp_init_runtime = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 840 | void __kmp_terminate_thread(int gtid) { |
| 841 | kmp_info_t *th = __kmp_threads[gtid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 842 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 843 | if (!th) |
| 844 | return; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 845 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 846 | KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 847 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 848 | if (TerminateThread(th->th.th_info.ds.ds_thread, (DWORD)-1) == FALSE) { |
| 849 | /* It's OK, the thread may have exited already */ |
| 850 | } |
| 851 | __kmp_free_handle(th->th.th_info.ds.ds_thread); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 854 | void __kmp_clear_system_time(void) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 855 | BOOL status; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 856 | LARGE_INTEGER time; |
| 857 | status = QueryPerformanceCounter(&time); |
| 858 | __kmp_win32_time = (kmp_int64)time.QuadPart; |
| 859 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 860 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 861 | void __kmp_initialize_system_tick(void) { |
| 862 | { |
| 863 | BOOL status; |
| 864 | LARGE_INTEGER freq; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 865 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 866 | status = QueryPerformanceFrequency(&freq); |
| 867 | if (!status) { |
| 868 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 869 | __kmp_fatal(KMP_MSG(FunctionError, "QueryPerformanceFrequency()"), |
| 870 | KMP_ERR(error), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 871 | |
| 872 | } else { |
| 873 | __kmp_win32_tick = ((double)1.0) / (double)freq.QuadPart; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 874 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 875 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* Calculate the elapsed wall clock time for the user */ |
| 879 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 880 | void __kmp_elapsed(double *t) { |
| 881 | BOOL status; |
| 882 | LARGE_INTEGER now; |
| 883 | status = QueryPerformanceCounter(&now); |
| 884 | *t = ((double)now.QuadPart) * __kmp_win32_tick; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* Calculate the elapsed wall clock tick for the user */ |
| 888 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 889 | void __kmp_elapsed_tick(double *t) { *t = __kmp_win32_tick; } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 890 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 891 | void __kmp_read_system_time(double *delta) { |
| 892 | if (delta != NULL) { |
| 893 | BOOL status; |
| 894 | LARGE_INTEGER now; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 895 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 896 | status = QueryPerformanceCounter(&now); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 897 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 898 | *delta = ((double)(((kmp_int64)now.QuadPart) - __kmp_win32_time)) * |
| 899 | __kmp_win32_tick; |
| 900 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 903 | /* Return the current time stamp in nsec */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 904 | kmp_uint64 __kmp_now_nsec() { |
| 905 | LARGE_INTEGER now; |
| 906 | QueryPerformanceCounter(&now); |
| 907 | return 1e9 * __kmp_win32_tick * now.QuadPart; |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Andrey Churbanov | f700e9e | 2018-12-10 13:45:00 +0000 | [diff] [blame] | 910 | extern "C" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 911 | void *__stdcall __kmp_launch_worker(void *arg) { |
| 912 | volatile void *stack_data; |
| 913 | void *exit_val; |
| 914 | void *padding = 0; |
| 915 | kmp_info_t *this_thr = (kmp_info_t *)arg; |
| 916 | int gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 917 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 918 | gtid = this_thr->th.th_info.ds.ds_gtid; |
| 919 | __kmp_gtid_set_specific(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 920 | #ifdef KMP_TDATA_GTID |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 921 | #error "This define causes problems with LoadLibrary() + declspec(thread) " \ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 922 | "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \ |
| 923 | "reference: http://support.microsoft.com/kb/118816" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 924 | //__kmp_gtid = gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 925 | #endif |
| 926 | |
| 927 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 928 | __kmp_itt_thread_name(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 929 | #endif /* USE_ITT_BUILD */ |
| 930 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 931 | __kmp_affinity_set_init_mask(gtid, FALSE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 932 | |
| 933 | #if KMP_ARCH_X86 || KMP_ARCH_X86_64 |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 934 | // Set FP control regs to be a copy of the parallel initialization thread's. |
| 935 | __kmp_clear_x87_fpu_status_word(); |
| 936 | __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word); |
| 937 | __kmp_load_mxcsr(&__kmp_init_mxcsr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 938 | #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ |
| 939 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 940 | if (__kmp_stkoffset > 0 && gtid > 0) { |
| 941 | padding = KMP_ALLOCA(gtid * __kmp_stkoffset); |
| 942 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 943 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 944 | KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive); |
| 945 | this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId(); |
| 946 | TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 947 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 948 | if (TCR_4(__kmp_gtid_mode) < |
| 949 | 2) { // check stack only if it is used to get gtid |
| 950 | TCW_PTR(this_thr->th.th_info.ds.ds_stackbase, &stack_data); |
| 951 | KMP_ASSERT(this_thr->th.th_info.ds.ds_stackgrow == FALSE); |
| 952 | __kmp_check_stack_overlap(this_thr); |
| 953 | } |
| 954 | KMP_MB(); |
| 955 | exit_val = __kmp_launch_thread(this_thr); |
| 956 | KMP_FSYNC_RELEASING(&this_thr->th.th_info.ds.ds_alive); |
| 957 | TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE); |
| 958 | KMP_MB(); |
| 959 | return exit_val; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 962 | #if KMP_USE_MONITOR |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 963 | /* The monitor thread controls all of the threads in the complex */ |
| 964 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 965 | void *__stdcall __kmp_launch_monitor(void *arg) { |
| 966 | DWORD wait_status; |
| 967 | kmp_thread_t monitor; |
| 968 | int status; |
| 969 | int interval; |
| 970 | kmp_info_t *this_thr = (kmp_info_t *)arg; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 971 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 972 | KMP_DEBUG_ASSERT(__kmp_init_monitor); |
| 973 | TCW_4(__kmp_init_monitor, 2); // AC: Signal library that monitor has started |
| 974 | // TODO: hide "2" in enum (like {true,false,started}) |
| 975 | this_thr->th.th_info.ds.ds_thread_id = GetCurrentThreadId(); |
| 976 | TCW_4(this_thr->th.th_info.ds.ds_alive, TRUE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 977 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 978 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 979 | KA_TRACE(10, ("__kmp_launch_monitor: launched\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 980 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 981 | monitor = GetCurrentThread(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 982 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 983 | /* set thread priority */ |
| 984 | status = SetThreadPriority(monitor, THREAD_PRIORITY_HIGHEST); |
| 985 | if (!status) { |
| 986 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 987 | __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 988 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 989 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 990 | /* register us as monitor */ |
| 991 | __kmp_gtid_set_specific(KMP_GTID_MONITOR); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 992 | #ifdef KMP_TDATA_GTID |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 993 | #error "This define causes problems with LoadLibrary() + declspec(thread) " \ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 994 | "on Windows* OS. See CQ50564, tests kmp_load_library*.c and this MSDN " \ |
| 995 | "reference: http://support.microsoft.com/kb/118816" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 996 | //__kmp_gtid = KMP_GTID_MONITOR; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 997 | #endif |
| 998 | |
| 999 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1000 | __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore |
| 1001 | // monitor thread. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1002 | #endif /* USE_ITT_BUILD */ |
| 1003 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1004 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1005 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1006 | interval = (1000 / __kmp_monitor_wakeups); /* in milliseconds */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1007 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1008 | while (!TCR_4(__kmp_global.g.g_done)) { |
| 1009 | /* This thread monitors the state of the system */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1010 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1011 | KA_TRACE(15, ("__kmp_launch_monitor: update\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1012 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1013 | wait_status = WaitForSingleObject(__kmp_monitor_ev, interval); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1014 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1015 | if (wait_status == WAIT_TIMEOUT) { |
| 1016 | TCW_4(__kmp_global.g.g_time.dt.t_value, |
| 1017 | TCR_4(__kmp_global.g.g_time.dt.t_value) + 1); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1020 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 1021 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1022 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1023 | KA_TRACE(10, ("__kmp_launch_monitor: finished\n")); |
| 1024 | |
| 1025 | status = SetThreadPriority(monitor, THREAD_PRIORITY_NORMAL); |
| 1026 | if (!status) { |
| 1027 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1028 | __kmp_fatal(KMP_MSG(CantSetThreadPriority), KMP_ERR(error), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | if (__kmp_global.g.g_abort != 0) { |
| 1032 | /* now we need to terminate the worker threads */ |
| 1033 | /* the value of t_abort is the signal we caught */ |
| 1034 | int gtid; |
| 1035 | |
| 1036 | KA_TRACE(10, ("__kmp_launch_monitor: terminate sig=%d\n", |
| 1037 | (__kmp_global.g.g_abort))); |
| 1038 | |
| 1039 | /* terminate the OpenMP worker threads */ |
| 1040 | /* TODO this is not valid for sibling threads!! |
| 1041 | * the uber master might not be 0 anymore.. */ |
| 1042 | for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid) |
| 1043 | __kmp_terminate_thread(gtid); |
| 1044 | |
| 1045 | __kmp_cleanup(); |
| 1046 | |
| 1047 | Sleep(0); |
| 1048 | |
| 1049 | KA_TRACE(10, |
| 1050 | ("__kmp_launch_monitor: raise sig=%d\n", __kmp_global.g.g_abort)); |
| 1051 | |
| 1052 | if (__kmp_global.g.g_abort > 0) { |
| 1053 | raise(__kmp_global.g.g_abort); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1054 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1055 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1056 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1057 | TCW_4(this_thr->th.th_info.ds.ds_alive, FALSE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1058 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1059 | KMP_MB(); |
| 1060 | return arg; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1061 | } |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1062 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1063 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1064 | void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) { |
| 1065 | kmp_thread_t handle; |
| 1066 | DWORD idThread; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1067 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1068 | KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1069 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1070 | th->th.th_info.ds.ds_gtid = gtid; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1071 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1072 | if (KMP_UBER_GTID(gtid)) { |
| 1073 | int stack_data; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1074 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1075 | /* TODO: GetCurrentThread() returns a pseudo-handle that is unsuitable for |
| 1076 | other threads to use. Is it appropriate to just use GetCurrentThread? |
| 1077 | When should we close this handle? When unregistering the root? */ |
| 1078 | { |
| 1079 | BOOL rc; |
| 1080 | rc = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), |
| 1081 | GetCurrentProcess(), &th->th.th_info.ds.ds_thread, 0, |
| 1082 | FALSE, DUPLICATE_SAME_ACCESS); |
| 1083 | KMP_ASSERT(rc); |
| 1084 | KA_TRACE(10, (" __kmp_create_worker: ROOT Handle duplicated, th = %p, " |
| 1085 | "handle = %" KMP_UINTPTR_SPEC "\n", |
| 1086 | (LPVOID)th, th->th.th_info.ds.ds_thread)); |
| 1087 | th->th.th_info.ds.ds_thread_id = GetCurrentThreadId(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1088 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1089 | if (TCR_4(__kmp_gtid_mode) < 2) { // check stack only if used to get gtid |
| 1090 | /* we will dynamically update the stack range if gtid_mode == 1 */ |
| 1091 | TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data); |
| 1092 | TCW_PTR(th->th.th_info.ds.ds_stacksize, 0); |
| 1093 | TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE); |
| 1094 | __kmp_check_stack_overlap(th); |
| 1095 | } |
| 1096 | } else { |
| 1097 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1098 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1099 | /* Set stack size for this thread now. */ |
| 1100 | KA_TRACE(10, |
| 1101 | ("__kmp_create_worker: stack_size = %" KMP_SIZE_T_SPEC " bytes\n", |
| 1102 | stack_size)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1103 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1104 | stack_size += gtid * __kmp_stkoffset; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1105 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1106 | TCW_PTR(th->th.th_info.ds.ds_stacksize, stack_size); |
| 1107 | TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1108 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1109 | KA_TRACE(10, |
| 1110 | ("__kmp_create_worker: (before) stack_size = %" KMP_SIZE_T_SPEC |
| 1111 | " bytes, &__kmp_launch_worker = %p, th = %p, &idThread = %p\n", |
| 1112 | (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker, |
| 1113 | (LPVOID)th, &idThread)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1114 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1115 | handle = CreateThread( |
| 1116 | NULL, (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)__kmp_launch_worker, |
| 1117 | (LPVOID)th, STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1118 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1119 | KA_TRACE(10, |
| 1120 | ("__kmp_create_worker: (after) stack_size = %" KMP_SIZE_T_SPEC |
| 1121 | " bytes, &__kmp_launch_worker = %p, th = %p, " |
| 1122 | "idThread = %u, handle = %" KMP_UINTPTR_SPEC "\n", |
| 1123 | (SIZE_T)stack_size, (LPTHREAD_START_ROUTINE)&__kmp_launch_worker, |
| 1124 | (LPVOID)th, idThread, handle)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1125 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1126 | if (handle == 0) { |
| 1127 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1128 | __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1129 | } else { |
| 1130 | th->th.th_info.ds.ds_thread = handle; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1131 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1132 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1133 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 1134 | } |
| 1135 | |
| 1136 | KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1139 | int __kmp_still_running(kmp_info_t *th) { |
| 1140 | return (WAIT_TIMEOUT == WaitForSingleObject(th->th.th_info.ds.ds_thread, 0)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1143 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1144 | void __kmp_create_monitor(kmp_info_t *th) { |
| 1145 | kmp_thread_t handle; |
| 1146 | DWORD idThread; |
| 1147 | int ideal, new_ideal; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1148 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1149 | if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) { |
| 1150 | // We don't need monitor thread in case of MAX_BLOCKTIME |
| 1151 | KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of " |
| 1152 | "MAX blocktime\n")); |
| 1153 | th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op |
| 1154 | th->th.th_info.ds.ds_gtid = 0; |
| 1155 | TCW_4(__kmp_init_monitor, 2); // Signal to stop waiting for monitor creation |
| 1156 | return; |
| 1157 | } |
| 1158 | KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1159 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1160 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1161 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1162 | __kmp_monitor_ev = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1163 | if (__kmp_monitor_ev == NULL) { |
| 1164 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1165 | __kmp_fatal(KMP_MSG(CantCreateEvent), KMP_ERR(error), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1166 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1167 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1168 | __kmp_itt_system_object_created(__kmp_monitor_ev, "Event"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1169 | #endif /* USE_ITT_BUILD */ |
| 1170 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1171 | th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR; |
| 1172 | th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1173 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1174 | // FIXME - on Windows* OS, if __kmp_monitor_stksize = 0, figure out how |
| 1175 | // to automatically expand stacksize based on CreateThread error code. |
| 1176 | if (__kmp_monitor_stksize == 0) { |
| 1177 | __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE; |
| 1178 | } |
| 1179 | if (__kmp_monitor_stksize < __kmp_sys_min_stksize) { |
| 1180 | __kmp_monitor_stksize = __kmp_sys_min_stksize; |
| 1181 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1182 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1183 | KA_TRACE(10, ("__kmp_create_monitor: requested stacksize = %d bytes\n", |
| 1184 | (int)__kmp_monitor_stksize)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1185 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1186 | TCW_4(__kmp_global.g.g_time.dt.t_value, 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1187 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1188 | handle = |
| 1189 | CreateThread(NULL, (SIZE_T)__kmp_monitor_stksize, |
| 1190 | (LPTHREAD_START_ROUTINE)__kmp_launch_monitor, (LPVOID)th, |
| 1191 | STACK_SIZE_PARAM_IS_A_RESERVATION, &idThread); |
| 1192 | if (handle == 0) { |
| 1193 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1194 | __kmp_fatal(KMP_MSG(CantCreateThread), KMP_ERR(error), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1195 | } else |
| 1196 | th->th.th_info.ds.ds_thread = handle; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1197 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1198 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1199 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1200 | KA_TRACE(10, ("__kmp_create_monitor: monitor created %p\n", |
| 1201 | (void *)th->th.th_info.ds.ds_thread)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1202 | } |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1203 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1204 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1205 | /* Check to see if thread is still alive. |
| 1206 | NOTE: The ExitProcess(code) system call causes all threads to Terminate |
| 1207 | with a exit_val = code. Because of this we can not rely on exit_val having |
| 1208 | any particular value. So this routine may return STILL_ALIVE in exit_val |
| 1209 | even after the thread is dead. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1210 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1211 | int __kmp_is_thread_alive(kmp_info_t *th, DWORD *exit_val) { |
| 1212 | DWORD rc; |
| 1213 | rc = GetExitCodeThread(th->th.th_info.ds.ds_thread, exit_val); |
| 1214 | if (rc == 0) { |
| 1215 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1216 | __kmp_fatal(KMP_MSG(FunctionError, "GetExitCodeThread()"), KMP_ERR(error), |
| 1217 | __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1218 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1219 | return (*exit_val == STILL_ACTIVE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1222 | void __kmp_exit_thread(int exit_status) { |
| 1223 | ExitThread(exit_status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1224 | } // __kmp_exit_thread |
| 1225 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1226 | // This is a common part for both __kmp_reap_worker() and __kmp_reap_monitor(). |
| 1227 | static void __kmp_reap_common(kmp_info_t *th) { |
| 1228 | DWORD exit_val; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1229 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1230 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1231 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1232 | KA_TRACE( |
| 1233 | 10, ("__kmp_reap_common: try to reap (%d)\n", th->th.th_info.ds.ds_gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1234 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1235 | /* 2006-10-19: |
| 1236 | There are two opposite situations: |
| 1237 | 1. Windows* OS keep thread alive after it resets ds_alive flag and |
| 1238 | exits from thread function. (For example, see C70770/Q394281 "unloading of |
| 1239 | dll based on OMP is very slow".) |
| 1240 | 2. Windows* OS may kill thread before it resets ds_alive flag. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1241 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1242 | Right solution seems to be waiting for *either* thread termination *or* |
| 1243 | ds_alive resetting. */ |
| 1244 | { |
Jonathan Peyton | e47d32f | 2019-02-28 19:11:29 +0000 | [diff] [blame] | 1245 | // TODO: This code is very similar to KMP_WAIT. Need to generalize |
| 1246 | // KMP_WAIT to cover this usage also. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1247 | void *obj = NULL; |
Ed Maste | 414544c | 2017-07-07 21:06:05 +0000 | [diff] [blame] | 1248 | kmp_uint32 spins; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1249 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1250 | KMP_FSYNC_SPIN_INIT(obj, (void *)&th->th.th_info.ds.ds_alive); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1251 | #endif /* USE_ITT_BUILD */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1252 | KMP_INIT_YIELD(spins); |
| 1253 | do { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1254 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1255 | KMP_FSYNC_SPIN_PREPARE(obj); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1256 | #endif /* USE_ITT_BUILD */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1257 | __kmp_is_thread_alive(th, &exit_val); |
Jonathan Peyton | e47d32f | 2019-02-28 19:11:29 +0000 | [diff] [blame] | 1258 | KMP_YIELD_OVERSUB_ELSE_SPIN(spins); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1259 | } while (exit_val == STILL_ACTIVE && TCR_4(th->th.th_info.ds.ds_alive)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1260 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1261 | if (exit_val == STILL_ACTIVE) { |
| 1262 | KMP_FSYNC_CANCEL(obj); |
| 1263 | } else { |
| 1264 | KMP_FSYNC_SPIN_ACQUIRED(obj); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1265 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1266 | #endif /* USE_ITT_BUILD */ |
| 1267 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1268 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1269 | __kmp_free_handle(th->th.th_info.ds.ds_thread); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1270 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1271 | /* NOTE: The ExitProcess(code) system call causes all threads to Terminate |
| 1272 | with a exit_val = code. Because of this we can not rely on exit_val having |
| 1273 | any particular value. */ |
| 1274 | if (exit_val == STILL_ACTIVE) { |
| 1275 | KA_TRACE(1, ("__kmp_reap_common: thread still active.\n")); |
| 1276 | } else if ((void *)exit_val != (void *)th) { |
| 1277 | KA_TRACE(1, ("__kmp_reap_common: ExitProcess / TerminateThread used?\n")); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1278 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1279 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1280 | KA_TRACE(10, |
| 1281 | ("__kmp_reap_common: done reaping (%d), handle = %" KMP_UINTPTR_SPEC |
| 1282 | "\n", |
| 1283 | th->th.th_info.ds.ds_gtid, th->th.th_info.ds.ds_thread)); |
| 1284 | |
| 1285 | th->th.th_info.ds.ds_thread = 0; |
| 1286 | th->th.th_info.ds.ds_tid = KMP_GTID_DNE; |
| 1287 | th->th.th_info.ds.ds_gtid = KMP_GTID_DNE; |
| 1288 | th->th.th_info.ds.ds_thread_id = 0; |
| 1289 | |
| 1290 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1293 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1294 | void __kmp_reap_monitor(kmp_info_t *th) { |
| 1295 | int status; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1296 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1297 | KA_TRACE(10, ("__kmp_reap_monitor: try to reap %p\n", |
| 1298 | (void *)th->th.th_info.ds.ds_thread)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1299 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1300 | // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR. |
| 1301 | // If both tid and gtid are 0, it means the monitor did not ever start. |
| 1302 | // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down. |
| 1303 | KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid); |
| 1304 | if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) { |
| 1305 | KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n")); |
| 1306 | return; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1307 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1308 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1309 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1310 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1311 | status = SetEvent(__kmp_monitor_ev); |
| 1312 | if (status == FALSE) { |
| 1313 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1314 | __kmp_fatal(KMP_MSG(CantSetEvent), KMP_ERR(error), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1315 | } |
| 1316 | KA_TRACE(10, ("__kmp_reap_monitor: reaping thread (%d)\n", |
| 1317 | th->th.th_info.ds.ds_gtid)); |
| 1318 | __kmp_reap_common(th); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1319 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1320 | __kmp_free_handle(__kmp_monitor_ev); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1321 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1322 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1323 | } |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1324 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1325 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1326 | void __kmp_reap_worker(kmp_info_t *th) { |
| 1327 | KA_TRACE(10, ("__kmp_reap_worker: reaping thread (%d)\n", |
| 1328 | th->th.th_info.ds.ds_gtid)); |
| 1329 | __kmp_reap_common(th); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1332 | #if KMP_HANDLE_SIGNALS |
| 1333 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1334 | static void __kmp_team_handler(int signo) { |
| 1335 | if (__kmp_global.g.g_abort == 0) { |
| 1336 | // Stage 1 signal handler, let's shut down all of the threads. |
| 1337 | if (__kmp_debug_buf) { |
| 1338 | __kmp_dump_debug_buffer(); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1339 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1340 | KMP_MB(); // Flush all pending memory write invalidates. |
| 1341 | TCW_4(__kmp_global.g.g_abort, signo); |
| 1342 | KMP_MB(); // Flush all pending memory write invalidates. |
| 1343 | TCW_4(__kmp_global.g.g_done, TRUE); |
| 1344 | KMP_MB(); // Flush all pending memory write invalidates. |
| 1345 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1346 | } // __kmp_team_handler |
| 1347 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1348 | static sig_func_t __kmp_signal(int signum, sig_func_t handler) { |
| 1349 | sig_func_t old = signal(signum, handler); |
| 1350 | if (old == SIG_ERR) { |
| 1351 | int error = errno; |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1352 | __kmp_fatal(KMP_MSG(FunctionError, "signal"), KMP_ERR(error), |
| 1353 | __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1354 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1355 | return old; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1358 | static void __kmp_install_one_handler(int sig, sig_func_t handler, |
| 1359 | int parallel_init) { |
| 1360 | sig_func_t old; |
| 1361 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 1362 | KB_TRACE(60, ("__kmp_install_one_handler: called: sig=%d\n", sig)); |
| 1363 | if (parallel_init) { |
| 1364 | old = __kmp_signal(sig, handler); |
| 1365 | // SIG_DFL on Windows* OS in NULL or 0. |
| 1366 | if (old == __kmp_sighldrs[sig]) { |
| 1367 | __kmp_siginstalled[sig] = 1; |
| 1368 | } else { // Restore/keep user's handler if one previously installed. |
| 1369 | old = __kmp_signal(sig, old); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1370 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1371 | } else { |
| 1372 | // Save initial/system signal handlers to see if user handlers installed. |
| 1373 | // 2009-09-23: It is a dead code. On Windows* OS __kmp_install_signals |
| 1374 | // called once with parallel_init == TRUE. |
| 1375 | old = __kmp_signal(sig, SIG_DFL); |
| 1376 | __kmp_sighldrs[sig] = old; |
| 1377 | __kmp_signal(sig, old); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1378 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1379 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1380 | } // __kmp_install_one_handler |
| 1381 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1382 | static void __kmp_remove_one_handler(int sig) { |
| 1383 | if (__kmp_siginstalled[sig]) { |
| 1384 | sig_func_t old; |
| 1385 | KMP_MB(); // Flush all pending memory write invalidates. |
| 1386 | KB_TRACE(60, ("__kmp_remove_one_handler: called: sig=%d\n", sig)); |
| 1387 | old = __kmp_signal(sig, __kmp_sighldrs[sig]); |
| 1388 | if (old != __kmp_team_handler) { |
| 1389 | KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, " |
| 1390 | "restoring: sig=%d\n", |
| 1391 | sig)); |
| 1392 | old = __kmp_signal(sig, old); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1393 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1394 | __kmp_sighldrs[sig] = NULL; |
| 1395 | __kmp_siginstalled[sig] = 0; |
| 1396 | KMP_MB(); // Flush all pending memory write invalidates. |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1397 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1398 | } // __kmp_remove_one_handler |
| 1399 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1400 | void __kmp_install_signals(int parallel_init) { |
| 1401 | KB_TRACE(10, ("__kmp_install_signals: called\n")); |
| 1402 | if (!__kmp_handle_signals) { |
| 1403 | KB_TRACE(10, ("__kmp_install_signals: KMP_HANDLE_SIGNALS is false - " |
| 1404 | "handlers not installed\n")); |
| 1405 | return; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1406 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1407 | __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init); |
| 1408 | __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init); |
| 1409 | __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init); |
| 1410 | __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init); |
| 1411 | __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init); |
| 1412 | __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1413 | } // __kmp_install_signals |
| 1414 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1415 | void __kmp_remove_signals(void) { |
| 1416 | int sig; |
| 1417 | KB_TRACE(10, ("__kmp_remove_signals: called\n")); |
| 1418 | for (sig = 1; sig < NSIG; ++sig) { |
| 1419 | __kmp_remove_one_handler(sig); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1420 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1421 | } // __kmp_remove_signals |
| 1422 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1423 | #endif // KMP_HANDLE_SIGNALS |
| 1424 | |
| 1425 | /* Put the thread to sleep for a time period */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1426 | void __kmp_thread_sleep(int millis) { |
| 1427 | DWORD status; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1428 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1429 | status = SleepEx((DWORD)millis, FALSE); |
| 1430 | if (status) { |
| 1431 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1432 | __kmp_fatal(KMP_MSG(FunctionError, "SleepEx()"), KMP_ERR(error), |
| 1433 | __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1434 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1437 | // Determine whether the given address is mapped into the current address space. |
| 1438 | int __kmp_is_address_mapped(void *addr) { |
| 1439 | DWORD status; |
| 1440 | MEMORY_BASIC_INFORMATION lpBuffer; |
| 1441 | SIZE_T dwLength; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1442 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1443 | dwLength = sizeof(MEMORY_BASIC_INFORMATION); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1444 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1445 | status = VirtualQuery(addr, &lpBuffer, dwLength); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1446 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1447 | return !(((lpBuffer.State == MEM_RESERVE) || (lpBuffer.State == MEM_FREE)) || |
| 1448 | ((lpBuffer.Protect == PAGE_NOACCESS) || |
| 1449 | (lpBuffer.Protect == PAGE_EXECUTE))); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1452 | kmp_uint64 __kmp_hardware_timestamp(void) { |
| 1453 | kmp_uint64 r = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1454 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1455 | QueryPerformanceCounter((LARGE_INTEGER *)&r); |
| 1456 | return r; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | /* Free handle and check the error code */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1460 | void __kmp_free_handle(kmp_thread_t tHandle) { |
| 1461 | /* called with parameter type HANDLE also, thus suppose kmp_thread_t defined |
| 1462 | * as HANDLE */ |
| 1463 | BOOL rc; |
| 1464 | rc = CloseHandle(tHandle); |
| 1465 | if (!rc) { |
| 1466 | DWORD error = GetLastError(); |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1467 | __kmp_fatal(KMP_MSG(CantCloseHandle), KMP_ERR(error), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1468 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1471 | int __kmp_get_load_balance(int max) { |
| 1472 | static ULONG glb_buff_size = 100 * 1024; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1473 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1474 | // Saved count of the running threads for the thread balance algortihm |
| 1475 | static int glb_running_threads = 0; |
| 1476 | static double glb_call_time = 0; /* Thread balance algorithm call time */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1477 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1478 | int running_threads = 0; // Number of running threads in the system. |
| 1479 | NTSTATUS status = 0; |
| 1480 | ULONG buff_size = 0; |
| 1481 | ULONG info_size = 0; |
| 1482 | void *buffer = NULL; |
| 1483 | PSYSTEM_PROCESS_INFORMATION spi = NULL; |
| 1484 | int first_time = 1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1485 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1486 | double call_time = 0.0; // start, finish; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1487 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1488 | __kmp_elapsed(&call_time); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1489 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1490 | if (glb_call_time && |
| 1491 | (call_time - glb_call_time < __kmp_load_balance_interval)) { |
| 1492 | running_threads = glb_running_threads; |
| 1493 | goto finish; |
| 1494 | } |
| 1495 | glb_call_time = call_time; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1496 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1497 | // Do not spend time on running algorithm if we have a permanent error. |
| 1498 | if (NtQuerySystemInformation == NULL) { |
| 1499 | running_threads = -1; |
| 1500 | goto finish; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1501 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (max <= 0) { |
| 1504 | max = INT_MAX; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1505 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1506 | |
| 1507 | do { |
| 1508 | |
| 1509 | if (first_time) { |
| 1510 | buff_size = glb_buff_size; |
| 1511 | } else { |
| 1512 | buff_size = 2 * buff_size; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1513 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1514 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1515 | buffer = KMP_INTERNAL_REALLOC(buffer, buff_size); |
| 1516 | if (buffer == NULL) { |
| 1517 | running_threads = -1; |
| 1518 | goto finish; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1519 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1520 | status = NtQuerySystemInformation(SystemProcessInformation, buffer, |
| 1521 | buff_size, &info_size); |
| 1522 | first_time = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1523 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1524 | } while (status == STATUS_INFO_LENGTH_MISMATCH); |
| 1525 | glb_buff_size = buff_size; |
| 1526 | |
| 1527 | #define CHECK(cond) \ |
| 1528 | { \ |
| 1529 | KMP_DEBUG_ASSERT(cond); \ |
| 1530 | if (!(cond)) { \ |
| 1531 | running_threads = -1; \ |
| 1532 | goto finish; \ |
| 1533 | } \ |
| 1534 | } |
| 1535 | |
| 1536 | CHECK(buff_size >= info_size); |
| 1537 | spi = PSYSTEM_PROCESS_INFORMATION(buffer); |
| 1538 | for (;;) { |
| 1539 | ptrdiff_t offset = uintptr_t(spi) - uintptr_t(buffer); |
| 1540 | CHECK(0 <= offset && |
| 1541 | offset + sizeof(SYSTEM_PROCESS_INFORMATION) < info_size); |
| 1542 | HANDLE pid = spi->ProcessId; |
| 1543 | ULONG num = spi->NumberOfThreads; |
| 1544 | CHECK(num >= 1); |
| 1545 | size_t spi_size = |
| 1546 | sizeof(SYSTEM_PROCESS_INFORMATION) + sizeof(SYSTEM_THREAD) * (num - 1); |
| 1547 | CHECK(offset + spi_size < |
| 1548 | info_size); // Make sure process info record fits the buffer. |
| 1549 | if (spi->NextEntryOffset != 0) { |
| 1550 | CHECK(spi_size <= |
| 1551 | spi->NextEntryOffset); // And do not overlap with the next record. |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1552 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1553 | // pid == 0 corresponds to the System Idle Process. It always has running |
| 1554 | // threads on all cores. So, we don't consider the running threads of this |
| 1555 | // process. |
| 1556 | if (pid != 0) { |
| 1557 | for (int i = 0; i < num; ++i) { |
| 1558 | THREAD_STATE state = spi->Threads[i].State; |
| 1559 | // Count threads that have Ready or Running state. |
| 1560 | // !!! TODO: Why comment does not match the code??? |
| 1561 | if (state == StateRunning) { |
| 1562 | ++running_threads; |
| 1563 | // Stop counting running threads if the number is already greater than |
| 1564 | // the number of available cores |
| 1565 | if (running_threads >= max) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1566 | goto finish; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1567 | } |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1571 | if (spi->NextEntryOffset == 0) { |
| 1572 | break; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1573 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1574 | spi = PSYSTEM_PROCESS_INFORMATION(uintptr_t(spi) + spi->NextEntryOffset); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1575 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1576 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1577 | #undef CHECK |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1578 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1579 | finish: // Clean up and exit. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1580 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1581 | if (buffer != NULL) { |
| 1582 | KMP_INTERNAL_FREE(buffer); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1583 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1584 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1585 | glb_running_threads = running_threads; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1586 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1587 | return running_threads; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1588 | } //__kmp_get_load_balance() |