blob: 891109a17af9aade33d162dd884160795721a350 [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
Paul Osmialowskifb043fd2016-05-16 09:44:11 +0000426#if KMP_USE_FUTEX && !KMP_OS_CNK
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
Paul Osmialowskifb043fd2016-05-16 09:44:11 +0000443#endif // KMP_USE_FUTEX && !KMP_OS_CNK
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 Peyton749b4d52016-01-27 21:02:04 +00001009 /* Set stack size for this thread now.
1010 * 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
1012 * offset. Since we want the user to have a sufficient stacksize AND support a stack offset, we
1013 * 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
1271 /* First, check to see whether the monitor thread exists. This could prevent a hang,
1272 but if the monitor dies after the pthread_kill call and before the pthread_join
1273 call, it will still hang. */
1274
1275 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1276 if (status == ESRCH) {
1277
1278 KA_TRACE( 10, ("__kmp_reap_monitor: monitor does not exist, returning\n") );
1279
1280 } else
1281 {
Jim Cownie07ea89f2014-09-03 11:10:54 +00001282 __kmp_resume_monitor(); // Wake up the monitor thread
Jim Cownie5e8470a2013-09-27 10:38:44 +00001283 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1284 if (exit_val != th) {
1285 __kmp_msg(
1286 kmp_ms_fatal,
1287 KMP_MSG( ReapMonitorError ),
1288 KMP_ERR( status ),
1289 __kmp_msg_null
1290 );
1291 }
1292 }
1293
1294 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1295 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1296
1297 KA_TRACE( 10, ("__kmp_reap_monitor: done reaping monitor thread with handle %#.8lx\n",
1298 th->th.th_info.ds.ds_thread ) );
1299
1300 KMP_MB(); /* Flush all pending memory write invalidates. */
1301
1302}
1303
1304void
1305__kmp_reap_worker( kmp_info_t *th )
1306{
1307 int status;
1308 void *exit_val;
1309
1310 KMP_MB(); /* Flush all pending memory write invalidates. */
1311
1312 KA_TRACE( 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid ) );
1313
1314 /* First, check to see whether the worker thread exists. This could prevent a hang,
1315 but if the worker dies after the pthread_kill call and before the pthread_join
1316 call, it will still hang. */
1317
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001318 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1319 if (status == ESRCH) {
1320 KA_TRACE( 10, ("__kmp_reap_worker: worker T#%d does not exist, returning\n", th->th.th_info.ds.ds_gtid ) );
1321 }
1322 else {
1323 KA_TRACE( 10, ("__kmp_reap_worker: try to join with worker T#%d\n", th->th.th_info.ds.ds_gtid ) );
1324 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001325#ifdef KMP_DEBUG
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001326 /* Don't expose these to the user until we understand when they trigger */
1327 if ( status != 0 ) {
1328 __kmp_msg(kmp_ms_fatal, KMP_MSG( ReapWorkerError ), KMP_ERR( status ), __kmp_msg_null);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001329 }
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001330 if ( exit_val != th ) {
1331 KA_TRACE( 10, ( "__kmp_reap_worker: worker T#%d did not reap properly, exit_val = %p\n",
1332 th->th.th_info.ds.ds_gtid, exit_val ) );
1333 }
1334#endif /* KMP_DEBUG */
1335 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001336
1337 KA_TRACE( 10, ("__kmp_reap_worker: done reaping T#%d\n", th->th.th_info.ds.ds_gtid ) );
1338
1339 KMP_MB(); /* Flush all pending memory write invalidates. */
1340}
1341
1342
1343/* ------------------------------------------------------------------------ */
1344/* ------------------------------------------------------------------------ */
1345
1346#if KMP_HANDLE_SIGNALS
1347
1348
1349static void
1350__kmp_null_handler( int signo )
1351{
1352 // Do nothing, for doing SIG_IGN-type actions.
1353} // __kmp_null_handler
1354
1355
1356static void
1357__kmp_team_handler( int signo )
1358{
1359 if ( __kmp_global.g.g_abort == 0 ) {
1360 /* Stage 1 signal handler, let's shut down all of the threads */
1361 #ifdef KMP_DEBUG
1362 __kmp_debug_printf( "__kmp_team_handler: caught signal = %d\n", signo );
1363 #endif
1364 switch ( signo ) {
1365 case SIGHUP :
1366 case SIGINT :
1367 case SIGQUIT :
1368 case SIGILL :
1369 case SIGABRT :
1370 case SIGFPE :
1371 case SIGBUS :
1372 case SIGSEGV :
1373 #ifdef SIGSYS
1374 case SIGSYS :
1375 #endif
1376 case SIGTERM :
1377 if ( __kmp_debug_buf ) {
1378 __kmp_dump_debug_buffer( );
1379 }; // if
1380 KMP_MB(); // Flush all pending memory write invalidates.
1381 TCW_4( __kmp_global.g.g_abort, signo );
1382 KMP_MB(); // Flush all pending memory write invalidates.
1383 TCW_4( __kmp_global.g.g_done, TRUE );
1384 KMP_MB(); // Flush all pending memory write invalidates.
1385 break;
1386 default:
1387 #ifdef KMP_DEBUG
1388 __kmp_debug_printf( "__kmp_team_handler: unknown signal type" );
1389 #endif
1390 break;
1391 }; // switch
1392 }; // if
1393} // __kmp_team_handler
1394
1395
1396static
1397void __kmp_sigaction( int signum, const struct sigaction * act, struct sigaction * oldact ) {
1398 int rc = sigaction( signum, act, oldact );
1399 KMP_CHECK_SYSFAIL_ERRNO( "sigaction", rc );
1400}
1401
1402
1403static void
1404__kmp_install_one_handler( int sig, sig_func_t handler_func, int parallel_init )
1405{
1406 KMP_MB(); // Flush all pending memory write invalidates.
1407 KB_TRACE( 60, ( "__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init ) );
1408 if ( parallel_init ) {
1409 struct sigaction new_action;
1410 struct sigaction old_action;
1411 new_action.sa_handler = handler_func;
1412 new_action.sa_flags = 0;
1413 sigfillset( & new_action.sa_mask );
1414 __kmp_sigaction( sig, & new_action, & old_action );
1415 if ( old_action.sa_handler == __kmp_sighldrs[ sig ].sa_handler ) {
1416 sigaddset( & __kmp_sigset, sig );
1417 } else {
1418 // Restore/keep user's handler if one previously installed.
1419 __kmp_sigaction( sig, & old_action, NULL );
1420 }; // if
1421 } else {
1422 // Save initial/system signal handlers to see if user handlers installed.
1423 __kmp_sigaction( sig, NULL, & __kmp_sighldrs[ sig ] );
1424 }; // if
1425 KMP_MB(); // Flush all pending memory write invalidates.
1426} // __kmp_install_one_handler
1427
1428
1429static void
1430__kmp_remove_one_handler( int sig )
1431{
1432 KB_TRACE( 60, ( "__kmp_remove_one_handler( %d )\n", sig ) );
1433 if ( sigismember( & __kmp_sigset, sig ) ) {
1434 struct sigaction old;
1435 KMP_MB(); // Flush all pending memory write invalidates.
1436 __kmp_sigaction( sig, & __kmp_sighldrs[ sig ], & old );
1437 if ( ( old.sa_handler != __kmp_team_handler ) && ( old.sa_handler != __kmp_null_handler ) ) {
1438 // Restore the users signal handler.
1439 KB_TRACE( 10, ( "__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1440 __kmp_sigaction( sig, & old, NULL );
1441 }; // if
1442 sigdelset( & __kmp_sigset, sig );
1443 KMP_MB(); // Flush all pending memory write invalidates.
1444 }; // if
1445} // __kmp_remove_one_handler
1446
1447
1448void
1449__kmp_install_signals( int parallel_init )
1450{
1451 KB_TRACE( 10, ( "__kmp_install_signals( %d )\n", parallel_init ) );
1452 if ( __kmp_handle_signals || ! parallel_init ) {
1453 // If ! parallel_init, we do not install handlers, just save original handlers.
1454 // Let us do it even __handle_signals is 0.
1455 sigemptyset( & __kmp_sigset );
1456 __kmp_install_one_handler( SIGHUP, __kmp_team_handler, parallel_init );
1457 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1458 __kmp_install_one_handler( SIGQUIT, __kmp_team_handler, parallel_init );
1459 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1460 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1461 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1462 __kmp_install_one_handler( SIGBUS, __kmp_team_handler, parallel_init );
1463 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1464 #ifdef SIGSYS
1465 __kmp_install_one_handler( SIGSYS, __kmp_team_handler, parallel_init );
1466 #endif // SIGSYS
1467 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1468 #ifdef SIGPIPE
1469 __kmp_install_one_handler( SIGPIPE, __kmp_team_handler, parallel_init );
1470 #endif // SIGPIPE
1471 }; // if
1472} // __kmp_install_signals
1473
1474
1475void
1476__kmp_remove_signals( void )
1477{
1478 int sig;
1479 KB_TRACE( 10, ( "__kmp_remove_signals()\n" ) );
1480 for ( sig = 1; sig < NSIG; ++ sig ) {
1481 __kmp_remove_one_handler( sig );
1482 }; // for sig
1483} // __kmp_remove_signals
1484
1485
1486#endif // KMP_HANDLE_SIGNALS
1487
1488/* ------------------------------------------------------------------------ */
1489/* ------------------------------------------------------------------------ */
1490
1491void
1492__kmp_enable( int new_state )
1493{
1494 #ifdef KMP_CANCEL_THREADS
1495 int status, old_state;
1496 status = pthread_setcancelstate( new_state, & old_state );
1497 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1498 KMP_DEBUG_ASSERT( old_state == PTHREAD_CANCEL_DISABLE );
1499 #endif
1500}
1501
1502void
1503__kmp_disable( int * old_state )
1504{
1505 #ifdef KMP_CANCEL_THREADS
1506 int status;
1507 status = pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, old_state );
1508 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1509 #endif
1510}
1511
1512/* ------------------------------------------------------------------------ */
1513/* ------------------------------------------------------------------------ */
1514
1515static void
1516__kmp_atfork_prepare (void)
1517{
1518 /* nothing to do */
1519}
1520
1521static void
1522__kmp_atfork_parent (void)
1523{
1524 /* nothing to do */
1525}
1526
1527/*
1528 Reset the library so execution in the child starts "all over again" with
1529 clean data structures in initial states. Don't worry about freeing memory
1530 allocated by parent, just abandon it to be safe.
1531*/
1532static void
1533__kmp_atfork_child (void)
1534{
1535 /* TODO make sure this is done right for nested/sibling */
1536 // ATT: Memory leaks are here? TODO: Check it and fix.
1537 /* KMP_ASSERT( 0 ); */
1538
1539 ++__kmp_fork_count;
1540
1541 __kmp_init_runtime = FALSE;
1542 __kmp_init_monitor = 0;
1543 __kmp_init_parallel = FALSE;
1544 __kmp_init_middle = FALSE;
1545 __kmp_init_serial = FALSE;
1546 TCW_4(__kmp_init_gtid, FALSE);
1547 __kmp_init_common = FALSE;
1548
1549 TCW_4(__kmp_init_user_locks, FALSE);
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001550#if ! KMP_USE_DYNAMIC_LOCK
Jim Cownie07ea89f2014-09-03 11:10:54 +00001551 __kmp_user_lock_table.used = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001552 __kmp_user_lock_table.allocated = 0;
1553 __kmp_user_lock_table.table = NULL;
1554 __kmp_lock_blocks = NULL;
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001555#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001556
1557 __kmp_all_nth = 0;
1558 TCW_4(__kmp_nth, 0);
1559
1560 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate here
1561 so threadprivate doesn't use stale data */
1562 KA_TRACE( 10, ( "__kmp_atfork_child: checking cache address list %p\n",
1563 __kmp_threadpriv_cache_list ) );
1564
1565 while ( __kmp_threadpriv_cache_list != NULL ) {
1566
1567 if ( *__kmp_threadpriv_cache_list -> addr != NULL ) {
1568 KC_TRACE( 50, ( "__kmp_atfork_child: zeroing cache at address %p\n",
1569 &(*__kmp_threadpriv_cache_list -> addr) ) );
1570
1571 *__kmp_threadpriv_cache_list -> addr = NULL;
1572 }
1573 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list -> next;
1574 }
1575
1576 __kmp_init_runtime = FALSE;
1577
1578 /* reset statically initialized locks */
1579 __kmp_init_bootstrap_lock( &__kmp_initz_lock );
1580 __kmp_init_bootstrap_lock( &__kmp_stdio_lock );
1581 __kmp_init_bootstrap_lock( &__kmp_console_lock );
1582
1583 /* This is necessary to make sure no stale data is left around */
1584 /* AC: customers complain that we use unsafe routines in the atfork
1585 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1586 in dynamic_link when check the presence of shared tbbmalloc library.
1587 Suggestion is to make the library initialization lazier, similar
1588 to what done for __kmpc_begin(). */
1589 // TODO: synchronize all static initializations with regular library
1590 // startup; look at kmp_global.c and etc.
1591 //__kmp_internal_begin ();
1592
1593}
1594
1595void
1596__kmp_register_atfork(void) {
1597 if ( __kmp_need_register_atfork ) {
1598 int status = pthread_atfork( __kmp_atfork_prepare, __kmp_atfork_parent, __kmp_atfork_child );
1599 KMP_CHECK_SYSFAIL( "pthread_atfork", status );
1600 __kmp_need_register_atfork = FALSE;
1601 }
1602}
1603
1604void
1605__kmp_suspend_initialize( void )
1606{
1607 int status;
1608 status = pthread_mutexattr_init( &__kmp_suspend_mutex_attr );
1609 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
1610 status = pthread_condattr_init( &__kmp_suspend_cond_attr );
1611 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
1612}
1613
1614static void
1615__kmp_suspend_initialize_thread( kmp_info_t *th )
1616{
1617 if ( th->th.th_suspend_init_count <= __kmp_fork_count ) {
1618 /* this means we haven't initialized the suspension pthread objects for this thread
1619 in this instance of the process */
1620 int status;
1621 status = pthread_cond_init( &th->th.th_suspend_cv.c_cond, &__kmp_suspend_cond_attr );
1622 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
1623 status = pthread_mutex_init( &th->th.th_suspend_mx.m_mutex, & __kmp_suspend_mutex_attr );
1624 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
1625 *(volatile int*)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1626 };
1627}
1628
1629void
1630__kmp_suspend_uninitialize_thread( kmp_info_t *th )
1631{
1632 if(th->th.th_suspend_init_count > __kmp_fork_count) {
1633 /* this means we have initialize the suspension pthread objects for this thread
1634 in this instance of the process */
1635 int status;
1636
1637 status = pthread_cond_destroy( &th->th.th_suspend_cv.c_cond );
1638 if ( status != 0 && status != EBUSY ) {
1639 KMP_SYSFAIL( "pthread_cond_destroy", status );
1640 };
1641 status = pthread_mutex_destroy( &th->th.th_suspend_mx.m_mutex );
1642 if ( status != 0 && status != EBUSY ) {
1643 KMP_SYSFAIL( "pthread_mutex_destroy", status );
1644 };
1645 --th->th.th_suspend_init_count;
1646 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1647 }
1648}
1649
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001650/* This routine puts the calling thread to sleep after setting the
1651 * sleep bit for the indicated flag variable to true.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001652 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001653template <class C>
1654static inline void __kmp_suspend_template( int th_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001655{
Jonathan Peyton45be4502015-08-11 21:36:41 +00001656 KMP_TIME_DEVELOPER_BLOCK(USER_suspend);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001657 kmp_info_t *th = __kmp_threads[th_gtid];
1658 int status;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001659 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001660
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001661 KF_TRACE( 30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001662
1663 __kmp_suspend_initialize_thread( th );
1664
1665 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1666 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1667
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001668 KF_TRACE( 10, ( "__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1669 th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001670
1671 /* TODO: shouldn't this use release semantics to ensure that __kmp_suspend_initialize_thread
1672 gets called first?
1673 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001674 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001675
Jonathan Peytone03b62f2015-10-08 18:49:40 +00001676 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x, was %x\n",
1677 th_gtid, flag->get(), *(flag->get()), old_spin ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001678
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001679 if ( flag->done_check_val(old_spin) ) {
1680 old_spin = flag->unset_sleeping();
1681 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d false alarm, reset sleep bit for spin(%p)\n",
1682 th_gtid, flag->get()) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001683 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001684 /* Encapsulate in a loop as the documentation states that this may
1685 * "with low probability" return when the condition variable has
1686 * not been signaled or broadcast
1687 */
1688 int deactivated = FALSE;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001689 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1690 while ( flag->is_sleeping() ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001691#ifdef DEBUG_SUSPEND
1692 char buffer[128];
1693 __kmp_suspend_count++;
1694 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001695 __kmp_printf( "__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001696#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001697 // Mark the thread as no longer active (only in the first iteration of the loop).
Jim Cownie5e8470a2013-09-27 10:38:44 +00001698 if ( ! deactivated ) {
1699 th->th.th_active = FALSE;
1700 if ( th->th.th_active_in_pool ) {
1701 th->th.th_active_in_pool = FALSE;
1702 KMP_TEST_THEN_DEC32(
1703 (kmp_int32 *) &__kmp_thread_pool_active_nth );
1704 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
1705 }
1706 deactivated = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001707 }
1708
1709#if USE_SUSPEND_TIMEOUT
1710 struct timespec now;
1711 struct timeval tval;
1712 int msecs;
1713
1714 status = gettimeofday( &tval, NULL );
1715 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1716 TIMEVAL_TO_TIMESPEC( &tval, &now );
1717
1718 msecs = (4*__kmp_dflt_blocktime) + 200;
1719 now.tv_sec += msecs / 1000;
1720 now.tv_nsec += (msecs % 1000)*1000;
1721
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001722 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_timedwait\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001723 th_gtid ) );
1724 status = pthread_cond_timedwait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex, & now );
1725#else
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001726 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_wait\n",
Jonathan Peyton1bd61b42015-10-08 19:44:16 +00001727 th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001728 status = pthread_cond_wait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex );
1729#endif
1730
1731 if ( (status != 0) && (status != EINTR) && (status != ETIMEDOUT) ) {
1732 KMP_SYSFAIL( "pthread_cond_wait", status );
1733 }
1734#ifdef KMP_DEBUG
1735 if (status == ETIMEDOUT) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001736 if ( flag->is_sleeping() ) {
1737 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001738 } else {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001739 KF_TRACE( 2, ( "__kmp_suspend_template: T#%d timeout wakeup, sleep bit not set!\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001740 th_gtid ) );
1741 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001742 } else if ( flag->is_sleeping() ) {
1743 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001744 }
1745#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001746 } // while
1747
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001748 // Mark the thread as active again (if it was previous marked as inactive)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001749 if ( deactivated ) {
1750 th->th.th_active = TRUE;
1751 if ( TCR_4(th->th.th_in_pool) ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001752 KMP_TEST_THEN_INC32( (kmp_int32 *) &__kmp_thread_pool_active_nth );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001753 th->th.th_active_in_pool = TRUE;
1754 }
1755 }
1756 }
1757
1758#ifdef DEBUG_SUSPEND
1759 {
1760 char buffer[128];
1761 __kmp_print_cond( buffer, &th->th.th_suspend_cv);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001762 __kmp_printf( "__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001763 }
1764#endif
1765
Jim Cownie5e8470a2013-09-27 10:38:44 +00001766 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1767 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1768
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001769 KF_TRACE( 30, ("__kmp_suspend_template: T#%d exit\n", th_gtid ) );
1770}
1771
1772void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
1773 __kmp_suspend_template(th_gtid, flag);
1774}
1775void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
1776 __kmp_suspend_template(th_gtid, flag);
1777}
1778void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
1779 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001780}
1781
1782
1783/* This routine signals the thread specified by target_gtid to wake up
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001784 * after setting the sleep bit indicated by the flag argument to FALSE.
1785 * The target thread must already have called __kmp_suspend_template()
Jim Cownie5e8470a2013-09-27 10:38:44 +00001786 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001787template <class C>
1788static inline void __kmp_resume_template( int target_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001789{
Jonathan Peyton45be4502015-08-11 21:36:41 +00001790 KMP_TIME_DEVELOPER_BLOCK(USER_resume);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001791 kmp_info_t *th = __kmp_threads[target_gtid];
1792 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001793
1794#ifdef KMP_DEBUG
1795 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1796#endif
1797
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001798 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 +00001799 KMP_DEBUG_ASSERT( gtid != target_gtid );
1800
1801 __kmp_suspend_initialize_thread( th );
1802
1803 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1804 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001805
Jonathan Peyton3f5dfc22015-11-09 16:31:51 +00001806 if (!flag) { // coming from __kmp_null_resume_wrapper
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001807 flag = (C *)th->th.th_sleep_loc;
1808 }
1809
Jonathan Peyton3f5dfc22015-11-09 16:31:51 +00001810 // First, check if the flag is null or its type has changed. If so, someone else woke it up.
1811 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 +00001812 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p)\n",
1813 gtid, target_gtid, NULL ) );
1814 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1815 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1816 return;
1817 }
Jonathan Peyton1bd61b42015-10-08 19:44:16 +00001818 else { // if multiple threads are sleeping, flag should be internally referring to a specific thread here
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001819 typename C::flag_t old_spin = flag->unset_sleeping();
1820 if ( ! flag->is_sleeping_val(old_spin) ) {
1821 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p): "
1822 "%u => %u\n",
1823 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001824 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1825 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1826 return;
1827 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001828 KF_TRACE( 5, ( "__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p): "
1829 "%u => %u\n",
1830 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001831 }
1832 TCW_PTR(th->th.th_sleep_loc, NULL);
1833
Jim Cownie5e8470a2013-09-27 10:38:44 +00001834
1835#ifdef DEBUG_SUSPEND
1836 {
1837 char buffer[128];
1838 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001839 __kmp_printf( "__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, target_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001840 }
1841#endif
1842
Jim Cownie5e8470a2013-09-27 10:38:44 +00001843 status = pthread_cond_signal( &th->th.th_suspend_cv.c_cond );
1844 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1845 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1846 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001847 KF_TRACE( 30, ( "__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001848 gtid, target_gtid ) );
1849}
1850
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001851void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
1852 __kmp_resume_template(target_gtid, flag);
1853}
1854void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
1855 __kmp_resume_template(target_gtid, flag);
1856}
1857void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
1858 __kmp_resume_template(target_gtid, flag);
1859}
1860
Jim Cownie07ea89f2014-09-03 11:10:54 +00001861void
1862__kmp_resume_monitor()
1863{
Jonathan Peyton11dc82f2016-05-05 16:15:57 +00001864 KMP_TIME_DEVELOPER_BLOCK(USER_resume);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001865 int status;
1866#ifdef KMP_DEBUG
1867 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1868 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n",
1869 gtid, KMP_GTID_MONITOR ) );
1870 KMP_DEBUG_ASSERT( gtid != KMP_GTID_MONITOR );
1871#endif
1872 status = pthread_mutex_lock( &__kmp_wait_mx.m_mutex );
1873 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1874#ifdef DEBUG_SUSPEND
1875 {
1876 char buffer[128];
1877 __kmp_print_cond( buffer, &__kmp_wait_cv.c_cond );
1878 __kmp_printf( "__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid, KMP_GTID_MONITOR, buffer );
1879 }
1880#endif
1881 status = pthread_cond_signal( &__kmp_wait_cv.c_cond );
1882 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1883 status = pthread_mutex_unlock( &__kmp_wait_mx.m_mutex );
1884 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1885 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d exiting after signaling wake up for T#%d\n",
1886 gtid, KMP_GTID_MONITOR ) );
1887}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001888
1889/* ------------------------------------------------------------------------ */
1890/* ------------------------------------------------------------------------ */
1891
1892void
1893__kmp_yield( int cond )
1894{
1895 if (cond && __kmp_yielding_on) {
1896 sched_yield();
1897 }
1898}
1899
1900/* ------------------------------------------------------------------------ */
1901/* ------------------------------------------------------------------------ */
1902
1903void
1904__kmp_gtid_set_specific( int gtid )
1905{
Jonathan Peytonf2520102016-04-18 21:33:01 +00001906 if( __kmp_init_gtid ) {
1907 int status;
1908 status = pthread_setspecific( __kmp_gtid_threadprivate_key, (void*)(intptr_t)(gtid+1) );
1909 KMP_CHECK_SYSFAIL( "pthread_setspecific", status );
1910 } else {
1911 KA_TRACE( 50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n" ) );
1912 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001913}
1914
1915int
1916__kmp_gtid_get_specific()
1917{
1918 int gtid;
Jonathan Peytonf2520102016-04-18 21:33:01 +00001919 if ( !__kmp_init_gtid ) {
1920 KA_TRACE( 50, ("__kmp_gtid_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001921 return KMP_GTID_SHUTDOWN;
1922 }
1923 gtid = (int)(size_t)pthread_getspecific( __kmp_gtid_threadprivate_key );
1924 if ( gtid == 0 ) {
1925 gtid = KMP_GTID_DNE;
1926 }
1927 else {
1928 gtid--;
1929 }
1930 KA_TRACE( 50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1931 __kmp_gtid_threadprivate_key, gtid ));
1932 return gtid;
1933}
1934
1935/* ------------------------------------------------------------------------ */
1936/* ------------------------------------------------------------------------ */
1937
1938double
1939__kmp_read_cpu_time( void )
1940{
1941 /*clock_t t;*/
1942 struct tms buffer;
1943
1944 /*t =*/ times( & buffer );
1945
1946 return (buffer.tms_utime + buffer.tms_cutime) / (double) CLOCKS_PER_SEC;
1947}
1948
1949int
1950__kmp_read_system_info( struct kmp_sys_info *info )
1951{
1952 int status;
1953 struct rusage r_usage;
1954
1955 memset( info, 0, sizeof( *info ) );
1956
1957 status = getrusage( RUSAGE_SELF, &r_usage);
1958 KMP_CHECK_SYSFAIL_ERRNO( "getrusage", status );
1959
1960 info->maxrss = r_usage.ru_maxrss; /* the maximum resident set size utilized (in kilobytes) */
1961 info->minflt = r_usage.ru_minflt; /* the number of page faults serviced without any I/O */
1962 info->majflt = r_usage.ru_majflt; /* the number of page faults serviced that required I/O */
1963 info->nswap = r_usage.ru_nswap; /* the number of times a process was "swapped" out of memory */
1964 info->inblock = r_usage.ru_inblock; /* the number of times the file system had to perform input */
1965 info->oublock = r_usage.ru_oublock; /* the number of times the file system had to perform output */
1966 info->nvcsw = r_usage.ru_nvcsw; /* the number of times a context switch was voluntarily */
1967 info->nivcsw = r_usage.ru_nivcsw; /* the number of times a context switch was forced */
1968
1969 return (status != 0);
1970}
1971
1972/* ------------------------------------------------------------------------ */
1973/* ------------------------------------------------------------------------ */
1974
Jim Cownie5e8470a2013-09-27 10:38:44 +00001975void
1976__kmp_read_system_time( double *delta )
1977{
1978 double t_ns;
1979 struct timeval tval;
1980 struct timespec stop;
1981 int status;
1982
1983 status = gettimeofday( &tval, NULL );
1984 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1985 TIMEVAL_TO_TIMESPEC( &tval, &stop );
1986 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
1987 *delta = (t_ns * 1e-9);
1988}
1989
1990void
1991__kmp_clear_system_time( void )
1992{
1993 struct timeval tval;
1994 int status;
1995 status = gettimeofday( &tval, NULL );
1996 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1997 TIMEVAL_TO_TIMESPEC( &tval, &__kmp_sys_timer_data.start );
1998}
1999
2000/* ------------------------------------------------------------------------ */
2001/* ------------------------------------------------------------------------ */
2002
2003#ifdef BUILD_TV
2004
2005void
2006__kmp_tv_threadprivate_store( kmp_info_t *th, void *global_addr, void *thread_addr )
2007{
2008 struct tv_data *p;
2009
2010 p = (struct tv_data *) __kmp_allocate( sizeof( *p ) );
2011
2012 p->u.tp.global_addr = global_addr;
2013 p->u.tp.thread_addr = thread_addr;
2014
2015 p->type = (void *) 1;
2016
2017 p->next = th->th.th_local.tv_data;
2018 th->th.th_local.tv_data = p;
2019
2020 if ( p->next == 0 ) {
2021 int rc = pthread_setspecific( __kmp_tv_key, p );
2022 KMP_CHECK_SYSFAIL( "pthread_setspecific", rc );
2023 }
2024}
2025
2026#endif /* BUILD_TV */
2027
2028/* ------------------------------------------------------------------------ */
2029/* ------------------------------------------------------------------------ */
2030
2031static int
2032__kmp_get_xproc( void ) {
2033
2034 int r = 0;
2035
Joerg Sonnenberger7649cd42015-09-21 20:29:12 +00002036 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00002037
2038 r = sysconf( _SC_NPROCESSORS_ONLN );
2039
2040 #elif KMP_OS_DARWIN
2041
2042 // Bug C77011 High "OpenMP Threads and number of active cores".
2043
2044 // Find the number of available CPUs.
2045 kern_return_t rc;
2046 host_basic_info_data_t info;
2047 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
2048 rc = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) & info, & num );
2049 if ( rc == 0 && num == HOST_BASIC_INFO_COUNT ) {
2050 // Cannot use KA_TRACE() here because this code works before trace support is
2051 // initialized.
2052 r = info.avail_cpus;
2053 } else {
2054 KMP_WARNING( CantGetNumAvailCPU );
2055 KMP_INFORM( AssumedNumCPU );
2056 }; // if
2057
2058 #else
2059
2060 #error "Unknown or unsupported OS."
2061
2062 #endif
2063
2064 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
2065
2066} // __kmp_get_xproc
2067
Jim Cownie181b4bb2013-12-23 17:28:57 +00002068int
2069__kmp_read_from_file( char const *path, char const *format, ... )
2070{
2071 int result;
2072 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002073
Jim Cownie181b4bb2013-12-23 17:28:57 +00002074 va_start(args, format);
2075 FILE *f = fopen(path, "rb");
2076 if ( f == NULL )
2077 return 0;
2078 result = vfscanf(f, format, args);
2079 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002080
Jim Cownie5e8470a2013-09-27 10:38:44 +00002081 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00002082}
Jim Cownie5e8470a2013-09-27 10:38:44 +00002083
2084void
2085__kmp_runtime_initialize( void )
2086{
2087 int status;
2088 pthread_mutexattr_t mutex_attr;
2089 pthread_condattr_t cond_attr;
2090
2091 if ( __kmp_init_runtime ) {
2092 return;
2093 }; // if
2094
2095 #if ( KMP_ARCH_X86 || KMP_ARCH_X86_64 )
2096 if ( ! __kmp_cpuinfo.initialized ) {
2097 __kmp_query_cpuid( &__kmp_cpuinfo );
2098 }; // if
2099 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
2100
Jim Cownie5e8470a2013-09-27 10:38:44 +00002101 __kmp_xproc = __kmp_get_xproc();
2102
2103 if ( sysconf( _SC_THREADS ) ) {
2104
2105 /* Query the maximum number of threads */
2106 __kmp_sys_max_nth = sysconf( _SC_THREAD_THREADS_MAX );
2107 if ( __kmp_sys_max_nth == -1 ) {
2108 /* Unlimited threads for NPTL */
2109 __kmp_sys_max_nth = INT_MAX;
2110 }
2111 else if ( __kmp_sys_max_nth <= 1 ) {
2112 /* Can't tell, just use PTHREAD_THREADS_MAX */
2113 __kmp_sys_max_nth = KMP_MAX_NTH;
2114 }
2115
2116 /* Query the minimum stack size */
2117 __kmp_sys_min_stksize = sysconf( _SC_THREAD_STACK_MIN );
2118 if ( __kmp_sys_min_stksize <= 1 ) {
2119 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
2120 }
2121 }
2122
2123 /* Set up minimum number of threads to switch to TLS gtid */
2124 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
2125
Jim Cownie5e8470a2013-09-27 10:38:44 +00002126 #ifdef BUILD_TV
2127 {
2128 int rc = pthread_key_create( & __kmp_tv_key, 0 );
2129 KMP_CHECK_SYSFAIL( "pthread_key_create", rc );
2130 }
2131 #endif
2132
2133 status = pthread_key_create( &__kmp_gtid_threadprivate_key, __kmp_internal_end_dest );
2134 KMP_CHECK_SYSFAIL( "pthread_key_create", status );
2135 status = pthread_mutexattr_init( & mutex_attr );
2136 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
2137 status = pthread_mutex_init( & __kmp_wait_mx.m_mutex, & mutex_attr );
2138 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
2139 status = pthread_condattr_init( & cond_attr );
2140 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
2141 status = pthread_cond_init( & __kmp_wait_cv.c_cond, & cond_attr );
2142 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
2143#if USE_ITT_BUILD
2144 __kmp_itt_initialize();
2145#endif /* USE_ITT_BUILD */
2146
2147 __kmp_init_runtime = TRUE;
2148}
2149
2150void
2151__kmp_runtime_destroy( void )
2152{
2153 int status;
2154
2155 if ( ! __kmp_init_runtime ) {
2156 return; // Nothing to do.
2157 };
2158
2159#if USE_ITT_BUILD
2160 __kmp_itt_destroy();
2161#endif /* USE_ITT_BUILD */
2162
2163 status = pthread_key_delete( __kmp_gtid_threadprivate_key );
2164 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2165 #ifdef BUILD_TV
2166 status = pthread_key_delete( __kmp_tv_key );
2167 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2168 #endif
2169
2170 status = pthread_mutex_destroy( & __kmp_wait_mx.m_mutex );
2171 if ( status != 0 && status != EBUSY ) {
2172 KMP_SYSFAIL( "pthread_mutex_destroy", status );
2173 }
2174 status = pthread_cond_destroy( & __kmp_wait_cv.c_cond );
2175 if ( status != 0 && status != EBUSY ) {
2176 KMP_SYSFAIL( "pthread_cond_destroy", status );
2177 }
Alp Toker763b9392014-02-28 09:42:41 +00002178 #if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00002179 __kmp_affinity_uninitialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00002180 #endif
2181
2182 __kmp_init_runtime = FALSE;
2183}
2184
2185
2186/* Put the thread to sleep for a time period */
2187/* NOTE: not currently used anywhere */
2188void
2189__kmp_thread_sleep( int millis )
2190{
2191 sleep( ( millis + 500 ) / 1000 );
2192}
2193
2194/* Calculate the elapsed wall clock time for the user */
2195void
2196__kmp_elapsed( double *t )
2197{
2198 int status;
2199# ifdef FIX_SGI_CLOCK
2200 struct timespec ts;
2201
2202 status = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
2203 KMP_CHECK_SYSFAIL_ERRNO( "clock_gettime", status );
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +00002204 *t = (double) ts.tv_nsec * (1.0 / (double) KMP_NSEC_PER_SEC) +
Jim Cownie5e8470a2013-09-27 10:38:44 +00002205 (double) ts.tv_sec;
2206# else
2207 struct timeval tv;
2208
2209 status = gettimeofday( & tv, NULL );
2210 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +00002211 *t = (double) tv.tv_usec * (1.0 / (double) KMP_USEC_PER_SEC) +
Jim Cownie5e8470a2013-09-27 10:38:44 +00002212 (double) tv.tv_sec;
2213# endif
2214}
2215
2216/* Calculate the elapsed wall clock tick for the user */
2217void
2218__kmp_elapsed_tick( double *t )
2219{
2220 *t = 1 / (double) CLOCKS_PER_SEC;
2221}
2222
Jonathan Peyton377aa402016-04-14 16:00:37 +00002223/* Return the current time stamp in nsec */
2224kmp_uint64
2225__kmp_now_nsec()
2226{
2227 struct timeval t;
2228 gettimeofday(&t, NULL);
2229 return KMP_NSEC_PER_SEC*t.tv_sec + 1000*t.tv_usec;
2230}
2231
Jim Cownie5e8470a2013-09-27 10:38:44 +00002232/*
2233 Determine whether the given address is mapped into the current address space.
2234*/
2235
2236int
2237__kmp_is_address_mapped( void * addr ) {
2238
2239 int found = 0;
2240 int rc;
2241
Joerg Sonnenberger7649cd42015-09-21 20:29:12 +00002242 #if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00002243
2244 /*
2245 On Linux* OS, read the /proc/<pid>/maps pseudo-file to get all the address ranges mapped
2246 into the address space.
2247 */
2248
2249 char * name = __kmp_str_format( "/proc/%d/maps", getpid() );
2250 FILE * file = NULL;
2251
2252 file = fopen( name, "r" );
2253 KMP_ASSERT( file != NULL );
2254
2255 for ( ; ; ) {
2256
2257 void * beginning = NULL;
2258 void * ending = NULL;
2259 char perms[ 5 ];
2260
2261 rc = fscanf( file, "%p-%p %4s %*[^\n]\n", & beginning, & ending, perms );
2262 if ( rc == EOF ) {
2263 break;
2264 }; // if
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002265 KMP_ASSERT( rc == 3 && KMP_STRLEN( perms ) == 4 ); // Make sure all fields are read.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002266
2267 // Ending address is not included in the region, but beginning is.
2268 if ( ( addr >= beginning ) && ( addr < ending ) ) {
2269 perms[ 2 ] = 0; // 3th and 4th character does not matter.
2270 if ( strcmp( perms, "rw" ) == 0 ) {
2271 // Memory we are looking for should be readable and writable.
2272 found = 1;
2273 }; // if
2274 break;
2275 }; // if
2276
2277 }; // forever
2278
2279 // Free resources.
2280 fclose( file );
2281 KMP_INTERNAL_FREE( name );
2282
2283 #elif KMP_OS_DARWIN
2284
2285 /*
2286 On OS X*, /proc pseudo filesystem is not available. Try to read memory using vm
2287 interface.
2288 */
2289
2290 int buffer;
2291 vm_size_t count;
2292 rc =
2293 vm_read_overwrite(
2294 mach_task_self(), // Task to read memory of.
2295 (vm_address_t)( addr ), // Address to read from.
2296 1, // Number of bytes to be read.
2297 (vm_address_t)( & buffer ), // Address of buffer to save read bytes in.
2298 & count // Address of var to save number of read bytes in.
2299 );
2300 if ( rc == 0 ) {
2301 // Memory successfully read.
2302 found = 1;
2303 }; // if
2304
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +00002305 #elif KMP_OS_FREEBSD || KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +00002306
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +00002307 // FIXME(FreeBSD, NetBSD): Implement this
Alp Toker763b9392014-02-28 09:42:41 +00002308 found = 1;
2309
Jim Cownie5e8470a2013-09-27 10:38:44 +00002310 #else
2311
2312 #error "Unknown or unsupported OS"
2313
2314 #endif
2315
2316 return found;
2317
2318} // __kmp_is_address_mapped
2319
2320#ifdef USE_LOAD_BALANCE
2321
2322
2323# if KMP_OS_DARWIN
2324
2325// The function returns the rounded value of the system load average
2326// during given time interval which depends on the value of
2327// __kmp_load_balance_interval variable (default is 60 sec, other values
2328// may be 300 sec or 900 sec).
2329// It returns -1 in case of error.
2330int
2331__kmp_get_load_balance( int max )
2332{
2333 double averages[3];
2334 int ret_avg = 0;
2335
2336 int res = getloadavg( averages, 3 );
2337
2338 //Check __kmp_load_balance_interval to determine which of averages to use.
2339 // getloadavg() may return the number of samples less than requested that is
2340 // less than 3.
2341 if ( __kmp_load_balance_interval < 180 && ( res >= 1 ) ) {
2342 ret_avg = averages[0];// 1 min
2343 } else if ( ( __kmp_load_balance_interval >= 180
2344 && __kmp_load_balance_interval < 600 ) && ( res >= 2 ) ) {
2345 ret_avg = averages[1];// 5 min
2346 } else if ( ( __kmp_load_balance_interval >= 600 ) && ( res == 3 ) ) {
2347 ret_avg = averages[2];// 15 min
Alp Toker8f2d3f02014-02-24 10:40:15 +00002348 } else {// Error occurred
Jim Cownie5e8470a2013-09-27 10:38:44 +00002349 return -1;
2350 }
2351
2352 return ret_avg;
2353}
2354
2355# else // Linux* OS
2356
2357// The fuction returns number of running (not sleeping) threads, or -1 in case of error.
2358// Error could be reported if Linux* OS kernel too old (without "/proc" support).
2359// Counting running threads stops if max running threads encountered.
2360int
2361__kmp_get_load_balance( int max )
2362{
2363 static int permanent_error = 0;
2364
2365 static int glb_running_threads = 0; /* Saved count of the running threads for the thread balance algortihm */
2366 static double glb_call_time = 0; /* Thread balance algorithm call time */
2367
2368 int running_threads = 0; // Number of running threads in the system.
2369
2370 DIR * proc_dir = NULL; // Handle of "/proc/" directory.
2371 struct dirent * proc_entry = NULL;
2372
2373 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2374 DIR * task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2375 struct dirent * task_entry = NULL;
2376 int task_path_fixed_len;
2377
2378 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2379 int stat_file = -1;
2380 int stat_path_fixed_len;
2381
2382 int total_processes = 0; // Total number of processes in system.
2383 int total_threads = 0; // Total number of threads in system.
2384
2385 double call_time = 0.0;
2386
2387 __kmp_str_buf_init( & task_path );
2388 __kmp_str_buf_init( & stat_path );
2389
2390 __kmp_elapsed( & call_time );
2391
2392 if ( glb_call_time &&
2393 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
2394 running_threads = glb_running_threads;
2395 goto finish;
2396 }
2397
2398 glb_call_time = call_time;
2399
2400 // Do not spend time on scanning "/proc/" if we have a permanent error.
2401 if ( permanent_error ) {
2402 running_threads = -1;
2403 goto finish;
2404 }; // if
2405
2406 if ( max <= 0 ) {
2407 max = INT_MAX;
2408 }; // if
2409
2410 // Open "/proc/" directory.
2411 proc_dir = opendir( "/proc" );
2412 if ( proc_dir == NULL ) {
2413 // Cannot open "/prroc/". Probably the kernel does not support it. Return an error now and
2414 // in subsequent calls.
2415 running_threads = -1;
2416 permanent_error = 1;
2417 goto finish;
2418 }; // if
2419
2420 // Initialize fixed part of task_path. This part will not change.
2421 __kmp_str_buf_cat( & task_path, "/proc/", 6 );
2422 task_path_fixed_len = task_path.used; // Remember number of used characters.
2423
2424 proc_entry = readdir( proc_dir );
2425 while ( proc_entry != NULL ) {
2426 // Proc entry is a directory and name starts with a digit. Assume it is a process'
2427 // directory.
2428 if ( proc_entry->d_type == DT_DIR && isdigit( proc_entry->d_name[ 0 ] ) ) {
2429
2430 ++ total_processes;
2431 // Make sure init process is the very first in "/proc", so we can replace
2432 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes == 1.
2433 // We are going to check that total_processes == 1 => d_name == "1" is true (where
2434 // "=>" is implication). Since C++ does not have => operator, let us replace it with its
2435 // equivalent: a => b == ! a || b.
2436 KMP_DEBUG_ASSERT( total_processes != 1 || strcmp( proc_entry->d_name, "1" ) == 0 );
2437
2438 // Construct task_path.
2439 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002440 __kmp_str_buf_cat( & task_path, proc_entry->d_name, KMP_STRLEN( proc_entry->d_name ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002441 __kmp_str_buf_cat( & task_path, "/task", 5 );
2442
2443 task_dir = opendir( task_path.str );
2444 if ( task_dir == NULL ) {
2445 // Process can finish between reading "/proc/" directory entry and opening process'
2446 // "task/" directory. So, in general case we should not complain, but have to skip
2447 // this process and read the next one.
2448 // But on systems with no "task/" support we will spend lot of time to scan "/proc/"
2449 // tree again and again without any benefit. "init" process (its pid is 1) should
2450 // exist always, so, if we cannot open "/proc/1/task/" directory, it means "task/"
2451 // is not supported by kernel. Report an error now and in the future.
2452 if ( strcmp( proc_entry->d_name, "1" ) == 0 ) {
2453 running_threads = -1;
2454 permanent_error = 1;
2455 goto finish;
2456 }; // if
2457 } else {
2458 // Construct fixed part of stat file path.
2459 __kmp_str_buf_clear( & stat_path );
2460 __kmp_str_buf_cat( & stat_path, task_path.str, task_path.used );
2461 __kmp_str_buf_cat( & stat_path, "/", 1 );
2462 stat_path_fixed_len = stat_path.used;
2463
2464 task_entry = readdir( task_dir );
2465 while ( task_entry != NULL ) {
2466 // It is a directory and name starts with a digit.
2467 if ( proc_entry->d_type == DT_DIR && isdigit( task_entry->d_name[ 0 ] ) ) {
2468
2469 ++ total_threads;
2470
2471 // Consruct complete stat file path. Easiest way would be:
2472 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str, task_entry->d_name );
2473 // but seriae of __kmp_str_buf_cat works a bit faster.
2474 stat_path.used = stat_path_fixed_len; // Reset stat path to its fixed part.
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002475 __kmp_str_buf_cat( & stat_path, task_entry->d_name, KMP_STRLEN( task_entry->d_name ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002476 __kmp_str_buf_cat( & stat_path, "/stat", 5 );
2477
2478 // Note: Low-level API (open/read/close) is used. High-level API
2479 // (fopen/fclose) works ~ 30 % slower.
2480 stat_file = open( stat_path.str, O_RDONLY );
2481 if ( stat_file == -1 ) {
2482 // We cannot report an error because task (thread) can terminate just
2483 // before reading this file.
2484 } else {
2485 /*
2486 Content of "stat" file looks like:
2487
2488 24285 (program) S ...
2489
2490 It is a single line (if program name does not include fanny
2491 symbols). First number is a thread id, then name of executable file
2492 name in paretheses, then state of the thread. We need just thread
2493 state.
2494
2495 Good news: Length of program name is 15 characters max. Longer
2496 names are truncated.
2497
2498 Thus, we need rather short buffer: 15 chars for program name +
2499 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
2500
2501 Bad news: Program name may contain special symbols like space,
2502 closing parenthesis, or even new line. This makes parsing "stat"
2503 file not 100 % reliable. In case of fanny program names parsing
2504 may fail (report incorrect thread state).
2505
2506 Parsing "status" file looks more promissing (due to different
2507 file structure and escaping special symbols) but reading and
2508 parsing of "status" file works slower.
2509
2510 -- ln
2511 */
2512 char buffer[ 65 ];
2513 int len;
2514 len = read( stat_file, buffer, sizeof( buffer ) - 1 );
2515 if ( len >= 0 ) {
2516 buffer[ len ] = 0;
2517 // Using scanf:
2518 // sscanf( buffer, "%*d (%*s) %c ", & state );
2519 // looks very nice, but searching for a closing parenthesis works a
2520 // bit faster.
2521 char * close_parent = strstr( buffer, ") " );
2522 if ( close_parent != NULL ) {
2523 char state = * ( close_parent + 2 );
2524 if ( state == 'R' ) {
2525 ++ running_threads;
2526 if ( running_threads >= max ) {
2527 goto finish;
2528 }; // if
2529 }; // if
2530 }; // if
2531 }; // if
2532 close( stat_file );
2533 stat_file = -1;
2534 }; // if
2535 }; // if
2536 task_entry = readdir( task_dir );
2537 }; // while
2538 closedir( task_dir );
2539 task_dir = NULL;
2540 }; // if
2541 }; // if
2542 proc_entry = readdir( proc_dir );
2543 }; // while
2544
2545 //
2546 // There _might_ be a timing hole where the thread executing this
2547 // code get skipped in the load balance, and running_threads is 0.
2548 // Assert in the debug builds only!!!
2549 //
2550 KMP_DEBUG_ASSERT( running_threads > 0 );
2551 if ( running_threads <= 0 ) {
2552 running_threads = 1;
2553 }
2554
2555 finish: // Clean up and exit.
2556 if ( proc_dir != NULL ) {
2557 closedir( proc_dir );
2558 }; // if
2559 __kmp_str_buf_free( & task_path );
2560 if ( task_dir != NULL ) {
2561 closedir( task_dir );
2562 }; // if
2563 __kmp_str_buf_free( & stat_path );
2564 if ( stat_file != -1 ) {
2565 close( stat_file );
2566 }; // if
2567
2568 glb_running_threads = running_threads;
2569
2570 return running_threads;
2571
2572} // __kmp_get_load_balance
2573
2574# endif // KMP_OS_DARWIN
2575
2576#endif // USE_LOAD_BALANCE
2577
Paul Osmialowski7e5e8682016-05-13 08:26:42 +00002578#if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || (KMP_OS_LINUX && KMP_ARCH_AARCH64))
Jim Cownie3051f972014-08-07 10:12:54 +00002579
2580// we really only need the case with 1 argument, because CLANG always build
2581// a struct of pointers to shared variables referenced in the outlined function
2582int
2583__kmp_invoke_microtask( microtask_t pkfn,
2584 int gtid, int tid,
Jonathan Peyton122dd762015-07-13 18:55:45 +00002585 int argc, void *p_argv[]
2586#if OMPT_SUPPORT
2587 , void **exit_frame_ptr
2588#endif
2589)
2590{
2591#if OMPT_SUPPORT
2592 *exit_frame_ptr = __builtin_frame_address(0);
2593#endif
2594
Jim Cownie3051f972014-08-07 10:12:54 +00002595 switch (argc) {
2596 default:
2597 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2598 fflush(stderr);
2599 exit(-1);
2600 case 0:
2601 (*pkfn)(&gtid, &tid);
2602 break;
2603 case 1:
2604 (*pkfn)(&gtid, &tid, p_argv[0]);
2605 break;
2606 case 2:
2607 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2608 break;
2609 case 3:
2610 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2611 break;
2612 case 4:
2613 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2614 break;
2615 case 5:
2616 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2617 break;
2618 case 6:
2619 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2620 p_argv[5]);
2621 break;
2622 case 7:
2623 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2624 p_argv[5], p_argv[6]);
2625 break;
2626 case 8:
2627 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2628 p_argv[5], p_argv[6], p_argv[7]);
2629 break;
2630 case 9:
2631 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2632 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2633 break;
2634 case 10:
2635 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2636 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2637 break;
2638 case 11:
2639 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2640 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2641 break;
2642 case 12:
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]);
2646 break;
2647 case 13:
2648 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2649 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2650 p_argv[11], p_argv[12]);
2651 break;
2652 case 14:
2653 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2654 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2655 p_argv[11], p_argv[12], p_argv[13]);
2656 break;
2657 case 15:
2658 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2659 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2660 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2661 break;
2662 }
2663
Jonathan Peyton122dd762015-07-13 18:55:45 +00002664#if OMPT_SUPPORT
2665 *exit_frame_ptr = 0;
2666#endif
2667
Jim Cownie3051f972014-08-07 10:12:54 +00002668 return 1;
2669}
2670
2671#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002672
Jim Cownie5e8470a2013-09-27 10:38:44 +00002673// end of file //
2674