blob: 5f42d722a42966a9f579ee17e6826ffd50108d05 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * z_Linux_util.c -- 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"
17#include "kmp_wrapper_getpid.h"
18#include "kmp_itt.h"
19#include "kmp_str.h"
20#include "kmp_i18n.h"
Paul Osmialowskifb043fd2016-05-16 09:44:11 +000021#include "kmp_lock.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000022#include "kmp_io.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000023#include "kmp_stats.h"
24#include "kmp_wait_release.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000025
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +000026#if !KMP_OS_FREEBSD && !KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +000027# include <alloca.h>
28#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000029#include <unistd.h>
30#include <math.h> // HUGE_VAL.
31#include <sys/time.h>
32#include <sys/times.h>
33#include <sys/resource.h>
34#include <sys/syscall.h>
35
Jim Cownie3051f972014-08-07 10:12:54 +000036#if KMP_OS_LINUX && !KMP_OS_CNK
Jim Cownie5e8470a2013-09-27 10:38:44 +000037# include <sys/sysinfo.h>
Paul Osmialowskifb043fd2016-05-16 09:44:11 +000038# if KMP_USE_FUTEX
Jim Cownie5e8470a2013-09-27 10:38:44 +000039// We should really include <futex.h>, but that causes compatibility problems on different
40// Linux* OS distributions that either require that you include (or break when you try to include)
41// <pci/types.h>.
42// Since all we need is the two macros below (which are part of the kernel ABI, so can't change)
43// we just define the constants here and don't include <futex.h>
44# ifndef FUTEX_WAIT
45# define FUTEX_WAIT 0
46# endif
47# ifndef FUTEX_WAKE
48# define FUTEX_WAKE 1
49# endif
50# endif
51#elif KMP_OS_DARWIN
52# include <sys/sysctl.h>
53# include <mach/mach.h>
Alp Toker763b9392014-02-28 09:42:41 +000054#elif KMP_OS_FREEBSD
Alp Toker763b9392014-02-28 09:42:41 +000055# include <pthread_np.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000056#endif
57
Jim Cownie5e8470a2013-09-27 10:38:44 +000058#include <dirent.h>
59#include <ctype.h>
60#include <fcntl.h>
61
62/* ------------------------------------------------------------------------ */
63/* ------------------------------------------------------------------------ */
64
65struct kmp_sys_timer {
66 struct timespec start;
67};
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
75 typedef void (* sig_func_t )( int );
76 STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[ NSIG ];
77 static sigset_t __kmp_sigset;
78#endif
79
80static int __kmp_init_runtime = FALSE;
81
82static int __kmp_fork_count = 0;
83
84static pthread_condattr_t __kmp_suspend_cond_attr;
85static pthread_mutexattr_t __kmp_suspend_mutex_attr;
86
87static kmp_cond_align_t __kmp_wait_cv;
88static kmp_mutex_align_t __kmp_wait_mx;
89
90/* ------------------------------------------------------------------------ */
91/* ------------------------------------------------------------------------ */
92
93#ifdef DEBUG_SUSPEND
94static void
95__kmp_print_cond( char *buffer, kmp_cond_align_t *cond )
96{
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000097 KMP_SNPRINTF( buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))",
Jim Cownie5e8470a2013-09-27 10:38:44 +000098 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
99 cond->c_cond.__c_waiting );
100}
101#endif
102
103/* ------------------------------------------------------------------------ */
104/* ------------------------------------------------------------------------ */
105
Jim Cownie3051f972014-08-07 10:12:54 +0000106#if ( KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000107
108/*
109 * Affinity support
110 */
111
112/*
113 * On some of the older OS's that we build on, these constants aren't present
114 * in <asm/unistd.h> #included from <sys.syscall.h>. They must be the same on
115 * all systems of the same arch where they are defined, and they cannot change.
116 * stone forever.
117 */
118
Jim Cownie181b4bb2013-12-23 17:28:57 +0000119# if KMP_ARCH_X86 || KMP_ARCH_ARM
Jim Cownie5e8470a2013-09-27 10:38:44 +0000120# ifndef __NR_sched_setaffinity
121# define __NR_sched_setaffinity 241
122# elif __NR_sched_setaffinity != 241
123# error Wrong code for setaffinity system call.
124# endif /* __NR_sched_setaffinity */
125# ifndef __NR_sched_getaffinity
126# define __NR_sched_getaffinity 242
127# elif __NR_sched_getaffinity != 242
128# error Wrong code for getaffinity system call.
129# endif /* __NR_sched_getaffinity */
130
Andrey Churbanovcbda8682015-01-13 14:43:35 +0000131# elif KMP_ARCH_AARCH64
132# ifndef __NR_sched_setaffinity
133# define __NR_sched_setaffinity 122
134# elif __NR_sched_setaffinity != 122
135# error Wrong code for setaffinity system call.
136# endif /* __NR_sched_setaffinity */
137# ifndef __NR_sched_getaffinity
138# define __NR_sched_getaffinity 123
139# elif __NR_sched_getaffinity != 123
140# error Wrong code for getaffinity system call.
141# endif /* __NR_sched_getaffinity */
142
Jim Cownie5e8470a2013-09-27 10:38:44 +0000143# elif KMP_ARCH_X86_64
144# ifndef __NR_sched_setaffinity
145# define __NR_sched_setaffinity 203
146# elif __NR_sched_setaffinity != 203
147# error Wrong code for setaffinity system call.
148# endif /* __NR_sched_setaffinity */
149# ifndef __NR_sched_getaffinity
150# define __NR_sched_getaffinity 204
151# elif __NR_sched_getaffinity != 204
152# error Wrong code for getaffinity system call.
153# endif /* __NR_sched_getaffinity */
154
Jim Cownie3051f972014-08-07 10:12:54 +0000155# elif KMP_ARCH_PPC64
156# ifndef __NR_sched_setaffinity
157# define __NR_sched_setaffinity 222
158# elif __NR_sched_setaffinity != 222
159# error Wrong code for setaffinity system call.
160# endif /* __NR_sched_setaffinity */
161# ifndef __NR_sched_getaffinity
162# define __NR_sched_getaffinity 223
163# elif __NR_sched_getaffinity != 223
164# error Wrong code for getaffinity system call.
165# endif /* __NR_sched_getaffinity */
166
167
Jim Cownie5e8470a2013-09-27 10:38:44 +0000168# else
169# error Unknown or unsupported architecture
170
171# endif /* KMP_ARCH_* */
172
173int
174__kmp_set_system_affinity( kmp_affin_mask_t const *mask, int abort_on_error )
175{
176 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
177 "Illegal set affinity operation when not capable");
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000178#if KMP_USE_HWLOC
179 int retval = hwloc_set_cpubind(__kmp_hwloc_topology, (hwloc_cpuset_t)mask, HWLOC_CPUBIND_THREAD);
180#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000181 int retval = syscall( __NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask );
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000182#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000183 if (retval >= 0) {
184 return 0;
185 }
186 int error = errno;
187 if (abort_on_error) {
188 __kmp_msg(
189 kmp_ms_fatal,
190 KMP_MSG( FatalSysError ),
191 KMP_ERR( error ),
192 __kmp_msg_null
193 );
194 }
195 return error;
196}
197
198int
199__kmp_get_system_affinity( kmp_affin_mask_t *mask, int abort_on_error )
200{
201 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
202 "Illegal get affinity operation when not capable");
203
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000204#if KMP_USE_HWLOC
205 int retval = hwloc_get_cpubind(__kmp_hwloc_topology, (hwloc_cpuset_t)mask, HWLOC_CPUBIND_THREAD);
206#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000207 int retval = syscall( __NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask );
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000208#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000209 if (retval >= 0) {
210 return 0;
211 }
212 int error = errno;
213 if (abort_on_error) {
214 __kmp_msg(
215 kmp_ms_fatal,
216 KMP_MSG( FatalSysError ),
217 KMP_ERR( error ),
218 __kmp_msg_null
219 );
220 }
221 return error;
222}
223
224void
225__kmp_affinity_bind_thread( int which )
226{
227 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
228 "Illegal set affinity operation when not capable");
229
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000230 kmp_affin_mask_t *mask;
231 KMP_CPU_ALLOC_ON_STACK(mask);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000232 KMP_CPU_ZERO(mask);
233 KMP_CPU_SET(which, mask);
234 __kmp_set_system_affinity(mask, TRUE);
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000235 KMP_CPU_FREE_FROM_STACK(mask);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000236}
237
238/*
239 * Determine if we can access affinity functionality on this version of
240 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
241 * __kmp_affin_mask_size to the appropriate value (0 means not capable).
242 */
243void
244__kmp_affinity_determine_capable(const char *env_var)
245{
246 //
247 // Check and see if the OS supports thread affinity.
248 //
249
250# define KMP_CPU_SET_SIZE_LIMIT (1024*1024)
251
252 int gCode;
253 int sCode;
254 kmp_affin_mask_t *buf;
255 buf = ( kmp_affin_mask_t * ) KMP_INTERNAL_MALLOC( KMP_CPU_SET_SIZE_LIMIT );
256
257 // If Linux* OS:
258 // If the syscall fails or returns a suggestion for the size,
259 // then we don't have to search for an appropriate size.
260 gCode = syscall( __NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf );
261 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
Alp Toker8f2d3f02014-02-24 10:40:15 +0000262 "initial getaffinity call returned %d errno = %d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +0000263 gCode, errno));
264
265 //if ((gCode < 0) && (errno == ENOSYS))
266 if (gCode < 0) {
267 //
268 // System call not supported
269 //
270 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
271 && (__kmp_affinity_type != affinity_none)
272 && (__kmp_affinity_type != affinity_default)
273 && (__kmp_affinity_type != affinity_disabled))) {
274 int error = errno;
275 __kmp_msg(
276 kmp_ms_warning,
277 KMP_MSG( GetAffSysCallNotSupported, env_var ),
278 KMP_ERR( error ),
279 __kmp_msg_null
280 );
281 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000282 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000283 KMP_INTERNAL_FREE(buf);
284 return;
285 }
286 if (gCode > 0) { // Linux* OS only
287 // The optimal situation: the OS returns the size of the buffer
288 // it expects.
289 //
290 // A verification of correct behavior is that Isetaffinity on a NULL
291 // buffer with the same size fails with errno set to EFAULT.
292 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
293 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
294 "setaffinity for mask size %d returned %d errno = %d\n",
295 gCode, sCode, errno));
296 if (sCode < 0) {
297 if (errno == ENOSYS) {
298 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
299 && (__kmp_affinity_type != affinity_none)
300 && (__kmp_affinity_type != affinity_default)
301 && (__kmp_affinity_type != affinity_disabled))) {
302 int error = errno;
303 __kmp_msg(
304 kmp_ms_warning,
305 KMP_MSG( SetAffSysCallNotSupported, env_var ),
306 KMP_ERR( error ),
307 __kmp_msg_null
308 );
309 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000310 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000311 KMP_INTERNAL_FREE(buf);
312 }
313 if (errno == EFAULT) {
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000314 KMP_AFFINITY_ENABLE(gCode);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000315 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
316 "affinity supported (mask size %d)\n",
317 (int)__kmp_affin_mask_size));
318 KMP_INTERNAL_FREE(buf);
319 return;
320 }
321 }
322 }
323
324 //
325 // Call the getaffinity system call repeatedly with increasing set sizes
326 // until we succeed, or reach an upper bound on the search.
327 //
328 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
329 "searching for proper set size\n"));
330 int size;
331 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
332 gCode = syscall( __NR_sched_getaffinity, 0, size, buf );
333 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
334 "getaffinity for mask size %d returned %d errno = %d\n", size,
335 gCode, errno));
336
337 if (gCode < 0) {
338 if ( errno == ENOSYS )
339 {
340 //
341 // We shouldn't get here
342 //
343 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
344 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
345 size));
346 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
347 && (__kmp_affinity_type != affinity_none)
348 && (__kmp_affinity_type != affinity_default)
349 && (__kmp_affinity_type != affinity_disabled))) {
350 int error = errno;
351 __kmp_msg(
352 kmp_ms_warning,
353 KMP_MSG( GetAffSysCallNotSupported, env_var ),
354 KMP_ERR( error ),
355 __kmp_msg_null
356 );
357 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000358 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000359 KMP_INTERNAL_FREE(buf);
360 return;
361 }
362 continue;
363 }
364
365 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
366 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
367 "setaffinity for mask size %d returned %d errno = %d\n",
368 gCode, sCode, errno));
369 if (sCode < 0) {
370 if (errno == ENOSYS) { // Linux* OS only
371 //
372 // We shouldn't get here
373 //
374 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
375 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
376 size));
377 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
378 && (__kmp_affinity_type != affinity_none)
379 && (__kmp_affinity_type != affinity_default)
380 && (__kmp_affinity_type != affinity_disabled))) {
381 int error = errno;
382 __kmp_msg(
383 kmp_ms_warning,
384 KMP_MSG( SetAffSysCallNotSupported, env_var ),
385 KMP_ERR( error ),
386 __kmp_msg_null
387 );
388 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000389 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000390 KMP_INTERNAL_FREE(buf);
391 return;
392 }
393 if (errno == EFAULT) {
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000394 KMP_AFFINITY_ENABLE(gCode);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000395 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
396 "affinity supported (mask size %d)\n",
397 (int)__kmp_affin_mask_size));
398 KMP_INTERNAL_FREE(buf);
399 return;
400 }
401 }
402 }
403 //int error = errno; // save uncaught error code
404 KMP_INTERNAL_FREE(buf);
405 // errno = error; // restore uncaught error code, will be printed at the next KMP_WARNING below
406
407 //
408 // Affinity is not supported
409 //
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000410 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000411 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
412 "cannot determine mask size - affinity not supported\n"));
413 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
414 && (__kmp_affinity_type != affinity_none)
415 && (__kmp_affinity_type != affinity_default)
416 && (__kmp_affinity_type != affinity_disabled))) {
417 KMP_WARNING( AffCantGetMaskSize, env_var );
418 }
419}
420
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000421#endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000422
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000423/* ------------------------------------------------------------------------ */
424/* ------------------------------------------------------------------------ */
425
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000426#if KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000427
428int
429__kmp_futex_determine_capable()
430{
431 int loc = 0;
432 int rc = syscall( __NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0 );
433 int retval = ( rc == 0 ) || ( errno != ENOSYS );
434
435 KA_TRACE(10, ( "__kmp_futex_determine_capable: rc = %d errno = %d\n", rc,
436 errno ) );
437 KA_TRACE(10, ( "__kmp_futex_determine_capable: futex syscall%s supported\n",
438 retval ? "" : " not" ) );
439
440 return retval;
441}
442
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000443#endif // KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000444
445/* ------------------------------------------------------------------------ */
446/* ------------------------------------------------------------------------ */
447
448#if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000449/*
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000450 * Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
451 * use compare_and_store for these routines
Jim Cownie5e8470a2013-09-27 10:38:44 +0000452 */
453
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000454kmp_int8
455__kmp_test_then_or8( volatile kmp_int8 *p, kmp_int8 d )
456{
457 kmp_int8 old_value, new_value;
458
459 old_value = TCR_1( *p );
460 new_value = old_value | d;
461
462 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
463 {
464 KMP_CPU_PAUSE();
465 old_value = TCR_1( *p );
466 new_value = old_value | d;
467 }
468 return old_value;
469}
470
471kmp_int8
472__kmp_test_then_and8( volatile kmp_int8 *p, kmp_int8 d )
473{
474 kmp_int8 old_value, new_value;
475
476 old_value = TCR_1( *p );
477 new_value = old_value & d;
478
479 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
480 {
481 KMP_CPU_PAUSE();
482 old_value = TCR_1( *p );
483 new_value = old_value & d;
484 }
485 return old_value;
486}
487
Jim Cownie5e8470a2013-09-27 10:38:44 +0000488kmp_int32
489__kmp_test_then_or32( volatile kmp_int32 *p, kmp_int32 d )
490{
491 kmp_int32 old_value, new_value;
492
493 old_value = TCR_4( *p );
494 new_value = old_value | d;
495
Jim Cownie3051f972014-08-07 10:12:54 +0000496 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000497 {
498 KMP_CPU_PAUSE();
499 old_value = TCR_4( *p );
500 new_value = old_value | d;
501 }
502 return old_value;
503}
504
505kmp_int32
506__kmp_test_then_and32( volatile kmp_int32 *p, kmp_int32 d )
507{
508 kmp_int32 old_value, new_value;
509
510 old_value = TCR_4( *p );
511 new_value = old_value & d;
512
Jim Cownie3051f972014-08-07 10:12:54 +0000513 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000514 {
515 KMP_CPU_PAUSE();
516 old_value = TCR_4( *p );
517 new_value = old_value & d;
518 }
519 return old_value;
520}
521
Paul Osmialowski7e5e8682016-05-13 08:26:42 +0000522# if KMP_ARCH_X86 || KMP_ARCH_PPC64 || (KMP_OS_LINUX && KMP_ARCH_AARCH64)
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000523kmp_int8
524__kmp_test_then_add8( volatile kmp_int8 *p, kmp_int8 d )
525{
526 kmp_int8 old_value, new_value;
527
528 old_value = TCR_1( *p );
529 new_value = old_value + d;
530
531 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
532 {
533 KMP_CPU_PAUSE();
534 old_value = TCR_1( *p );
535 new_value = old_value + d;
536 }
537 return old_value;
538}
539
Jim Cownie5e8470a2013-09-27 10:38:44 +0000540kmp_int64
541__kmp_test_then_add64( volatile kmp_int64 *p, kmp_int64 d )
542{
543 kmp_int64 old_value, new_value;
544
545 old_value = TCR_8( *p );
546 new_value = old_value + d;
547
Jim Cownie3051f972014-08-07 10:12:54 +0000548 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000549 {
550 KMP_CPU_PAUSE();
551 old_value = TCR_8( *p );
552 new_value = old_value + d;
553 }
554 return old_value;
555}
Paul Osmialowski7e5e8682016-05-13 08:26:42 +0000556# endif /* KMP_ARCH_X86 || KMP_ARCH_PPC64 || (KMP_OS_LINUX && KMP_ARCH_AARCH64) */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000557
558kmp_int64
559__kmp_test_then_or64( volatile kmp_int64 *p, kmp_int64 d )
560{
561 kmp_int64 old_value, new_value;
562
563 old_value = TCR_8( *p );
564 new_value = old_value | d;
Jim Cownie3051f972014-08-07 10:12:54 +0000565 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000566 {
567 KMP_CPU_PAUSE();
568 old_value = TCR_8( *p );
569 new_value = old_value | d;
570 }
571 return old_value;
572}
573
574kmp_int64
575__kmp_test_then_and64( volatile kmp_int64 *p, kmp_int64 d )
576{
577 kmp_int64 old_value, new_value;
578
579 old_value = TCR_8( *p );
580 new_value = old_value & d;
Jim Cownie3051f972014-08-07 10:12:54 +0000581 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000582 {
583 KMP_CPU_PAUSE();
584 old_value = TCR_8( *p );
585 new_value = old_value & d;
586 }
587 return old_value;
588}
589
590#endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
591
592void
593__kmp_terminate_thread( int gtid )
594{
595 int status;
596 kmp_info_t *th = __kmp_threads[ gtid ];
597
598 if ( !th ) return;
599
600 #ifdef KMP_CANCEL_THREADS
601 KA_TRACE( 10, ("__kmp_terminate_thread: kill (%d)\n", gtid ) );
602 status = pthread_cancel( th->th.th_info.ds.ds_thread );
603 if ( status != 0 && status != ESRCH ) {
604 __kmp_msg(
605 kmp_ms_fatal,
606 KMP_MSG( CantTerminateWorkerThread ),
607 KMP_ERR( status ),
608 __kmp_msg_null
609 );
610 }; // if
611 #endif
612 __kmp_yield( TRUE );
613} //
614
615/* ------------------------------------------------------------------------ */
616/* ------------------------------------------------------------------------ */
617
618/* ------------------------------------------------------------------------ */
619/* ------------------------------------------------------------------------ */
620
621/*
622 * Set thread stack info according to values returned by
623 * pthread_getattr_np().
624 * If values are unreasonable, assume call failed and use
625 * incremental stack refinement method instead.
626 * Returns TRUE if the stack parameters could be determined exactly,
627 * FALSE if incremental refinement is necessary.
628 */
629static kmp_int32
630__kmp_set_stack_info( int gtid, kmp_info_t *th )
631{
632 int stack_data;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000633#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000634 /* Linux* OS only -- no pthread_getattr_np support on OS X* */
635 pthread_attr_t attr;
636 int status;
637 size_t size = 0;
638 void * addr = 0;
639
640 /* Always do incremental stack refinement for ubermaster threads since the initial
641 thread stack range can be reduced by sibling thread creation so pthread_attr_getstack
642 may cause thread gtid aliasing */
643 if ( ! KMP_UBER_GTID(gtid) ) {
644
645 /* Fetch the real thread attributes */
646 status = pthread_attr_init( &attr );
647 KMP_CHECK_SYSFAIL( "pthread_attr_init", status );
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000648#if KMP_OS_FREEBSD || KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +0000649 status = pthread_attr_get_np( pthread_self(), &attr );
650 KMP_CHECK_SYSFAIL( "pthread_attr_get_np", status );
651#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000652 status = pthread_getattr_np( pthread_self(), &attr );
653 KMP_CHECK_SYSFAIL( "pthread_getattr_np", status );
Alp Toker763b9392014-02-28 09:42:41 +0000654#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000655 status = pthread_attr_getstack( &attr, &addr, &size );
656 KMP_CHECK_SYSFAIL( "pthread_attr_getstack", status );
657 KA_TRACE( 60, ( "__kmp_set_stack_info: T#%d pthread_attr_getstack returned size: %lu, "
658 "low addr: %p\n",
659 gtid, size, addr ));
660
661 status = pthread_attr_destroy( &attr );
662 KMP_CHECK_SYSFAIL( "pthread_attr_destroy", status );
663 }
664
665 if ( size != 0 && addr != 0 ) { /* was stack parameter determination successful? */
666 /* Store the correct base and size */
667 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size));
668 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
669 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
670 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000671 }
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000672#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD */
Alp Toker763b9392014-02-28 09:42:41 +0000673 /* Use incremental refinement starting from initial conservative estimate */
674 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
675 TCW_PTR(th -> th.th_info.ds.ds_stackbase, &stack_data);
676 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
677 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000678}
679
680static void*
681__kmp_launch_worker( void *thr )
682{
683 int status, old_type, old_state;
684#ifdef KMP_BLOCK_SIGNALS
685 sigset_t new_set, old_set;
686#endif /* KMP_BLOCK_SIGNALS */
687 void *exit_val;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000688#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Andrey Churbanov368b70e2015-08-05 11:12:45 +0000689 void * volatile padding = 0;
Jonathan Peyton2321d572015-06-08 19:25:25 +0000690#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000691 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000692
693 gtid = ((kmp_info_t*)thr) -> th.th_info.ds.ds_gtid;
694 __kmp_gtid_set_specific( gtid );
695#ifdef KMP_TDATA_GTID
696 __kmp_gtid = gtid;
697#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000698#if KMP_STATS_ENABLED
699 // set __thread local index to point to thread-specific stats
700 __kmp_stats_thread_ptr = ((kmp_info_t*)thr)->th.th_stats;
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000701 KMP_START_EXPLICIT_TIMER(OMP_worker_thread_life);
702 KMP_SET_THREAD_STATE(IDLE);
703 KMP_INIT_PARTITIONED_TIMERS(OMP_idle);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000704#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000705
706#if USE_ITT_BUILD
707 __kmp_itt_thread_name( gtid );
708#endif /* USE_ITT_BUILD */
709
Alp Toker763b9392014-02-28 09:42:41 +0000710#if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000711 __kmp_affinity_set_init_mask( gtid, FALSE );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000712#endif
713
714#ifdef KMP_CANCEL_THREADS
715 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
716 KMP_CHECK_SYSFAIL( "pthread_setcanceltype", status );
717 /* josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? */
718 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
719 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
720#endif
721
722#if KMP_ARCH_X86 || KMP_ARCH_X86_64
723 //
724 // Set the FP control regs to be a copy of
725 // the parallel initialization thread's.
726 //
727 __kmp_clear_x87_fpu_status_word();
728 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
729 __kmp_load_mxcsr( &__kmp_init_mxcsr );
730#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
731
732#ifdef KMP_BLOCK_SIGNALS
733 status = sigfillset( & new_set );
734 KMP_CHECK_SYSFAIL_ERRNO( "sigfillset", status );
735 status = pthread_sigmask( SIG_BLOCK, & new_set, & old_set );
736 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
737#endif /* KMP_BLOCK_SIGNALS */
738
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000739#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000740 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
Andrey Churbanov74bf17b2015-04-02 13:27:08 +0000741 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000742 }
743#endif
744
745 KMP_MB();
746 __kmp_set_stack_info( gtid, (kmp_info_t*)thr );
747
748 __kmp_check_stack_overlap( (kmp_info_t*)thr );
749
750 exit_val = __kmp_launch_thread( (kmp_info_t *) thr );
751
752#ifdef KMP_BLOCK_SIGNALS
753 status = pthread_sigmask( SIG_SETMASK, & old_set, NULL );
754 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
755#endif /* KMP_BLOCK_SIGNALS */
756
757 return exit_val;
758}
759
Jim Cownie5e8470a2013-09-27 10:38:44 +0000760/* The monitor thread controls all of the threads in the complex */
761
762static void*
763__kmp_launch_monitor( void *thr )
764{
765 int status, old_type, old_state;
766#ifdef KMP_BLOCK_SIGNALS
767 sigset_t new_set;
768#endif /* KMP_BLOCK_SIGNALS */
769 struct timespec interval;
770 int yield_count;
771 int yield_cycles = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000772
773 KMP_MB(); /* Flush all pending memory write invalidates. */
774
775 KA_TRACE( 10, ("__kmp_launch_monitor: #1 launched\n" ) );
776
777 /* register us as the monitor thread */
778 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
779#ifdef KMP_TDATA_GTID
780 __kmp_gtid = KMP_GTID_MONITOR;
781#endif
782
783 KMP_MB();
784
785#if USE_ITT_BUILD
786 __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore monitor thread.
787#endif /* USE_ITT_BUILD */
788
789 __kmp_set_stack_info( ((kmp_info_t*)thr)->th.th_info.ds.ds_gtid, (kmp_info_t*)thr );
790
791 __kmp_check_stack_overlap( (kmp_info_t*)thr );
792
793#ifdef KMP_CANCEL_THREADS
794 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
795 KMP_CHECK_SYSFAIL( "pthread_setcanceltype", status );
796 /* josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? */
797 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
798 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
799#endif
800
801 #if KMP_REAL_TIME_FIX
802 // This is a potential fix which allows application with real-time scheduling policy work.
803 // However, decision about the fix is not made yet, so it is disabled by default.
804 { // Are program started with real-time scheduling policy?
805 int sched = sched_getscheduler( 0 );
806 if ( sched == SCHED_FIFO || sched == SCHED_RR ) {
807 // Yes, we are a part of real-time application. Try to increase the priority of the
808 // monitor.
809 struct sched_param param;
810 int max_priority = sched_get_priority_max( sched );
811 int rc;
812 KMP_WARNING( RealTimeSchedNotSupported );
813 sched_getparam( 0, & param );
814 if ( param.sched_priority < max_priority ) {
815 param.sched_priority += 1;
816 rc = sched_setscheduler( 0, sched, & param );
817 if ( rc != 0 ) {
818 int error = errno;
819 __kmp_msg(
820 kmp_ms_warning,
821 KMP_MSG( CantChangeMonitorPriority ),
822 KMP_ERR( error ),
823 KMP_MSG( MonitorWillStarve ),
824 __kmp_msg_null
825 );
826 }; // if
827 } else {
828 // We cannot abort here, because number of CPUs may be enough for all the threads,
829 // including the monitor thread, so application could potentially work...
830 __kmp_msg(
831 kmp_ms_warning,
832 KMP_MSG( RunningAtMaxPriority ),
833 KMP_MSG( MonitorWillStarve ),
834 KMP_HNT( RunningAtMaxPriority ),
835 __kmp_msg_null
836 );
837 }; // if
838 }; // if
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000839 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 ); // AC: free thread that waits for monitor started
Jim Cownie5e8470a2013-09-27 10:38:44 +0000840 }
841 #endif // KMP_REAL_TIME_FIX
842
843 KMP_MB(); /* Flush all pending memory write invalidates. */
844
845 if ( __kmp_monitor_wakeups == 1 ) {
846 interval.tv_sec = 1;
847 interval.tv_nsec = 0;
848 } else {
849 interval.tv_sec = 0;
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +0000850 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000851 }
852
853 KA_TRACE( 10, ("__kmp_launch_monitor: #2 monitor\n" ) );
854
855 if (__kmp_yield_cycle) {
856 __kmp_yielding_on = 0; /* Start out with yielding shut off */
857 yield_count = __kmp_yield_off_count;
858 } else {
859 __kmp_yielding_on = 1; /* Yielding is on permanently */
860 }
861
862 while( ! TCR_4( __kmp_global.g.g_done ) ) {
863 struct timespec now;
864 struct timeval tval;
865
866 /* This thread monitors the state of the system */
867
868 KA_TRACE( 15, ( "__kmp_launch_monitor: update\n" ) );
869
870 status = gettimeofday( &tval, NULL );
871 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
872 TIMEVAL_TO_TIMESPEC( &tval, &now );
873
874 now.tv_sec += interval.tv_sec;
875 now.tv_nsec += interval.tv_nsec;
876
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +0000877 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000878 now.tv_sec += 1;
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +0000879 now.tv_nsec -= KMP_NSEC_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000880 }
881
882 status = pthread_mutex_lock( & __kmp_wait_mx.m_mutex );
883 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie07ea89f2014-09-03 11:10:54 +0000884 // AC: the monitor should not fall asleep if g_done has been set
885 if ( !TCR_4(__kmp_global.g.g_done) ) { // check once more under mutex
886 status = pthread_cond_timedwait( &__kmp_wait_cv.c_cond, &__kmp_wait_mx.m_mutex, &now );
887 if ( status != 0 ) {
888 if ( status != ETIMEDOUT && status != EINTR ) {
889 KMP_SYSFAIL( "pthread_cond_timedwait", status );
890 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000891 };
892 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000893 status = pthread_mutex_unlock( & __kmp_wait_mx.m_mutex );
894 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
895
896 if (__kmp_yield_cycle) {
897 yield_cycles++;
898 if ( (yield_cycles % yield_count) == 0 ) {
899 if (__kmp_yielding_on) {
900 __kmp_yielding_on = 0; /* Turn it off now */
901 yield_count = __kmp_yield_off_count;
902 } else {
903 __kmp_yielding_on = 1; /* Turn it on now */
904 yield_count = __kmp_yield_on_count;
905 }
906 yield_cycles = 0;
907 }
908 } else {
909 __kmp_yielding_on = 1;
910 }
911
912 TCW_4( __kmp_global.g.g_time.dt.t_value,
913 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
914
915 KMP_MB(); /* Flush all pending memory write invalidates. */
916 }
917
918 KA_TRACE( 10, ("__kmp_launch_monitor: #3 cleanup\n" ) );
919
920#ifdef KMP_BLOCK_SIGNALS
921 status = sigfillset( & new_set );
922 KMP_CHECK_SYSFAIL_ERRNO( "sigfillset", status );
923 status = pthread_sigmask( SIG_UNBLOCK, & new_set, NULL );
924 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
925#endif /* KMP_BLOCK_SIGNALS */
926
927 KA_TRACE( 10, ("__kmp_launch_monitor: #4 finished\n" ) );
928
929 if( __kmp_global.g.g_abort != 0 ) {
930 /* now we need to terminate the worker threads */
931 /* the value of t_abort is the signal we caught */
932
933 int gtid;
934
935 KA_TRACE( 10, ("__kmp_launch_monitor: #5 terminate sig=%d\n", __kmp_global.g.g_abort ) );
936
937 /* terminate the OpenMP worker threads */
938 /* TODO this is not valid for sibling threads!!
939 * the uber master might not be 0 anymore.. */
940 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
941 __kmp_terminate_thread( gtid );
942
943 __kmp_cleanup();
944
945 KA_TRACE( 10, ("__kmp_launch_monitor: #6 raise sig=%d\n", __kmp_global.g.g_abort ) );
946
947 if (__kmp_global.g.g_abort > 0)
948 raise( __kmp_global.g.g_abort );
949
950 }
951
952 KA_TRACE( 10, ("__kmp_launch_monitor: #7 exit\n" ) );
953
954 return thr;
955}
956
957void
958__kmp_create_worker( int gtid, kmp_info_t *th, size_t stack_size )
959{
960 pthread_t handle;
961 pthread_attr_t thread_attr;
962 int status;
963
964
965 th->th.th_info.ds.ds_gtid = gtid;
966
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000967#if KMP_STATS_ENABLED
968 // sets up worker thread stats
969 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
970
971 // th->th.th_stats is used to transfer thread specific stats-pointer to __kmp_launch_worker
972 // So when thread is created (goes into __kmp_launch_worker) it will
973 // set it's __thread local pointer to th->th.th_stats
974 th->th.th_stats = __kmp_stats_list.push_back(gtid);
975 if(KMP_UBER_GTID(gtid)) {
976 __kmp_stats_start_time = tsc_tick_count::now();
977 __kmp_stats_thread_ptr = th->th.th_stats;
978 __kmp_stats_init();
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000979 KMP_START_EXPLICIT_TIMER(OMP_worker_thread_life);
980 KMP_SET_THREAD_STATE(SERIAL_REGION);
981 KMP_INIT_PARTITIONED_TIMERS(OMP_serial);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000982 }
983 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
984
985#endif // KMP_STATS_ENABLED
986
Jim Cownie5e8470a2013-09-27 10:38:44 +0000987 if ( KMP_UBER_GTID(gtid) ) {
988 KA_TRACE( 10, ("__kmp_create_worker: uber thread (%d)\n", gtid ) );
989 th -> th.th_info.ds.ds_thread = pthread_self();
990 __kmp_set_stack_info( gtid, th );
991 __kmp_check_stack_overlap( th );
992 return;
993 }; // if
994
995 KA_TRACE( 10, ("__kmp_create_worker: try to create thread (%d)\n", gtid ) );
996
997 KMP_MB(); /* Flush all pending memory write invalidates. */
998
999#ifdef KMP_THREAD_ATTR
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001000 status = pthread_attr_init( &thread_attr );
1001 if ( status != 0 ) {
1002 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantInitThreadAttrs ), KMP_ERR( status ), __kmp_msg_null);
1003 }; // if
1004 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1005 if ( status != 0 ) {
1006 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantSetWorkerState ), KMP_ERR( status ), __kmp_msg_null);
1007 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001008
Jonathan Peyton61118492016-05-20 19:03:38 +00001009 /* Set stack size for this thread now.
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001010 * The multiple of 2 is there because on some machines, requesting an unusual stacksize
1011 * causes the thread to have an offset before the dummy alloca() takes place to create the
Jonathan Peyton61118492016-05-20 19:03:38 +00001012 * offset. Since we want the user to have a sufficient stacksize AND support a stack offset, we
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001013 * alloca() twice the offset so that the upcoming alloca() does not eliminate any premade
1014 * offset, and also gives the user the stack space they requested for all threads */
1015 stack_size += gtid * __kmp_stkoffset * 2;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001016
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001017 KA_TRACE( 10, ( "__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
1018 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
1019 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001020
1021# ifdef _POSIX_THREAD_ATTR_STACKSIZE
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001022 status = pthread_attr_setstacksize( & thread_attr, stack_size );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001023# ifdef KMP_BACKUP_STKSIZE
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001024 if ( status != 0 ) {
1025 if ( ! __kmp_env_stksize ) {
1026 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
1027 __kmp_stksize = KMP_BACKUP_STKSIZE;
1028 KA_TRACE( 10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
1029 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
1030 "bytes\n",
1031 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size )
1032 );
1033 status = pthread_attr_setstacksize( &thread_attr, stack_size );
1034 }; // if
1035 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001036# endif /* KMP_BACKUP_STKSIZE */
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001037 if ( status != 0 ) {
1038 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantSetWorkerStackSize, stack_size ), KMP_ERR( status ),
1039 KMP_HNT( ChangeWorkerStackSize ), __kmp_msg_null);
1040 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001041# endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001042
Jim Cownie5e8470a2013-09-27 10:38:44 +00001043#endif /* KMP_THREAD_ATTR */
1044
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001045 status = pthread_create( & handle, & thread_attr, __kmp_launch_worker, (void *) th );
1046 if ( status != 0 || ! handle ) { // ??? Why do we check handle??
Jim Cownie5e8470a2013-09-27 10:38:44 +00001047#ifdef _POSIX_THREAD_ATTR_STACKSIZE
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001048 if ( status == EINVAL ) {
1049 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantSetWorkerStackSize, stack_size ), KMP_ERR( status ),
1050 KMP_HNT( IncreaseWorkerStackSize ), __kmp_msg_null);
1051 };
1052 if ( status == ENOMEM ) {
1053 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantSetWorkerStackSize, stack_size ), KMP_ERR( status ),
1054 KMP_HNT( DecreaseWorkerStackSize ), __kmp_msg_null);
1055 };
Jim Cownie5e8470a2013-09-27 10:38:44 +00001056#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001057 if ( status == EAGAIN ) {
1058 __kmp_msg(kmp_ms_fatal, KMP_MSG( NoResourcesForWorkerThread ), KMP_ERR( status ),
1059 KMP_HNT( Decrease_NUM_THREADS ), __kmp_msg_null);
1060 }; // if
1061 KMP_SYSFAIL( "pthread_create", status );
1062 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001063
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001064 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001065
1066#ifdef KMP_THREAD_ATTR
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001067 status = pthread_attr_destroy( & thread_attr );
1068 if ( status ) {
1069 __kmp_msg(kmp_ms_warning, KMP_MSG( CantDestroyThreadAttrs ), KMP_ERR( status ), __kmp_msg_null);
1070 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +00001071#endif /* KMP_THREAD_ATTR */
1072
1073 KMP_MB(); /* Flush all pending memory write invalidates. */
1074
1075 KA_TRACE( 10, ("__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1076
1077} // __kmp_create_worker
1078
1079
1080void
1081__kmp_create_monitor( kmp_info_t *th )
1082{
1083 pthread_t handle;
1084 pthread_attr_t thread_attr;
1085 size_t size;
1086 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001087 int auto_adj_size = FALSE;
1088
Jonathan Peyton4fee5f62015-12-18 23:20:36 +00001089 if( __kmp_dflt_blocktime == KMP_MAX_BLOCKTIME ) {
1090 // We don't need monitor thread in case of MAX_BLOCKTIME
1091 KA_TRACE( 10, ("__kmp_create_monitor: skipping monitor thread because of MAX blocktime\n" ) );
1092 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
1093 th->th.th_info.ds.ds_gtid = 0;
1094 return;
1095 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001096 KA_TRACE( 10, ("__kmp_create_monitor: try to create monitor\n" ) );
1097
1098 KMP_MB(); /* Flush all pending memory write invalidates. */
1099
1100 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1101 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1102 #if KMP_REAL_TIME_FIX
1103 TCW_4( __kmp_global.g.g_time.dt.t_value, -1 ); // Will use it for synchronization a bit later.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001104 #else
1105 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001106 #endif // KMP_REAL_TIME_FIX
1107
1108 #ifdef KMP_THREAD_ATTR
1109 if ( __kmp_monitor_stksize == 0 ) {
1110 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1111 auto_adj_size = TRUE;
1112 }
1113 status = pthread_attr_init( &thread_attr );
1114 if ( status != 0 ) {
1115 __kmp_msg(
1116 kmp_ms_fatal,
1117 KMP_MSG( CantInitThreadAttrs ),
1118 KMP_ERR( status ),
1119 __kmp_msg_null
1120 );
1121 }; // if
1122 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1123 if ( status != 0 ) {
1124 __kmp_msg(
1125 kmp_ms_fatal,
1126 KMP_MSG( CantSetMonitorState ),
1127 KMP_ERR( status ),
1128 __kmp_msg_null
1129 );
1130 }; // if
1131
1132 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1133 status = pthread_attr_getstacksize( & thread_attr, & size );
1134 KMP_CHECK_SYSFAIL( "pthread_attr_getstacksize", status );
1135 #else
1136 size = __kmp_sys_min_stksize;
1137 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1138 #endif /* KMP_THREAD_ATTR */
1139
1140 if ( __kmp_monitor_stksize == 0 ) {
1141 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1142 }
1143 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1144 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1145 }
1146
1147 KA_TRACE( 10, ( "__kmp_create_monitor: default stacksize = %lu bytes,"
1148 "requested stacksize = %lu bytes\n",
1149 size, __kmp_monitor_stksize ) );
1150
1151 retry:
1152
1153 /* Set stack size for this thread now. */
1154
1155 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1156 KA_TRACE( 10, ( "__kmp_create_monitor: setting stacksize = %lu bytes,",
1157 __kmp_monitor_stksize ) );
1158 status = pthread_attr_setstacksize( & thread_attr, __kmp_monitor_stksize );
1159 if ( status != 0 ) {
1160 if ( auto_adj_size ) {
1161 __kmp_monitor_stksize *= 2;
1162 goto retry;
1163 }
1164 __kmp_msg(
1165 kmp_ms_warning, // should this be fatal? BB
1166 KMP_MSG( CantSetMonitorStackSize, (long int) __kmp_monitor_stksize ),
1167 KMP_ERR( status ),
1168 KMP_HNT( ChangeMonitorStackSize ),
1169 __kmp_msg_null
1170 );
1171 }; // if
1172 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1173
Jim Cownie5e8470a2013-09-27 10:38:44 +00001174 status = pthread_create( &handle, & thread_attr, __kmp_launch_monitor, (void *) th );
1175
1176 if ( status != 0 ) {
1177 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1178 if ( status == EINVAL ) {
1179 if ( auto_adj_size && ( __kmp_monitor_stksize < (size_t)0x40000000 ) ) {
1180 __kmp_monitor_stksize *= 2;
1181 goto retry;
1182 }
1183 __kmp_msg(
1184 kmp_ms_fatal,
1185 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1186 KMP_ERR( status ),
1187 KMP_HNT( IncreaseMonitorStackSize ),
1188 __kmp_msg_null
1189 );
1190 }; // if
1191 if ( status == ENOMEM ) {
1192 __kmp_msg(
1193 kmp_ms_fatal,
1194 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1195 KMP_ERR( status ),
1196 KMP_HNT( DecreaseMonitorStackSize ),
1197 __kmp_msg_null
1198 );
1199 }; // if
1200 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1201 if ( status == EAGAIN ) {
1202 __kmp_msg(
1203 kmp_ms_fatal,
1204 KMP_MSG( NoResourcesForMonitorThread ),
1205 KMP_ERR( status ),
1206 KMP_HNT( DecreaseNumberOfThreadsInUse ),
1207 __kmp_msg_null
1208 );
1209 }; // if
1210 KMP_SYSFAIL( "pthread_create", status );
1211 }; // if
1212
1213 th->th.th_info.ds.ds_thread = handle;
1214
1215 #if KMP_REAL_TIME_FIX
1216 // Wait for the monitor thread is really started and set its *priority*.
1217 KMP_DEBUG_ASSERT( sizeof( kmp_uint32 ) == sizeof( __kmp_global.g.g_time.dt.t_value ) );
1218 __kmp_wait_yield_4(
1219 (kmp_uint32 volatile *) & __kmp_global.g.g_time.dt.t_value, -1, & __kmp_neq_4, NULL
1220 );
1221 #endif // KMP_REAL_TIME_FIX
1222
1223 #ifdef KMP_THREAD_ATTR
1224 status = pthread_attr_destroy( & thread_attr );
1225 if ( status != 0 ) {
1226 __kmp_msg( //
1227 kmp_ms_warning,
1228 KMP_MSG( CantDestroyThreadAttrs ),
1229 KMP_ERR( status ),
1230 __kmp_msg_null
1231 );
1232 }; // if
1233 #endif
1234
1235 KMP_MB(); /* Flush all pending memory write invalidates. */
1236
1237 KA_TRACE( 10, ( "__kmp_create_monitor: monitor created %#.8lx\n", th->th.th_info.ds.ds_thread ) );
1238
1239} // __kmp_create_monitor
1240
1241void
1242__kmp_exit_thread(
1243 int exit_status
1244) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001245 pthread_exit( (void *)(intptr_t) exit_status );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001246} // __kmp_exit_thread
1247
Jim Cownie07ea89f2014-09-03 11:10:54 +00001248void __kmp_resume_monitor();
1249
Jim Cownie5e8470a2013-09-27 10:38:44 +00001250void
1251__kmp_reap_monitor( kmp_info_t *th )
1252{
Jonathan Peyton7c4d66d2015-06-08 20:01:14 +00001253 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001254 void *exit_val;
1255
1256 KA_TRACE( 10, ("__kmp_reap_monitor: try to reap monitor thread with handle %#.8lx\n",
1257 th->th.th_info.ds.ds_thread ) );
1258
1259 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1260 // If both tid and gtid are 0, it means the monitor did not ever start.
1261 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1262 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1263 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
Jonathan Peyton4fee5f62015-12-18 23:20:36 +00001264 KA_TRACE( 10, ("__kmp_reap_monitor: monitor did not start, returning\n") );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001265 return;
1266 }; // if
1267
1268 KMP_MB(); /* Flush all pending memory write invalidates. */
1269
1270
Jonathan Peyton93495de2016-06-13 17:36:40 +00001271 /* First, check to see whether the monitor thread exists to wake it up. This is
1272 to avoid performance problem when the monitor sleeps during blocktime-size
1273 interval */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001274
1275 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
Jonathan Peyton93495de2016-06-13 17:36:40 +00001276 if (status != ESRCH) {
Jim Cownie07ea89f2014-09-03 11:10:54 +00001277 __kmp_resume_monitor(); // Wake up the monitor thread
Jonathan Peyton93495de2016-06-13 17:36:40 +00001278 }
1279 KA_TRACE( 10, ("__kmp_reap_monitor: try to join with monitor\n") );
1280 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1281 if (exit_val != th) {
1282 __kmp_msg(
1283 kmp_ms_fatal,
1284 KMP_MSG( ReapMonitorError ),
1285 KMP_ERR( status ),
1286 __kmp_msg_null
1287 );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001288 }
1289
1290 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1291 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1292
1293 KA_TRACE( 10, ("__kmp_reap_monitor: done reaping monitor thread with handle %#.8lx\n",
1294 th->th.th_info.ds.ds_thread ) );
1295
1296 KMP_MB(); /* Flush all pending memory write invalidates. */
1297
1298}
1299
1300void
1301__kmp_reap_worker( kmp_info_t *th )
1302{
1303 int status;
1304 void *exit_val;
1305
1306 KMP_MB(); /* Flush all pending memory write invalidates. */
1307
1308 KA_TRACE( 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid ) );
1309
Jonathan Peyton93495de2016-06-13 17:36:40 +00001310 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001311#ifdef KMP_DEBUG
Jonathan Peyton93495de2016-06-13 17:36:40 +00001312 /* Don't expose these to the user until we understand when they trigger */
1313 if ( status != 0 ) {
1314 __kmp_msg(kmp_ms_fatal, KMP_MSG( ReapWorkerError ), KMP_ERR( status ), __kmp_msg_null);
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001315 }
Jonathan Peyton93495de2016-06-13 17:36:40 +00001316 if ( exit_val != th ) {
1317 KA_TRACE( 10, ( "__kmp_reap_worker: worker T#%d did not reap properly, exit_val = %p\n",
1318 th->th.th_info.ds.ds_gtid, exit_val ) );
1319 }
1320#endif /* KMP_DEBUG */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001321
1322 KA_TRACE( 10, ("__kmp_reap_worker: done reaping T#%d\n", th->th.th_info.ds.ds_gtid ) );
1323
1324 KMP_MB(); /* Flush all pending memory write invalidates. */
1325}
1326
1327
1328/* ------------------------------------------------------------------------ */
1329/* ------------------------------------------------------------------------ */
1330
1331#if KMP_HANDLE_SIGNALS
1332
1333
1334static void
1335__kmp_null_handler( int signo )
1336{
1337 // Do nothing, for doing SIG_IGN-type actions.
1338} // __kmp_null_handler
1339
1340
1341static void
1342__kmp_team_handler( int signo )
1343{
1344 if ( __kmp_global.g.g_abort == 0 ) {
1345 /* Stage 1 signal handler, let's shut down all of the threads */
1346 #ifdef KMP_DEBUG
1347 __kmp_debug_printf( "__kmp_team_handler: caught signal = %d\n", signo );
1348 #endif
1349 switch ( signo ) {
1350 case SIGHUP :
1351 case SIGINT :
1352 case SIGQUIT :
1353 case SIGILL :
1354 case SIGABRT :
1355 case SIGFPE :
1356 case SIGBUS :
1357 case SIGSEGV :
1358 #ifdef SIGSYS
1359 case SIGSYS :
1360 #endif
1361 case SIGTERM :
1362 if ( __kmp_debug_buf ) {
1363 __kmp_dump_debug_buffer( );
1364 }; // if
1365 KMP_MB(); // Flush all pending memory write invalidates.
1366 TCW_4( __kmp_global.g.g_abort, signo );
1367 KMP_MB(); // Flush all pending memory write invalidates.
1368 TCW_4( __kmp_global.g.g_done, TRUE );
1369 KMP_MB(); // Flush all pending memory write invalidates.
1370 break;
1371 default:
1372 #ifdef KMP_DEBUG
1373 __kmp_debug_printf( "__kmp_team_handler: unknown signal type" );
1374 #endif
1375 break;
1376 }; // switch
1377 }; // if
1378} // __kmp_team_handler
1379
1380
1381static
1382void __kmp_sigaction( int signum, const struct sigaction * act, struct sigaction * oldact ) {
1383 int rc = sigaction( signum, act, oldact );
1384 KMP_CHECK_SYSFAIL_ERRNO( "sigaction", rc );
1385}
1386
1387
1388static void
1389__kmp_install_one_handler( int sig, sig_func_t handler_func, int parallel_init )
1390{
1391 KMP_MB(); // Flush all pending memory write invalidates.
1392 KB_TRACE( 60, ( "__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init ) );
1393 if ( parallel_init ) {
1394 struct sigaction new_action;
1395 struct sigaction old_action;
1396 new_action.sa_handler = handler_func;
1397 new_action.sa_flags = 0;
1398 sigfillset( & new_action.sa_mask );
1399 __kmp_sigaction( sig, & new_action, & old_action );
1400 if ( old_action.sa_handler == __kmp_sighldrs[ sig ].sa_handler ) {
1401 sigaddset( & __kmp_sigset, sig );
1402 } else {
1403 // Restore/keep user's handler if one previously installed.
1404 __kmp_sigaction( sig, & old_action, NULL );
1405 }; // if
1406 } else {
1407 // Save initial/system signal handlers to see if user handlers installed.
1408 __kmp_sigaction( sig, NULL, & __kmp_sighldrs[ sig ] );
1409 }; // if
1410 KMP_MB(); // Flush all pending memory write invalidates.
1411} // __kmp_install_one_handler
1412
1413
1414static void
1415__kmp_remove_one_handler( int sig )
1416{
1417 KB_TRACE( 60, ( "__kmp_remove_one_handler( %d )\n", sig ) );
1418 if ( sigismember( & __kmp_sigset, sig ) ) {
1419 struct sigaction old;
1420 KMP_MB(); // Flush all pending memory write invalidates.
1421 __kmp_sigaction( sig, & __kmp_sighldrs[ sig ], & old );
1422 if ( ( old.sa_handler != __kmp_team_handler ) && ( old.sa_handler != __kmp_null_handler ) ) {
1423 // Restore the users signal handler.
1424 KB_TRACE( 10, ( "__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1425 __kmp_sigaction( sig, & old, NULL );
1426 }; // if
1427 sigdelset( & __kmp_sigset, sig );
1428 KMP_MB(); // Flush all pending memory write invalidates.
1429 }; // if
1430} // __kmp_remove_one_handler
1431
1432
1433void
1434__kmp_install_signals( int parallel_init )
1435{
1436 KB_TRACE( 10, ( "__kmp_install_signals( %d )\n", parallel_init ) );
1437 if ( __kmp_handle_signals || ! parallel_init ) {
1438 // If ! parallel_init, we do not install handlers, just save original handlers.
1439 // Let us do it even __handle_signals is 0.
1440 sigemptyset( & __kmp_sigset );
1441 __kmp_install_one_handler( SIGHUP, __kmp_team_handler, parallel_init );
1442 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1443 __kmp_install_one_handler( SIGQUIT, __kmp_team_handler, parallel_init );
1444 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1445 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1446 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1447 __kmp_install_one_handler( SIGBUS, __kmp_team_handler, parallel_init );
1448 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1449 #ifdef SIGSYS
1450 __kmp_install_one_handler( SIGSYS, __kmp_team_handler, parallel_init );
1451 #endif // SIGSYS
1452 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1453 #ifdef SIGPIPE
1454 __kmp_install_one_handler( SIGPIPE, __kmp_team_handler, parallel_init );
1455 #endif // SIGPIPE
1456 }; // if
1457} // __kmp_install_signals
1458
1459
1460void
1461__kmp_remove_signals( void )
1462{
1463 int sig;
1464 KB_TRACE( 10, ( "__kmp_remove_signals()\n" ) );
1465 for ( sig = 1; sig < NSIG; ++ sig ) {
1466 __kmp_remove_one_handler( sig );
1467 }; // for sig
1468} // __kmp_remove_signals
1469
1470
1471#endif // KMP_HANDLE_SIGNALS
1472
1473/* ------------------------------------------------------------------------ */
1474/* ------------------------------------------------------------------------ */
1475
1476void
1477__kmp_enable( int new_state )
1478{
1479 #ifdef KMP_CANCEL_THREADS
1480 int status, old_state;
1481 status = pthread_setcancelstate( new_state, & old_state );
1482 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1483 KMP_DEBUG_ASSERT( old_state == PTHREAD_CANCEL_DISABLE );
1484 #endif
1485}
1486
1487void
1488__kmp_disable( int * old_state )
1489{
1490 #ifdef KMP_CANCEL_THREADS
1491 int status;
1492 status = pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, old_state );
1493 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1494 #endif
1495}
1496
1497/* ------------------------------------------------------------------------ */
1498/* ------------------------------------------------------------------------ */
1499
1500static void
1501__kmp_atfork_prepare (void)
1502{
1503 /* nothing to do */
1504}
1505
1506static void
1507__kmp_atfork_parent (void)
1508{
1509 /* nothing to do */
1510}
1511
1512/*
1513 Reset the library so execution in the child starts "all over again" with
1514 clean data structures in initial states. Don't worry about freeing memory
1515 allocated by parent, just abandon it to be safe.
1516*/
1517static void
1518__kmp_atfork_child (void)
1519{
1520 /* TODO make sure this is done right for nested/sibling */
1521 // ATT: Memory leaks are here? TODO: Check it and fix.
1522 /* KMP_ASSERT( 0 ); */
1523
1524 ++__kmp_fork_count;
1525
1526 __kmp_init_runtime = FALSE;
1527 __kmp_init_monitor = 0;
1528 __kmp_init_parallel = FALSE;
1529 __kmp_init_middle = FALSE;
1530 __kmp_init_serial = FALSE;
1531 TCW_4(__kmp_init_gtid, FALSE);
1532 __kmp_init_common = FALSE;
1533
1534 TCW_4(__kmp_init_user_locks, FALSE);
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001535#if ! KMP_USE_DYNAMIC_LOCK
Jim Cownie07ea89f2014-09-03 11:10:54 +00001536 __kmp_user_lock_table.used = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001537 __kmp_user_lock_table.allocated = 0;
1538 __kmp_user_lock_table.table = NULL;
1539 __kmp_lock_blocks = NULL;
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001540#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001541
1542 __kmp_all_nth = 0;
1543 TCW_4(__kmp_nth, 0);
1544
1545 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate here
1546 so threadprivate doesn't use stale data */
1547 KA_TRACE( 10, ( "__kmp_atfork_child: checking cache address list %p\n",
1548 __kmp_threadpriv_cache_list ) );
1549
1550 while ( __kmp_threadpriv_cache_list != NULL ) {
1551
1552 if ( *__kmp_threadpriv_cache_list -> addr != NULL ) {
1553 KC_TRACE( 50, ( "__kmp_atfork_child: zeroing cache at address %p\n",
1554 &(*__kmp_threadpriv_cache_list -> addr) ) );
1555
1556 *__kmp_threadpriv_cache_list -> addr = NULL;
1557 }
1558 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list -> next;
1559 }
1560
1561 __kmp_init_runtime = FALSE;
1562
1563 /* reset statically initialized locks */
1564 __kmp_init_bootstrap_lock( &__kmp_initz_lock );
1565 __kmp_init_bootstrap_lock( &__kmp_stdio_lock );
1566 __kmp_init_bootstrap_lock( &__kmp_console_lock );
1567
1568 /* This is necessary to make sure no stale data is left around */
1569 /* AC: customers complain that we use unsafe routines in the atfork
1570 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1571 in dynamic_link when check the presence of shared tbbmalloc library.
1572 Suggestion is to make the library initialization lazier, similar
1573 to what done for __kmpc_begin(). */
1574 // TODO: synchronize all static initializations with regular library
1575 // startup; look at kmp_global.c and etc.
1576 //__kmp_internal_begin ();
1577
1578}
1579
1580void
1581__kmp_register_atfork(void) {
1582 if ( __kmp_need_register_atfork ) {
1583 int status = pthread_atfork( __kmp_atfork_prepare, __kmp_atfork_parent, __kmp_atfork_child );
1584 KMP_CHECK_SYSFAIL( "pthread_atfork", status );
1585 __kmp_need_register_atfork = FALSE;
1586 }
1587}
1588
1589void
1590__kmp_suspend_initialize( void )
1591{
1592 int status;
1593 status = pthread_mutexattr_init( &__kmp_suspend_mutex_attr );
1594 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
1595 status = pthread_condattr_init( &__kmp_suspend_cond_attr );
1596 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
1597}
1598
1599static void
1600__kmp_suspend_initialize_thread( kmp_info_t *th )
1601{
1602 if ( th->th.th_suspend_init_count <= __kmp_fork_count ) {
1603 /* this means we haven't initialized the suspension pthread objects for this thread
1604 in this instance of the process */
1605 int status;
1606 status = pthread_cond_init( &th->th.th_suspend_cv.c_cond, &__kmp_suspend_cond_attr );
1607 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
1608 status = pthread_mutex_init( &th->th.th_suspend_mx.m_mutex, & __kmp_suspend_mutex_attr );
1609 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
1610 *(volatile int*)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1611 };
1612}
1613
1614void
1615__kmp_suspend_uninitialize_thread( kmp_info_t *th )
1616{
1617 if(th->th.th_suspend_init_count > __kmp_fork_count) {
1618 /* this means we have initialize the suspension pthread objects for this thread
1619 in this instance of the process */
1620 int status;
1621
1622 status = pthread_cond_destroy( &th->th.th_suspend_cv.c_cond );
1623 if ( status != 0 && status != EBUSY ) {
1624 KMP_SYSFAIL( "pthread_cond_destroy", status );
1625 };
1626 status = pthread_mutex_destroy( &th->th.th_suspend_mx.m_mutex );
1627 if ( status != 0 && status != EBUSY ) {
1628 KMP_SYSFAIL( "pthread_mutex_destroy", status );
1629 };
1630 --th->th.th_suspend_init_count;
1631 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1632 }
1633}
1634
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001635/* This routine puts the calling thread to sleep after setting the
1636 * sleep bit for the indicated flag variable to true.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001637 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001638template <class C>
1639static inline void __kmp_suspend_template( int th_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001640{
Jonathan Peyton45be4502015-08-11 21:36:41 +00001641 KMP_TIME_DEVELOPER_BLOCK(USER_suspend);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001642 kmp_info_t *th = __kmp_threads[th_gtid];
1643 int status;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001644 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001645
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001646 KF_TRACE( 30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001647
1648 __kmp_suspend_initialize_thread( th );
1649
1650 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1651 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1652
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001653 KF_TRACE( 10, ( "__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1654 th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001655
1656 /* TODO: shouldn't this use release semantics to ensure that __kmp_suspend_initialize_thread
1657 gets called first?
1658 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001659 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001660
Jonathan Peytone03b62f2015-10-08 18:49:40 +00001661 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x, was %x\n",
1662 th_gtid, flag->get(), *(flag->get()), old_spin ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001663
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001664 if ( flag->done_check_val(old_spin) ) {
1665 old_spin = flag->unset_sleeping();
1666 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d false alarm, reset sleep bit for spin(%p)\n",
1667 th_gtid, flag->get()) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001668 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001669 /* Encapsulate in a loop as the documentation states that this may
1670 * "with low probability" return when the condition variable has
1671 * not been signaled or broadcast
1672 */
1673 int deactivated = FALSE;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001674 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1675 while ( flag->is_sleeping() ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001676#ifdef DEBUG_SUSPEND
1677 char buffer[128];
1678 __kmp_suspend_count++;
1679 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001680 __kmp_printf( "__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001681#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001682 // Mark the thread as no longer active (only in the first iteration of the loop).
Jim Cownie5e8470a2013-09-27 10:38:44 +00001683 if ( ! deactivated ) {
1684 th->th.th_active = FALSE;
1685 if ( th->th.th_active_in_pool ) {
1686 th->th.th_active_in_pool = FALSE;
1687 KMP_TEST_THEN_DEC32(
1688 (kmp_int32 *) &__kmp_thread_pool_active_nth );
1689 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
1690 }
1691 deactivated = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001692 }
1693
1694#if USE_SUSPEND_TIMEOUT
1695 struct timespec now;
1696 struct timeval tval;
1697 int msecs;
1698
1699 status = gettimeofday( &tval, NULL );
1700 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1701 TIMEVAL_TO_TIMESPEC( &tval, &now );
1702
1703 msecs = (4*__kmp_dflt_blocktime) + 200;
1704 now.tv_sec += msecs / 1000;
1705 now.tv_nsec += (msecs % 1000)*1000;
1706
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001707 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_timedwait\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001708 th_gtid ) );
1709 status = pthread_cond_timedwait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex, & now );
1710#else
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001711 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_wait\n",
Jonathan Peyton1bd61b42015-10-08 19:44:16 +00001712 th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001713 status = pthread_cond_wait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex );
1714#endif
1715
1716 if ( (status != 0) && (status != EINTR) && (status != ETIMEDOUT) ) {
1717 KMP_SYSFAIL( "pthread_cond_wait", status );
1718 }
1719#ifdef KMP_DEBUG
1720 if (status == ETIMEDOUT) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001721 if ( flag->is_sleeping() ) {
1722 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001723 } else {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001724 KF_TRACE( 2, ( "__kmp_suspend_template: T#%d timeout wakeup, sleep bit not set!\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001725 th_gtid ) );
1726 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001727 } else if ( flag->is_sleeping() ) {
1728 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001729 }
1730#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001731 } // while
1732
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001733 // Mark the thread as active again (if it was previous marked as inactive)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001734 if ( deactivated ) {
1735 th->th.th_active = TRUE;
1736 if ( TCR_4(th->th.th_in_pool) ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001737 KMP_TEST_THEN_INC32( (kmp_int32 *) &__kmp_thread_pool_active_nth );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001738 th->th.th_active_in_pool = TRUE;
1739 }
1740 }
1741 }
1742
1743#ifdef DEBUG_SUSPEND
1744 {
1745 char buffer[128];
1746 __kmp_print_cond( buffer, &th->th.th_suspend_cv);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001747 __kmp_printf( "__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001748 }
1749#endif
1750
Jim Cownie5e8470a2013-09-27 10:38:44 +00001751 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1752 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1753
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001754 KF_TRACE( 30, ("__kmp_suspend_template: T#%d exit\n", th_gtid ) );
1755}
1756
1757void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
1758 __kmp_suspend_template(th_gtid, flag);
1759}
1760void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
1761 __kmp_suspend_template(th_gtid, flag);
1762}
1763void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
1764 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001765}
1766
1767
1768/* This routine signals the thread specified by target_gtid to wake up
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001769 * after setting the sleep bit indicated by the flag argument to FALSE.
1770 * The target thread must already have called __kmp_suspend_template()
Jim Cownie5e8470a2013-09-27 10:38:44 +00001771 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001772template <class C>
1773static inline void __kmp_resume_template( int target_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001774{
Jonathan Peyton45be4502015-08-11 21:36:41 +00001775 KMP_TIME_DEVELOPER_BLOCK(USER_resume);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001776 kmp_info_t *th = __kmp_threads[target_gtid];
1777 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001778
1779#ifdef KMP_DEBUG
1780 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1781#endif
1782
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001783 KF_TRACE( 30, ( "__kmp_resume_template: T#%d wants to wakeup T#%d enter\n", gtid, target_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001784 KMP_DEBUG_ASSERT( gtid != target_gtid );
1785
1786 __kmp_suspend_initialize_thread( th );
1787
1788 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1789 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001790
Jonathan Peyton3f5dfc22015-11-09 16:31:51 +00001791 if (!flag) { // coming from __kmp_null_resume_wrapper
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001792 flag = (C *)th->th.th_sleep_loc;
1793 }
1794
Jonathan Peyton3f5dfc22015-11-09 16:31:51 +00001795 // First, check if the flag is null or its type has changed. If so, someone else woke it up.
1796 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type simply shows what flag was cast to
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001797 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p)\n",
1798 gtid, target_gtid, NULL ) );
1799 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1800 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1801 return;
1802 }
Jonathan Peyton1bd61b42015-10-08 19:44:16 +00001803 else { // if multiple threads are sleeping, flag should be internally referring to a specific thread here
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001804 typename C::flag_t old_spin = flag->unset_sleeping();
1805 if ( ! flag->is_sleeping_val(old_spin) ) {
1806 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p): "
1807 "%u => %u\n",
1808 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001809 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1810 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1811 return;
1812 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001813 KF_TRACE( 5, ( "__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p): "
1814 "%u => %u\n",
1815 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001816 }
1817 TCW_PTR(th->th.th_sleep_loc, NULL);
1818
Jim Cownie5e8470a2013-09-27 10:38:44 +00001819
1820#ifdef DEBUG_SUSPEND
1821 {
1822 char buffer[128];
1823 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001824 __kmp_printf( "__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, target_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001825 }
1826#endif
1827
Jim Cownie5e8470a2013-09-27 10:38:44 +00001828 status = pthread_cond_signal( &th->th.th_suspend_cv.c_cond );
1829 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1830 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1831 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001832 KF_TRACE( 30, ( "__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001833 gtid, target_gtid ) );
1834}
1835
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001836void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
1837 __kmp_resume_template(target_gtid, flag);
1838}
1839void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
1840 __kmp_resume_template(target_gtid, flag);
1841}
1842void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
1843 __kmp_resume_template(target_gtid, flag);
1844}
1845
Jim Cownie07ea89f2014-09-03 11:10:54 +00001846void
1847__kmp_resume_monitor()
1848{
Jonathan Peyton11dc82f2016-05-05 16:15:57 +00001849 KMP_TIME_DEVELOPER_BLOCK(USER_resume);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001850 int status;
1851#ifdef KMP_DEBUG
1852 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1853 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n",
1854 gtid, KMP_GTID_MONITOR ) );
1855 KMP_DEBUG_ASSERT( gtid != KMP_GTID_MONITOR );
1856#endif
1857 status = pthread_mutex_lock( &__kmp_wait_mx.m_mutex );
1858 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1859#ifdef DEBUG_SUSPEND
1860 {
1861 char buffer[128];
1862 __kmp_print_cond( buffer, &__kmp_wait_cv.c_cond );
1863 __kmp_printf( "__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid, KMP_GTID_MONITOR, buffer );
1864 }
1865#endif
1866 status = pthread_cond_signal( &__kmp_wait_cv.c_cond );
1867 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1868 status = pthread_mutex_unlock( &__kmp_wait_mx.m_mutex );
1869 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1870 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d exiting after signaling wake up for T#%d\n",
1871 gtid, KMP_GTID_MONITOR ) );
1872}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001873
1874/* ------------------------------------------------------------------------ */
1875/* ------------------------------------------------------------------------ */
1876
1877void
1878__kmp_yield( int cond )
1879{
1880 if (cond && __kmp_yielding_on) {
1881 sched_yield();
1882 }
1883}
1884
1885/* ------------------------------------------------------------------------ */
1886/* ------------------------------------------------------------------------ */
1887
1888void
1889__kmp_gtid_set_specific( int gtid )
1890{
Jonathan Peytonf2520102016-04-18 21:33:01 +00001891 if( __kmp_init_gtid ) {
1892 int status;
1893 status = pthread_setspecific( __kmp_gtid_threadprivate_key, (void*)(intptr_t)(gtid+1) );
1894 KMP_CHECK_SYSFAIL( "pthread_setspecific", status );
1895 } else {
1896 KA_TRACE( 50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n" ) );
1897 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001898}
1899
1900int
1901__kmp_gtid_get_specific()
1902{
1903 int gtid;
Jonathan Peytonf2520102016-04-18 21:33:01 +00001904 if ( !__kmp_init_gtid ) {
1905 KA_TRACE( 50, ("__kmp_gtid_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001906 return KMP_GTID_SHUTDOWN;
1907 }
1908 gtid = (int)(size_t)pthread_getspecific( __kmp_gtid_threadprivate_key );
1909 if ( gtid == 0 ) {
1910 gtid = KMP_GTID_DNE;
1911 }
1912 else {
1913 gtid--;
1914 }
1915 KA_TRACE( 50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1916 __kmp_gtid_threadprivate_key, gtid ));
1917 return gtid;
1918}
1919
1920/* ------------------------------------------------------------------------ */
1921/* ------------------------------------------------------------------------ */
1922
1923double
1924__kmp_read_cpu_time( void )
1925{
1926 /*clock_t t;*/
1927 struct tms buffer;
1928
1929 /*t =*/ times( & buffer );
1930
1931 return (buffer.tms_utime + buffer.tms_cutime) / (double) CLOCKS_PER_SEC;
1932}
1933
1934int
1935__kmp_read_system_info( struct kmp_sys_info *info )
1936{
1937 int status;
1938 struct rusage r_usage;
1939
1940 memset( info, 0, sizeof( *info ) );
1941
1942 status = getrusage( RUSAGE_SELF, &r_usage);
1943 KMP_CHECK_SYSFAIL_ERRNO( "getrusage", status );
1944
1945 info->maxrss = r_usage.ru_maxrss; /* the maximum resident set size utilized (in kilobytes) */
1946 info->minflt = r_usage.ru_minflt; /* the number of page faults serviced without any I/O */
1947 info->majflt = r_usage.ru_majflt; /* the number of page faults serviced that required I/O */
1948 info->nswap = r_usage.ru_nswap; /* the number of times a process was "swapped" out of memory */
1949 info->inblock = r_usage.ru_inblock; /* the number of times the file system had to perform input */
1950 info->oublock = r_usage.ru_oublock; /* the number of times the file system had to perform output */
1951 info->nvcsw = r_usage.ru_nvcsw; /* the number of times a context switch was voluntarily */
1952 info->nivcsw = r_usage.ru_nivcsw; /* the number of times a context switch was forced */
1953
1954 return (status != 0);
1955}
1956
1957/* ------------------------------------------------------------------------ */
1958/* ------------------------------------------------------------------------ */
1959
Jim Cownie5e8470a2013-09-27 10:38:44 +00001960void
1961__kmp_read_system_time( double *delta )
1962{
1963 double t_ns;
1964 struct timeval tval;
1965 struct timespec stop;
1966 int status;
1967
1968 status = gettimeofday( &tval, NULL );
1969 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1970 TIMEVAL_TO_TIMESPEC( &tval, &stop );
1971 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
1972 *delta = (t_ns * 1e-9);
1973}
1974
1975void
1976__kmp_clear_system_time( void )
1977{
1978 struct timeval tval;
1979 int status;
1980 status = gettimeofday( &tval, NULL );
1981 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1982 TIMEVAL_TO_TIMESPEC( &tval, &__kmp_sys_timer_data.start );
1983}
1984
1985/* ------------------------------------------------------------------------ */
1986/* ------------------------------------------------------------------------ */
1987
1988#ifdef BUILD_TV
1989
1990void
1991__kmp_tv_threadprivate_store( kmp_info_t *th, void *global_addr, void *thread_addr )
1992{
1993 struct tv_data *p;
1994
1995 p = (struct tv_data *) __kmp_allocate( sizeof( *p ) );
1996
1997 p->u.tp.global_addr = global_addr;
1998 p->u.tp.thread_addr = thread_addr;
1999
2000 p->type = (void *) 1;
2001
2002 p->next = th->th.th_local.tv_data;
2003 th->th.th_local.tv_data = p;
2004
2005 if ( p->next == 0 ) {
2006 int rc = pthread_setspecific( __kmp_tv_key, p );
2007 KMP_CHECK_SYSFAIL( "pthread_setspecific", rc );
2008 }
2009}
2010
2011#endif /* BUILD_TV */
2012
2013/* ------------------------------------------------------------------------ */
2014/* ------------------------------------------------------------------------ */
2015
2016static int
2017__kmp_get_xproc( void ) {
2018
2019 int r = 0;
2020
Joerg Sonnenberger7649cd42015-09-21 20:29:12 +00002021 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00002022
2023 r = sysconf( _SC_NPROCESSORS_ONLN );
2024
2025 #elif KMP_OS_DARWIN
2026
2027 // Bug C77011 High "OpenMP Threads and number of active cores".
2028
2029 // Find the number of available CPUs.
2030 kern_return_t rc;
2031 host_basic_info_data_t info;
2032 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
2033 rc = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) & info, & num );
2034 if ( rc == 0 && num == HOST_BASIC_INFO_COUNT ) {
2035 // Cannot use KA_TRACE() here because this code works before trace support is
2036 // initialized.
2037 r = info.avail_cpus;
2038 } else {
2039 KMP_WARNING( CantGetNumAvailCPU );
2040 KMP_INFORM( AssumedNumCPU );
2041 }; // if
2042
2043 #else
2044
2045 #error "Unknown or unsupported OS."
2046
2047 #endif
2048
2049 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
2050
2051} // __kmp_get_xproc
2052
Jim Cownie181b4bb2013-12-23 17:28:57 +00002053int
2054__kmp_read_from_file( char const *path, char const *format, ... )
2055{
2056 int result;
2057 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002058
Jim Cownie181b4bb2013-12-23 17:28:57 +00002059 va_start(args, format);
2060 FILE *f = fopen(path, "rb");
2061 if ( f == NULL )
2062 return 0;
2063 result = vfscanf(f, format, args);
2064 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002065
Jim Cownie5e8470a2013-09-27 10:38:44 +00002066 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00002067}
Jim Cownie5e8470a2013-09-27 10:38:44 +00002068
2069void
2070__kmp_runtime_initialize( void )
2071{
2072 int status;
2073 pthread_mutexattr_t mutex_attr;
2074 pthread_condattr_t cond_attr;
2075
2076 if ( __kmp_init_runtime ) {
2077 return;
2078 }; // if
2079
2080 #if ( KMP_ARCH_X86 || KMP_ARCH_X86_64 )
2081 if ( ! __kmp_cpuinfo.initialized ) {
2082 __kmp_query_cpuid( &__kmp_cpuinfo );
2083 }; // if
2084 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
2085
Jim Cownie5e8470a2013-09-27 10:38:44 +00002086 __kmp_xproc = __kmp_get_xproc();
2087
2088 if ( sysconf( _SC_THREADS ) ) {
2089
2090 /* Query the maximum number of threads */
2091 __kmp_sys_max_nth = sysconf( _SC_THREAD_THREADS_MAX );
2092 if ( __kmp_sys_max_nth == -1 ) {
2093 /* Unlimited threads for NPTL */
2094 __kmp_sys_max_nth = INT_MAX;
2095 }
2096 else if ( __kmp_sys_max_nth <= 1 ) {
2097 /* Can't tell, just use PTHREAD_THREADS_MAX */
2098 __kmp_sys_max_nth = KMP_MAX_NTH;
2099 }
2100
2101 /* Query the minimum stack size */
2102 __kmp_sys_min_stksize = sysconf( _SC_THREAD_STACK_MIN );
2103 if ( __kmp_sys_min_stksize <= 1 ) {
2104 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
2105 }
2106 }
2107
2108 /* Set up minimum number of threads to switch to TLS gtid */
2109 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
2110
Jim Cownie5e8470a2013-09-27 10:38:44 +00002111 #ifdef BUILD_TV
2112 {
2113 int rc = pthread_key_create( & __kmp_tv_key, 0 );
2114 KMP_CHECK_SYSFAIL( "pthread_key_create", rc );
2115 }
2116 #endif
2117
2118 status = pthread_key_create( &__kmp_gtid_threadprivate_key, __kmp_internal_end_dest );
2119 KMP_CHECK_SYSFAIL( "pthread_key_create", status );
2120 status = pthread_mutexattr_init( & mutex_attr );
2121 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
2122 status = pthread_mutex_init( & __kmp_wait_mx.m_mutex, & mutex_attr );
2123 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
2124 status = pthread_condattr_init( & cond_attr );
2125 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
2126 status = pthread_cond_init( & __kmp_wait_cv.c_cond, & cond_attr );
2127 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
2128#if USE_ITT_BUILD
2129 __kmp_itt_initialize();
2130#endif /* USE_ITT_BUILD */
2131
2132 __kmp_init_runtime = TRUE;
2133}
2134
2135void
2136__kmp_runtime_destroy( void )
2137{
2138 int status;
2139
2140 if ( ! __kmp_init_runtime ) {
2141 return; // Nothing to do.
2142 };
2143
2144#if USE_ITT_BUILD
2145 __kmp_itt_destroy();
2146#endif /* USE_ITT_BUILD */
2147
2148 status = pthread_key_delete( __kmp_gtid_threadprivate_key );
2149 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2150 #ifdef BUILD_TV
2151 status = pthread_key_delete( __kmp_tv_key );
2152 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2153 #endif
2154
2155 status = pthread_mutex_destroy( & __kmp_wait_mx.m_mutex );
2156 if ( status != 0 && status != EBUSY ) {
2157 KMP_SYSFAIL( "pthread_mutex_destroy", status );
2158 }
2159 status = pthread_cond_destroy( & __kmp_wait_cv.c_cond );
2160 if ( status != 0 && status != EBUSY ) {
2161 KMP_SYSFAIL( "pthread_cond_destroy", status );
2162 }
Alp Toker763b9392014-02-28 09:42:41 +00002163 #if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00002164 __kmp_affinity_uninitialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00002165 #endif
2166
2167 __kmp_init_runtime = FALSE;
2168}
2169
2170
2171/* Put the thread to sleep for a time period */
2172/* NOTE: not currently used anywhere */
2173void
2174__kmp_thread_sleep( int millis )
2175{
2176 sleep( ( millis + 500 ) / 1000 );
2177}
2178
2179/* Calculate the elapsed wall clock time for the user */
2180void
2181__kmp_elapsed( double *t )
2182{
2183 int status;
2184# ifdef FIX_SGI_CLOCK
2185 struct timespec ts;
2186
2187 status = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
2188 KMP_CHECK_SYSFAIL_ERRNO( "clock_gettime", status );
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +00002189 *t = (double) ts.tv_nsec * (1.0 / (double) KMP_NSEC_PER_SEC) +
Jim Cownie5e8470a2013-09-27 10:38:44 +00002190 (double) ts.tv_sec;
2191# else
2192 struct timeval tv;
2193
2194 status = gettimeofday( & tv, NULL );
2195 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +00002196 *t = (double) tv.tv_usec * (1.0 / (double) KMP_USEC_PER_SEC) +
Jim Cownie5e8470a2013-09-27 10:38:44 +00002197 (double) tv.tv_sec;
2198# endif
2199}
2200
2201/* Calculate the elapsed wall clock tick for the user */
2202void
2203__kmp_elapsed_tick( double *t )
2204{
2205 *t = 1 / (double) CLOCKS_PER_SEC;
2206}
2207
Jonathan Peyton377aa402016-04-14 16:00:37 +00002208/* Return the current time stamp in nsec */
2209kmp_uint64
2210__kmp_now_nsec()
2211{
2212 struct timeval t;
2213 gettimeofday(&t, NULL);
2214 return KMP_NSEC_PER_SEC*t.tv_sec + 1000*t.tv_usec;
2215}
2216
Jim Cownie5e8470a2013-09-27 10:38:44 +00002217/*
2218 Determine whether the given address is mapped into the current address space.
2219*/
2220
2221int
2222__kmp_is_address_mapped( void * addr ) {
2223
2224 int found = 0;
2225 int rc;
2226
Joerg Sonnenberger7649cd42015-09-21 20:29:12 +00002227 #if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00002228
2229 /*
2230 On Linux* OS, read the /proc/<pid>/maps pseudo-file to get all the address ranges mapped
2231 into the address space.
2232 */
2233
2234 char * name = __kmp_str_format( "/proc/%d/maps", getpid() );
2235 FILE * file = NULL;
2236
2237 file = fopen( name, "r" );
2238 KMP_ASSERT( file != NULL );
2239
2240 for ( ; ; ) {
2241
2242 void * beginning = NULL;
2243 void * ending = NULL;
2244 char perms[ 5 ];
2245
2246 rc = fscanf( file, "%p-%p %4s %*[^\n]\n", & beginning, & ending, perms );
2247 if ( rc == EOF ) {
2248 break;
2249 }; // if
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002250 KMP_ASSERT( rc == 3 && KMP_STRLEN( perms ) == 4 ); // Make sure all fields are read.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002251
2252 // Ending address is not included in the region, but beginning is.
2253 if ( ( addr >= beginning ) && ( addr < ending ) ) {
2254 perms[ 2 ] = 0; // 3th and 4th character does not matter.
2255 if ( strcmp( perms, "rw" ) == 0 ) {
2256 // Memory we are looking for should be readable and writable.
2257 found = 1;
2258 }; // if
2259 break;
2260 }; // if
2261
2262 }; // forever
2263
2264 // Free resources.
2265 fclose( file );
2266 KMP_INTERNAL_FREE( name );
2267
2268 #elif KMP_OS_DARWIN
2269
2270 /*
2271 On OS X*, /proc pseudo filesystem is not available. Try to read memory using vm
2272 interface.
2273 */
2274
2275 int buffer;
2276 vm_size_t count;
2277 rc =
2278 vm_read_overwrite(
2279 mach_task_self(), // Task to read memory of.
2280 (vm_address_t)( addr ), // Address to read from.
2281 1, // Number of bytes to be read.
2282 (vm_address_t)( & buffer ), // Address of buffer to save read bytes in.
2283 & count // Address of var to save number of read bytes in.
2284 );
2285 if ( rc == 0 ) {
2286 // Memory successfully read.
2287 found = 1;
2288 }; // if
2289
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +00002290 #elif KMP_OS_FREEBSD || KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +00002291
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +00002292 // FIXME(FreeBSD, NetBSD): Implement this
Alp Toker763b9392014-02-28 09:42:41 +00002293 found = 1;
2294
Jim Cownie5e8470a2013-09-27 10:38:44 +00002295 #else
2296
2297 #error "Unknown or unsupported OS"
2298
2299 #endif
2300
2301 return found;
2302
2303} // __kmp_is_address_mapped
2304
2305#ifdef USE_LOAD_BALANCE
2306
2307
2308# if KMP_OS_DARWIN
2309
2310// The function returns the rounded value of the system load average
2311// during given time interval which depends on the value of
2312// __kmp_load_balance_interval variable (default is 60 sec, other values
2313// may be 300 sec or 900 sec).
2314// It returns -1 in case of error.
2315int
2316__kmp_get_load_balance( int max )
2317{
2318 double averages[3];
2319 int ret_avg = 0;
2320
2321 int res = getloadavg( averages, 3 );
2322
2323 //Check __kmp_load_balance_interval to determine which of averages to use.
2324 // getloadavg() may return the number of samples less than requested that is
2325 // less than 3.
2326 if ( __kmp_load_balance_interval < 180 && ( res >= 1 ) ) {
2327 ret_avg = averages[0];// 1 min
2328 } else if ( ( __kmp_load_balance_interval >= 180
2329 && __kmp_load_balance_interval < 600 ) && ( res >= 2 ) ) {
2330 ret_avg = averages[1];// 5 min
2331 } else if ( ( __kmp_load_balance_interval >= 600 ) && ( res == 3 ) ) {
2332 ret_avg = averages[2];// 15 min
Alp Toker8f2d3f02014-02-24 10:40:15 +00002333 } else {// Error occurred
Jim Cownie5e8470a2013-09-27 10:38:44 +00002334 return -1;
2335 }
2336
2337 return ret_avg;
2338}
2339
2340# else // Linux* OS
2341
2342// The fuction returns number of running (not sleeping) threads, or -1 in case of error.
2343// Error could be reported if Linux* OS kernel too old (without "/proc" support).
2344// Counting running threads stops if max running threads encountered.
2345int
2346__kmp_get_load_balance( int max )
2347{
2348 static int permanent_error = 0;
2349
2350 static int glb_running_threads = 0; /* Saved count of the running threads for the thread balance algortihm */
2351 static double glb_call_time = 0; /* Thread balance algorithm call time */
2352
2353 int running_threads = 0; // Number of running threads in the system.
2354
2355 DIR * proc_dir = NULL; // Handle of "/proc/" directory.
2356 struct dirent * proc_entry = NULL;
2357
2358 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2359 DIR * task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2360 struct dirent * task_entry = NULL;
2361 int task_path_fixed_len;
2362
2363 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2364 int stat_file = -1;
2365 int stat_path_fixed_len;
2366
2367 int total_processes = 0; // Total number of processes in system.
2368 int total_threads = 0; // Total number of threads in system.
2369
2370 double call_time = 0.0;
2371
2372 __kmp_str_buf_init( & task_path );
2373 __kmp_str_buf_init( & stat_path );
2374
2375 __kmp_elapsed( & call_time );
2376
2377 if ( glb_call_time &&
2378 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
2379 running_threads = glb_running_threads;
2380 goto finish;
2381 }
2382
2383 glb_call_time = call_time;
2384
2385 // Do not spend time on scanning "/proc/" if we have a permanent error.
2386 if ( permanent_error ) {
2387 running_threads = -1;
2388 goto finish;
2389 }; // if
2390
2391 if ( max <= 0 ) {
2392 max = INT_MAX;
2393 }; // if
2394
2395 // Open "/proc/" directory.
2396 proc_dir = opendir( "/proc" );
2397 if ( proc_dir == NULL ) {
2398 // Cannot open "/prroc/". Probably the kernel does not support it. Return an error now and
2399 // in subsequent calls.
2400 running_threads = -1;
2401 permanent_error = 1;
2402 goto finish;
2403 }; // if
2404
2405 // Initialize fixed part of task_path. This part will not change.
2406 __kmp_str_buf_cat( & task_path, "/proc/", 6 );
2407 task_path_fixed_len = task_path.used; // Remember number of used characters.
2408
2409 proc_entry = readdir( proc_dir );
2410 while ( proc_entry != NULL ) {
2411 // Proc entry is a directory and name starts with a digit. Assume it is a process'
2412 // directory.
2413 if ( proc_entry->d_type == DT_DIR && isdigit( proc_entry->d_name[ 0 ] ) ) {
2414
2415 ++ total_processes;
2416 // Make sure init process is the very first in "/proc", so we can replace
2417 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes == 1.
2418 // We are going to check that total_processes == 1 => d_name == "1" is true (where
2419 // "=>" is implication). Since C++ does not have => operator, let us replace it with its
2420 // equivalent: a => b == ! a || b.
2421 KMP_DEBUG_ASSERT( total_processes != 1 || strcmp( proc_entry->d_name, "1" ) == 0 );
2422
2423 // Construct task_path.
2424 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002425 __kmp_str_buf_cat( & task_path, proc_entry->d_name, KMP_STRLEN( proc_entry->d_name ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002426 __kmp_str_buf_cat( & task_path, "/task", 5 );
2427
2428 task_dir = opendir( task_path.str );
2429 if ( task_dir == NULL ) {
2430 // Process can finish between reading "/proc/" directory entry and opening process'
2431 // "task/" directory. So, in general case we should not complain, but have to skip
2432 // this process and read the next one.
2433 // But on systems with no "task/" support we will spend lot of time to scan "/proc/"
2434 // tree again and again without any benefit. "init" process (its pid is 1) should
2435 // exist always, so, if we cannot open "/proc/1/task/" directory, it means "task/"
2436 // is not supported by kernel. Report an error now and in the future.
2437 if ( strcmp( proc_entry->d_name, "1" ) == 0 ) {
2438 running_threads = -1;
2439 permanent_error = 1;
2440 goto finish;
2441 }; // if
2442 } else {
2443 // Construct fixed part of stat file path.
2444 __kmp_str_buf_clear( & stat_path );
2445 __kmp_str_buf_cat( & stat_path, task_path.str, task_path.used );
2446 __kmp_str_buf_cat( & stat_path, "/", 1 );
2447 stat_path_fixed_len = stat_path.used;
2448
2449 task_entry = readdir( task_dir );
2450 while ( task_entry != NULL ) {
2451 // It is a directory and name starts with a digit.
2452 if ( proc_entry->d_type == DT_DIR && isdigit( task_entry->d_name[ 0 ] ) ) {
2453
2454 ++ total_threads;
2455
2456 // Consruct complete stat file path. Easiest way would be:
2457 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str, task_entry->d_name );
2458 // but seriae of __kmp_str_buf_cat works a bit faster.
2459 stat_path.used = stat_path_fixed_len; // Reset stat path to its fixed part.
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002460 __kmp_str_buf_cat( & stat_path, task_entry->d_name, KMP_STRLEN( task_entry->d_name ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002461 __kmp_str_buf_cat( & stat_path, "/stat", 5 );
2462
2463 // Note: Low-level API (open/read/close) is used. High-level API
2464 // (fopen/fclose) works ~ 30 % slower.
2465 stat_file = open( stat_path.str, O_RDONLY );
2466 if ( stat_file == -1 ) {
2467 // We cannot report an error because task (thread) can terminate just
2468 // before reading this file.
2469 } else {
2470 /*
2471 Content of "stat" file looks like:
2472
2473 24285 (program) S ...
2474
2475 It is a single line (if program name does not include fanny
2476 symbols). First number is a thread id, then name of executable file
2477 name in paretheses, then state of the thread. We need just thread
2478 state.
2479
2480 Good news: Length of program name is 15 characters max. Longer
2481 names are truncated.
2482
2483 Thus, we need rather short buffer: 15 chars for program name +
2484 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
2485
2486 Bad news: Program name may contain special symbols like space,
2487 closing parenthesis, or even new line. This makes parsing "stat"
2488 file not 100 % reliable. In case of fanny program names parsing
2489 may fail (report incorrect thread state).
2490
2491 Parsing "status" file looks more promissing (due to different
2492 file structure and escaping special symbols) but reading and
2493 parsing of "status" file works slower.
2494
2495 -- ln
2496 */
2497 char buffer[ 65 ];
2498 int len;
2499 len = read( stat_file, buffer, sizeof( buffer ) - 1 );
2500 if ( len >= 0 ) {
2501 buffer[ len ] = 0;
2502 // Using scanf:
2503 // sscanf( buffer, "%*d (%*s) %c ", & state );
2504 // looks very nice, but searching for a closing parenthesis works a
2505 // bit faster.
2506 char * close_parent = strstr( buffer, ") " );
2507 if ( close_parent != NULL ) {
2508 char state = * ( close_parent + 2 );
2509 if ( state == 'R' ) {
2510 ++ running_threads;
2511 if ( running_threads >= max ) {
2512 goto finish;
2513 }; // if
2514 }; // if
2515 }; // if
2516 }; // if
2517 close( stat_file );
2518 stat_file = -1;
2519 }; // if
2520 }; // if
2521 task_entry = readdir( task_dir );
2522 }; // while
2523 closedir( task_dir );
2524 task_dir = NULL;
2525 }; // if
2526 }; // if
2527 proc_entry = readdir( proc_dir );
2528 }; // while
2529
2530 //
2531 // There _might_ be a timing hole where the thread executing this
2532 // code get skipped in the load balance, and running_threads is 0.
2533 // Assert in the debug builds only!!!
2534 //
2535 KMP_DEBUG_ASSERT( running_threads > 0 );
2536 if ( running_threads <= 0 ) {
2537 running_threads = 1;
2538 }
2539
2540 finish: // Clean up and exit.
2541 if ( proc_dir != NULL ) {
2542 closedir( proc_dir );
2543 }; // if
2544 __kmp_str_buf_free( & task_path );
2545 if ( task_dir != NULL ) {
2546 closedir( task_dir );
2547 }; // if
2548 __kmp_str_buf_free( & stat_path );
2549 if ( stat_file != -1 ) {
2550 close( stat_file );
2551 }; // if
2552
2553 glb_running_threads = running_threads;
2554
2555 return running_threads;
2556
2557} // __kmp_get_load_balance
2558
2559# endif // KMP_OS_DARWIN
2560
2561#endif // USE_LOAD_BALANCE
2562
Hal Finkel91e19a32016-05-26 04:48:14 +00002563#if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || (KMP_OS_LINUX && KMP_ARCH_AARCH64) || KMP_ARCH_PPC64)
Jim Cownie3051f972014-08-07 10:12:54 +00002564
2565// we really only need the case with 1 argument, because CLANG always build
2566// a struct of pointers to shared variables referenced in the outlined function
2567int
2568__kmp_invoke_microtask( microtask_t pkfn,
2569 int gtid, int tid,
Jonathan Peyton61118492016-05-20 19:03:38 +00002570 int argc, void *p_argv[]
Jonathan Peyton122dd762015-07-13 18:55:45 +00002571#if OMPT_SUPPORT
2572 , void **exit_frame_ptr
2573#endif
Jonathan Peyton61118492016-05-20 19:03:38 +00002574)
Jonathan Peyton122dd762015-07-13 18:55:45 +00002575{
2576#if OMPT_SUPPORT
2577 *exit_frame_ptr = __builtin_frame_address(0);
2578#endif
2579
Jim Cownie3051f972014-08-07 10:12:54 +00002580 switch (argc) {
2581 default:
2582 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2583 fflush(stderr);
2584 exit(-1);
2585 case 0:
2586 (*pkfn)(&gtid, &tid);
2587 break;
2588 case 1:
2589 (*pkfn)(&gtid, &tid, p_argv[0]);
2590 break;
2591 case 2:
2592 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2593 break;
2594 case 3:
2595 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2596 break;
2597 case 4:
2598 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2599 break;
2600 case 5:
2601 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2602 break;
2603 case 6:
2604 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2605 p_argv[5]);
2606 break;
2607 case 7:
2608 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2609 p_argv[5], p_argv[6]);
2610 break;
2611 case 8:
2612 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2613 p_argv[5], p_argv[6], p_argv[7]);
2614 break;
2615 case 9:
2616 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2617 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2618 break;
2619 case 10:
2620 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2621 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2622 break;
2623 case 11:
2624 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2625 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2626 break;
2627 case 12:
2628 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2629 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2630 p_argv[11]);
2631 break;
2632 case 13:
2633 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2634 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2635 p_argv[11], p_argv[12]);
2636 break;
2637 case 14:
2638 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2639 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2640 p_argv[11], p_argv[12], p_argv[13]);
2641 break;
2642 case 15:
2643 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2644 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2645 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2646 break;
2647 }
2648
Jonathan Peyton122dd762015-07-13 18:55:45 +00002649#if OMPT_SUPPORT
2650 *exit_frame_ptr = 0;
2651#endif
2652
Jim Cownie3051f972014-08-07 10:12:54 +00002653 return 1;
2654}
2655
2656#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002657
Jim Cownie5e8470a2013-09-27 10:38:44 +00002658// end of file //
2659