blob: d8aacf4c41f939ceb8f71a3958c9b4416e71d69c [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * z_Linux_util.c -- platform specific routines.
Jim Cownie4cc4bb42014-10-07 16:25:50 +00003 * $Revision: 43473 $
4 * $Date: 2014-09-26 15:02:57 -0500 (Fri, 26 Sep 2014) $
Jim Cownie5e8470a2013-09-27 10:38:44 +00005 */
6
7
8//===----------------------------------------------------------------------===//
9//
10// The LLVM Compiler Infrastructure
11//
12// This file is dual licensed under the MIT and the University of Illinois Open
13// Source Licenses. See LICENSE.txt for details.
14//
15//===----------------------------------------------------------------------===//
16
17
18#include "kmp.h"
19#include "kmp_wrapper_getpid.h"
20#include "kmp_itt.h"
21#include "kmp_str.h"
22#include "kmp_i18n.h"
23#include "kmp_io.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000024#include "kmp_stats.h"
25#include "kmp_wait_release.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000026
Alp Toker763b9392014-02-28 09:42:41 +000027#if !KMP_OS_FREEBSD
28# include <alloca.h>
29#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000030#include <unistd.h>
31#include <math.h> // HUGE_VAL.
32#include <sys/time.h>
33#include <sys/times.h>
34#include <sys/resource.h>
35#include <sys/syscall.h>
36
Jim Cownie3051f972014-08-07 10:12:54 +000037#if KMP_OS_LINUX && !KMP_OS_CNK
Jim Cownie5e8470a2013-09-27 10:38:44 +000038# include <sys/sysinfo.h>
Andrey Churbanovcbda8682015-01-13 14:43:35 +000039# if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64)
Jim Cownie5e8470a2013-09-27 10:38:44 +000040// We should really include <futex.h>, but that causes compatibility problems on different
41// Linux* OS distributions that either require that you include (or break when you try to include)
42// <pci/types.h>.
43// Since all we need is the two macros below (which are part of the kernel ABI, so can't change)
44// we just define the constants here and don't include <futex.h>
45# ifndef FUTEX_WAIT
46# define FUTEX_WAIT 0
47# endif
48# ifndef FUTEX_WAKE
49# define FUTEX_WAKE 1
50# endif
51# endif
52#elif KMP_OS_DARWIN
53# include <sys/sysctl.h>
54# include <mach/mach.h>
Alp Toker763b9392014-02-28 09:42:41 +000055#elif KMP_OS_FREEBSD
56# include <sys/sysctl.h>
57# include <pthread_np.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000058#endif
59
60
61#include <dirent.h>
62#include <ctype.h>
63#include <fcntl.h>
64
Jim Cownie181b4bb2013-12-23 17:28:57 +000065// For non-x86 architecture
Andrey Churbanovcbda8682015-01-13 14:43:35 +000066#if KMP_COMPILER_GCC && !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64)
Jim Cownie181b4bb2013-12-23 17:28:57 +000067# include <stdbool.h>
68# include <ffi.h>
69#endif
70
Jim Cownie5e8470a2013-09-27 10:38:44 +000071/* ------------------------------------------------------------------------ */
72/* ------------------------------------------------------------------------ */
73
74struct kmp_sys_timer {
75 struct timespec start;
76};
77
78// Convert timespec to nanoseconds.
79#define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec)
80
81static struct kmp_sys_timer __kmp_sys_timer_data;
82
83#if KMP_HANDLE_SIGNALS
84 typedef void (* sig_func_t )( int );
85 STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[ NSIG ];
86 static sigset_t __kmp_sigset;
87#endif
88
89static int __kmp_init_runtime = FALSE;
90
91static int __kmp_fork_count = 0;
92
93static pthread_condattr_t __kmp_suspend_cond_attr;
94static pthread_mutexattr_t __kmp_suspend_mutex_attr;
95
96static kmp_cond_align_t __kmp_wait_cv;
97static kmp_mutex_align_t __kmp_wait_mx;
98
99/* ------------------------------------------------------------------------ */
100/* ------------------------------------------------------------------------ */
101
102#ifdef DEBUG_SUSPEND
103static void
104__kmp_print_cond( char *buffer, kmp_cond_align_t *cond )
105{
106 sprintf( buffer, "(cond (lock (%ld, %d)), (descr (%p)))",
107 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
108 cond->c_cond.__c_waiting );
109}
110#endif
111
112/* ------------------------------------------------------------------------ */
113/* ------------------------------------------------------------------------ */
114
Jim Cownie3051f972014-08-07 10:12:54 +0000115#if ( KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000116
117/*
118 * Affinity support
119 */
120
121/*
122 * On some of the older OS's that we build on, these constants aren't present
123 * in <asm/unistd.h> #included from <sys.syscall.h>. They must be the same on
124 * all systems of the same arch where they are defined, and they cannot change.
125 * stone forever.
126 */
127
Jim Cownie181b4bb2013-12-23 17:28:57 +0000128# if KMP_ARCH_X86 || KMP_ARCH_ARM
Jim Cownie5e8470a2013-09-27 10:38:44 +0000129# ifndef __NR_sched_setaffinity
130# define __NR_sched_setaffinity 241
131# elif __NR_sched_setaffinity != 241
132# error Wrong code for setaffinity system call.
133# endif /* __NR_sched_setaffinity */
134# ifndef __NR_sched_getaffinity
135# define __NR_sched_getaffinity 242
136# elif __NR_sched_getaffinity != 242
137# error Wrong code for getaffinity system call.
138# endif /* __NR_sched_getaffinity */
139
Andrey Churbanovcbda8682015-01-13 14:43:35 +0000140# elif KMP_ARCH_AARCH64
141# ifndef __NR_sched_setaffinity
142# define __NR_sched_setaffinity 122
143# elif __NR_sched_setaffinity != 122
144# error Wrong code for setaffinity system call.
145# endif /* __NR_sched_setaffinity */
146# ifndef __NR_sched_getaffinity
147# define __NR_sched_getaffinity 123
148# elif __NR_sched_getaffinity != 123
149# error Wrong code for getaffinity system call.
150# endif /* __NR_sched_getaffinity */
151
Jim Cownie5e8470a2013-09-27 10:38:44 +0000152# elif KMP_ARCH_X86_64
153# ifndef __NR_sched_setaffinity
154# define __NR_sched_setaffinity 203
155# elif __NR_sched_setaffinity != 203
156# error Wrong code for setaffinity system call.
157# endif /* __NR_sched_setaffinity */
158# ifndef __NR_sched_getaffinity
159# define __NR_sched_getaffinity 204
160# elif __NR_sched_getaffinity != 204
161# error Wrong code for getaffinity system call.
162# endif /* __NR_sched_getaffinity */
163
Jim Cownie3051f972014-08-07 10:12:54 +0000164# elif KMP_ARCH_PPC64
165# ifndef __NR_sched_setaffinity
166# define __NR_sched_setaffinity 222
167# elif __NR_sched_setaffinity != 222
168# error Wrong code for setaffinity system call.
169# endif /* __NR_sched_setaffinity */
170# ifndef __NR_sched_getaffinity
171# define __NR_sched_getaffinity 223
172# elif __NR_sched_getaffinity != 223
173# error Wrong code for getaffinity system call.
174# endif /* __NR_sched_getaffinity */
175
176
Jim Cownie5e8470a2013-09-27 10:38:44 +0000177# else
178# error Unknown or unsupported architecture
179
180# endif /* KMP_ARCH_* */
181
182int
183__kmp_set_system_affinity( kmp_affin_mask_t const *mask, int abort_on_error )
184{
185 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
186 "Illegal set affinity operation when not capable");
187
188 int retval = syscall( __NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask );
189 if (retval >= 0) {
190 return 0;
191 }
192 int error = errno;
193 if (abort_on_error) {
194 __kmp_msg(
195 kmp_ms_fatal,
196 KMP_MSG( FatalSysError ),
197 KMP_ERR( error ),
198 __kmp_msg_null
199 );
200 }
201 return error;
202}
203
204int
205__kmp_get_system_affinity( kmp_affin_mask_t *mask, int abort_on_error )
206{
207 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
208 "Illegal get affinity operation when not capable");
209
210 int retval = syscall( __NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask );
211 if (retval >= 0) {
212 return 0;
213 }
214 int error = errno;
215 if (abort_on_error) {
216 __kmp_msg(
217 kmp_ms_fatal,
218 KMP_MSG( FatalSysError ),
219 KMP_ERR( error ),
220 __kmp_msg_null
221 );
222 }
223 return error;
224}
225
226void
227__kmp_affinity_bind_thread( int which )
228{
229 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
230 "Illegal set affinity operation when not capable");
231
232 kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
233 KMP_CPU_ZERO(mask);
234 KMP_CPU_SET(which, mask);
235 __kmp_set_system_affinity(mask, TRUE);
236}
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 }
282 __kmp_affin_mask_size = 0; // should already be 0
283 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 }
310 __kmp_affin_mask_size = 0; // should already be 0
311 KMP_INTERNAL_FREE(buf);
312 }
313 if (errno == EFAULT) {
314 __kmp_affin_mask_size = gCode;
315 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 }
358 __kmp_affin_mask_size = 0; // should already be 0
359 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 }
389 __kmp_affin_mask_size = 0; // should already be 0
390 KMP_INTERNAL_FREE(buf);
391 return;
392 }
393 if (errno == EFAULT) {
394 __kmp_affin_mask_size = gCode;
395 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 //
410 __kmp_affin_mask_size = 0;
411 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
421
422/*
423 * Change thread to the affinity mask pointed to by affin_mask argument
424 * and return a pointer to the old value in the old_mask argument, if argument
425 * is non-NULL.
426 */
427
428void
429__kmp_change_thread_affinity_mask( int gtid, kmp_affin_mask_t *new_mask,
430 kmp_affin_mask_t *old_mask )
431{
432 KMP_DEBUG_ASSERT( gtid == __kmp_get_gtid() );
433 if ( KMP_AFFINITY_CAPABLE() ) {
434 int status;
435 kmp_info_t *th = __kmp_threads[ gtid ];
436
437 KMP_DEBUG_ASSERT( new_mask != NULL );
438
439 if ( old_mask != NULL ) {
440 status = __kmp_get_system_affinity( old_mask, TRUE );
441 int error = errno;
442 if ( status != 0 ) {
443 __kmp_msg(
444 kmp_ms_fatal,
445 KMP_MSG( ChangeThreadAffMaskError ),
446 KMP_ERR( error ),
447 __kmp_msg_null
448 );
449 }
450 }
451
452 __kmp_set_system_affinity( new_mask, TRUE );
453
454 if (__kmp_affinity_verbose) {
455 char old_buf[KMP_AFFIN_MASK_PRINT_LEN];
456 char new_buf[KMP_AFFIN_MASK_PRINT_LEN];
457 __kmp_affinity_print_mask(old_buf, KMP_AFFIN_MASK_PRINT_LEN, old_mask);
458 __kmp_affinity_print_mask(new_buf, KMP_AFFIN_MASK_PRINT_LEN, new_mask);
459 KMP_INFORM( ChangeAffMask, "KMP_AFFINITY (Bind)", gtid, old_buf, new_buf );
460
461 }
462
463 /* Make sure old value is correct in thread data structures */
464 KMP_DEBUG_ASSERT( old_mask != NULL && (memcmp(old_mask,
465 th->th.th_affin_mask, __kmp_affin_mask_size) == 0) );
466 KMP_CPU_COPY( th->th.th_affin_mask, new_mask );
467 }
468}
469
Alp Toker98758b02014-03-02 04:12:06 +0000470#endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000471
472/* ------------------------------------------------------------------------ */
473/* ------------------------------------------------------------------------ */
474
Andrey Churbanovcbda8682015-01-13 14:43:35 +0000475#if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && !KMP_OS_CNK
Jim Cownie5e8470a2013-09-27 10:38:44 +0000476
477int
478__kmp_futex_determine_capable()
479{
480 int loc = 0;
481 int rc = syscall( __NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0 );
482 int retval = ( rc == 0 ) || ( errno != ENOSYS );
483
484 KA_TRACE(10, ( "__kmp_futex_determine_capable: rc = %d errno = %d\n", rc,
485 errno ) );
486 KA_TRACE(10, ( "__kmp_futex_determine_capable: futex syscall%s supported\n",
487 retval ? "" : " not" ) );
488
489 return retval;
490}
491
Jim Cownie3051f972014-08-07 10:12:54 +0000492#endif // KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM) && !KMP_OS_CNK
Jim Cownie5e8470a2013-09-27 10:38:44 +0000493
494/* ------------------------------------------------------------------------ */
495/* ------------------------------------------------------------------------ */
496
497#if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS)
498/*
499 * Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
500 * use compare_and_store for these routines
501 */
502
503kmp_int32
504__kmp_test_then_or32( volatile kmp_int32 *p, kmp_int32 d )
505{
506 kmp_int32 old_value, new_value;
507
508 old_value = TCR_4( *p );
509 new_value = old_value | d;
510
Jim Cownie3051f972014-08-07 10:12:54 +0000511 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000512 {
513 KMP_CPU_PAUSE();
514 old_value = TCR_4( *p );
515 new_value = old_value | d;
516 }
517 return old_value;
518}
519
520kmp_int32
521__kmp_test_then_and32( volatile kmp_int32 *p, kmp_int32 d )
522{
523 kmp_int32 old_value, new_value;
524
525 old_value = TCR_4( *p );
526 new_value = old_value & d;
527
Jim Cownie3051f972014-08-07 10:12:54 +0000528 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000529 {
530 KMP_CPU_PAUSE();
531 old_value = TCR_4( *p );
532 new_value = old_value & d;
533 }
534 return old_value;
535}
536
Andrey Churbanovcbda8682015-01-13 14:43:35 +0000537# if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64
Jim Cownie5e8470a2013-09-27 10:38:44 +0000538kmp_int64
539__kmp_test_then_add64( volatile kmp_int64 *p, kmp_int64 d )
540{
541 kmp_int64 old_value, new_value;
542
543 old_value = TCR_8( *p );
544 new_value = old_value + d;
545
Jim Cownie3051f972014-08-07 10:12:54 +0000546 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000547 {
548 KMP_CPU_PAUSE();
549 old_value = TCR_8( *p );
550 new_value = old_value + d;
551 }
552 return old_value;
553}
554# endif /* KMP_ARCH_X86 */
555
556kmp_int64
557__kmp_test_then_or64( volatile kmp_int64 *p, kmp_int64 d )
558{
559 kmp_int64 old_value, new_value;
560
561 old_value = TCR_8( *p );
562 new_value = old_value | d;
Jim Cownie3051f972014-08-07 10:12:54 +0000563 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000564 {
565 KMP_CPU_PAUSE();
566 old_value = TCR_8( *p );
567 new_value = old_value | d;
568 }
569 return old_value;
570}
571
572kmp_int64
573__kmp_test_then_and64( volatile kmp_int64 *p, kmp_int64 d )
574{
575 kmp_int64 old_value, new_value;
576
577 old_value = TCR_8( *p );
578 new_value = old_value & d;
Jim Cownie3051f972014-08-07 10:12:54 +0000579 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000580 {
581 KMP_CPU_PAUSE();
582 old_value = TCR_8( *p );
583 new_value = old_value & d;
584 }
585 return old_value;
586}
587
588#endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
589
590void
591__kmp_terminate_thread( int gtid )
592{
593 int status;
594 kmp_info_t *th = __kmp_threads[ gtid ];
595
596 if ( !th ) return;
597
598 #ifdef KMP_CANCEL_THREADS
599 KA_TRACE( 10, ("__kmp_terminate_thread: kill (%d)\n", gtid ) );
600 status = pthread_cancel( th->th.th_info.ds.ds_thread );
601 if ( status != 0 && status != ESRCH ) {
602 __kmp_msg(
603 kmp_ms_fatal,
604 KMP_MSG( CantTerminateWorkerThread ),
605 KMP_ERR( status ),
606 __kmp_msg_null
607 );
608 }; // if
609 #endif
610 __kmp_yield( TRUE );
611} //
612
613/* ------------------------------------------------------------------------ */
614/* ------------------------------------------------------------------------ */
615
616/* ------------------------------------------------------------------------ */
617/* ------------------------------------------------------------------------ */
618
619/*
620 * Set thread stack info according to values returned by
621 * pthread_getattr_np().
622 * If values are unreasonable, assume call failed and use
623 * incremental stack refinement method instead.
624 * Returns TRUE if the stack parameters could be determined exactly,
625 * FALSE if incremental refinement is necessary.
626 */
627static kmp_int32
628__kmp_set_stack_info( int gtid, kmp_info_t *th )
629{
630 int stack_data;
Alp Toker763b9392014-02-28 09:42:41 +0000631#if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000632 /* Linux* OS only -- no pthread_getattr_np support on OS X* */
633 pthread_attr_t attr;
634 int status;
635 size_t size = 0;
636 void * addr = 0;
637
638 /* Always do incremental stack refinement for ubermaster threads since the initial
639 thread stack range can be reduced by sibling thread creation so pthread_attr_getstack
640 may cause thread gtid aliasing */
641 if ( ! KMP_UBER_GTID(gtid) ) {
642
643 /* Fetch the real thread attributes */
644 status = pthread_attr_init( &attr );
645 KMP_CHECK_SYSFAIL( "pthread_attr_init", status );
Alp Toker763b9392014-02-28 09:42:41 +0000646#if KMP_OS_FREEBSD
647 status = pthread_attr_get_np( pthread_self(), &attr );
648 KMP_CHECK_SYSFAIL( "pthread_attr_get_np", status );
649#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000650 status = pthread_getattr_np( pthread_self(), &attr );
651 KMP_CHECK_SYSFAIL( "pthread_getattr_np", status );
Alp Toker763b9392014-02-28 09:42:41 +0000652#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000653 status = pthread_attr_getstack( &attr, &addr, &size );
654 KMP_CHECK_SYSFAIL( "pthread_attr_getstack", status );
655 KA_TRACE( 60, ( "__kmp_set_stack_info: T#%d pthread_attr_getstack returned size: %lu, "
656 "low addr: %p\n",
657 gtid, size, addr ));
658
659 status = pthread_attr_destroy( &attr );
660 KMP_CHECK_SYSFAIL( "pthread_attr_destroy", status );
661 }
662
663 if ( size != 0 && addr != 0 ) { /* was stack parameter determination successful? */
664 /* Store the correct base and size */
665 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size));
666 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
667 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
668 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000669 }
Alp Toker763b9392014-02-28 09:42:41 +0000670#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD */
Alp Toker763b9392014-02-28 09:42:41 +0000671 /* Use incremental refinement starting from initial conservative estimate */
672 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
673 TCW_PTR(th -> th.th_info.ds.ds_stackbase, &stack_data);
674 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
675 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000676}
677
678static void*
679__kmp_launch_worker( void *thr )
680{
681 int status, old_type, old_state;
682#ifdef KMP_BLOCK_SIGNALS
683 sigset_t new_set, old_set;
684#endif /* KMP_BLOCK_SIGNALS */
685 void *exit_val;
686 void *padding = 0;
687 int gtid;
688 int error;
689
690 gtid = ((kmp_info_t*)thr) -> th.th_info.ds.ds_gtid;
691 __kmp_gtid_set_specific( gtid );
692#ifdef KMP_TDATA_GTID
693 __kmp_gtid = gtid;
694#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000695#if KMP_STATS_ENABLED
696 // set __thread local index to point to thread-specific stats
697 __kmp_stats_thread_ptr = ((kmp_info_t*)thr)->th.th_stats;
698#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000699
700#if USE_ITT_BUILD
701 __kmp_itt_thread_name( gtid );
702#endif /* USE_ITT_BUILD */
703
Alp Toker763b9392014-02-28 09:42:41 +0000704#if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000705 __kmp_affinity_set_init_mask( gtid, FALSE );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000706#endif
707
708#ifdef KMP_CANCEL_THREADS
709 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
710 KMP_CHECK_SYSFAIL( "pthread_setcanceltype", status );
711 /* josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? */
712 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
713 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
714#endif
715
716#if KMP_ARCH_X86 || KMP_ARCH_X86_64
717 //
718 // Set the FP control regs to be a copy of
719 // the parallel initialization thread's.
720 //
721 __kmp_clear_x87_fpu_status_word();
722 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
723 __kmp_load_mxcsr( &__kmp_init_mxcsr );
724#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
725
726#ifdef KMP_BLOCK_SIGNALS
727 status = sigfillset( & new_set );
728 KMP_CHECK_SYSFAIL_ERRNO( "sigfillset", status );
729 status = pthread_sigmask( SIG_BLOCK, & new_set, & old_set );
730 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
731#endif /* KMP_BLOCK_SIGNALS */
732
Alp Toker763b9392014-02-28 09:42:41 +0000733#if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000734 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
735 padding = alloca( gtid * __kmp_stkoffset );
736 }
737#endif
738
739 KMP_MB();
740 __kmp_set_stack_info( gtid, (kmp_info_t*)thr );
741
742 __kmp_check_stack_overlap( (kmp_info_t*)thr );
743
744 exit_val = __kmp_launch_thread( (kmp_info_t *) thr );
745
746#ifdef KMP_BLOCK_SIGNALS
747 status = pthread_sigmask( SIG_SETMASK, & old_set, NULL );
748 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
749#endif /* KMP_BLOCK_SIGNALS */
750
751 return exit_val;
752}
753
754
755/* The monitor thread controls all of the threads in the complex */
756
757static void*
758__kmp_launch_monitor( void *thr )
759{
760 int status, old_type, old_state;
761#ifdef KMP_BLOCK_SIGNALS
762 sigset_t new_set;
763#endif /* KMP_BLOCK_SIGNALS */
764 struct timespec interval;
765 int yield_count;
766 int yield_cycles = 0;
767 int error;
768
769 KMP_MB(); /* Flush all pending memory write invalidates. */
770
771 KA_TRACE( 10, ("__kmp_launch_monitor: #1 launched\n" ) );
772
773 /* register us as the monitor thread */
774 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
775#ifdef KMP_TDATA_GTID
776 __kmp_gtid = KMP_GTID_MONITOR;
777#endif
778
779 KMP_MB();
780
781#if USE_ITT_BUILD
782 __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore monitor thread.
783#endif /* USE_ITT_BUILD */
784
785 __kmp_set_stack_info( ((kmp_info_t*)thr)->th.th_info.ds.ds_gtid, (kmp_info_t*)thr );
786
787 __kmp_check_stack_overlap( (kmp_info_t*)thr );
788
789#ifdef KMP_CANCEL_THREADS
790 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
791 KMP_CHECK_SYSFAIL( "pthread_setcanceltype", status );
792 /* josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? */
793 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
794 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
795#endif
796
797 #if KMP_REAL_TIME_FIX
798 // This is a potential fix which allows application with real-time scheduling policy work.
799 // However, decision about the fix is not made yet, so it is disabled by default.
800 { // Are program started with real-time scheduling policy?
801 int sched = sched_getscheduler( 0 );
802 if ( sched == SCHED_FIFO || sched == SCHED_RR ) {
803 // Yes, we are a part of real-time application. Try to increase the priority of the
804 // monitor.
805 struct sched_param param;
806 int max_priority = sched_get_priority_max( sched );
807 int rc;
808 KMP_WARNING( RealTimeSchedNotSupported );
809 sched_getparam( 0, & param );
810 if ( param.sched_priority < max_priority ) {
811 param.sched_priority += 1;
812 rc = sched_setscheduler( 0, sched, & param );
813 if ( rc != 0 ) {
814 int error = errno;
815 __kmp_msg(
816 kmp_ms_warning,
817 KMP_MSG( CantChangeMonitorPriority ),
818 KMP_ERR( error ),
819 KMP_MSG( MonitorWillStarve ),
820 __kmp_msg_null
821 );
822 }; // if
823 } else {
824 // We cannot abort here, because number of CPUs may be enough for all the threads,
825 // including the monitor thread, so application could potentially work...
826 __kmp_msg(
827 kmp_ms_warning,
828 KMP_MSG( RunningAtMaxPriority ),
829 KMP_MSG( MonitorWillStarve ),
830 KMP_HNT( RunningAtMaxPriority ),
831 __kmp_msg_null
832 );
833 }; // if
834 }; // if
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000835 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 +0000836 }
837 #endif // KMP_REAL_TIME_FIX
838
839 KMP_MB(); /* Flush all pending memory write invalidates. */
840
841 if ( __kmp_monitor_wakeups == 1 ) {
842 interval.tv_sec = 1;
843 interval.tv_nsec = 0;
844 } else {
845 interval.tv_sec = 0;
846 interval.tv_nsec = (NSEC_PER_SEC / __kmp_monitor_wakeups);
847 }
848
849 KA_TRACE( 10, ("__kmp_launch_monitor: #2 monitor\n" ) );
850
851 if (__kmp_yield_cycle) {
852 __kmp_yielding_on = 0; /* Start out with yielding shut off */
853 yield_count = __kmp_yield_off_count;
854 } else {
855 __kmp_yielding_on = 1; /* Yielding is on permanently */
856 }
857
858 while( ! TCR_4( __kmp_global.g.g_done ) ) {
859 struct timespec now;
860 struct timeval tval;
861
862 /* This thread monitors the state of the system */
863
864 KA_TRACE( 15, ( "__kmp_launch_monitor: update\n" ) );
865
866 status = gettimeofday( &tval, NULL );
867 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
868 TIMEVAL_TO_TIMESPEC( &tval, &now );
869
870 now.tv_sec += interval.tv_sec;
871 now.tv_nsec += interval.tv_nsec;
872
873 if (now.tv_nsec >= NSEC_PER_SEC) {
874 now.tv_sec += 1;
875 now.tv_nsec -= NSEC_PER_SEC;
876 }
877
878 status = pthread_mutex_lock( & __kmp_wait_mx.m_mutex );
879 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie07ea89f2014-09-03 11:10:54 +0000880 // AC: the monitor should not fall asleep if g_done has been set
881 if ( !TCR_4(__kmp_global.g.g_done) ) { // check once more under mutex
882 status = pthread_cond_timedwait( &__kmp_wait_cv.c_cond, &__kmp_wait_mx.m_mutex, &now );
883 if ( status != 0 ) {
884 if ( status != ETIMEDOUT && status != EINTR ) {
885 KMP_SYSFAIL( "pthread_cond_timedwait", status );
886 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000887 };
888 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000889 status = pthread_mutex_unlock( & __kmp_wait_mx.m_mutex );
890 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
891
892 if (__kmp_yield_cycle) {
893 yield_cycles++;
894 if ( (yield_cycles % yield_count) == 0 ) {
895 if (__kmp_yielding_on) {
896 __kmp_yielding_on = 0; /* Turn it off now */
897 yield_count = __kmp_yield_off_count;
898 } else {
899 __kmp_yielding_on = 1; /* Turn it on now */
900 yield_count = __kmp_yield_on_count;
901 }
902 yield_cycles = 0;
903 }
904 } else {
905 __kmp_yielding_on = 1;
906 }
907
908 TCW_4( __kmp_global.g.g_time.dt.t_value,
909 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
910
911 KMP_MB(); /* Flush all pending memory write invalidates. */
912 }
913
914 KA_TRACE( 10, ("__kmp_launch_monitor: #3 cleanup\n" ) );
915
916#ifdef KMP_BLOCK_SIGNALS
917 status = sigfillset( & new_set );
918 KMP_CHECK_SYSFAIL_ERRNO( "sigfillset", status );
919 status = pthread_sigmask( SIG_UNBLOCK, & new_set, NULL );
920 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
921#endif /* KMP_BLOCK_SIGNALS */
922
923 KA_TRACE( 10, ("__kmp_launch_monitor: #4 finished\n" ) );
924
925 if( __kmp_global.g.g_abort != 0 ) {
926 /* now we need to terminate the worker threads */
927 /* the value of t_abort is the signal we caught */
928
929 int gtid;
930
931 KA_TRACE( 10, ("__kmp_launch_monitor: #5 terminate sig=%d\n", __kmp_global.g.g_abort ) );
932
933 /* terminate the OpenMP worker threads */
934 /* TODO this is not valid for sibling threads!!
935 * the uber master might not be 0 anymore.. */
936 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
937 __kmp_terminate_thread( gtid );
938
939 __kmp_cleanup();
940
941 KA_TRACE( 10, ("__kmp_launch_monitor: #6 raise sig=%d\n", __kmp_global.g.g_abort ) );
942
943 if (__kmp_global.g.g_abort > 0)
944 raise( __kmp_global.g.g_abort );
945
946 }
947
948 KA_TRACE( 10, ("__kmp_launch_monitor: #7 exit\n" ) );
949
950 return thr;
951}
952
953void
954__kmp_create_worker( int gtid, kmp_info_t *th, size_t stack_size )
955{
956 pthread_t handle;
957 pthread_attr_t thread_attr;
958 int status;
959
960
961 th->th.th_info.ds.ds_gtid = gtid;
962
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000963#if KMP_STATS_ENABLED
964 // sets up worker thread stats
965 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
966
967 // th->th.th_stats is used to transfer thread specific stats-pointer to __kmp_launch_worker
968 // So when thread is created (goes into __kmp_launch_worker) it will
969 // set it's __thread local pointer to th->th.th_stats
970 th->th.th_stats = __kmp_stats_list.push_back(gtid);
971 if(KMP_UBER_GTID(gtid)) {
972 __kmp_stats_start_time = tsc_tick_count::now();
973 __kmp_stats_thread_ptr = th->th.th_stats;
974 __kmp_stats_init();
975 KMP_START_EXPLICIT_TIMER(OMP_serial);
976 KMP_START_EXPLICIT_TIMER(OMP_start_end);
977 }
978 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
979
980#endif // KMP_STATS_ENABLED
981
Jim Cownie5e8470a2013-09-27 10:38:44 +0000982 if ( KMP_UBER_GTID(gtid) ) {
983 KA_TRACE( 10, ("__kmp_create_worker: uber thread (%d)\n", gtid ) );
984 th -> th.th_info.ds.ds_thread = pthread_self();
985 __kmp_set_stack_info( gtid, th );
986 __kmp_check_stack_overlap( th );
987 return;
988 }; // if
989
990 KA_TRACE( 10, ("__kmp_create_worker: try to create thread (%d)\n", gtid ) );
991
992 KMP_MB(); /* Flush all pending memory write invalidates. */
993
994#ifdef KMP_THREAD_ATTR
995 {
996 status = pthread_attr_init( &thread_attr );
997 if ( status != 0 ) {
998 __kmp_msg(
999 kmp_ms_fatal,
1000 KMP_MSG( CantInitThreadAttrs ),
1001 KMP_ERR( status ),
1002 __kmp_msg_null
1003 );
1004 }; // if
1005 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1006 if ( status != 0 ) {
1007 __kmp_msg(
1008 kmp_ms_fatal,
1009 KMP_MSG( CantSetWorkerState ),
1010 KMP_ERR( status ),
1011 __kmp_msg_null
1012 );
1013 }; // if
1014
1015 /* Set stack size for this thread now. */
1016 stack_size += gtid * __kmp_stkoffset;
1017
1018 KA_TRACE( 10, ( "__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
1019 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
1020 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size ) );
1021
1022# ifdef _POSIX_THREAD_ATTR_STACKSIZE
1023 status = pthread_attr_setstacksize( & thread_attr, stack_size );
1024# ifdef KMP_BACKUP_STKSIZE
1025 if ( status != 0 ) {
1026 if ( ! __kmp_env_stksize ) {
1027 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
1028 __kmp_stksize = KMP_BACKUP_STKSIZE;
1029 KA_TRACE( 10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
1030 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
1031 "bytes\n",
1032 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size )
1033 );
1034 status = pthread_attr_setstacksize( &thread_attr, stack_size );
1035 }; // if
1036 }; // if
1037# endif /* KMP_BACKUP_STKSIZE */
1038 if ( status != 0 ) {
1039 __kmp_msg(
1040 kmp_ms_fatal,
1041 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1042 KMP_ERR( status ),
1043 KMP_HNT( ChangeWorkerStackSize ),
1044 __kmp_msg_null
1045 );
1046 }; // if
1047# endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1048 }
1049#endif /* KMP_THREAD_ATTR */
1050
1051 {
1052 status = pthread_create( & handle, & thread_attr, __kmp_launch_worker, (void *) th );
1053 if ( status != 0 || ! handle ) { // ??? Why do we check handle??
1054#ifdef _POSIX_THREAD_ATTR_STACKSIZE
1055 if ( status == EINVAL ) {
1056 __kmp_msg(
1057 kmp_ms_fatal,
1058 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1059 KMP_ERR( status ),
1060 KMP_HNT( IncreaseWorkerStackSize ),
1061 __kmp_msg_null
1062 );
1063 };
1064 if ( status == ENOMEM ) {
1065 __kmp_msg(
1066 kmp_ms_fatal,
1067 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1068 KMP_ERR( status ),
1069 KMP_HNT( DecreaseWorkerStackSize ),
1070 __kmp_msg_null
1071 );
1072 };
1073#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1074 if ( status == EAGAIN ) {
1075 __kmp_msg(
1076 kmp_ms_fatal,
1077 KMP_MSG( NoResourcesForWorkerThread ),
1078 KMP_ERR( status ),
1079 KMP_HNT( Decrease_NUM_THREADS ),
1080 __kmp_msg_null
1081 );
1082 }; // if
1083 KMP_SYSFAIL( "pthread_create", status );
1084 }; // if
1085
1086 th->th.th_info.ds.ds_thread = handle;
1087 }
1088
1089#ifdef KMP_THREAD_ATTR
1090 {
1091 status = pthread_attr_destroy( & thread_attr );
1092 if ( status ) {
1093 __kmp_msg(
1094 kmp_ms_warning,
1095 KMP_MSG( CantDestroyThreadAttrs ),
1096 KMP_ERR( status ),
1097 __kmp_msg_null
1098 );
1099 }; // if
1100 }
1101#endif /* KMP_THREAD_ATTR */
1102
1103 KMP_MB(); /* Flush all pending memory write invalidates. */
1104
1105 KA_TRACE( 10, ("__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1106
1107} // __kmp_create_worker
1108
1109
1110void
1111__kmp_create_monitor( kmp_info_t *th )
1112{
1113 pthread_t handle;
1114 pthread_attr_t thread_attr;
1115 size_t size;
1116 int status;
1117 int caller_gtid = __kmp_get_gtid();
1118 int auto_adj_size = FALSE;
1119
1120 KA_TRACE( 10, ("__kmp_create_monitor: try to create monitor\n" ) );
1121
1122 KMP_MB(); /* Flush all pending memory write invalidates. */
1123
1124 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1125 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1126 #if KMP_REAL_TIME_FIX
1127 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 +00001128 #else
1129 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001130 #endif // KMP_REAL_TIME_FIX
1131
1132 #ifdef KMP_THREAD_ATTR
1133 if ( __kmp_monitor_stksize == 0 ) {
1134 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1135 auto_adj_size = TRUE;
1136 }
1137 status = pthread_attr_init( &thread_attr );
1138 if ( status != 0 ) {
1139 __kmp_msg(
1140 kmp_ms_fatal,
1141 KMP_MSG( CantInitThreadAttrs ),
1142 KMP_ERR( status ),
1143 __kmp_msg_null
1144 );
1145 }; // if
1146 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1147 if ( status != 0 ) {
1148 __kmp_msg(
1149 kmp_ms_fatal,
1150 KMP_MSG( CantSetMonitorState ),
1151 KMP_ERR( status ),
1152 __kmp_msg_null
1153 );
1154 }; // if
1155
1156 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1157 status = pthread_attr_getstacksize( & thread_attr, & size );
1158 KMP_CHECK_SYSFAIL( "pthread_attr_getstacksize", status );
1159 #else
1160 size = __kmp_sys_min_stksize;
1161 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1162 #endif /* KMP_THREAD_ATTR */
1163
1164 if ( __kmp_monitor_stksize == 0 ) {
1165 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1166 }
1167 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1168 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1169 }
1170
1171 KA_TRACE( 10, ( "__kmp_create_monitor: default stacksize = %lu bytes,"
1172 "requested stacksize = %lu bytes\n",
1173 size, __kmp_monitor_stksize ) );
1174
1175 retry:
1176
1177 /* Set stack size for this thread now. */
1178
1179 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1180 KA_TRACE( 10, ( "__kmp_create_monitor: setting stacksize = %lu bytes,",
1181 __kmp_monitor_stksize ) );
1182 status = pthread_attr_setstacksize( & thread_attr, __kmp_monitor_stksize );
1183 if ( status != 0 ) {
1184 if ( auto_adj_size ) {
1185 __kmp_monitor_stksize *= 2;
1186 goto retry;
1187 }
1188 __kmp_msg(
1189 kmp_ms_warning, // should this be fatal? BB
1190 KMP_MSG( CantSetMonitorStackSize, (long int) __kmp_monitor_stksize ),
1191 KMP_ERR( status ),
1192 KMP_HNT( ChangeMonitorStackSize ),
1193 __kmp_msg_null
1194 );
1195 }; // if
1196 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1197
Jim Cownie5e8470a2013-09-27 10:38:44 +00001198 status = pthread_create( &handle, & thread_attr, __kmp_launch_monitor, (void *) th );
1199
1200 if ( status != 0 ) {
1201 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1202 if ( status == EINVAL ) {
1203 if ( auto_adj_size && ( __kmp_monitor_stksize < (size_t)0x40000000 ) ) {
1204 __kmp_monitor_stksize *= 2;
1205 goto retry;
1206 }
1207 __kmp_msg(
1208 kmp_ms_fatal,
1209 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1210 KMP_ERR( status ),
1211 KMP_HNT( IncreaseMonitorStackSize ),
1212 __kmp_msg_null
1213 );
1214 }; // if
1215 if ( status == ENOMEM ) {
1216 __kmp_msg(
1217 kmp_ms_fatal,
1218 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1219 KMP_ERR( status ),
1220 KMP_HNT( DecreaseMonitorStackSize ),
1221 __kmp_msg_null
1222 );
1223 }; // if
1224 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1225 if ( status == EAGAIN ) {
1226 __kmp_msg(
1227 kmp_ms_fatal,
1228 KMP_MSG( NoResourcesForMonitorThread ),
1229 KMP_ERR( status ),
1230 KMP_HNT( DecreaseNumberOfThreadsInUse ),
1231 __kmp_msg_null
1232 );
1233 }; // if
1234 KMP_SYSFAIL( "pthread_create", status );
1235 }; // if
1236
1237 th->th.th_info.ds.ds_thread = handle;
1238
1239 #if KMP_REAL_TIME_FIX
1240 // Wait for the monitor thread is really started and set its *priority*.
1241 KMP_DEBUG_ASSERT( sizeof( kmp_uint32 ) == sizeof( __kmp_global.g.g_time.dt.t_value ) );
1242 __kmp_wait_yield_4(
1243 (kmp_uint32 volatile *) & __kmp_global.g.g_time.dt.t_value, -1, & __kmp_neq_4, NULL
1244 );
1245 #endif // KMP_REAL_TIME_FIX
1246
1247 #ifdef KMP_THREAD_ATTR
1248 status = pthread_attr_destroy( & thread_attr );
1249 if ( status != 0 ) {
1250 __kmp_msg( //
1251 kmp_ms_warning,
1252 KMP_MSG( CantDestroyThreadAttrs ),
1253 KMP_ERR( status ),
1254 __kmp_msg_null
1255 );
1256 }; // if
1257 #endif
1258
1259 KMP_MB(); /* Flush all pending memory write invalidates. */
1260
1261 KA_TRACE( 10, ( "__kmp_create_monitor: monitor created %#.8lx\n", th->th.th_info.ds.ds_thread ) );
1262
1263} // __kmp_create_monitor
1264
1265void
1266__kmp_exit_thread(
1267 int exit_status
1268) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001269 pthread_exit( (void *)(intptr_t) exit_status );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001270} // __kmp_exit_thread
1271
Jim Cownie07ea89f2014-09-03 11:10:54 +00001272void __kmp_resume_monitor();
1273
Jim Cownie5e8470a2013-09-27 10:38:44 +00001274void
1275__kmp_reap_monitor( kmp_info_t *th )
1276{
1277 int status, i;
1278 void *exit_val;
1279
1280 KA_TRACE( 10, ("__kmp_reap_monitor: try to reap monitor thread with handle %#.8lx\n",
1281 th->th.th_info.ds.ds_thread ) );
1282
1283 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1284 // If both tid and gtid are 0, it means the monitor did not ever start.
1285 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1286 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1287 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
1288 return;
1289 }; // if
1290
1291 KMP_MB(); /* Flush all pending memory write invalidates. */
1292
1293
1294 /* First, check to see whether the monitor thread exists. This could prevent a hang,
1295 but if the monitor dies after the pthread_kill call and before the pthread_join
1296 call, it will still hang. */
1297
1298 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1299 if (status == ESRCH) {
1300
1301 KA_TRACE( 10, ("__kmp_reap_monitor: monitor does not exist, returning\n") );
1302
1303 } else
1304 {
Jim Cownie07ea89f2014-09-03 11:10:54 +00001305 __kmp_resume_monitor(); // Wake up the monitor thread
Jim Cownie5e8470a2013-09-27 10:38:44 +00001306 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1307 if (exit_val != th) {
1308 __kmp_msg(
1309 kmp_ms_fatal,
1310 KMP_MSG( ReapMonitorError ),
1311 KMP_ERR( status ),
1312 __kmp_msg_null
1313 );
1314 }
1315 }
1316
1317 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1318 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1319
1320 KA_TRACE( 10, ("__kmp_reap_monitor: done reaping monitor thread with handle %#.8lx\n",
1321 th->th.th_info.ds.ds_thread ) );
1322
1323 KMP_MB(); /* Flush all pending memory write invalidates. */
1324
1325}
1326
1327void
1328__kmp_reap_worker( kmp_info_t *th )
1329{
1330 int status;
1331 void *exit_val;
1332
1333 KMP_MB(); /* Flush all pending memory write invalidates. */
1334
1335 KA_TRACE( 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid ) );
1336
1337 /* First, check to see whether the worker thread exists. This could prevent a hang,
1338 but if the worker dies after the pthread_kill call and before the pthread_join
1339 call, it will still hang. */
1340
1341 {
1342 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1343 if (status == ESRCH) {
1344 KA_TRACE( 10, ("__kmp_reap_worker: worker T#%d does not exist, returning\n",
1345 th->th.th_info.ds.ds_gtid ) );
1346 }
1347 else {
1348 KA_TRACE( 10, ("__kmp_reap_worker: try to join with worker T#%d\n",
1349 th->th.th_info.ds.ds_gtid ) );
1350
1351 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1352#ifdef KMP_DEBUG
1353 /* Don't expose these to the user until we understand when they trigger */
1354 if ( status != 0 ) {
1355 __kmp_msg(
1356 kmp_ms_fatal,
1357 KMP_MSG( ReapWorkerError ),
1358 KMP_ERR( status ),
1359 __kmp_msg_null
1360 );
1361 }
1362 if ( exit_val != th ) {
1363 KA_TRACE( 10, ( "__kmp_reap_worker: worker T#%d did not reap properly, "
1364 "exit_val = %p\n",
1365 th->th.th_info.ds.ds_gtid, exit_val ) );
1366 }
1367#endif /* KMP_DEBUG */
1368 }
1369 }
1370
1371 KA_TRACE( 10, ("__kmp_reap_worker: done reaping T#%d\n", th->th.th_info.ds.ds_gtid ) );
1372
1373 KMP_MB(); /* Flush all pending memory write invalidates. */
1374}
1375
1376
1377/* ------------------------------------------------------------------------ */
1378/* ------------------------------------------------------------------------ */
1379
1380#if KMP_HANDLE_SIGNALS
1381
1382
1383static void
1384__kmp_null_handler( int signo )
1385{
1386 // Do nothing, for doing SIG_IGN-type actions.
1387} // __kmp_null_handler
1388
1389
1390static void
1391__kmp_team_handler( int signo )
1392{
1393 if ( __kmp_global.g.g_abort == 0 ) {
1394 /* Stage 1 signal handler, let's shut down all of the threads */
1395 #ifdef KMP_DEBUG
1396 __kmp_debug_printf( "__kmp_team_handler: caught signal = %d\n", signo );
1397 #endif
1398 switch ( signo ) {
1399 case SIGHUP :
1400 case SIGINT :
1401 case SIGQUIT :
1402 case SIGILL :
1403 case SIGABRT :
1404 case SIGFPE :
1405 case SIGBUS :
1406 case SIGSEGV :
1407 #ifdef SIGSYS
1408 case SIGSYS :
1409 #endif
1410 case SIGTERM :
1411 if ( __kmp_debug_buf ) {
1412 __kmp_dump_debug_buffer( );
1413 }; // if
1414 KMP_MB(); // Flush all pending memory write invalidates.
1415 TCW_4( __kmp_global.g.g_abort, signo );
1416 KMP_MB(); // Flush all pending memory write invalidates.
1417 TCW_4( __kmp_global.g.g_done, TRUE );
1418 KMP_MB(); // Flush all pending memory write invalidates.
1419 break;
1420 default:
1421 #ifdef KMP_DEBUG
1422 __kmp_debug_printf( "__kmp_team_handler: unknown signal type" );
1423 #endif
1424 break;
1425 }; // switch
1426 }; // if
1427} // __kmp_team_handler
1428
1429
1430static
1431void __kmp_sigaction( int signum, const struct sigaction * act, struct sigaction * oldact ) {
1432 int rc = sigaction( signum, act, oldact );
1433 KMP_CHECK_SYSFAIL_ERRNO( "sigaction", rc );
1434}
1435
1436
1437static void
1438__kmp_install_one_handler( int sig, sig_func_t handler_func, int parallel_init )
1439{
1440 KMP_MB(); // Flush all pending memory write invalidates.
1441 KB_TRACE( 60, ( "__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init ) );
1442 if ( parallel_init ) {
1443 struct sigaction new_action;
1444 struct sigaction old_action;
1445 new_action.sa_handler = handler_func;
1446 new_action.sa_flags = 0;
1447 sigfillset( & new_action.sa_mask );
1448 __kmp_sigaction( sig, & new_action, & old_action );
1449 if ( old_action.sa_handler == __kmp_sighldrs[ sig ].sa_handler ) {
1450 sigaddset( & __kmp_sigset, sig );
1451 } else {
1452 // Restore/keep user's handler if one previously installed.
1453 __kmp_sigaction( sig, & old_action, NULL );
1454 }; // if
1455 } else {
1456 // Save initial/system signal handlers to see if user handlers installed.
1457 __kmp_sigaction( sig, NULL, & __kmp_sighldrs[ sig ] );
1458 }; // if
1459 KMP_MB(); // Flush all pending memory write invalidates.
1460} // __kmp_install_one_handler
1461
1462
1463static void
1464__kmp_remove_one_handler( int sig )
1465{
1466 KB_TRACE( 60, ( "__kmp_remove_one_handler( %d )\n", sig ) );
1467 if ( sigismember( & __kmp_sigset, sig ) ) {
1468 struct sigaction old;
1469 KMP_MB(); // Flush all pending memory write invalidates.
1470 __kmp_sigaction( sig, & __kmp_sighldrs[ sig ], & old );
1471 if ( ( old.sa_handler != __kmp_team_handler ) && ( old.sa_handler != __kmp_null_handler ) ) {
1472 // Restore the users signal handler.
1473 KB_TRACE( 10, ( "__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1474 __kmp_sigaction( sig, & old, NULL );
1475 }; // if
1476 sigdelset( & __kmp_sigset, sig );
1477 KMP_MB(); // Flush all pending memory write invalidates.
1478 }; // if
1479} // __kmp_remove_one_handler
1480
1481
1482void
1483__kmp_install_signals( int parallel_init )
1484{
1485 KB_TRACE( 10, ( "__kmp_install_signals( %d )\n", parallel_init ) );
1486 if ( __kmp_handle_signals || ! parallel_init ) {
1487 // If ! parallel_init, we do not install handlers, just save original handlers.
1488 // Let us do it even __handle_signals is 0.
1489 sigemptyset( & __kmp_sigset );
1490 __kmp_install_one_handler( SIGHUP, __kmp_team_handler, parallel_init );
1491 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1492 __kmp_install_one_handler( SIGQUIT, __kmp_team_handler, parallel_init );
1493 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1494 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1495 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1496 __kmp_install_one_handler( SIGBUS, __kmp_team_handler, parallel_init );
1497 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1498 #ifdef SIGSYS
1499 __kmp_install_one_handler( SIGSYS, __kmp_team_handler, parallel_init );
1500 #endif // SIGSYS
1501 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1502 #ifdef SIGPIPE
1503 __kmp_install_one_handler( SIGPIPE, __kmp_team_handler, parallel_init );
1504 #endif // SIGPIPE
1505 }; // if
1506} // __kmp_install_signals
1507
1508
1509void
1510__kmp_remove_signals( void )
1511{
1512 int sig;
1513 KB_TRACE( 10, ( "__kmp_remove_signals()\n" ) );
1514 for ( sig = 1; sig < NSIG; ++ sig ) {
1515 __kmp_remove_one_handler( sig );
1516 }; // for sig
1517} // __kmp_remove_signals
1518
1519
1520#endif // KMP_HANDLE_SIGNALS
1521
1522/* ------------------------------------------------------------------------ */
1523/* ------------------------------------------------------------------------ */
1524
1525void
1526__kmp_enable( int new_state )
1527{
1528 #ifdef KMP_CANCEL_THREADS
1529 int status, old_state;
1530 status = pthread_setcancelstate( new_state, & old_state );
1531 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1532 KMP_DEBUG_ASSERT( old_state == PTHREAD_CANCEL_DISABLE );
1533 #endif
1534}
1535
1536void
1537__kmp_disable( int * old_state )
1538{
1539 #ifdef KMP_CANCEL_THREADS
1540 int status;
1541 status = pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, old_state );
1542 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1543 #endif
1544}
1545
1546/* ------------------------------------------------------------------------ */
1547/* ------------------------------------------------------------------------ */
1548
1549static void
1550__kmp_atfork_prepare (void)
1551{
1552 /* nothing to do */
1553}
1554
1555static void
1556__kmp_atfork_parent (void)
1557{
1558 /* nothing to do */
1559}
1560
1561/*
1562 Reset the library so execution in the child starts "all over again" with
1563 clean data structures in initial states. Don't worry about freeing memory
1564 allocated by parent, just abandon it to be safe.
1565*/
1566static void
1567__kmp_atfork_child (void)
1568{
1569 /* TODO make sure this is done right for nested/sibling */
1570 // ATT: Memory leaks are here? TODO: Check it and fix.
1571 /* KMP_ASSERT( 0 ); */
1572
1573 ++__kmp_fork_count;
1574
1575 __kmp_init_runtime = FALSE;
1576 __kmp_init_monitor = 0;
1577 __kmp_init_parallel = FALSE;
1578 __kmp_init_middle = FALSE;
1579 __kmp_init_serial = FALSE;
1580 TCW_4(__kmp_init_gtid, FALSE);
1581 __kmp_init_common = FALSE;
1582
1583 TCW_4(__kmp_init_user_locks, FALSE);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001584 __kmp_user_lock_table.used = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001585 __kmp_user_lock_table.allocated = 0;
1586 __kmp_user_lock_table.table = NULL;
1587 __kmp_lock_blocks = NULL;
1588
1589 __kmp_all_nth = 0;
1590 TCW_4(__kmp_nth, 0);
1591
1592 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate here
1593 so threadprivate doesn't use stale data */
1594 KA_TRACE( 10, ( "__kmp_atfork_child: checking cache address list %p\n",
1595 __kmp_threadpriv_cache_list ) );
1596
1597 while ( __kmp_threadpriv_cache_list != NULL ) {
1598
1599 if ( *__kmp_threadpriv_cache_list -> addr != NULL ) {
1600 KC_TRACE( 50, ( "__kmp_atfork_child: zeroing cache at address %p\n",
1601 &(*__kmp_threadpriv_cache_list -> addr) ) );
1602
1603 *__kmp_threadpriv_cache_list -> addr = NULL;
1604 }
1605 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list -> next;
1606 }
1607
1608 __kmp_init_runtime = FALSE;
1609
1610 /* reset statically initialized locks */
1611 __kmp_init_bootstrap_lock( &__kmp_initz_lock );
1612 __kmp_init_bootstrap_lock( &__kmp_stdio_lock );
1613 __kmp_init_bootstrap_lock( &__kmp_console_lock );
1614
1615 /* This is necessary to make sure no stale data is left around */
1616 /* AC: customers complain that we use unsafe routines in the atfork
1617 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1618 in dynamic_link when check the presence of shared tbbmalloc library.
1619 Suggestion is to make the library initialization lazier, similar
1620 to what done for __kmpc_begin(). */
1621 // TODO: synchronize all static initializations with regular library
1622 // startup; look at kmp_global.c and etc.
1623 //__kmp_internal_begin ();
1624
1625}
1626
1627void
1628__kmp_register_atfork(void) {
1629 if ( __kmp_need_register_atfork ) {
1630 int status = pthread_atfork( __kmp_atfork_prepare, __kmp_atfork_parent, __kmp_atfork_child );
1631 KMP_CHECK_SYSFAIL( "pthread_atfork", status );
1632 __kmp_need_register_atfork = FALSE;
1633 }
1634}
1635
1636void
1637__kmp_suspend_initialize( void )
1638{
1639 int status;
1640 status = pthread_mutexattr_init( &__kmp_suspend_mutex_attr );
1641 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
1642 status = pthread_condattr_init( &__kmp_suspend_cond_attr );
1643 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
1644}
1645
1646static void
1647__kmp_suspend_initialize_thread( kmp_info_t *th )
1648{
1649 if ( th->th.th_suspend_init_count <= __kmp_fork_count ) {
1650 /* this means we haven't initialized the suspension pthread objects for this thread
1651 in this instance of the process */
1652 int status;
1653 status = pthread_cond_init( &th->th.th_suspend_cv.c_cond, &__kmp_suspend_cond_attr );
1654 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
1655 status = pthread_mutex_init( &th->th.th_suspend_mx.m_mutex, & __kmp_suspend_mutex_attr );
1656 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
1657 *(volatile int*)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1658 };
1659}
1660
1661void
1662__kmp_suspend_uninitialize_thread( kmp_info_t *th )
1663{
1664 if(th->th.th_suspend_init_count > __kmp_fork_count) {
1665 /* this means we have initialize the suspension pthread objects for this thread
1666 in this instance of the process */
1667 int status;
1668
1669 status = pthread_cond_destroy( &th->th.th_suspend_cv.c_cond );
1670 if ( status != 0 && status != EBUSY ) {
1671 KMP_SYSFAIL( "pthread_cond_destroy", status );
1672 };
1673 status = pthread_mutex_destroy( &th->th.th_suspend_mx.m_mutex );
1674 if ( status != 0 && status != EBUSY ) {
1675 KMP_SYSFAIL( "pthread_mutex_destroy", status );
1676 };
1677 --th->th.th_suspend_init_count;
1678 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1679 }
1680}
1681
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001682/* This routine puts the calling thread to sleep after setting the
1683 * sleep bit for the indicated flag variable to true.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001684 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001685template <class C>
1686static inline void __kmp_suspend_template( int th_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001687{
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001688 KMP_TIME_BLOCK(USER_suspend);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001689 kmp_info_t *th = __kmp_threads[th_gtid];
1690 int status;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001691 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001692
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001693 KF_TRACE( 30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001694
1695 __kmp_suspend_initialize_thread( th );
1696
1697 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1698 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1699
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001700 KF_TRACE( 10, ( "__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1701 th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001702
1703 /* TODO: shouldn't this use release semantics to ensure that __kmp_suspend_initialize_thread
1704 gets called first?
1705 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001706 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001707
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001708 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%d\n",
1709 th_gtid, flag->get(), *(flag->get()) ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001710
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001711 if ( flag->done_check_val(old_spin) ) {
1712 old_spin = flag->unset_sleeping();
1713 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d false alarm, reset sleep bit for spin(%p)\n",
1714 th_gtid, flag->get()) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001715 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001716 /* Encapsulate in a loop as the documentation states that this may
1717 * "with low probability" return when the condition variable has
1718 * not been signaled or broadcast
1719 */
1720 int deactivated = FALSE;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001721 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1722 while ( flag->is_sleeping() ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001723#ifdef DEBUG_SUSPEND
1724 char buffer[128];
1725 __kmp_suspend_count++;
1726 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001727 __kmp_printf( "__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001728#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001729 // Mark the thread as no longer active (only in the first iteration of the loop).
Jim Cownie5e8470a2013-09-27 10:38:44 +00001730 if ( ! deactivated ) {
1731 th->th.th_active = FALSE;
1732 if ( th->th.th_active_in_pool ) {
1733 th->th.th_active_in_pool = FALSE;
1734 KMP_TEST_THEN_DEC32(
1735 (kmp_int32 *) &__kmp_thread_pool_active_nth );
1736 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
1737 }
1738 deactivated = TRUE;
1739
1740
1741 }
1742
1743#if USE_SUSPEND_TIMEOUT
1744 struct timespec now;
1745 struct timeval tval;
1746 int msecs;
1747
1748 status = gettimeofday( &tval, NULL );
1749 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1750 TIMEVAL_TO_TIMESPEC( &tval, &now );
1751
1752 msecs = (4*__kmp_dflt_blocktime) + 200;
1753 now.tv_sec += msecs / 1000;
1754 now.tv_nsec += (msecs % 1000)*1000;
1755
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001756 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_timedwait\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001757 th_gtid ) );
1758 status = pthread_cond_timedwait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex, & now );
1759#else
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001760 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_wait\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001761 th_gtid ) );
1762
1763 status = pthread_cond_wait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex );
1764#endif
1765
1766 if ( (status != 0) && (status != EINTR) && (status != ETIMEDOUT) ) {
1767 KMP_SYSFAIL( "pthread_cond_wait", status );
1768 }
1769#ifdef KMP_DEBUG
1770 if (status == ETIMEDOUT) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001771 if ( flag->is_sleeping() ) {
1772 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001773 } else {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001774 KF_TRACE( 2, ( "__kmp_suspend_template: T#%d timeout wakeup, sleep bit not set!\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001775 th_gtid ) );
1776 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001777 } else if ( flag->is_sleeping() ) {
1778 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001779 }
1780#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001781 } // while
1782
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001783 // Mark the thread as active again (if it was previous marked as inactive)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001784 if ( deactivated ) {
1785 th->th.th_active = TRUE;
1786 if ( TCR_4(th->th.th_in_pool) ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001787 KMP_TEST_THEN_INC32( (kmp_int32 *) &__kmp_thread_pool_active_nth );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001788 th->th.th_active_in_pool = TRUE;
1789 }
1790 }
1791 }
1792
1793#ifdef DEBUG_SUSPEND
1794 {
1795 char buffer[128];
1796 __kmp_print_cond( buffer, &th->th.th_suspend_cv);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001797 __kmp_printf( "__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001798 }
1799#endif
1800
1801
1802 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1803 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1804
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001805 KF_TRACE( 30, ("__kmp_suspend_template: T#%d exit\n", th_gtid ) );
1806}
1807
1808void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
1809 __kmp_suspend_template(th_gtid, flag);
1810}
1811void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
1812 __kmp_suspend_template(th_gtid, flag);
1813}
1814void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
1815 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001816}
1817
1818
1819/* This routine signals the thread specified by target_gtid to wake up
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001820 * after setting the sleep bit indicated by the flag argument to FALSE.
1821 * The target thread must already have called __kmp_suspend_template()
Jim Cownie5e8470a2013-09-27 10:38:44 +00001822 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001823template <class C>
1824static inline void __kmp_resume_template( int target_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001825{
1826 kmp_info_t *th = __kmp_threads[target_gtid];
1827 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001828
1829#ifdef KMP_DEBUG
1830 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1831#endif
1832
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001833 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 +00001834 KMP_DEBUG_ASSERT( gtid != target_gtid );
1835
1836 __kmp_suspend_initialize_thread( th );
1837
1838 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1839 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001840
1841 if (!flag) {
1842 flag = (C *)th->th.th_sleep_loc;
1843 }
1844
1845 if (!flag) {
1846 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p)\n",
1847 gtid, target_gtid, NULL ) );
1848 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1849 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1850 return;
1851 }
1852 else {
1853 typename C::flag_t old_spin = flag->unset_sleeping();
1854 if ( ! flag->is_sleeping_val(old_spin) ) {
1855 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p): "
1856 "%u => %u\n",
1857 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001858
1859 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1860 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1861 return;
1862 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001863 KF_TRACE( 5, ( "__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p): "
1864 "%u => %u\n",
1865 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001866 }
1867 TCW_PTR(th->th.th_sleep_loc, NULL);
1868
Jim Cownie5e8470a2013-09-27 10:38:44 +00001869
1870#ifdef DEBUG_SUSPEND
1871 {
1872 char buffer[128];
1873 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001874 __kmp_printf( "__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, target_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001875 }
1876#endif
1877
1878
1879 status = pthread_cond_signal( &th->th.th_suspend_cv.c_cond );
1880 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1881 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1882 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001883 KF_TRACE( 30, ( "__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001884 gtid, target_gtid ) );
1885}
1886
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001887void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
1888 __kmp_resume_template(target_gtid, flag);
1889}
1890void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
1891 __kmp_resume_template(target_gtid, flag);
1892}
1893void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
1894 __kmp_resume_template(target_gtid, flag);
1895}
1896
Jim Cownie07ea89f2014-09-03 11:10:54 +00001897void
1898__kmp_resume_monitor()
1899{
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001900 KMP_TIME_BLOCK(USER_resume);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001901 int status;
1902#ifdef KMP_DEBUG
1903 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1904 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n",
1905 gtid, KMP_GTID_MONITOR ) );
1906 KMP_DEBUG_ASSERT( gtid != KMP_GTID_MONITOR );
1907#endif
1908 status = pthread_mutex_lock( &__kmp_wait_mx.m_mutex );
1909 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1910#ifdef DEBUG_SUSPEND
1911 {
1912 char buffer[128];
1913 __kmp_print_cond( buffer, &__kmp_wait_cv.c_cond );
1914 __kmp_printf( "__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid, KMP_GTID_MONITOR, buffer );
1915 }
1916#endif
1917 status = pthread_cond_signal( &__kmp_wait_cv.c_cond );
1918 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1919 status = pthread_mutex_unlock( &__kmp_wait_mx.m_mutex );
1920 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1921 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d exiting after signaling wake up for T#%d\n",
1922 gtid, KMP_GTID_MONITOR ) );
1923}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001924
1925/* ------------------------------------------------------------------------ */
1926/* ------------------------------------------------------------------------ */
1927
1928void
1929__kmp_yield( int cond )
1930{
1931 if (cond && __kmp_yielding_on) {
1932 sched_yield();
1933 }
1934}
1935
1936/* ------------------------------------------------------------------------ */
1937/* ------------------------------------------------------------------------ */
1938
1939void
1940__kmp_gtid_set_specific( int gtid )
1941{
1942 int status;
1943 KMP_ASSERT( __kmp_init_runtime );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001944 status = pthread_setspecific( __kmp_gtid_threadprivate_key, (void*)(intptr_t)(gtid+1) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001945 KMP_CHECK_SYSFAIL( "pthread_setspecific", status );
1946}
1947
1948int
1949__kmp_gtid_get_specific()
1950{
1951 int gtid;
1952 if ( !__kmp_init_runtime ) {
1953 KA_TRACE( 50, ("__kmp_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
1954 return KMP_GTID_SHUTDOWN;
1955 }
1956 gtid = (int)(size_t)pthread_getspecific( __kmp_gtid_threadprivate_key );
1957 if ( gtid == 0 ) {
1958 gtid = KMP_GTID_DNE;
1959 }
1960 else {
1961 gtid--;
1962 }
1963 KA_TRACE( 50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1964 __kmp_gtid_threadprivate_key, gtid ));
1965 return gtid;
1966}
1967
1968/* ------------------------------------------------------------------------ */
1969/* ------------------------------------------------------------------------ */
1970
1971double
1972__kmp_read_cpu_time( void )
1973{
1974 /*clock_t t;*/
1975 struct tms buffer;
1976
1977 /*t =*/ times( & buffer );
1978
1979 return (buffer.tms_utime + buffer.tms_cutime) / (double) CLOCKS_PER_SEC;
1980}
1981
1982int
1983__kmp_read_system_info( struct kmp_sys_info *info )
1984{
1985 int status;
1986 struct rusage r_usage;
1987
1988 memset( info, 0, sizeof( *info ) );
1989
1990 status = getrusage( RUSAGE_SELF, &r_usage);
1991 KMP_CHECK_SYSFAIL_ERRNO( "getrusage", status );
1992
1993 info->maxrss = r_usage.ru_maxrss; /* the maximum resident set size utilized (in kilobytes) */
1994 info->minflt = r_usage.ru_minflt; /* the number of page faults serviced without any I/O */
1995 info->majflt = r_usage.ru_majflt; /* the number of page faults serviced that required I/O */
1996 info->nswap = r_usage.ru_nswap; /* the number of times a process was "swapped" out of memory */
1997 info->inblock = r_usage.ru_inblock; /* the number of times the file system had to perform input */
1998 info->oublock = r_usage.ru_oublock; /* the number of times the file system had to perform output */
1999 info->nvcsw = r_usage.ru_nvcsw; /* the number of times a context switch was voluntarily */
2000 info->nivcsw = r_usage.ru_nivcsw; /* the number of times a context switch was forced */
2001
2002 return (status != 0);
2003}
2004
2005/* ------------------------------------------------------------------------ */
2006/* ------------------------------------------------------------------------ */
2007
2008
2009void
2010__kmp_read_system_time( double *delta )
2011{
2012 double t_ns;
2013 struct timeval tval;
2014 struct timespec stop;
2015 int status;
2016
2017 status = gettimeofday( &tval, NULL );
2018 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
2019 TIMEVAL_TO_TIMESPEC( &tval, &stop );
2020 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
2021 *delta = (t_ns * 1e-9);
2022}
2023
2024void
2025__kmp_clear_system_time( void )
2026{
2027 struct timeval tval;
2028 int status;
2029 status = gettimeofday( &tval, NULL );
2030 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
2031 TIMEVAL_TO_TIMESPEC( &tval, &__kmp_sys_timer_data.start );
2032}
2033
2034/* ------------------------------------------------------------------------ */
2035/* ------------------------------------------------------------------------ */
2036
2037#ifdef BUILD_TV
2038
2039void
2040__kmp_tv_threadprivate_store( kmp_info_t *th, void *global_addr, void *thread_addr )
2041{
2042 struct tv_data *p;
2043
2044 p = (struct tv_data *) __kmp_allocate( sizeof( *p ) );
2045
2046 p->u.tp.global_addr = global_addr;
2047 p->u.tp.thread_addr = thread_addr;
2048
2049 p->type = (void *) 1;
2050
2051 p->next = th->th.th_local.tv_data;
2052 th->th.th_local.tv_data = p;
2053
2054 if ( p->next == 0 ) {
2055 int rc = pthread_setspecific( __kmp_tv_key, p );
2056 KMP_CHECK_SYSFAIL( "pthread_setspecific", rc );
2057 }
2058}
2059
2060#endif /* BUILD_TV */
2061
2062/* ------------------------------------------------------------------------ */
2063/* ------------------------------------------------------------------------ */
2064
2065static int
2066__kmp_get_xproc( void ) {
2067
2068 int r = 0;
2069
2070 #if KMP_OS_LINUX
2071
2072 r = sysconf( _SC_NPROCESSORS_ONLN );
2073
2074 #elif KMP_OS_DARWIN
2075
2076 // Bug C77011 High "OpenMP Threads and number of active cores".
2077
2078 // Find the number of available CPUs.
2079 kern_return_t rc;
2080 host_basic_info_data_t info;
2081 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
2082 rc = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) & info, & num );
2083 if ( rc == 0 && num == HOST_BASIC_INFO_COUNT ) {
2084 // Cannot use KA_TRACE() here because this code works before trace support is
2085 // initialized.
2086 r = info.avail_cpus;
2087 } else {
2088 KMP_WARNING( CantGetNumAvailCPU );
2089 KMP_INFORM( AssumedNumCPU );
2090 }; // if
2091
Alp Toker763b9392014-02-28 09:42:41 +00002092 #elif KMP_OS_FREEBSD
2093
2094 int mib[] = { CTL_HW, HW_NCPU };
2095 size_t len = sizeof( r );
2096 if ( sysctl( mib, 2, &r, &len, NULL, 0 ) < 0 ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002097 r = 0;
2098 KMP_WARNING( CantGetNumAvailCPU );
2099 KMP_INFORM( AssumedNumCPU );
Alp Toker763b9392014-02-28 09:42:41 +00002100 }
2101
Jim Cownie5e8470a2013-09-27 10:38:44 +00002102 #else
2103
2104 #error "Unknown or unsupported OS."
2105
2106 #endif
2107
2108 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
2109
2110} // __kmp_get_xproc
2111
Jim Cownie181b4bb2013-12-23 17:28:57 +00002112int
2113__kmp_read_from_file( char const *path, char const *format, ... )
2114{
2115 int result;
2116 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002117
Jim Cownie181b4bb2013-12-23 17:28:57 +00002118 va_start(args, format);
2119 FILE *f = fopen(path, "rb");
2120 if ( f == NULL )
2121 return 0;
2122 result = vfscanf(f, format, args);
2123 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002124
Jim Cownie5e8470a2013-09-27 10:38:44 +00002125 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00002126}
Jim Cownie5e8470a2013-09-27 10:38:44 +00002127
2128void
2129__kmp_runtime_initialize( void )
2130{
2131 int status;
2132 pthread_mutexattr_t mutex_attr;
2133 pthread_condattr_t cond_attr;
2134
2135 if ( __kmp_init_runtime ) {
2136 return;
2137 }; // if
2138
2139 #if ( KMP_ARCH_X86 || KMP_ARCH_X86_64 )
2140 if ( ! __kmp_cpuinfo.initialized ) {
2141 __kmp_query_cpuid( &__kmp_cpuinfo );
2142 }; // if
2143 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
2144
Jim Cownie5e8470a2013-09-27 10:38:44 +00002145 __kmp_xproc = __kmp_get_xproc();
2146
2147 if ( sysconf( _SC_THREADS ) ) {
2148
2149 /* Query the maximum number of threads */
2150 __kmp_sys_max_nth = sysconf( _SC_THREAD_THREADS_MAX );
2151 if ( __kmp_sys_max_nth == -1 ) {
2152 /* Unlimited threads for NPTL */
2153 __kmp_sys_max_nth = INT_MAX;
2154 }
2155 else if ( __kmp_sys_max_nth <= 1 ) {
2156 /* Can't tell, just use PTHREAD_THREADS_MAX */
2157 __kmp_sys_max_nth = KMP_MAX_NTH;
2158 }
2159
2160 /* Query the minimum stack size */
2161 __kmp_sys_min_stksize = sysconf( _SC_THREAD_STACK_MIN );
2162 if ( __kmp_sys_min_stksize <= 1 ) {
2163 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
2164 }
2165 }
2166
2167 /* Set up minimum number of threads to switch to TLS gtid */
2168 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
2169
2170
2171 #ifdef BUILD_TV
2172 {
2173 int rc = pthread_key_create( & __kmp_tv_key, 0 );
2174 KMP_CHECK_SYSFAIL( "pthread_key_create", rc );
2175 }
2176 #endif
2177
2178 status = pthread_key_create( &__kmp_gtid_threadprivate_key, __kmp_internal_end_dest );
2179 KMP_CHECK_SYSFAIL( "pthread_key_create", status );
2180 status = pthread_mutexattr_init( & mutex_attr );
2181 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
2182 status = pthread_mutex_init( & __kmp_wait_mx.m_mutex, & mutex_attr );
2183 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
2184 status = pthread_condattr_init( & cond_attr );
2185 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
2186 status = pthread_cond_init( & __kmp_wait_cv.c_cond, & cond_attr );
2187 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
2188#if USE_ITT_BUILD
2189 __kmp_itt_initialize();
2190#endif /* USE_ITT_BUILD */
2191
2192 __kmp_init_runtime = TRUE;
2193}
2194
2195void
2196__kmp_runtime_destroy( void )
2197{
2198 int status;
2199
2200 if ( ! __kmp_init_runtime ) {
2201 return; // Nothing to do.
2202 };
2203
2204#if USE_ITT_BUILD
2205 __kmp_itt_destroy();
2206#endif /* USE_ITT_BUILD */
2207
2208 status = pthread_key_delete( __kmp_gtid_threadprivate_key );
2209 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2210 #ifdef BUILD_TV
2211 status = pthread_key_delete( __kmp_tv_key );
2212 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2213 #endif
2214
2215 status = pthread_mutex_destroy( & __kmp_wait_mx.m_mutex );
2216 if ( status != 0 && status != EBUSY ) {
2217 KMP_SYSFAIL( "pthread_mutex_destroy", status );
2218 }
2219 status = pthread_cond_destroy( & __kmp_wait_cv.c_cond );
2220 if ( status != 0 && status != EBUSY ) {
2221 KMP_SYSFAIL( "pthread_cond_destroy", status );
2222 }
Alp Toker763b9392014-02-28 09:42:41 +00002223 #if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00002224 __kmp_affinity_uninitialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00002225 #endif
2226
2227 __kmp_init_runtime = FALSE;
2228}
2229
2230
2231/* Put the thread to sleep for a time period */
2232/* NOTE: not currently used anywhere */
2233void
2234__kmp_thread_sleep( int millis )
2235{
2236 sleep( ( millis + 500 ) / 1000 );
2237}
2238
2239/* Calculate the elapsed wall clock time for the user */
2240void
2241__kmp_elapsed( double *t )
2242{
2243 int status;
2244# ifdef FIX_SGI_CLOCK
2245 struct timespec ts;
2246
2247 status = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
2248 KMP_CHECK_SYSFAIL_ERRNO( "clock_gettime", status );
2249 *t = (double) ts.tv_nsec * (1.0 / (double) NSEC_PER_SEC) +
2250 (double) ts.tv_sec;
2251# else
2252 struct timeval tv;
2253
2254 status = gettimeofday( & tv, NULL );
2255 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
2256 *t = (double) tv.tv_usec * (1.0 / (double) USEC_PER_SEC) +
2257 (double) tv.tv_sec;
2258# endif
2259}
2260
2261/* Calculate the elapsed wall clock tick for the user */
2262void
2263__kmp_elapsed_tick( double *t )
2264{
2265 *t = 1 / (double) CLOCKS_PER_SEC;
2266}
2267
2268/*
2269 Determine whether the given address is mapped into the current address space.
2270*/
2271
2272int
2273__kmp_is_address_mapped( void * addr ) {
2274
2275 int found = 0;
2276 int rc;
2277
2278 #if KMP_OS_LINUX
2279
2280 /*
2281 On Linux* OS, read the /proc/<pid>/maps pseudo-file to get all the address ranges mapped
2282 into the address space.
2283 */
2284
2285 char * name = __kmp_str_format( "/proc/%d/maps", getpid() );
2286 FILE * file = NULL;
2287
2288 file = fopen( name, "r" );
2289 KMP_ASSERT( file != NULL );
2290
2291 for ( ; ; ) {
2292
2293 void * beginning = NULL;
2294 void * ending = NULL;
2295 char perms[ 5 ];
2296
2297 rc = fscanf( file, "%p-%p %4s %*[^\n]\n", & beginning, & ending, perms );
2298 if ( rc == EOF ) {
2299 break;
2300 }; // if
2301 KMP_ASSERT( rc == 3 && strlen( perms ) == 4 ); // Make sure all fields are read.
2302
2303 // Ending address is not included in the region, but beginning is.
2304 if ( ( addr >= beginning ) && ( addr < ending ) ) {
2305 perms[ 2 ] = 0; // 3th and 4th character does not matter.
2306 if ( strcmp( perms, "rw" ) == 0 ) {
2307 // Memory we are looking for should be readable and writable.
2308 found = 1;
2309 }; // if
2310 break;
2311 }; // if
2312
2313 }; // forever
2314
2315 // Free resources.
2316 fclose( file );
2317 KMP_INTERNAL_FREE( name );
2318
2319 #elif KMP_OS_DARWIN
2320
2321 /*
2322 On OS X*, /proc pseudo filesystem is not available. Try to read memory using vm
2323 interface.
2324 */
2325
2326 int buffer;
2327 vm_size_t count;
2328 rc =
2329 vm_read_overwrite(
2330 mach_task_self(), // Task to read memory of.
2331 (vm_address_t)( addr ), // Address to read from.
2332 1, // Number of bytes to be read.
2333 (vm_address_t)( & buffer ), // Address of buffer to save read bytes in.
2334 & count // Address of var to save number of read bytes in.
2335 );
2336 if ( rc == 0 ) {
2337 // Memory successfully read.
2338 found = 1;
2339 }; // if
2340
Alp Toker763b9392014-02-28 09:42:41 +00002341 #elif KMP_OS_FREEBSD
2342
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002343 // FIXME(FreeBSD*): Implement this
Alp Toker763b9392014-02-28 09:42:41 +00002344 found = 1;
2345
Jim Cownie5e8470a2013-09-27 10:38:44 +00002346 #else
2347
2348 #error "Unknown or unsupported OS"
2349
2350 #endif
2351
2352 return found;
2353
2354} // __kmp_is_address_mapped
2355
2356#ifdef USE_LOAD_BALANCE
2357
2358
2359# if KMP_OS_DARWIN
2360
2361// The function returns the rounded value of the system load average
2362// during given time interval which depends on the value of
2363// __kmp_load_balance_interval variable (default is 60 sec, other values
2364// may be 300 sec or 900 sec).
2365// It returns -1 in case of error.
2366int
2367__kmp_get_load_balance( int max )
2368{
2369 double averages[3];
2370 int ret_avg = 0;
2371
2372 int res = getloadavg( averages, 3 );
2373
2374 //Check __kmp_load_balance_interval to determine which of averages to use.
2375 // getloadavg() may return the number of samples less than requested that is
2376 // less than 3.
2377 if ( __kmp_load_balance_interval < 180 && ( res >= 1 ) ) {
2378 ret_avg = averages[0];// 1 min
2379 } else if ( ( __kmp_load_balance_interval >= 180
2380 && __kmp_load_balance_interval < 600 ) && ( res >= 2 ) ) {
2381 ret_avg = averages[1];// 5 min
2382 } else if ( ( __kmp_load_balance_interval >= 600 ) && ( res == 3 ) ) {
2383 ret_avg = averages[2];// 15 min
Alp Toker8f2d3f02014-02-24 10:40:15 +00002384 } else {// Error occurred
Jim Cownie5e8470a2013-09-27 10:38:44 +00002385 return -1;
2386 }
2387
2388 return ret_avg;
2389}
2390
2391# else // Linux* OS
2392
2393// The fuction returns number of running (not sleeping) threads, or -1 in case of error.
2394// Error could be reported if Linux* OS kernel too old (without "/proc" support).
2395// Counting running threads stops if max running threads encountered.
2396int
2397__kmp_get_load_balance( int max )
2398{
2399 static int permanent_error = 0;
2400
2401 static int glb_running_threads = 0; /* Saved count of the running threads for the thread balance algortihm */
2402 static double glb_call_time = 0; /* Thread balance algorithm call time */
2403
2404 int running_threads = 0; // Number of running threads in the system.
2405
2406 DIR * proc_dir = NULL; // Handle of "/proc/" directory.
2407 struct dirent * proc_entry = NULL;
2408
2409 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2410 DIR * task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2411 struct dirent * task_entry = NULL;
2412 int task_path_fixed_len;
2413
2414 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2415 int stat_file = -1;
2416 int stat_path_fixed_len;
2417
2418 int total_processes = 0; // Total number of processes in system.
2419 int total_threads = 0; // Total number of threads in system.
2420
2421 double call_time = 0.0;
2422
2423 __kmp_str_buf_init( & task_path );
2424 __kmp_str_buf_init( & stat_path );
2425
2426 __kmp_elapsed( & call_time );
2427
2428 if ( glb_call_time &&
2429 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
2430 running_threads = glb_running_threads;
2431 goto finish;
2432 }
2433
2434 glb_call_time = call_time;
2435
2436 // Do not spend time on scanning "/proc/" if we have a permanent error.
2437 if ( permanent_error ) {
2438 running_threads = -1;
2439 goto finish;
2440 }; // if
2441
2442 if ( max <= 0 ) {
2443 max = INT_MAX;
2444 }; // if
2445
2446 // Open "/proc/" directory.
2447 proc_dir = opendir( "/proc" );
2448 if ( proc_dir == NULL ) {
2449 // Cannot open "/prroc/". Probably the kernel does not support it. Return an error now and
2450 // in subsequent calls.
2451 running_threads = -1;
2452 permanent_error = 1;
2453 goto finish;
2454 }; // if
2455
2456 // Initialize fixed part of task_path. This part will not change.
2457 __kmp_str_buf_cat( & task_path, "/proc/", 6 );
2458 task_path_fixed_len = task_path.used; // Remember number of used characters.
2459
2460 proc_entry = readdir( proc_dir );
2461 while ( proc_entry != NULL ) {
2462 // Proc entry is a directory and name starts with a digit. Assume it is a process'
2463 // directory.
2464 if ( proc_entry->d_type == DT_DIR && isdigit( proc_entry->d_name[ 0 ] ) ) {
2465
2466 ++ total_processes;
2467 // Make sure init process is the very first in "/proc", so we can replace
2468 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes == 1.
2469 // We are going to check that total_processes == 1 => d_name == "1" is true (where
2470 // "=>" is implication). Since C++ does not have => operator, let us replace it with its
2471 // equivalent: a => b == ! a || b.
2472 KMP_DEBUG_ASSERT( total_processes != 1 || strcmp( proc_entry->d_name, "1" ) == 0 );
2473
2474 // Construct task_path.
2475 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
2476 __kmp_str_buf_cat( & task_path, proc_entry->d_name, strlen( proc_entry->d_name ) );
2477 __kmp_str_buf_cat( & task_path, "/task", 5 );
2478
2479 task_dir = opendir( task_path.str );
2480 if ( task_dir == NULL ) {
2481 // Process can finish between reading "/proc/" directory entry and opening process'
2482 // "task/" directory. So, in general case we should not complain, but have to skip
2483 // this process and read the next one.
2484 // But on systems with no "task/" support we will spend lot of time to scan "/proc/"
2485 // tree again and again without any benefit. "init" process (its pid is 1) should
2486 // exist always, so, if we cannot open "/proc/1/task/" directory, it means "task/"
2487 // is not supported by kernel. Report an error now and in the future.
2488 if ( strcmp( proc_entry->d_name, "1" ) == 0 ) {
2489 running_threads = -1;
2490 permanent_error = 1;
2491 goto finish;
2492 }; // if
2493 } else {
2494 // Construct fixed part of stat file path.
2495 __kmp_str_buf_clear( & stat_path );
2496 __kmp_str_buf_cat( & stat_path, task_path.str, task_path.used );
2497 __kmp_str_buf_cat( & stat_path, "/", 1 );
2498 stat_path_fixed_len = stat_path.used;
2499
2500 task_entry = readdir( task_dir );
2501 while ( task_entry != NULL ) {
2502 // It is a directory and name starts with a digit.
2503 if ( proc_entry->d_type == DT_DIR && isdigit( task_entry->d_name[ 0 ] ) ) {
2504
2505 ++ total_threads;
2506
2507 // Consruct complete stat file path. Easiest way would be:
2508 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str, task_entry->d_name );
2509 // but seriae of __kmp_str_buf_cat works a bit faster.
2510 stat_path.used = stat_path_fixed_len; // Reset stat path to its fixed part.
2511 __kmp_str_buf_cat( & stat_path, task_entry->d_name, strlen( task_entry->d_name ) );
2512 __kmp_str_buf_cat( & stat_path, "/stat", 5 );
2513
2514 // Note: Low-level API (open/read/close) is used. High-level API
2515 // (fopen/fclose) works ~ 30 % slower.
2516 stat_file = open( stat_path.str, O_RDONLY );
2517 if ( stat_file == -1 ) {
2518 // We cannot report an error because task (thread) can terminate just
2519 // before reading this file.
2520 } else {
2521 /*
2522 Content of "stat" file looks like:
2523
2524 24285 (program) S ...
2525
2526 It is a single line (if program name does not include fanny
2527 symbols). First number is a thread id, then name of executable file
2528 name in paretheses, then state of the thread. We need just thread
2529 state.
2530
2531 Good news: Length of program name is 15 characters max. Longer
2532 names are truncated.
2533
2534 Thus, we need rather short buffer: 15 chars for program name +
2535 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
2536
2537 Bad news: Program name may contain special symbols like space,
2538 closing parenthesis, or even new line. This makes parsing "stat"
2539 file not 100 % reliable. In case of fanny program names parsing
2540 may fail (report incorrect thread state).
2541
2542 Parsing "status" file looks more promissing (due to different
2543 file structure and escaping special symbols) but reading and
2544 parsing of "status" file works slower.
2545
2546 -- ln
2547 */
2548 char buffer[ 65 ];
2549 int len;
2550 len = read( stat_file, buffer, sizeof( buffer ) - 1 );
2551 if ( len >= 0 ) {
2552 buffer[ len ] = 0;
2553 // Using scanf:
2554 // sscanf( buffer, "%*d (%*s) %c ", & state );
2555 // looks very nice, but searching for a closing parenthesis works a
2556 // bit faster.
2557 char * close_parent = strstr( buffer, ") " );
2558 if ( close_parent != NULL ) {
2559 char state = * ( close_parent + 2 );
2560 if ( state == 'R' ) {
2561 ++ running_threads;
2562 if ( running_threads >= max ) {
2563 goto finish;
2564 }; // if
2565 }; // if
2566 }; // if
2567 }; // if
2568 close( stat_file );
2569 stat_file = -1;
2570 }; // if
2571 }; // if
2572 task_entry = readdir( task_dir );
2573 }; // while
2574 closedir( task_dir );
2575 task_dir = NULL;
2576 }; // if
2577 }; // if
2578 proc_entry = readdir( proc_dir );
2579 }; // while
2580
2581 //
2582 // There _might_ be a timing hole where the thread executing this
2583 // code get skipped in the load balance, and running_threads is 0.
2584 // Assert in the debug builds only!!!
2585 //
2586 KMP_DEBUG_ASSERT( running_threads > 0 );
2587 if ( running_threads <= 0 ) {
2588 running_threads = 1;
2589 }
2590
2591 finish: // Clean up and exit.
2592 if ( proc_dir != NULL ) {
2593 closedir( proc_dir );
2594 }; // if
2595 __kmp_str_buf_free( & task_path );
2596 if ( task_dir != NULL ) {
2597 closedir( task_dir );
2598 }; // if
2599 __kmp_str_buf_free( & stat_path );
2600 if ( stat_file != -1 ) {
2601 close( stat_file );
2602 }; // if
2603
2604 glb_running_threads = running_threads;
2605
2606 return running_threads;
2607
2608} // __kmp_get_load_balance
2609
2610# endif // KMP_OS_DARWIN
2611
2612#endif // USE_LOAD_BALANCE
2613
Jim Cownie181b4bb2013-12-23 17:28:57 +00002614
Andrey Churbanovcbda8682015-01-13 14:43:35 +00002615#if KMP_COMPILER_GCC && !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64)
Jim Cownie181b4bb2013-12-23 17:28:57 +00002616
2617int __kmp_invoke_microtask( microtask_t pkfn, int gtid, int tid, int argc,
2618 void *p_argv[] )
2619{
2620 int argc_full = argc + 2;
2621 int i;
2622 ffi_cif cif;
2623 ffi_type *types[argc_full];
2624 void *args[argc_full];
2625 void *idp[2];
2626
2627 /* We're only passing pointers to the target. */
2628 for (i = 0; i < argc_full; i++)
2629 types[i] = &ffi_type_pointer;
2630
2631 /* Ugly double-indirection, but that's how it goes... */
2632 idp[0] = &gtid;
2633 idp[1] = &tid;
2634 args[0] = &idp[0];
2635 args[1] = &idp[1];
2636
2637 for (i = 0; i < argc; i++)
2638 args[2 + i] = &p_argv[i];
2639
2640 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argc_full,
2641 &ffi_type_void, types) != FFI_OK)
2642 abort();
2643
2644 ffi_call(&cif, (void (*)(void))pkfn, NULL, args);
2645
2646 return 1;
2647}
2648
Jim Cownie3051f972014-08-07 10:12:54 +00002649#endif // KMP_COMPILER_GCC && !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_PPC64)
2650
Andrey Churbanovcbda8682015-01-13 14:43:35 +00002651#if KMP_ARCH_PPC64 || KMP_ARCH_AARCH64
Jim Cownie3051f972014-08-07 10:12:54 +00002652
2653// we really only need the case with 1 argument, because CLANG always build
2654// a struct of pointers to shared variables referenced in the outlined function
2655int
2656__kmp_invoke_microtask( microtask_t pkfn,
2657 int gtid, int tid,
2658 int argc, void *p_argv[] ) {
2659 switch (argc) {
2660 default:
2661 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2662 fflush(stderr);
2663 exit(-1);
2664 case 0:
2665 (*pkfn)(&gtid, &tid);
2666 break;
2667 case 1:
2668 (*pkfn)(&gtid, &tid, p_argv[0]);
2669 break;
2670 case 2:
2671 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2672 break;
2673 case 3:
2674 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2675 break;
2676 case 4:
2677 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2678 break;
2679 case 5:
2680 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2681 break;
2682 case 6:
2683 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2684 p_argv[5]);
2685 break;
2686 case 7:
2687 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2688 p_argv[5], p_argv[6]);
2689 break;
2690 case 8:
2691 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2692 p_argv[5], p_argv[6], p_argv[7]);
2693 break;
2694 case 9:
2695 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2696 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2697 break;
2698 case 10:
2699 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2700 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2701 break;
2702 case 11:
2703 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2704 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2705 break;
2706 case 12:
2707 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2708 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2709 p_argv[11]);
2710 break;
2711 case 13:
2712 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2713 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2714 p_argv[11], p_argv[12]);
2715 break;
2716 case 14:
2717 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2718 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2719 p_argv[11], p_argv[12], p_argv[13]);
2720 break;
2721 case 15:
2722 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2723 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2724 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2725 break;
2726 }
2727
2728 return 1;
2729}
2730
2731#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002732
Jim Cownie5e8470a2013-09-27 10:38:44 +00002733// end of file //
2734