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