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