blob: 7b008d74f66930fa5c09f8745431c38d3cbdd3db [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
Jonathan Peytonde4749b2016-12-14 23:01:24 +00002 * z_Linux_util.cpp -- platform specific routines.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
Jim Cownie5e8470a2013-09-27 10:38:44 +00005//===----------------------------------------------------------------------===//
6//
7// The LLVM Compiler Infrastructure
8//
9// This file is dual licensed under the MIT and the University of Illinois Open
10// Source Licenses. See LICENSE.txt for details.
11//
12//===----------------------------------------------------------------------===//
13
Jim Cownie5e8470a2013-09-27 10:38:44 +000014#include "kmp.h"
Jonathan Peyton1cdd87a2016-11-14 21:08:35 +000015#include "kmp_affinity.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000016#include "kmp_i18n.h"
17#include "kmp_io.h"
18#include "kmp_itt.h"
19#include "kmp_lock.h"
20#include "kmp_stats.h"
21#include "kmp_str.h"
22#include "kmp_wait_release.h"
23#include "kmp_wrapper_getpid.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000024
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +000025#if !KMP_OS_DRAGONFLY && !KMP_OS_FREEBSD && !KMP_OS_NETBSD && !KMP_OS_OPENBSD
Jonathan Peyton30419822017-05-12 18:01:32 +000026#include <alloca.h>
Alp Toker763b9392014-02-28 09:42:41 +000027#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000028#include <math.h> // HUGE_VAL.
Jim Cownie5e8470a2013-09-27 10:38:44 +000029#include <sys/resource.h>
30#include <sys/syscall.h>
Jonathan Peyton30419822017-05-12 18:01:32 +000031#include <sys/time.h>
32#include <sys/times.h>
33#include <unistd.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000034
Jim Cownie3051f972014-08-07 10:12:54 +000035#if KMP_OS_LINUX && !KMP_OS_CNK
Jonathan Peyton30419822017-05-12 18:01:32 +000036#include <sys/sysinfo.h>
37#if KMP_USE_FUTEX
38// We should really include <futex.h>, but that causes compatibility problems on
39// different Linux* OS distributions that either require that you include (or
40// break when you try to include) <pci/types.h>. Since all we need is the two
41// macros below (which are part of the kernel ABI, so can't change) we just
42// define the constants here and don't include <futex.h>
43#ifndef FUTEX_WAIT
44#define FUTEX_WAIT 0
45#endif
46#ifndef FUTEX_WAKE
47#define FUTEX_WAKE 1
48#endif
49#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000050#elif KMP_OS_DARWIN
Jonathan Peyton30419822017-05-12 18:01:32 +000051#include <mach/mach.h>
52#include <sys/sysctl.h>
Kamil Rytarowskia56ac942018-12-09 16:40:33 +000053#elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD
Jonathan Peyton30419822017-05-12 18:01:32 +000054#include <pthread_np.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000055#endif
56
Jim Cownie5e8470a2013-09-27 10:38:44 +000057#include <ctype.h>
Jonathan Peyton30419822017-05-12 18:01:32 +000058#include <dirent.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000059#include <fcntl.h>
60
Jonas Hahnfeld50fed042016-11-07 15:58:36 +000061#include "tsan_annotations.h"
62
Jim Cownie5e8470a2013-09-27 10:38:44 +000063struct kmp_sys_timer {
Jonathan Peyton30419822017-05-12 18:01:32 +000064 struct timespec start;
Jim Cownie5e8470a2013-09-27 10:38:44 +000065};
66
67// Convert timespec to nanoseconds.
68#define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec)
69
70static struct kmp_sys_timer __kmp_sys_timer_data;
71
72#if KMP_HANDLE_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +000073typedef void (*sig_func_t)(int);
74STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[NSIG];
75static sigset_t __kmp_sigset;
Jim Cownie5e8470a2013-09-27 10:38:44 +000076#endif
77
Jonathan Peyton30419822017-05-12 18:01:32 +000078static int __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +000079
80static int __kmp_fork_count = 0;
81
Jonathan Peyton30419822017-05-12 18:01:32 +000082static pthread_condattr_t __kmp_suspend_cond_attr;
Jim Cownie5e8470a2013-09-27 10:38:44 +000083static pthread_mutexattr_t __kmp_suspend_mutex_attr;
84
Jonathan Peyton30419822017-05-12 18:01:32 +000085static kmp_cond_align_t __kmp_wait_cv;
86static kmp_mutex_align_t __kmp_wait_mx;
Jim Cownie5e8470a2013-09-27 10:38:44 +000087
Jonathan Peyton35d75ae2017-03-20 22:11:31 +000088kmp_uint64 __kmp_ticks_per_msec = 1000000;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +000089
Jim Cownie5e8470a2013-09-27 10:38:44 +000090#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +000091static void __kmp_print_cond(char *buffer, kmp_cond_align_t *cond) {
92 KMP_SNPRINTF(buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))",
93 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
94 cond->c_cond.__c_waiting);
Jim Cownie5e8470a2013-09-27 10:38:44 +000095}
96#endif
97
Jonathan Peyton30419822017-05-12 18:01:32 +000098#if (KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
Jim Cownie5e8470a2013-09-27 10:38:44 +000099
Jonathan Peyton30419822017-05-12 18:01:32 +0000100/* Affinity support */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000101
Jonathan Peyton30419822017-05-12 18:01:32 +0000102void __kmp_affinity_bind_thread(int which) {
103 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
104 "Illegal set affinity operation when not capable");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000105
Jonathan Peyton30419822017-05-12 18:01:32 +0000106 kmp_affin_mask_t *mask;
107 KMP_CPU_ALLOC_ON_STACK(mask);
108 KMP_CPU_ZERO(mask);
109 KMP_CPU_SET(which, mask);
110 __kmp_set_system_affinity(mask, TRUE);
111 KMP_CPU_FREE_FROM_STACK(mask);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000112}
113
Jonathan Peyton30419822017-05-12 18:01:32 +0000114/* Determine if we can access affinity functionality on this version of
Jim Cownie5e8470a2013-09-27 10:38:44 +0000115 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
Jonathan Peyton30419822017-05-12 18:01:32 +0000116 * __kmp_affin_mask_size to the appropriate value (0 means not capable). */
117void __kmp_affinity_determine_capable(const char *env_var) {
118// Check and see if the OS supports thread affinity.
119
120#define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024)
121
122 int gCode;
123 int sCode;
124 unsigned char *buf;
125 buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
126
127 // If Linux* OS:
128 // If the syscall fails or returns a suggestion for the size,
129 // then we don't have to search for an appropriate size.
130 gCode = syscall(__NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf);
131 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
132 "initial getaffinity call returned %d errno = %d\n",
133 gCode, errno));
134
135 // if ((gCode < 0) && (errno == ENOSYS))
136 if (gCode < 0) {
137 // System call not supported
138 if (__kmp_affinity_verbose ||
139 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
140 (__kmp_affinity_type != affinity_default) &&
141 (__kmp_affinity_type != affinity_disabled))) {
142 int error = errno;
143 kmp_msg_t err_code = KMP_ERR(error);
144 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
145 err_code, __kmp_msg_null);
146 if (__kmp_generate_warnings == kmp_warnings_off) {
147 __kmp_str_free(&err_code.str);
148 }
149 }
150 KMP_AFFINITY_DISABLE();
151 KMP_INTERNAL_FREE(buf);
152 return;
153 }
154 if (gCode > 0) { // Linux* OS only
155 // The optimal situation: the OS returns the size of the buffer it expects.
Jim Cownie5e8470a2013-09-27 10:38:44 +0000156 //
Jonathan Peyton30419822017-05-12 18:01:32 +0000157 // A verification of correct behavior is that Isetaffinity on a NULL
158 // buffer with the same size fails with errno set to EFAULT.
159 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL);
160 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
161 "setaffinity for mask size %d returned %d errno = %d\n",
162 gCode, sCode, errno));
163 if (sCode < 0) {
164 if (errno == ENOSYS) {
165 if (__kmp_affinity_verbose ||
166 (__kmp_affinity_warnings &&
167 (__kmp_affinity_type != affinity_none) &&
168 (__kmp_affinity_type != affinity_default) &&
169 (__kmp_affinity_type != affinity_disabled))) {
170 int error = errno;
171 kmp_msg_t err_code = KMP_ERR(error);
172 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var),
173 err_code, __kmp_msg_null);
174 if (__kmp_generate_warnings == kmp_warnings_off) {
175 __kmp_str_free(&err_code.str);
176 }
177 }
178 KMP_AFFINITY_DISABLE();
179 KMP_INTERNAL_FREE(buf);
180 }
181 if (errno == EFAULT) {
182 KMP_AFFINITY_ENABLE(gCode);
183 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
184 "affinity supported (mask size %d)\n",
185 (int)__kmp_affin_mask_size));
186 KMP_INTERNAL_FREE(buf);
187 return;
188 }
189 }
190 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000191
Jonathan Peyton30419822017-05-12 18:01:32 +0000192 // Call the getaffinity system call repeatedly with increasing set sizes
193 // until we succeed, or reach an upper bound on the search.
194 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
195 "searching for proper set size\n"));
196 int size;
197 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
198 gCode = syscall(__NR_sched_getaffinity, 0, size, buf);
199 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
200 "getaffinity for mask size %d returned %d errno = %d\n",
201 size, gCode, errno));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000202
Jim Cownie5e8470a2013-09-27 10:38:44 +0000203 if (gCode < 0) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000204 if (errno == ENOSYS) {
205 // We shouldn't get here
206 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
207 "inconsistent OS call behavior: errno == ENOSYS for mask "
208 "size %d\n",
209 size));
210 if (__kmp_affinity_verbose ||
211 (__kmp_affinity_warnings &&
212 (__kmp_affinity_type != affinity_none) &&
213 (__kmp_affinity_type != affinity_default) &&
214 (__kmp_affinity_type != affinity_disabled))) {
215 int error = errno;
216 kmp_msg_t err_code = KMP_ERR(error);
217 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
218 err_code, __kmp_msg_null);
219 if (__kmp_generate_warnings == kmp_warnings_off) {
220 __kmp_str_free(&err_code.str);
221 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000222 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000223 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000224 KMP_INTERNAL_FREE(buf);
225 return;
Jonathan Peyton30419822017-05-12 18:01:32 +0000226 }
227 continue;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000228 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000229
230 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL);
231 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
232 "setaffinity for mask size %d returned %d errno = %d\n",
233 gCode, sCode, errno));
234 if (sCode < 0) {
235 if (errno == ENOSYS) { // Linux* OS only
236 // We shouldn't get here
237 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
238 "inconsistent OS call behavior: errno == ENOSYS for mask "
239 "size %d\n",
240 size));
241 if (__kmp_affinity_verbose ||
242 (__kmp_affinity_warnings &&
243 (__kmp_affinity_type != affinity_none) &&
244 (__kmp_affinity_type != affinity_default) &&
245 (__kmp_affinity_type != affinity_disabled))) {
246 int error = errno;
247 kmp_msg_t err_code = KMP_ERR(error);
248 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var),
249 err_code, __kmp_msg_null);
250 if (__kmp_generate_warnings == kmp_warnings_off) {
251 __kmp_str_free(&err_code.str);
252 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000253 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000254 KMP_AFFINITY_DISABLE();
255 KMP_INTERNAL_FREE(buf);
256 return;
257 }
258 if (errno == EFAULT) {
259 KMP_AFFINITY_ENABLE(gCode);
260 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
261 "affinity supported (mask size %d)\n",
262 (int)__kmp_affin_mask_size));
263 KMP_INTERNAL_FREE(buf);
264 return;
265 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000266 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000267 }
268 // save uncaught error code
269 // int error = errno;
270 KMP_INTERNAL_FREE(buf);
271 // restore uncaught error code, will be printed at the next KMP_WARNING below
272 // errno = error;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000273
Jonathan Peyton30419822017-05-12 18:01:32 +0000274 // Affinity is not supported
275 KMP_AFFINITY_DISABLE();
276 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
277 "cannot determine mask size - affinity not supported\n"));
278 if (__kmp_affinity_verbose ||
279 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
280 (__kmp_affinity_type != affinity_default) &&
281 (__kmp_affinity_type != affinity_disabled))) {
282 KMP_WARNING(AffCantGetMaskSize, env_var);
283 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000284}
285
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000286#endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000287
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000288#if KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000289
Jonathan Peyton30419822017-05-12 18:01:32 +0000290int __kmp_futex_determine_capable() {
291 int loc = 0;
292 int rc = syscall(__NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0);
293 int retval = (rc == 0) || (errno != ENOSYS);
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000294
Jonathan Peyton30419822017-05-12 18:01:32 +0000295 KA_TRACE(10,
296 ("__kmp_futex_determine_capable: rc = %d errno = %d\n", rc, errno));
297 KA_TRACE(10, ("__kmp_futex_determine_capable: futex syscall%s supported\n",
298 retval ? "" : " not"));
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000299
Jonathan Peyton30419822017-05-12 18:01:32 +0000300 return retval;
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000301}
302
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000303#endif // KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000304
Jonathan Peyton30419822017-05-12 18:01:32 +0000305#if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (!KMP_ASM_INTRINS)
306/* Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
307 use compare_and_store for these routines */
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000308
Jonathan Peyton30419822017-05-12 18:01:32 +0000309kmp_int8 __kmp_test_then_or8(volatile kmp_int8 *p, kmp_int8 d) {
310 kmp_int8 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000311
Jonathan Peyton30419822017-05-12 18:01:32 +0000312 old_value = TCR_1(*p);
313 new_value = old_value | d;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000314
Jonathan Peyton30419822017-05-12 18:01:32 +0000315 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
316 KMP_CPU_PAUSE();
317 old_value = TCR_1(*p);
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000318 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000319 }
320 return old_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000321}
322
Jonathan Peyton30419822017-05-12 18:01:32 +0000323kmp_int8 __kmp_test_then_and8(volatile kmp_int8 *p, kmp_int8 d) {
324 kmp_int8 old_value, new_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000325
Jonathan Peyton30419822017-05-12 18:01:32 +0000326 old_value = TCR_1(*p);
327 new_value = old_value & d;
328
329 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
330 KMP_CPU_PAUSE();
331 old_value = TCR_1(*p);
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000332 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000333 }
334 return old_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000335}
336
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000337kmp_uint32 __kmp_test_then_or32(volatile kmp_uint32 *p, kmp_uint32 d) {
338 kmp_uint32 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000339
Jonathan Peyton30419822017-05-12 18:01:32 +0000340 old_value = TCR_4(*p);
341 new_value = old_value | d;
342
343 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
344 KMP_CPU_PAUSE();
345 old_value = TCR_4(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000346 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000347 }
348 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000349}
350
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000351kmp_uint32 __kmp_test_then_and32(volatile kmp_uint32 *p, kmp_uint32 d) {
352 kmp_uint32 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000353
Jonathan Peyton30419822017-05-12 18:01:32 +0000354 old_value = TCR_4(*p);
355 new_value = old_value & d;
356
357 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
358 KMP_CPU_PAUSE();
359 old_value = TCR_4(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000360 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000361 }
362 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000363}
364
Jonathan Peyton30419822017-05-12 18:01:32 +0000365#if KMP_ARCH_X86
366kmp_int8 __kmp_test_then_add8(volatile kmp_int8 *p, kmp_int8 d) {
367 kmp_int8 old_value, new_value;
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000368
Jonathan Peyton30419822017-05-12 18:01:32 +0000369 old_value = TCR_1(*p);
370 new_value = old_value + d;
371
372 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
373 KMP_CPU_PAUSE();
374 old_value = TCR_1(*p);
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000375 new_value = old_value + d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000376 }
377 return old_value;
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000378}
379
Jonathan Peyton30419822017-05-12 18:01:32 +0000380kmp_int64 __kmp_test_then_add64(volatile kmp_int64 *p, kmp_int64 d) {
381 kmp_int64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000382
Jonathan Peyton30419822017-05-12 18:01:32 +0000383 old_value = TCR_8(*p);
384 new_value = old_value + d;
385
386 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
387 KMP_CPU_PAUSE();
388 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000389 new_value = old_value + d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000390 }
391 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000392}
Jonathan Peyton30419822017-05-12 18:01:32 +0000393#endif /* KMP_ARCH_X86 */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000394
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000395kmp_uint64 __kmp_test_then_or64(volatile kmp_uint64 *p, kmp_uint64 d) {
396 kmp_uint64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000397
Jonathan Peyton30419822017-05-12 18:01:32 +0000398 old_value = TCR_8(*p);
399 new_value = old_value | d;
400 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
401 KMP_CPU_PAUSE();
402 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000403 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000404 }
405 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000406}
407
Andrey Churbanov5ba90c72017-07-17 09:03:14 +0000408kmp_uint64 __kmp_test_then_and64(volatile kmp_uint64 *p, kmp_uint64 d) {
409 kmp_uint64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000410
Jonathan Peyton30419822017-05-12 18:01:32 +0000411 old_value = TCR_8(*p);
412 new_value = old_value & d;
413 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
414 KMP_CPU_PAUSE();
415 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000416 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000417 }
418 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000419}
420
421#endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
422
Jonathan Peyton30419822017-05-12 18:01:32 +0000423void __kmp_terminate_thread(int gtid) {
424 int status;
425 kmp_info_t *th = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000426
Jonathan Peyton30419822017-05-12 18:01:32 +0000427 if (!th)
428 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000429
Jonathan Peyton30419822017-05-12 18:01:32 +0000430#ifdef KMP_CANCEL_THREADS
431 KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
432 status = pthread_cancel(th->th.th_info.ds.ds_thread);
433 if (status != 0 && status != ESRCH) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000434 __kmp_fatal(KMP_MSG(CantTerminateWorkerThread), KMP_ERR(status),
435 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000436 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000437#endif
438 __kmp_yield(TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000439} //
440
Jonathan Peyton30419822017-05-12 18:01:32 +0000441/* Set thread stack info according to values returned by pthread_getattr_np().
442 If values are unreasonable, assume call failed and use incremental stack
443 refinement method instead. Returns TRUE if the stack parameters could be
444 determined exactly, FALSE if incremental refinement is necessary. */
445static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) {
446 int stack_data;
Kamil Rytarowskia56ac942018-12-09 16:40:33 +0000447#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
448 KMP_OS_HURD
Jonathan Peyton30419822017-05-12 18:01:32 +0000449 pthread_attr_t attr;
450 int status;
451 size_t size = 0;
452 void *addr = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000453
Jonathan Peyton30419822017-05-12 18:01:32 +0000454 /* Always do incremental stack refinement for ubermaster threads since the
455 initial thread stack range can be reduced by sibling thread creation so
456 pthread_attr_getstack may cause thread gtid aliasing */
457 if (!KMP_UBER_GTID(gtid)) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000458
Jonathan Peyton30419822017-05-12 18:01:32 +0000459 /* Fetch the real thread attributes */
460 status = pthread_attr_init(&attr);
461 KMP_CHECK_SYSFAIL("pthread_attr_init", status);
Kamil Rytarowskia56ac942018-12-09 16:40:33 +0000462#if KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000463 status = pthread_attr_get_np(pthread_self(), &attr);
464 KMP_CHECK_SYSFAIL("pthread_attr_get_np", status);
Alp Toker763b9392014-02-28 09:42:41 +0000465#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000466 status = pthread_getattr_np(pthread_self(), &attr);
467 KMP_CHECK_SYSFAIL("pthread_getattr_np", status);
Alp Toker763b9392014-02-28 09:42:41 +0000468#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000469 status = pthread_attr_getstack(&attr, &addr, &size);
470 KMP_CHECK_SYSFAIL("pthread_attr_getstack", status);
471 KA_TRACE(60,
472 ("__kmp_set_stack_info: T#%d pthread_attr_getstack returned size:"
473 " %lu, low addr: %p\n",
474 gtid, size, addr));
475 status = pthread_attr_destroy(&attr);
476 KMP_CHECK_SYSFAIL("pthread_attr_destroy", status);
477 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000478
Jonathan Peyton30419822017-05-12 18:01:32 +0000479 if (size != 0 && addr != 0) { // was stack parameter determination successful?
480 /* Store the correct base and size */
481 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size));
482 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
483 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
484 return TRUE;
485 }
Kamil Rytarowskia56ac942018-12-09 16:40:33 +0000486#endif /* KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD ||
487 KMP_OS_HURD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000488 /* Use incremental refinement starting from initial conservative estimate */
489 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
490 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
491 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
492 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000493}
494
Jonathan Peyton30419822017-05-12 18:01:32 +0000495static void *__kmp_launch_worker(void *thr) {
496 int status, old_type, old_state;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000497#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000498 sigset_t new_set, old_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000499#endif /* KMP_BLOCK_SIGNALS */
Jonathan Peyton30419822017-05-12 18:01:32 +0000500 void *exit_val;
Kamil Rytarowskia56ac942018-12-09 16:40:33 +0000501#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +0000502 KMP_OS_OPENBSD || KMP_OS_HURD
Jonathan Peyton30419822017-05-12 18:01:32 +0000503 void *volatile padding = 0;
Jonathan Peyton2321d572015-06-08 19:25:25 +0000504#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000505 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000506
Jonathan Peyton30419822017-05-12 18:01:32 +0000507 gtid = ((kmp_info_t *)thr)->th.th_info.ds.ds_gtid;
508 __kmp_gtid_set_specific(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000509#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000510 __kmp_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000511#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000512#if KMP_STATS_ENABLED
Jonathan Peyton40039ac2017-11-07 23:32:13 +0000513 // set thread local index to point to thread-specific stats
Jonathan Peyton30419822017-05-12 18:01:32 +0000514 __kmp_stats_thread_ptr = ((kmp_info_t *)thr)->th.th_stats;
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000515 __kmp_stats_thread_ptr->startLife();
Jonathan Peyton30419822017-05-12 18:01:32 +0000516 KMP_SET_THREAD_STATE(IDLE);
517 KMP_INIT_PARTITIONED_TIMERS(OMP_idle);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000518#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000519
520#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000521 __kmp_itt_thread_name(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000522#endif /* USE_ITT_BUILD */
523
Alp Toker763b9392014-02-28 09:42:41 +0000524#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +0000525 __kmp_affinity_set_init_mask(gtid, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000526#endif
527
528#ifdef KMP_CANCEL_THREADS
Jonathan Peyton30419822017-05-12 18:01:32 +0000529 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
530 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
531 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
532 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
533 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000534#endif
535
536#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000537 // Set FP control regs to be a copy of the parallel initialization thread's.
538 __kmp_clear_x87_fpu_status_word();
539 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
540 __kmp_load_mxcsr(&__kmp_init_mxcsr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000541#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
542
543#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000544 status = sigfillset(&new_set);
545 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
546 status = pthread_sigmask(SIG_BLOCK, &new_set, &old_set);
547 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000548#endif /* KMP_BLOCK_SIGNALS */
549
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +0000550#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
551 KMP_OS_OPENBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000552 if (__kmp_stkoffset > 0 && gtid > 0) {
553 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
554 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000555#endif
556
Jonathan Peyton30419822017-05-12 18:01:32 +0000557 KMP_MB();
558 __kmp_set_stack_info(gtid, (kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000559
Jonathan Peyton30419822017-05-12 18:01:32 +0000560 __kmp_check_stack_overlap((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000561
Jonathan Peyton30419822017-05-12 18:01:32 +0000562 exit_val = __kmp_launch_thread((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000563
564#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000565 status = pthread_sigmask(SIG_SETMASK, &old_set, NULL);
566 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000567#endif /* KMP_BLOCK_SIGNALS */
568
Jonathan Peyton30419822017-05-12 18:01:32 +0000569 return exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000570}
571
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000572#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000573/* The monitor thread controls all of the threads in the complex */
574
Jonathan Peyton30419822017-05-12 18:01:32 +0000575static void *__kmp_launch_monitor(void *thr) {
576 int status, old_type, old_state;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000577#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000578 sigset_t new_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000579#endif /* KMP_BLOCK_SIGNALS */
Jonathan Peyton30419822017-05-12 18:01:32 +0000580 struct timespec interval;
581 int yield_count;
582 int yield_cycles = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000583
Jonathan Peyton30419822017-05-12 18:01:32 +0000584 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000585
Jonathan Peyton30419822017-05-12 18:01:32 +0000586 KA_TRACE(10, ("__kmp_launch_monitor: #1 launched\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000587
Jonathan Peyton30419822017-05-12 18:01:32 +0000588 /* register us as the monitor thread */
589 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000590#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000591 __kmp_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000592#endif
593
Jonathan Peyton30419822017-05-12 18:01:32 +0000594 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000595
596#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000597 // Instruct Intel(R) Threading Tools to ignore monitor thread.
598 __kmp_itt_thread_ignore();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000599#endif /* USE_ITT_BUILD */
600
Jonathan Peyton30419822017-05-12 18:01:32 +0000601 __kmp_set_stack_info(((kmp_info_t *)thr)->th.th_info.ds.ds_gtid,
602 (kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000603
Jonathan Peyton30419822017-05-12 18:01:32 +0000604 __kmp_check_stack_overlap((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000605
606#ifdef KMP_CANCEL_THREADS
Jonathan Peyton30419822017-05-12 18:01:32 +0000607 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
608 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
609 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
610 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
611 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000612#endif
613
Jonathan Peyton30419822017-05-12 18:01:32 +0000614#if KMP_REAL_TIME_FIX
615 // This is a potential fix which allows application with real-time scheduling
616 // policy work. However, decision about the fix is not made yet, so it is
617 // disabled by default.
618 { // Are program started with real-time scheduling policy?
619 int sched = sched_getscheduler(0);
620 if (sched == SCHED_FIFO || sched == SCHED_RR) {
621 // Yes, we are a part of real-time application. Try to increase the
622 // priority of the monitor.
623 struct sched_param param;
624 int max_priority = sched_get_priority_max(sched);
625 int rc;
626 KMP_WARNING(RealTimeSchedNotSupported);
627 sched_getparam(0, &param);
628 if (param.sched_priority < max_priority) {
629 param.sched_priority += 1;
630 rc = sched_setscheduler(0, sched, &param);
631 if (rc != 0) {
632 int error = errno;
633 kmp_msg_t err_code = KMP_ERR(error);
634 __kmp_msg(kmp_ms_warning, KMP_MSG(CantChangeMonitorPriority),
635 err_code, KMP_MSG(MonitorWillStarve), __kmp_msg_null);
636 if (__kmp_generate_warnings == kmp_warnings_off) {
637 __kmp_str_free(&err_code.str);
638 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000639 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000640 } else {
641 // We cannot abort here, because number of CPUs may be enough for all
642 // the threads, including the monitor thread, so application could
643 // potentially work...
644 __kmp_msg(kmp_ms_warning, KMP_MSG(RunningAtMaxPriority),
645 KMP_MSG(MonitorWillStarve), KMP_HNT(RunningAtMaxPriority),
646 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000647 }
648 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000649 // AC: free thread that waits for monitor started
650 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
651 }
652#endif // KMP_REAL_TIME_FIX
Jim Cownie5e8470a2013-09-27 10:38:44 +0000653
Jonathan Peyton30419822017-05-12 18:01:32 +0000654 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000655
Jonathan Peyton30419822017-05-12 18:01:32 +0000656 if (__kmp_monitor_wakeups == 1) {
657 interval.tv_sec = 1;
658 interval.tv_nsec = 0;
659 } else {
660 interval.tv_sec = 0;
661 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
662 }
663
664 KA_TRACE(10, ("__kmp_launch_monitor: #2 monitor\n"));
665
666 if (__kmp_yield_cycle) {
667 __kmp_yielding_on = 0; /* Start out with yielding shut off */
668 yield_count = __kmp_yield_off_count;
669 } else {
670 __kmp_yielding_on = 1; /* Yielding is on permanently */
671 }
672
673 while (!TCR_4(__kmp_global.g.g_done)) {
674 struct timespec now;
675 struct timeval tval;
676
677 /* This thread monitors the state of the system */
678
679 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
680
681 status = gettimeofday(&tval, NULL);
682 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
683 TIMEVAL_TO_TIMESPEC(&tval, &now);
684
685 now.tv_sec += interval.tv_sec;
686 now.tv_nsec += interval.tv_nsec;
687
688 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
689 now.tv_sec += 1;
690 now.tv_nsec -= KMP_NSEC_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000691 }
692
Jonathan Peyton30419822017-05-12 18:01:32 +0000693 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
694 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
695 // AC: the monitor should not fall asleep if g_done has been set
696 if (!TCR_4(__kmp_global.g.g_done)) { // check once more under mutex
697 status = pthread_cond_timedwait(&__kmp_wait_cv.c_cond,
698 &__kmp_wait_mx.m_mutex, &now);
699 if (status != 0) {
700 if (status != ETIMEDOUT && status != EINTR) {
701 KMP_SYSFAIL("pthread_cond_timedwait", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000702 }
703 }
704 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000705 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
706 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000707
708 if (__kmp_yield_cycle) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000709 yield_cycles++;
710 if ((yield_cycles % yield_count) == 0) {
711 if (__kmp_yielding_on) {
712 __kmp_yielding_on = 0; /* Turn it off now */
713 yield_count = __kmp_yield_off_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000714 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +0000715 __kmp_yielding_on = 1; /* Turn it on now */
716 yield_count = __kmp_yield_on_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000717 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000718 yield_cycles = 0;
719 }
720 } else {
721 __kmp_yielding_on = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000722 }
723
Jonathan Peyton30419822017-05-12 18:01:32 +0000724 TCW_4(__kmp_global.g.g_time.dt.t_value,
725 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
726
727 KMP_MB(); /* Flush all pending memory write invalidates. */
728 }
729
730 KA_TRACE(10, ("__kmp_launch_monitor: #3 cleanup\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000731
732#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000733 status = sigfillset(&new_set);
734 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
735 status = pthread_sigmask(SIG_UNBLOCK, &new_set, NULL);
736 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000737#endif /* KMP_BLOCK_SIGNALS */
738
Jonathan Peyton30419822017-05-12 18:01:32 +0000739 KA_TRACE(10, ("__kmp_launch_monitor: #4 finished\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000740
Jonathan Peyton30419822017-05-12 18:01:32 +0000741 if (__kmp_global.g.g_abort != 0) {
742 /* now we need to terminate the worker threads */
743 /* the value of t_abort is the signal we caught */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000744
Jonathan Peyton30419822017-05-12 18:01:32 +0000745 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000746
Jonathan Peyton30419822017-05-12 18:01:32 +0000747 KA_TRACE(10, ("__kmp_launch_monitor: #5 terminate sig=%d\n",
748 __kmp_global.g.g_abort));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000749
Jonathan Peyton30419822017-05-12 18:01:32 +0000750 /* terminate the OpenMP worker threads */
751 /* TODO this is not valid for sibling threads!!
752 * the uber master might not be 0 anymore.. */
753 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
754 __kmp_terminate_thread(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000755
Jonathan Peyton30419822017-05-12 18:01:32 +0000756 __kmp_cleanup();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000757
Jonathan Peyton30419822017-05-12 18:01:32 +0000758 KA_TRACE(10, ("__kmp_launch_monitor: #6 raise sig=%d\n",
759 __kmp_global.g.g_abort));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000760
Jonathan Peyton30419822017-05-12 18:01:32 +0000761 if (__kmp_global.g.g_abort > 0)
762 raise(__kmp_global.g.g_abort);
763 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000764
Jonathan Peyton30419822017-05-12 18:01:32 +0000765 KA_TRACE(10, ("__kmp_launch_monitor: #7 exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000766
Jonathan Peyton30419822017-05-12 18:01:32 +0000767 return thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000768}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000769#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000770
Jonathan Peyton30419822017-05-12 18:01:32 +0000771void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
772 pthread_t handle;
773 pthread_attr_t thread_attr;
774 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000775
Jonathan Peyton30419822017-05-12 18:01:32 +0000776 th->th.th_info.ds.ds_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000777
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000778#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000779 // sets up worker thread stats
780 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000781
Jonathan Peyton30419822017-05-12 18:01:32 +0000782 // th->th.th_stats is used to transfer thread-specific stats-pointer to
783 // __kmp_launch_worker. So when thread is created (goes into
Jonathan Peyton40039ac2017-11-07 23:32:13 +0000784 // __kmp_launch_worker) it will set its thread local pointer to
Jonathan Peyton30419822017-05-12 18:01:32 +0000785 // th->th.th_stats
786 if (!KMP_UBER_GTID(gtid)) {
787 th->th.th_stats = __kmp_stats_list->push_back(gtid);
788 } else {
789 // For root threads, __kmp_stats_thread_ptr is set in __kmp_register_root(),
790 // so set the th->th.th_stats field to it.
791 th->th.th_stats = __kmp_stats_thread_ptr;
792 }
793 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000794
795#endif // KMP_STATS_ENABLED
796
Jonathan Peyton30419822017-05-12 18:01:32 +0000797 if (KMP_UBER_GTID(gtid)) {
798 KA_TRACE(10, ("__kmp_create_worker: uber thread (%d)\n", gtid));
799 th->th.th_info.ds.ds_thread = pthread_self();
800 __kmp_set_stack_info(gtid, th);
801 __kmp_check_stack_overlap(th);
802 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000803 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000804
Jonathan Peyton30419822017-05-12 18:01:32 +0000805 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000806
Jonathan Peyton30419822017-05-12 18:01:32 +0000807 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000808
809#ifdef KMP_THREAD_ATTR
Jonathan Peyton30419822017-05-12 18:01:32 +0000810 status = pthread_attr_init(&thread_attr);
811 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000812 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000813 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000814 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
815 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000816 __kmp_fatal(KMP_MSG(CantSetWorkerState), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000817 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000818
Jonathan Peyton30419822017-05-12 18:01:32 +0000819 /* Set stack size for this thread now.
820 The multiple of 2 is there because on some machines, requesting an unusual
821 stacksize causes the thread to have an offset before the dummy alloca()
822 takes place to create the offset. Since we want the user to have a
823 sufficient stacksize AND support a stack offset, we alloca() twice the
824 offset so that the upcoming alloca() does not eliminate any premade offset,
825 and also gives the user the stack space they requested for all threads */
826 stack_size += gtid * __kmp_stkoffset * 2;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000827
Jonathan Peyton30419822017-05-12 18:01:32 +0000828 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
829 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
830 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000831
Jim Cownie5e8470a2013-09-27 10:38:44 +0000832#ifdef _POSIX_THREAD_ATTR_STACKSIZE
Jonathan Peyton30419822017-05-12 18:01:32 +0000833 status = pthread_attr_setstacksize(&thread_attr, stack_size);
834#ifdef KMP_BACKUP_STKSIZE
835 if (status != 0) {
836 if (!__kmp_env_stksize) {
837 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
838 __kmp_stksize = KMP_BACKUP_STKSIZE;
839 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
840 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
841 "bytes\n",
842 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
843 status = pthread_attr_setstacksize(&thread_attr, stack_size);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000844 }
845 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000846#endif /* KMP_BACKUP_STKSIZE */
847 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000848 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
849 KMP_HNT(ChangeWorkerStackSize), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000850 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000851#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000852
Jim Cownie5e8470a2013-09-27 10:38:44 +0000853#endif /* KMP_THREAD_ATTR */
854
Jonathan Peyton30419822017-05-12 18:01:32 +0000855 status =
856 pthread_create(&handle, &thread_attr, __kmp_launch_worker, (void *)th);
857 if (status != 0 || !handle) { // ??? Why do we check handle??
858#ifdef _POSIX_THREAD_ATTR_STACKSIZE
859 if (status == EINVAL) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000860 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
861 KMP_HNT(IncreaseWorkerStackSize), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000862 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000863 if (status == ENOMEM) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000864 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
865 KMP_HNT(DecreaseWorkerStackSize), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000866 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000867#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
868 if (status == EAGAIN) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000869 __kmp_fatal(KMP_MSG(NoResourcesForWorkerThread), KMP_ERR(status),
870 KMP_HNT(Decrease_NUM_THREADS), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000871 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000872 KMP_SYSFAIL("pthread_create", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000873 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000874
Jonathan Peyton30419822017-05-12 18:01:32 +0000875 th->th.th_info.ds.ds_thread = handle;
876
877#ifdef KMP_THREAD_ATTR
878 status = pthread_attr_destroy(&thread_attr);
879 if (status) {
880 kmp_msg_t err_code = KMP_ERR(status);
881 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
882 __kmp_msg_null);
883 if (__kmp_generate_warnings == kmp_warnings_off) {
884 __kmp_str_free(&err_code.str);
885 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000886 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000887#endif /* KMP_THREAD_ATTR */
888
889 KMP_MB(); /* Flush all pending memory write invalidates. */
890
891 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000892
893} // __kmp_create_worker
894
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000895#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +0000896void __kmp_create_monitor(kmp_info_t *th) {
897 pthread_t handle;
898 pthread_attr_t thread_attr;
899 size_t size;
900 int status;
901 int auto_adj_size = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000902
Jonathan Peyton30419822017-05-12 18:01:32 +0000903 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
904 // We don't need monitor thread in case of MAX_BLOCKTIME
905 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
906 "MAX blocktime\n"));
907 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
908 th->th.th_info.ds.ds_gtid = 0;
909 return;
910 }
911 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
912
913 KMP_MB(); /* Flush all pending memory write invalidates. */
914
915 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
916 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
917#if KMP_REAL_TIME_FIX
918 TCW_4(__kmp_global.g.g_time.dt.t_value,
919 -1); // Will use it for synchronization a bit later.
920#else
921 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
922#endif // KMP_REAL_TIME_FIX
923
924#ifdef KMP_THREAD_ATTR
925 if (__kmp_monitor_stksize == 0) {
926 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
927 auto_adj_size = TRUE;
928 }
929 status = pthread_attr_init(&thread_attr);
930 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000931 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000932 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000933 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
934 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000935 __kmp_fatal(KMP_MSG(CantSetMonitorState), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000936 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000937
938#ifdef _POSIX_THREAD_ATTR_STACKSIZE
939 status = pthread_attr_getstacksize(&thread_attr, &size);
940 KMP_CHECK_SYSFAIL("pthread_attr_getstacksize", status);
941#else
942 size = __kmp_sys_min_stksize;
943#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
944#endif /* KMP_THREAD_ATTR */
945
946 if (__kmp_monitor_stksize == 0) {
947 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
948 }
949 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
950 __kmp_monitor_stksize = __kmp_sys_min_stksize;
951 }
952
953 KA_TRACE(10, ("__kmp_create_monitor: default stacksize = %lu bytes,"
954 "requested stacksize = %lu bytes\n",
955 size, __kmp_monitor_stksize));
956
957retry:
958
959/* Set stack size for this thread now. */
960#ifdef _POSIX_THREAD_ATTR_STACKSIZE
961 KA_TRACE(10, ("__kmp_create_monitor: setting stacksize = %lu bytes,",
962 __kmp_monitor_stksize));
963 status = pthread_attr_setstacksize(&thread_attr, __kmp_monitor_stksize);
964 if (status != 0) {
965 if (auto_adj_size) {
966 __kmp_monitor_stksize *= 2;
967 goto retry;
Jonathan Peyton4fee5f62015-12-18 23:20:36 +0000968 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000969 kmp_msg_t err_code = KMP_ERR(status);
970 __kmp_msg(kmp_ms_warning, // should this be fatal? BB
971 KMP_MSG(CantSetMonitorStackSize, (long int)__kmp_monitor_stksize),
972 err_code, KMP_HNT(ChangeMonitorStackSize), __kmp_msg_null);
973 if (__kmp_generate_warnings == kmp_warnings_off) {
974 __kmp_str_free(&err_code.str);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000975 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000976 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000977#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000978
Jonathan Peyton30419822017-05-12 18:01:32 +0000979 status =
980 pthread_create(&handle, &thread_attr, __kmp_launch_monitor, (void *)th);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000981
Jonathan Peyton30419822017-05-12 18:01:32 +0000982 if (status != 0) {
983#ifdef _POSIX_THREAD_ATTR_STACKSIZE
984 if (status == EINVAL) {
985 if (auto_adj_size && (__kmp_monitor_stksize < (size_t)0x40000000)) {
986 __kmp_monitor_stksize *= 2;
987 goto retry;
988 }
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000989 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
990 KMP_ERR(status), KMP_HNT(IncreaseMonitorStackSize),
991 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000992 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000993 if (status == ENOMEM) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000994 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
995 KMP_ERR(status), KMP_HNT(DecreaseMonitorStackSize),
996 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000997 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000998#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
999 if (status == EAGAIN) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001000 __kmp_fatal(KMP_MSG(NoResourcesForMonitorThread), KMP_ERR(status),
1001 KMP_HNT(DecreaseNumberOfThreadsInUse), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001002 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001003 KMP_SYSFAIL("pthread_create", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001004 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001005
Jonathan Peyton30419822017-05-12 18:01:32 +00001006 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001007
Jonathan Peyton30419822017-05-12 18:01:32 +00001008#if KMP_REAL_TIME_FIX
1009 // Wait for the monitor thread is really started and set its *priority*.
1010 KMP_DEBUG_ASSERT(sizeof(kmp_uint32) ==
1011 sizeof(__kmp_global.g.g_time.dt.t_value));
1012 __kmp_wait_yield_4((kmp_uint32 volatile *)&__kmp_global.g.g_time.dt.t_value,
1013 -1, &__kmp_neq_4, NULL);
1014#endif // KMP_REAL_TIME_FIX
Jim Cownie5e8470a2013-09-27 10:38:44 +00001015
Jonathan Peyton30419822017-05-12 18:01:32 +00001016#ifdef KMP_THREAD_ATTR
1017 status = pthread_attr_destroy(&thread_attr);
1018 if (status != 0) {
1019 kmp_msg_t err_code = KMP_ERR(status);
1020 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
1021 __kmp_msg_null);
1022 if (__kmp_generate_warnings == kmp_warnings_off) {
1023 __kmp_str_free(&err_code.str);
1024 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001025 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001026#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001027
Jonathan Peyton30419822017-05-12 18:01:32 +00001028 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001029
Jonathan Peyton30419822017-05-12 18:01:32 +00001030 KA_TRACE(10, ("__kmp_create_monitor: monitor created %#.8lx\n",
1031 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001032
1033} // __kmp_create_monitor
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001034#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001035
Jonathan Peyton30419822017-05-12 18:01:32 +00001036void __kmp_exit_thread(int exit_status) {
1037 pthread_exit((void *)(intptr_t)exit_status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001038} // __kmp_exit_thread
1039
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001040#if KMP_USE_MONITOR
Jim Cownie07ea89f2014-09-03 11:10:54 +00001041void __kmp_resume_monitor();
1042
Jonathan Peyton30419822017-05-12 18:01:32 +00001043void __kmp_reap_monitor(kmp_info_t *th) {
1044 int status;
1045 void *exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001046
Jonathan Peyton30419822017-05-12 18:01:32 +00001047 KA_TRACE(10, ("__kmp_reap_monitor: try to reap monitor thread with handle"
1048 " %#.8lx\n",
1049 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001050
Jonathan Peyton30419822017-05-12 18:01:32 +00001051 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1052 // If both tid and gtid are 0, it means the monitor did not ever start.
1053 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1054 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1055 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1056 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1057 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001058 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001059
Jonathan Peyton30419822017-05-12 18:01:32 +00001060 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001061
Jonathan Peyton30419822017-05-12 18:01:32 +00001062 /* First, check to see whether the monitor thread exists to wake it up. This
1063 is to avoid performance problem when the monitor sleeps during
1064 blocktime-size interval */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001065
Jonathan Peyton30419822017-05-12 18:01:32 +00001066 status = pthread_kill(th->th.th_info.ds.ds_thread, 0);
1067 if (status != ESRCH) {
1068 __kmp_resume_monitor(); // Wake up the monitor thread
1069 }
1070 KA_TRACE(10, ("__kmp_reap_monitor: try to join with monitor\n"));
1071 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
1072 if (exit_val != th) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001073 __kmp_fatal(KMP_MSG(ReapMonitorError), KMP_ERR(status), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001074 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001075
Jonathan Peyton30419822017-05-12 18:01:32 +00001076 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1077 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001078
Jonathan Peyton30419822017-05-12 18:01:32 +00001079 KA_TRACE(10, ("__kmp_reap_monitor: done reaping monitor thread with handle"
1080 " %#.8lx\n",
1081 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001082
Jonathan Peyton30419822017-05-12 18:01:32 +00001083 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001084}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001085#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001086
Jonathan Peyton30419822017-05-12 18:01:32 +00001087void __kmp_reap_worker(kmp_info_t *th) {
1088 int status;
1089 void *exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001090
Jonathan Peyton30419822017-05-12 18:01:32 +00001091 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001092
Jonathan Peyton30419822017-05-12 18:01:32 +00001093 KA_TRACE(
1094 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001095
Jonathan Peyton30419822017-05-12 18:01:32 +00001096 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001097#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001098 /* Don't expose these to the user until we understand when they trigger */
1099 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001100 __kmp_fatal(KMP_MSG(ReapWorkerError), KMP_ERR(status), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001101 }
1102 if (exit_val != th) {
1103 KA_TRACE(10, ("__kmp_reap_worker: worker T#%d did not reap properly, "
1104 "exit_val = %p\n",
1105 th->th.th_info.ds.ds_gtid, exit_val));
1106 }
Jonathan Peyton93495de2016-06-13 17:36:40 +00001107#endif /* KMP_DEBUG */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001108
Jonathan Peyton30419822017-05-12 18:01:32 +00001109 KA_TRACE(10, ("__kmp_reap_worker: done reaping T#%d\n",
1110 th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001111
Jonathan Peyton30419822017-05-12 18:01:32 +00001112 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001113}
1114
Jim Cownie5e8470a2013-09-27 10:38:44 +00001115#if KMP_HANDLE_SIGNALS
1116
Jonathan Peyton30419822017-05-12 18:01:32 +00001117static void __kmp_null_handler(int signo) {
1118 // Do nothing, for doing SIG_IGN-type actions.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001119} // __kmp_null_handler
1120
Jonathan Peyton30419822017-05-12 18:01:32 +00001121static void __kmp_team_handler(int signo) {
1122 if (__kmp_global.g.g_abort == 0) {
1123/* Stage 1 signal handler, let's shut down all of the threads */
1124#ifdef KMP_DEBUG
1125 __kmp_debug_printf("__kmp_team_handler: caught signal = %d\n", signo);
1126#endif
1127 switch (signo) {
1128 case SIGHUP:
1129 case SIGINT:
1130 case SIGQUIT:
1131 case SIGILL:
1132 case SIGABRT:
1133 case SIGFPE:
1134 case SIGBUS:
1135 case SIGSEGV:
1136#ifdef SIGSYS
1137 case SIGSYS:
1138#endif
1139 case SIGTERM:
1140 if (__kmp_debug_buf) {
1141 __kmp_dump_debug_buffer();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001142 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001143 KMP_MB(); // Flush all pending memory write invalidates.
1144 TCW_4(__kmp_global.g.g_abort, signo);
1145 KMP_MB(); // Flush all pending memory write invalidates.
1146 TCW_4(__kmp_global.g.g_done, TRUE);
1147 KMP_MB(); // Flush all pending memory write invalidates.
1148 break;
1149 default:
1150#ifdef KMP_DEBUG
1151 __kmp_debug_printf("__kmp_team_handler: unknown signal type");
1152#endif
1153 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001154 }
1155 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001156} // __kmp_team_handler
1157
Jonathan Peyton30419822017-05-12 18:01:32 +00001158static void __kmp_sigaction(int signum, const struct sigaction *act,
1159 struct sigaction *oldact) {
1160 int rc = sigaction(signum, act, oldact);
1161 KMP_CHECK_SYSFAIL_ERRNO("sigaction", rc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001162}
1163
Jonathan Peyton30419822017-05-12 18:01:32 +00001164static void __kmp_install_one_handler(int sig, sig_func_t handler_func,
1165 int parallel_init) {
1166 KMP_MB(); // Flush all pending memory write invalidates.
1167 KB_TRACE(60,
1168 ("__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init));
1169 if (parallel_init) {
1170 struct sigaction new_action;
1171 struct sigaction old_action;
1172 new_action.sa_handler = handler_func;
1173 new_action.sa_flags = 0;
1174 sigfillset(&new_action.sa_mask);
1175 __kmp_sigaction(sig, &new_action, &old_action);
1176 if (old_action.sa_handler == __kmp_sighldrs[sig].sa_handler) {
1177 sigaddset(&__kmp_sigset, sig);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001178 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00001179 // Restore/keep user's handler if one previously installed.
1180 __kmp_sigaction(sig, &old_action, NULL);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001181 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001182 } else {
1183 // Save initial/system signal handlers to see if user handlers installed.
1184 __kmp_sigaction(sig, NULL, &__kmp_sighldrs[sig]);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001185 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001186 KMP_MB(); // Flush all pending memory write invalidates.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001187} // __kmp_install_one_handler
1188
Jonathan Peyton30419822017-05-12 18:01:32 +00001189static void __kmp_remove_one_handler(int sig) {
1190 KB_TRACE(60, ("__kmp_remove_one_handler( %d )\n", sig));
1191 if (sigismember(&__kmp_sigset, sig)) {
1192 struct sigaction old;
1193 KMP_MB(); // Flush all pending memory write invalidates.
1194 __kmp_sigaction(sig, &__kmp_sighldrs[sig], &old);
1195 if ((old.sa_handler != __kmp_team_handler) &&
1196 (old.sa_handler != __kmp_null_handler)) {
1197 // Restore the users signal handler.
1198 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1199 "restoring: sig=%d\n",
1200 sig));
1201 __kmp_sigaction(sig, &old, NULL);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001202 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001203 sigdelset(&__kmp_sigset, sig);
1204 KMP_MB(); // Flush all pending memory write invalidates.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001205 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001206} // __kmp_remove_one_handler
1207
Jonathan Peyton30419822017-05-12 18:01:32 +00001208void __kmp_install_signals(int parallel_init) {
1209 KB_TRACE(10, ("__kmp_install_signals( %d )\n", parallel_init));
1210 if (__kmp_handle_signals || !parallel_init) {
1211 // If ! parallel_init, we do not install handlers, just save original
1212 // handlers. Let us do it even __handle_signals is 0.
1213 sigemptyset(&__kmp_sigset);
1214 __kmp_install_one_handler(SIGHUP, __kmp_team_handler, parallel_init);
1215 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1216 __kmp_install_one_handler(SIGQUIT, __kmp_team_handler, parallel_init);
1217 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1218 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1219 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1220 __kmp_install_one_handler(SIGBUS, __kmp_team_handler, parallel_init);
1221 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1222#ifdef SIGSYS
1223 __kmp_install_one_handler(SIGSYS, __kmp_team_handler, parallel_init);
1224#endif // SIGSYS
1225 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
1226#ifdef SIGPIPE
1227 __kmp_install_one_handler(SIGPIPE, __kmp_team_handler, parallel_init);
1228#endif // SIGPIPE
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001229 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001230} // __kmp_install_signals
1231
Jonathan Peyton30419822017-05-12 18:01:32 +00001232void __kmp_remove_signals(void) {
1233 int sig;
1234 KB_TRACE(10, ("__kmp_remove_signals()\n"));
1235 for (sig = 1; sig < NSIG; ++sig) {
1236 __kmp_remove_one_handler(sig);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001237 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001238} // __kmp_remove_signals
1239
Jim Cownie5e8470a2013-09-27 10:38:44 +00001240#endif // KMP_HANDLE_SIGNALS
1241
Jonathan Peyton30419822017-05-12 18:01:32 +00001242void __kmp_enable(int new_state) {
1243#ifdef KMP_CANCEL_THREADS
1244 int status, old_state;
1245 status = pthread_setcancelstate(new_state, &old_state);
1246 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
1247 KMP_DEBUG_ASSERT(old_state == PTHREAD_CANCEL_DISABLE);
1248#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001249}
1250
Jonathan Peyton30419822017-05-12 18:01:32 +00001251void __kmp_disable(int *old_state) {
1252#ifdef KMP_CANCEL_THREADS
1253 int status;
1254 status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state);
1255 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
1256#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001257}
1258
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001259static void __kmp_atfork_prepare(void) {
1260 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
1261 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001262}
1263
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001264static void __kmp_atfork_parent(void) {
1265 __kmp_release_bootstrap_lock(&__kmp_initz_lock);
1266 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001267}
1268
Jonathan Peyton30419822017-05-12 18:01:32 +00001269/* Reset the library so execution in the child starts "all over again" with
1270 clean data structures in initial states. Don't worry about freeing memory
1271 allocated by parent, just abandon it to be safe. */
1272static void __kmp_atfork_child(void) {
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001273 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
Jonathan Peyton30419822017-05-12 18:01:32 +00001274 /* TODO make sure this is done right for nested/sibling */
1275 // ATT: Memory leaks are here? TODO: Check it and fix.
1276 /* KMP_ASSERT( 0 ); */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001277
Jonathan Peyton30419822017-05-12 18:01:32 +00001278 ++__kmp_fork_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001279
Jonathan Peyton072ccb72017-06-15 21:51:07 +00001280#if KMP_AFFINITY_SUPPORTED
1281#if KMP_OS_LINUX
Jonathan Peytond330e632017-06-13 17:16:12 +00001282 // reset the affinity in the child to the initial thread
1283 // affinity in the parent
1284 kmp_set_thread_affinity_mask_initial();
1285#endif
Jonathan Peyton072ccb72017-06-15 21:51:07 +00001286 // Set default not to bind threads tightly in the child (we’re expecting
1287 // over-subscription after the fork and this can improve things for
1288 // scripting languages that use OpenMP inside process-parallel code).
1289 __kmp_affinity_type = affinity_none;
1290#if OMP_40_ENABLED
1291 if (__kmp_nested_proc_bind.bind_types != NULL) {
1292 __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
1293 }
1294#endif // OMP_40_ENABLED
1295#endif // KMP_AFFINITY_SUPPORTED
Jonathan Peytond330e632017-06-13 17:16:12 +00001296
Jonathan Peyton30419822017-05-12 18:01:32 +00001297 __kmp_init_runtime = FALSE;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001298#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001299 __kmp_init_monitor = 0;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001300#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001301 __kmp_init_parallel = FALSE;
1302 __kmp_init_middle = FALSE;
1303 __kmp_init_serial = FALSE;
1304 TCW_4(__kmp_init_gtid, FALSE);
1305 __kmp_init_common = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001306
Jonathan Peyton30419822017-05-12 18:01:32 +00001307 TCW_4(__kmp_init_user_locks, FALSE);
1308#if !KMP_USE_DYNAMIC_LOCK
1309 __kmp_user_lock_table.used = 1;
1310 __kmp_user_lock_table.allocated = 0;
1311 __kmp_user_lock_table.table = NULL;
1312 __kmp_lock_blocks = NULL;
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001313#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001314
Jonathan Peyton30419822017-05-12 18:01:32 +00001315 __kmp_all_nth = 0;
1316 TCW_4(__kmp_nth, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001317
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001318 __kmp_thread_pool = NULL;
1319 __kmp_thread_pool_insert_pt = NULL;
1320 __kmp_team_pool = NULL;
1321
Jonathan Peyton30419822017-05-12 18:01:32 +00001322 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate
1323 here so threadprivate doesn't use stale data */
1324 KA_TRACE(10, ("__kmp_atfork_child: checking cache address list %p\n",
1325 __kmp_threadpriv_cache_list));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001326
Jonathan Peyton30419822017-05-12 18:01:32 +00001327 while (__kmp_threadpriv_cache_list != NULL) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001328
Jonathan Peyton30419822017-05-12 18:01:32 +00001329 if (*__kmp_threadpriv_cache_list->addr != NULL) {
1330 KC_TRACE(50, ("__kmp_atfork_child: zeroing cache at address %p\n",
1331 &(*__kmp_threadpriv_cache_list->addr)));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001332
Jonathan Peyton30419822017-05-12 18:01:32 +00001333 *__kmp_threadpriv_cache_list->addr = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001334 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001335 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list->next;
1336 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001337
Jonathan Peyton30419822017-05-12 18:01:32 +00001338 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001339
Jonathan Peyton30419822017-05-12 18:01:32 +00001340 /* reset statically initialized locks */
1341 __kmp_init_bootstrap_lock(&__kmp_initz_lock);
1342 __kmp_init_bootstrap_lock(&__kmp_stdio_lock);
1343 __kmp_init_bootstrap_lock(&__kmp_console_lock);
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001344 __kmp_init_bootstrap_lock(&__kmp_task_team_lock);
1345
Andrey Churbanov5388acd2018-01-11 15:09:49 +00001346#if USE_ITT_BUILD
Jonathan Peytoneaa9e402018-01-10 18:21:48 +00001347 __kmp_itt_reset(); // reset ITT's global state
Andrey Churbanov5388acd2018-01-11 15:09:49 +00001348#endif /* USE_ITT_BUILD */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001349
Jonathan Peyton30419822017-05-12 18:01:32 +00001350 /* This is necessary to make sure no stale data is left around */
1351 /* AC: customers complain that we use unsafe routines in the atfork
1352 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1353 in dynamic_link when check the presence of shared tbbmalloc library.
1354 Suggestion is to make the library initialization lazier, similar
1355 to what done for __kmpc_begin(). */
1356 // TODO: synchronize all static initializations with regular library
1357 // startup; look at kmp_global.cpp and etc.
1358 //__kmp_internal_begin ();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001359}
1360
Jonathan Peyton30419822017-05-12 18:01:32 +00001361void __kmp_register_atfork(void) {
1362 if (__kmp_need_register_atfork) {
1363 int status = pthread_atfork(__kmp_atfork_prepare, __kmp_atfork_parent,
1364 __kmp_atfork_child);
1365 KMP_CHECK_SYSFAIL("pthread_atfork", status);
1366 __kmp_need_register_atfork = FALSE;
1367 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001368}
1369
Jonathan Peyton30419822017-05-12 18:01:32 +00001370void __kmp_suspend_initialize(void) {
1371 int status;
1372 status = pthread_mutexattr_init(&__kmp_suspend_mutex_attr);
1373 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status);
1374 status = pthread_condattr_init(&__kmp_suspend_cond_attr);
1375 KMP_CHECK_SYSFAIL("pthread_condattr_init", status);
1376}
1377
1378static void __kmp_suspend_initialize_thread(kmp_info_t *th) {
1379 ANNOTATE_HAPPENS_AFTER(&th->th.th_suspend_init_count);
1380 if (th->th.th_suspend_init_count <= __kmp_fork_count) {
1381 /* this means we haven't initialized the suspension pthread objects for this
1382 thread in this instance of the process */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001383 int status;
Jonathan Peyton30419822017-05-12 18:01:32 +00001384 status = pthread_cond_init(&th->th.th_suspend_cv.c_cond,
1385 &__kmp_suspend_cond_attr);
1386 KMP_CHECK_SYSFAIL("pthread_cond_init", status);
1387 status = pthread_mutex_init(&th->th.th_suspend_mx.m_mutex,
1388 &__kmp_suspend_mutex_attr);
1389 KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
1390 *(volatile int *)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1391 ANNOTATE_HAPPENS_BEFORE(&th->th.th_suspend_init_count);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001392 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001393}
1394
Jonathan Peyton30419822017-05-12 18:01:32 +00001395void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
1396 if (th->th.th_suspend_init_count > __kmp_fork_count) {
1397 /* this means we have initialize the suspension pthread objects for this
1398 thread in this instance of the process */
1399 int status;
1400
1401 status = pthread_cond_destroy(&th->th.th_suspend_cv.c_cond);
1402 if (status != 0 && status != EBUSY) {
1403 KMP_SYSFAIL("pthread_cond_destroy", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001404 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001405 status = pthread_mutex_destroy(&th->th.th_suspend_mx.m_mutex);
1406 if (status != 0 && status != EBUSY) {
1407 KMP_SYSFAIL("pthread_mutex_destroy", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001408 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001409 --th->th.th_suspend_init_count;
1410 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1411 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001412}
1413
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001414/* This routine puts the calling thread to sleep after setting the
Jonathan Peyton30419822017-05-12 18:01:32 +00001415 sleep bit for the indicated flag variable to true. */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001416template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +00001417static inline void __kmp_suspend_template(int th_gtid, C *flag) {
1418 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_suspend);
1419 kmp_info_t *th = __kmp_threads[th_gtid];
1420 int status;
1421 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001422
Jonathan Peyton30419822017-05-12 18:01:32 +00001423 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid,
1424 flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001425
Jonathan Peyton30419822017-05-12 18:01:32 +00001426 __kmp_suspend_initialize_thread(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001427
Jonathan Peyton30419822017-05-12 18:01:32 +00001428 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1429 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001430
Jonathan Peyton30419822017-05-12 18:01:32 +00001431 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1432 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001433
Jonathan Peyton30419822017-05-12 18:01:32 +00001434 /* TODO: shouldn't this use release semantics to ensure that
1435 __kmp_suspend_initialize_thread gets called first? */
1436 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001437
Jonathan Peyton30419822017-05-12 18:01:32 +00001438 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x,"
1439 " was %x\n",
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001440 th_gtid, flag->get(), flag->load(), old_spin));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001441
Jonathan Peyton30419822017-05-12 18:01:32 +00001442 if (flag->done_check_val(old_spin)) {
1443 old_spin = flag->unset_sleeping();
1444 KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
1445 "for spin(%p)\n",
1446 th_gtid, flag->get()));
1447 } else {
1448 /* Encapsulate in a loop as the documentation states that this may
1449 "with low probability" return when the condition variable has
1450 not been signaled or broadcast */
1451 int deactivated = FALSE;
1452 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1453
1454 while (flag->is_sleeping()) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001455#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001456 char buffer[128];
1457 __kmp_suspend_count++;
1458 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1459 __kmp_printf("__kmp_suspend_template: suspending T#%d: %s\n", th_gtid,
1460 buffer);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001461#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001462 // Mark the thread as no longer active (only in the first iteration of the
1463 // loop).
1464 if (!deactivated) {
1465 th->th.th_active = FALSE;
1466 if (th->th.th_active_in_pool) {
1467 th->th.th_active_in_pool = FALSE;
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001468 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +00001469 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
1470 }
1471 deactivated = TRUE;
1472 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001473
1474#if USE_SUSPEND_TIMEOUT
Jonathan Peyton30419822017-05-12 18:01:32 +00001475 struct timespec now;
1476 struct timeval tval;
1477 int msecs;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001478
Jonathan Peyton30419822017-05-12 18:01:32 +00001479 status = gettimeofday(&tval, NULL);
1480 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1481 TIMEVAL_TO_TIMESPEC(&tval, &now);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001482
Jonathan Peyton30419822017-05-12 18:01:32 +00001483 msecs = (4 * __kmp_dflt_blocktime) + 200;
1484 now.tv_sec += msecs / 1000;
1485 now.tv_nsec += (msecs % 1000) * 1000;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001486
Jonathan Peyton30419822017-05-12 18:01:32 +00001487 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
1488 "pthread_cond_timedwait\n",
1489 th_gtid));
1490 status = pthread_cond_timedwait(&th->th.th_suspend_cv.c_cond,
1491 &th->th.th_suspend_mx.m_mutex, &now);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001492#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001493 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform"
1494 " pthread_cond_wait\n",
1495 th_gtid));
1496 status = pthread_cond_wait(&th->th.th_suspend_cv.c_cond,
1497 &th->th.th_suspend_mx.m_mutex);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001498#endif
1499
Jonathan Peyton30419822017-05-12 18:01:32 +00001500 if ((status != 0) && (status != EINTR) && (status != ETIMEDOUT)) {
1501 KMP_SYSFAIL("pthread_cond_wait", status);
1502 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001503#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001504 if (status == ETIMEDOUT) {
1505 if (flag->is_sleeping()) {
1506 KF_TRACE(100,
1507 ("__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid));
1508 } else {
1509 KF_TRACE(2, ("__kmp_suspend_template: T#%d timeout wakeup, sleep bit "
1510 "not set!\n",
1511 th_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001512 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001513 } else if (flag->is_sleeping()) {
1514 KF_TRACE(100,
1515 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
1516 }
1517#endif
1518 } // while
Jim Cownie5e8470a2013-09-27 10:38:44 +00001519
Jonathan Peyton30419822017-05-12 18:01:32 +00001520 // Mark the thread as active again (if it was previous marked as inactive)
1521 if (deactivated) {
1522 th->th.th_active = TRUE;
1523 if (TCR_4(th->th.th_in_pool)) {
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001524 KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +00001525 th->th.th_active_in_pool = TRUE;
1526 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001527 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001528 }
1529#ifdef DEBUG_SUSPEND
1530 {
1531 char buffer[128];
1532 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1533 __kmp_printf("__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid,
1534 buffer);
1535 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001536#endif
1537
Jonathan Peyton30419822017-05-12 18:01:32 +00001538 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1539 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1540 KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001541}
1542
1543void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001544 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001545}
1546void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001547 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001548}
1549void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001550 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001551}
1552
Jim Cownie5e8470a2013-09-27 10:38:44 +00001553/* This routine signals the thread specified by target_gtid to wake up
Jonathan Peyton30419822017-05-12 18:01:32 +00001554 after setting the sleep bit indicated by the flag argument to FALSE.
1555 The target thread must already have called __kmp_suspend_template() */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001556template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +00001557static inline void __kmp_resume_template(int target_gtid, C *flag) {
1558 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1559 kmp_info_t *th = __kmp_threads[target_gtid];
1560 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001561
1562#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001563 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001564#endif
1565
Jonathan Peyton30419822017-05-12 18:01:32 +00001566 KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
1567 gtid, target_gtid));
1568 KMP_DEBUG_ASSERT(gtid != target_gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001569
Jonathan Peyton30419822017-05-12 18:01:32 +00001570 __kmp_suspend_initialize_thread(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001571
Jonathan Peyton30419822017-05-12 18:01:32 +00001572 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1573 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001574
Jonathan Peyton30419822017-05-12 18:01:32 +00001575 if (!flag) { // coming from __kmp_null_resume_wrapper
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00001576 flag = (C *)CCAST(void *, th->th.th_sleep_loc);
Jonathan Peyton30419822017-05-12 18:01:32 +00001577 }
1578
1579 // First, check if the flag is null or its type has changed. If so, someone
1580 // else woke it up.
1581 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type
1582 // simply shows what
1583 // flag was cast to
1584 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1585 "awake: flag(%p)\n",
1586 gtid, target_gtid, NULL));
1587 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1588 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1589 return;
1590 } else { // if multiple threads are sleeping, flag should be internally
1591 // referring to a specific thread here
1592 typename C::flag_t old_spin = flag->unset_sleeping();
1593 if (!flag->is_sleeping_val(old_spin)) {
1594 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1595 "awake: flag(%p): "
1596 "%u => %u\n",
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001597 gtid, target_gtid, flag->get(), old_spin, flag->load()));
Jonathan Peyton30419822017-05-12 18:01:32 +00001598 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1599 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1600 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001601 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001602 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset "
1603 "sleep bit for flag's loc(%p): "
1604 "%u => %u\n",
Jonathan Peyton37e2ef52018-07-09 17:36:22 +00001605 gtid, target_gtid, flag->get(), old_spin, flag->load()));
Jonathan Peyton30419822017-05-12 18:01:32 +00001606 }
1607 TCW_PTR(th->th.th_sleep_loc, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001608
1609#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001610 {
1611 char buffer[128];
1612 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1613 __kmp_printf("__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid,
1614 target_gtid, buffer);
1615 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001616#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001617 status = pthread_cond_signal(&th->th.th_suspend_cv.c_cond);
1618 KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
1619 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1620 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1621 KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
1622 " for T#%d\n",
1623 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001624}
1625
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001626void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001627 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001628}
1629void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001630 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001631}
1632void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001633 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001634}
1635
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001636#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001637void __kmp_resume_monitor() {
1638 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1639 int status;
Jim Cownie07ea89f2014-09-03 11:10:54 +00001640#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001641 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1642 KF_TRACE(30, ("__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n", gtid,
1643 KMP_GTID_MONITOR));
1644 KMP_DEBUG_ASSERT(gtid != KMP_GTID_MONITOR);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001645#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001646 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
1647 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001648#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001649 {
1650 char buffer[128];
1651 __kmp_print_cond(buffer, &__kmp_wait_cv.c_cond);
1652 __kmp_printf("__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid,
1653 KMP_GTID_MONITOR, buffer);
1654 }
Jim Cownie07ea89f2014-09-03 11:10:54 +00001655#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001656 status = pthread_cond_signal(&__kmp_wait_cv.c_cond);
1657 KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
1658 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
1659 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1660 KF_TRACE(30, ("__kmp_resume_monitor: T#%d exiting after signaling wake up"
1661 " for T#%d\n",
1662 gtid, KMP_GTID_MONITOR));
Jim Cownie07ea89f2014-09-03 11:10:54 +00001663}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001664#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001665
Jonathan Peyton30419822017-05-12 18:01:32 +00001666void __kmp_yield(int cond) {
1667 if (!cond)
1668 return;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001669#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001670 if (!__kmp_yielding_on)
1671 return;
Jonathan Peyton581fdba2017-02-15 17:19:21 +00001672#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001673 if (__kmp_yield_cycle && !KMP_YIELD_NOW())
1674 return;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001675#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001676 sched_yield();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001677}
1678
Jonathan Peyton30419822017-05-12 18:01:32 +00001679void __kmp_gtid_set_specific(int gtid) {
1680 if (__kmp_init_gtid) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001681 int status;
Jonathan Peyton30419822017-05-12 18:01:32 +00001682 status = pthread_setspecific(__kmp_gtid_threadprivate_key,
1683 (void *)(intptr_t)(gtid + 1));
1684 KMP_CHECK_SYSFAIL("pthread_setspecific", status);
1685 } else {
1686 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
1687 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001688}
1689
Jonathan Peyton30419822017-05-12 18:01:32 +00001690int __kmp_gtid_get_specific() {
1691 int gtid;
1692 if (!__kmp_init_gtid) {
1693 KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
1694 "KMP_GTID_SHUTDOWN\n"));
1695 return KMP_GTID_SHUTDOWN;
1696 }
1697 gtid = (int)(size_t)pthread_getspecific(__kmp_gtid_threadprivate_key);
1698 if (gtid == 0) {
1699 gtid = KMP_GTID_DNE;
1700 } else {
1701 gtid--;
1702 }
1703 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1704 __kmp_gtid_threadprivate_key, gtid));
1705 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001706}
1707
Jonathan Peyton30419822017-05-12 18:01:32 +00001708double __kmp_read_cpu_time(void) {
1709 /*clock_t t;*/
1710 struct tms buffer;
1711
1712 /*t =*/times(&buffer);
1713
1714 return (buffer.tms_utime + buffer.tms_cutime) / (double)CLOCKS_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001715}
1716
Jonathan Peyton30419822017-05-12 18:01:32 +00001717int __kmp_read_system_info(struct kmp_sys_info *info) {
1718 int status;
1719 struct rusage r_usage;
1720
1721 memset(info, 0, sizeof(*info));
1722
1723 status = getrusage(RUSAGE_SELF, &r_usage);
1724 KMP_CHECK_SYSFAIL_ERRNO("getrusage", status);
1725
1726 // The maximum resident set size utilized (in kilobytes)
1727 info->maxrss = r_usage.ru_maxrss;
1728 // The number of page faults serviced without any I/O
1729 info->minflt = r_usage.ru_minflt;
1730 // The number of page faults serviced that required I/O
1731 info->majflt = r_usage.ru_majflt;
1732 // The number of times a process was "swapped" out of memory
1733 info->nswap = r_usage.ru_nswap;
1734 // The number of times the file system had to perform input
1735 info->inblock = r_usage.ru_inblock;
1736 // The number of times the file system had to perform output
1737 info->oublock = r_usage.ru_oublock;
1738 // The number of times a context switch was voluntarily
1739 info->nvcsw = r_usage.ru_nvcsw;
1740 // The number of times a context switch was forced
1741 info->nivcsw = r_usage.ru_nivcsw;
1742
1743 return (status != 0);
1744}
1745
1746void __kmp_read_system_time(double *delta) {
1747 double t_ns;
1748 struct timeval tval;
1749 struct timespec stop;
1750 int status;
1751
1752 status = gettimeofday(&tval, NULL);
1753 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1754 TIMEVAL_TO_TIMESPEC(&tval, &stop);
1755 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
1756 *delta = (t_ns * 1e-9);
1757}
1758
1759void __kmp_clear_system_time(void) {
1760 struct timeval tval;
1761 int status;
1762 status = gettimeofday(&tval, NULL);
1763 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1764 TIMEVAL_TO_TIMESPEC(&tval, &__kmp_sys_timer_data.start);
1765}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001766
Jonathan Peyton30419822017-05-12 18:01:32 +00001767static int __kmp_get_xproc(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001768
Jonathan Peyton30419822017-05-12 18:01:32 +00001769 int r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001770
Kamil Rytarowskia56ac942018-12-09 16:40:33 +00001771#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +00001772 KMP_OS_OPENBSD || KMP_OS_HURD
Jim Cownie5e8470a2013-09-27 10:38:44 +00001773
Jonathan Peyton30419822017-05-12 18:01:32 +00001774 r = sysconf(_SC_NPROCESSORS_ONLN);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001775
Jonathan Peyton30419822017-05-12 18:01:32 +00001776#elif KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00001777
Jonathan Peyton30419822017-05-12 18:01:32 +00001778 // Bug C77011 High "OpenMP Threads and number of active cores".
Jim Cownie5e8470a2013-09-27 10:38:44 +00001779
Jonathan Peyton30419822017-05-12 18:01:32 +00001780 // Find the number of available CPUs.
1781 kern_return_t rc;
1782 host_basic_info_data_t info;
1783 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
1784 rc = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&info, &num);
1785 if (rc == 0 && num == HOST_BASIC_INFO_COUNT) {
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00001786 // Cannot use KA_TRACE() here because this code works before trace support
1787 // is initialized.
Jonathan Peyton30419822017-05-12 18:01:32 +00001788 r = info.avail_cpus;
1789 } else {
1790 KMP_WARNING(CantGetNumAvailCPU);
1791 KMP_INFORM(AssumedNumCPU);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001792 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001793
Jonathan Peyton30419822017-05-12 18:01:32 +00001794#else
Jim Cownie5e8470a2013-09-27 10:38:44 +00001795
Jonathan Peyton30419822017-05-12 18:01:32 +00001796#error "Unknown or unsupported OS."
Jim Cownie5e8470a2013-09-27 10:38:44 +00001797
Jonathan Peyton30419822017-05-12 18:01:32 +00001798#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001799
Jonathan Peyton30419822017-05-12 18:01:32 +00001800 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001801
1802} // __kmp_get_xproc
1803
Jonathan Peyton30419822017-05-12 18:01:32 +00001804int __kmp_read_from_file(char const *path, char const *format, ...) {
1805 int result;
1806 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001807
Jonathan Peyton30419822017-05-12 18:01:32 +00001808 va_start(args, format);
1809 FILE *f = fopen(path, "rb");
1810 if (f == NULL)
1811 return 0;
1812 result = vfscanf(f, format, args);
1813 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001814
Jonathan Peyton30419822017-05-12 18:01:32 +00001815 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00001816}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001817
Jonathan Peyton30419822017-05-12 18:01:32 +00001818void __kmp_runtime_initialize(void) {
1819 int status;
1820 pthread_mutexattr_t mutex_attr;
1821 pthread_condattr_t cond_attr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001822
Jonathan Peyton30419822017-05-12 18:01:32 +00001823 if (__kmp_init_runtime) {
1824 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001825 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001826
Jonathan Peyton30419822017-05-12 18:01:32 +00001827#if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
1828 if (!__kmp_cpuinfo.initialized) {
1829 __kmp_query_cpuid(&__kmp_cpuinfo);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001830 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001831#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001832
Jonathan Peyton30419822017-05-12 18:01:32 +00001833 __kmp_xproc = __kmp_get_xproc();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001834
Jonathan Peyton30419822017-05-12 18:01:32 +00001835 if (sysconf(_SC_THREADS)) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001836
Jonathan Peyton30419822017-05-12 18:01:32 +00001837 /* Query the maximum number of threads */
1838 __kmp_sys_max_nth = sysconf(_SC_THREAD_THREADS_MAX);
1839 if (__kmp_sys_max_nth == -1) {
1840 /* Unlimited threads for NPTL */
1841 __kmp_sys_max_nth = INT_MAX;
1842 } else if (__kmp_sys_max_nth <= 1) {
1843 /* Can't tell, just use PTHREAD_THREADS_MAX */
1844 __kmp_sys_max_nth = KMP_MAX_NTH;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001845 }
1846
Jonathan Peyton30419822017-05-12 18:01:32 +00001847 /* Query the minimum stack size */
1848 __kmp_sys_min_stksize = sysconf(_SC_THREAD_STACK_MIN);
1849 if (__kmp_sys_min_stksize <= 1) {
1850 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
1851 }
1852 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001853
Jonathan Peyton30419822017-05-12 18:01:32 +00001854 /* Set up minimum number of threads to switch to TLS gtid */
1855 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001856
Jonathan Peyton30419822017-05-12 18:01:32 +00001857 status = pthread_key_create(&__kmp_gtid_threadprivate_key,
1858 __kmp_internal_end_dest);
1859 KMP_CHECK_SYSFAIL("pthread_key_create", status);
1860 status = pthread_mutexattr_init(&mutex_attr);
1861 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status);
1862 status = pthread_mutex_init(&__kmp_wait_mx.m_mutex, &mutex_attr);
1863 KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
1864 status = pthread_condattr_init(&cond_attr);
1865 KMP_CHECK_SYSFAIL("pthread_condattr_init", status);
1866 status = pthread_cond_init(&__kmp_wait_cv.c_cond, &cond_attr);
1867 KMP_CHECK_SYSFAIL("pthread_cond_init", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001868#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001869 __kmp_itt_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001870#endif /* USE_ITT_BUILD */
1871
Jonathan Peyton30419822017-05-12 18:01:32 +00001872 __kmp_init_runtime = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001873}
1874
Jonathan Peyton30419822017-05-12 18:01:32 +00001875void __kmp_runtime_destroy(void) {
1876 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001877
Jonathan Peyton30419822017-05-12 18:01:32 +00001878 if (!__kmp_init_runtime) {
1879 return; // Nothing to do.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001880 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001881
1882#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001883 __kmp_itt_destroy();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001884#endif /* USE_ITT_BUILD */
1885
Jonathan Peyton30419822017-05-12 18:01:32 +00001886 status = pthread_key_delete(__kmp_gtid_threadprivate_key);
1887 KMP_CHECK_SYSFAIL("pthread_key_delete", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001888
Jonathan Peyton30419822017-05-12 18:01:32 +00001889 status = pthread_mutex_destroy(&__kmp_wait_mx.m_mutex);
1890 if (status != 0 && status != EBUSY) {
1891 KMP_SYSFAIL("pthread_mutex_destroy", status);
1892 }
1893 status = pthread_cond_destroy(&__kmp_wait_cv.c_cond);
1894 if (status != 0 && status != EBUSY) {
1895 KMP_SYSFAIL("pthread_cond_destroy", status);
1896 }
1897#if KMP_AFFINITY_SUPPORTED
1898 __kmp_affinity_uninitialize();
1899#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001900
Jonathan Peyton30419822017-05-12 18:01:32 +00001901 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001902}
1903
Jim Cownie5e8470a2013-09-27 10:38:44 +00001904/* Put the thread to sleep for a time period */
1905/* NOTE: not currently used anywhere */
Jonathan Peyton30419822017-05-12 18:01:32 +00001906void __kmp_thread_sleep(int millis) { sleep((millis + 500) / 1000); }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001907
1908/* Calculate the elapsed wall clock time for the user */
Jonathan Peyton30419822017-05-12 18:01:32 +00001909void __kmp_elapsed(double *t) {
1910 int status;
1911#ifdef FIX_SGI_CLOCK
1912 struct timespec ts;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001913
Jonathan Peyton30419822017-05-12 18:01:32 +00001914 status = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
1915 KMP_CHECK_SYSFAIL_ERRNO("clock_gettime", status);
1916 *t =
1917 (double)ts.tv_nsec * (1.0 / (double)KMP_NSEC_PER_SEC) + (double)ts.tv_sec;
1918#else
1919 struct timeval tv;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001920
Jonathan Peyton30419822017-05-12 18:01:32 +00001921 status = gettimeofday(&tv, NULL);
1922 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1923 *t =
1924 (double)tv.tv_usec * (1.0 / (double)KMP_USEC_PER_SEC) + (double)tv.tv_sec;
1925#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001926}
1927
1928/* Calculate the elapsed wall clock tick for the user */
Jonathan Peyton30419822017-05-12 18:01:32 +00001929void __kmp_elapsed_tick(double *t) { *t = 1 / (double)CLOCKS_PER_SEC; }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001930
Jonathan Peyton377aa402016-04-14 16:00:37 +00001931/* Return the current time stamp in nsec */
Jonathan Peyton30419822017-05-12 18:01:32 +00001932kmp_uint64 __kmp_now_nsec() {
1933 struct timeval t;
1934 gettimeofday(&t, NULL);
1935 return KMP_NSEC_PER_SEC * t.tv_sec + 1000 * t.tv_usec;
Jonathan Peyton377aa402016-04-14 16:00:37 +00001936}
1937
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001938#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton35d75ae2017-03-20 22:11:31 +00001939/* Measure clock ticks per millisecond */
Jonathan Peyton30419822017-05-12 18:01:32 +00001940void __kmp_initialize_system_tick() {
1941 kmp_uint64 delay = 100000; // 50~100 usec on most machines.
1942 kmp_uint64 nsec = __kmp_now_nsec();
1943 kmp_uint64 goal = __kmp_hardware_timestamp() + delay;
1944 kmp_uint64 now;
1945 while ((now = __kmp_hardware_timestamp()) < goal)
1946 ;
1947 __kmp_ticks_per_msec =
1948 (kmp_uint64)(1e6 * (delay + (now - goal)) / (__kmp_now_nsec() - nsec));
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001949}
1950#endif
1951
Jonathan Peyton30419822017-05-12 18:01:32 +00001952/* Determine whether the given address is mapped into the current address
1953 space. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001954
Jonathan Peyton30419822017-05-12 18:01:32 +00001955int __kmp_is_address_mapped(void *addr) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001956
Jonathan Peyton30419822017-05-12 18:01:32 +00001957 int found = 0;
1958 int rc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001959
Andrey Churbanov855d0982018-11-07 12:27:38 +00001960#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_HURD
Jim Cownie5e8470a2013-09-27 10:38:44 +00001961
Andrey Churbanov855d0982018-11-07 12:27:38 +00001962 /* On GNUish OSes, read the /proc/<pid>/maps pseudo-file to get all the address
Jonathan Peyton30419822017-05-12 18:01:32 +00001963 ranges mapped into the address space. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001964
Jonathan Peyton30419822017-05-12 18:01:32 +00001965 char *name = __kmp_str_format("/proc/%d/maps", getpid());
1966 FILE *file = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001967
Jonathan Peyton30419822017-05-12 18:01:32 +00001968 file = fopen(name, "r");
1969 KMP_ASSERT(file != NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001970
Jonathan Peyton30419822017-05-12 18:01:32 +00001971 for (;;) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001972
Jonathan Peyton30419822017-05-12 18:01:32 +00001973 void *beginning = NULL;
1974 void *ending = NULL;
1975 char perms[5];
Jim Cownie5e8470a2013-09-27 10:38:44 +00001976
Jonathan Peyton30419822017-05-12 18:01:32 +00001977 rc = fscanf(file, "%p-%p %4s %*[^\n]\n", &beginning, &ending, perms);
1978 if (rc == EOF) {
1979 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001980 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001981 KMP_ASSERT(rc == 3 &&
1982 KMP_STRLEN(perms) == 4); // Make sure all fields are read.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001983
Jonathan Peyton30419822017-05-12 18:01:32 +00001984 // Ending address is not included in the region, but beginning is.
1985 if ((addr >= beginning) && (addr < ending)) {
1986 perms[2] = 0; // 3th and 4th character does not matter.
1987 if (strcmp(perms, "rw") == 0) {
1988 // Memory we are looking for should be readable and writable.
Alp Toker763b9392014-02-28 09:42:41 +00001989 found = 1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001990 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001991 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001992 }
1993 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001994
Jonathan Peyton30419822017-05-12 18:01:32 +00001995 // Free resources.
1996 fclose(file);
1997 KMP_INTERNAL_FREE(name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001998
Jonathan Peyton30419822017-05-12 18:01:32 +00001999#elif KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002000
Jonathan Peyton30419822017-05-12 18:01:32 +00002001 /* On OS X*, /proc pseudo filesystem is not available. Try to read memory
2002 using vm interface. */
2003
2004 int buffer;
2005 vm_size_t count;
2006 rc = vm_read_overwrite(
2007 mach_task_self(), // Task to read memory of.
2008 (vm_address_t)(addr), // Address to read from.
2009 1, // Number of bytes to be read.
2010 (vm_address_t)(&buffer), // Address of buffer to save read bytes in.
2011 &count // Address of var to save number of read bytes in.
2012 );
2013 if (rc == 0) {
2014 // Memory successfully read.
2015 found = 1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002016 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002017
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +00002018#elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_OPENBSD
Jonathan Peyton30419822017-05-12 18:01:32 +00002019
Kamil Rytarowski7e1ea992018-12-09 16:46:48 +00002020 // FIXME(DragonFly, FreeBSD, NetBSD, OpenBSD): Implement this
Jonathan Peyton30419822017-05-12 18:01:32 +00002021 found = 1;
2022
2023#else
2024
2025#error "Unknown or unsupported OS"
2026
2027#endif
2028
2029 return found;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002030
2031} // __kmp_is_address_mapped
2032
2033#ifdef USE_LOAD_BALANCE
2034
Jonathan Peyton30419822017-05-12 18:01:32 +00002035#if KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002036
2037// The function returns the rounded value of the system load average
2038// during given time interval which depends on the value of
2039// __kmp_load_balance_interval variable (default is 60 sec, other values
2040// may be 300 sec or 900 sec).
2041// It returns -1 in case of error.
Jonathan Peyton30419822017-05-12 18:01:32 +00002042int __kmp_get_load_balance(int max) {
2043 double averages[3];
2044 int ret_avg = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002045
Jonathan Peyton30419822017-05-12 18:01:32 +00002046 int res = getloadavg(averages, 3);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002047
Jonathan Peyton30419822017-05-12 18:01:32 +00002048 // Check __kmp_load_balance_interval to determine which of averages to use.
2049 // getloadavg() may return the number of samples less than requested that is
2050 // less than 3.
2051 if (__kmp_load_balance_interval < 180 && (res >= 1)) {
2052 ret_avg = averages[0]; // 1 min
2053 } else if ((__kmp_load_balance_interval >= 180 &&
2054 __kmp_load_balance_interval < 600) &&
2055 (res >= 2)) {
2056 ret_avg = averages[1]; // 5 min
2057 } else if ((__kmp_load_balance_interval >= 600) && (res == 3)) {
2058 ret_avg = averages[2]; // 15 min
2059 } else { // Error occurred
2060 return -1;
2061 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002062
Jonathan Peyton30419822017-05-12 18:01:32 +00002063 return ret_avg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002064}
2065
Jonathan Peyton30419822017-05-12 18:01:32 +00002066#else // Linux* OS
Jim Cownie5e8470a2013-09-27 10:38:44 +00002067
Jonathan Peyton30419822017-05-12 18:01:32 +00002068// The fuction returns number of running (not sleeping) threads, or -1 in case
2069// of error. Error could be reported if Linux* OS kernel too old (without
2070// "/proc" support). Counting running threads stops if max running threads
2071// encountered.
2072int __kmp_get_load_balance(int max) {
2073 static int permanent_error = 0;
2074 static int glb_running_threads = 0; // Saved count of the running threads for
2075 // the thread balance algortihm
2076 static double glb_call_time = 0; /* Thread balance algorithm call time */
Jim Cownie5e8470a2013-09-27 10:38:44 +00002077
Jonathan Peyton30419822017-05-12 18:01:32 +00002078 int running_threads = 0; // Number of running threads in the system.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002079
Jonathan Peyton30419822017-05-12 18:01:32 +00002080 DIR *proc_dir = NULL; // Handle of "/proc/" directory.
2081 struct dirent *proc_entry = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002082
Jonathan Peyton30419822017-05-12 18:01:32 +00002083 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2084 DIR *task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2085 struct dirent *task_entry = NULL;
2086 int task_path_fixed_len;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002087
Jonathan Peyton30419822017-05-12 18:01:32 +00002088 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2089 int stat_file = -1;
2090 int stat_path_fixed_len;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002091
Jonathan Peyton30419822017-05-12 18:01:32 +00002092 int total_processes = 0; // Total number of processes in system.
2093 int total_threads = 0; // Total number of threads in system.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002094
Jonathan Peyton30419822017-05-12 18:01:32 +00002095 double call_time = 0.0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002096
Jonathan Peyton30419822017-05-12 18:01:32 +00002097 __kmp_str_buf_init(&task_path);
2098 __kmp_str_buf_init(&stat_path);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002099
Jonathan Peyton30419822017-05-12 18:01:32 +00002100 __kmp_elapsed(&call_time);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002101
Jonathan Peyton30419822017-05-12 18:01:32 +00002102 if (glb_call_time &&
2103 (call_time - glb_call_time < __kmp_load_balance_interval)) {
2104 running_threads = glb_running_threads;
2105 goto finish;
2106 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002107
Jonathan Peyton30419822017-05-12 18:01:32 +00002108 glb_call_time = call_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002109
Jonathan Peyton30419822017-05-12 18:01:32 +00002110 // Do not spend time on scanning "/proc/" if we have a permanent error.
2111 if (permanent_error) {
2112 running_threads = -1;
2113 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002114 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002115
Jonathan Peyton30419822017-05-12 18:01:32 +00002116 if (max <= 0) {
2117 max = INT_MAX;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002118 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002119
Jonathan Peyton30419822017-05-12 18:01:32 +00002120 // Open "/proc/" directory.
2121 proc_dir = opendir("/proc");
2122 if (proc_dir == NULL) {
2123 // Cannot open "/prroc/". Probably the kernel does not support it. Return an
2124 // error now and in subsequent calls.
2125 running_threads = -1;
2126 permanent_error = 1;
2127 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002128 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002129
Jonathan Peyton30419822017-05-12 18:01:32 +00002130 // Initialize fixed part of task_path. This part will not change.
2131 __kmp_str_buf_cat(&task_path, "/proc/", 6);
2132 task_path_fixed_len = task_path.used; // Remember number of used characters.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002133
Jonathan Peyton30419822017-05-12 18:01:32 +00002134 proc_entry = readdir(proc_dir);
2135 while (proc_entry != NULL) {
2136 // Proc entry is a directory and name starts with a digit. Assume it is a
2137 // process' directory.
2138 if (proc_entry->d_type == DT_DIR && isdigit(proc_entry->d_name[0])) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002139
Jonathan Peyton30419822017-05-12 18:01:32 +00002140 ++total_processes;
2141 // Make sure init process is the very first in "/proc", so we can replace
2142 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes ==
2143 // 1. We are going to check that total_processes == 1 => d_name == "1" is
2144 // true (where "=>" is implication). Since C++ does not have => operator,
2145 // let us replace it with its equivalent: a => b == ! a || b.
2146 KMP_DEBUG_ASSERT(total_processes != 1 ||
2147 strcmp(proc_entry->d_name, "1") == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002148
Jonathan Peyton30419822017-05-12 18:01:32 +00002149 // Construct task_path.
2150 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
2151 __kmp_str_buf_cat(&task_path, proc_entry->d_name,
2152 KMP_STRLEN(proc_entry->d_name));
2153 __kmp_str_buf_cat(&task_path, "/task", 5);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002154
Jonathan Peyton30419822017-05-12 18:01:32 +00002155 task_dir = opendir(task_path.str);
2156 if (task_dir == NULL) {
2157 // Process can finish between reading "/proc/" directory entry and
2158 // opening process' "task/" directory. So, in general case we should not
2159 // complain, but have to skip this process and read the next one. But on
2160 // systems with no "task/" support we will spend lot of time to scan
2161 // "/proc/" tree again and again without any benefit. "init" process
2162 // (its pid is 1) should exist always, so, if we cannot open
2163 // "/proc/1/task/" directory, it means "task/" is not supported by
2164 // kernel. Report an error now and in the future.
2165 if (strcmp(proc_entry->d_name, "1") == 0) {
2166 running_threads = -1;
2167 permanent_error = 1;
2168 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002169 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002170 } else {
2171 // Construct fixed part of stat file path.
2172 __kmp_str_buf_clear(&stat_path);
2173 __kmp_str_buf_cat(&stat_path, task_path.str, task_path.used);
2174 __kmp_str_buf_cat(&stat_path, "/", 1);
2175 stat_path_fixed_len = stat_path.used;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002176
Jonathan Peyton30419822017-05-12 18:01:32 +00002177 task_entry = readdir(task_dir);
2178 while (task_entry != NULL) {
2179 // It is a directory and name starts with a digit.
2180 if (proc_entry->d_type == DT_DIR && isdigit(task_entry->d_name[0])) {
2181 ++total_threads;
2182
2183 // Consruct complete stat file path. Easiest way would be:
2184 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str,
2185 // task_entry->d_name );
2186 // but seriae of __kmp_str_buf_cat works a bit faster.
2187 stat_path.used =
2188 stat_path_fixed_len; // Reset stat path to its fixed part.
2189 __kmp_str_buf_cat(&stat_path, task_entry->d_name,
2190 KMP_STRLEN(task_entry->d_name));
2191 __kmp_str_buf_cat(&stat_path, "/stat", 5);
2192
2193 // Note: Low-level API (open/read/close) is used. High-level API
2194 // (fopen/fclose) works ~ 30 % slower.
2195 stat_file = open(stat_path.str, O_RDONLY);
2196 if (stat_file == -1) {
2197 // We cannot report an error because task (thread) can terminate
2198 // just before reading this file.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002199 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002200 /* Content of "stat" file looks like:
2201 24285 (program) S ...
Jim Cownie5e8470a2013-09-27 10:38:44 +00002202
Jonathan Peyton30419822017-05-12 18:01:32 +00002203 It is a single line (if program name does not include funny
2204 symbols). First number is a thread id, then name of executable
2205 file name in paretheses, then state of the thread. We need just
2206 thread state.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002207
Jonathan Peyton30419822017-05-12 18:01:32 +00002208 Good news: Length of program name is 15 characters max. Longer
2209 names are truncated.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002210
Jonathan Peyton30419822017-05-12 18:01:32 +00002211 Thus, we need rather short buffer: 15 chars for program name +
2212 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002213
Jonathan Peyton30419822017-05-12 18:01:32 +00002214 Bad news: Program name may contain special symbols like space,
2215 closing parenthesis, or even new line. This makes parsing
2216 "stat" file not 100 % reliable. In case of fanny program names
2217 parsing may fail (report incorrect thread state).
Jim Cownie5e8470a2013-09-27 10:38:44 +00002218
Jonathan Peyton30419822017-05-12 18:01:32 +00002219 Parsing "status" file looks more promissing (due to different
2220 file structure and escaping special symbols) but reading and
2221 parsing of "status" file works slower.
2222 -- ln
2223 */
2224 char buffer[65];
2225 int len;
2226 len = read(stat_file, buffer, sizeof(buffer) - 1);
2227 if (len >= 0) {
2228 buffer[len] = 0;
2229 // Using scanf:
2230 // sscanf( buffer, "%*d (%*s) %c ", & state );
2231 // looks very nice, but searching for a closing parenthesis
2232 // works a bit faster.
2233 char *close_parent = strstr(buffer, ") ");
2234 if (close_parent != NULL) {
2235 char state = *(close_parent + 2);
2236 if (state == 'R') {
2237 ++running_threads;
2238 if (running_threads >= max) {
2239 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002240 }
2241 }
2242 }
2243 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002244 close(stat_file);
2245 stat_file = -1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002246 }
2247 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002248 task_entry = readdir(task_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002249 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002250 closedir(task_dir);
2251 task_dir = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002252 }
2253 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002254 proc_entry = readdir(proc_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002255 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002256
Jonathan Peyton30419822017-05-12 18:01:32 +00002257 // There _might_ be a timing hole where the thread executing this
2258 // code get skipped in the load balance, and running_threads is 0.
2259 // Assert in the debug builds only!!!
2260 KMP_DEBUG_ASSERT(running_threads > 0);
2261 if (running_threads <= 0) {
2262 running_threads = 1;
2263 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002264
Jonathan Peyton30419822017-05-12 18:01:32 +00002265finish: // Clean up and exit.
2266 if (proc_dir != NULL) {
2267 closedir(proc_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002268 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002269 __kmp_str_buf_free(&task_path);
2270 if (task_dir != NULL) {
2271 closedir(task_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002272 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002273 __kmp_str_buf_free(&stat_path);
2274 if (stat_file != -1) {
2275 close(stat_file);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002276 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002277
Jonathan Peyton30419822017-05-12 18:01:32 +00002278 glb_running_threads = running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002279
Jonathan Peyton30419822017-05-12 18:01:32 +00002280 return running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002281
2282} // __kmp_get_load_balance
2283
Jonathan Peyton30419822017-05-12 18:01:32 +00002284#endif // KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002285
2286#endif // USE_LOAD_BALANCE
2287
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00002288#if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \
2289 ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || KMP_ARCH_PPC64)
Jim Cownie3051f972014-08-07 10:12:54 +00002290
2291// we really only need the case with 1 argument, because CLANG always build
2292// a struct of pointers to shared variables referenced in the outlined function
Jonathan Peyton30419822017-05-12 18:01:32 +00002293int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int tid, int argc,
2294 void *p_argv[]
Jonathan Peyton122dd762015-07-13 18:55:45 +00002295#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00002296 ,
2297 void **exit_frame_ptr
Jonathan Peyton122dd762015-07-13 18:55:45 +00002298#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002299 ) {
Jonathan Peyton122dd762015-07-13 18:55:45 +00002300#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002301 *exit_frame_ptr = OMPT_GET_FRAME_ADDRESS(0);
Jonathan Peyton122dd762015-07-13 18:55:45 +00002302#endif
2303
Jim Cownie3051f972014-08-07 10:12:54 +00002304 switch (argc) {
2305 default:
2306 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2307 fflush(stderr);
2308 exit(-1);
2309 case 0:
2310 (*pkfn)(&gtid, &tid);
2311 break;
2312 case 1:
2313 (*pkfn)(&gtid, &tid, p_argv[0]);
2314 break;
2315 case 2:
2316 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2317 break;
2318 case 3:
2319 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2320 break;
2321 case 4:
2322 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2323 break;
2324 case 5:
2325 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2326 break;
2327 case 6:
2328 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2329 p_argv[5]);
2330 break;
2331 case 7:
2332 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2333 p_argv[5], p_argv[6]);
2334 break;
2335 case 8:
2336 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2337 p_argv[5], p_argv[6], p_argv[7]);
2338 break;
2339 case 9:
2340 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2341 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2342 break;
2343 case 10:
2344 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2345 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2346 break;
2347 case 11:
2348 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2349 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2350 break;
2351 case 12:
2352 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2353 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2354 p_argv[11]);
2355 break;
2356 case 13:
2357 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2358 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2359 p_argv[11], p_argv[12]);
2360 break;
2361 case 14:
2362 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2363 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2364 p_argv[11], p_argv[12], p_argv[13]);
2365 break;
2366 case 15:
2367 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2368 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2369 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2370 break;
2371 }
2372
Jonathan Peyton122dd762015-07-13 18:55:45 +00002373#if OMPT_SUPPORT
2374 *exit_frame_ptr = 0;
2375#endif
2376
Jim Cownie3051f972014-08-07 10:12:54 +00002377 return 1;
2378}
2379
2380#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002381
Jim Cownie5e8470a2013-09-27 10:38:44 +00002382// end of file //