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