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_Linux_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 | 1cdd87a | 2016-11-14 21:08:35 +0000 | [diff] [blame] | 14 | #include "kmp_affinity.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 15 | #include "kmp_i18n.h" |
| 16 | #include "kmp_io.h" |
| 17 | #include "kmp_itt.h" |
| 18 | #include "kmp_lock.h" |
| 19 | #include "kmp_stats.h" |
| 20 | #include "kmp_str.h" |
| 21 | #include "kmp_wait_release.h" |
| 22 | #include "kmp_wrapper_getpid.h" |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 23 | |
Kamil Rytarowski | 7e1ea99 | 2018-12-09 16:46:48 +0000 | [diff] [blame] | 24 | #if !KMP_OS_DRAGONFLY && !KMP_OS_FREEBSD && !KMP_OS_NETBSD && !KMP_OS_OPENBSD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 25 | #include <alloca.h> |
Alp Toker | 763b939 | 2014-02-28 09:42:41 +0000 | [diff] [blame] | 26 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 27 | #include <math.h> // HUGE_VAL. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 28 | #include <sys/resource.h> |
| 29 | #include <sys/syscall.h> |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 30 | #include <sys/time.h> |
| 31 | #include <sys/times.h> |
| 32 | #include <unistd.h> |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 33 | |
Jim Cownie | 3051f97 | 2014-08-07 10:12:54 +0000 | [diff] [blame] | 34 | #if KMP_OS_LINUX && !KMP_OS_CNK |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 35 | #include <sys/sysinfo.h> |
| 36 | #if KMP_USE_FUTEX |
| 37 | // We should really include <futex.h>, but that causes compatibility problems on |
| 38 | // different Linux* OS distributions that either require that you include (or |
| 39 | // break when you try to include) <pci/types.h>. Since all we need is the two |
| 40 | // macros below (which are part of the kernel ABI, so can't change) we just |
| 41 | // define the constants here and don't include <futex.h> |
| 42 | #ifndef FUTEX_WAIT |
| 43 | #define FUTEX_WAIT 0 |
| 44 | #endif |
| 45 | #ifndef FUTEX_WAKE |
| 46 | #define FUTEX_WAKE 1 |
| 47 | #endif |
| 48 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 49 | #elif KMP_OS_DARWIN |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 50 | #include <mach/mach.h> |
| 51 | #include <sys/sysctl.h> |
Kamil Rytarowski | a56ac94 | 2018-12-09 16:40:33 +0000 | [diff] [blame] | 52 | #elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 53 | #include <pthread_np.h> |
Kamil Rytarowski | 316f423 | 2018-12-11 18:35:07 +0000 | [diff] [blame] | 54 | #elif KMP_OS_NETBSD |
| 55 | #include <sys/types.h> |
| 56 | #include <sys/sysctl.h> |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 59 | #include <ctype.h> |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 60 | #include <dirent.h> |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 61 | #include <fcntl.h> |
| 62 | |
Jonas Hahnfeld | 50fed04 | 2016-11-07 15:58:36 +0000 | [diff] [blame] | 63 | #include "tsan_annotations.h" |
| 64 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 65 | struct kmp_sys_timer { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 66 | struct timespec start; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | // Convert timespec to nanoseconds. |
| 70 | #define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec) |
| 71 | |
| 72 | static struct kmp_sys_timer __kmp_sys_timer_data; |
| 73 | |
| 74 | #if KMP_HANDLE_SIGNALS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 75 | typedef void (*sig_func_t)(int); |
| 76 | STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[NSIG]; |
| 77 | static sigset_t __kmp_sigset; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 80 | static int __kmp_init_runtime = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 81 | |
| 82 | static int __kmp_fork_count = 0; |
| 83 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 84 | static pthread_condattr_t __kmp_suspend_cond_attr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 85 | static pthread_mutexattr_t __kmp_suspend_mutex_attr; |
| 86 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 87 | static kmp_cond_align_t __kmp_wait_cv; |
| 88 | static kmp_mutex_align_t __kmp_wait_mx; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 89 | |
Jonathan Peyton | 35d75ae | 2017-03-20 22:11:31 +0000 | [diff] [blame] | 90 | kmp_uint64 __kmp_ticks_per_msec = 1000000; |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 91 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 92 | #ifdef DEBUG_SUSPEND |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 93 | static void __kmp_print_cond(char *buffer, kmp_cond_align_t *cond) { |
| 94 | KMP_SNPRINTF(buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))", |
| 95 | cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock, |
| 96 | cond->c_cond.__c_waiting); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 97 | } |
| 98 | #endif |
| 99 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 100 | #if (KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 101 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 102 | /* Affinity support */ |
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 | void __kmp_affinity_bind_thread(int which) { |
| 105 | KMP_ASSERT2(KMP_AFFINITY_CAPABLE(), |
| 106 | "Illegal set affinity operation when not capable"); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 107 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 108 | kmp_affin_mask_t *mask; |
| 109 | KMP_CPU_ALLOC_ON_STACK(mask); |
| 110 | KMP_CPU_ZERO(mask); |
| 111 | KMP_CPU_SET(which, mask); |
| 112 | __kmp_set_system_affinity(mask, TRUE); |
| 113 | KMP_CPU_FREE_FROM_STACK(mask); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 116 | /* Determine if we can access affinity functionality on this version of |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 117 | * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 118 | * __kmp_affin_mask_size to the appropriate value (0 means not capable). */ |
| 119 | void __kmp_affinity_determine_capable(const char *env_var) { |
| 120 | // Check and see if the OS supports thread affinity. |
| 121 | |
| 122 | #define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024) |
| 123 | |
| 124 | int gCode; |
| 125 | int sCode; |
| 126 | unsigned char *buf; |
| 127 | buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT); |
| 128 | |
| 129 | // If Linux* OS: |
| 130 | // If the syscall fails or returns a suggestion for the size, |
| 131 | // then we don't have to search for an appropriate size. |
| 132 | gCode = syscall(__NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf); |
| 133 | KA_TRACE(30, ("__kmp_affinity_determine_capable: " |
| 134 | "initial getaffinity call returned %d errno = %d\n", |
| 135 | gCode, errno)); |
| 136 | |
| 137 | // if ((gCode < 0) && (errno == ENOSYS)) |
| 138 | if (gCode < 0) { |
| 139 | // System call not supported |
| 140 | if (__kmp_affinity_verbose || |
| 141 | (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) && |
| 142 | (__kmp_affinity_type != affinity_default) && |
| 143 | (__kmp_affinity_type != affinity_disabled))) { |
| 144 | int error = errno; |
| 145 | kmp_msg_t err_code = KMP_ERR(error); |
| 146 | __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var), |
| 147 | err_code, __kmp_msg_null); |
| 148 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 149 | __kmp_str_free(&err_code.str); |
| 150 | } |
| 151 | } |
| 152 | KMP_AFFINITY_DISABLE(); |
| 153 | KMP_INTERNAL_FREE(buf); |
| 154 | return; |
| 155 | } |
| 156 | if (gCode > 0) { // Linux* OS only |
| 157 | // The optimal situation: the OS returns the size of the buffer it expects. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 158 | // |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 159 | // A verification of correct behavior is that Isetaffinity on a NULL |
| 160 | // buffer with the same size fails with errno set to EFAULT. |
| 161 | sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL); |
| 162 | KA_TRACE(30, ("__kmp_affinity_determine_capable: " |
| 163 | "setaffinity for mask size %d returned %d errno = %d\n", |
| 164 | gCode, sCode, errno)); |
| 165 | if (sCode < 0) { |
| 166 | if (errno == ENOSYS) { |
| 167 | if (__kmp_affinity_verbose || |
| 168 | (__kmp_affinity_warnings && |
| 169 | (__kmp_affinity_type != affinity_none) && |
| 170 | (__kmp_affinity_type != affinity_default) && |
| 171 | (__kmp_affinity_type != affinity_disabled))) { |
| 172 | int error = errno; |
| 173 | kmp_msg_t err_code = KMP_ERR(error); |
| 174 | __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var), |
| 175 | err_code, __kmp_msg_null); |
| 176 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 177 | __kmp_str_free(&err_code.str); |
| 178 | } |
| 179 | } |
| 180 | KMP_AFFINITY_DISABLE(); |
| 181 | KMP_INTERNAL_FREE(buf); |
| 182 | } |
| 183 | if (errno == EFAULT) { |
| 184 | KMP_AFFINITY_ENABLE(gCode); |
| 185 | KA_TRACE(10, ("__kmp_affinity_determine_capable: " |
| 186 | "affinity supported (mask size %d)\n", |
| 187 | (int)__kmp_affin_mask_size)); |
| 188 | KMP_INTERNAL_FREE(buf); |
| 189 | return; |
| 190 | } |
| 191 | } |
| 192 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 193 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 194 | // Call the getaffinity system call repeatedly with increasing set sizes |
| 195 | // until we succeed, or reach an upper bound on the search. |
| 196 | KA_TRACE(30, ("__kmp_affinity_determine_capable: " |
| 197 | "searching for proper set size\n")); |
| 198 | int size; |
| 199 | for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) { |
| 200 | gCode = syscall(__NR_sched_getaffinity, 0, size, buf); |
| 201 | KA_TRACE(30, ("__kmp_affinity_determine_capable: " |
| 202 | "getaffinity for mask size %d returned %d errno = %d\n", |
| 203 | size, gCode, errno)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 204 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 205 | if (gCode < 0) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 206 | if (errno == ENOSYS) { |
| 207 | // We shouldn't get here |
| 208 | KA_TRACE(30, ("__kmp_affinity_determine_capable: " |
| 209 | "inconsistent OS call behavior: errno == ENOSYS for mask " |
| 210 | "size %d\n", |
| 211 | size)); |
| 212 | if (__kmp_affinity_verbose || |
| 213 | (__kmp_affinity_warnings && |
| 214 | (__kmp_affinity_type != affinity_none) && |
| 215 | (__kmp_affinity_type != affinity_default) && |
| 216 | (__kmp_affinity_type != affinity_disabled))) { |
| 217 | int error = errno; |
| 218 | kmp_msg_t err_code = KMP_ERR(error); |
| 219 | __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var), |
| 220 | err_code, __kmp_msg_null); |
| 221 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 222 | __kmp_str_free(&err_code.str); |
| 223 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 224 | } |
Andrey Churbanov | 1f037e4 | 2015-03-10 09:15:26 +0000 | [diff] [blame] | 225 | KMP_AFFINITY_DISABLE(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 226 | KMP_INTERNAL_FREE(buf); |
| 227 | return; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 228 | } |
| 229 | continue; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 230 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 231 | |
| 232 | sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL); |
| 233 | KA_TRACE(30, ("__kmp_affinity_determine_capable: " |
| 234 | "setaffinity for mask size %d returned %d errno = %d\n", |
| 235 | gCode, sCode, errno)); |
| 236 | if (sCode < 0) { |
| 237 | if (errno == ENOSYS) { // Linux* OS only |
| 238 | // We shouldn't get here |
| 239 | KA_TRACE(30, ("__kmp_affinity_determine_capable: " |
| 240 | "inconsistent OS call behavior: errno == ENOSYS for mask " |
| 241 | "size %d\n", |
| 242 | size)); |
| 243 | if (__kmp_affinity_verbose || |
| 244 | (__kmp_affinity_warnings && |
| 245 | (__kmp_affinity_type != affinity_none) && |
| 246 | (__kmp_affinity_type != affinity_default) && |
| 247 | (__kmp_affinity_type != affinity_disabled))) { |
| 248 | int error = errno; |
| 249 | kmp_msg_t err_code = KMP_ERR(error); |
| 250 | __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var), |
| 251 | err_code, __kmp_msg_null); |
| 252 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 253 | __kmp_str_free(&err_code.str); |
| 254 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 255 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 256 | KMP_AFFINITY_DISABLE(); |
| 257 | KMP_INTERNAL_FREE(buf); |
| 258 | return; |
| 259 | } |
| 260 | if (errno == EFAULT) { |
| 261 | KMP_AFFINITY_ENABLE(gCode); |
| 262 | KA_TRACE(10, ("__kmp_affinity_determine_capable: " |
| 263 | "affinity supported (mask size %d)\n", |
| 264 | (int)__kmp_affin_mask_size)); |
| 265 | KMP_INTERNAL_FREE(buf); |
| 266 | return; |
| 267 | } |
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 | } |
| 270 | // save uncaught error code |
| 271 | // int error = errno; |
| 272 | KMP_INTERNAL_FREE(buf); |
| 273 | // restore uncaught error code, will be printed at the next KMP_WARNING below |
| 274 | // errno = error; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 275 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 276 | // Affinity is not supported |
| 277 | KMP_AFFINITY_DISABLE(); |
| 278 | KA_TRACE(10, ("__kmp_affinity_determine_capable: " |
| 279 | "cannot determine mask size - affinity not supported\n")); |
| 280 | if (__kmp_affinity_verbose || |
| 281 | (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) && |
| 282 | (__kmp_affinity_type != affinity_default) && |
| 283 | (__kmp_affinity_type != affinity_disabled))) { |
| 284 | KMP_WARNING(AffCantGetMaskSize, env_var); |
| 285 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 288 | #endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 289 | |
Jonathan Peyton | 9d2412c | 2016-06-22 16:35:12 +0000 | [diff] [blame] | 290 | #if KMP_USE_FUTEX |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 291 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 292 | int __kmp_futex_determine_capable() { |
| 293 | int loc = 0; |
| 294 | int rc = syscall(__NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0); |
| 295 | int retval = (rc == 0) || (errno != ENOSYS); |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 296 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 297 | KA_TRACE(10, |
| 298 | ("__kmp_futex_determine_capable: rc = %d errno = %d\n", rc, errno)); |
| 299 | KA_TRACE(10, ("__kmp_futex_determine_capable: futex syscall%s supported\n", |
| 300 | retval ? "" : " not")); |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 301 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 302 | return retval; |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Jonathan Peyton | 9d2412c | 2016-06-22 16:35:12 +0000 | [diff] [blame] | 305 | #endif // KMP_USE_FUTEX |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 306 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 307 | #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (!KMP_ASM_INTRINS) |
| 308 | /* Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to |
| 309 | use compare_and_store for these routines */ |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 310 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 311 | kmp_int8 __kmp_test_then_or8(volatile kmp_int8 *p, kmp_int8 d) { |
| 312 | kmp_int8 old_value, new_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 313 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 314 | old_value = TCR_1(*p); |
| 315 | new_value = old_value | d; |
Andrey Churbanov | 7b2ab71 | 2015-03-10 09:03:42 +0000 | [diff] [blame] | 316 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 317 | while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) { |
| 318 | KMP_CPU_PAUSE(); |
| 319 | old_value = TCR_1(*p); |
Andrey Churbanov | 7b2ab71 | 2015-03-10 09:03:42 +0000 | [diff] [blame] | 320 | new_value = old_value | d; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 321 | } |
| 322 | return old_value; |
Andrey Churbanov | 7b2ab71 | 2015-03-10 09:03:42 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 325 | kmp_int8 __kmp_test_then_and8(volatile kmp_int8 *p, kmp_int8 d) { |
| 326 | kmp_int8 old_value, new_value; |
Andrey Churbanov | 7b2ab71 | 2015-03-10 09:03:42 +0000 | [diff] [blame] | 327 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 328 | old_value = TCR_1(*p); |
| 329 | new_value = old_value & d; |
| 330 | |
| 331 | while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) { |
| 332 | KMP_CPU_PAUSE(); |
| 333 | old_value = TCR_1(*p); |
Andrey Churbanov | 7b2ab71 | 2015-03-10 09:03:42 +0000 | [diff] [blame] | 334 | new_value = old_value & d; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 335 | } |
| 336 | return old_value; |
Andrey Churbanov | 7b2ab71 | 2015-03-10 09:03:42 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Andrey Churbanov | 5ba90c7 | 2017-07-17 09:03:14 +0000 | [diff] [blame] | 339 | kmp_uint32 __kmp_test_then_or32(volatile kmp_uint32 *p, kmp_uint32 d) { |
| 340 | kmp_uint32 old_value, new_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 341 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 342 | old_value = TCR_4(*p); |
| 343 | new_value = old_value | d; |
| 344 | |
| 345 | while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) { |
| 346 | KMP_CPU_PAUSE(); |
| 347 | old_value = TCR_4(*p); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 348 | new_value = old_value | d; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 349 | } |
| 350 | return old_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Andrey Churbanov | 5ba90c7 | 2017-07-17 09:03:14 +0000 | [diff] [blame] | 353 | kmp_uint32 __kmp_test_then_and32(volatile kmp_uint32 *p, kmp_uint32 d) { |
| 354 | kmp_uint32 old_value, new_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 355 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 356 | old_value = TCR_4(*p); |
| 357 | new_value = old_value & d; |
| 358 | |
| 359 | while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) { |
| 360 | KMP_CPU_PAUSE(); |
| 361 | old_value = TCR_4(*p); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 362 | new_value = old_value & d; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 363 | } |
| 364 | return old_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 367 | #if KMP_ARCH_X86 |
| 368 | kmp_int8 __kmp_test_then_add8(volatile kmp_int8 *p, kmp_int8 d) { |
| 369 | kmp_int8 old_value, new_value; |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 370 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 371 | old_value = TCR_1(*p); |
| 372 | new_value = old_value + d; |
| 373 | |
| 374 | while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) { |
| 375 | KMP_CPU_PAUSE(); |
| 376 | old_value = TCR_1(*p); |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 377 | new_value = old_value + d; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 378 | } |
| 379 | return old_value; |
Andrey Churbanov | d39f11c | 2015-03-10 10:14:57 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 382 | kmp_int64 __kmp_test_then_add64(volatile kmp_int64 *p, kmp_int64 d) { |
| 383 | kmp_int64 old_value, new_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 384 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 385 | old_value = TCR_8(*p); |
| 386 | new_value = old_value + d; |
| 387 | |
| 388 | while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) { |
| 389 | KMP_CPU_PAUSE(); |
| 390 | old_value = TCR_8(*p); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 391 | new_value = old_value + d; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 392 | } |
| 393 | return old_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 394 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 395 | #endif /* KMP_ARCH_X86 */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 396 | |
Andrey Churbanov | 5ba90c7 | 2017-07-17 09:03:14 +0000 | [diff] [blame] | 397 | kmp_uint64 __kmp_test_then_or64(volatile kmp_uint64 *p, kmp_uint64 d) { |
| 398 | kmp_uint64 old_value, new_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 399 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 400 | old_value = TCR_8(*p); |
| 401 | new_value = old_value | d; |
| 402 | while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) { |
| 403 | KMP_CPU_PAUSE(); |
| 404 | old_value = TCR_8(*p); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 405 | new_value = old_value | d; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 406 | } |
| 407 | return old_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Andrey Churbanov | 5ba90c7 | 2017-07-17 09:03:14 +0000 | [diff] [blame] | 410 | kmp_uint64 __kmp_test_then_and64(volatile kmp_uint64 *p, kmp_uint64 d) { |
| 411 | kmp_uint64 old_value, new_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 412 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 413 | old_value = TCR_8(*p); |
| 414 | new_value = old_value & d; |
| 415 | while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) { |
| 416 | KMP_CPU_PAUSE(); |
| 417 | old_value = TCR_8(*p); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 418 | new_value = old_value & d; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 419 | } |
| 420 | return old_value; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | #endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */ |
| 424 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 425 | void __kmp_terminate_thread(int gtid) { |
| 426 | int status; |
| 427 | kmp_info_t *th = __kmp_threads[gtid]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 428 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 429 | if (!th) |
| 430 | return; |
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 | #ifdef KMP_CANCEL_THREADS |
| 433 | KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid)); |
| 434 | status = pthread_cancel(th->th.th_info.ds.ds_thread); |
| 435 | if (status != 0 && status != ESRCH) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 436 | __kmp_fatal(KMP_MSG(CantTerminateWorkerThread), KMP_ERR(status), |
| 437 | __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 438 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 439 | #endif |
Jonathan Peyton | e47d32f | 2019-02-28 19:11:29 +0000 | [diff] [blame] | 440 | KMP_YIELD(TRUE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 441 | } // |
| 442 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 443 | /* Set thread stack info according to values returned by pthread_getattr_np(). |
| 444 | If values are unreasonable, assume call failed and use incremental stack |
| 445 | refinement method instead. Returns TRUE if the stack parameters could be |
| 446 | determined exactly, FALSE if incremental refinement is necessary. */ |
| 447 | static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) { |
| 448 | int stack_data; |
Kamil Rytarowski | a56ac94 | 2018-12-09 16:40:33 +0000 | [diff] [blame] | 449 | #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \ |
| 450 | KMP_OS_HURD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 451 | pthread_attr_t attr; |
| 452 | int status; |
| 453 | size_t size = 0; |
| 454 | void *addr = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 455 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 456 | /* Always do incremental stack refinement for ubermaster threads since the |
| 457 | initial thread stack range can be reduced by sibling thread creation so |
| 458 | pthread_attr_getstack may cause thread gtid aliasing */ |
| 459 | if (!KMP_UBER_GTID(gtid)) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 460 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 461 | /* Fetch the real thread attributes */ |
| 462 | status = pthread_attr_init(&attr); |
| 463 | KMP_CHECK_SYSFAIL("pthread_attr_init", status); |
Kamil Rytarowski | a56ac94 | 2018-12-09 16:40:33 +0000 | [diff] [blame] | 464 | #if KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 465 | status = pthread_attr_get_np(pthread_self(), &attr); |
| 466 | KMP_CHECK_SYSFAIL("pthread_attr_get_np", status); |
Alp Toker | 763b939 | 2014-02-28 09:42:41 +0000 | [diff] [blame] | 467 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 468 | status = pthread_getattr_np(pthread_self(), &attr); |
| 469 | KMP_CHECK_SYSFAIL("pthread_getattr_np", status); |
Alp Toker | 763b939 | 2014-02-28 09:42:41 +0000 | [diff] [blame] | 470 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 471 | status = pthread_attr_getstack(&attr, &addr, &size); |
| 472 | KMP_CHECK_SYSFAIL("pthread_attr_getstack", status); |
| 473 | KA_TRACE(60, |
| 474 | ("__kmp_set_stack_info: T#%d pthread_attr_getstack returned size:" |
| 475 | " %lu, low addr: %p\n", |
| 476 | gtid, size, addr)); |
| 477 | status = pthread_attr_destroy(&attr); |
| 478 | KMP_CHECK_SYSFAIL("pthread_attr_destroy", status); |
| 479 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 480 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 481 | if (size != 0 && addr != 0) { // was stack parameter determination successful? |
| 482 | /* Store the correct base and size */ |
| 483 | TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size)); |
| 484 | TCW_PTR(th->th.th_info.ds.ds_stacksize, size); |
| 485 | TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE); |
| 486 | return TRUE; |
| 487 | } |
Kamil Rytarowski | a56ac94 | 2018-12-09 16:40:33 +0000 | [diff] [blame] | 488 | #endif /* KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || |
| 489 | KMP_OS_HURD */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 490 | /* Use incremental refinement starting from initial conservative estimate */ |
| 491 | TCW_PTR(th->th.th_info.ds.ds_stacksize, 0); |
| 492 | TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data); |
| 493 | TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE); |
| 494 | return FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 497 | static void *__kmp_launch_worker(void *thr) { |
| 498 | int status, old_type, old_state; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 499 | #ifdef KMP_BLOCK_SIGNALS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 500 | sigset_t new_set, old_set; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 501 | #endif /* KMP_BLOCK_SIGNALS */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 502 | void *exit_val; |
Kamil Rytarowski | a56ac94 | 2018-12-09 16:40:33 +0000 | [diff] [blame] | 503 | #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \ |
Kamil Rytarowski | 7e1ea99 | 2018-12-09 16:46:48 +0000 | [diff] [blame] | 504 | KMP_OS_OPENBSD || KMP_OS_HURD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 505 | void *volatile padding = 0; |
Jonathan Peyton | 2321d57 | 2015-06-08 19:25:25 +0000 | [diff] [blame] | 506 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 507 | int gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 508 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 509 | gtid = ((kmp_info_t *)thr)->th.th_info.ds.ds_gtid; |
| 510 | __kmp_gtid_set_specific(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 511 | #ifdef KMP_TDATA_GTID |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 512 | __kmp_gtid = gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 513 | #endif |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 514 | #if KMP_STATS_ENABLED |
Jonathan Peyton | 40039ac | 2017-11-07 23:32:13 +0000 | [diff] [blame] | 515 | // set thread local index to point to thread-specific stats |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 516 | __kmp_stats_thread_ptr = ((kmp_info_t *)thr)->th.th_stats; |
Jonathan Peyton | f0682ac | 2018-07-30 17:41:08 +0000 | [diff] [blame] | 517 | __kmp_stats_thread_ptr->startLife(); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 518 | KMP_SET_THREAD_STATE(IDLE); |
| 519 | KMP_INIT_PARTITIONED_TIMERS(OMP_idle); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 520 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 521 | |
| 522 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 523 | __kmp_itt_thread_name(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 524 | #endif /* USE_ITT_BUILD */ |
| 525 | |
Alp Toker | 763b939 | 2014-02-28 09:42:41 +0000 | [diff] [blame] | 526 | #if KMP_AFFINITY_SUPPORTED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 527 | __kmp_affinity_set_init_mask(gtid, FALSE); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 528 | #endif |
| 529 | |
| 530 | #ifdef KMP_CANCEL_THREADS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 531 | status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type); |
| 532 | KMP_CHECK_SYSFAIL("pthread_setcanceltype", status); |
| 533 | // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? |
| 534 | status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state); |
| 535 | KMP_CHECK_SYSFAIL("pthread_setcancelstate", status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 536 | #endif |
| 537 | |
| 538 | #if KMP_ARCH_X86 || KMP_ARCH_X86_64 |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 539 | // Set FP control regs to be a copy of the parallel initialization thread's. |
| 540 | __kmp_clear_x87_fpu_status_word(); |
| 541 | __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word); |
| 542 | __kmp_load_mxcsr(&__kmp_init_mxcsr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 543 | #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ |
| 544 | |
| 545 | #ifdef KMP_BLOCK_SIGNALS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 546 | status = sigfillset(&new_set); |
| 547 | KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status); |
| 548 | status = pthread_sigmask(SIG_BLOCK, &new_set, &old_set); |
| 549 | KMP_CHECK_SYSFAIL("pthread_sigmask", status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 550 | #endif /* KMP_BLOCK_SIGNALS */ |
| 551 | |
Kamil Rytarowski | 7e1ea99 | 2018-12-09 16:46:48 +0000 | [diff] [blame] | 552 | #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \ |
| 553 | KMP_OS_OPENBSD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 554 | if (__kmp_stkoffset > 0 && gtid > 0) { |
| 555 | padding = KMP_ALLOCA(gtid * __kmp_stkoffset); |
| 556 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 557 | #endif |
| 558 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 559 | KMP_MB(); |
| 560 | __kmp_set_stack_info(gtid, (kmp_info_t *)thr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 561 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 562 | __kmp_check_stack_overlap((kmp_info_t *)thr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 563 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 564 | exit_val = __kmp_launch_thread((kmp_info_t *)thr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 565 | |
| 566 | #ifdef KMP_BLOCK_SIGNALS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 567 | status = pthread_sigmask(SIG_SETMASK, &old_set, NULL); |
| 568 | KMP_CHECK_SYSFAIL("pthread_sigmask", status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 569 | #endif /* KMP_BLOCK_SIGNALS */ |
| 570 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 571 | return exit_val; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 574 | #if KMP_USE_MONITOR |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 575 | /* The monitor thread controls all of the threads in the complex */ |
| 576 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 577 | static void *__kmp_launch_monitor(void *thr) { |
| 578 | int status, old_type, old_state; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 579 | #ifdef KMP_BLOCK_SIGNALS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 580 | sigset_t new_set; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 581 | #endif /* KMP_BLOCK_SIGNALS */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 582 | struct timespec interval; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 583 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 584 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 585 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 586 | KA_TRACE(10, ("__kmp_launch_monitor: #1 launched\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 587 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 588 | /* register us as the monitor thread */ |
| 589 | __kmp_gtid_set_specific(KMP_GTID_MONITOR); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 590 | #ifdef KMP_TDATA_GTID |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 591 | __kmp_gtid = KMP_GTID_MONITOR; |
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 | KMP_MB(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 595 | |
| 596 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 597 | // Instruct Intel(R) Threading Tools to ignore monitor thread. |
| 598 | __kmp_itt_thread_ignore(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 599 | #endif /* USE_ITT_BUILD */ |
| 600 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 601 | __kmp_set_stack_info(((kmp_info_t *)thr)->th.th_info.ds.ds_gtid, |
| 602 | (kmp_info_t *)thr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 603 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 604 | __kmp_check_stack_overlap((kmp_info_t *)thr); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 605 | |
| 606 | #ifdef KMP_CANCEL_THREADS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 607 | status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type); |
| 608 | KMP_CHECK_SYSFAIL("pthread_setcanceltype", status); |
| 609 | // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? |
| 610 | status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state); |
| 611 | KMP_CHECK_SYSFAIL("pthread_setcancelstate", status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 612 | #endif |
| 613 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 614 | #if KMP_REAL_TIME_FIX |
| 615 | // This is a potential fix which allows application with real-time scheduling |
| 616 | // policy work. However, decision about the fix is not made yet, so it is |
| 617 | // disabled by default. |
| 618 | { // Are program started with real-time scheduling policy? |
| 619 | int sched = sched_getscheduler(0); |
| 620 | if (sched == SCHED_FIFO || sched == SCHED_RR) { |
| 621 | // Yes, we are a part of real-time application. Try to increase the |
| 622 | // priority of the monitor. |
| 623 | struct sched_param param; |
| 624 | int max_priority = sched_get_priority_max(sched); |
| 625 | int rc; |
| 626 | KMP_WARNING(RealTimeSchedNotSupported); |
| 627 | sched_getparam(0, ¶m); |
| 628 | if (param.sched_priority < max_priority) { |
| 629 | param.sched_priority += 1; |
| 630 | rc = sched_setscheduler(0, sched, ¶m); |
| 631 | if (rc != 0) { |
| 632 | int error = errno; |
| 633 | kmp_msg_t err_code = KMP_ERR(error); |
| 634 | __kmp_msg(kmp_ms_warning, KMP_MSG(CantChangeMonitorPriority), |
| 635 | err_code, KMP_MSG(MonitorWillStarve), __kmp_msg_null); |
| 636 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 637 | __kmp_str_free(&err_code.str); |
| 638 | } |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 639 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 640 | } else { |
| 641 | // We cannot abort here, because number of CPUs may be enough for all |
| 642 | // the threads, including the monitor thread, so application could |
| 643 | // potentially work... |
| 644 | __kmp_msg(kmp_ms_warning, KMP_MSG(RunningAtMaxPriority), |
| 645 | KMP_MSG(MonitorWillStarve), KMP_HNT(RunningAtMaxPriority), |
| 646 | __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 649 | // AC: free thread that waits for monitor started |
| 650 | TCW_4(__kmp_global.g.g_time.dt.t_value, 0); |
| 651 | } |
| 652 | #endif // KMP_REAL_TIME_FIX |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 653 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 654 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 655 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 656 | if (__kmp_monitor_wakeups == 1) { |
| 657 | interval.tv_sec = 1; |
| 658 | interval.tv_nsec = 0; |
| 659 | } else { |
| 660 | interval.tv_sec = 0; |
| 661 | interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups); |
| 662 | } |
| 663 | |
| 664 | KA_TRACE(10, ("__kmp_launch_monitor: #2 monitor\n")); |
| 665 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 666 | while (!TCR_4(__kmp_global.g.g_done)) { |
| 667 | struct timespec now; |
| 668 | struct timeval tval; |
| 669 | |
| 670 | /* This thread monitors the state of the system */ |
| 671 | |
| 672 | KA_TRACE(15, ("__kmp_launch_monitor: update\n")); |
| 673 | |
| 674 | status = gettimeofday(&tval, NULL); |
| 675 | KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); |
| 676 | TIMEVAL_TO_TIMESPEC(&tval, &now); |
| 677 | |
| 678 | now.tv_sec += interval.tv_sec; |
| 679 | now.tv_nsec += interval.tv_nsec; |
| 680 | |
| 681 | if (now.tv_nsec >= KMP_NSEC_PER_SEC) { |
| 682 | now.tv_sec += 1; |
| 683 | now.tv_nsec -= KMP_NSEC_PER_SEC; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 686 | status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex); |
| 687 | KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); |
| 688 | // AC: the monitor should not fall asleep if g_done has been set |
| 689 | if (!TCR_4(__kmp_global.g.g_done)) { // check once more under mutex |
| 690 | status = pthread_cond_timedwait(&__kmp_wait_cv.c_cond, |
| 691 | &__kmp_wait_mx.m_mutex, &now); |
| 692 | if (status != 0) { |
| 693 | if (status != ETIMEDOUT && status != EINTR) { |
| 694 | KMP_SYSFAIL("pthread_cond_timedwait", status); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 698 | status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex); |
| 699 | KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 700 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 701 | TCW_4(__kmp_global.g.g_time.dt.t_value, |
| 702 | TCR_4(__kmp_global.g.g_time.dt.t_value) + 1); |
| 703 | |
| 704 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 705 | } |
| 706 | |
| 707 | KA_TRACE(10, ("__kmp_launch_monitor: #3 cleanup\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 708 | |
| 709 | #ifdef KMP_BLOCK_SIGNALS |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 710 | status = sigfillset(&new_set); |
| 711 | KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status); |
| 712 | status = pthread_sigmask(SIG_UNBLOCK, &new_set, NULL); |
| 713 | KMP_CHECK_SYSFAIL("pthread_sigmask", status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 714 | #endif /* KMP_BLOCK_SIGNALS */ |
| 715 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 716 | KA_TRACE(10, ("__kmp_launch_monitor: #4 finished\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 717 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 718 | if (__kmp_global.g.g_abort != 0) { |
| 719 | /* now we need to terminate the worker threads */ |
| 720 | /* the value of t_abort is the signal we caught */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 721 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 722 | int gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 723 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 724 | KA_TRACE(10, ("__kmp_launch_monitor: #5 terminate sig=%d\n", |
| 725 | __kmp_global.g.g_abort)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 726 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 727 | /* terminate the OpenMP worker threads */ |
| 728 | /* TODO this is not valid for sibling threads!! |
| 729 | * the uber master might not be 0 anymore.. */ |
| 730 | for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid) |
| 731 | __kmp_terminate_thread(gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 732 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 733 | __kmp_cleanup(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 734 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 735 | KA_TRACE(10, ("__kmp_launch_monitor: #6 raise sig=%d\n", |
| 736 | __kmp_global.g.g_abort)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 737 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 738 | if (__kmp_global.g.g_abort > 0) |
| 739 | raise(__kmp_global.g.g_abort); |
| 740 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 741 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 742 | KA_TRACE(10, ("__kmp_launch_monitor: #7 exit\n")); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 743 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 744 | return thr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 745 | } |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 746 | #endif // KMP_USE_MONITOR |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 747 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 748 | void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) { |
| 749 | pthread_t handle; |
| 750 | pthread_attr_t thread_attr; |
| 751 | int status; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 752 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 753 | th->th.th_info.ds.ds_gtid = gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 754 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 755 | #if KMP_STATS_ENABLED |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 756 | // sets up worker thread stats |
| 757 | __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 758 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 759 | // th->th.th_stats is used to transfer thread-specific stats-pointer to |
| 760 | // __kmp_launch_worker. So when thread is created (goes into |
Jonathan Peyton | 40039ac | 2017-11-07 23:32:13 +0000 | [diff] [blame] | 761 | // __kmp_launch_worker) it will set its thread local pointer to |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 762 | // th->th.th_stats |
| 763 | if (!KMP_UBER_GTID(gtid)) { |
| 764 | th->th.th_stats = __kmp_stats_list->push_back(gtid); |
| 765 | } else { |
| 766 | // For root threads, __kmp_stats_thread_ptr is set in __kmp_register_root(), |
| 767 | // so set the th->th.th_stats field to it. |
| 768 | th->th.th_stats = __kmp_stats_thread_ptr; |
| 769 | } |
| 770 | __kmp_release_tas_lock(&__kmp_stats_lock, gtid); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 771 | |
| 772 | #endif // KMP_STATS_ENABLED |
| 773 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 774 | if (KMP_UBER_GTID(gtid)) { |
| 775 | KA_TRACE(10, ("__kmp_create_worker: uber thread (%d)\n", gtid)); |
| 776 | th->th.th_info.ds.ds_thread = pthread_self(); |
| 777 | __kmp_set_stack_info(gtid, th); |
| 778 | __kmp_check_stack_overlap(th); |
| 779 | return; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 780 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 781 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 782 | KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 783 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 784 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 785 | |
| 786 | #ifdef KMP_THREAD_ATTR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 787 | status = pthread_attr_init(&thread_attr); |
| 788 | if (status != 0) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 789 | __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 790 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 791 | status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); |
| 792 | if (status != 0) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 793 | __kmp_fatal(KMP_MSG(CantSetWorkerState), KMP_ERR(status), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 794 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 795 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 796 | /* Set stack size for this thread now. |
| 797 | The multiple of 2 is there because on some machines, requesting an unusual |
| 798 | stacksize causes the thread to have an offset before the dummy alloca() |
| 799 | takes place to create the offset. Since we want the user to have a |
| 800 | sufficient stacksize AND support a stack offset, we alloca() twice the |
| 801 | offset so that the upcoming alloca() does not eliminate any premade offset, |
| 802 | and also gives the user the stack space they requested for all threads */ |
| 803 | stack_size += gtid * __kmp_stkoffset * 2; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 804 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 805 | KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, " |
| 806 | "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n", |
| 807 | gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 808 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 809 | #ifdef _POSIX_THREAD_ATTR_STACKSIZE |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 810 | status = pthread_attr_setstacksize(&thread_attr, stack_size); |
| 811 | #ifdef KMP_BACKUP_STKSIZE |
| 812 | if (status != 0) { |
| 813 | if (!__kmp_env_stksize) { |
| 814 | stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset; |
| 815 | __kmp_stksize = KMP_BACKUP_STKSIZE; |
| 816 | KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, " |
| 817 | "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu " |
| 818 | "bytes\n", |
| 819 | gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size)); |
| 820 | status = pthread_attr_setstacksize(&thread_attr, stack_size); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 823 | #endif /* KMP_BACKUP_STKSIZE */ |
| 824 | if (status != 0) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 825 | __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status), |
| 826 | KMP_HNT(ChangeWorkerStackSize), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 827 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 828 | #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 829 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 830 | #endif /* KMP_THREAD_ATTR */ |
| 831 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 832 | status = |
| 833 | pthread_create(&handle, &thread_attr, __kmp_launch_worker, (void *)th); |
| 834 | if (status != 0 || !handle) { // ??? Why do we check handle?? |
| 835 | #ifdef _POSIX_THREAD_ATTR_STACKSIZE |
| 836 | if (status == EINVAL) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 837 | __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status), |
| 838 | KMP_HNT(IncreaseWorkerStackSize), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 839 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 840 | if (status == ENOMEM) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 841 | __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status), |
| 842 | KMP_HNT(DecreaseWorkerStackSize), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 843 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 844 | #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ |
| 845 | if (status == EAGAIN) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 846 | __kmp_fatal(KMP_MSG(NoResourcesForWorkerThread), KMP_ERR(status), |
| 847 | KMP_HNT(Decrease_NUM_THREADS), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 848 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 849 | KMP_SYSFAIL("pthread_create", status); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 850 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 851 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 852 | th->th.th_info.ds.ds_thread = handle; |
| 853 | |
| 854 | #ifdef KMP_THREAD_ATTR |
| 855 | status = pthread_attr_destroy(&thread_attr); |
| 856 | if (status) { |
| 857 | kmp_msg_t err_code = KMP_ERR(status); |
| 858 | __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code, |
| 859 | __kmp_msg_null); |
| 860 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 861 | __kmp_str_free(&err_code.str); |
| 862 | } |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 863 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 864 | #endif /* KMP_THREAD_ATTR */ |
| 865 | |
| 866 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 867 | |
| 868 | KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 869 | |
| 870 | } // __kmp_create_worker |
| 871 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 872 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 873 | void __kmp_create_monitor(kmp_info_t *th) { |
| 874 | pthread_t handle; |
| 875 | pthread_attr_t thread_attr; |
| 876 | size_t size; |
| 877 | int status; |
| 878 | int auto_adj_size = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 879 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 880 | if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) { |
| 881 | // We don't need monitor thread in case of MAX_BLOCKTIME |
| 882 | KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of " |
| 883 | "MAX blocktime\n")); |
| 884 | th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op |
| 885 | th->th.th_info.ds.ds_gtid = 0; |
| 886 | return; |
| 887 | } |
| 888 | KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n")); |
| 889 | |
| 890 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
| 891 | |
| 892 | th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR; |
| 893 | th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR; |
| 894 | #if KMP_REAL_TIME_FIX |
| 895 | TCW_4(__kmp_global.g.g_time.dt.t_value, |
| 896 | -1); // Will use it for synchronization a bit later. |
| 897 | #else |
| 898 | TCW_4(__kmp_global.g.g_time.dt.t_value, 0); |
| 899 | #endif // KMP_REAL_TIME_FIX |
| 900 | |
| 901 | #ifdef KMP_THREAD_ATTR |
| 902 | if (__kmp_monitor_stksize == 0) { |
| 903 | __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE; |
| 904 | auto_adj_size = TRUE; |
| 905 | } |
| 906 | status = pthread_attr_init(&thread_attr); |
| 907 | if (status != 0) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 908 | __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 909 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 910 | status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); |
| 911 | if (status != 0) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 912 | __kmp_fatal(KMP_MSG(CantSetMonitorState), KMP_ERR(status), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 913 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 914 | |
| 915 | #ifdef _POSIX_THREAD_ATTR_STACKSIZE |
| 916 | status = pthread_attr_getstacksize(&thread_attr, &size); |
| 917 | KMP_CHECK_SYSFAIL("pthread_attr_getstacksize", status); |
| 918 | #else |
| 919 | size = __kmp_sys_min_stksize; |
| 920 | #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ |
| 921 | #endif /* KMP_THREAD_ATTR */ |
| 922 | |
| 923 | if (__kmp_monitor_stksize == 0) { |
| 924 | __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE; |
| 925 | } |
| 926 | if (__kmp_monitor_stksize < __kmp_sys_min_stksize) { |
| 927 | __kmp_monitor_stksize = __kmp_sys_min_stksize; |
| 928 | } |
| 929 | |
| 930 | KA_TRACE(10, ("__kmp_create_monitor: default stacksize = %lu bytes," |
| 931 | "requested stacksize = %lu bytes\n", |
| 932 | size, __kmp_monitor_stksize)); |
| 933 | |
| 934 | retry: |
| 935 | |
| 936 | /* Set stack size for this thread now. */ |
| 937 | #ifdef _POSIX_THREAD_ATTR_STACKSIZE |
| 938 | KA_TRACE(10, ("__kmp_create_monitor: setting stacksize = %lu bytes,", |
| 939 | __kmp_monitor_stksize)); |
| 940 | status = pthread_attr_setstacksize(&thread_attr, __kmp_monitor_stksize); |
| 941 | if (status != 0) { |
| 942 | if (auto_adj_size) { |
| 943 | __kmp_monitor_stksize *= 2; |
| 944 | goto retry; |
Jonathan Peyton | 4fee5f6 | 2015-12-18 23:20:36 +0000 | [diff] [blame] | 945 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 946 | kmp_msg_t err_code = KMP_ERR(status); |
| 947 | __kmp_msg(kmp_ms_warning, // should this be fatal? BB |
| 948 | KMP_MSG(CantSetMonitorStackSize, (long int)__kmp_monitor_stksize), |
| 949 | err_code, KMP_HNT(ChangeMonitorStackSize), __kmp_msg_null); |
| 950 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 951 | __kmp_str_free(&err_code.str); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 952 | } |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 953 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 954 | #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 955 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 956 | status = |
| 957 | pthread_create(&handle, &thread_attr, __kmp_launch_monitor, (void *)th); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 958 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 959 | if (status != 0) { |
| 960 | #ifdef _POSIX_THREAD_ATTR_STACKSIZE |
| 961 | if (status == EINVAL) { |
| 962 | if (auto_adj_size && (__kmp_monitor_stksize < (size_t)0x40000000)) { |
| 963 | __kmp_monitor_stksize *= 2; |
| 964 | goto retry; |
| 965 | } |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 966 | __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize), |
| 967 | KMP_ERR(status), KMP_HNT(IncreaseMonitorStackSize), |
| 968 | __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 969 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 970 | if (status == ENOMEM) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 971 | __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize), |
| 972 | KMP_ERR(status), KMP_HNT(DecreaseMonitorStackSize), |
| 973 | __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 974 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 975 | #endif /* _POSIX_THREAD_ATTR_STACKSIZE */ |
| 976 | if (status == EAGAIN) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 977 | __kmp_fatal(KMP_MSG(NoResourcesForMonitorThread), KMP_ERR(status), |
| 978 | KMP_HNT(DecreaseNumberOfThreadsInUse), __kmp_msg_null); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 979 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 980 | KMP_SYSFAIL("pthread_create", status); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 981 | } |
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 | th->th.th_info.ds.ds_thread = handle; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 984 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 985 | #if KMP_REAL_TIME_FIX |
| 986 | // Wait for the monitor thread is really started and set its *priority*. |
| 987 | KMP_DEBUG_ASSERT(sizeof(kmp_uint32) == |
| 988 | sizeof(__kmp_global.g.g_time.dt.t_value)); |
Jonathan Peyton | e47d32f | 2019-02-28 19:11:29 +0000 | [diff] [blame] | 989 | __kmp_wait_4((kmp_uint32 volatile *)&__kmp_global.g.g_time.dt.t_value, -1, |
| 990 | &__kmp_neq_4, NULL); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 991 | #endif // KMP_REAL_TIME_FIX |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 992 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 993 | #ifdef KMP_THREAD_ATTR |
| 994 | status = pthread_attr_destroy(&thread_attr); |
| 995 | if (status != 0) { |
| 996 | kmp_msg_t err_code = KMP_ERR(status); |
| 997 | __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code, |
| 998 | __kmp_msg_null); |
| 999 | if (__kmp_generate_warnings == kmp_warnings_off) { |
| 1000 | __kmp_str_free(&err_code.str); |
| 1001 | } |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1002 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1003 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1004 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1005 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
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 | KA_TRACE(10, ("__kmp_create_monitor: monitor created %#.8lx\n", |
| 1008 | th->th.th_info.ds.ds_thread)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1009 | |
| 1010 | } // __kmp_create_monitor |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1011 | #endif // KMP_USE_MONITOR |
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 | void __kmp_exit_thread(int exit_status) { |
| 1014 | pthread_exit((void *)(intptr_t)exit_status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1015 | } // __kmp_exit_thread |
| 1016 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1017 | #if KMP_USE_MONITOR |
Jim Cownie | 07ea89f | 2014-09-03 11:10:54 +0000 | [diff] [blame] | 1018 | void __kmp_resume_monitor(); |
| 1019 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1020 | void __kmp_reap_monitor(kmp_info_t *th) { |
| 1021 | int status; |
| 1022 | void *exit_val; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1023 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1024 | KA_TRACE(10, ("__kmp_reap_monitor: try to reap monitor thread with handle" |
| 1025 | " %#.8lx\n", |
| 1026 | th->th.th_info.ds.ds_thread)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1027 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1028 | // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR. |
| 1029 | // If both tid and gtid are 0, it means the monitor did not ever start. |
| 1030 | // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down. |
| 1031 | KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid); |
| 1032 | if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) { |
| 1033 | KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n")); |
| 1034 | return; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1035 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1036 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1037 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1038 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1039 | /* First, check to see whether the monitor thread exists to wake it up. This |
| 1040 | is to avoid performance problem when the monitor sleeps during |
| 1041 | blocktime-size interval */ |
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 | status = pthread_kill(th->th.th_info.ds.ds_thread, 0); |
| 1044 | if (status != ESRCH) { |
| 1045 | __kmp_resume_monitor(); // Wake up the monitor thread |
| 1046 | } |
| 1047 | KA_TRACE(10, ("__kmp_reap_monitor: try to join with monitor\n")); |
| 1048 | status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val); |
| 1049 | if (exit_val != th) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1050 | __kmp_fatal(KMP_MSG(ReapMonitorError), KMP_ERR(status), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1051 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1052 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1053 | th->th.th_info.ds.ds_tid = KMP_GTID_DNE; |
| 1054 | th->th.th_info.ds.ds_gtid = KMP_GTID_DNE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1055 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1056 | KA_TRACE(10, ("__kmp_reap_monitor: done reaping monitor thread with handle" |
| 1057 | " %#.8lx\n", |
| 1058 | th->th.th_info.ds.ds_thread)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1059 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1060 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1061 | } |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1062 | #endif // KMP_USE_MONITOR |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1063 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1064 | void __kmp_reap_worker(kmp_info_t *th) { |
| 1065 | int status; |
| 1066 | void *exit_val; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1067 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1068 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1069 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1070 | KA_TRACE( |
| 1071 | 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1072 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1073 | status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1074 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1075 | /* Don't expose these to the user until we understand when they trigger */ |
| 1076 | if (status != 0) { |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 1077 | __kmp_fatal(KMP_MSG(ReapWorkerError), KMP_ERR(status), __kmp_msg_null); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1078 | } |
| 1079 | if (exit_val != th) { |
| 1080 | KA_TRACE(10, ("__kmp_reap_worker: worker T#%d did not reap properly, " |
| 1081 | "exit_val = %p\n", |
| 1082 | th->th.th_info.ds.ds_gtid, exit_val)); |
| 1083 | } |
Jonathan Peyton | 93495de | 2016-06-13 17:36:40 +0000 | [diff] [blame] | 1084 | #endif /* KMP_DEBUG */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1085 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1086 | KA_TRACE(10, ("__kmp_reap_worker: done reaping T#%d\n", |
| 1087 | th->th.th_info.ds.ds_gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1088 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1089 | KMP_MB(); /* Flush all pending memory write invalidates. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1092 | #if KMP_HANDLE_SIGNALS |
| 1093 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1094 | static void __kmp_null_handler(int signo) { |
| 1095 | // Do nothing, for doing SIG_IGN-type actions. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1096 | } // __kmp_null_handler |
| 1097 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1098 | static void __kmp_team_handler(int signo) { |
| 1099 | if (__kmp_global.g.g_abort == 0) { |
| 1100 | /* Stage 1 signal handler, let's shut down all of the threads */ |
| 1101 | #ifdef KMP_DEBUG |
| 1102 | __kmp_debug_printf("__kmp_team_handler: caught signal = %d\n", signo); |
| 1103 | #endif |
| 1104 | switch (signo) { |
| 1105 | case SIGHUP: |
| 1106 | case SIGINT: |
| 1107 | case SIGQUIT: |
| 1108 | case SIGILL: |
| 1109 | case SIGABRT: |
| 1110 | case SIGFPE: |
| 1111 | case SIGBUS: |
| 1112 | case SIGSEGV: |
| 1113 | #ifdef SIGSYS |
| 1114 | case SIGSYS: |
| 1115 | #endif |
| 1116 | case SIGTERM: |
| 1117 | if (__kmp_debug_buf) { |
| 1118 | __kmp_dump_debug_buffer(); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1119 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1120 | KMP_MB(); // Flush all pending memory write invalidates. |
| 1121 | TCW_4(__kmp_global.g.g_abort, signo); |
| 1122 | KMP_MB(); // Flush all pending memory write invalidates. |
| 1123 | TCW_4(__kmp_global.g.g_done, TRUE); |
| 1124 | KMP_MB(); // Flush all pending memory write invalidates. |
| 1125 | break; |
| 1126 | default: |
| 1127 | #ifdef KMP_DEBUG |
| 1128 | __kmp_debug_printf("__kmp_team_handler: unknown signal type"); |
| 1129 | #endif |
| 1130 | break; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1131 | } |
| 1132 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1133 | } // __kmp_team_handler |
| 1134 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1135 | static void __kmp_sigaction(int signum, const struct sigaction *act, |
| 1136 | struct sigaction *oldact) { |
| 1137 | int rc = sigaction(signum, act, oldact); |
| 1138 | KMP_CHECK_SYSFAIL_ERRNO("sigaction", rc); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1141 | static void __kmp_install_one_handler(int sig, sig_func_t handler_func, |
| 1142 | int parallel_init) { |
| 1143 | KMP_MB(); // Flush all pending memory write invalidates. |
| 1144 | KB_TRACE(60, |
| 1145 | ("__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init)); |
| 1146 | if (parallel_init) { |
| 1147 | struct sigaction new_action; |
| 1148 | struct sigaction old_action; |
| 1149 | new_action.sa_handler = handler_func; |
| 1150 | new_action.sa_flags = 0; |
| 1151 | sigfillset(&new_action.sa_mask); |
| 1152 | __kmp_sigaction(sig, &new_action, &old_action); |
| 1153 | if (old_action.sa_handler == __kmp_sighldrs[sig].sa_handler) { |
| 1154 | sigaddset(&__kmp_sigset, sig); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1155 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1156 | // Restore/keep user's handler if one previously installed. |
| 1157 | __kmp_sigaction(sig, &old_action, NULL); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1158 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1159 | } else { |
| 1160 | // Save initial/system signal handlers to see if user handlers installed. |
| 1161 | __kmp_sigaction(sig, NULL, &__kmp_sighldrs[sig]); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1162 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1163 | KMP_MB(); // Flush all pending memory write invalidates. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1164 | } // __kmp_install_one_handler |
| 1165 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1166 | static void __kmp_remove_one_handler(int sig) { |
| 1167 | KB_TRACE(60, ("__kmp_remove_one_handler( %d )\n", sig)); |
| 1168 | if (sigismember(&__kmp_sigset, sig)) { |
| 1169 | struct sigaction old; |
| 1170 | KMP_MB(); // Flush all pending memory write invalidates. |
| 1171 | __kmp_sigaction(sig, &__kmp_sighldrs[sig], &old); |
| 1172 | if ((old.sa_handler != __kmp_team_handler) && |
| 1173 | (old.sa_handler != __kmp_null_handler)) { |
| 1174 | // Restore the users signal handler. |
| 1175 | KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, " |
| 1176 | "restoring: sig=%d\n", |
| 1177 | sig)); |
| 1178 | __kmp_sigaction(sig, &old, NULL); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1179 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1180 | sigdelset(&__kmp_sigset, sig); |
| 1181 | KMP_MB(); // Flush all pending memory write invalidates. |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1182 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1183 | } // __kmp_remove_one_handler |
| 1184 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1185 | void __kmp_install_signals(int parallel_init) { |
| 1186 | KB_TRACE(10, ("__kmp_install_signals( %d )\n", parallel_init)); |
| 1187 | if (__kmp_handle_signals || !parallel_init) { |
| 1188 | // If ! parallel_init, we do not install handlers, just save original |
| 1189 | // handlers. Let us do it even __handle_signals is 0. |
| 1190 | sigemptyset(&__kmp_sigset); |
| 1191 | __kmp_install_one_handler(SIGHUP, __kmp_team_handler, parallel_init); |
| 1192 | __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init); |
| 1193 | __kmp_install_one_handler(SIGQUIT, __kmp_team_handler, parallel_init); |
| 1194 | __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init); |
| 1195 | __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init); |
| 1196 | __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init); |
| 1197 | __kmp_install_one_handler(SIGBUS, __kmp_team_handler, parallel_init); |
| 1198 | __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init); |
| 1199 | #ifdef SIGSYS |
| 1200 | __kmp_install_one_handler(SIGSYS, __kmp_team_handler, parallel_init); |
| 1201 | #endif // SIGSYS |
| 1202 | __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init); |
| 1203 | #ifdef SIGPIPE |
| 1204 | __kmp_install_one_handler(SIGPIPE, __kmp_team_handler, parallel_init); |
| 1205 | #endif // SIGPIPE |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1206 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1207 | } // __kmp_install_signals |
| 1208 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1209 | void __kmp_remove_signals(void) { |
| 1210 | int sig; |
| 1211 | KB_TRACE(10, ("__kmp_remove_signals()\n")); |
| 1212 | for (sig = 1; sig < NSIG; ++sig) { |
| 1213 | __kmp_remove_one_handler(sig); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1214 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1215 | } // __kmp_remove_signals |
| 1216 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1217 | #endif // KMP_HANDLE_SIGNALS |
| 1218 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1219 | void __kmp_enable(int new_state) { |
| 1220 | #ifdef KMP_CANCEL_THREADS |
| 1221 | int status, old_state; |
| 1222 | status = pthread_setcancelstate(new_state, &old_state); |
| 1223 | KMP_CHECK_SYSFAIL("pthread_setcancelstate", status); |
| 1224 | KMP_DEBUG_ASSERT(old_state == PTHREAD_CANCEL_DISABLE); |
| 1225 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1228 | void __kmp_disable(int *old_state) { |
| 1229 | #ifdef KMP_CANCEL_THREADS |
| 1230 | int status; |
| 1231 | status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state); |
| 1232 | KMP_CHECK_SYSFAIL("pthread_setcancelstate", status); |
| 1233 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Jonathan Peyton | eaa9e40 | 2018-01-10 18:21:48 +0000 | [diff] [blame] | 1236 | static void __kmp_atfork_prepare(void) { |
| 1237 | __kmp_acquire_bootstrap_lock(&__kmp_initz_lock); |
| 1238 | __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Jonathan Peyton | eaa9e40 | 2018-01-10 18:21:48 +0000 | [diff] [blame] | 1241 | static void __kmp_atfork_parent(void) { |
| 1242 | __kmp_release_bootstrap_lock(&__kmp_initz_lock); |
| 1243 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1246 | /* Reset the library so execution in the child starts "all over again" with |
| 1247 | clean data structures in initial states. Don't worry about freeing memory |
| 1248 | allocated by parent, just abandon it to be safe. */ |
| 1249 | static void __kmp_atfork_child(void) { |
Jonathan Peyton | eaa9e40 | 2018-01-10 18:21:48 +0000 | [diff] [blame] | 1250 | __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1251 | /* TODO make sure this is done right for nested/sibling */ |
| 1252 | // ATT: Memory leaks are here? TODO: Check it and fix. |
| 1253 | /* KMP_ASSERT( 0 ); */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1254 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1255 | ++__kmp_fork_count; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1256 | |
Jonathan Peyton | 072ccb7 | 2017-06-15 21:51:07 +0000 | [diff] [blame] | 1257 | #if KMP_AFFINITY_SUPPORTED |
| 1258 | #if KMP_OS_LINUX |
Jonathan Peyton | d330e63 | 2017-06-13 17:16:12 +0000 | [diff] [blame] | 1259 | // reset the affinity in the child to the initial thread |
| 1260 | // affinity in the parent |
| 1261 | kmp_set_thread_affinity_mask_initial(); |
| 1262 | #endif |
Jonathan Peyton | 072ccb7 | 2017-06-15 21:51:07 +0000 | [diff] [blame] | 1263 | // Set default not to bind threads tightly in the child (we’re expecting |
| 1264 | // over-subscription after the fork and this can improve things for |
| 1265 | // scripting languages that use OpenMP inside process-parallel code). |
| 1266 | __kmp_affinity_type = affinity_none; |
| 1267 | #if OMP_40_ENABLED |
| 1268 | if (__kmp_nested_proc_bind.bind_types != NULL) { |
| 1269 | __kmp_nested_proc_bind.bind_types[0] = proc_bind_false; |
| 1270 | } |
| 1271 | #endif // OMP_40_ENABLED |
| 1272 | #endif // KMP_AFFINITY_SUPPORTED |
Jonathan Peyton | d330e63 | 2017-06-13 17:16:12 +0000 | [diff] [blame] | 1273 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1274 | __kmp_init_runtime = FALSE; |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1275 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1276 | __kmp_init_monitor = 0; |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1277 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1278 | __kmp_init_parallel = FALSE; |
| 1279 | __kmp_init_middle = FALSE; |
| 1280 | __kmp_init_serial = FALSE; |
| 1281 | TCW_4(__kmp_init_gtid, FALSE); |
| 1282 | __kmp_init_common = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1283 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1284 | TCW_4(__kmp_init_user_locks, FALSE); |
| 1285 | #if !KMP_USE_DYNAMIC_LOCK |
| 1286 | __kmp_user_lock_table.used = 1; |
| 1287 | __kmp_user_lock_table.allocated = 0; |
| 1288 | __kmp_user_lock_table.table = NULL; |
| 1289 | __kmp_lock_blocks = NULL; |
Andrey Churbanov | 5c56fb5 | 2015-02-20 18:05:17 +0000 | [diff] [blame] | 1290 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1291 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1292 | __kmp_all_nth = 0; |
| 1293 | TCW_4(__kmp_nth, 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1294 | |
Jonathan Peyton | eaa9e40 | 2018-01-10 18:21:48 +0000 | [diff] [blame] | 1295 | __kmp_thread_pool = NULL; |
| 1296 | __kmp_thread_pool_insert_pt = NULL; |
| 1297 | __kmp_team_pool = NULL; |
| 1298 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1299 | /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate |
| 1300 | here so threadprivate doesn't use stale data */ |
| 1301 | KA_TRACE(10, ("__kmp_atfork_child: checking cache address list %p\n", |
| 1302 | __kmp_threadpriv_cache_list)); |
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 | while (__kmp_threadpriv_cache_list != NULL) { |
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 | if (*__kmp_threadpriv_cache_list->addr != NULL) { |
| 1307 | KC_TRACE(50, ("__kmp_atfork_child: zeroing cache at address %p\n", |
| 1308 | &(*__kmp_threadpriv_cache_list->addr))); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1309 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1310 | *__kmp_threadpriv_cache_list->addr = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1311 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1312 | __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list->next; |
| 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 | __kmp_init_runtime = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1316 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1317 | /* reset statically initialized locks */ |
| 1318 | __kmp_init_bootstrap_lock(&__kmp_initz_lock); |
| 1319 | __kmp_init_bootstrap_lock(&__kmp_stdio_lock); |
| 1320 | __kmp_init_bootstrap_lock(&__kmp_console_lock); |
Jonathan Peyton | eaa9e40 | 2018-01-10 18:21:48 +0000 | [diff] [blame] | 1321 | __kmp_init_bootstrap_lock(&__kmp_task_team_lock); |
| 1322 | |
Andrey Churbanov | 5388acd | 2018-01-11 15:09:49 +0000 | [diff] [blame] | 1323 | #if USE_ITT_BUILD |
Jonathan Peyton | eaa9e40 | 2018-01-10 18:21:48 +0000 | [diff] [blame] | 1324 | __kmp_itt_reset(); // reset ITT's global state |
Andrey Churbanov | 5388acd | 2018-01-11 15:09:49 +0000 | [diff] [blame] | 1325 | #endif /* USE_ITT_BUILD */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1326 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1327 | /* This is necessary to make sure no stale data is left around */ |
| 1328 | /* AC: customers complain that we use unsafe routines in the atfork |
| 1329 | handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen |
| 1330 | in dynamic_link when check the presence of shared tbbmalloc library. |
| 1331 | Suggestion is to make the library initialization lazier, similar |
| 1332 | to what done for __kmpc_begin(). */ |
| 1333 | // TODO: synchronize all static initializations with regular library |
| 1334 | // startup; look at kmp_global.cpp and etc. |
| 1335 | //__kmp_internal_begin (); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1338 | void __kmp_register_atfork(void) { |
| 1339 | if (__kmp_need_register_atfork) { |
| 1340 | int status = pthread_atfork(__kmp_atfork_prepare, __kmp_atfork_parent, |
| 1341 | __kmp_atfork_child); |
| 1342 | KMP_CHECK_SYSFAIL("pthread_atfork", status); |
| 1343 | __kmp_need_register_atfork = FALSE; |
| 1344 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1347 | void __kmp_suspend_initialize(void) { |
| 1348 | int status; |
| 1349 | status = pthread_mutexattr_init(&__kmp_suspend_mutex_attr); |
| 1350 | KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status); |
| 1351 | status = pthread_condattr_init(&__kmp_suspend_cond_attr); |
| 1352 | KMP_CHECK_SYSFAIL("pthread_condattr_init", status); |
| 1353 | } |
| 1354 | |
Jonathan Peyton | feac33e | 2019-04-08 17:50:02 +0000 | [diff] [blame] | 1355 | void __kmp_suspend_initialize_thread(kmp_info_t *th) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1356 | ANNOTATE_HAPPENS_AFTER(&th->th.th_suspend_init_count); |
| 1357 | if (th->th.th_suspend_init_count <= __kmp_fork_count) { |
| 1358 | /* this means we haven't initialized the suspension pthread objects for this |
| 1359 | thread in this instance of the process */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1360 | int status; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1361 | status = pthread_cond_init(&th->th.th_suspend_cv.c_cond, |
| 1362 | &__kmp_suspend_cond_attr); |
| 1363 | KMP_CHECK_SYSFAIL("pthread_cond_init", status); |
| 1364 | status = pthread_mutex_init(&th->th.th_suspend_mx.m_mutex, |
| 1365 | &__kmp_suspend_mutex_attr); |
| 1366 | KMP_CHECK_SYSFAIL("pthread_mutex_init", status); |
| 1367 | *(volatile int *)&th->th.th_suspend_init_count = __kmp_fork_count + 1; |
| 1368 | ANNOTATE_HAPPENS_BEFORE(&th->th.th_suspend_init_count); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1369 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1372 | void __kmp_suspend_uninitialize_thread(kmp_info_t *th) { |
| 1373 | if (th->th.th_suspend_init_count > __kmp_fork_count) { |
| 1374 | /* this means we have initialize the suspension pthread objects for this |
| 1375 | thread in this instance of the process */ |
| 1376 | int status; |
| 1377 | |
| 1378 | status = pthread_cond_destroy(&th->th.th_suspend_cv.c_cond); |
| 1379 | if (status != 0 && status != EBUSY) { |
| 1380 | KMP_SYSFAIL("pthread_cond_destroy", status); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1381 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1382 | status = pthread_mutex_destroy(&th->th.th_suspend_mx.m_mutex); |
| 1383 | if (status != 0 && status != EBUSY) { |
| 1384 | KMP_SYSFAIL("pthread_mutex_destroy", status); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1385 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1386 | --th->th.th_suspend_init_count; |
| 1387 | KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count); |
| 1388 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Jonathan Peyton | 9b8bb32 | 2019-01-16 20:07:39 +0000 | [diff] [blame] | 1391 | // return true if lock obtained, false otherwise |
| 1392 | int __kmp_try_suspend_mx(kmp_info_t *th) { |
| 1393 | return (pthread_mutex_trylock(&th->th.th_suspend_mx.m_mutex) == 0); |
| 1394 | } |
| 1395 | |
| 1396 | void __kmp_lock_suspend_mx(kmp_info_t *th) { |
| 1397 | int status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex); |
| 1398 | KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); |
| 1399 | } |
| 1400 | |
| 1401 | void __kmp_unlock_suspend_mx(kmp_info_t *th) { |
| 1402 | int status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); |
| 1403 | KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); |
| 1404 | } |
| 1405 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1406 | /* This routine puts the calling thread to sleep after setting the |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1407 | sleep bit for the indicated flag variable to true. */ |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1408 | template <class C> |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1409 | static inline void __kmp_suspend_template(int th_gtid, C *flag) { |
| 1410 | KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_suspend); |
| 1411 | kmp_info_t *th = __kmp_threads[th_gtid]; |
| 1412 | int status; |
| 1413 | typename C::flag_t old_spin; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1414 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1415 | KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, |
| 1416 | flag->get())); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1417 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1418 | __kmp_suspend_initialize_thread(th); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1419 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1420 | status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex); |
| 1421 | KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1422 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1423 | KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n", |
| 1424 | th_gtid, flag->get())); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1425 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1426 | /* TODO: shouldn't this use release semantics to ensure that |
| 1427 | __kmp_suspend_initialize_thread gets called first? */ |
| 1428 | old_spin = flag->set_sleeping(); |
Jonathan Peyton | 9b8bb32 | 2019-01-16 20:07:39 +0000 | [diff] [blame] | 1429 | #if OMP_50_ENABLED |
| 1430 | if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME && |
| 1431 | __kmp_pause_status != kmp_soft_paused) { |
| 1432 | flag->unset_sleeping(); |
| 1433 | status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); |
| 1434 | KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); |
| 1435 | return; |
| 1436 | } |
| 1437 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1438 | KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x," |
| 1439 | " was %x\n", |
Jonathan Peyton | 37e2ef5 | 2018-07-09 17:36:22 +0000 | [diff] [blame] | 1440 | th_gtid, flag->get(), flag->load(), old_spin)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1441 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1442 | if (flag->done_check_val(old_spin)) { |
| 1443 | old_spin = flag->unset_sleeping(); |
| 1444 | KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit " |
| 1445 | "for spin(%p)\n", |
| 1446 | th_gtid, flag->get())); |
| 1447 | } else { |
| 1448 | /* Encapsulate in a loop as the documentation states that this may |
| 1449 | "with low probability" return when the condition variable has |
| 1450 | not been signaled or broadcast */ |
| 1451 | int deactivated = FALSE; |
| 1452 | TCW_PTR(th->th.th_sleep_loc, (void *)flag); |
| 1453 | |
| 1454 | while (flag->is_sleeping()) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1455 | #ifdef DEBUG_SUSPEND |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1456 | char buffer[128]; |
| 1457 | __kmp_suspend_count++; |
| 1458 | __kmp_print_cond(buffer, &th->th.th_suspend_cv); |
| 1459 | __kmp_printf("__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, |
| 1460 | buffer); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1461 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1462 | // Mark the thread as no longer active (only in the first iteration of the |
| 1463 | // loop). |
| 1464 | if (!deactivated) { |
| 1465 | th->th.th_active = FALSE; |
| 1466 | if (th->th.th_active_in_pool) { |
| 1467 | th->th.th_active_in_pool = FALSE; |
Jonathan Peyton | 37e2ef5 | 2018-07-09 17:36:22 +0000 | [diff] [blame] | 1468 | KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1469 | KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0); |
| 1470 | } |
| 1471 | deactivated = TRUE; |
| 1472 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1473 | |
| 1474 | #if USE_SUSPEND_TIMEOUT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1475 | struct timespec now; |
| 1476 | struct timeval tval; |
| 1477 | int msecs; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1478 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1479 | status = gettimeofday(&tval, NULL); |
| 1480 | KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); |
| 1481 | TIMEVAL_TO_TIMESPEC(&tval, &now); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1482 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1483 | msecs = (4 * __kmp_dflt_blocktime) + 200; |
| 1484 | now.tv_sec += msecs / 1000; |
| 1485 | now.tv_nsec += (msecs % 1000) * 1000; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1486 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1487 | KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform " |
| 1488 | "pthread_cond_timedwait\n", |
| 1489 | th_gtid)); |
| 1490 | status = pthread_cond_timedwait(&th->th.th_suspend_cv.c_cond, |
| 1491 | &th->th.th_suspend_mx.m_mutex, &now); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1492 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1493 | KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform" |
| 1494 | " pthread_cond_wait\n", |
| 1495 | th_gtid)); |
| 1496 | status = pthread_cond_wait(&th->th.th_suspend_cv.c_cond, |
| 1497 | &th->th.th_suspend_mx.m_mutex); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1498 | #endif |
| 1499 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1500 | if ((status != 0) && (status != EINTR) && (status != ETIMEDOUT)) { |
| 1501 | KMP_SYSFAIL("pthread_cond_wait", status); |
| 1502 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1503 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1504 | if (status == ETIMEDOUT) { |
| 1505 | if (flag->is_sleeping()) { |
| 1506 | KF_TRACE(100, |
| 1507 | ("__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid)); |
| 1508 | } else { |
| 1509 | KF_TRACE(2, ("__kmp_suspend_template: T#%d timeout wakeup, sleep bit " |
| 1510 | "not set!\n", |
| 1511 | th_gtid)); |
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 | } else if (flag->is_sleeping()) { |
| 1514 | KF_TRACE(100, |
| 1515 | ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid)); |
| 1516 | } |
| 1517 | #endif |
| 1518 | } // while |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1519 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1520 | // Mark the thread as active again (if it was previous marked as inactive) |
| 1521 | if (deactivated) { |
| 1522 | th->th.th_active = TRUE; |
| 1523 | if (TCR_4(th->th.th_in_pool)) { |
Jonathan Peyton | 37e2ef5 | 2018-07-09 17:36:22 +0000 | [diff] [blame] | 1524 | KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1525 | th->th.th_active_in_pool = TRUE; |
| 1526 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1527 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1528 | } |
| 1529 | #ifdef DEBUG_SUSPEND |
| 1530 | { |
| 1531 | char buffer[128]; |
| 1532 | __kmp_print_cond(buffer, &th->th.th_suspend_cv); |
| 1533 | __kmp_printf("__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, |
| 1534 | buffer); |
| 1535 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1536 | #endif |
| 1537 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1538 | status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); |
| 1539 | KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); |
| 1540 | KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid)); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1544 | __kmp_suspend_template(th_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1545 | } |
| 1546 | void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1547 | __kmp_suspend_template(th_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1548 | } |
| 1549 | void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1550 | __kmp_suspend_template(th_gtid, flag); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1553 | /* This routine signals the thread specified by target_gtid to wake up |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1554 | after setting the sleep bit indicated by the flag argument to FALSE. |
| 1555 | The target thread must already have called __kmp_suspend_template() */ |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1556 | template <class C> |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1557 | static inline void __kmp_resume_template(int target_gtid, C *flag) { |
| 1558 | KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume); |
| 1559 | kmp_info_t *th = __kmp_threads[target_gtid]; |
| 1560 | int status; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1561 | |
| 1562 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1563 | int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1564 | #endif |
| 1565 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1566 | KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", |
| 1567 | gtid, target_gtid)); |
| 1568 | KMP_DEBUG_ASSERT(gtid != target_gtid); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1569 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1570 | __kmp_suspend_initialize_thread(th); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1571 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1572 | status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex); |
| 1573 | KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1574 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1575 | if (!flag) { // coming from __kmp_null_resume_wrapper |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 1576 | flag = (C *)CCAST(void *, th->th.th_sleep_loc); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | // First, check if the flag is null or its type has changed. If so, someone |
| 1580 | // else woke it up. |
| 1581 | if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type |
| 1582 | // simply shows what |
| 1583 | // flag was cast to |
| 1584 | KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already " |
| 1585 | "awake: flag(%p)\n", |
| 1586 | gtid, target_gtid, NULL)); |
| 1587 | status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); |
| 1588 | KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); |
| 1589 | return; |
| 1590 | } else { // if multiple threads are sleeping, flag should be internally |
| 1591 | // referring to a specific thread here |
| 1592 | typename C::flag_t old_spin = flag->unset_sleeping(); |
| 1593 | if (!flag->is_sleeping_val(old_spin)) { |
| 1594 | KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already " |
| 1595 | "awake: flag(%p): " |
| 1596 | "%u => %u\n", |
Jonathan Peyton | 37e2ef5 | 2018-07-09 17:36:22 +0000 | [diff] [blame] | 1597 | gtid, target_gtid, flag->get(), old_spin, flag->load())); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1598 | status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); |
| 1599 | KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); |
| 1600 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1601 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1602 | KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset " |
| 1603 | "sleep bit for flag's loc(%p): " |
| 1604 | "%u => %u\n", |
Jonathan Peyton | 37e2ef5 | 2018-07-09 17:36:22 +0000 | [diff] [blame] | 1605 | gtid, target_gtid, flag->get(), old_spin, flag->load())); |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1606 | } |
| 1607 | TCW_PTR(th->th.th_sleep_loc, NULL); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1608 | |
| 1609 | #ifdef DEBUG_SUSPEND |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1610 | { |
| 1611 | char buffer[128]; |
| 1612 | __kmp_print_cond(buffer, &th->th.th_suspend_cv); |
| 1613 | __kmp_printf("__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, |
| 1614 | target_gtid, buffer); |
| 1615 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1616 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1617 | status = pthread_cond_signal(&th->th.th_suspend_cv.c_cond); |
| 1618 | KMP_CHECK_SYSFAIL("pthread_cond_signal", status); |
| 1619 | status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex); |
| 1620 | KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); |
| 1621 | KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up" |
| 1622 | " for T#%d\n", |
| 1623 | gtid, target_gtid)); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1626 | void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1627 | __kmp_resume_template(target_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1628 | } |
| 1629 | void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1630 | __kmp_resume_template(target_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1631 | } |
| 1632 | void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1633 | __kmp_resume_template(target_gtid, flag); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1636 | #if KMP_USE_MONITOR |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1637 | void __kmp_resume_monitor() { |
| 1638 | KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume); |
| 1639 | int status; |
Jim Cownie | 07ea89f | 2014-09-03 11:10:54 +0000 | [diff] [blame] | 1640 | #ifdef KMP_DEBUG |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1641 | int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1; |
| 1642 | KF_TRACE(30, ("__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n", gtid, |
| 1643 | KMP_GTID_MONITOR)); |
| 1644 | KMP_DEBUG_ASSERT(gtid != KMP_GTID_MONITOR); |
Jim Cownie | 07ea89f | 2014-09-03 11:10:54 +0000 | [diff] [blame] | 1645 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1646 | status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex); |
| 1647 | KMP_CHECK_SYSFAIL("pthread_mutex_lock", status); |
Jim Cownie | 07ea89f | 2014-09-03 11:10:54 +0000 | [diff] [blame] | 1648 | #ifdef DEBUG_SUSPEND |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1649 | { |
| 1650 | char buffer[128]; |
| 1651 | __kmp_print_cond(buffer, &__kmp_wait_cv.c_cond); |
| 1652 | __kmp_printf("__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid, |
| 1653 | KMP_GTID_MONITOR, buffer); |
| 1654 | } |
Jim Cownie | 07ea89f | 2014-09-03 11:10:54 +0000 | [diff] [blame] | 1655 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1656 | status = pthread_cond_signal(&__kmp_wait_cv.c_cond); |
| 1657 | KMP_CHECK_SYSFAIL("pthread_cond_signal", status); |
| 1658 | status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex); |
| 1659 | KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status); |
| 1660 | KF_TRACE(30, ("__kmp_resume_monitor: T#%d exiting after signaling wake up" |
| 1661 | " for T#%d\n", |
| 1662 | gtid, KMP_GTID_MONITOR)); |
Jim Cownie | 07ea89f | 2014-09-03 11:10:54 +0000 | [diff] [blame] | 1663 | } |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1664 | #endif // KMP_USE_MONITOR |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1665 | |
Jonathan Peyton | e47d32f | 2019-02-28 19:11:29 +0000 | [diff] [blame] | 1666 | void __kmp_yield() { sched_yield(); } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1667 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1668 | void __kmp_gtid_set_specific(int gtid) { |
| 1669 | if (__kmp_init_gtid) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1670 | int status; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1671 | status = pthread_setspecific(__kmp_gtid_threadprivate_key, |
| 1672 | (void *)(intptr_t)(gtid + 1)); |
| 1673 | KMP_CHECK_SYSFAIL("pthread_setspecific", status); |
| 1674 | } else { |
| 1675 | KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n")); |
| 1676 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1679 | int __kmp_gtid_get_specific() { |
| 1680 | int gtid; |
| 1681 | if (!__kmp_init_gtid) { |
| 1682 | KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning " |
| 1683 | "KMP_GTID_SHUTDOWN\n")); |
| 1684 | return KMP_GTID_SHUTDOWN; |
| 1685 | } |
| 1686 | gtid = (int)(size_t)pthread_getspecific(__kmp_gtid_threadprivate_key); |
| 1687 | if (gtid == 0) { |
| 1688 | gtid = KMP_GTID_DNE; |
| 1689 | } else { |
| 1690 | gtid--; |
| 1691 | } |
| 1692 | KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n", |
| 1693 | __kmp_gtid_threadprivate_key, gtid)); |
| 1694 | return gtid; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1697 | double __kmp_read_cpu_time(void) { |
| 1698 | /*clock_t t;*/ |
| 1699 | struct tms buffer; |
| 1700 | |
| 1701 | /*t =*/times(&buffer); |
| 1702 | |
| 1703 | return (buffer.tms_utime + buffer.tms_cutime) / (double)CLOCKS_PER_SEC; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1706 | int __kmp_read_system_info(struct kmp_sys_info *info) { |
| 1707 | int status; |
| 1708 | struct rusage r_usage; |
| 1709 | |
| 1710 | memset(info, 0, sizeof(*info)); |
| 1711 | |
| 1712 | status = getrusage(RUSAGE_SELF, &r_usage); |
| 1713 | KMP_CHECK_SYSFAIL_ERRNO("getrusage", status); |
| 1714 | |
| 1715 | // The maximum resident set size utilized (in kilobytes) |
| 1716 | info->maxrss = r_usage.ru_maxrss; |
| 1717 | // The number of page faults serviced without any I/O |
| 1718 | info->minflt = r_usage.ru_minflt; |
| 1719 | // The number of page faults serviced that required I/O |
| 1720 | info->majflt = r_usage.ru_majflt; |
| 1721 | // The number of times a process was "swapped" out of memory |
| 1722 | info->nswap = r_usage.ru_nswap; |
| 1723 | // The number of times the file system had to perform input |
| 1724 | info->inblock = r_usage.ru_inblock; |
| 1725 | // The number of times the file system had to perform output |
| 1726 | info->oublock = r_usage.ru_oublock; |
| 1727 | // The number of times a context switch was voluntarily |
| 1728 | info->nvcsw = r_usage.ru_nvcsw; |
| 1729 | // The number of times a context switch was forced |
| 1730 | info->nivcsw = r_usage.ru_nivcsw; |
| 1731 | |
| 1732 | return (status != 0); |
| 1733 | } |
| 1734 | |
| 1735 | void __kmp_read_system_time(double *delta) { |
| 1736 | double t_ns; |
| 1737 | struct timeval tval; |
| 1738 | struct timespec stop; |
| 1739 | int status; |
| 1740 | |
| 1741 | status = gettimeofday(&tval, NULL); |
| 1742 | KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); |
| 1743 | TIMEVAL_TO_TIMESPEC(&tval, &stop); |
| 1744 | t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start); |
| 1745 | *delta = (t_ns * 1e-9); |
| 1746 | } |
| 1747 | |
| 1748 | void __kmp_clear_system_time(void) { |
| 1749 | struct timeval tval; |
| 1750 | int status; |
| 1751 | status = gettimeofday(&tval, NULL); |
| 1752 | KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); |
| 1753 | TIMEVAL_TO_TIMESPEC(&tval, &__kmp_sys_timer_data.start); |
| 1754 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1755 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1756 | static int __kmp_get_xproc(void) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1757 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1758 | int r = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1759 | |
Kamil Rytarowski | a56ac94 | 2018-12-09 16:40:33 +0000 | [diff] [blame] | 1760 | #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \ |
Kamil Rytarowski | 7e1ea99 | 2018-12-09 16:46:48 +0000 | [diff] [blame] | 1761 | KMP_OS_OPENBSD || KMP_OS_HURD |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1762 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1763 | r = sysconf(_SC_NPROCESSORS_ONLN); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1764 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1765 | #elif KMP_OS_DARWIN |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1766 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1767 | // Bug C77011 High "OpenMP Threads and number of active cores". |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1768 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1769 | // Find the number of available CPUs. |
| 1770 | kern_return_t rc; |
| 1771 | host_basic_info_data_t info; |
| 1772 | mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT; |
| 1773 | rc = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&info, &num); |
| 1774 | if (rc == 0 && num == HOST_BASIC_INFO_COUNT) { |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 1775 | // Cannot use KA_TRACE() here because this code works before trace support |
| 1776 | // is initialized. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1777 | r = info.avail_cpus; |
| 1778 | } else { |
| 1779 | KMP_WARNING(CantGetNumAvailCPU); |
| 1780 | KMP_INFORM(AssumedNumCPU); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1781 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1782 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1783 | #else |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1784 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1785 | #error "Unknown or unsupported OS." |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1786 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1787 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1788 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1789 | return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1790 | |
| 1791 | } // __kmp_get_xproc |
| 1792 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1793 | int __kmp_read_from_file(char const *path, char const *format, ...) { |
| 1794 | int result; |
| 1795 | va_list args; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1796 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1797 | va_start(args, format); |
| 1798 | FILE *f = fopen(path, "rb"); |
| 1799 | if (f == NULL) |
| 1800 | return 0; |
| 1801 | result = vfscanf(f, format, args); |
| 1802 | fclose(f); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1803 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1804 | return result; |
Jim Cownie | 181b4bb | 2013-12-23 17:28:57 +0000 | [diff] [blame] | 1805 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1806 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1807 | void __kmp_runtime_initialize(void) { |
| 1808 | int status; |
| 1809 | pthread_mutexattr_t mutex_attr; |
| 1810 | pthread_condattr_t cond_attr; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1811 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1812 | if (__kmp_init_runtime) { |
| 1813 | return; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1814 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1815 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1816 | #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) |
| 1817 | if (!__kmp_cpuinfo.initialized) { |
| 1818 | __kmp_query_cpuid(&__kmp_cpuinfo); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1819 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1820 | #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1821 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1822 | __kmp_xproc = __kmp_get_xproc(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1823 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1824 | if (sysconf(_SC_THREADS)) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1825 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1826 | /* Query the maximum number of threads */ |
| 1827 | __kmp_sys_max_nth = sysconf(_SC_THREAD_THREADS_MAX); |
| 1828 | if (__kmp_sys_max_nth == -1) { |
| 1829 | /* Unlimited threads for NPTL */ |
| 1830 | __kmp_sys_max_nth = INT_MAX; |
| 1831 | } else if (__kmp_sys_max_nth <= 1) { |
| 1832 | /* Can't tell, just use PTHREAD_THREADS_MAX */ |
| 1833 | __kmp_sys_max_nth = KMP_MAX_NTH; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1836 | /* Query the minimum stack size */ |
| 1837 | __kmp_sys_min_stksize = sysconf(_SC_THREAD_STACK_MIN); |
| 1838 | if (__kmp_sys_min_stksize <= 1) { |
| 1839 | __kmp_sys_min_stksize = KMP_MIN_STKSIZE; |
| 1840 | } |
| 1841 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1842 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1843 | /* Set up minimum number of threads to switch to TLS gtid */ |
| 1844 | __kmp_tls_gtid_min = KMP_TLS_GTID_MIN; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1845 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1846 | status = pthread_key_create(&__kmp_gtid_threadprivate_key, |
| 1847 | __kmp_internal_end_dest); |
| 1848 | KMP_CHECK_SYSFAIL("pthread_key_create", status); |
| 1849 | status = pthread_mutexattr_init(&mutex_attr); |
| 1850 | KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status); |
| 1851 | status = pthread_mutex_init(&__kmp_wait_mx.m_mutex, &mutex_attr); |
| 1852 | KMP_CHECK_SYSFAIL("pthread_mutex_init", status); |
| 1853 | status = pthread_condattr_init(&cond_attr); |
| 1854 | KMP_CHECK_SYSFAIL("pthread_condattr_init", status); |
| 1855 | status = pthread_cond_init(&__kmp_wait_cv.c_cond, &cond_attr); |
| 1856 | KMP_CHECK_SYSFAIL("pthread_cond_init", status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1857 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1858 | __kmp_itt_initialize(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1859 | #endif /* USE_ITT_BUILD */ |
| 1860 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1861 | __kmp_init_runtime = TRUE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1864 | void __kmp_runtime_destroy(void) { |
| 1865 | int status; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1866 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1867 | if (!__kmp_init_runtime) { |
| 1868 | return; // Nothing to do. |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1869 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1870 | |
| 1871 | #if USE_ITT_BUILD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1872 | __kmp_itt_destroy(); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1873 | #endif /* USE_ITT_BUILD */ |
| 1874 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1875 | status = pthread_key_delete(__kmp_gtid_threadprivate_key); |
| 1876 | KMP_CHECK_SYSFAIL("pthread_key_delete", status); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1877 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1878 | status = pthread_mutex_destroy(&__kmp_wait_mx.m_mutex); |
| 1879 | if (status != 0 && status != EBUSY) { |
| 1880 | KMP_SYSFAIL("pthread_mutex_destroy", status); |
| 1881 | } |
| 1882 | status = pthread_cond_destroy(&__kmp_wait_cv.c_cond); |
| 1883 | if (status != 0 && status != EBUSY) { |
| 1884 | KMP_SYSFAIL("pthread_cond_destroy", status); |
| 1885 | } |
| 1886 | #if KMP_AFFINITY_SUPPORTED |
| 1887 | __kmp_affinity_uninitialize(); |
| 1888 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1889 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1890 | __kmp_init_runtime = FALSE; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1893 | /* Put the thread to sleep for a time period */ |
| 1894 | /* NOTE: not currently used anywhere */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1895 | void __kmp_thread_sleep(int millis) { sleep((millis + 500) / 1000); } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1896 | |
| 1897 | /* Calculate the elapsed wall clock time for the user */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1898 | void __kmp_elapsed(double *t) { |
| 1899 | int status; |
| 1900 | #ifdef FIX_SGI_CLOCK |
| 1901 | struct timespec ts; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1902 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1903 | status = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); |
| 1904 | KMP_CHECK_SYSFAIL_ERRNO("clock_gettime", status); |
| 1905 | *t = |
| 1906 | (double)ts.tv_nsec * (1.0 / (double)KMP_NSEC_PER_SEC) + (double)ts.tv_sec; |
| 1907 | #else |
| 1908 | struct timeval tv; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1909 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1910 | status = gettimeofday(&tv, NULL); |
| 1911 | KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status); |
| 1912 | *t = |
| 1913 | (double)tv.tv_usec * (1.0 / (double)KMP_USEC_PER_SEC) + (double)tv.tv_sec; |
| 1914 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | /* Calculate the elapsed wall clock tick for the user */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1918 | void __kmp_elapsed_tick(double *t) { *t = 1 / (double)CLOCKS_PER_SEC; } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1919 | |
Jonathan Peyton | 377aa40 | 2016-04-14 16:00:37 +0000 | [diff] [blame] | 1920 | /* Return the current time stamp in nsec */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1921 | kmp_uint64 __kmp_now_nsec() { |
| 1922 | struct timeval t; |
| 1923 | gettimeofday(&t, NULL); |
Jonathan Peyton | bdb0a2f | 2018-12-13 23:18:55 +0000 | [diff] [blame] | 1924 | kmp_uint64 nsec = (kmp_uint64)KMP_NSEC_PER_SEC * (kmp_uint64)t.tv_sec + |
| 1925 | (kmp_uint64)1000 * (kmp_uint64)t.tv_usec; |
| 1926 | return nsec; |
Jonathan Peyton | 377aa40 | 2016-04-14 16:00:37 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1929 | #if KMP_ARCH_X86 || KMP_ARCH_X86_64 |
Jonathan Peyton | 35d75ae | 2017-03-20 22:11:31 +0000 | [diff] [blame] | 1930 | /* Measure clock ticks per millisecond */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1931 | void __kmp_initialize_system_tick() { |
Jonathan Peyton | bdb0a2f | 2018-12-13 23:18:55 +0000 | [diff] [blame] | 1932 | kmp_uint64 now, nsec2, diff; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1933 | kmp_uint64 delay = 100000; // 50~100 usec on most machines. |
| 1934 | kmp_uint64 nsec = __kmp_now_nsec(); |
| 1935 | kmp_uint64 goal = __kmp_hardware_timestamp() + delay; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1936 | while ((now = __kmp_hardware_timestamp()) < goal) |
| 1937 | ; |
Jonathan Peyton | bdb0a2f | 2018-12-13 23:18:55 +0000 | [diff] [blame] | 1938 | nsec2 = __kmp_now_nsec(); |
| 1939 | diff = nsec2 - nsec; |
| 1940 | if (diff > 0) { |
| 1941 | kmp_uint64 tpms = (kmp_uint64)(1e6 * (delay + (now - goal)) / diff); |
| 1942 | if (tpms > 0) |
| 1943 | __kmp_ticks_per_msec = tpms; |
| 1944 | } |
Jonathan Peyton | b66d1aa | 2016-09-27 17:11:17 +0000 | [diff] [blame] | 1945 | } |
| 1946 | #endif |
| 1947 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1948 | /* Determine whether the given address is mapped into the current address |
| 1949 | space. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1950 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1951 | int __kmp_is_address_mapped(void *addr) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1952 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1953 | int found = 0; |
| 1954 | int rc; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1955 | |
Andrey Churbanov | 855d098 | 2018-11-07 12:27:38 +0000 | [diff] [blame] | 1956 | #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_HURD |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1957 | |
Andrey Churbanov | 855d098 | 2018-11-07 12:27:38 +0000 | [diff] [blame] | 1958 | /* On GNUish OSes, read the /proc/<pid>/maps pseudo-file to get all the address |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1959 | ranges mapped into the address space. */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1960 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1961 | char *name = __kmp_str_format("/proc/%d/maps", getpid()); |
| 1962 | FILE *file = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1963 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1964 | file = fopen(name, "r"); |
| 1965 | KMP_ASSERT(file != NULL); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1966 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1967 | for (;;) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1968 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1969 | void *beginning = NULL; |
| 1970 | void *ending = NULL; |
| 1971 | char perms[5]; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1972 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1973 | rc = fscanf(file, "%p-%p %4s %*[^\n]\n", &beginning, &ending, perms); |
| 1974 | if (rc == EOF) { |
| 1975 | break; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1976 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1977 | KMP_ASSERT(rc == 3 && |
| 1978 | KMP_STRLEN(perms) == 4); // Make sure all fields are read. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1979 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1980 | // Ending address is not included in the region, but beginning is. |
| 1981 | if ((addr >= beginning) && (addr < ending)) { |
| 1982 | perms[2] = 0; // 3th and 4th character does not matter. |
| 1983 | if (strcmp(perms, "rw") == 0) { |
| 1984 | // Memory we are looking for should be readable and writable. |
Alp Toker | 763b939 | 2014-02-28 09:42:41 +0000 | [diff] [blame] | 1985 | found = 1; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1986 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1987 | break; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 1988 | } |
| 1989 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1990 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1991 | // Free resources. |
| 1992 | fclose(file); |
| 1993 | KMP_INTERNAL_FREE(name); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1994 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1995 | #elif KMP_OS_DARWIN |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1996 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 1997 | /* On OS X*, /proc pseudo filesystem is not available. Try to read memory |
| 1998 | using vm interface. */ |
| 1999 | |
| 2000 | int buffer; |
| 2001 | vm_size_t count; |
| 2002 | rc = vm_read_overwrite( |
| 2003 | mach_task_self(), // Task to read memory of. |
| 2004 | (vm_address_t)(addr), // Address to read from. |
| 2005 | 1, // Number of bytes to be read. |
| 2006 | (vm_address_t)(&buffer), // Address of buffer to save read bytes in. |
| 2007 | &count // Address of var to save number of read bytes in. |
| 2008 | ); |
| 2009 | if (rc == 0) { |
| 2010 | // Memory successfully read. |
| 2011 | found = 1; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2012 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2013 | |
Kamil Rytarowski | 316f423 | 2018-12-11 18:35:07 +0000 | [diff] [blame] | 2014 | #elif KMP_OS_NETBSD |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2015 | |
Kamil Rytarowski | 316f423 | 2018-12-11 18:35:07 +0000 | [diff] [blame] | 2016 | int mib[5]; |
| 2017 | mib[0] = CTL_VM; |
| 2018 | mib[1] = VM_PROC; |
| 2019 | mib[2] = VM_PROC_MAP; |
| 2020 | mib[3] = getpid(); |
| 2021 | mib[4] = sizeof(struct kinfo_vmentry); |
| 2022 | |
| 2023 | size_t size; |
| 2024 | rc = sysctl(mib, __arraycount(mib), NULL, &size, NULL, 0); |
| 2025 | KMP_ASSERT(!rc); |
| 2026 | KMP_ASSERT(size); |
| 2027 | |
| 2028 | size = size * 4 / 3; |
| 2029 | struct kinfo_vmentry *kiv = (struct kinfo_vmentry *)KMP_INTERNAL_MALLOC(size); |
| 2030 | KMP_ASSERT(kiv); |
| 2031 | |
| 2032 | rc = sysctl(mib, __arraycount(mib), kiv, &size, NULL, 0); |
| 2033 | KMP_ASSERT(!rc); |
| 2034 | KMP_ASSERT(size); |
| 2035 | |
| 2036 | for (size_t i = 0; i < size; i++) { |
| 2037 | if (kiv[i].kve_start >= (uint64_t)addr && |
| 2038 | kiv[i].kve_end <= (uint64_t)addr) { |
| 2039 | found = 1; |
| 2040 | break; |
| 2041 | } |
| 2042 | } |
| 2043 | KMP_INTERNAL_FREE(kiv); |
| 2044 | #elif KMP_OS_DRAGONFLY || KMP_OS_OPENBSD |
| 2045 | |
| 2046 | // FIXME(DragonFly, OpenBSD): Implement this |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2047 | found = 1; |
| 2048 | |
| 2049 | #else |
| 2050 | |
| 2051 | #error "Unknown or unsupported OS" |
| 2052 | |
| 2053 | #endif |
| 2054 | |
| 2055 | return found; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2056 | |
| 2057 | } // __kmp_is_address_mapped |
| 2058 | |
| 2059 | #ifdef USE_LOAD_BALANCE |
| 2060 | |
Michal Gorny | 70cdd83 | 2018-12-11 19:02:09 +0000 | [diff] [blame] | 2061 | #if KMP_OS_DARWIN || KMP_OS_NETBSD |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2062 | |
| 2063 | // The function returns the rounded value of the system load average |
| 2064 | // during given time interval which depends on the value of |
| 2065 | // __kmp_load_balance_interval variable (default is 60 sec, other values |
| 2066 | // may be 300 sec or 900 sec). |
| 2067 | // It returns -1 in case of error. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2068 | int __kmp_get_load_balance(int max) { |
| 2069 | double averages[3]; |
| 2070 | int ret_avg = 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2071 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2072 | int res = getloadavg(averages, 3); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2073 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2074 | // Check __kmp_load_balance_interval to determine which of averages to use. |
| 2075 | // getloadavg() may return the number of samples less than requested that is |
| 2076 | // less than 3. |
| 2077 | if (__kmp_load_balance_interval < 180 && (res >= 1)) { |
| 2078 | ret_avg = averages[0]; // 1 min |
| 2079 | } else if ((__kmp_load_balance_interval >= 180 && |
| 2080 | __kmp_load_balance_interval < 600) && |
| 2081 | (res >= 2)) { |
| 2082 | ret_avg = averages[1]; // 5 min |
| 2083 | } else if ((__kmp_load_balance_interval >= 600) && (res == 3)) { |
| 2084 | ret_avg = averages[2]; // 15 min |
| 2085 | } else { // Error occurred |
| 2086 | return -1; |
| 2087 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2088 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2089 | return ret_avg; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2092 | #else // Linux* OS |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2093 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2094 | // The fuction returns number of running (not sleeping) threads, or -1 in case |
| 2095 | // of error. Error could be reported if Linux* OS kernel too old (without |
| 2096 | // "/proc" support). Counting running threads stops if max running threads |
| 2097 | // encountered. |
| 2098 | int __kmp_get_load_balance(int max) { |
| 2099 | static int permanent_error = 0; |
| 2100 | static int glb_running_threads = 0; // Saved count of the running threads for |
| 2101 | // the thread balance algortihm |
| 2102 | static double glb_call_time = 0; /* Thread balance algorithm call time */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2103 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2104 | int running_threads = 0; // Number of running threads in the system. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2105 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2106 | DIR *proc_dir = NULL; // Handle of "/proc/" directory. |
| 2107 | struct dirent *proc_entry = NULL; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2108 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2109 | kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path. |
| 2110 | DIR *task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory. |
| 2111 | struct dirent *task_entry = NULL; |
| 2112 | int task_path_fixed_len; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2113 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2114 | kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path. |
| 2115 | int stat_file = -1; |
| 2116 | int stat_path_fixed_len; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2117 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2118 | int total_processes = 0; // Total number of processes in system. |
| 2119 | int total_threads = 0; // Total number of threads in system. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2120 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2121 | double call_time = 0.0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2122 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2123 | __kmp_str_buf_init(&task_path); |
| 2124 | __kmp_str_buf_init(&stat_path); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2125 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2126 | __kmp_elapsed(&call_time); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2127 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2128 | if (glb_call_time && |
| 2129 | (call_time - glb_call_time < __kmp_load_balance_interval)) { |
| 2130 | running_threads = glb_running_threads; |
| 2131 | goto finish; |
| 2132 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2133 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2134 | glb_call_time = call_time; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2135 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2136 | // Do not spend time on scanning "/proc/" if we have a permanent error. |
| 2137 | if (permanent_error) { |
| 2138 | running_threads = -1; |
| 2139 | goto finish; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2140 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2141 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2142 | if (max <= 0) { |
| 2143 | max = INT_MAX; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2144 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2145 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2146 | // Open "/proc/" directory. |
| 2147 | proc_dir = opendir("/proc"); |
| 2148 | if (proc_dir == NULL) { |
| 2149 | // Cannot open "/prroc/". Probably the kernel does not support it. Return an |
| 2150 | // error now and in subsequent calls. |
| 2151 | running_threads = -1; |
| 2152 | permanent_error = 1; |
| 2153 | goto finish; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2154 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2155 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2156 | // Initialize fixed part of task_path. This part will not change. |
| 2157 | __kmp_str_buf_cat(&task_path, "/proc/", 6); |
| 2158 | task_path_fixed_len = task_path.used; // Remember number of used characters. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2159 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2160 | proc_entry = readdir(proc_dir); |
| 2161 | while (proc_entry != NULL) { |
| 2162 | // Proc entry is a directory and name starts with a digit. Assume it is a |
| 2163 | // process' directory. |
| 2164 | if (proc_entry->d_type == DT_DIR && isdigit(proc_entry->d_name[0])) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2165 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2166 | ++total_processes; |
| 2167 | // Make sure init process is the very first in "/proc", so we can replace |
| 2168 | // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes == |
| 2169 | // 1. We are going to check that total_processes == 1 => d_name == "1" is |
| 2170 | // true (where "=>" is implication). Since C++ does not have => operator, |
| 2171 | // let us replace it with its equivalent: a => b == ! a || b. |
| 2172 | KMP_DEBUG_ASSERT(total_processes != 1 || |
| 2173 | strcmp(proc_entry->d_name, "1") == 0); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2174 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2175 | // Construct task_path. |
| 2176 | task_path.used = task_path_fixed_len; // Reset task_path to "/proc/". |
| 2177 | __kmp_str_buf_cat(&task_path, proc_entry->d_name, |
| 2178 | KMP_STRLEN(proc_entry->d_name)); |
| 2179 | __kmp_str_buf_cat(&task_path, "/task", 5); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2180 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2181 | task_dir = opendir(task_path.str); |
| 2182 | if (task_dir == NULL) { |
| 2183 | // Process can finish between reading "/proc/" directory entry and |
| 2184 | // opening process' "task/" directory. So, in general case we should not |
| 2185 | // complain, but have to skip this process and read the next one. But on |
| 2186 | // systems with no "task/" support we will spend lot of time to scan |
| 2187 | // "/proc/" tree again and again without any benefit. "init" process |
| 2188 | // (its pid is 1) should exist always, so, if we cannot open |
| 2189 | // "/proc/1/task/" directory, it means "task/" is not supported by |
| 2190 | // kernel. Report an error now and in the future. |
| 2191 | if (strcmp(proc_entry->d_name, "1") == 0) { |
| 2192 | running_threads = -1; |
| 2193 | permanent_error = 1; |
| 2194 | goto finish; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2195 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2196 | } else { |
| 2197 | // Construct fixed part of stat file path. |
| 2198 | __kmp_str_buf_clear(&stat_path); |
| 2199 | __kmp_str_buf_cat(&stat_path, task_path.str, task_path.used); |
| 2200 | __kmp_str_buf_cat(&stat_path, "/", 1); |
| 2201 | stat_path_fixed_len = stat_path.used; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2202 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2203 | task_entry = readdir(task_dir); |
| 2204 | while (task_entry != NULL) { |
| 2205 | // It is a directory and name starts with a digit. |
| 2206 | if (proc_entry->d_type == DT_DIR && isdigit(task_entry->d_name[0])) { |
| 2207 | ++total_threads; |
| 2208 | |
| 2209 | // Consruct complete stat file path. Easiest way would be: |
| 2210 | // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str, |
| 2211 | // task_entry->d_name ); |
| 2212 | // but seriae of __kmp_str_buf_cat works a bit faster. |
| 2213 | stat_path.used = |
| 2214 | stat_path_fixed_len; // Reset stat path to its fixed part. |
| 2215 | __kmp_str_buf_cat(&stat_path, task_entry->d_name, |
| 2216 | KMP_STRLEN(task_entry->d_name)); |
| 2217 | __kmp_str_buf_cat(&stat_path, "/stat", 5); |
| 2218 | |
| 2219 | // Note: Low-level API (open/read/close) is used. High-level API |
| 2220 | // (fopen/fclose) works ~ 30 % slower. |
| 2221 | stat_file = open(stat_path.str, O_RDONLY); |
| 2222 | if (stat_file == -1) { |
| 2223 | // We cannot report an error because task (thread) can terminate |
| 2224 | // just before reading this file. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2225 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2226 | /* Content of "stat" file looks like: |
| 2227 | 24285 (program) S ... |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2228 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2229 | It is a single line (if program name does not include funny |
| 2230 | symbols). First number is a thread id, then name of executable |
| 2231 | file name in paretheses, then state of the thread. We need just |
| 2232 | thread state. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2233 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2234 | Good news: Length of program name is 15 characters max. Longer |
| 2235 | names are truncated. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2236 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2237 | Thus, we need rather short buffer: 15 chars for program name + |
| 2238 | 2 parenthesis, + 3 spaces + ~7 digits of pid = 37. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2239 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2240 | Bad news: Program name may contain special symbols like space, |
| 2241 | closing parenthesis, or even new line. This makes parsing |
| 2242 | "stat" file not 100 % reliable. In case of fanny program names |
| 2243 | parsing may fail (report incorrect thread state). |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2244 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2245 | Parsing "status" file looks more promissing (due to different |
| 2246 | file structure and escaping special symbols) but reading and |
| 2247 | parsing of "status" file works slower. |
| 2248 | -- ln |
| 2249 | */ |
| 2250 | char buffer[65]; |
| 2251 | int len; |
| 2252 | len = read(stat_file, buffer, sizeof(buffer) - 1); |
| 2253 | if (len >= 0) { |
| 2254 | buffer[len] = 0; |
| 2255 | // Using scanf: |
| 2256 | // sscanf( buffer, "%*d (%*s) %c ", & state ); |
| 2257 | // looks very nice, but searching for a closing parenthesis |
| 2258 | // works a bit faster. |
| 2259 | char *close_parent = strstr(buffer, ") "); |
| 2260 | if (close_parent != NULL) { |
| 2261 | char state = *(close_parent + 2); |
| 2262 | if (state == 'R') { |
| 2263 | ++running_threads; |
| 2264 | if (running_threads >= max) { |
| 2265 | goto finish; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | } |
| 2269 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2270 | close(stat_file); |
| 2271 | stat_file = -1; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2272 | } |
| 2273 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2274 | task_entry = readdir(task_dir); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2275 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2276 | closedir(task_dir); |
| 2277 | task_dir = NULL; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2278 | } |
| 2279 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2280 | proc_entry = readdir(proc_dir); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2281 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2282 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2283 | // There _might_ be a timing hole where the thread executing this |
| 2284 | // code get skipped in the load balance, and running_threads is 0. |
| 2285 | // Assert in the debug builds only!!! |
| 2286 | KMP_DEBUG_ASSERT(running_threads > 0); |
| 2287 | if (running_threads <= 0) { |
| 2288 | running_threads = 1; |
| 2289 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2290 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2291 | finish: // Clean up and exit. |
| 2292 | if (proc_dir != NULL) { |
| 2293 | closedir(proc_dir); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2294 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2295 | __kmp_str_buf_free(&task_path); |
| 2296 | if (task_dir != NULL) { |
| 2297 | closedir(task_dir); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2298 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2299 | __kmp_str_buf_free(&stat_path); |
| 2300 | if (stat_file != -1) { |
| 2301 | close(stat_file); |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 2302 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2303 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2304 | glb_running_threads = running_threads; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2305 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2306 | return running_threads; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2307 | |
| 2308 | } // __kmp_get_load_balance |
| 2309 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2310 | #endif // KMP_OS_DARWIN |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2311 | |
| 2312 | #endif // USE_LOAD_BALANCE |
| 2313 | |
Andrey Churbanov | c47afcd | 2017-07-03 11:24:08 +0000 | [diff] [blame] | 2314 | #if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \ |
| 2315 | ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || KMP_ARCH_PPC64) |
Jim Cownie | 3051f97 | 2014-08-07 10:12:54 +0000 | [diff] [blame] | 2316 | |
| 2317 | // we really only need the case with 1 argument, because CLANG always build |
| 2318 | // a struct of pointers to shared variables referenced in the outlined function |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2319 | int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int tid, int argc, |
| 2320 | void *p_argv[] |
Jonathan Peyton | 122dd76 | 2015-07-13 18:55:45 +0000 | [diff] [blame] | 2321 | #if OMPT_SUPPORT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2322 | , |
| 2323 | void **exit_frame_ptr |
Jonathan Peyton | 122dd76 | 2015-07-13 18:55:45 +0000 | [diff] [blame] | 2324 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 2325 | ) { |
Jonathan Peyton | 122dd76 | 2015-07-13 18:55:45 +0000 | [diff] [blame] | 2326 | #if OMPT_SUPPORT |
Joachim Protze | 82e94a5 | 2017-11-01 10:08:30 +0000 | [diff] [blame] | 2327 | *exit_frame_ptr = OMPT_GET_FRAME_ADDRESS(0); |
Jonathan Peyton | 122dd76 | 2015-07-13 18:55:45 +0000 | [diff] [blame] | 2328 | #endif |
| 2329 | |
Jim Cownie | 3051f97 | 2014-08-07 10:12:54 +0000 | [diff] [blame] | 2330 | switch (argc) { |
| 2331 | default: |
| 2332 | fprintf(stderr, "Too many args to microtask: %d!\n", argc); |
| 2333 | fflush(stderr); |
| 2334 | exit(-1); |
| 2335 | case 0: |
| 2336 | (*pkfn)(>id, &tid); |
| 2337 | break; |
| 2338 | case 1: |
| 2339 | (*pkfn)(>id, &tid, p_argv[0]); |
| 2340 | break; |
| 2341 | case 2: |
| 2342 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1]); |
| 2343 | break; |
| 2344 | case 3: |
| 2345 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2]); |
| 2346 | break; |
| 2347 | case 4: |
| 2348 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]); |
| 2349 | break; |
| 2350 | case 5: |
| 2351 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]); |
| 2352 | break; |
| 2353 | case 6: |
| 2354 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2355 | p_argv[5]); |
| 2356 | break; |
| 2357 | case 7: |
| 2358 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2359 | p_argv[5], p_argv[6]); |
| 2360 | break; |
| 2361 | case 8: |
| 2362 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2363 | p_argv[5], p_argv[6], p_argv[7]); |
| 2364 | break; |
| 2365 | case 9: |
| 2366 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2367 | p_argv[5], p_argv[6], p_argv[7], p_argv[8]); |
| 2368 | break; |
| 2369 | case 10: |
| 2370 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2371 | p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]); |
| 2372 | break; |
| 2373 | case 11: |
| 2374 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2375 | p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]); |
| 2376 | break; |
| 2377 | case 12: |
| 2378 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2379 | p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10], |
| 2380 | p_argv[11]); |
| 2381 | break; |
| 2382 | case 13: |
| 2383 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2384 | p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10], |
| 2385 | p_argv[11], p_argv[12]); |
| 2386 | break; |
| 2387 | case 14: |
| 2388 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2389 | p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10], |
| 2390 | p_argv[11], p_argv[12], p_argv[13]); |
| 2391 | break; |
| 2392 | case 15: |
| 2393 | (*pkfn)(>id, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4], |
| 2394 | p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10], |
| 2395 | p_argv[11], p_argv[12], p_argv[13], p_argv[14]); |
| 2396 | break; |
| 2397 | } |
| 2398 | |
Jonathan Peyton | 122dd76 | 2015-07-13 18:55:45 +0000 | [diff] [blame] | 2399 | #if OMPT_SUPPORT |
| 2400 | *exit_frame_ptr = 0; |
| 2401 | #endif |
| 2402 | |
Jim Cownie | 3051f97 | 2014-08-07 10:12:54 +0000 | [diff] [blame] | 2403 | return 1; |
| 2404 | } |
| 2405 | |
| 2406 | #endif |
Jim Cownie | 181b4bb | 2013-12-23 17:28:57 +0000 | [diff] [blame] | 2407 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 2408 | // end of file // |