blob: 42ff0ea1503e2a6f31a5c6d6586f97f0ed2ffe54 [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
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +000025#if !KMP_OS_FREEBSD && !KMP_OS_NETBSD
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>
Alp Toker763b9392014-02-28 09:42:41 +000053#elif 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;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000447#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000448 /* Linux* OS only -- no pthread_getattr_np support on OS X* */
449 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);
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000462#if 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 }
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000486#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000487 /* Use incremental refinement starting from initial conservative estimate */
488 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
489 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
490 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
491 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000492}
493
Jonathan Peyton30419822017-05-12 18:01:32 +0000494static void *__kmp_launch_worker(void *thr) {
495 int status, old_type, old_state;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000496#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000497 sigset_t new_set, old_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000498#endif /* KMP_BLOCK_SIGNALS */
Jonathan Peyton30419822017-05-12 18:01:32 +0000499 void *exit_val;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000500#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000501 void *volatile padding = 0;
Jonathan Peyton2321d572015-06-08 19:25:25 +0000502#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000503 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000504
Jonathan Peyton30419822017-05-12 18:01:32 +0000505 gtid = ((kmp_info_t *)thr)->th.th_info.ds.ds_gtid;
506 __kmp_gtid_set_specific(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000507#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000508 __kmp_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000509#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000510#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000511 // set __thread local index to point to thread-specific stats
512 __kmp_stats_thread_ptr = ((kmp_info_t *)thr)->th.th_stats;
513 KMP_START_EXPLICIT_TIMER(OMP_worker_thread_life);
514 KMP_SET_THREAD_STATE(IDLE);
515 KMP_INIT_PARTITIONED_TIMERS(OMP_idle);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000516#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000517
518#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000519 __kmp_itt_thread_name(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000520#endif /* USE_ITT_BUILD */
521
Alp Toker763b9392014-02-28 09:42:41 +0000522#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +0000523 __kmp_affinity_set_init_mask(gtid, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000524#endif
525
526#ifdef KMP_CANCEL_THREADS
Jonathan Peyton30419822017-05-12 18:01:32 +0000527 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
528 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
529 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
530 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
531 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000532#endif
533
534#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000535 // Set FP control regs to be a copy of the parallel initialization thread's.
536 __kmp_clear_x87_fpu_status_word();
537 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
538 __kmp_load_mxcsr(&__kmp_init_mxcsr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000539#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
540
541#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000542 status = sigfillset(&new_set);
543 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
544 status = pthread_sigmask(SIG_BLOCK, &new_set, &old_set);
545 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000546#endif /* KMP_BLOCK_SIGNALS */
547
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000548#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000549 if (__kmp_stkoffset > 0 && gtid > 0) {
550 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
551 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000552#endif
553
Jonathan Peyton30419822017-05-12 18:01:32 +0000554 KMP_MB();
555 __kmp_set_stack_info(gtid, (kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000556
Jonathan Peyton30419822017-05-12 18:01:32 +0000557 __kmp_check_stack_overlap((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000558
Jonathan Peyton30419822017-05-12 18:01:32 +0000559 exit_val = __kmp_launch_thread((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000560
561#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000562 status = pthread_sigmask(SIG_SETMASK, &old_set, NULL);
563 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000564#endif /* KMP_BLOCK_SIGNALS */
565
Jonathan Peyton30419822017-05-12 18:01:32 +0000566 return exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000567}
568
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000569#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000570/* The monitor thread controls all of the threads in the complex */
571
Jonathan Peyton30419822017-05-12 18:01:32 +0000572static void *__kmp_launch_monitor(void *thr) {
573 int status, old_type, old_state;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000574#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000575 sigset_t new_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000576#endif /* KMP_BLOCK_SIGNALS */
Jonathan Peyton30419822017-05-12 18:01:32 +0000577 struct timespec interval;
578 int yield_count;
579 int yield_cycles = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000580
Jonathan Peyton30419822017-05-12 18:01:32 +0000581 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000582
Jonathan Peyton30419822017-05-12 18:01:32 +0000583 KA_TRACE(10, ("__kmp_launch_monitor: #1 launched\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000584
Jonathan Peyton30419822017-05-12 18:01:32 +0000585 /* register us as the monitor thread */
586 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000587#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000588 __kmp_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000589#endif
590
Jonathan Peyton30419822017-05-12 18:01:32 +0000591 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000592
593#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000594 // Instruct Intel(R) Threading Tools to ignore monitor thread.
595 __kmp_itt_thread_ignore();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000596#endif /* USE_ITT_BUILD */
597
Jonathan Peyton30419822017-05-12 18:01:32 +0000598 __kmp_set_stack_info(((kmp_info_t *)thr)->th.th_info.ds.ds_gtid,
599 (kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000600
Jonathan Peyton30419822017-05-12 18:01:32 +0000601 __kmp_check_stack_overlap((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000602
603#ifdef KMP_CANCEL_THREADS
Jonathan Peyton30419822017-05-12 18:01:32 +0000604 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
605 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
606 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
607 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
608 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000609#endif
610
Jonathan Peyton30419822017-05-12 18:01:32 +0000611#if KMP_REAL_TIME_FIX
612 // This is a potential fix which allows application with real-time scheduling
613 // policy work. However, decision about the fix is not made yet, so it is
614 // disabled by default.
615 { // Are program started with real-time scheduling policy?
616 int sched = sched_getscheduler(0);
617 if (sched == SCHED_FIFO || sched == SCHED_RR) {
618 // Yes, we are a part of real-time application. Try to increase the
619 // priority of the monitor.
620 struct sched_param param;
621 int max_priority = sched_get_priority_max(sched);
622 int rc;
623 KMP_WARNING(RealTimeSchedNotSupported);
624 sched_getparam(0, &param);
625 if (param.sched_priority < max_priority) {
626 param.sched_priority += 1;
627 rc = sched_setscheduler(0, sched, &param);
628 if (rc != 0) {
629 int error = errno;
630 kmp_msg_t err_code = KMP_ERR(error);
631 __kmp_msg(kmp_ms_warning, KMP_MSG(CantChangeMonitorPriority),
632 err_code, KMP_MSG(MonitorWillStarve), __kmp_msg_null);
633 if (__kmp_generate_warnings == kmp_warnings_off) {
634 __kmp_str_free(&err_code.str);
635 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000636 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000637 } else {
638 // We cannot abort here, because number of CPUs may be enough for all
639 // the threads, including the monitor thread, so application could
640 // potentially work...
641 __kmp_msg(kmp_ms_warning, KMP_MSG(RunningAtMaxPriority),
642 KMP_MSG(MonitorWillStarve), KMP_HNT(RunningAtMaxPriority),
643 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000644 }
645 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000646 // AC: free thread that waits for monitor started
647 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
648 }
649#endif // KMP_REAL_TIME_FIX
Jim Cownie5e8470a2013-09-27 10:38:44 +0000650
Jonathan Peyton30419822017-05-12 18:01:32 +0000651 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000652
Jonathan Peyton30419822017-05-12 18:01:32 +0000653 if (__kmp_monitor_wakeups == 1) {
654 interval.tv_sec = 1;
655 interval.tv_nsec = 0;
656 } else {
657 interval.tv_sec = 0;
658 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
659 }
660
661 KA_TRACE(10, ("__kmp_launch_monitor: #2 monitor\n"));
662
663 if (__kmp_yield_cycle) {
664 __kmp_yielding_on = 0; /* Start out with yielding shut off */
665 yield_count = __kmp_yield_off_count;
666 } else {
667 __kmp_yielding_on = 1; /* Yielding is on permanently */
668 }
669
670 while (!TCR_4(__kmp_global.g.g_done)) {
671 struct timespec now;
672 struct timeval tval;
673
674 /* This thread monitors the state of the system */
675
676 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
677
678 status = gettimeofday(&tval, NULL);
679 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
680 TIMEVAL_TO_TIMESPEC(&tval, &now);
681
682 now.tv_sec += interval.tv_sec;
683 now.tv_nsec += interval.tv_nsec;
684
685 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
686 now.tv_sec += 1;
687 now.tv_nsec -= KMP_NSEC_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000688 }
689
Jonathan Peyton30419822017-05-12 18:01:32 +0000690 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
691 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
692 // AC: the monitor should not fall asleep if g_done has been set
693 if (!TCR_4(__kmp_global.g.g_done)) { // check once more under mutex
694 status = pthread_cond_timedwait(&__kmp_wait_cv.c_cond,
695 &__kmp_wait_mx.m_mutex, &now);
696 if (status != 0) {
697 if (status != ETIMEDOUT && status != EINTR) {
698 KMP_SYSFAIL("pthread_cond_timedwait", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000699 }
700 }
701 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000702 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
703 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000704
705 if (__kmp_yield_cycle) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000706 yield_cycles++;
707 if ((yield_cycles % yield_count) == 0) {
708 if (__kmp_yielding_on) {
709 __kmp_yielding_on = 0; /* Turn it off now */
710 yield_count = __kmp_yield_off_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000711 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +0000712 __kmp_yielding_on = 1; /* Turn it on now */
713 yield_count = __kmp_yield_on_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000714 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000715 yield_cycles = 0;
716 }
717 } else {
718 __kmp_yielding_on = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000719 }
720
Jonathan Peyton30419822017-05-12 18:01:32 +0000721 TCW_4(__kmp_global.g.g_time.dt.t_value,
722 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
723
724 KMP_MB(); /* Flush all pending memory write invalidates. */
725 }
726
727 KA_TRACE(10, ("__kmp_launch_monitor: #3 cleanup\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000728
729#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000730 status = sigfillset(&new_set);
731 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
732 status = pthread_sigmask(SIG_UNBLOCK, &new_set, NULL);
733 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000734#endif /* KMP_BLOCK_SIGNALS */
735
Jonathan Peyton30419822017-05-12 18:01:32 +0000736 KA_TRACE(10, ("__kmp_launch_monitor: #4 finished\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000737
Jonathan Peyton30419822017-05-12 18:01:32 +0000738 if (__kmp_global.g.g_abort != 0) {
739 /* now we need to terminate the worker threads */
740 /* the value of t_abort is the signal we caught */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000741
Jonathan Peyton30419822017-05-12 18:01:32 +0000742 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000743
Jonathan Peyton30419822017-05-12 18:01:32 +0000744 KA_TRACE(10, ("__kmp_launch_monitor: #5 terminate sig=%d\n",
745 __kmp_global.g.g_abort));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000746
Jonathan Peyton30419822017-05-12 18:01:32 +0000747 /* terminate the OpenMP worker threads */
748 /* TODO this is not valid for sibling threads!!
749 * the uber master might not be 0 anymore.. */
750 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
751 __kmp_terminate_thread(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000752
Jonathan Peyton30419822017-05-12 18:01:32 +0000753 __kmp_cleanup();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000754
Jonathan Peyton30419822017-05-12 18:01:32 +0000755 KA_TRACE(10, ("__kmp_launch_monitor: #6 raise sig=%d\n",
756 __kmp_global.g.g_abort));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000757
Jonathan Peyton30419822017-05-12 18:01:32 +0000758 if (__kmp_global.g.g_abort > 0)
759 raise(__kmp_global.g.g_abort);
760 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000761
Jonathan Peyton30419822017-05-12 18:01:32 +0000762 KA_TRACE(10, ("__kmp_launch_monitor: #7 exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000763
Jonathan Peyton30419822017-05-12 18:01:32 +0000764 return thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000765}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000766#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000767
Jonathan Peyton30419822017-05-12 18:01:32 +0000768void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
769 pthread_t handle;
770 pthread_attr_t thread_attr;
771 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000772
Jonathan Peyton30419822017-05-12 18:01:32 +0000773 th->th.th_info.ds.ds_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000774
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000775#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000776 // sets up worker thread stats
777 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000778
Jonathan Peyton30419822017-05-12 18:01:32 +0000779 // th->th.th_stats is used to transfer thread-specific stats-pointer to
780 // __kmp_launch_worker. So when thread is created (goes into
781 // __kmp_launch_worker) it will set its __thread local pointer to
782 // th->th.th_stats
783 if (!KMP_UBER_GTID(gtid)) {
784 th->th.th_stats = __kmp_stats_list->push_back(gtid);
785 } else {
786 // For root threads, __kmp_stats_thread_ptr is set in __kmp_register_root(),
787 // so set the th->th.th_stats field to it.
788 th->th.th_stats = __kmp_stats_thread_ptr;
789 }
790 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000791
792#endif // KMP_STATS_ENABLED
793
Jonathan Peyton30419822017-05-12 18:01:32 +0000794 if (KMP_UBER_GTID(gtid)) {
795 KA_TRACE(10, ("__kmp_create_worker: uber thread (%d)\n", gtid));
796 th->th.th_info.ds.ds_thread = pthread_self();
797 __kmp_set_stack_info(gtid, th);
798 __kmp_check_stack_overlap(th);
799 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000800 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000801
Jonathan Peyton30419822017-05-12 18:01:32 +0000802 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000803
Jonathan Peyton30419822017-05-12 18:01:32 +0000804 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000805
806#ifdef KMP_THREAD_ATTR
Jonathan Peyton30419822017-05-12 18:01:32 +0000807 status = pthread_attr_init(&thread_attr);
808 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000809 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000810 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000811 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
812 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000813 __kmp_fatal(KMP_MSG(CantSetWorkerState), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000814 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000815
Jonathan Peyton30419822017-05-12 18:01:32 +0000816 /* Set stack size for this thread now.
817 The multiple of 2 is there because on some machines, requesting an unusual
818 stacksize causes the thread to have an offset before the dummy alloca()
819 takes place to create the offset. Since we want the user to have a
820 sufficient stacksize AND support a stack offset, we alloca() twice the
821 offset so that the upcoming alloca() does not eliminate any premade offset,
822 and also gives the user the stack space they requested for all threads */
823 stack_size += gtid * __kmp_stkoffset * 2;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000824
Jonathan Peyton30419822017-05-12 18:01:32 +0000825 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
826 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
827 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000828
Jim Cownie5e8470a2013-09-27 10:38:44 +0000829#ifdef _POSIX_THREAD_ATTR_STACKSIZE
Jonathan Peyton30419822017-05-12 18:01:32 +0000830 status = pthread_attr_setstacksize(&thread_attr, stack_size);
831#ifdef KMP_BACKUP_STKSIZE
832 if (status != 0) {
833 if (!__kmp_env_stksize) {
834 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
835 __kmp_stksize = KMP_BACKUP_STKSIZE;
836 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
837 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
838 "bytes\n",
839 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
840 status = pthread_attr_setstacksize(&thread_attr, stack_size);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000841 }
842 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000843#endif /* KMP_BACKUP_STKSIZE */
844 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000845 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
846 KMP_HNT(ChangeWorkerStackSize), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000847 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000848#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000849
Jim Cownie5e8470a2013-09-27 10:38:44 +0000850#endif /* KMP_THREAD_ATTR */
851
Jonathan Peyton30419822017-05-12 18:01:32 +0000852 status =
853 pthread_create(&handle, &thread_attr, __kmp_launch_worker, (void *)th);
854 if (status != 0 || !handle) { // ??? Why do we check handle??
855#ifdef _POSIX_THREAD_ATTR_STACKSIZE
856 if (status == EINVAL) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000857 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
858 KMP_HNT(IncreaseWorkerStackSize), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000859 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000860 if (status == ENOMEM) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000861 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
862 KMP_HNT(DecreaseWorkerStackSize), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000863 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000864#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
865 if (status == EAGAIN) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000866 __kmp_fatal(KMP_MSG(NoResourcesForWorkerThread), KMP_ERR(status),
867 KMP_HNT(Decrease_NUM_THREADS), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000868 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000869 KMP_SYSFAIL("pthread_create", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000870 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000871
Jonathan Peyton30419822017-05-12 18:01:32 +0000872 th->th.th_info.ds.ds_thread = handle;
873
874#ifdef KMP_THREAD_ATTR
875 status = pthread_attr_destroy(&thread_attr);
876 if (status) {
877 kmp_msg_t err_code = KMP_ERR(status);
878 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
879 __kmp_msg_null);
880 if (__kmp_generate_warnings == kmp_warnings_off) {
881 __kmp_str_free(&err_code.str);
882 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000883 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000884#endif /* KMP_THREAD_ATTR */
885
886 KMP_MB(); /* Flush all pending memory write invalidates. */
887
888 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000889
890} // __kmp_create_worker
891
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000892#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +0000893void __kmp_create_monitor(kmp_info_t *th) {
894 pthread_t handle;
895 pthread_attr_t thread_attr;
896 size_t size;
897 int status;
898 int auto_adj_size = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000899
Jonathan Peyton30419822017-05-12 18:01:32 +0000900 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
901 // We don't need monitor thread in case of MAX_BLOCKTIME
902 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
903 "MAX blocktime\n"));
904 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
905 th->th.th_info.ds.ds_gtid = 0;
906 return;
907 }
908 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
909
910 KMP_MB(); /* Flush all pending memory write invalidates. */
911
912 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
913 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
914#if KMP_REAL_TIME_FIX
915 TCW_4(__kmp_global.g.g_time.dt.t_value,
916 -1); // Will use it for synchronization a bit later.
917#else
918 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
919#endif // KMP_REAL_TIME_FIX
920
921#ifdef KMP_THREAD_ATTR
922 if (__kmp_monitor_stksize == 0) {
923 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
924 auto_adj_size = TRUE;
925 }
926 status = pthread_attr_init(&thread_attr);
927 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000928 __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000929 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000930 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
931 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000932 __kmp_fatal(KMP_MSG(CantSetMonitorState), KMP_ERR(status), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000933 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000934
935#ifdef _POSIX_THREAD_ATTR_STACKSIZE
936 status = pthread_attr_getstacksize(&thread_attr, &size);
937 KMP_CHECK_SYSFAIL("pthread_attr_getstacksize", status);
938#else
939 size = __kmp_sys_min_stksize;
940#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
941#endif /* KMP_THREAD_ATTR */
942
943 if (__kmp_monitor_stksize == 0) {
944 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
945 }
946 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
947 __kmp_monitor_stksize = __kmp_sys_min_stksize;
948 }
949
950 KA_TRACE(10, ("__kmp_create_monitor: default stacksize = %lu bytes,"
951 "requested stacksize = %lu bytes\n",
952 size, __kmp_monitor_stksize));
953
954retry:
955
956/* Set stack size for this thread now. */
957#ifdef _POSIX_THREAD_ATTR_STACKSIZE
958 KA_TRACE(10, ("__kmp_create_monitor: setting stacksize = %lu bytes,",
959 __kmp_monitor_stksize));
960 status = pthread_attr_setstacksize(&thread_attr, __kmp_monitor_stksize);
961 if (status != 0) {
962 if (auto_adj_size) {
963 __kmp_monitor_stksize *= 2;
964 goto retry;
Jonathan Peyton4fee5f62015-12-18 23:20:36 +0000965 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000966 kmp_msg_t err_code = KMP_ERR(status);
967 __kmp_msg(kmp_ms_warning, // should this be fatal? BB
968 KMP_MSG(CantSetMonitorStackSize, (long int)__kmp_monitor_stksize),
969 err_code, KMP_HNT(ChangeMonitorStackSize), __kmp_msg_null);
970 if (__kmp_generate_warnings == kmp_warnings_off) {
971 __kmp_str_free(&err_code.str);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000972 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000973 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000974#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000975
Jonathan Peyton30419822017-05-12 18:01:32 +0000976 status =
977 pthread_create(&handle, &thread_attr, __kmp_launch_monitor, (void *)th);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000978
Jonathan Peyton30419822017-05-12 18:01:32 +0000979 if (status != 0) {
980#ifdef _POSIX_THREAD_ATTR_STACKSIZE
981 if (status == EINVAL) {
982 if (auto_adj_size && (__kmp_monitor_stksize < (size_t)0x40000000)) {
983 __kmp_monitor_stksize *= 2;
984 goto retry;
985 }
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000986 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
987 KMP_ERR(status), KMP_HNT(IncreaseMonitorStackSize),
988 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000989 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000990 if (status == ENOMEM) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000991 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
992 KMP_ERR(status), KMP_HNT(DecreaseMonitorStackSize),
993 __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000994 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000995#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
996 if (status == EAGAIN) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +0000997 __kmp_fatal(KMP_MSG(NoResourcesForMonitorThread), KMP_ERR(status),
998 KMP_HNT(DecreaseNumberOfThreadsInUse), __kmp_msg_null);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +0000999 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001000 KMP_SYSFAIL("pthread_create", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001001 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001002
Jonathan Peyton30419822017-05-12 18:01:32 +00001003 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001004
Jonathan Peyton30419822017-05-12 18:01:32 +00001005#if KMP_REAL_TIME_FIX
1006 // Wait for the monitor thread is really started and set its *priority*.
1007 KMP_DEBUG_ASSERT(sizeof(kmp_uint32) ==
1008 sizeof(__kmp_global.g.g_time.dt.t_value));
1009 __kmp_wait_yield_4((kmp_uint32 volatile *)&__kmp_global.g.g_time.dt.t_value,
1010 -1, &__kmp_neq_4, NULL);
1011#endif // KMP_REAL_TIME_FIX
Jim Cownie5e8470a2013-09-27 10:38:44 +00001012
Jonathan Peyton30419822017-05-12 18:01:32 +00001013#ifdef KMP_THREAD_ATTR
1014 status = pthread_attr_destroy(&thread_attr);
1015 if (status != 0) {
1016 kmp_msg_t err_code = KMP_ERR(status);
1017 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
1018 __kmp_msg_null);
1019 if (__kmp_generate_warnings == kmp_warnings_off) {
1020 __kmp_str_free(&err_code.str);
1021 }
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001022 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001023#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001024
Jonathan Peyton30419822017-05-12 18:01:32 +00001025 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001026
Jonathan Peyton30419822017-05-12 18:01:32 +00001027 KA_TRACE(10, ("__kmp_create_monitor: monitor created %#.8lx\n",
1028 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001029
1030} // __kmp_create_monitor
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001031#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001032
Jonathan Peyton30419822017-05-12 18:01:32 +00001033void __kmp_exit_thread(int exit_status) {
1034 pthread_exit((void *)(intptr_t)exit_status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001035} // __kmp_exit_thread
1036
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001037#if KMP_USE_MONITOR
Jim Cownie07ea89f2014-09-03 11:10:54 +00001038void __kmp_resume_monitor();
1039
Jonathan Peyton30419822017-05-12 18:01:32 +00001040void __kmp_reap_monitor(kmp_info_t *th) {
1041 int status;
1042 void *exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001043
Jonathan Peyton30419822017-05-12 18:01:32 +00001044 KA_TRACE(10, ("__kmp_reap_monitor: try to reap monitor thread with handle"
1045 " %#.8lx\n",
1046 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001047
Jonathan Peyton30419822017-05-12 18:01:32 +00001048 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1049 // If both tid and gtid are 0, it means the monitor did not ever start.
1050 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1051 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1052 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1053 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1054 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001055 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001056
Jonathan Peyton30419822017-05-12 18:01:32 +00001057 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001058
Jonathan Peyton30419822017-05-12 18:01:32 +00001059 /* First, check to see whether the monitor thread exists to wake it up. This
1060 is to avoid performance problem when the monitor sleeps during
1061 blocktime-size interval */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001062
Jonathan Peyton30419822017-05-12 18:01:32 +00001063 status = pthread_kill(th->th.th_info.ds.ds_thread, 0);
1064 if (status != ESRCH) {
1065 __kmp_resume_monitor(); // Wake up the monitor thread
1066 }
1067 KA_TRACE(10, ("__kmp_reap_monitor: try to join with monitor\n"));
1068 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
1069 if (exit_val != th) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001070 __kmp_fatal(KMP_MSG(ReapMonitorError), KMP_ERR(status), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001071 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001072
Jonathan Peyton30419822017-05-12 18:01:32 +00001073 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1074 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001075
Jonathan Peyton30419822017-05-12 18:01:32 +00001076 KA_TRACE(10, ("__kmp_reap_monitor: done reaping monitor thread with handle"
1077 " %#.8lx\n",
1078 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001079
Jonathan Peyton30419822017-05-12 18:01:32 +00001080 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001081}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001082#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001083
Jonathan Peyton30419822017-05-12 18:01:32 +00001084void __kmp_reap_worker(kmp_info_t *th) {
1085 int status;
1086 void *exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001087
Jonathan Peyton30419822017-05-12 18:01:32 +00001088 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001089
Jonathan Peyton30419822017-05-12 18:01:32 +00001090 KA_TRACE(
1091 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001092
Jonathan Peyton30419822017-05-12 18:01:32 +00001093 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001094#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001095 /* Don't expose these to the user until we understand when they trigger */
1096 if (status != 0) {
Jonathan Peyton6a393f72017-09-05 15:43:58 +00001097 __kmp_fatal(KMP_MSG(ReapWorkerError), KMP_ERR(status), __kmp_msg_null);
Jonathan Peyton30419822017-05-12 18:01:32 +00001098 }
1099 if (exit_val != th) {
1100 KA_TRACE(10, ("__kmp_reap_worker: worker T#%d did not reap properly, "
1101 "exit_val = %p\n",
1102 th->th.th_info.ds.ds_gtid, exit_val));
1103 }
Jonathan Peyton93495de2016-06-13 17:36:40 +00001104#endif /* KMP_DEBUG */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001105
Jonathan Peyton30419822017-05-12 18:01:32 +00001106 KA_TRACE(10, ("__kmp_reap_worker: done reaping T#%d\n",
1107 th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001108
Jonathan Peyton30419822017-05-12 18:01:32 +00001109 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001110}
1111
Jim Cownie5e8470a2013-09-27 10:38:44 +00001112#if KMP_HANDLE_SIGNALS
1113
Jonathan Peyton30419822017-05-12 18:01:32 +00001114static void __kmp_null_handler(int signo) {
1115 // Do nothing, for doing SIG_IGN-type actions.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001116} // __kmp_null_handler
1117
Jonathan Peyton30419822017-05-12 18:01:32 +00001118static void __kmp_team_handler(int signo) {
1119 if (__kmp_global.g.g_abort == 0) {
1120/* Stage 1 signal handler, let's shut down all of the threads */
1121#ifdef KMP_DEBUG
1122 __kmp_debug_printf("__kmp_team_handler: caught signal = %d\n", signo);
1123#endif
1124 switch (signo) {
1125 case SIGHUP:
1126 case SIGINT:
1127 case SIGQUIT:
1128 case SIGILL:
1129 case SIGABRT:
1130 case SIGFPE:
1131 case SIGBUS:
1132 case SIGSEGV:
1133#ifdef SIGSYS
1134 case SIGSYS:
1135#endif
1136 case SIGTERM:
1137 if (__kmp_debug_buf) {
1138 __kmp_dump_debug_buffer();
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001139 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001140 KMP_MB(); // Flush all pending memory write invalidates.
1141 TCW_4(__kmp_global.g.g_abort, signo);
1142 KMP_MB(); // Flush all pending memory write invalidates.
1143 TCW_4(__kmp_global.g.g_done, TRUE);
1144 KMP_MB(); // Flush all pending memory write invalidates.
1145 break;
1146 default:
1147#ifdef KMP_DEBUG
1148 __kmp_debug_printf("__kmp_team_handler: unknown signal type");
1149#endif
1150 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001151 }
1152 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001153} // __kmp_team_handler
1154
Jonathan Peyton30419822017-05-12 18:01:32 +00001155static void __kmp_sigaction(int signum, const struct sigaction *act,
1156 struct sigaction *oldact) {
1157 int rc = sigaction(signum, act, oldact);
1158 KMP_CHECK_SYSFAIL_ERRNO("sigaction", rc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001159}
1160
Jonathan Peyton30419822017-05-12 18:01:32 +00001161static void __kmp_install_one_handler(int sig, sig_func_t handler_func,
1162 int parallel_init) {
1163 KMP_MB(); // Flush all pending memory write invalidates.
1164 KB_TRACE(60,
1165 ("__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init));
1166 if (parallel_init) {
1167 struct sigaction new_action;
1168 struct sigaction old_action;
1169 new_action.sa_handler = handler_func;
1170 new_action.sa_flags = 0;
1171 sigfillset(&new_action.sa_mask);
1172 __kmp_sigaction(sig, &new_action, &old_action);
1173 if (old_action.sa_handler == __kmp_sighldrs[sig].sa_handler) {
1174 sigaddset(&__kmp_sigset, sig);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001175 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00001176 // Restore/keep user's handler if one previously installed.
1177 __kmp_sigaction(sig, &old_action, NULL);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001178 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001179 } else {
1180 // Save initial/system signal handlers to see if user handlers installed.
1181 __kmp_sigaction(sig, NULL, &__kmp_sighldrs[sig]);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001182 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001183 KMP_MB(); // Flush all pending memory write invalidates.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001184} // __kmp_install_one_handler
1185
Jonathan Peyton30419822017-05-12 18:01:32 +00001186static void __kmp_remove_one_handler(int sig) {
1187 KB_TRACE(60, ("__kmp_remove_one_handler( %d )\n", sig));
1188 if (sigismember(&__kmp_sigset, sig)) {
1189 struct sigaction old;
1190 KMP_MB(); // Flush all pending memory write invalidates.
1191 __kmp_sigaction(sig, &__kmp_sighldrs[sig], &old);
1192 if ((old.sa_handler != __kmp_team_handler) &&
1193 (old.sa_handler != __kmp_null_handler)) {
1194 // Restore the users signal handler.
1195 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1196 "restoring: sig=%d\n",
1197 sig));
1198 __kmp_sigaction(sig, &old, NULL);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001199 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001200 sigdelset(&__kmp_sigset, sig);
1201 KMP_MB(); // Flush all pending memory write invalidates.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001202 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001203} // __kmp_remove_one_handler
1204
Jonathan Peyton30419822017-05-12 18:01:32 +00001205void __kmp_install_signals(int parallel_init) {
1206 KB_TRACE(10, ("__kmp_install_signals( %d )\n", parallel_init));
1207 if (__kmp_handle_signals || !parallel_init) {
1208 // If ! parallel_init, we do not install handlers, just save original
1209 // handlers. Let us do it even __handle_signals is 0.
1210 sigemptyset(&__kmp_sigset);
1211 __kmp_install_one_handler(SIGHUP, __kmp_team_handler, parallel_init);
1212 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1213 __kmp_install_one_handler(SIGQUIT, __kmp_team_handler, parallel_init);
1214 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1215 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1216 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1217 __kmp_install_one_handler(SIGBUS, __kmp_team_handler, parallel_init);
1218 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1219#ifdef SIGSYS
1220 __kmp_install_one_handler(SIGSYS, __kmp_team_handler, parallel_init);
1221#endif // SIGSYS
1222 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
1223#ifdef SIGPIPE
1224 __kmp_install_one_handler(SIGPIPE, __kmp_team_handler, parallel_init);
1225#endif // SIGPIPE
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001226 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001227} // __kmp_install_signals
1228
Jonathan Peyton30419822017-05-12 18:01:32 +00001229void __kmp_remove_signals(void) {
1230 int sig;
1231 KB_TRACE(10, ("__kmp_remove_signals()\n"));
1232 for (sig = 1; sig < NSIG; ++sig) {
1233 __kmp_remove_one_handler(sig);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001234 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001235} // __kmp_remove_signals
1236
Jim Cownie5e8470a2013-09-27 10:38:44 +00001237#endif // KMP_HANDLE_SIGNALS
1238
Jonathan Peyton30419822017-05-12 18:01:32 +00001239void __kmp_enable(int new_state) {
1240#ifdef KMP_CANCEL_THREADS
1241 int status, old_state;
1242 status = pthread_setcancelstate(new_state, &old_state);
1243 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
1244 KMP_DEBUG_ASSERT(old_state == PTHREAD_CANCEL_DISABLE);
1245#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001246}
1247
Jonathan Peyton30419822017-05-12 18:01:32 +00001248void __kmp_disable(int *old_state) {
1249#ifdef KMP_CANCEL_THREADS
1250 int status;
1251 status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state);
1252 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
1253#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001254}
1255
Jonathan Peyton30419822017-05-12 18:01:32 +00001256static void __kmp_atfork_prepare(void) { /* nothing to do */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001257}
1258
Jonathan Peyton30419822017-05-12 18:01:32 +00001259static void __kmp_atfork_parent(void) { /* nothing to do */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001260}
1261
Jonathan Peyton30419822017-05-12 18:01:32 +00001262/* Reset the library so execution in the child starts "all over again" with
1263 clean data structures in initial states. Don't worry about freeing memory
1264 allocated by parent, just abandon it to be safe. */
1265static void __kmp_atfork_child(void) {
1266 /* TODO make sure this is done right for nested/sibling */
1267 // ATT: Memory leaks are here? TODO: Check it and fix.
1268 /* KMP_ASSERT( 0 ); */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001269
Jonathan Peyton30419822017-05-12 18:01:32 +00001270 ++__kmp_fork_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001271
Jonathan Peyton072ccb72017-06-15 21:51:07 +00001272#if KMP_AFFINITY_SUPPORTED
1273#if KMP_OS_LINUX
Jonathan Peytond330e632017-06-13 17:16:12 +00001274 // reset the affinity in the child to the initial thread
1275 // affinity in the parent
1276 kmp_set_thread_affinity_mask_initial();
1277#endif
Jonathan Peyton072ccb72017-06-15 21:51:07 +00001278 // Set default not to bind threads tightly in the child (we’re expecting
1279 // over-subscription after the fork and this can improve things for
1280 // scripting languages that use OpenMP inside process-parallel code).
1281 __kmp_affinity_type = affinity_none;
1282#if OMP_40_ENABLED
1283 if (__kmp_nested_proc_bind.bind_types != NULL) {
1284 __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
1285 }
1286#endif // OMP_40_ENABLED
1287#endif // KMP_AFFINITY_SUPPORTED
Jonathan Peytond330e632017-06-13 17:16:12 +00001288
Jonathan Peyton30419822017-05-12 18:01:32 +00001289 __kmp_init_runtime = FALSE;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001290#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001291 __kmp_init_monitor = 0;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001292#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001293 __kmp_init_parallel = FALSE;
1294 __kmp_init_middle = FALSE;
1295 __kmp_init_serial = FALSE;
1296 TCW_4(__kmp_init_gtid, FALSE);
1297 __kmp_init_common = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001298
Jonathan Peyton30419822017-05-12 18:01:32 +00001299 TCW_4(__kmp_init_user_locks, FALSE);
1300#if !KMP_USE_DYNAMIC_LOCK
1301 __kmp_user_lock_table.used = 1;
1302 __kmp_user_lock_table.allocated = 0;
1303 __kmp_user_lock_table.table = NULL;
1304 __kmp_lock_blocks = NULL;
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001305#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001306
Jonathan Peyton30419822017-05-12 18:01:32 +00001307 __kmp_all_nth = 0;
1308 TCW_4(__kmp_nth, 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001309
Jonathan Peyton30419822017-05-12 18:01:32 +00001310 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate
1311 here so threadprivate doesn't use stale data */
1312 KA_TRACE(10, ("__kmp_atfork_child: checking cache address list %p\n",
1313 __kmp_threadpriv_cache_list));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001314
Jonathan Peyton30419822017-05-12 18:01:32 +00001315 while (__kmp_threadpriv_cache_list != NULL) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001316
Jonathan Peyton30419822017-05-12 18:01:32 +00001317 if (*__kmp_threadpriv_cache_list->addr != NULL) {
1318 KC_TRACE(50, ("__kmp_atfork_child: zeroing cache at address %p\n",
1319 &(*__kmp_threadpriv_cache_list->addr)));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001320
Jonathan Peyton30419822017-05-12 18:01:32 +00001321 *__kmp_threadpriv_cache_list->addr = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001322 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001323 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list->next;
1324 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001325
Jonathan Peyton30419822017-05-12 18:01:32 +00001326 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001327
Jonathan Peyton30419822017-05-12 18:01:32 +00001328 /* reset statically initialized locks */
1329 __kmp_init_bootstrap_lock(&__kmp_initz_lock);
1330 __kmp_init_bootstrap_lock(&__kmp_stdio_lock);
1331 __kmp_init_bootstrap_lock(&__kmp_console_lock);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001332
Jonathan Peyton30419822017-05-12 18:01:32 +00001333 /* This is necessary to make sure no stale data is left around */
1334 /* AC: customers complain that we use unsafe routines in the atfork
1335 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1336 in dynamic_link when check the presence of shared tbbmalloc library.
1337 Suggestion is to make the library initialization lazier, similar
1338 to what done for __kmpc_begin(). */
1339 // TODO: synchronize all static initializations with regular library
1340 // startup; look at kmp_global.cpp and etc.
1341 //__kmp_internal_begin ();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001342}
1343
Jonathan Peyton30419822017-05-12 18:01:32 +00001344void __kmp_register_atfork(void) {
1345 if (__kmp_need_register_atfork) {
1346 int status = pthread_atfork(__kmp_atfork_prepare, __kmp_atfork_parent,
1347 __kmp_atfork_child);
1348 KMP_CHECK_SYSFAIL("pthread_atfork", status);
1349 __kmp_need_register_atfork = FALSE;
1350 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001351}
1352
Jonathan Peyton30419822017-05-12 18:01:32 +00001353void __kmp_suspend_initialize(void) {
1354 int status;
1355 status = pthread_mutexattr_init(&__kmp_suspend_mutex_attr);
1356 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status);
1357 status = pthread_condattr_init(&__kmp_suspend_cond_attr);
1358 KMP_CHECK_SYSFAIL("pthread_condattr_init", status);
1359}
1360
1361static void __kmp_suspend_initialize_thread(kmp_info_t *th) {
1362 ANNOTATE_HAPPENS_AFTER(&th->th.th_suspend_init_count);
1363 if (th->th.th_suspend_init_count <= __kmp_fork_count) {
1364 /* this means we haven't initialized the suspension pthread objects for this
1365 thread in this instance of the process */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001366 int status;
Jonathan Peyton30419822017-05-12 18:01:32 +00001367 status = pthread_cond_init(&th->th.th_suspend_cv.c_cond,
1368 &__kmp_suspend_cond_attr);
1369 KMP_CHECK_SYSFAIL("pthread_cond_init", status);
1370 status = pthread_mutex_init(&th->th.th_suspend_mx.m_mutex,
1371 &__kmp_suspend_mutex_attr);
1372 KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
1373 *(volatile int *)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1374 ANNOTATE_HAPPENS_BEFORE(&th->th.th_suspend_init_count);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001375 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001376}
1377
Jonathan Peyton30419822017-05-12 18:01:32 +00001378void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
1379 if (th->th.th_suspend_init_count > __kmp_fork_count) {
1380 /* this means we have initialize the suspension pthread objects for this
1381 thread in this instance of the process */
1382 int status;
1383
1384 status = pthread_cond_destroy(&th->th.th_suspend_cv.c_cond);
1385 if (status != 0 && status != EBUSY) {
1386 KMP_SYSFAIL("pthread_cond_destroy", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001387 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001388 status = pthread_mutex_destroy(&th->th.th_suspend_mx.m_mutex);
1389 if (status != 0 && status != EBUSY) {
1390 KMP_SYSFAIL("pthread_mutex_destroy", status);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001391 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001392 --th->th.th_suspend_init_count;
1393 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1394 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001395}
1396
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001397/* This routine puts the calling thread to sleep after setting the
Jonathan Peyton30419822017-05-12 18:01:32 +00001398 sleep bit for the indicated flag variable to true. */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001399template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +00001400static inline void __kmp_suspend_template(int th_gtid, C *flag) {
1401 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_suspend);
1402 kmp_info_t *th = __kmp_threads[th_gtid];
1403 int status;
1404 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001405
Jonathan Peyton30419822017-05-12 18:01:32 +00001406 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid,
1407 flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001408
Jonathan Peyton30419822017-05-12 18:01:32 +00001409 __kmp_suspend_initialize_thread(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001410
Jonathan Peyton30419822017-05-12 18:01:32 +00001411 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1412 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001413
Jonathan Peyton30419822017-05-12 18:01:32 +00001414 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1415 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001416
Jonathan Peyton30419822017-05-12 18:01:32 +00001417 /* TODO: shouldn't this use release semantics to ensure that
1418 __kmp_suspend_initialize_thread gets called first? */
1419 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001420
Jonathan Peyton30419822017-05-12 18:01:32 +00001421 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x,"
1422 " was %x\n",
1423 th_gtid, flag->get(), *(flag->get()), old_spin));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001424
Jonathan Peyton30419822017-05-12 18:01:32 +00001425 if (flag->done_check_val(old_spin)) {
1426 old_spin = flag->unset_sleeping();
1427 KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
1428 "for spin(%p)\n",
1429 th_gtid, flag->get()));
1430 } else {
1431 /* Encapsulate in a loop as the documentation states that this may
1432 "with low probability" return when the condition variable has
1433 not been signaled or broadcast */
1434 int deactivated = FALSE;
1435 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1436
1437 while (flag->is_sleeping()) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001438#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001439 char buffer[128];
1440 __kmp_suspend_count++;
1441 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1442 __kmp_printf("__kmp_suspend_template: suspending T#%d: %s\n", th_gtid,
1443 buffer);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001444#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001445 // Mark the thread as no longer active (only in the first iteration of the
1446 // loop).
1447 if (!deactivated) {
1448 th->th.th_active = FALSE;
1449 if (th->th.th_active_in_pool) {
1450 th->th.th_active_in_pool = FALSE;
Andrey Churbanov5ba90c72017-07-17 09:03:14 +00001451 KMP_TEST_THEN_DEC32(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +00001452 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
1453 }
1454 deactivated = TRUE;
1455 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001456
1457#if USE_SUSPEND_TIMEOUT
Jonathan Peyton30419822017-05-12 18:01:32 +00001458 struct timespec now;
1459 struct timeval tval;
1460 int msecs;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001461
Jonathan Peyton30419822017-05-12 18:01:32 +00001462 status = gettimeofday(&tval, NULL);
1463 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1464 TIMEVAL_TO_TIMESPEC(&tval, &now);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001465
Jonathan Peyton30419822017-05-12 18:01:32 +00001466 msecs = (4 * __kmp_dflt_blocktime) + 200;
1467 now.tv_sec += msecs / 1000;
1468 now.tv_nsec += (msecs % 1000) * 1000;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001469
Jonathan Peyton30419822017-05-12 18:01:32 +00001470 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
1471 "pthread_cond_timedwait\n",
1472 th_gtid));
1473 status = pthread_cond_timedwait(&th->th.th_suspend_cv.c_cond,
1474 &th->th.th_suspend_mx.m_mutex, &now);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001475#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001476 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform"
1477 " pthread_cond_wait\n",
1478 th_gtid));
1479 status = pthread_cond_wait(&th->th.th_suspend_cv.c_cond,
1480 &th->th.th_suspend_mx.m_mutex);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001481#endif
1482
Jonathan Peyton30419822017-05-12 18:01:32 +00001483 if ((status != 0) && (status != EINTR) && (status != ETIMEDOUT)) {
1484 KMP_SYSFAIL("pthread_cond_wait", status);
1485 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001486#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001487 if (status == ETIMEDOUT) {
1488 if (flag->is_sleeping()) {
1489 KF_TRACE(100,
1490 ("__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid));
1491 } else {
1492 KF_TRACE(2, ("__kmp_suspend_template: T#%d timeout wakeup, sleep bit "
1493 "not set!\n",
1494 th_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001495 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001496 } else if (flag->is_sleeping()) {
1497 KF_TRACE(100,
1498 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
1499 }
1500#endif
1501 } // while
Jim Cownie5e8470a2013-09-27 10:38:44 +00001502
Jonathan Peyton30419822017-05-12 18:01:32 +00001503 // Mark the thread as active again (if it was previous marked as inactive)
1504 if (deactivated) {
1505 th->th.th_active = TRUE;
1506 if (TCR_4(th->th.th_in_pool)) {
Andrey Churbanov5ba90c72017-07-17 09:03:14 +00001507 KMP_TEST_THEN_INC32(&__kmp_thread_pool_active_nth);
Jonathan Peyton30419822017-05-12 18:01:32 +00001508 th->th.th_active_in_pool = TRUE;
1509 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001510 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001511 }
1512#ifdef DEBUG_SUSPEND
1513 {
1514 char buffer[128];
1515 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1516 __kmp_printf("__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid,
1517 buffer);
1518 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001519#endif
1520
Jonathan Peyton30419822017-05-12 18:01:32 +00001521 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1522 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1523 KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001524}
1525
1526void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001527 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001528}
1529void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001530 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001531}
1532void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001533 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001534}
1535
Jim Cownie5e8470a2013-09-27 10:38:44 +00001536/* This routine signals the thread specified by target_gtid to wake up
Jonathan Peyton30419822017-05-12 18:01:32 +00001537 after setting the sleep bit indicated by the flag argument to FALSE.
1538 The target thread must already have called __kmp_suspend_template() */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001539template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +00001540static inline void __kmp_resume_template(int target_gtid, C *flag) {
1541 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1542 kmp_info_t *th = __kmp_threads[target_gtid];
1543 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001544
1545#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001546 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001547#endif
1548
Jonathan Peyton30419822017-05-12 18:01:32 +00001549 KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
1550 gtid, target_gtid));
1551 KMP_DEBUG_ASSERT(gtid != target_gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001552
Jonathan Peyton30419822017-05-12 18:01:32 +00001553 __kmp_suspend_initialize_thread(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001554
Jonathan Peyton30419822017-05-12 18:01:32 +00001555 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1556 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001557
Jonathan Peyton30419822017-05-12 18:01:32 +00001558 if (!flag) { // coming from __kmp_null_resume_wrapper
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00001559 flag = (C *)CCAST(void *, th->th.th_sleep_loc);
Jonathan Peyton30419822017-05-12 18:01:32 +00001560 }
1561
1562 // First, check if the flag is null or its type has changed. If so, someone
1563 // else woke it up.
1564 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type
1565 // simply shows what
1566 // flag was cast to
1567 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1568 "awake: flag(%p)\n",
1569 gtid, target_gtid, NULL));
1570 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1571 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1572 return;
1573 } else { // if multiple threads are sleeping, flag should be internally
1574 // referring to a specific thread here
1575 typename C::flag_t old_spin = flag->unset_sleeping();
1576 if (!flag->is_sleeping_val(old_spin)) {
1577 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1578 "awake: flag(%p): "
1579 "%u => %u\n",
1580 gtid, target_gtid, flag->get(), old_spin, *flag->get()));
1581 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1582 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1583 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001584 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001585 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset "
1586 "sleep bit for flag's loc(%p): "
1587 "%u => %u\n",
1588 gtid, target_gtid, flag->get(), old_spin, *flag->get()));
1589 }
1590 TCW_PTR(th->th.th_sleep_loc, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001591
1592#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001593 {
1594 char buffer[128];
1595 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1596 __kmp_printf("__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid,
1597 target_gtid, buffer);
1598 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001599#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001600 status = pthread_cond_signal(&th->th.th_suspend_cv.c_cond);
1601 KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
1602 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1603 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1604 KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
1605 " for T#%d\n",
1606 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001607}
1608
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001609void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001610 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001611}
1612void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001613 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001614}
1615void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001616 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001617}
1618
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001619#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001620void __kmp_resume_monitor() {
1621 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1622 int status;
Jim Cownie07ea89f2014-09-03 11:10:54 +00001623#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001624 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1625 KF_TRACE(30, ("__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n", gtid,
1626 KMP_GTID_MONITOR));
1627 KMP_DEBUG_ASSERT(gtid != KMP_GTID_MONITOR);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001628#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001629 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
1630 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001631#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001632 {
1633 char buffer[128];
1634 __kmp_print_cond(buffer, &__kmp_wait_cv.c_cond);
1635 __kmp_printf("__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid,
1636 KMP_GTID_MONITOR, buffer);
1637 }
Jim Cownie07ea89f2014-09-03 11:10:54 +00001638#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001639 status = pthread_cond_signal(&__kmp_wait_cv.c_cond);
1640 KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
1641 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
1642 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1643 KF_TRACE(30, ("__kmp_resume_monitor: T#%d exiting after signaling wake up"
1644 " for T#%d\n",
1645 gtid, KMP_GTID_MONITOR));
Jim Cownie07ea89f2014-09-03 11:10:54 +00001646}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001647#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001648
Jonathan Peyton30419822017-05-12 18:01:32 +00001649void __kmp_yield(int cond) {
1650 if (!cond)
1651 return;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001652#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001653 if (!__kmp_yielding_on)
1654 return;
Jonathan Peyton581fdba2017-02-15 17:19:21 +00001655#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001656 if (__kmp_yield_cycle && !KMP_YIELD_NOW())
1657 return;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001658#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001659 sched_yield();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001660}
1661
Jonathan Peyton30419822017-05-12 18:01:32 +00001662void __kmp_gtid_set_specific(int gtid) {
1663 if (__kmp_init_gtid) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001664 int status;
Jonathan Peyton30419822017-05-12 18:01:32 +00001665 status = pthread_setspecific(__kmp_gtid_threadprivate_key,
1666 (void *)(intptr_t)(gtid + 1));
1667 KMP_CHECK_SYSFAIL("pthread_setspecific", status);
1668 } else {
1669 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
1670 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001671}
1672
Jonathan Peyton30419822017-05-12 18:01:32 +00001673int __kmp_gtid_get_specific() {
1674 int gtid;
1675 if (!__kmp_init_gtid) {
1676 KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
1677 "KMP_GTID_SHUTDOWN\n"));
1678 return KMP_GTID_SHUTDOWN;
1679 }
1680 gtid = (int)(size_t)pthread_getspecific(__kmp_gtid_threadprivate_key);
1681 if (gtid == 0) {
1682 gtid = KMP_GTID_DNE;
1683 } else {
1684 gtid--;
1685 }
1686 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1687 __kmp_gtid_threadprivate_key, gtid));
1688 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001689}
1690
Jonathan Peyton30419822017-05-12 18:01:32 +00001691double __kmp_read_cpu_time(void) {
1692 /*clock_t t;*/
1693 struct tms buffer;
1694
1695 /*t =*/times(&buffer);
1696
1697 return (buffer.tms_utime + buffer.tms_cutime) / (double)CLOCKS_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001698}
1699
Jonathan Peyton30419822017-05-12 18:01:32 +00001700int __kmp_read_system_info(struct kmp_sys_info *info) {
1701 int status;
1702 struct rusage r_usage;
1703
1704 memset(info, 0, sizeof(*info));
1705
1706 status = getrusage(RUSAGE_SELF, &r_usage);
1707 KMP_CHECK_SYSFAIL_ERRNO("getrusage", status);
1708
1709 // The maximum resident set size utilized (in kilobytes)
1710 info->maxrss = r_usage.ru_maxrss;
1711 // The number of page faults serviced without any I/O
1712 info->minflt = r_usage.ru_minflt;
1713 // The number of page faults serviced that required I/O
1714 info->majflt = r_usage.ru_majflt;
1715 // The number of times a process was "swapped" out of memory
1716 info->nswap = r_usage.ru_nswap;
1717 // The number of times the file system had to perform input
1718 info->inblock = r_usage.ru_inblock;
1719 // The number of times the file system had to perform output
1720 info->oublock = r_usage.ru_oublock;
1721 // The number of times a context switch was voluntarily
1722 info->nvcsw = r_usage.ru_nvcsw;
1723 // The number of times a context switch was forced
1724 info->nivcsw = r_usage.ru_nivcsw;
1725
1726 return (status != 0);
1727}
1728
1729void __kmp_read_system_time(double *delta) {
1730 double t_ns;
1731 struct timeval tval;
1732 struct timespec stop;
1733 int status;
1734
1735 status = gettimeofday(&tval, NULL);
1736 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1737 TIMEVAL_TO_TIMESPEC(&tval, &stop);
1738 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
1739 *delta = (t_ns * 1e-9);
1740}
1741
1742void __kmp_clear_system_time(void) {
1743 struct timeval tval;
1744 int status;
1745 status = gettimeofday(&tval, NULL);
1746 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1747 TIMEVAL_TO_TIMESPEC(&tval, &__kmp_sys_timer_data.start);
1748}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001749
Jonathan Peyton30419822017-05-12 18:01:32 +00001750static int __kmp_get_xproc(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001751
Jonathan Peyton30419822017-05-12 18:01:32 +00001752 int r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001753
Jonathan Peyton30419822017-05-12 18:01:32 +00001754#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00001755
Jonathan Peyton30419822017-05-12 18:01:32 +00001756 r = sysconf(_SC_NPROCESSORS_ONLN);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001757
Jonathan Peyton30419822017-05-12 18:01:32 +00001758#elif KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00001759
Jonathan Peyton30419822017-05-12 18:01:32 +00001760 // Bug C77011 High "OpenMP Threads and number of active cores".
Jim Cownie5e8470a2013-09-27 10:38:44 +00001761
Jonathan Peyton30419822017-05-12 18:01:32 +00001762 // Find the number of available CPUs.
1763 kern_return_t rc;
1764 host_basic_info_data_t info;
1765 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
1766 rc = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&info, &num);
1767 if (rc == 0 && num == HOST_BASIC_INFO_COUNT) {
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00001768 // Cannot use KA_TRACE() here because this code works before trace support
1769 // is initialized.
Jonathan Peyton30419822017-05-12 18:01:32 +00001770 r = info.avail_cpus;
1771 } else {
1772 KMP_WARNING(CantGetNumAvailCPU);
1773 KMP_INFORM(AssumedNumCPU);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001774 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001775
Jonathan Peyton30419822017-05-12 18:01:32 +00001776#else
Jim Cownie5e8470a2013-09-27 10:38:44 +00001777
Jonathan Peyton30419822017-05-12 18:01:32 +00001778#error "Unknown or unsupported OS."
Jim Cownie5e8470a2013-09-27 10:38:44 +00001779
Jonathan Peyton30419822017-05-12 18:01:32 +00001780#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001781
Jonathan Peyton30419822017-05-12 18:01:32 +00001782 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001783
1784} // __kmp_get_xproc
1785
Jonathan Peyton30419822017-05-12 18:01:32 +00001786int __kmp_read_from_file(char const *path, char const *format, ...) {
1787 int result;
1788 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001789
Jonathan Peyton30419822017-05-12 18:01:32 +00001790 va_start(args, format);
1791 FILE *f = fopen(path, "rb");
1792 if (f == NULL)
1793 return 0;
1794 result = vfscanf(f, format, args);
1795 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001796
Jonathan Peyton30419822017-05-12 18:01:32 +00001797 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00001798}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001799
Jonathan Peyton30419822017-05-12 18:01:32 +00001800void __kmp_runtime_initialize(void) {
1801 int status;
1802 pthread_mutexattr_t mutex_attr;
1803 pthread_condattr_t cond_attr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001804
Jonathan Peyton30419822017-05-12 18:01:32 +00001805 if (__kmp_init_runtime) {
1806 return;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001807 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001808
Jonathan Peyton30419822017-05-12 18:01:32 +00001809#if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
1810 if (!__kmp_cpuinfo.initialized) {
1811 __kmp_query_cpuid(&__kmp_cpuinfo);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001812 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001813#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001814
Jonathan Peyton30419822017-05-12 18:01:32 +00001815 __kmp_xproc = __kmp_get_xproc();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001816
Jonathan Peyton30419822017-05-12 18:01:32 +00001817 if (sysconf(_SC_THREADS)) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001818
Jonathan Peyton30419822017-05-12 18:01:32 +00001819 /* Query the maximum number of threads */
1820 __kmp_sys_max_nth = sysconf(_SC_THREAD_THREADS_MAX);
1821 if (__kmp_sys_max_nth == -1) {
1822 /* Unlimited threads for NPTL */
1823 __kmp_sys_max_nth = INT_MAX;
1824 } else if (__kmp_sys_max_nth <= 1) {
1825 /* Can't tell, just use PTHREAD_THREADS_MAX */
1826 __kmp_sys_max_nth = KMP_MAX_NTH;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001827 }
1828
Jonathan Peyton30419822017-05-12 18:01:32 +00001829 /* Query the minimum stack size */
1830 __kmp_sys_min_stksize = sysconf(_SC_THREAD_STACK_MIN);
1831 if (__kmp_sys_min_stksize <= 1) {
1832 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
1833 }
1834 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001835
Jonathan Peyton30419822017-05-12 18:01:32 +00001836 /* Set up minimum number of threads to switch to TLS gtid */
1837 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001838
Jonathan Peyton30419822017-05-12 18:01:32 +00001839 status = pthread_key_create(&__kmp_gtid_threadprivate_key,
1840 __kmp_internal_end_dest);
1841 KMP_CHECK_SYSFAIL("pthread_key_create", status);
1842 status = pthread_mutexattr_init(&mutex_attr);
1843 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status);
1844 status = pthread_mutex_init(&__kmp_wait_mx.m_mutex, &mutex_attr);
1845 KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
1846 status = pthread_condattr_init(&cond_attr);
1847 KMP_CHECK_SYSFAIL("pthread_condattr_init", status);
1848 status = pthread_cond_init(&__kmp_wait_cv.c_cond, &cond_attr);
1849 KMP_CHECK_SYSFAIL("pthread_cond_init", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001850#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001851 __kmp_itt_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001852#endif /* USE_ITT_BUILD */
1853
Jonathan Peyton30419822017-05-12 18:01:32 +00001854 __kmp_init_runtime = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001855}
1856
Jonathan Peyton30419822017-05-12 18:01:32 +00001857void __kmp_runtime_destroy(void) {
1858 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001859
Jonathan Peyton30419822017-05-12 18:01:32 +00001860 if (!__kmp_init_runtime) {
1861 return; // Nothing to do.
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001862 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001863
1864#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001865 __kmp_itt_destroy();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001866#endif /* USE_ITT_BUILD */
1867
Jonathan Peyton30419822017-05-12 18:01:32 +00001868 status = pthread_key_delete(__kmp_gtid_threadprivate_key);
1869 KMP_CHECK_SYSFAIL("pthread_key_delete", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001870
Jonathan Peyton30419822017-05-12 18:01:32 +00001871 status = pthread_mutex_destroy(&__kmp_wait_mx.m_mutex);
1872 if (status != 0 && status != EBUSY) {
1873 KMP_SYSFAIL("pthread_mutex_destroy", status);
1874 }
1875 status = pthread_cond_destroy(&__kmp_wait_cv.c_cond);
1876 if (status != 0 && status != EBUSY) {
1877 KMP_SYSFAIL("pthread_cond_destroy", status);
1878 }
1879#if KMP_AFFINITY_SUPPORTED
1880 __kmp_affinity_uninitialize();
1881#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001882
Jonathan Peyton30419822017-05-12 18:01:32 +00001883 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001884}
1885
Jim Cownie5e8470a2013-09-27 10:38:44 +00001886/* Put the thread to sleep for a time period */
1887/* NOTE: not currently used anywhere */
Jonathan Peyton30419822017-05-12 18:01:32 +00001888void __kmp_thread_sleep(int millis) { sleep((millis + 500) / 1000); }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001889
1890/* Calculate the elapsed wall clock time for the user */
Jonathan Peyton30419822017-05-12 18:01:32 +00001891void __kmp_elapsed(double *t) {
1892 int status;
1893#ifdef FIX_SGI_CLOCK
1894 struct timespec ts;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001895
Jonathan Peyton30419822017-05-12 18:01:32 +00001896 status = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
1897 KMP_CHECK_SYSFAIL_ERRNO("clock_gettime", status);
1898 *t =
1899 (double)ts.tv_nsec * (1.0 / (double)KMP_NSEC_PER_SEC) + (double)ts.tv_sec;
1900#else
1901 struct timeval tv;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001902
Jonathan Peyton30419822017-05-12 18:01:32 +00001903 status = gettimeofday(&tv, NULL);
1904 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1905 *t =
1906 (double)tv.tv_usec * (1.0 / (double)KMP_USEC_PER_SEC) + (double)tv.tv_sec;
1907#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001908}
1909
1910/* Calculate the elapsed wall clock tick for the user */
Jonathan Peyton30419822017-05-12 18:01:32 +00001911void __kmp_elapsed_tick(double *t) { *t = 1 / (double)CLOCKS_PER_SEC; }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001912
Jonathan Peyton377aa402016-04-14 16:00:37 +00001913/* Return the current time stamp in nsec */
Jonathan Peyton30419822017-05-12 18:01:32 +00001914kmp_uint64 __kmp_now_nsec() {
1915 struct timeval t;
1916 gettimeofday(&t, NULL);
1917 return KMP_NSEC_PER_SEC * t.tv_sec + 1000 * t.tv_usec;
Jonathan Peyton377aa402016-04-14 16:00:37 +00001918}
1919
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001920#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton35d75ae2017-03-20 22:11:31 +00001921/* Measure clock ticks per millisecond */
Jonathan Peyton30419822017-05-12 18:01:32 +00001922void __kmp_initialize_system_tick() {
1923 kmp_uint64 delay = 100000; // 50~100 usec on most machines.
1924 kmp_uint64 nsec = __kmp_now_nsec();
1925 kmp_uint64 goal = __kmp_hardware_timestamp() + delay;
1926 kmp_uint64 now;
1927 while ((now = __kmp_hardware_timestamp()) < goal)
1928 ;
1929 __kmp_ticks_per_msec =
1930 (kmp_uint64)(1e6 * (delay + (now - goal)) / (__kmp_now_nsec() - nsec));
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001931}
1932#endif
1933
Jonathan Peyton30419822017-05-12 18:01:32 +00001934/* Determine whether the given address is mapped into the current address
1935 space. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001936
Jonathan Peyton30419822017-05-12 18:01:32 +00001937int __kmp_is_address_mapped(void *addr) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001938
Jonathan Peyton30419822017-05-12 18:01:32 +00001939 int found = 0;
1940 int rc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001941
Jonathan Peyton30419822017-05-12 18:01:32 +00001942#if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00001943
Jonathan Peyton30419822017-05-12 18:01:32 +00001944 /* On Linux* OS, read the /proc/<pid>/maps pseudo-file to get all the address
1945 ranges mapped into the address space. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001946
Jonathan Peyton30419822017-05-12 18:01:32 +00001947 char *name = __kmp_str_format("/proc/%d/maps", getpid());
1948 FILE *file = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001949
Jonathan Peyton30419822017-05-12 18:01:32 +00001950 file = fopen(name, "r");
1951 KMP_ASSERT(file != NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001952
Jonathan Peyton30419822017-05-12 18:01:32 +00001953 for (;;) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001954
Jonathan Peyton30419822017-05-12 18:01:32 +00001955 void *beginning = NULL;
1956 void *ending = NULL;
1957 char perms[5];
Jim Cownie5e8470a2013-09-27 10:38:44 +00001958
Jonathan Peyton30419822017-05-12 18:01:32 +00001959 rc = fscanf(file, "%p-%p %4s %*[^\n]\n", &beginning, &ending, perms);
1960 if (rc == EOF) {
1961 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001962 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001963 KMP_ASSERT(rc == 3 &&
1964 KMP_STRLEN(perms) == 4); // Make sure all fields are read.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001965
Jonathan Peyton30419822017-05-12 18:01:32 +00001966 // Ending address is not included in the region, but beginning is.
1967 if ((addr >= beginning) && (addr < ending)) {
1968 perms[2] = 0; // 3th and 4th character does not matter.
1969 if (strcmp(perms, "rw") == 0) {
1970 // Memory we are looking for should be readable and writable.
Alp Toker763b9392014-02-28 09:42:41 +00001971 found = 1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001972 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001973 break;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001974 }
1975 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001976
Jonathan Peyton30419822017-05-12 18:01:32 +00001977 // Free resources.
1978 fclose(file);
1979 KMP_INTERNAL_FREE(name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001980
Jonathan Peyton30419822017-05-12 18:01:32 +00001981#elif KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00001982
Jonathan Peyton30419822017-05-12 18:01:32 +00001983 /* On OS X*, /proc pseudo filesystem is not available. Try to read memory
1984 using vm interface. */
1985
1986 int buffer;
1987 vm_size_t count;
1988 rc = vm_read_overwrite(
1989 mach_task_self(), // Task to read memory of.
1990 (vm_address_t)(addr), // Address to read from.
1991 1, // Number of bytes to be read.
1992 (vm_address_t)(&buffer), // Address of buffer to save read bytes in.
1993 &count // Address of var to save number of read bytes in.
1994 );
1995 if (rc == 0) {
1996 // Memory successfully read.
1997 found = 1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00001998 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001999
2000#elif KMP_OS_FREEBSD || KMP_OS_NETBSD
2001
2002 // FIXME(FreeBSD, NetBSD): Implement this
2003 found = 1;
2004
2005#else
2006
2007#error "Unknown or unsupported OS"
2008
2009#endif
2010
2011 return found;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002012
2013} // __kmp_is_address_mapped
2014
2015#ifdef USE_LOAD_BALANCE
2016
Jonathan Peyton30419822017-05-12 18:01:32 +00002017#if KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002018
2019// The function returns the rounded value of the system load average
2020// during given time interval which depends on the value of
2021// __kmp_load_balance_interval variable (default is 60 sec, other values
2022// may be 300 sec or 900 sec).
2023// It returns -1 in case of error.
Jonathan Peyton30419822017-05-12 18:01:32 +00002024int __kmp_get_load_balance(int max) {
2025 double averages[3];
2026 int ret_avg = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002027
Jonathan Peyton30419822017-05-12 18:01:32 +00002028 int res = getloadavg(averages, 3);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002029
Jonathan Peyton30419822017-05-12 18:01:32 +00002030 // Check __kmp_load_balance_interval to determine which of averages to use.
2031 // getloadavg() may return the number of samples less than requested that is
2032 // less than 3.
2033 if (__kmp_load_balance_interval < 180 && (res >= 1)) {
2034 ret_avg = averages[0]; // 1 min
2035 } else if ((__kmp_load_balance_interval >= 180 &&
2036 __kmp_load_balance_interval < 600) &&
2037 (res >= 2)) {
2038 ret_avg = averages[1]; // 5 min
2039 } else if ((__kmp_load_balance_interval >= 600) && (res == 3)) {
2040 ret_avg = averages[2]; // 15 min
2041 } else { // Error occurred
2042 return -1;
2043 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002044
Jonathan Peyton30419822017-05-12 18:01:32 +00002045 return ret_avg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002046}
2047
Jonathan Peyton30419822017-05-12 18:01:32 +00002048#else // Linux* OS
Jim Cownie5e8470a2013-09-27 10:38:44 +00002049
Jonathan Peyton30419822017-05-12 18:01:32 +00002050// The fuction returns number of running (not sleeping) threads, or -1 in case
2051// of error. Error could be reported if Linux* OS kernel too old (without
2052// "/proc" support). Counting running threads stops if max running threads
2053// encountered.
2054int __kmp_get_load_balance(int max) {
2055 static int permanent_error = 0;
2056 static int glb_running_threads = 0; // Saved count of the running threads for
2057 // the thread balance algortihm
2058 static double glb_call_time = 0; /* Thread balance algorithm call time */
Jim Cownie5e8470a2013-09-27 10:38:44 +00002059
Jonathan Peyton30419822017-05-12 18:01:32 +00002060 int running_threads = 0; // Number of running threads in the system.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002061
Jonathan Peyton30419822017-05-12 18:01:32 +00002062 DIR *proc_dir = NULL; // Handle of "/proc/" directory.
2063 struct dirent *proc_entry = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002064
Jonathan Peyton30419822017-05-12 18:01:32 +00002065 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2066 DIR *task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2067 struct dirent *task_entry = NULL;
2068 int task_path_fixed_len;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002069
Jonathan Peyton30419822017-05-12 18:01:32 +00002070 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2071 int stat_file = -1;
2072 int stat_path_fixed_len;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002073
Jonathan Peyton30419822017-05-12 18:01:32 +00002074 int total_processes = 0; // Total number of processes in system.
2075 int total_threads = 0; // Total number of threads in system.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002076
Jonathan Peyton30419822017-05-12 18:01:32 +00002077 double call_time = 0.0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002078
Jonathan Peyton30419822017-05-12 18:01:32 +00002079 __kmp_str_buf_init(&task_path);
2080 __kmp_str_buf_init(&stat_path);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002081
Jonathan Peyton30419822017-05-12 18:01:32 +00002082 __kmp_elapsed(&call_time);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002083
Jonathan Peyton30419822017-05-12 18:01:32 +00002084 if (glb_call_time &&
2085 (call_time - glb_call_time < __kmp_load_balance_interval)) {
2086 running_threads = glb_running_threads;
2087 goto finish;
2088 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002089
Jonathan Peyton30419822017-05-12 18:01:32 +00002090 glb_call_time = call_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002091
Jonathan Peyton30419822017-05-12 18:01:32 +00002092 // Do not spend time on scanning "/proc/" if we have a permanent error.
2093 if (permanent_error) {
2094 running_threads = -1;
2095 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002096 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002097
Jonathan Peyton30419822017-05-12 18:01:32 +00002098 if (max <= 0) {
2099 max = INT_MAX;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002100 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002101
Jonathan Peyton30419822017-05-12 18:01:32 +00002102 // Open "/proc/" directory.
2103 proc_dir = opendir("/proc");
2104 if (proc_dir == NULL) {
2105 // Cannot open "/prroc/". Probably the kernel does not support it. Return an
2106 // error now and in subsequent calls.
2107 running_threads = -1;
2108 permanent_error = 1;
2109 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002110 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002111
Jonathan Peyton30419822017-05-12 18:01:32 +00002112 // Initialize fixed part of task_path. This part will not change.
2113 __kmp_str_buf_cat(&task_path, "/proc/", 6);
2114 task_path_fixed_len = task_path.used; // Remember number of used characters.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002115
Jonathan Peyton30419822017-05-12 18:01:32 +00002116 proc_entry = readdir(proc_dir);
2117 while (proc_entry != NULL) {
2118 // Proc entry is a directory and name starts with a digit. Assume it is a
2119 // process' directory.
2120 if (proc_entry->d_type == DT_DIR && isdigit(proc_entry->d_name[0])) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002121
Jonathan Peyton30419822017-05-12 18:01:32 +00002122 ++total_processes;
2123 // Make sure init process is the very first in "/proc", so we can replace
2124 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes ==
2125 // 1. We are going to check that total_processes == 1 => d_name == "1" is
2126 // true (where "=>" is implication). Since C++ does not have => operator,
2127 // let us replace it with its equivalent: a => b == ! a || b.
2128 KMP_DEBUG_ASSERT(total_processes != 1 ||
2129 strcmp(proc_entry->d_name, "1") == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002130
Jonathan Peyton30419822017-05-12 18:01:32 +00002131 // Construct task_path.
2132 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
2133 __kmp_str_buf_cat(&task_path, proc_entry->d_name,
2134 KMP_STRLEN(proc_entry->d_name));
2135 __kmp_str_buf_cat(&task_path, "/task", 5);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002136
Jonathan Peyton30419822017-05-12 18:01:32 +00002137 task_dir = opendir(task_path.str);
2138 if (task_dir == NULL) {
2139 // Process can finish between reading "/proc/" directory entry and
2140 // opening process' "task/" directory. So, in general case we should not
2141 // complain, but have to skip this process and read the next one. But on
2142 // systems with no "task/" support we will spend lot of time to scan
2143 // "/proc/" tree again and again without any benefit. "init" process
2144 // (its pid is 1) should exist always, so, if we cannot open
2145 // "/proc/1/task/" directory, it means "task/" is not supported by
2146 // kernel. Report an error now and in the future.
2147 if (strcmp(proc_entry->d_name, "1") == 0) {
2148 running_threads = -1;
2149 permanent_error = 1;
2150 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002151 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002152 } else {
2153 // Construct fixed part of stat file path.
2154 __kmp_str_buf_clear(&stat_path);
2155 __kmp_str_buf_cat(&stat_path, task_path.str, task_path.used);
2156 __kmp_str_buf_cat(&stat_path, "/", 1);
2157 stat_path_fixed_len = stat_path.used;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002158
Jonathan Peyton30419822017-05-12 18:01:32 +00002159 task_entry = readdir(task_dir);
2160 while (task_entry != NULL) {
2161 // It is a directory and name starts with a digit.
2162 if (proc_entry->d_type == DT_DIR && isdigit(task_entry->d_name[0])) {
2163 ++total_threads;
2164
2165 // Consruct complete stat file path. Easiest way would be:
2166 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str,
2167 // task_entry->d_name );
2168 // but seriae of __kmp_str_buf_cat works a bit faster.
2169 stat_path.used =
2170 stat_path_fixed_len; // Reset stat path to its fixed part.
2171 __kmp_str_buf_cat(&stat_path, task_entry->d_name,
2172 KMP_STRLEN(task_entry->d_name));
2173 __kmp_str_buf_cat(&stat_path, "/stat", 5);
2174
2175 // Note: Low-level API (open/read/close) is used. High-level API
2176 // (fopen/fclose) works ~ 30 % slower.
2177 stat_file = open(stat_path.str, O_RDONLY);
2178 if (stat_file == -1) {
2179 // We cannot report an error because task (thread) can terminate
2180 // just before reading this file.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002181 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002182 /* Content of "stat" file looks like:
2183 24285 (program) S ...
Jim Cownie5e8470a2013-09-27 10:38:44 +00002184
Jonathan Peyton30419822017-05-12 18:01:32 +00002185 It is a single line (if program name does not include funny
2186 symbols). First number is a thread id, then name of executable
2187 file name in paretheses, then state of the thread. We need just
2188 thread state.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002189
Jonathan Peyton30419822017-05-12 18:01:32 +00002190 Good news: Length of program name is 15 characters max. Longer
2191 names are truncated.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002192
Jonathan Peyton30419822017-05-12 18:01:32 +00002193 Thus, we need rather short buffer: 15 chars for program name +
2194 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002195
Jonathan Peyton30419822017-05-12 18:01:32 +00002196 Bad news: Program name may contain special symbols like space,
2197 closing parenthesis, or even new line. This makes parsing
2198 "stat" file not 100 % reliable. In case of fanny program names
2199 parsing may fail (report incorrect thread state).
Jim Cownie5e8470a2013-09-27 10:38:44 +00002200
Jonathan Peyton30419822017-05-12 18:01:32 +00002201 Parsing "status" file looks more promissing (due to different
2202 file structure and escaping special symbols) but reading and
2203 parsing of "status" file works slower.
2204 -- ln
2205 */
2206 char buffer[65];
2207 int len;
2208 len = read(stat_file, buffer, sizeof(buffer) - 1);
2209 if (len >= 0) {
2210 buffer[len] = 0;
2211 // Using scanf:
2212 // sscanf( buffer, "%*d (%*s) %c ", & state );
2213 // looks very nice, but searching for a closing parenthesis
2214 // works a bit faster.
2215 char *close_parent = strstr(buffer, ") ");
2216 if (close_parent != NULL) {
2217 char state = *(close_parent + 2);
2218 if (state == 'R') {
2219 ++running_threads;
2220 if (running_threads >= max) {
2221 goto finish;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002222 }
2223 }
2224 }
2225 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002226 close(stat_file);
2227 stat_file = -1;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002228 }
2229 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002230 task_entry = readdir(task_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002231 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002232 closedir(task_dir);
2233 task_dir = NULL;
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002234 }
2235 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002236 proc_entry = readdir(proc_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002237 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002238
Jonathan Peyton30419822017-05-12 18:01:32 +00002239 // There _might_ be a timing hole where the thread executing this
2240 // code get skipped in the load balance, and running_threads is 0.
2241 // Assert in the debug builds only!!!
2242 KMP_DEBUG_ASSERT(running_threads > 0);
2243 if (running_threads <= 0) {
2244 running_threads = 1;
2245 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002246
Jonathan Peyton30419822017-05-12 18:01:32 +00002247finish: // Clean up and exit.
2248 if (proc_dir != NULL) {
2249 closedir(proc_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002250 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002251 __kmp_str_buf_free(&task_path);
2252 if (task_dir != NULL) {
2253 closedir(task_dir);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002254 }
Jonathan Peyton30419822017-05-12 18:01:32 +00002255 __kmp_str_buf_free(&stat_path);
2256 if (stat_file != -1) {
2257 close(stat_file);
Jonathan Peytonbd3a7632017-09-27 20:36:27 +00002258 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002259
Jonathan Peyton30419822017-05-12 18:01:32 +00002260 glb_running_threads = running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002261
Jonathan Peyton30419822017-05-12 18:01:32 +00002262 return running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002263
2264} // __kmp_get_load_balance
2265
Jonathan Peyton30419822017-05-12 18:01:32 +00002266#endif // KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002267
2268#endif // USE_LOAD_BALANCE
2269
Andrey Churbanovc47afcd2017-07-03 11:24:08 +00002270#if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \
2271 ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || KMP_ARCH_PPC64)
Jim Cownie3051f972014-08-07 10:12:54 +00002272
2273// we really only need the case with 1 argument, because CLANG always build
2274// a struct of pointers to shared variables referenced in the outlined function
Jonathan Peyton30419822017-05-12 18:01:32 +00002275int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int tid, int argc,
2276 void *p_argv[]
Jonathan Peyton122dd762015-07-13 18:55:45 +00002277#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00002278 ,
2279 void **exit_frame_ptr
Jonathan Peyton122dd762015-07-13 18:55:45 +00002280#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002281 ) {
Jonathan Peyton122dd762015-07-13 18:55:45 +00002282#if OMPT_SUPPORT
Joachim Protze82e94a52017-11-01 10:08:30 +00002283 *exit_frame_ptr = OMPT_GET_FRAME_ADDRESS(0);
Jonathan Peyton122dd762015-07-13 18:55:45 +00002284#endif
2285
Jim Cownie3051f972014-08-07 10:12:54 +00002286 switch (argc) {
2287 default:
2288 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2289 fflush(stderr);
2290 exit(-1);
2291 case 0:
2292 (*pkfn)(&gtid, &tid);
2293 break;
2294 case 1:
2295 (*pkfn)(&gtid, &tid, p_argv[0]);
2296 break;
2297 case 2:
2298 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2299 break;
2300 case 3:
2301 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2302 break;
2303 case 4:
2304 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2305 break;
2306 case 5:
2307 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2308 break;
2309 case 6:
2310 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2311 p_argv[5]);
2312 break;
2313 case 7:
2314 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2315 p_argv[5], p_argv[6]);
2316 break;
2317 case 8:
2318 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2319 p_argv[5], p_argv[6], p_argv[7]);
2320 break;
2321 case 9:
2322 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2323 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2324 break;
2325 case 10:
2326 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2327 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2328 break;
2329 case 11:
2330 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2331 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2332 break;
2333 case 12:
2334 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2335 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2336 p_argv[11]);
2337 break;
2338 case 13:
2339 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2340 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2341 p_argv[11], p_argv[12]);
2342 break;
2343 case 14:
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], p_argv[10],
2346 p_argv[11], p_argv[12], p_argv[13]);
2347 break;
2348 case 15:
2349 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2350 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2351 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2352 break;
2353 }
2354
Jonathan Peyton122dd762015-07-13 18:55:45 +00002355#if OMPT_SUPPORT
2356 *exit_frame_ptr = 0;
2357#endif
2358
Jim Cownie3051f972014-08-07 10:12:54 +00002359 return 1;
2360}
2361
2362#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002363
Jim Cownie5e8470a2013-09-27 10:38:44 +00002364// end of file //