blob: a0d1639c53ebaad61f7bf7aed271080cfc6c8a2f [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
5
6//===----------------------------------------------------------------------===//
7//
8// The LLVM Compiler Infrastructure
9//
10// This file is dual licensed under the MIT and the University of Illinois Open
11// Source Licenses. See LICENSE.txt for details.
12//
13//===----------------------------------------------------------------------===//
14
15
16#include "kmp.h"
Jonathan Peyton1cdd87a2016-11-14 21:08:35 +000017#include "kmp_affinity.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000018#include "kmp_i18n.h"
19#include "kmp_io.h"
20#include "kmp_itt.h"
21#include "kmp_lock.h"
22#include "kmp_stats.h"
23#include "kmp_str.h"
24#include "kmp_wait_release.h"
25#include "kmp_wrapper_getpid.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000026
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +000027#if !KMP_OS_FREEBSD && !KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +000028#include <alloca.h>
Alp Toker763b9392014-02-28 09:42:41 +000029#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000030#include <math.h> // HUGE_VAL.
Jim Cownie5e8470a2013-09-27 10:38:44 +000031#include <sys/resource.h>
32#include <sys/syscall.h>
Jonathan Peyton30419822017-05-12 18:01:32 +000033#include <sys/time.h>
34#include <sys/times.h>
35#include <unistd.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000036
Jim Cownie3051f972014-08-07 10:12:54 +000037#if KMP_OS_LINUX && !KMP_OS_CNK
Jonathan Peyton30419822017-05-12 18:01:32 +000038#include <sys/sysinfo.h>
39#if KMP_USE_FUTEX
40// We should really include <futex.h>, but that causes compatibility problems on
41// different Linux* OS distributions that either require that you include (or
42// break when you try to include) <pci/types.h>. Since all we need is the two
43// macros below (which are part of the kernel ABI, so can't change) we just
44// define the constants here and don't include <futex.h>
45#ifndef FUTEX_WAIT
46#define FUTEX_WAIT 0
47#endif
48#ifndef FUTEX_WAKE
49#define FUTEX_WAKE 1
50#endif
51#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000052#elif KMP_OS_DARWIN
Jonathan Peyton30419822017-05-12 18:01:32 +000053#include <mach/mach.h>
54#include <sys/sysctl.h>
Alp Toker763b9392014-02-28 09:42:41 +000055#elif KMP_OS_FREEBSD
Jonathan Peyton30419822017-05-12 18:01:32 +000056#include <pthread_np.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000057#endif
58
Jim Cownie5e8470a2013-09-27 10:38:44 +000059#include <ctype.h>
Jonathan Peyton30419822017-05-12 18:01:32 +000060#include <dirent.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000061#include <fcntl.h>
62
Jonas Hahnfeld50fed042016-11-07 15:58:36 +000063#include "tsan_annotations.h"
64
Jim Cownie5e8470a2013-09-27 10:38:44 +000065struct kmp_sys_timer {
Jonathan Peyton30419822017-05-12 18:01:32 +000066 struct timespec start;
Jim Cownie5e8470a2013-09-27 10:38:44 +000067};
68
69// Convert timespec to nanoseconds.
70#define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec)
71
72static struct kmp_sys_timer __kmp_sys_timer_data;
73
74#if KMP_HANDLE_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +000075typedef void (*sig_func_t)(int);
76STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[NSIG];
77static sigset_t __kmp_sigset;
Jim Cownie5e8470a2013-09-27 10:38:44 +000078#endif
79
Jonathan Peyton30419822017-05-12 18:01:32 +000080static int __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +000081
82static int __kmp_fork_count = 0;
83
Jonathan Peyton30419822017-05-12 18:01:32 +000084static pthread_condattr_t __kmp_suspend_cond_attr;
Jim Cownie5e8470a2013-09-27 10:38:44 +000085static pthread_mutexattr_t __kmp_suspend_mutex_attr;
86
Jonathan Peyton30419822017-05-12 18:01:32 +000087static kmp_cond_align_t __kmp_wait_cv;
88static kmp_mutex_align_t __kmp_wait_mx;
Jim Cownie5e8470a2013-09-27 10:38:44 +000089
Jonathan Peyton35d75ae2017-03-20 22:11:31 +000090kmp_uint64 __kmp_ticks_per_msec = 1000000;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +000091
Jim Cownie5e8470a2013-09-27 10:38:44 +000092#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +000093static void __kmp_print_cond(char *buffer, kmp_cond_align_t *cond) {
94 KMP_SNPRINTF(buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))",
95 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
96 cond->c_cond.__c_waiting);
Jim Cownie5e8470a2013-09-27 10:38:44 +000097}
98#endif
99
Jonathan Peyton30419822017-05-12 18:01:32 +0000100#if (KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000101
Jonathan Peyton30419822017-05-12 18:01:32 +0000102/* Affinity support */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000103
Jonathan Peyton30419822017-05-12 18:01:32 +0000104void __kmp_affinity_bind_thread(int which) {
105 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
106 "Illegal set affinity operation when not capable");
Jim Cownie5e8470a2013-09-27 10:38:44 +0000107
Jonathan Peyton30419822017-05-12 18:01:32 +0000108 kmp_affin_mask_t *mask;
109 KMP_CPU_ALLOC_ON_STACK(mask);
110 KMP_CPU_ZERO(mask);
111 KMP_CPU_SET(which, mask);
112 __kmp_set_system_affinity(mask, TRUE);
113 KMP_CPU_FREE_FROM_STACK(mask);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000114}
115
Jonathan Peyton30419822017-05-12 18:01:32 +0000116/* Determine if we can access affinity functionality on this version of
Jim Cownie5e8470a2013-09-27 10:38:44 +0000117 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
Jonathan Peyton30419822017-05-12 18:01:32 +0000118 * __kmp_affin_mask_size to the appropriate value (0 means not capable). */
119void __kmp_affinity_determine_capable(const char *env_var) {
120// Check and see if the OS supports thread affinity.
121
122#define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024)
123
124 int gCode;
125 int sCode;
126 unsigned char *buf;
127 buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
128
129 // If Linux* OS:
130 // If the syscall fails or returns a suggestion for the size,
131 // then we don't have to search for an appropriate size.
132 gCode = syscall(__NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf);
133 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
134 "initial getaffinity call returned %d errno = %d\n",
135 gCode, errno));
136
137 // if ((gCode < 0) && (errno == ENOSYS))
138 if (gCode < 0) {
139 // System call not supported
140 if (__kmp_affinity_verbose ||
141 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
142 (__kmp_affinity_type != affinity_default) &&
143 (__kmp_affinity_type != affinity_disabled))) {
144 int error = errno;
145 kmp_msg_t err_code = KMP_ERR(error);
146 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
147 err_code, __kmp_msg_null);
148 if (__kmp_generate_warnings == kmp_warnings_off) {
149 __kmp_str_free(&err_code.str);
150 }
151 }
152 KMP_AFFINITY_DISABLE();
153 KMP_INTERNAL_FREE(buf);
154 return;
155 }
156 if (gCode > 0) { // Linux* OS only
157 // The optimal situation: the OS returns the size of the buffer it expects.
Jim Cownie5e8470a2013-09-27 10:38:44 +0000158 //
Jonathan Peyton30419822017-05-12 18:01:32 +0000159 // A verification of correct behavior is that Isetaffinity on a NULL
160 // buffer with the same size fails with errno set to EFAULT.
161 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL);
162 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
163 "setaffinity for mask size %d returned %d errno = %d\n",
164 gCode, sCode, errno));
165 if (sCode < 0) {
166 if (errno == ENOSYS) {
167 if (__kmp_affinity_verbose ||
168 (__kmp_affinity_warnings &&
169 (__kmp_affinity_type != affinity_none) &&
170 (__kmp_affinity_type != affinity_default) &&
171 (__kmp_affinity_type != affinity_disabled))) {
172 int error = errno;
173 kmp_msg_t err_code = KMP_ERR(error);
174 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var),
175 err_code, __kmp_msg_null);
176 if (__kmp_generate_warnings == kmp_warnings_off) {
177 __kmp_str_free(&err_code.str);
178 }
179 }
180 KMP_AFFINITY_DISABLE();
181 KMP_INTERNAL_FREE(buf);
182 }
183 if (errno == EFAULT) {
184 KMP_AFFINITY_ENABLE(gCode);
185 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
186 "affinity supported (mask size %d)\n",
187 (int)__kmp_affin_mask_size));
188 KMP_INTERNAL_FREE(buf);
189 return;
190 }
191 }
192 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000193
Jonathan Peyton30419822017-05-12 18:01:32 +0000194 // Call the getaffinity system call repeatedly with increasing set sizes
195 // until we succeed, or reach an upper bound on the search.
196 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
197 "searching for proper set size\n"));
198 int size;
199 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
200 gCode = syscall(__NR_sched_getaffinity, 0, size, buf);
201 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
202 "getaffinity for mask size %d returned %d errno = %d\n",
203 size, gCode, errno));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000204
Jim Cownie5e8470a2013-09-27 10:38:44 +0000205 if (gCode < 0) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000206 if (errno == ENOSYS) {
207 // We shouldn't get here
208 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
209 "inconsistent OS call behavior: errno == ENOSYS for mask "
210 "size %d\n",
211 size));
212 if (__kmp_affinity_verbose ||
213 (__kmp_affinity_warnings &&
214 (__kmp_affinity_type != affinity_none) &&
215 (__kmp_affinity_type != affinity_default) &&
216 (__kmp_affinity_type != affinity_disabled))) {
217 int error = errno;
218 kmp_msg_t err_code = KMP_ERR(error);
219 __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
220 err_code, __kmp_msg_null);
221 if (__kmp_generate_warnings == kmp_warnings_off) {
222 __kmp_str_free(&err_code.str);
223 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000224 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000225 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000226 KMP_INTERNAL_FREE(buf);
227 return;
Jonathan Peyton30419822017-05-12 18:01:32 +0000228 }
229 continue;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000230 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000231
232 sCode = syscall(__NR_sched_setaffinity, 0, gCode, NULL);
233 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
234 "setaffinity for mask size %d returned %d errno = %d\n",
235 gCode, sCode, errno));
236 if (sCode < 0) {
237 if (errno == ENOSYS) { // Linux* OS only
238 // We shouldn't get here
239 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
240 "inconsistent OS call behavior: errno == ENOSYS for mask "
241 "size %d\n",
242 size));
243 if (__kmp_affinity_verbose ||
244 (__kmp_affinity_warnings &&
245 (__kmp_affinity_type != affinity_none) &&
246 (__kmp_affinity_type != affinity_default) &&
247 (__kmp_affinity_type != affinity_disabled))) {
248 int error = errno;
249 kmp_msg_t err_code = KMP_ERR(error);
250 __kmp_msg(kmp_ms_warning, KMP_MSG(SetAffSysCallNotSupported, env_var),
251 err_code, __kmp_msg_null);
252 if (__kmp_generate_warnings == kmp_warnings_off) {
253 __kmp_str_free(&err_code.str);
254 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000255 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000256 KMP_AFFINITY_DISABLE();
257 KMP_INTERNAL_FREE(buf);
258 return;
259 }
260 if (errno == EFAULT) {
261 KMP_AFFINITY_ENABLE(gCode);
262 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
263 "affinity supported (mask size %d)\n",
264 (int)__kmp_affin_mask_size));
265 KMP_INTERNAL_FREE(buf);
266 return;
267 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000268 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000269 }
270 // save uncaught error code
271 // int error = errno;
272 KMP_INTERNAL_FREE(buf);
273 // restore uncaught error code, will be printed at the next KMP_WARNING below
274 // errno = error;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000275
Jonathan Peyton30419822017-05-12 18:01:32 +0000276 // Affinity is not supported
277 KMP_AFFINITY_DISABLE();
278 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
279 "cannot determine mask size - affinity not supported\n"));
280 if (__kmp_affinity_verbose ||
281 (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
282 (__kmp_affinity_type != affinity_default) &&
283 (__kmp_affinity_type != affinity_disabled))) {
284 KMP_WARNING(AffCantGetMaskSize, env_var);
285 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000286}
287
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000288#endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000289
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000290#if KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000291
Jonathan Peyton30419822017-05-12 18:01:32 +0000292int __kmp_futex_determine_capable() {
293 int loc = 0;
294 int rc = syscall(__NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0);
295 int retval = (rc == 0) || (errno != ENOSYS);
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000296
Jonathan Peyton30419822017-05-12 18:01:32 +0000297 KA_TRACE(10,
298 ("__kmp_futex_determine_capable: rc = %d errno = %d\n", rc, errno));
299 KA_TRACE(10, ("__kmp_futex_determine_capable: futex syscall%s supported\n",
300 retval ? "" : " not"));
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000301
Jonathan Peyton30419822017-05-12 18:01:32 +0000302 return retval;
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000303}
304
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000305#endif // KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000306
Jonathan Peyton30419822017-05-12 18:01:32 +0000307#if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (!KMP_ASM_INTRINS)
308/* Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
309 use compare_and_store for these routines */
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000310
Jonathan Peyton30419822017-05-12 18:01:32 +0000311kmp_int8 __kmp_test_then_or8(volatile kmp_int8 *p, kmp_int8 d) {
312 kmp_int8 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000313
Jonathan Peyton30419822017-05-12 18:01:32 +0000314 old_value = TCR_1(*p);
315 new_value = old_value | d;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000316
Jonathan Peyton30419822017-05-12 18:01:32 +0000317 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
318 KMP_CPU_PAUSE();
319 old_value = TCR_1(*p);
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000320 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000321 }
322 return old_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000323}
324
Jonathan Peyton30419822017-05-12 18:01:32 +0000325kmp_int8 __kmp_test_then_and8(volatile kmp_int8 *p, kmp_int8 d) {
326 kmp_int8 old_value, new_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000327
Jonathan Peyton30419822017-05-12 18:01:32 +0000328 old_value = TCR_1(*p);
329 new_value = old_value & d;
330
331 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
332 KMP_CPU_PAUSE();
333 old_value = TCR_1(*p);
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000334 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000335 }
336 return old_value;
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000337}
338
Jonathan Peyton30419822017-05-12 18:01:32 +0000339kmp_int32 __kmp_test_then_or32(volatile kmp_int32 *p, kmp_int32 d) {
340 kmp_int32 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000341
Jonathan Peyton30419822017-05-12 18:01:32 +0000342 old_value = TCR_4(*p);
343 new_value = old_value | d;
344
345 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
346 KMP_CPU_PAUSE();
347 old_value = TCR_4(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000348 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000349 }
350 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000351}
352
Jonathan Peyton30419822017-05-12 18:01:32 +0000353kmp_int32 __kmp_test_then_and32(volatile kmp_int32 *p, kmp_int32 d) {
354 kmp_int32 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000355
Jonathan Peyton30419822017-05-12 18:01:32 +0000356 old_value = TCR_4(*p);
357 new_value = old_value & d;
358
359 while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
360 KMP_CPU_PAUSE();
361 old_value = TCR_4(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000362 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000363 }
364 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000365}
366
Jonathan Peyton30419822017-05-12 18:01:32 +0000367#if KMP_ARCH_X86
368kmp_int8 __kmp_test_then_add8(volatile kmp_int8 *p, kmp_int8 d) {
369 kmp_int8 old_value, new_value;
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000370
Jonathan Peyton30419822017-05-12 18:01:32 +0000371 old_value = TCR_1(*p);
372 new_value = old_value + d;
373
374 while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
375 KMP_CPU_PAUSE();
376 old_value = TCR_1(*p);
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000377 new_value = old_value + d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000378 }
379 return old_value;
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000380}
381
Jonathan Peyton30419822017-05-12 18:01:32 +0000382kmp_int64 __kmp_test_then_add64(volatile kmp_int64 *p, kmp_int64 d) {
383 kmp_int64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000384
Jonathan Peyton30419822017-05-12 18:01:32 +0000385 old_value = TCR_8(*p);
386 new_value = old_value + d;
387
388 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
389 KMP_CPU_PAUSE();
390 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000391 new_value = old_value + d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000392 }
393 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000394}
Jonathan Peyton30419822017-05-12 18:01:32 +0000395#endif /* KMP_ARCH_X86 */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000396
Jonathan Peyton30419822017-05-12 18:01:32 +0000397kmp_int64 __kmp_test_then_or64(volatile kmp_int64 *p, kmp_int64 d) {
398 kmp_int64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000399
Jonathan Peyton30419822017-05-12 18:01:32 +0000400 old_value = TCR_8(*p);
401 new_value = old_value | d;
402 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
403 KMP_CPU_PAUSE();
404 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000405 new_value = old_value | d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000406 }
407 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000408}
409
Jonathan Peyton30419822017-05-12 18:01:32 +0000410kmp_int64 __kmp_test_then_and64(volatile kmp_int64 *p, kmp_int64 d) {
411 kmp_int64 old_value, new_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000412
Jonathan Peyton30419822017-05-12 18:01:32 +0000413 old_value = TCR_8(*p);
414 new_value = old_value & d;
415 while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
416 KMP_CPU_PAUSE();
417 old_value = TCR_8(*p);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000418 new_value = old_value & d;
Jonathan Peyton30419822017-05-12 18:01:32 +0000419 }
420 return old_value;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000421}
422
423#endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
424
Jonathan Peyton30419822017-05-12 18:01:32 +0000425void __kmp_terminate_thread(int gtid) {
426 int status;
427 kmp_info_t *th = __kmp_threads[gtid];
Jim Cownie5e8470a2013-09-27 10:38:44 +0000428
Jonathan Peyton30419822017-05-12 18:01:32 +0000429 if (!th)
430 return;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000431
Jonathan Peyton30419822017-05-12 18:01:32 +0000432#ifdef KMP_CANCEL_THREADS
433 KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
434 status = pthread_cancel(th->th.th_info.ds.ds_thread);
435 if (status != 0 && status != ESRCH) {
436 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantTerminateWorkerThread), KMP_ERR(status),
437 __kmp_msg_null);
438 }; // if
439#endif
440 __kmp_yield(TRUE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000441} //
442
Jonathan Peyton30419822017-05-12 18:01:32 +0000443/* Set thread stack info according to values returned by pthread_getattr_np().
444 If values are unreasonable, assume call failed and use incremental stack
445 refinement method instead. Returns TRUE if the stack parameters could be
446 determined exactly, FALSE if incremental refinement is necessary. */
447static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) {
448 int stack_data;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000449#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000450 /* Linux* OS only -- no pthread_getattr_np support on OS X* */
451 pthread_attr_t attr;
452 int status;
453 size_t size = 0;
454 void *addr = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000455
Jonathan Peyton30419822017-05-12 18:01:32 +0000456 /* Always do incremental stack refinement for ubermaster threads since the
457 initial thread stack range can be reduced by sibling thread creation so
458 pthread_attr_getstack may cause thread gtid aliasing */
459 if (!KMP_UBER_GTID(gtid)) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000460
Jonathan Peyton30419822017-05-12 18:01:32 +0000461 /* Fetch the real thread attributes */
462 status = pthread_attr_init(&attr);
463 KMP_CHECK_SYSFAIL("pthread_attr_init", status);
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000464#if KMP_OS_FREEBSD || KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000465 status = pthread_attr_get_np(pthread_self(), &attr);
466 KMP_CHECK_SYSFAIL("pthread_attr_get_np", status);
Alp Toker763b9392014-02-28 09:42:41 +0000467#else
Jonathan Peyton30419822017-05-12 18:01:32 +0000468 status = pthread_getattr_np(pthread_self(), &attr);
469 KMP_CHECK_SYSFAIL("pthread_getattr_np", status);
Alp Toker763b9392014-02-28 09:42:41 +0000470#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000471 status = pthread_attr_getstack(&attr, &addr, &size);
472 KMP_CHECK_SYSFAIL("pthread_attr_getstack", status);
473 KA_TRACE(60,
474 ("__kmp_set_stack_info: T#%d pthread_attr_getstack returned size:"
475 " %lu, low addr: %p\n",
476 gtid, size, addr));
477 status = pthread_attr_destroy(&attr);
478 KMP_CHECK_SYSFAIL("pthread_attr_destroy", status);
479 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000480
Jonathan Peyton30419822017-05-12 18:01:32 +0000481 if (size != 0 && addr != 0) { // was stack parameter determination successful?
482 /* Store the correct base and size */
483 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size));
484 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
485 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
486 return TRUE;
487 }
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000488#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD */
Jonathan Peyton30419822017-05-12 18:01:32 +0000489 /* Use incremental refinement starting from initial conservative estimate */
490 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
491 TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
492 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
493 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000494}
495
Jonathan Peyton30419822017-05-12 18:01:32 +0000496static void *__kmp_launch_worker(void *thr) {
497 int status, old_type, old_state;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000498#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000499 sigset_t new_set, old_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000500#endif /* KMP_BLOCK_SIGNALS */
Jonathan Peyton30419822017-05-12 18:01:32 +0000501 void *exit_val;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000502#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000503 void *volatile padding = 0;
Jonathan Peyton2321d572015-06-08 19:25:25 +0000504#endif
Jonathan Peyton30419822017-05-12 18:01:32 +0000505 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000506
Jonathan Peyton30419822017-05-12 18:01:32 +0000507 gtid = ((kmp_info_t *)thr)->th.th_info.ds.ds_gtid;
508 __kmp_gtid_set_specific(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000509#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000510 __kmp_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000511#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000512#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000513 // set __thread local index to point to thread-specific stats
514 __kmp_stats_thread_ptr = ((kmp_info_t *)thr)->th.th_stats;
515 KMP_START_EXPLICIT_TIMER(OMP_worker_thread_life);
516 KMP_SET_THREAD_STATE(IDLE);
517 KMP_INIT_PARTITIONED_TIMERS(OMP_idle);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000518#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000519
520#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000521 __kmp_itt_thread_name(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000522#endif /* USE_ITT_BUILD */
523
Alp Toker763b9392014-02-28 09:42:41 +0000524#if KMP_AFFINITY_SUPPORTED
Jonathan Peyton30419822017-05-12 18:01:32 +0000525 __kmp_affinity_set_init_mask(gtid, FALSE);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000526#endif
527
528#ifdef KMP_CANCEL_THREADS
Jonathan Peyton30419822017-05-12 18:01:32 +0000529 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
530 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
531 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
532 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
533 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000534#endif
535
536#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000537 // Set FP control regs to be a copy of the parallel initialization thread's.
538 __kmp_clear_x87_fpu_status_word();
539 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
540 __kmp_load_mxcsr(&__kmp_init_mxcsr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000541#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
542
543#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000544 status = sigfillset(&new_set);
545 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
546 status = pthread_sigmask(SIG_BLOCK, &new_set, &old_set);
547 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000548#endif /* KMP_BLOCK_SIGNALS */
549
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000550#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jonathan Peyton30419822017-05-12 18:01:32 +0000551 if (__kmp_stkoffset > 0 && gtid > 0) {
552 padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
553 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000554#endif
555
Jonathan Peyton30419822017-05-12 18:01:32 +0000556 KMP_MB();
557 __kmp_set_stack_info(gtid, (kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000558
Jonathan Peyton30419822017-05-12 18:01:32 +0000559 __kmp_check_stack_overlap((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000560
Jonathan Peyton30419822017-05-12 18:01:32 +0000561 exit_val = __kmp_launch_thread((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000562
563#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000564 status = pthread_sigmask(SIG_SETMASK, &old_set, NULL);
565 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000566#endif /* KMP_BLOCK_SIGNALS */
567
Jonathan Peyton30419822017-05-12 18:01:32 +0000568 return exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000569}
570
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000571#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000572/* The monitor thread controls all of the threads in the complex */
573
Jonathan Peyton30419822017-05-12 18:01:32 +0000574static void *__kmp_launch_monitor(void *thr) {
575 int status, old_type, old_state;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000576#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000577 sigset_t new_set;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000578#endif /* KMP_BLOCK_SIGNALS */
Jonathan Peyton30419822017-05-12 18:01:32 +0000579 struct timespec interval;
580 int yield_count;
581 int yield_cycles = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000582
Jonathan Peyton30419822017-05-12 18:01:32 +0000583 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000584
Jonathan Peyton30419822017-05-12 18:01:32 +0000585 KA_TRACE(10, ("__kmp_launch_monitor: #1 launched\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000586
Jonathan Peyton30419822017-05-12 18:01:32 +0000587 /* register us as the monitor thread */
588 __kmp_gtid_set_specific(KMP_GTID_MONITOR);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000589#ifdef KMP_TDATA_GTID
Jonathan Peyton30419822017-05-12 18:01:32 +0000590 __kmp_gtid = KMP_GTID_MONITOR;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000591#endif
592
Jonathan Peyton30419822017-05-12 18:01:32 +0000593 KMP_MB();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000594
595#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +0000596 // Instruct Intel(R) Threading Tools to ignore monitor thread.
597 __kmp_itt_thread_ignore();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000598#endif /* USE_ITT_BUILD */
599
Jonathan Peyton30419822017-05-12 18:01:32 +0000600 __kmp_set_stack_info(((kmp_info_t *)thr)->th.th_info.ds.ds_gtid,
601 (kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000602
Jonathan Peyton30419822017-05-12 18:01:32 +0000603 __kmp_check_stack_overlap((kmp_info_t *)thr);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000604
605#ifdef KMP_CANCEL_THREADS
Jonathan Peyton30419822017-05-12 18:01:32 +0000606 status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
607 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
608 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
609 status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
610 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000611#endif
612
Jonathan Peyton30419822017-05-12 18:01:32 +0000613#if KMP_REAL_TIME_FIX
614 // This is a potential fix which allows application with real-time scheduling
615 // policy work. However, decision about the fix is not made yet, so it is
616 // disabled by default.
617 { // Are program started with real-time scheduling policy?
618 int sched = sched_getscheduler(0);
619 if (sched == SCHED_FIFO || sched == SCHED_RR) {
620 // Yes, we are a part of real-time application. Try to increase the
621 // priority of the monitor.
622 struct sched_param param;
623 int max_priority = sched_get_priority_max(sched);
624 int rc;
625 KMP_WARNING(RealTimeSchedNotSupported);
626 sched_getparam(0, &param);
627 if (param.sched_priority < max_priority) {
628 param.sched_priority += 1;
629 rc = sched_setscheduler(0, sched, &param);
630 if (rc != 0) {
631 int error = errno;
632 kmp_msg_t err_code = KMP_ERR(error);
633 __kmp_msg(kmp_ms_warning, KMP_MSG(CantChangeMonitorPriority),
634 err_code, KMP_MSG(MonitorWillStarve), __kmp_msg_null);
635 if (__kmp_generate_warnings == kmp_warnings_off) {
636 __kmp_str_free(&err_code.str);
637 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000638 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +0000639 } else {
640 // We cannot abort here, because number of CPUs may be enough for all
641 // the threads, including the monitor thread, so application could
642 // potentially work...
643 __kmp_msg(kmp_ms_warning, KMP_MSG(RunningAtMaxPriority),
644 KMP_MSG(MonitorWillStarve), KMP_HNT(RunningAtMaxPriority),
645 __kmp_msg_null);
646 }; // if
647 }; // if
648 // AC: free thread that waits for monitor started
649 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
650 }
651#endif // KMP_REAL_TIME_FIX
Jim Cownie5e8470a2013-09-27 10:38:44 +0000652
Jonathan Peyton30419822017-05-12 18:01:32 +0000653 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000654
Jonathan Peyton30419822017-05-12 18:01:32 +0000655 if (__kmp_monitor_wakeups == 1) {
656 interval.tv_sec = 1;
657 interval.tv_nsec = 0;
658 } else {
659 interval.tv_sec = 0;
660 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
661 }
662
663 KA_TRACE(10, ("__kmp_launch_monitor: #2 monitor\n"));
664
665 if (__kmp_yield_cycle) {
666 __kmp_yielding_on = 0; /* Start out with yielding shut off */
667 yield_count = __kmp_yield_off_count;
668 } else {
669 __kmp_yielding_on = 1; /* Yielding is on permanently */
670 }
671
672 while (!TCR_4(__kmp_global.g.g_done)) {
673 struct timespec now;
674 struct timeval tval;
675
676 /* This thread monitors the state of the system */
677
678 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
679
680 status = gettimeofday(&tval, NULL);
681 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
682 TIMEVAL_TO_TIMESPEC(&tval, &now);
683
684 now.tv_sec += interval.tv_sec;
685 now.tv_nsec += interval.tv_nsec;
686
687 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
688 now.tv_sec += 1;
689 now.tv_nsec -= KMP_NSEC_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000690 }
691
Jonathan Peyton30419822017-05-12 18:01:32 +0000692 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
693 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
694 // AC: the monitor should not fall asleep if g_done has been set
695 if (!TCR_4(__kmp_global.g.g_done)) { // check once more under mutex
696 status = pthread_cond_timedwait(&__kmp_wait_cv.c_cond,
697 &__kmp_wait_mx.m_mutex, &now);
698 if (status != 0) {
699 if (status != ETIMEDOUT && status != EINTR) {
700 KMP_SYSFAIL("pthread_cond_timedwait", status);
701 };
702 };
703 };
704 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
705 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000706
707 if (__kmp_yield_cycle) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000708 yield_cycles++;
709 if ((yield_cycles % yield_count) == 0) {
710 if (__kmp_yielding_on) {
711 __kmp_yielding_on = 0; /* Turn it off now */
712 yield_count = __kmp_yield_off_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000713 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +0000714 __kmp_yielding_on = 1; /* Turn it on now */
715 yield_count = __kmp_yield_on_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000716 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000717 yield_cycles = 0;
718 }
719 } else {
720 __kmp_yielding_on = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000721 }
722
Jonathan Peyton30419822017-05-12 18:01:32 +0000723 TCW_4(__kmp_global.g.g_time.dt.t_value,
724 TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
725
726 KMP_MB(); /* Flush all pending memory write invalidates. */
727 }
728
729 KA_TRACE(10, ("__kmp_launch_monitor: #3 cleanup\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000730
731#ifdef KMP_BLOCK_SIGNALS
Jonathan Peyton30419822017-05-12 18:01:32 +0000732 status = sigfillset(&new_set);
733 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
734 status = pthread_sigmask(SIG_UNBLOCK, &new_set, NULL);
735 KMP_CHECK_SYSFAIL("pthread_sigmask", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000736#endif /* KMP_BLOCK_SIGNALS */
737
Jonathan Peyton30419822017-05-12 18:01:32 +0000738 KA_TRACE(10, ("__kmp_launch_monitor: #4 finished\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000739
Jonathan Peyton30419822017-05-12 18:01:32 +0000740 if (__kmp_global.g.g_abort != 0) {
741 /* now we need to terminate the worker threads */
742 /* the value of t_abort is the signal we caught */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000743
Jonathan Peyton30419822017-05-12 18:01:32 +0000744 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000745
Jonathan Peyton30419822017-05-12 18:01:32 +0000746 KA_TRACE(10, ("__kmp_launch_monitor: #5 terminate sig=%d\n",
747 __kmp_global.g.g_abort));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000748
Jonathan Peyton30419822017-05-12 18:01:32 +0000749 /* terminate the OpenMP worker threads */
750 /* TODO this is not valid for sibling threads!!
751 * the uber master might not be 0 anymore.. */
752 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
753 __kmp_terminate_thread(gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000754
Jonathan Peyton30419822017-05-12 18:01:32 +0000755 __kmp_cleanup();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000756
Jonathan Peyton30419822017-05-12 18:01:32 +0000757 KA_TRACE(10, ("__kmp_launch_monitor: #6 raise sig=%d\n",
758 __kmp_global.g.g_abort));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000759
Jonathan Peyton30419822017-05-12 18:01:32 +0000760 if (__kmp_global.g.g_abort > 0)
761 raise(__kmp_global.g.g_abort);
762 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000763
Jonathan Peyton30419822017-05-12 18:01:32 +0000764 KA_TRACE(10, ("__kmp_launch_monitor: #7 exit\n"));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000765
Jonathan Peyton30419822017-05-12 18:01:32 +0000766 return thr;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000767}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000768#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000769
Jonathan Peyton30419822017-05-12 18:01:32 +0000770void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
771 pthread_t handle;
772 pthread_attr_t thread_attr;
773 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000774
Jonathan Peyton30419822017-05-12 18:01:32 +0000775 th->th.th_info.ds.ds_gtid = gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000776
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000777#if KMP_STATS_ENABLED
Jonathan Peyton30419822017-05-12 18:01:32 +0000778 // sets up worker thread stats
779 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000780
Jonathan Peyton30419822017-05-12 18:01:32 +0000781 // th->th.th_stats is used to transfer thread-specific stats-pointer to
782 // __kmp_launch_worker. So when thread is created (goes into
783 // __kmp_launch_worker) it will set its __thread local pointer to
784 // th->th.th_stats
785 if (!KMP_UBER_GTID(gtid)) {
786 th->th.th_stats = __kmp_stats_list->push_back(gtid);
787 } else {
788 // For root threads, __kmp_stats_thread_ptr is set in __kmp_register_root(),
789 // so set the th->th.th_stats field to it.
790 th->th.th_stats = __kmp_stats_thread_ptr;
791 }
792 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000793
794#endif // KMP_STATS_ENABLED
795
Jonathan Peyton30419822017-05-12 18:01:32 +0000796 if (KMP_UBER_GTID(gtid)) {
797 KA_TRACE(10, ("__kmp_create_worker: uber thread (%d)\n", gtid));
798 th->th.th_info.ds.ds_thread = pthread_self();
799 __kmp_set_stack_info(gtid, th);
800 __kmp_check_stack_overlap(th);
801 return;
802 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +0000803
Jonathan Peyton30419822017-05-12 18:01:32 +0000804 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000805
Jonathan Peyton30419822017-05-12 18:01:32 +0000806 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000807
808#ifdef KMP_THREAD_ATTR
Jonathan Peyton30419822017-05-12 18:01:32 +0000809 status = pthread_attr_init(&thread_attr);
810 if (status != 0) {
811 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantInitThreadAttrs), KMP_ERR(status),
812 __kmp_msg_null);
813 }; // if
814 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
815 if (status != 0) {
816 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetWorkerState), KMP_ERR(status),
817 __kmp_msg_null);
818 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +0000819
Jonathan Peyton30419822017-05-12 18:01:32 +0000820 /* Set stack size for this thread now.
821 The multiple of 2 is there because on some machines, requesting an unusual
822 stacksize causes the thread to have an offset before the dummy alloca()
823 takes place to create the offset. Since we want the user to have a
824 sufficient stacksize AND support a stack offset, we alloca() twice the
825 offset so that the upcoming alloca() does not eliminate any premade offset,
826 and also gives the user the stack space they requested for all threads */
827 stack_size += gtid * __kmp_stkoffset * 2;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000828
Jonathan Peyton30419822017-05-12 18:01:32 +0000829 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
830 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
831 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000832
Jim Cownie5e8470a2013-09-27 10:38:44 +0000833#ifdef _POSIX_THREAD_ATTR_STACKSIZE
Jonathan Peyton30419822017-05-12 18:01:32 +0000834 status = pthread_attr_setstacksize(&thread_attr, stack_size);
835#ifdef KMP_BACKUP_STKSIZE
836 if (status != 0) {
837 if (!__kmp_env_stksize) {
838 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
839 __kmp_stksize = KMP_BACKUP_STKSIZE;
840 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
841 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
842 "bytes\n",
843 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
844 status = pthread_attr_setstacksize(&thread_attr, stack_size);
845 }; // if
846 }; // if
847#endif /* KMP_BACKUP_STKSIZE */
848 if (status != 0) {
849 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetWorkerStackSize, stack_size),
850 KMP_ERR(status), KMP_HNT(ChangeWorkerStackSize), __kmp_msg_null);
851 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +0000852#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000853
Jim Cownie5e8470a2013-09-27 10:38:44 +0000854#endif /* KMP_THREAD_ATTR */
855
Jonathan Peyton30419822017-05-12 18:01:32 +0000856 status =
857 pthread_create(&handle, &thread_attr, __kmp_launch_worker, (void *)th);
858 if (status != 0 || !handle) { // ??? Why do we check handle??
859#ifdef _POSIX_THREAD_ATTR_STACKSIZE
860 if (status == EINVAL) {
861 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetWorkerStackSize, stack_size),
862 KMP_ERR(status), KMP_HNT(IncreaseWorkerStackSize),
863 __kmp_msg_null);
864 };
865 if (status == ENOMEM) {
866 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetWorkerStackSize, stack_size),
867 KMP_ERR(status), KMP_HNT(DecreaseWorkerStackSize),
868 __kmp_msg_null);
869 };
870#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
871 if (status == EAGAIN) {
872 __kmp_msg(kmp_ms_fatal, KMP_MSG(NoResourcesForWorkerThread),
873 KMP_ERR(status), KMP_HNT(Decrease_NUM_THREADS), __kmp_msg_null);
874 }; // if
875 KMP_SYSFAIL("pthread_create", status);
876 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +0000877
Jonathan Peyton30419822017-05-12 18:01:32 +0000878 th->th.th_info.ds.ds_thread = handle;
879
880#ifdef KMP_THREAD_ATTR
881 status = pthread_attr_destroy(&thread_attr);
882 if (status) {
883 kmp_msg_t err_code = KMP_ERR(status);
884 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
885 __kmp_msg_null);
886 if (__kmp_generate_warnings == kmp_warnings_off) {
887 __kmp_str_free(&err_code.str);
888 }
889 }; // if
890#endif /* KMP_THREAD_ATTR */
891
892 KMP_MB(); /* Flush all pending memory write invalidates. */
893
894 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +0000895
896} // __kmp_create_worker
897
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000898#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +0000899void __kmp_create_monitor(kmp_info_t *th) {
900 pthread_t handle;
901 pthread_attr_t thread_attr;
902 size_t size;
903 int status;
904 int auto_adj_size = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000905
Jonathan Peyton30419822017-05-12 18:01:32 +0000906 if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
907 // We don't need monitor thread in case of MAX_BLOCKTIME
908 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
909 "MAX blocktime\n"));
910 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
911 th->th.th_info.ds.ds_gtid = 0;
912 return;
913 }
914 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
915
916 KMP_MB(); /* Flush all pending memory write invalidates. */
917
918 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
919 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
920#if KMP_REAL_TIME_FIX
921 TCW_4(__kmp_global.g.g_time.dt.t_value,
922 -1); // Will use it for synchronization a bit later.
923#else
924 TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
925#endif // KMP_REAL_TIME_FIX
926
927#ifdef KMP_THREAD_ATTR
928 if (__kmp_monitor_stksize == 0) {
929 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
930 auto_adj_size = TRUE;
931 }
932 status = pthread_attr_init(&thread_attr);
933 if (status != 0) {
934 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantInitThreadAttrs), KMP_ERR(status),
935 __kmp_msg_null);
936 }; // if
937 status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
938 if (status != 0) {
939 __kmp_msg(kmp_ms_fatal, KMP_MSG(CantSetMonitorState), KMP_ERR(status),
940 __kmp_msg_null);
941 }; // if
942
943#ifdef _POSIX_THREAD_ATTR_STACKSIZE
944 status = pthread_attr_getstacksize(&thread_attr, &size);
945 KMP_CHECK_SYSFAIL("pthread_attr_getstacksize", status);
946#else
947 size = __kmp_sys_min_stksize;
948#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
949#endif /* KMP_THREAD_ATTR */
950
951 if (__kmp_monitor_stksize == 0) {
952 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
953 }
954 if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
955 __kmp_monitor_stksize = __kmp_sys_min_stksize;
956 }
957
958 KA_TRACE(10, ("__kmp_create_monitor: default stacksize = %lu bytes,"
959 "requested stacksize = %lu bytes\n",
960 size, __kmp_monitor_stksize));
961
962retry:
963
964/* Set stack size for this thread now. */
965#ifdef _POSIX_THREAD_ATTR_STACKSIZE
966 KA_TRACE(10, ("__kmp_create_monitor: setting stacksize = %lu bytes,",
967 __kmp_monitor_stksize));
968 status = pthread_attr_setstacksize(&thread_attr, __kmp_monitor_stksize);
969 if (status != 0) {
970 if (auto_adj_size) {
971 __kmp_monitor_stksize *= 2;
972 goto retry;
Jonathan Peyton4fee5f62015-12-18 23:20:36 +0000973 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000974 kmp_msg_t err_code = KMP_ERR(status);
975 __kmp_msg(kmp_ms_warning, // should this be fatal? BB
976 KMP_MSG(CantSetMonitorStackSize, (long int)__kmp_monitor_stksize),
977 err_code, KMP_HNT(ChangeMonitorStackSize), __kmp_msg_null);
978 if (__kmp_generate_warnings == kmp_warnings_off) {
979 __kmp_str_free(&err_code.str);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000980 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000981 }; // if
982#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000983
Jonathan Peyton30419822017-05-12 18:01:32 +0000984 status =
985 pthread_create(&handle, &thread_attr, __kmp_launch_monitor, (void *)th);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000986
Jonathan Peyton30419822017-05-12 18:01:32 +0000987 if (status != 0) {
988#ifdef _POSIX_THREAD_ATTR_STACKSIZE
989 if (status == EINVAL) {
990 if (auto_adj_size && (__kmp_monitor_stksize < (size_t)0x40000000)) {
991 __kmp_monitor_stksize *= 2;
992 goto retry;
993 }
994 __kmp_msg(
995 kmp_ms_fatal, KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
996 KMP_ERR(status), KMP_HNT(IncreaseMonitorStackSize), __kmp_msg_null);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000997 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +0000998 if (status == ENOMEM) {
999 __kmp_msg(
1000 kmp_ms_fatal, KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
1001 KMP_ERR(status), KMP_HNT(DecreaseMonitorStackSize), __kmp_msg_null);
1002 }; // if
1003#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1004 if (status == EAGAIN) {
1005 __kmp_msg(kmp_ms_fatal, KMP_MSG(NoResourcesForMonitorThread),
1006 KMP_ERR(status), KMP_HNT(DecreaseNumberOfThreadsInUse),
1007 __kmp_msg_null);
1008 }; // if
1009 KMP_SYSFAIL("pthread_create", status);
1010 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001011
Jonathan Peyton30419822017-05-12 18:01:32 +00001012 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001013
Jonathan Peyton30419822017-05-12 18:01:32 +00001014#if KMP_REAL_TIME_FIX
1015 // Wait for the monitor thread is really started and set its *priority*.
1016 KMP_DEBUG_ASSERT(sizeof(kmp_uint32) ==
1017 sizeof(__kmp_global.g.g_time.dt.t_value));
1018 __kmp_wait_yield_4((kmp_uint32 volatile *)&__kmp_global.g.g_time.dt.t_value,
1019 -1, &__kmp_neq_4, NULL);
1020#endif // KMP_REAL_TIME_FIX
Jim Cownie5e8470a2013-09-27 10:38:44 +00001021
Jonathan Peyton30419822017-05-12 18:01:32 +00001022#ifdef KMP_THREAD_ATTR
1023 status = pthread_attr_destroy(&thread_attr);
1024 if (status != 0) {
1025 kmp_msg_t err_code = KMP_ERR(status);
1026 __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
1027 __kmp_msg_null);
1028 if (__kmp_generate_warnings == kmp_warnings_off) {
1029 __kmp_str_free(&err_code.str);
1030 }
1031 }; // if
1032#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001033
Jonathan Peyton30419822017-05-12 18:01:32 +00001034 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001035
Jonathan Peyton30419822017-05-12 18:01:32 +00001036 KA_TRACE(10, ("__kmp_create_monitor: monitor created %#.8lx\n",
1037 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001038
1039} // __kmp_create_monitor
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001040#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001041
Jonathan Peyton30419822017-05-12 18:01:32 +00001042void __kmp_exit_thread(int exit_status) {
1043 pthread_exit((void *)(intptr_t)exit_status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001044} // __kmp_exit_thread
1045
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001046#if KMP_USE_MONITOR
Jim Cownie07ea89f2014-09-03 11:10:54 +00001047void __kmp_resume_monitor();
1048
Jonathan Peyton30419822017-05-12 18:01:32 +00001049void __kmp_reap_monitor(kmp_info_t *th) {
1050 int status;
1051 void *exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001052
Jonathan Peyton30419822017-05-12 18:01:32 +00001053 KA_TRACE(10, ("__kmp_reap_monitor: try to reap monitor thread with handle"
1054 " %#.8lx\n",
1055 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001056
Jonathan Peyton30419822017-05-12 18:01:32 +00001057 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1058 // If both tid and gtid are 0, it means the monitor did not ever start.
1059 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1060 KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
1061 if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
1062 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1063 return;
1064 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001065
Jonathan Peyton30419822017-05-12 18:01:32 +00001066 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001067
Jonathan Peyton30419822017-05-12 18:01:32 +00001068 /* First, check to see whether the monitor thread exists to wake it up. This
1069 is to avoid performance problem when the monitor sleeps during
1070 blocktime-size interval */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001071
Jonathan Peyton30419822017-05-12 18:01:32 +00001072 status = pthread_kill(th->th.th_info.ds.ds_thread, 0);
1073 if (status != ESRCH) {
1074 __kmp_resume_monitor(); // Wake up the monitor thread
1075 }
1076 KA_TRACE(10, ("__kmp_reap_monitor: try to join with monitor\n"));
1077 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
1078 if (exit_val != th) {
1079 __kmp_msg(kmp_ms_fatal, KMP_MSG(ReapMonitorError), KMP_ERR(status),
1080 __kmp_msg_null);
1081 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001082
Jonathan Peyton30419822017-05-12 18:01:32 +00001083 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1084 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001085
Jonathan Peyton30419822017-05-12 18:01:32 +00001086 KA_TRACE(10, ("__kmp_reap_monitor: done reaping monitor thread with handle"
1087 " %#.8lx\n",
1088 th->th.th_info.ds.ds_thread));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001089
Jonathan Peyton30419822017-05-12 18:01:32 +00001090 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001091}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001092#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001093
Jonathan Peyton30419822017-05-12 18:01:32 +00001094void __kmp_reap_worker(kmp_info_t *th) {
1095 int status;
1096 void *exit_val;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001097
Jonathan Peyton30419822017-05-12 18:01:32 +00001098 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001099
Jonathan Peyton30419822017-05-12 18:01:32 +00001100 KA_TRACE(
1101 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001102
Jonathan Peyton30419822017-05-12 18:01:32 +00001103 status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001104#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001105 /* Don't expose these to the user until we understand when they trigger */
1106 if (status != 0) {
1107 __kmp_msg(kmp_ms_fatal, KMP_MSG(ReapWorkerError), KMP_ERR(status),
1108 __kmp_msg_null);
1109 }
1110 if (exit_val != th) {
1111 KA_TRACE(10, ("__kmp_reap_worker: worker T#%d did not reap properly, "
1112 "exit_val = %p\n",
1113 th->th.th_info.ds.ds_gtid, exit_val));
1114 }
Jonathan Peyton93495de2016-06-13 17:36:40 +00001115#endif /* KMP_DEBUG */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001116
Jonathan Peyton30419822017-05-12 18:01:32 +00001117 KA_TRACE(10, ("__kmp_reap_worker: done reaping T#%d\n",
1118 th->th.th_info.ds.ds_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001119
Jonathan Peyton30419822017-05-12 18:01:32 +00001120 KMP_MB(); /* Flush all pending memory write invalidates. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001121}
1122
Jim Cownie5e8470a2013-09-27 10:38:44 +00001123#if KMP_HANDLE_SIGNALS
1124
Jonathan Peyton30419822017-05-12 18:01:32 +00001125static void __kmp_null_handler(int signo) {
1126 // Do nothing, for doing SIG_IGN-type actions.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001127} // __kmp_null_handler
1128
Jonathan Peyton30419822017-05-12 18:01:32 +00001129static void __kmp_team_handler(int signo) {
1130 if (__kmp_global.g.g_abort == 0) {
1131/* Stage 1 signal handler, let's shut down all of the threads */
1132#ifdef KMP_DEBUG
1133 __kmp_debug_printf("__kmp_team_handler: caught signal = %d\n", signo);
1134#endif
1135 switch (signo) {
1136 case SIGHUP:
1137 case SIGINT:
1138 case SIGQUIT:
1139 case SIGILL:
1140 case SIGABRT:
1141 case SIGFPE:
1142 case SIGBUS:
1143 case SIGSEGV:
1144#ifdef SIGSYS
1145 case SIGSYS:
1146#endif
1147 case SIGTERM:
1148 if (__kmp_debug_buf) {
1149 __kmp_dump_debug_buffer();
1150 }; // if
1151 KMP_MB(); // Flush all pending memory write invalidates.
1152 TCW_4(__kmp_global.g.g_abort, signo);
1153 KMP_MB(); // Flush all pending memory write invalidates.
1154 TCW_4(__kmp_global.g.g_done, TRUE);
1155 KMP_MB(); // Flush all pending memory write invalidates.
1156 break;
1157 default:
1158#ifdef KMP_DEBUG
1159 __kmp_debug_printf("__kmp_team_handler: unknown signal type");
1160#endif
1161 break;
1162 }; // switch
1163 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001164} // __kmp_team_handler
1165
Jonathan Peyton30419822017-05-12 18:01:32 +00001166static void __kmp_sigaction(int signum, const struct sigaction *act,
1167 struct sigaction *oldact) {
1168 int rc = sigaction(signum, act, oldact);
1169 KMP_CHECK_SYSFAIL_ERRNO("sigaction", rc);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001170}
1171
Jonathan Peyton30419822017-05-12 18:01:32 +00001172static void __kmp_install_one_handler(int sig, sig_func_t handler_func,
1173 int parallel_init) {
1174 KMP_MB(); // Flush all pending memory write invalidates.
1175 KB_TRACE(60,
1176 ("__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init));
1177 if (parallel_init) {
1178 struct sigaction new_action;
1179 struct sigaction old_action;
1180 new_action.sa_handler = handler_func;
1181 new_action.sa_flags = 0;
1182 sigfillset(&new_action.sa_mask);
1183 __kmp_sigaction(sig, &new_action, &old_action);
1184 if (old_action.sa_handler == __kmp_sighldrs[sig].sa_handler) {
1185 sigaddset(&__kmp_sigset, sig);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001186 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00001187 // Restore/keep user's handler if one previously installed.
1188 __kmp_sigaction(sig, &old_action, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001189 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +00001190 } else {
1191 // Save initial/system signal handlers to see if user handlers installed.
1192 __kmp_sigaction(sig, NULL, &__kmp_sighldrs[sig]);
1193 }; // if
1194 KMP_MB(); // Flush all pending memory write invalidates.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001195} // __kmp_install_one_handler
1196
Jonathan Peyton30419822017-05-12 18:01:32 +00001197static void __kmp_remove_one_handler(int sig) {
1198 KB_TRACE(60, ("__kmp_remove_one_handler( %d )\n", sig));
1199 if (sigismember(&__kmp_sigset, sig)) {
1200 struct sigaction old;
1201 KMP_MB(); // Flush all pending memory write invalidates.
1202 __kmp_sigaction(sig, &__kmp_sighldrs[sig], &old);
1203 if ((old.sa_handler != __kmp_team_handler) &&
1204 (old.sa_handler != __kmp_null_handler)) {
1205 // Restore the users signal handler.
1206 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1207 "restoring: sig=%d\n",
1208 sig));
1209 __kmp_sigaction(sig, &old, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001210 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +00001211 sigdelset(&__kmp_sigset, sig);
1212 KMP_MB(); // Flush all pending memory write invalidates.
1213 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001214} // __kmp_remove_one_handler
1215
Jonathan Peyton30419822017-05-12 18:01:32 +00001216void __kmp_install_signals(int parallel_init) {
1217 KB_TRACE(10, ("__kmp_install_signals( %d )\n", parallel_init));
1218 if (__kmp_handle_signals || !parallel_init) {
1219 // If ! parallel_init, we do not install handlers, just save original
1220 // handlers. Let us do it even __handle_signals is 0.
1221 sigemptyset(&__kmp_sigset);
1222 __kmp_install_one_handler(SIGHUP, __kmp_team_handler, parallel_init);
1223 __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
1224 __kmp_install_one_handler(SIGQUIT, __kmp_team_handler, parallel_init);
1225 __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
1226 __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
1227 __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
1228 __kmp_install_one_handler(SIGBUS, __kmp_team_handler, parallel_init);
1229 __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
1230#ifdef SIGSYS
1231 __kmp_install_one_handler(SIGSYS, __kmp_team_handler, parallel_init);
1232#endif // SIGSYS
1233 __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
1234#ifdef SIGPIPE
1235 __kmp_install_one_handler(SIGPIPE, __kmp_team_handler, parallel_init);
1236#endif // SIGPIPE
1237 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001238} // __kmp_install_signals
1239
Jonathan Peyton30419822017-05-12 18:01:32 +00001240void __kmp_remove_signals(void) {
1241 int sig;
1242 KB_TRACE(10, ("__kmp_remove_signals()\n"));
1243 for (sig = 1; sig < NSIG; ++sig) {
1244 __kmp_remove_one_handler(sig);
1245 }; // for sig
Jim Cownie5e8470a2013-09-27 10:38:44 +00001246} // __kmp_remove_signals
1247
Jim Cownie5e8470a2013-09-27 10:38:44 +00001248#endif // KMP_HANDLE_SIGNALS
1249
Jonathan Peyton30419822017-05-12 18:01:32 +00001250void __kmp_enable(int new_state) {
1251#ifdef KMP_CANCEL_THREADS
1252 int status, old_state;
1253 status = pthread_setcancelstate(new_state, &old_state);
1254 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
1255 KMP_DEBUG_ASSERT(old_state == PTHREAD_CANCEL_DISABLE);
1256#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001257}
1258
Jonathan Peyton30419822017-05-12 18:01:32 +00001259void __kmp_disable(int *old_state) {
1260#ifdef KMP_CANCEL_THREADS
1261 int status;
1262 status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state);
1263 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
1264#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001265}
1266
Jonathan Peyton30419822017-05-12 18:01:32 +00001267static void __kmp_atfork_prepare(void) { /* nothing to do */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001268}
1269
Jonathan Peyton30419822017-05-12 18:01:32 +00001270static void __kmp_atfork_parent(void) { /* nothing to do */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001271}
1272
Jonathan Peyton30419822017-05-12 18:01:32 +00001273/* Reset the library so execution in the child starts "all over again" with
1274 clean data structures in initial states. Don't worry about freeing memory
1275 allocated by parent, just abandon it to be safe. */
1276static void __kmp_atfork_child(void) {
1277 /* TODO make sure this is done right for nested/sibling */
1278 // ATT: Memory leaks are here? TODO: Check it and fix.
1279 /* KMP_ASSERT( 0 ); */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001280
Jonathan Peyton30419822017-05-12 18:01:32 +00001281 ++__kmp_fork_count;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001282
Jonathan Peytond330e632017-06-13 17:16:12 +00001283#if KMP_AFFINITY_SUPPORTED && KMP_OS_LINUX
1284 // reset the affinity in the child to the initial thread
1285 // affinity in the parent
1286 kmp_set_thread_affinity_mask_initial();
1287#endif
1288
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);
1375 };
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);
Jim Cownie5e8470a2013-09-27 10:38:44 +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);
1391 };
1392 --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 Cownie5e8470a2013-09-27 10:38:44 +00001397
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001398/* This routine puts the calling thread to sleep after setting the
Jonathan Peyton30419822017-05-12 18:01:32 +00001399 sleep bit for the indicated flag variable to true. */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001400template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +00001401static inline void __kmp_suspend_template(int th_gtid, C *flag) {
1402 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_suspend);
1403 kmp_info_t *th = __kmp_threads[th_gtid];
1404 int status;
1405 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001406
Jonathan Peyton30419822017-05-12 18:01:32 +00001407 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid,
1408 flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001409
Jonathan Peyton30419822017-05-12 18:01:32 +00001410 __kmp_suspend_initialize_thread(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001411
Jonathan Peyton30419822017-05-12 18:01:32 +00001412 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1413 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001414
Jonathan Peyton30419822017-05-12 18:01:32 +00001415 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1416 th_gtid, flag->get()));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001417
Jonathan Peyton30419822017-05-12 18:01:32 +00001418 /* TODO: shouldn't this use release semantics to ensure that
1419 __kmp_suspend_initialize_thread gets called first? */
1420 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001421
Jonathan Peyton30419822017-05-12 18:01:32 +00001422 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x,"
1423 " was %x\n",
1424 th_gtid, flag->get(), *(flag->get()), old_spin));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001425
Jonathan Peyton30419822017-05-12 18:01:32 +00001426 if (flag->done_check_val(old_spin)) {
1427 old_spin = flag->unset_sleeping();
1428 KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
1429 "for spin(%p)\n",
1430 th_gtid, flag->get()));
1431 } else {
1432 /* Encapsulate in a loop as the documentation states that this may
1433 "with low probability" return when the condition variable has
1434 not been signaled or broadcast */
1435 int deactivated = FALSE;
1436 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1437
1438 while (flag->is_sleeping()) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001439#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001440 char buffer[128];
1441 __kmp_suspend_count++;
1442 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1443 __kmp_printf("__kmp_suspend_template: suspending T#%d: %s\n", th_gtid,
1444 buffer);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001445#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001446 // Mark the thread as no longer active (only in the first iteration of the
1447 // loop).
1448 if (!deactivated) {
1449 th->th.th_active = FALSE;
1450 if (th->th.th_active_in_pool) {
1451 th->th.th_active_in_pool = FALSE;
1452 KMP_TEST_THEN_DEC32((kmp_int32 *)&__kmp_thread_pool_active_nth);
1453 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
1454 }
1455 deactivated = TRUE;
1456 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001457
1458#if USE_SUSPEND_TIMEOUT
Jonathan Peyton30419822017-05-12 18:01:32 +00001459 struct timespec now;
1460 struct timeval tval;
1461 int msecs;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001462
Jonathan Peyton30419822017-05-12 18:01:32 +00001463 status = gettimeofday(&tval, NULL);
1464 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1465 TIMEVAL_TO_TIMESPEC(&tval, &now);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001466
Jonathan Peyton30419822017-05-12 18:01:32 +00001467 msecs = (4 * __kmp_dflt_blocktime) + 200;
1468 now.tv_sec += msecs / 1000;
1469 now.tv_nsec += (msecs % 1000) * 1000;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001470
Jonathan Peyton30419822017-05-12 18:01:32 +00001471 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
1472 "pthread_cond_timedwait\n",
1473 th_gtid));
1474 status = pthread_cond_timedwait(&th->th.th_suspend_cv.c_cond,
1475 &th->th.th_suspend_mx.m_mutex, &now);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001476#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001477 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform"
1478 " pthread_cond_wait\n",
1479 th_gtid));
1480 status = pthread_cond_wait(&th->th.th_suspend_cv.c_cond,
1481 &th->th.th_suspend_mx.m_mutex);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001482#endif
1483
Jonathan Peyton30419822017-05-12 18:01:32 +00001484 if ((status != 0) && (status != EINTR) && (status != ETIMEDOUT)) {
1485 KMP_SYSFAIL("pthread_cond_wait", status);
1486 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001487#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001488 if (status == ETIMEDOUT) {
1489 if (flag->is_sleeping()) {
1490 KF_TRACE(100,
1491 ("__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid));
1492 } else {
1493 KF_TRACE(2, ("__kmp_suspend_template: T#%d timeout wakeup, sleep bit "
1494 "not set!\n",
1495 th_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001496 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001497 } else if (flag->is_sleeping()) {
1498 KF_TRACE(100,
1499 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
1500 }
1501#endif
1502 } // while
Jim Cownie5e8470a2013-09-27 10:38:44 +00001503
Jonathan Peyton30419822017-05-12 18:01:32 +00001504 // Mark the thread as active again (if it was previous marked as inactive)
1505 if (deactivated) {
1506 th->th.th_active = TRUE;
1507 if (TCR_4(th->th.th_in_pool)) {
1508 KMP_TEST_THEN_INC32((kmp_int32 *)&__kmp_thread_pool_active_nth);
1509 th->th.th_active_in_pool = TRUE;
1510 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001511 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001512 }
1513#ifdef DEBUG_SUSPEND
1514 {
1515 char buffer[128];
1516 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1517 __kmp_printf("__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid,
1518 buffer);
1519 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001520#endif
1521
Jonathan Peyton30419822017-05-12 18:01:32 +00001522 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1523 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1524 KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001525}
1526
1527void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001528 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001529}
1530void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001531 __kmp_suspend_template(th_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001532}
1533void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001534 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001535}
1536
Jim Cownie5e8470a2013-09-27 10:38:44 +00001537/* This routine signals the thread specified by target_gtid to wake up
Jonathan Peyton30419822017-05-12 18:01:32 +00001538 after setting the sleep bit indicated by the flag argument to FALSE.
1539 The target thread must already have called __kmp_suspend_template() */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001540template <class C>
Jonathan Peyton30419822017-05-12 18:01:32 +00001541static inline void __kmp_resume_template(int target_gtid, C *flag) {
1542 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1543 kmp_info_t *th = __kmp_threads[target_gtid];
1544 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001545
1546#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001547 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001548#endif
1549
Jonathan Peyton30419822017-05-12 18:01:32 +00001550 KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
1551 gtid, target_gtid));
1552 KMP_DEBUG_ASSERT(gtid != target_gtid);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001553
Jonathan Peyton30419822017-05-12 18:01:32 +00001554 __kmp_suspend_initialize_thread(th);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001555
Jonathan Peyton30419822017-05-12 18:01:32 +00001556 status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
1557 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001558
Jonathan Peyton30419822017-05-12 18:01:32 +00001559 if (!flag) { // coming from __kmp_null_resume_wrapper
1560 flag = (C *)th->th.th_sleep_loc;
1561 }
1562
1563 // First, check if the flag is null or its type has changed. If so, someone
1564 // else woke it up.
1565 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type
1566 // simply shows what
1567 // flag was cast to
1568 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1569 "awake: flag(%p)\n",
1570 gtid, target_gtid, NULL));
1571 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1572 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1573 return;
1574 } else { // if multiple threads are sleeping, flag should be internally
1575 // referring to a specific thread here
1576 typename C::flag_t old_spin = flag->unset_sleeping();
1577 if (!flag->is_sleeping_val(old_spin)) {
1578 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1579 "awake: flag(%p): "
1580 "%u => %u\n",
1581 gtid, target_gtid, flag->get(), old_spin, *flag->get()));
1582 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1583 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1584 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001585 }
Jonathan Peyton30419822017-05-12 18:01:32 +00001586 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset "
1587 "sleep bit for flag's loc(%p): "
1588 "%u => %u\n",
1589 gtid, target_gtid, flag->get(), old_spin, *flag->get()));
1590 }
1591 TCW_PTR(th->th.th_sleep_loc, NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001592
1593#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001594 {
1595 char buffer[128];
1596 __kmp_print_cond(buffer, &th->th.th_suspend_cv);
1597 __kmp_printf("__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid,
1598 target_gtid, buffer);
1599 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001600#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001601 status = pthread_cond_signal(&th->th.th_suspend_cv.c_cond);
1602 KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
1603 status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
1604 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1605 KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
1606 " for T#%d\n",
1607 gtid, target_gtid));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001608}
1609
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001610void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001611 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001612}
1613void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001614 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001615}
1616void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
Jonathan Peyton30419822017-05-12 18:01:32 +00001617 __kmp_resume_template(target_gtid, flag);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001618}
1619
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001620#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001621void __kmp_resume_monitor() {
1622 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
1623 int status;
Jim Cownie07ea89f2014-09-03 11:10:54 +00001624#ifdef KMP_DEBUG
Jonathan Peyton30419822017-05-12 18:01:32 +00001625 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1626 KF_TRACE(30, ("__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n", gtid,
1627 KMP_GTID_MONITOR));
1628 KMP_DEBUG_ASSERT(gtid != KMP_GTID_MONITOR);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001629#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001630 status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
1631 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001632#ifdef DEBUG_SUSPEND
Jonathan Peyton30419822017-05-12 18:01:32 +00001633 {
1634 char buffer[128];
1635 __kmp_print_cond(buffer, &__kmp_wait_cv.c_cond);
1636 __kmp_printf("__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid,
1637 KMP_GTID_MONITOR, buffer);
1638 }
Jim Cownie07ea89f2014-09-03 11:10:54 +00001639#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001640 status = pthread_cond_signal(&__kmp_wait_cv.c_cond);
1641 KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
1642 status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
1643 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
1644 KF_TRACE(30, ("__kmp_resume_monitor: T#%d exiting after signaling wake up"
1645 " for T#%d\n",
1646 gtid, KMP_GTID_MONITOR));
Jim Cownie07ea89f2014-09-03 11:10:54 +00001647}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001648#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001649
Jonathan Peyton30419822017-05-12 18:01:32 +00001650void __kmp_yield(int cond) {
1651 if (!cond)
1652 return;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001653#if KMP_USE_MONITOR
Jonathan Peyton30419822017-05-12 18:01:32 +00001654 if (!__kmp_yielding_on)
1655 return;
Jonathan Peyton581fdba2017-02-15 17:19:21 +00001656#else
Jonathan Peyton30419822017-05-12 18:01:32 +00001657 if (__kmp_yield_cycle && !KMP_YIELD_NOW())
1658 return;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001659#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00001660 sched_yield();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001661}
1662
Jonathan Peyton30419822017-05-12 18:01:32 +00001663void __kmp_gtid_set_specific(int gtid) {
1664 if (__kmp_init_gtid) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001665 int status;
Jonathan Peyton30419822017-05-12 18:01:32 +00001666 status = pthread_setspecific(__kmp_gtid_threadprivate_key,
1667 (void *)(intptr_t)(gtid + 1));
1668 KMP_CHECK_SYSFAIL("pthread_setspecific", status);
1669 } else {
1670 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
1671 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001672}
1673
Jonathan Peyton30419822017-05-12 18:01:32 +00001674int __kmp_gtid_get_specific() {
1675 int gtid;
1676 if (!__kmp_init_gtid) {
1677 KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
1678 "KMP_GTID_SHUTDOWN\n"));
1679 return KMP_GTID_SHUTDOWN;
1680 }
1681 gtid = (int)(size_t)pthread_getspecific(__kmp_gtid_threadprivate_key);
1682 if (gtid == 0) {
1683 gtid = KMP_GTID_DNE;
1684 } else {
1685 gtid--;
1686 }
1687 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1688 __kmp_gtid_threadprivate_key, gtid));
1689 return gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001690}
1691
Jonathan Peyton30419822017-05-12 18:01:32 +00001692double __kmp_read_cpu_time(void) {
1693 /*clock_t t;*/
1694 struct tms buffer;
1695
1696 /*t =*/times(&buffer);
1697
1698 return (buffer.tms_utime + buffer.tms_cutime) / (double)CLOCKS_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001699}
1700
Jonathan Peyton30419822017-05-12 18:01:32 +00001701int __kmp_read_system_info(struct kmp_sys_info *info) {
1702 int status;
1703 struct rusage r_usage;
1704
1705 memset(info, 0, sizeof(*info));
1706
1707 status = getrusage(RUSAGE_SELF, &r_usage);
1708 KMP_CHECK_SYSFAIL_ERRNO("getrusage", status);
1709
1710 // The maximum resident set size utilized (in kilobytes)
1711 info->maxrss = r_usage.ru_maxrss;
1712 // The number of page faults serviced without any I/O
1713 info->minflt = r_usage.ru_minflt;
1714 // The number of page faults serviced that required I/O
1715 info->majflt = r_usage.ru_majflt;
1716 // The number of times a process was "swapped" out of memory
1717 info->nswap = r_usage.ru_nswap;
1718 // The number of times the file system had to perform input
1719 info->inblock = r_usage.ru_inblock;
1720 // The number of times the file system had to perform output
1721 info->oublock = r_usage.ru_oublock;
1722 // The number of times a context switch was voluntarily
1723 info->nvcsw = r_usage.ru_nvcsw;
1724 // The number of times a context switch was forced
1725 info->nivcsw = r_usage.ru_nivcsw;
1726
1727 return (status != 0);
1728}
1729
1730void __kmp_read_system_time(double *delta) {
1731 double t_ns;
1732 struct timeval tval;
1733 struct timespec stop;
1734 int status;
1735
1736 status = gettimeofday(&tval, NULL);
1737 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1738 TIMEVAL_TO_TIMESPEC(&tval, &stop);
1739 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
1740 *delta = (t_ns * 1e-9);
1741}
1742
1743void __kmp_clear_system_time(void) {
1744 struct timeval tval;
1745 int status;
1746 status = gettimeofday(&tval, NULL);
1747 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1748 TIMEVAL_TO_TIMESPEC(&tval, &__kmp_sys_timer_data.start);
1749}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001750
1751#ifdef BUILD_TV
1752
Jonathan Peyton30419822017-05-12 18:01:32 +00001753void __kmp_tv_threadprivate_store(kmp_info_t *th, void *global_addr,
1754 void *thread_addr) {
1755 struct tv_data *p;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001756
Jonathan Peyton30419822017-05-12 18:01:32 +00001757 p = (struct tv_data *)__kmp_allocate(sizeof(*p));
Jim Cownie5e8470a2013-09-27 10:38:44 +00001758
Jonathan Peyton30419822017-05-12 18:01:32 +00001759 p->u.tp.global_addr = global_addr;
1760 p->u.tp.thread_addr = thread_addr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001761
Jonathan Peyton30419822017-05-12 18:01:32 +00001762 p->type = (void *)1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001763
Jonathan Peyton30419822017-05-12 18:01:32 +00001764 p->next = th->th.th_local.tv_data;
1765 th->th.th_local.tv_data = p;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001766
Jonathan Peyton30419822017-05-12 18:01:32 +00001767 if (p->next == 0) {
1768 int rc = pthread_setspecific(__kmp_tv_key, p);
1769 KMP_CHECK_SYSFAIL("pthread_setspecific", rc);
1770 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001771}
1772
1773#endif /* BUILD_TV */
1774
Jonathan Peyton30419822017-05-12 18:01:32 +00001775static int __kmp_get_xproc(void) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001776
Jonathan Peyton30419822017-05-12 18:01:32 +00001777 int r = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001778
Jonathan Peyton30419822017-05-12 18:01:32 +00001779#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00001780
Jonathan Peyton30419822017-05-12 18:01:32 +00001781 r = sysconf(_SC_NPROCESSORS_ONLN);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001782
Jonathan Peyton30419822017-05-12 18:01:32 +00001783#elif KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00001784
Jonathan Peyton30419822017-05-12 18:01:32 +00001785 // Bug C77011 High "OpenMP Threads and number of active cores".
Jim Cownie5e8470a2013-09-27 10:38:44 +00001786
Jonathan Peyton30419822017-05-12 18:01:32 +00001787 // Find the number of available CPUs.
1788 kern_return_t rc;
1789 host_basic_info_data_t info;
1790 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
1791 rc = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&info, &num);
1792 if (rc == 0 && num == HOST_BASIC_INFO_COUNT) {
1793// Cannot use KA_TRACE() here because this code works before trace support is
1794// initialized.
1795 r = info.avail_cpus;
1796 } else {
1797 KMP_WARNING(CantGetNumAvailCPU);
1798 KMP_INFORM(AssumedNumCPU);
1799 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001800
Jonathan Peyton30419822017-05-12 18:01:32 +00001801#else
Jim Cownie5e8470a2013-09-27 10:38:44 +00001802
Jonathan Peyton30419822017-05-12 18:01:32 +00001803#error "Unknown or unsupported OS."
Jim Cownie5e8470a2013-09-27 10:38:44 +00001804
Jonathan Peyton30419822017-05-12 18:01:32 +00001805#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001806
Jonathan Peyton30419822017-05-12 18:01:32 +00001807 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001808
1809} // __kmp_get_xproc
1810
Jonathan Peyton30419822017-05-12 18:01:32 +00001811int __kmp_read_from_file(char const *path, char const *format, ...) {
1812 int result;
1813 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001814
Jonathan Peyton30419822017-05-12 18:01:32 +00001815 va_start(args, format);
1816 FILE *f = fopen(path, "rb");
1817 if (f == NULL)
1818 return 0;
1819 result = vfscanf(f, format, args);
1820 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001821
Jonathan Peyton30419822017-05-12 18:01:32 +00001822 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00001823}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001824
Jonathan Peyton30419822017-05-12 18:01:32 +00001825void __kmp_runtime_initialize(void) {
1826 int status;
1827 pthread_mutexattr_t mutex_attr;
1828 pthread_condattr_t cond_attr;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001829
Jonathan Peyton30419822017-05-12 18:01:32 +00001830 if (__kmp_init_runtime) {
1831 return;
1832 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001833
Jonathan Peyton30419822017-05-12 18:01:32 +00001834#if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
1835 if (!__kmp_cpuinfo.initialized) {
1836 __kmp_query_cpuid(&__kmp_cpuinfo);
1837 }; // if
1838#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001839
Jonathan Peyton30419822017-05-12 18:01:32 +00001840 __kmp_xproc = __kmp_get_xproc();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001841
Jonathan Peyton30419822017-05-12 18:01:32 +00001842 if (sysconf(_SC_THREADS)) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001843
Jonathan Peyton30419822017-05-12 18:01:32 +00001844 /* Query the maximum number of threads */
1845 __kmp_sys_max_nth = sysconf(_SC_THREAD_THREADS_MAX);
1846 if (__kmp_sys_max_nth == -1) {
1847 /* Unlimited threads for NPTL */
1848 __kmp_sys_max_nth = INT_MAX;
1849 } else if (__kmp_sys_max_nth <= 1) {
1850 /* Can't tell, just use PTHREAD_THREADS_MAX */
1851 __kmp_sys_max_nth = KMP_MAX_NTH;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001852 }
1853
Jonathan Peyton30419822017-05-12 18:01:32 +00001854 /* Query the minimum stack size */
1855 __kmp_sys_min_stksize = sysconf(_SC_THREAD_STACK_MIN);
1856 if (__kmp_sys_min_stksize <= 1) {
1857 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
1858 }
1859 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001860
Jonathan Peyton30419822017-05-12 18:01:32 +00001861 /* Set up minimum number of threads to switch to TLS gtid */
1862 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001863
Jonathan Peyton30419822017-05-12 18:01:32 +00001864#ifdef BUILD_TV
1865 {
1866 int rc = pthread_key_create(&__kmp_tv_key, 0);
1867 KMP_CHECK_SYSFAIL("pthread_key_create", rc);
1868 }
1869#endif
1870
1871 status = pthread_key_create(&__kmp_gtid_threadprivate_key,
1872 __kmp_internal_end_dest);
1873 KMP_CHECK_SYSFAIL("pthread_key_create", status);
1874 status = pthread_mutexattr_init(&mutex_attr);
1875 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status);
1876 status = pthread_mutex_init(&__kmp_wait_mx.m_mutex, &mutex_attr);
1877 KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
1878 status = pthread_condattr_init(&cond_attr);
1879 KMP_CHECK_SYSFAIL("pthread_condattr_init", status);
1880 status = pthread_cond_init(&__kmp_wait_cv.c_cond, &cond_attr);
1881 KMP_CHECK_SYSFAIL("pthread_cond_init", status);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001882#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001883 __kmp_itt_initialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001884#endif /* USE_ITT_BUILD */
1885
Jonathan Peyton30419822017-05-12 18:01:32 +00001886 __kmp_init_runtime = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001887}
1888
Jonathan Peyton30419822017-05-12 18:01:32 +00001889void __kmp_runtime_destroy(void) {
1890 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001891
Jonathan Peyton30419822017-05-12 18:01:32 +00001892 if (!__kmp_init_runtime) {
1893 return; // Nothing to do.
1894 };
Jim Cownie5e8470a2013-09-27 10:38:44 +00001895
1896#if USE_ITT_BUILD
Jonathan Peyton30419822017-05-12 18:01:32 +00001897 __kmp_itt_destroy();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001898#endif /* USE_ITT_BUILD */
1899
Jonathan Peyton30419822017-05-12 18:01:32 +00001900 status = pthread_key_delete(__kmp_gtid_threadprivate_key);
1901 KMP_CHECK_SYSFAIL("pthread_key_delete", status);
1902#ifdef BUILD_TV
1903 status = pthread_key_delete(__kmp_tv_key);
1904 KMP_CHECK_SYSFAIL("pthread_key_delete", status);
1905#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001906
Jonathan Peyton30419822017-05-12 18:01:32 +00001907 status = pthread_mutex_destroy(&__kmp_wait_mx.m_mutex);
1908 if (status != 0 && status != EBUSY) {
1909 KMP_SYSFAIL("pthread_mutex_destroy", status);
1910 }
1911 status = pthread_cond_destroy(&__kmp_wait_cv.c_cond);
1912 if (status != 0 && status != EBUSY) {
1913 KMP_SYSFAIL("pthread_cond_destroy", status);
1914 }
1915#if KMP_AFFINITY_SUPPORTED
1916 __kmp_affinity_uninitialize();
1917#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001918
Jonathan Peyton30419822017-05-12 18:01:32 +00001919 __kmp_init_runtime = FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001920}
1921
Jim Cownie5e8470a2013-09-27 10:38:44 +00001922/* Put the thread to sleep for a time period */
1923/* NOTE: not currently used anywhere */
Jonathan Peyton30419822017-05-12 18:01:32 +00001924void __kmp_thread_sleep(int millis) { sleep((millis + 500) / 1000); }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001925
1926/* Calculate the elapsed wall clock time for the user */
Jonathan Peyton30419822017-05-12 18:01:32 +00001927void __kmp_elapsed(double *t) {
1928 int status;
1929#ifdef FIX_SGI_CLOCK
1930 struct timespec ts;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001931
Jonathan Peyton30419822017-05-12 18:01:32 +00001932 status = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
1933 KMP_CHECK_SYSFAIL_ERRNO("clock_gettime", status);
1934 *t =
1935 (double)ts.tv_nsec * (1.0 / (double)KMP_NSEC_PER_SEC) + (double)ts.tv_sec;
1936#else
1937 struct timeval tv;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001938
Jonathan Peyton30419822017-05-12 18:01:32 +00001939 status = gettimeofday(&tv, NULL);
1940 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
1941 *t =
1942 (double)tv.tv_usec * (1.0 / (double)KMP_USEC_PER_SEC) + (double)tv.tv_sec;
1943#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001944}
1945
1946/* Calculate the elapsed wall clock tick for the user */
Jonathan Peyton30419822017-05-12 18:01:32 +00001947void __kmp_elapsed_tick(double *t) { *t = 1 / (double)CLOCKS_PER_SEC; }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001948
Jonathan Peyton377aa402016-04-14 16:00:37 +00001949/* Return the current time stamp in nsec */
Jonathan Peyton30419822017-05-12 18:01:32 +00001950kmp_uint64 __kmp_now_nsec() {
1951 struct timeval t;
1952 gettimeofday(&t, NULL);
1953 return KMP_NSEC_PER_SEC * t.tv_sec + 1000 * t.tv_usec;
Jonathan Peyton377aa402016-04-14 16:00:37 +00001954}
1955
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001956#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton35d75ae2017-03-20 22:11:31 +00001957/* Measure clock ticks per millisecond */
Jonathan Peyton30419822017-05-12 18:01:32 +00001958void __kmp_initialize_system_tick() {
1959 kmp_uint64 delay = 100000; // 50~100 usec on most machines.
1960 kmp_uint64 nsec = __kmp_now_nsec();
1961 kmp_uint64 goal = __kmp_hardware_timestamp() + delay;
1962 kmp_uint64 now;
1963 while ((now = __kmp_hardware_timestamp()) < goal)
1964 ;
1965 __kmp_ticks_per_msec =
1966 (kmp_uint64)(1e6 * (delay + (now - goal)) / (__kmp_now_nsec() - nsec));
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001967}
1968#endif
1969
Jonathan Peyton30419822017-05-12 18:01:32 +00001970/* Determine whether the given address is mapped into the current address
1971 space. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001972
Jonathan Peyton30419822017-05-12 18:01:32 +00001973int __kmp_is_address_mapped(void *addr) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001974
Jonathan Peyton30419822017-05-12 18:01:32 +00001975 int found = 0;
1976 int rc;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001977
Jonathan Peyton30419822017-05-12 18:01:32 +00001978#if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00001979
Jonathan Peyton30419822017-05-12 18:01:32 +00001980 /* On Linux* OS, read the /proc/<pid>/maps pseudo-file to get all the address
1981 ranges mapped into the address space. */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001982
Jonathan Peyton30419822017-05-12 18:01:32 +00001983 char *name = __kmp_str_format("/proc/%d/maps", getpid());
1984 FILE *file = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001985
Jonathan Peyton30419822017-05-12 18:01:32 +00001986 file = fopen(name, "r");
1987 KMP_ASSERT(file != NULL);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001988
Jonathan Peyton30419822017-05-12 18:01:32 +00001989 for (;;) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001990
Jonathan Peyton30419822017-05-12 18:01:32 +00001991 void *beginning = NULL;
1992 void *ending = NULL;
1993 char perms[5];
Jim Cownie5e8470a2013-09-27 10:38:44 +00001994
Jonathan Peyton30419822017-05-12 18:01:32 +00001995 rc = fscanf(file, "%p-%p %4s %*[^\n]\n", &beginning, &ending, perms);
1996 if (rc == EOF) {
1997 break;
1998 }; // if
1999 KMP_ASSERT(rc == 3 &&
2000 KMP_STRLEN(perms) == 4); // Make sure all fields are read.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002001
Jonathan Peyton30419822017-05-12 18:01:32 +00002002 // Ending address is not included in the region, but beginning is.
2003 if ((addr >= beginning) && (addr < ending)) {
2004 perms[2] = 0; // 3th and 4th character does not matter.
2005 if (strcmp(perms, "rw") == 0) {
2006 // Memory we are looking for should be readable and writable.
Alp Toker763b9392014-02-28 09:42:41 +00002007 found = 1;
Jonathan Peyton30419822017-05-12 18:01:32 +00002008 }; // if
2009 break;
2010 }; // if
Alp Toker763b9392014-02-28 09:42:41 +00002011
Jonathan Peyton30419822017-05-12 18:01:32 +00002012 }; // forever
Jim Cownie5e8470a2013-09-27 10:38:44 +00002013
Jonathan Peyton30419822017-05-12 18:01:32 +00002014 // Free resources.
2015 fclose(file);
2016 KMP_INTERNAL_FREE(name);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002017
Jonathan Peyton30419822017-05-12 18:01:32 +00002018#elif KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002019
Jonathan Peyton30419822017-05-12 18:01:32 +00002020 /* On OS X*, /proc pseudo filesystem is not available. Try to read memory
2021 using vm interface. */
2022
2023 int buffer;
2024 vm_size_t count;
2025 rc = vm_read_overwrite(
2026 mach_task_self(), // Task to read memory of.
2027 (vm_address_t)(addr), // Address to read from.
2028 1, // Number of bytes to be read.
2029 (vm_address_t)(&buffer), // Address of buffer to save read bytes in.
2030 &count // Address of var to save number of read bytes in.
2031 );
2032 if (rc == 0) {
2033 // Memory successfully read.
2034 found = 1;
2035 }; // if
2036
2037#elif KMP_OS_FREEBSD || KMP_OS_NETBSD
2038
2039 // FIXME(FreeBSD, NetBSD): Implement this
2040 found = 1;
2041
2042#else
2043
2044#error "Unknown or unsupported OS"
2045
2046#endif
2047
2048 return found;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002049
2050} // __kmp_is_address_mapped
2051
2052#ifdef USE_LOAD_BALANCE
2053
Jonathan Peyton30419822017-05-12 18:01:32 +00002054#if KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002055
2056// The function returns the rounded value of the system load average
2057// during given time interval which depends on the value of
2058// __kmp_load_balance_interval variable (default is 60 sec, other values
2059// may be 300 sec or 900 sec).
2060// It returns -1 in case of error.
Jonathan Peyton30419822017-05-12 18:01:32 +00002061int __kmp_get_load_balance(int max) {
2062 double averages[3];
2063 int ret_avg = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002064
Jonathan Peyton30419822017-05-12 18:01:32 +00002065 int res = getloadavg(averages, 3);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002066
Jonathan Peyton30419822017-05-12 18:01:32 +00002067 // Check __kmp_load_balance_interval to determine which of averages to use.
2068 // getloadavg() may return the number of samples less than requested that is
2069 // less than 3.
2070 if (__kmp_load_balance_interval < 180 && (res >= 1)) {
2071 ret_avg = averages[0]; // 1 min
2072 } else if ((__kmp_load_balance_interval >= 180 &&
2073 __kmp_load_balance_interval < 600) &&
2074 (res >= 2)) {
2075 ret_avg = averages[1]; // 5 min
2076 } else if ((__kmp_load_balance_interval >= 600) && (res == 3)) {
2077 ret_avg = averages[2]; // 15 min
2078 } else { // Error occurred
2079 return -1;
2080 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002081
Jonathan Peyton30419822017-05-12 18:01:32 +00002082 return ret_avg;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002083}
2084
Jonathan Peyton30419822017-05-12 18:01:32 +00002085#else // Linux* OS
Jim Cownie5e8470a2013-09-27 10:38:44 +00002086
Jonathan Peyton30419822017-05-12 18:01:32 +00002087// The fuction returns number of running (not sleeping) threads, or -1 in case
2088// of error. Error could be reported if Linux* OS kernel too old (without
2089// "/proc" support). Counting running threads stops if max running threads
2090// encountered.
2091int __kmp_get_load_balance(int max) {
2092 static int permanent_error = 0;
2093 static int glb_running_threads = 0; // Saved count of the running threads for
2094 // the thread balance algortihm
2095 static double glb_call_time = 0; /* Thread balance algorithm call time */
Jim Cownie5e8470a2013-09-27 10:38:44 +00002096
Jonathan Peyton30419822017-05-12 18:01:32 +00002097 int running_threads = 0; // Number of running threads in the system.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002098
Jonathan Peyton30419822017-05-12 18:01:32 +00002099 DIR *proc_dir = NULL; // Handle of "/proc/" directory.
2100 struct dirent *proc_entry = NULL;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002101
Jonathan Peyton30419822017-05-12 18:01:32 +00002102 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2103 DIR *task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2104 struct dirent *task_entry = NULL;
2105 int task_path_fixed_len;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002106
Jonathan Peyton30419822017-05-12 18:01:32 +00002107 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2108 int stat_file = -1;
2109 int stat_path_fixed_len;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002110
Jonathan Peyton30419822017-05-12 18:01:32 +00002111 int total_processes = 0; // Total number of processes in system.
2112 int total_threads = 0; // Total number of threads in system.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002113
Jonathan Peyton30419822017-05-12 18:01:32 +00002114 double call_time = 0.0;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002115
Jonathan Peyton30419822017-05-12 18:01:32 +00002116 __kmp_str_buf_init(&task_path);
2117 __kmp_str_buf_init(&stat_path);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002118
Jonathan Peyton30419822017-05-12 18:01:32 +00002119 __kmp_elapsed(&call_time);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002120
Jonathan Peyton30419822017-05-12 18:01:32 +00002121 if (glb_call_time &&
2122 (call_time - glb_call_time < __kmp_load_balance_interval)) {
2123 running_threads = glb_running_threads;
2124 goto finish;
2125 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002126
Jonathan Peyton30419822017-05-12 18:01:32 +00002127 glb_call_time = call_time;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002128
Jonathan Peyton30419822017-05-12 18:01:32 +00002129 // Do not spend time on scanning "/proc/" if we have a permanent error.
2130 if (permanent_error) {
2131 running_threads = -1;
2132 goto finish;
2133 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00002134
Jonathan Peyton30419822017-05-12 18:01:32 +00002135 if (max <= 0) {
2136 max = INT_MAX;
2137 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00002138
Jonathan Peyton30419822017-05-12 18:01:32 +00002139 // Open "/proc/" directory.
2140 proc_dir = opendir("/proc");
2141 if (proc_dir == NULL) {
2142 // Cannot open "/prroc/". Probably the kernel does not support it. Return an
2143 // error now and in subsequent calls.
2144 running_threads = -1;
2145 permanent_error = 1;
2146 goto finish;
2147 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00002148
Jonathan Peyton30419822017-05-12 18:01:32 +00002149 // Initialize fixed part of task_path. This part will not change.
2150 __kmp_str_buf_cat(&task_path, "/proc/", 6);
2151 task_path_fixed_len = task_path.used; // Remember number of used characters.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002152
Jonathan Peyton30419822017-05-12 18:01:32 +00002153 proc_entry = readdir(proc_dir);
2154 while (proc_entry != NULL) {
2155 // Proc entry is a directory and name starts with a digit. Assume it is a
2156 // process' directory.
2157 if (proc_entry->d_type == DT_DIR && isdigit(proc_entry->d_name[0])) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00002158
Jonathan Peyton30419822017-05-12 18:01:32 +00002159 ++total_processes;
2160 // Make sure init process is the very first in "/proc", so we can replace
2161 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes ==
2162 // 1. We are going to check that total_processes == 1 => d_name == "1" is
2163 // true (where "=>" is implication). Since C++ does not have => operator,
2164 // let us replace it with its equivalent: a => b == ! a || b.
2165 KMP_DEBUG_ASSERT(total_processes != 1 ||
2166 strcmp(proc_entry->d_name, "1") == 0);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002167
Jonathan Peyton30419822017-05-12 18:01:32 +00002168 // Construct task_path.
2169 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
2170 __kmp_str_buf_cat(&task_path, proc_entry->d_name,
2171 KMP_STRLEN(proc_entry->d_name));
2172 __kmp_str_buf_cat(&task_path, "/task", 5);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002173
Jonathan Peyton30419822017-05-12 18:01:32 +00002174 task_dir = opendir(task_path.str);
2175 if (task_dir == NULL) {
2176 // Process can finish between reading "/proc/" directory entry and
2177 // opening process' "task/" directory. So, in general case we should not
2178 // complain, but have to skip this process and read the next one. But on
2179 // systems with no "task/" support we will spend lot of time to scan
2180 // "/proc/" tree again and again without any benefit. "init" process
2181 // (its pid is 1) should exist always, so, if we cannot open
2182 // "/proc/1/task/" directory, it means "task/" is not supported by
2183 // kernel. Report an error now and in the future.
2184 if (strcmp(proc_entry->d_name, "1") == 0) {
2185 running_threads = -1;
2186 permanent_error = 1;
2187 goto finish;
2188 }; // if
2189 } else {
2190 // Construct fixed part of stat file path.
2191 __kmp_str_buf_clear(&stat_path);
2192 __kmp_str_buf_cat(&stat_path, task_path.str, task_path.used);
2193 __kmp_str_buf_cat(&stat_path, "/", 1);
2194 stat_path_fixed_len = stat_path.used;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002195
Jonathan Peyton30419822017-05-12 18:01:32 +00002196 task_entry = readdir(task_dir);
2197 while (task_entry != NULL) {
2198 // It is a directory and name starts with a digit.
2199 if (proc_entry->d_type == DT_DIR && isdigit(task_entry->d_name[0])) {
2200 ++total_threads;
2201
2202 // Consruct complete stat file path. Easiest way would be:
2203 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str,
2204 // task_entry->d_name );
2205 // but seriae of __kmp_str_buf_cat works a bit faster.
2206 stat_path.used =
2207 stat_path_fixed_len; // Reset stat path to its fixed part.
2208 __kmp_str_buf_cat(&stat_path, task_entry->d_name,
2209 KMP_STRLEN(task_entry->d_name));
2210 __kmp_str_buf_cat(&stat_path, "/stat", 5);
2211
2212 // Note: Low-level API (open/read/close) is used. High-level API
2213 // (fopen/fclose) works ~ 30 % slower.
2214 stat_file = open(stat_path.str, O_RDONLY);
2215 if (stat_file == -1) {
2216 // We cannot report an error because task (thread) can terminate
2217 // just before reading this file.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002218 } else {
Jonathan Peyton30419822017-05-12 18:01:32 +00002219 /* Content of "stat" file looks like:
2220 24285 (program) S ...
Jim Cownie5e8470a2013-09-27 10:38:44 +00002221
Jonathan Peyton30419822017-05-12 18:01:32 +00002222 It is a single line (if program name does not include funny
2223 symbols). First number is a thread id, then name of executable
2224 file name in paretheses, then state of the thread. We need just
2225 thread state.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002226
Jonathan Peyton30419822017-05-12 18:01:32 +00002227 Good news: Length of program name is 15 characters max. Longer
2228 names are truncated.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002229
Jonathan Peyton30419822017-05-12 18:01:32 +00002230 Thus, we need rather short buffer: 15 chars for program name +
2231 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002232
Jonathan Peyton30419822017-05-12 18:01:32 +00002233 Bad news: Program name may contain special symbols like space,
2234 closing parenthesis, or even new line. This makes parsing
2235 "stat" file not 100 % reliable. In case of fanny program names
2236 parsing may fail (report incorrect thread state).
Jim Cownie5e8470a2013-09-27 10:38:44 +00002237
Jonathan Peyton30419822017-05-12 18:01:32 +00002238 Parsing "status" file looks more promissing (due to different
2239 file structure and escaping special symbols) but reading and
2240 parsing of "status" file works slower.
2241 -- ln
2242 */
2243 char buffer[65];
2244 int len;
2245 len = read(stat_file, buffer, sizeof(buffer) - 1);
2246 if (len >= 0) {
2247 buffer[len] = 0;
2248 // Using scanf:
2249 // sscanf( buffer, "%*d (%*s) %c ", & state );
2250 // looks very nice, but searching for a closing parenthesis
2251 // works a bit faster.
2252 char *close_parent = strstr(buffer, ") ");
2253 if (close_parent != NULL) {
2254 char state = *(close_parent + 2);
2255 if (state == 'R') {
2256 ++running_threads;
2257 if (running_threads >= max) {
2258 goto finish;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002259 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +00002260 }; // if
2261 }; // if
2262 }; // if
2263 close(stat_file);
2264 stat_file = -1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002265 }; // if
Jonathan Peyton30419822017-05-12 18:01:32 +00002266 }; // if
2267 task_entry = readdir(task_dir);
2268 }; // while
2269 closedir(task_dir);
2270 task_dir = NULL;
2271 }; // if
2272 }; // if
2273 proc_entry = readdir(proc_dir);
2274 }; // while
Jim Cownie5e8470a2013-09-27 10:38:44 +00002275
Jonathan Peyton30419822017-05-12 18:01:32 +00002276 // There _might_ be a timing hole where the thread executing this
2277 // code get skipped in the load balance, and running_threads is 0.
2278 // Assert in the debug builds only!!!
2279 KMP_DEBUG_ASSERT(running_threads > 0);
2280 if (running_threads <= 0) {
2281 running_threads = 1;
2282 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00002283
Jonathan Peyton30419822017-05-12 18:01:32 +00002284finish: // Clean up and exit.
2285 if (proc_dir != NULL) {
2286 closedir(proc_dir);
2287 }; // if
2288 __kmp_str_buf_free(&task_path);
2289 if (task_dir != NULL) {
2290 closedir(task_dir);
2291 }; // if
2292 __kmp_str_buf_free(&stat_path);
2293 if (stat_file != -1) {
2294 close(stat_file);
2295 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00002296
Jonathan Peyton30419822017-05-12 18:01:32 +00002297 glb_running_threads = running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002298
Jonathan Peyton30419822017-05-12 18:01:32 +00002299 return running_threads;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002300
2301} // __kmp_get_load_balance
2302
Jonathan Peyton30419822017-05-12 18:01:32 +00002303#endif // KMP_OS_DARWIN
Jim Cownie5e8470a2013-09-27 10:38:44 +00002304
2305#endif // USE_LOAD_BALANCE
2306
Andrey Churbanov44fea6b2017-04-17 11:58:20 +00002307#if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || KMP_ARCH_PPC64)
Jim Cownie3051f972014-08-07 10:12:54 +00002308
2309// we really only need the case with 1 argument, because CLANG always build
2310// a struct of pointers to shared variables referenced in the outlined function
Jonathan Peyton30419822017-05-12 18:01:32 +00002311int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int tid, int argc,
2312 void *p_argv[]
Jonathan Peyton122dd762015-07-13 18:55:45 +00002313#if OMPT_SUPPORT
Jonathan Peyton30419822017-05-12 18:01:32 +00002314 ,
2315 void **exit_frame_ptr
Jonathan Peyton122dd762015-07-13 18:55:45 +00002316#endif
Jonathan Peyton30419822017-05-12 18:01:32 +00002317 ) {
Jonathan Peyton122dd762015-07-13 18:55:45 +00002318#if OMPT_SUPPORT
2319 *exit_frame_ptr = __builtin_frame_address(0);
2320#endif
2321
Jim Cownie3051f972014-08-07 10:12:54 +00002322 switch (argc) {
2323 default:
2324 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2325 fflush(stderr);
2326 exit(-1);
2327 case 0:
2328 (*pkfn)(&gtid, &tid);
2329 break;
2330 case 1:
2331 (*pkfn)(&gtid, &tid, p_argv[0]);
2332 break;
2333 case 2:
2334 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2335 break;
2336 case 3:
2337 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2338 break;
2339 case 4:
2340 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2341 break;
2342 case 5:
2343 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2344 break;
2345 case 6:
2346 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2347 p_argv[5]);
2348 break;
2349 case 7:
2350 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2351 p_argv[5], p_argv[6]);
2352 break;
2353 case 8:
2354 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2355 p_argv[5], p_argv[6], p_argv[7]);
2356 break;
2357 case 9:
2358 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2359 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2360 break;
2361 case 10:
2362 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2363 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2364 break;
2365 case 11:
2366 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2367 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2368 break;
2369 case 12:
2370 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2371 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2372 p_argv[11]);
2373 break;
2374 case 13:
2375 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2376 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2377 p_argv[11], p_argv[12]);
2378 break;
2379 case 14:
2380 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2381 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2382 p_argv[11], p_argv[12], p_argv[13]);
2383 break;
2384 case 15:
2385 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2386 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2387 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2388 break;
2389 }
2390
Jonathan Peyton122dd762015-07-13 18:55:45 +00002391#if OMPT_SUPPORT
2392 *exit_frame_ptr = 0;
2393#endif
2394
Jim Cownie3051f972014-08-07 10:12:54 +00002395 return 1;
2396}
2397
2398#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002399
Jim Cownie5e8470a2013-09-27 10:38:44 +00002400// end of file //