blob: d66d2a4557cd323c764352f8c5b161fac12cbae3 [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>
Jim Cownie181b4bb2013-12-23 17:28:57 +000039# if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM)
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
Jim Cownie3051f972014-08-07 10:12:54 +000066#if KMP_COMPILER_GCC && !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_PPC64)
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
140# elif KMP_ARCH_X86_64
141# ifndef __NR_sched_setaffinity
142# define __NR_sched_setaffinity 203
143# elif __NR_sched_setaffinity != 203
144# error Wrong code for setaffinity system call.
145# endif /* __NR_sched_setaffinity */
146# ifndef __NR_sched_getaffinity
147# define __NR_sched_getaffinity 204
148# elif __NR_sched_getaffinity != 204
149# error Wrong code for getaffinity system call.
150# endif /* __NR_sched_getaffinity */
151
Jim Cownie3051f972014-08-07 10:12:54 +0000152# elif KMP_ARCH_PPC64
153# ifndef __NR_sched_setaffinity
154# define __NR_sched_setaffinity 222
155# elif __NR_sched_setaffinity != 222
156# error Wrong code for setaffinity system call.
157# endif /* __NR_sched_setaffinity */
158# ifndef __NR_sched_getaffinity
159# define __NR_sched_getaffinity 223
160# elif __NR_sched_getaffinity != 223
161# error Wrong code for getaffinity system call.
162# endif /* __NR_sched_getaffinity */
163
164
Jim Cownie5e8470a2013-09-27 10:38:44 +0000165# else
166# error Unknown or unsupported architecture
167
168# endif /* KMP_ARCH_* */
169
170int
171__kmp_set_system_affinity( kmp_affin_mask_t const *mask, int abort_on_error )
172{
173 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
174 "Illegal set affinity operation when not capable");
175
176 int retval = syscall( __NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask );
177 if (retval >= 0) {
178 return 0;
179 }
180 int error = errno;
181 if (abort_on_error) {
182 __kmp_msg(
183 kmp_ms_fatal,
184 KMP_MSG( FatalSysError ),
185 KMP_ERR( error ),
186 __kmp_msg_null
187 );
188 }
189 return error;
190}
191
192int
193__kmp_get_system_affinity( kmp_affin_mask_t *mask, int abort_on_error )
194{
195 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
196 "Illegal get affinity operation when not capable");
197
198 int retval = syscall( __NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask );
199 if (retval >= 0) {
200 return 0;
201 }
202 int error = errno;
203 if (abort_on_error) {
204 __kmp_msg(
205 kmp_ms_fatal,
206 KMP_MSG( FatalSysError ),
207 KMP_ERR( error ),
208 __kmp_msg_null
209 );
210 }
211 return error;
212}
213
214void
215__kmp_affinity_bind_thread( int which )
216{
217 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
218 "Illegal set affinity operation when not capable");
219
220 kmp_affin_mask_t *mask = (kmp_affin_mask_t *)alloca(__kmp_affin_mask_size);
221 KMP_CPU_ZERO(mask);
222 KMP_CPU_SET(which, mask);
223 __kmp_set_system_affinity(mask, TRUE);
224}
225
226/*
227 * Determine if we can access affinity functionality on this version of
228 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
229 * __kmp_affin_mask_size to the appropriate value (0 means not capable).
230 */
231void
232__kmp_affinity_determine_capable(const char *env_var)
233{
234 //
235 // Check and see if the OS supports thread affinity.
236 //
237
238# define KMP_CPU_SET_SIZE_LIMIT (1024*1024)
239
240 int gCode;
241 int sCode;
242 kmp_affin_mask_t *buf;
243 buf = ( kmp_affin_mask_t * ) KMP_INTERNAL_MALLOC( KMP_CPU_SET_SIZE_LIMIT );
244
245 // If Linux* OS:
246 // If the syscall fails or returns a suggestion for the size,
247 // then we don't have to search for an appropriate size.
248 gCode = syscall( __NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf );
249 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
Alp Toker8f2d3f02014-02-24 10:40:15 +0000250 "initial getaffinity call returned %d errno = %d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +0000251 gCode, errno));
252
253 //if ((gCode < 0) && (errno == ENOSYS))
254 if (gCode < 0) {
255 //
256 // System call not supported
257 //
258 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
259 && (__kmp_affinity_type != affinity_none)
260 && (__kmp_affinity_type != affinity_default)
261 && (__kmp_affinity_type != affinity_disabled))) {
262 int error = errno;
263 __kmp_msg(
264 kmp_ms_warning,
265 KMP_MSG( GetAffSysCallNotSupported, env_var ),
266 KMP_ERR( error ),
267 __kmp_msg_null
268 );
269 }
270 __kmp_affin_mask_size = 0; // should already be 0
271 KMP_INTERNAL_FREE(buf);
272 return;
273 }
274 if (gCode > 0) { // Linux* OS only
275 // The optimal situation: the OS returns the size of the buffer
276 // it expects.
277 //
278 // A verification of correct behavior is that Isetaffinity on a NULL
279 // buffer with the same size fails with errno set to EFAULT.
280 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
281 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
282 "setaffinity for mask size %d returned %d errno = %d\n",
283 gCode, sCode, errno));
284 if (sCode < 0) {
285 if (errno == ENOSYS) {
286 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
287 && (__kmp_affinity_type != affinity_none)
288 && (__kmp_affinity_type != affinity_default)
289 && (__kmp_affinity_type != affinity_disabled))) {
290 int error = errno;
291 __kmp_msg(
292 kmp_ms_warning,
293 KMP_MSG( SetAffSysCallNotSupported, env_var ),
294 KMP_ERR( error ),
295 __kmp_msg_null
296 );
297 }
298 __kmp_affin_mask_size = 0; // should already be 0
299 KMP_INTERNAL_FREE(buf);
300 }
301 if (errno == EFAULT) {
302 __kmp_affin_mask_size = gCode;
303 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
304 "affinity supported (mask size %d)\n",
305 (int)__kmp_affin_mask_size));
306 KMP_INTERNAL_FREE(buf);
307 return;
308 }
309 }
310 }
311
312 //
313 // Call the getaffinity system call repeatedly with increasing set sizes
314 // until we succeed, or reach an upper bound on the search.
315 //
316 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
317 "searching for proper set size\n"));
318 int size;
319 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
320 gCode = syscall( __NR_sched_getaffinity, 0, size, buf );
321 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
322 "getaffinity for mask size %d returned %d errno = %d\n", size,
323 gCode, errno));
324
325 if (gCode < 0) {
326 if ( errno == ENOSYS )
327 {
328 //
329 // We shouldn't get here
330 //
331 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
332 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
333 size));
334 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
335 && (__kmp_affinity_type != affinity_none)
336 && (__kmp_affinity_type != affinity_default)
337 && (__kmp_affinity_type != affinity_disabled))) {
338 int error = errno;
339 __kmp_msg(
340 kmp_ms_warning,
341 KMP_MSG( GetAffSysCallNotSupported, env_var ),
342 KMP_ERR( error ),
343 __kmp_msg_null
344 );
345 }
346 __kmp_affin_mask_size = 0; // should already be 0
347 KMP_INTERNAL_FREE(buf);
348 return;
349 }
350 continue;
351 }
352
353 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
354 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
355 "setaffinity for mask size %d returned %d errno = %d\n",
356 gCode, sCode, errno));
357 if (sCode < 0) {
358 if (errno == ENOSYS) { // Linux* OS only
359 //
360 // We shouldn't get here
361 //
362 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
363 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
364 size));
365 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
366 && (__kmp_affinity_type != affinity_none)
367 && (__kmp_affinity_type != affinity_default)
368 && (__kmp_affinity_type != affinity_disabled))) {
369 int error = errno;
370 __kmp_msg(
371 kmp_ms_warning,
372 KMP_MSG( SetAffSysCallNotSupported, env_var ),
373 KMP_ERR( error ),
374 __kmp_msg_null
375 );
376 }
377 __kmp_affin_mask_size = 0; // should already be 0
378 KMP_INTERNAL_FREE(buf);
379 return;
380 }
381 if (errno == EFAULT) {
382 __kmp_affin_mask_size = gCode;
383 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
384 "affinity supported (mask size %d)\n",
385 (int)__kmp_affin_mask_size));
386 KMP_INTERNAL_FREE(buf);
387 return;
388 }
389 }
390 }
391 //int error = errno; // save uncaught error code
392 KMP_INTERNAL_FREE(buf);
393 // errno = error; // restore uncaught error code, will be printed at the next KMP_WARNING below
394
395 //
396 // Affinity is not supported
397 //
398 __kmp_affin_mask_size = 0;
399 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
400 "cannot determine mask size - affinity not supported\n"));
401 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
402 && (__kmp_affinity_type != affinity_none)
403 && (__kmp_affinity_type != affinity_default)
404 && (__kmp_affinity_type != affinity_disabled))) {
405 KMP_WARNING( AffCantGetMaskSize, env_var );
406 }
407}
408
409
410/*
411 * Change thread to the affinity mask pointed to by affin_mask argument
412 * and return a pointer to the old value in the old_mask argument, if argument
413 * is non-NULL.
414 */
415
416void
417__kmp_change_thread_affinity_mask( int gtid, kmp_affin_mask_t *new_mask,
418 kmp_affin_mask_t *old_mask )
419{
420 KMP_DEBUG_ASSERT( gtid == __kmp_get_gtid() );
421 if ( KMP_AFFINITY_CAPABLE() ) {
422 int status;
423 kmp_info_t *th = __kmp_threads[ gtid ];
424
425 KMP_DEBUG_ASSERT( new_mask != NULL );
426
427 if ( old_mask != NULL ) {
428 status = __kmp_get_system_affinity( old_mask, TRUE );
429 int error = errno;
430 if ( status != 0 ) {
431 __kmp_msg(
432 kmp_ms_fatal,
433 KMP_MSG( ChangeThreadAffMaskError ),
434 KMP_ERR( error ),
435 __kmp_msg_null
436 );
437 }
438 }
439
440 __kmp_set_system_affinity( new_mask, TRUE );
441
442 if (__kmp_affinity_verbose) {
443 char old_buf[KMP_AFFIN_MASK_PRINT_LEN];
444 char new_buf[KMP_AFFIN_MASK_PRINT_LEN];
445 __kmp_affinity_print_mask(old_buf, KMP_AFFIN_MASK_PRINT_LEN, old_mask);
446 __kmp_affinity_print_mask(new_buf, KMP_AFFIN_MASK_PRINT_LEN, new_mask);
447 KMP_INFORM( ChangeAffMask, "KMP_AFFINITY (Bind)", gtid, old_buf, new_buf );
448
449 }
450
451 /* Make sure old value is correct in thread data structures */
452 KMP_DEBUG_ASSERT( old_mask != NULL && (memcmp(old_mask,
453 th->th.th_affin_mask, __kmp_affin_mask_size) == 0) );
454 KMP_CPU_COPY( th->th.th_affin_mask, new_mask );
455 }
456}
457
Alp Toker98758b02014-03-02 04:12:06 +0000458#endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000459
460/* ------------------------------------------------------------------------ */
461/* ------------------------------------------------------------------------ */
462
Jim Cownie3051f972014-08-07 10:12:54 +0000463#if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM) && !KMP_OS_CNK
Jim Cownie5e8470a2013-09-27 10:38:44 +0000464
465int
466__kmp_futex_determine_capable()
467{
468 int loc = 0;
469 int rc = syscall( __NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0 );
470 int retval = ( rc == 0 ) || ( errno != ENOSYS );
471
472 KA_TRACE(10, ( "__kmp_futex_determine_capable: rc = %d errno = %d\n", rc,
473 errno ) );
474 KA_TRACE(10, ( "__kmp_futex_determine_capable: futex syscall%s supported\n",
475 retval ? "" : " not" ) );
476
477 return retval;
478}
479
Jim Cownie3051f972014-08-07 10:12:54 +0000480#endif // KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM) && !KMP_OS_CNK
Jim Cownie5e8470a2013-09-27 10:38:44 +0000481
482/* ------------------------------------------------------------------------ */
483/* ------------------------------------------------------------------------ */
484
485#if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS)
486/*
487 * Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
488 * use compare_and_store for these routines
489 */
490
491kmp_int32
492__kmp_test_then_or32( volatile kmp_int32 *p, kmp_int32 d )
493{
494 kmp_int32 old_value, new_value;
495
496 old_value = TCR_4( *p );
497 new_value = old_value | d;
498
Jim Cownie3051f972014-08-07 10:12:54 +0000499 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000500 {
501 KMP_CPU_PAUSE();
502 old_value = TCR_4( *p );
503 new_value = old_value | d;
504 }
505 return old_value;
506}
507
508kmp_int32
509__kmp_test_then_and32( volatile kmp_int32 *p, kmp_int32 d )
510{
511 kmp_int32 old_value, new_value;
512
513 old_value = TCR_4( *p );
514 new_value = old_value & d;
515
Jim Cownie3051f972014-08-07 10:12:54 +0000516 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000517 {
518 KMP_CPU_PAUSE();
519 old_value = TCR_4( *p );
520 new_value = old_value & d;
521 }
522 return old_value;
523}
524
Jim Cownie3051f972014-08-07 10:12:54 +0000525# if KMP_ARCH_X86 || KMP_ARCH_PPC64
Jim Cownie5e8470a2013-09-27 10:38:44 +0000526kmp_int64
527__kmp_test_then_add64( volatile kmp_int64 *p, kmp_int64 d )
528{
529 kmp_int64 old_value, new_value;
530
531 old_value = TCR_8( *p );
532 new_value = old_value + d;
533
Jim Cownie3051f972014-08-07 10:12:54 +0000534 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000535 {
536 KMP_CPU_PAUSE();
537 old_value = TCR_8( *p );
538 new_value = old_value + d;
539 }
540 return old_value;
541}
542# endif /* KMP_ARCH_X86 */
543
544kmp_int64
545__kmp_test_then_or64( volatile kmp_int64 *p, kmp_int64 d )
546{
547 kmp_int64 old_value, new_value;
548
549 old_value = TCR_8( *p );
550 new_value = old_value | d;
Jim Cownie3051f972014-08-07 10:12:54 +0000551 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000552 {
553 KMP_CPU_PAUSE();
554 old_value = TCR_8( *p );
555 new_value = old_value | d;
556 }
557 return old_value;
558}
559
560kmp_int64
561__kmp_test_then_and64( volatile kmp_int64 *p, kmp_int64 d )
562{
563 kmp_int64 old_value, new_value;
564
565 old_value = TCR_8( *p );
566 new_value = old_value & d;
Jim Cownie3051f972014-08-07 10:12:54 +0000567 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000568 {
569 KMP_CPU_PAUSE();
570 old_value = TCR_8( *p );
571 new_value = old_value & d;
572 }
573 return old_value;
574}
575
576#endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
577
578void
579__kmp_terminate_thread( int gtid )
580{
581 int status;
582 kmp_info_t *th = __kmp_threads[ gtid ];
583
584 if ( !th ) return;
585
586 #ifdef KMP_CANCEL_THREADS
587 KA_TRACE( 10, ("__kmp_terminate_thread: kill (%d)\n", gtid ) );
588 status = pthread_cancel( th->th.th_info.ds.ds_thread );
589 if ( status != 0 && status != ESRCH ) {
590 __kmp_msg(
591 kmp_ms_fatal,
592 KMP_MSG( CantTerminateWorkerThread ),
593 KMP_ERR( status ),
594 __kmp_msg_null
595 );
596 }; // if
597 #endif
598 __kmp_yield( TRUE );
599} //
600
601/* ------------------------------------------------------------------------ */
602/* ------------------------------------------------------------------------ */
603
604/* ------------------------------------------------------------------------ */
605/* ------------------------------------------------------------------------ */
606
607/*
608 * Set thread stack info according to values returned by
609 * pthread_getattr_np().
610 * If values are unreasonable, assume call failed and use
611 * incremental stack refinement method instead.
612 * Returns TRUE if the stack parameters could be determined exactly,
613 * FALSE if incremental refinement is necessary.
614 */
615static kmp_int32
616__kmp_set_stack_info( int gtid, kmp_info_t *th )
617{
618 int stack_data;
Alp Toker763b9392014-02-28 09:42:41 +0000619#if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000620 /* Linux* OS only -- no pthread_getattr_np support on OS X* */
621 pthread_attr_t attr;
622 int status;
623 size_t size = 0;
624 void * addr = 0;
625
626 /* Always do incremental stack refinement for ubermaster threads since the initial
627 thread stack range can be reduced by sibling thread creation so pthread_attr_getstack
628 may cause thread gtid aliasing */
629 if ( ! KMP_UBER_GTID(gtid) ) {
630
631 /* Fetch the real thread attributes */
632 status = pthread_attr_init( &attr );
633 KMP_CHECK_SYSFAIL( "pthread_attr_init", status );
Alp Toker763b9392014-02-28 09:42:41 +0000634#if KMP_OS_FREEBSD
635 status = pthread_attr_get_np( pthread_self(), &attr );
636 KMP_CHECK_SYSFAIL( "pthread_attr_get_np", status );
637#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000638 status = pthread_getattr_np( pthread_self(), &attr );
639 KMP_CHECK_SYSFAIL( "pthread_getattr_np", status );
Alp Toker763b9392014-02-28 09:42:41 +0000640#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000641 status = pthread_attr_getstack( &attr, &addr, &size );
642 KMP_CHECK_SYSFAIL( "pthread_attr_getstack", status );
643 KA_TRACE( 60, ( "__kmp_set_stack_info: T#%d pthread_attr_getstack returned size: %lu, "
644 "low addr: %p\n",
645 gtid, size, addr ));
646
647 status = pthread_attr_destroy( &attr );
648 KMP_CHECK_SYSFAIL( "pthread_attr_destroy", status );
649 }
650
651 if ( size != 0 && addr != 0 ) { /* was stack parameter determination successful? */
652 /* Store the correct base and size */
653 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size));
654 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
655 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
656 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000657 }
Alp Toker763b9392014-02-28 09:42:41 +0000658#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD */
Alp Toker763b9392014-02-28 09:42:41 +0000659 /* Use incremental refinement starting from initial conservative estimate */
660 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
661 TCW_PTR(th -> th.th_info.ds.ds_stackbase, &stack_data);
662 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
663 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000664}
665
666static void*
667__kmp_launch_worker( void *thr )
668{
669 int status, old_type, old_state;
670#ifdef KMP_BLOCK_SIGNALS
671 sigset_t new_set, old_set;
672#endif /* KMP_BLOCK_SIGNALS */
673 void *exit_val;
674 void *padding = 0;
675 int gtid;
676 int error;
677
678 gtid = ((kmp_info_t*)thr) -> th.th_info.ds.ds_gtid;
679 __kmp_gtid_set_specific( gtid );
680#ifdef KMP_TDATA_GTID
681 __kmp_gtid = gtid;
682#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000683#if KMP_STATS_ENABLED
684 // set __thread local index to point to thread-specific stats
685 __kmp_stats_thread_ptr = ((kmp_info_t*)thr)->th.th_stats;
686#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000687
688#if USE_ITT_BUILD
689 __kmp_itt_thread_name( gtid );
690#endif /* USE_ITT_BUILD */
691
Alp Toker763b9392014-02-28 09:42:41 +0000692#if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000693 __kmp_affinity_set_init_mask( gtid, FALSE );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000694#endif
695
696#ifdef KMP_CANCEL_THREADS
697 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
698 KMP_CHECK_SYSFAIL( "pthread_setcanceltype", status );
699 /* josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? */
700 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
701 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
702#endif
703
704#if KMP_ARCH_X86 || KMP_ARCH_X86_64
705 //
706 // Set the FP control regs to be a copy of
707 // the parallel initialization thread's.
708 //
709 __kmp_clear_x87_fpu_status_word();
710 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
711 __kmp_load_mxcsr( &__kmp_init_mxcsr );
712#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
713
714#ifdef KMP_BLOCK_SIGNALS
715 status = sigfillset( & new_set );
716 KMP_CHECK_SYSFAIL_ERRNO( "sigfillset", status );
717 status = pthread_sigmask( SIG_BLOCK, & new_set, & old_set );
718 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
719#endif /* KMP_BLOCK_SIGNALS */
720
Alp Toker763b9392014-02-28 09:42:41 +0000721#if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000722 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
723 padding = alloca( gtid * __kmp_stkoffset );
724 }
725#endif
726
727 KMP_MB();
728 __kmp_set_stack_info( gtid, (kmp_info_t*)thr );
729
730 __kmp_check_stack_overlap( (kmp_info_t*)thr );
731
732 exit_val = __kmp_launch_thread( (kmp_info_t *) thr );
733
734#ifdef KMP_BLOCK_SIGNALS
735 status = pthread_sigmask( SIG_SETMASK, & old_set, NULL );
736 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
737#endif /* KMP_BLOCK_SIGNALS */
738
739 return exit_val;
740}
741
742
743/* The monitor thread controls all of the threads in the complex */
744
745static void*
746__kmp_launch_monitor( void *thr )
747{
748 int status, old_type, old_state;
749#ifdef KMP_BLOCK_SIGNALS
750 sigset_t new_set;
751#endif /* KMP_BLOCK_SIGNALS */
752 struct timespec interval;
753 int yield_count;
754 int yield_cycles = 0;
755 int error;
756
757 KMP_MB(); /* Flush all pending memory write invalidates. */
758
759 KA_TRACE( 10, ("__kmp_launch_monitor: #1 launched\n" ) );
760
761 /* register us as the monitor thread */
762 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
763#ifdef KMP_TDATA_GTID
764 __kmp_gtid = KMP_GTID_MONITOR;
765#endif
766
767 KMP_MB();
768
769#if USE_ITT_BUILD
770 __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore monitor thread.
771#endif /* USE_ITT_BUILD */
772
773 __kmp_set_stack_info( ((kmp_info_t*)thr)->th.th_info.ds.ds_gtid, (kmp_info_t*)thr );
774
775 __kmp_check_stack_overlap( (kmp_info_t*)thr );
776
777#ifdef KMP_CANCEL_THREADS
778 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
779 KMP_CHECK_SYSFAIL( "pthread_setcanceltype", status );
780 /* josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? */
781 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
782 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
783#endif
784
785 #if KMP_REAL_TIME_FIX
786 // This is a potential fix which allows application with real-time scheduling policy work.
787 // However, decision about the fix is not made yet, so it is disabled by default.
788 { // Are program started with real-time scheduling policy?
789 int sched = sched_getscheduler( 0 );
790 if ( sched == SCHED_FIFO || sched == SCHED_RR ) {
791 // Yes, we are a part of real-time application. Try to increase the priority of the
792 // monitor.
793 struct sched_param param;
794 int max_priority = sched_get_priority_max( sched );
795 int rc;
796 KMP_WARNING( RealTimeSchedNotSupported );
797 sched_getparam( 0, & param );
798 if ( param.sched_priority < max_priority ) {
799 param.sched_priority += 1;
800 rc = sched_setscheduler( 0, sched, & param );
801 if ( rc != 0 ) {
802 int error = errno;
803 __kmp_msg(
804 kmp_ms_warning,
805 KMP_MSG( CantChangeMonitorPriority ),
806 KMP_ERR( error ),
807 KMP_MSG( MonitorWillStarve ),
808 __kmp_msg_null
809 );
810 }; // if
811 } else {
812 // We cannot abort here, because number of CPUs may be enough for all the threads,
813 // including the monitor thread, so application could potentially work...
814 __kmp_msg(
815 kmp_ms_warning,
816 KMP_MSG( RunningAtMaxPriority ),
817 KMP_MSG( MonitorWillStarve ),
818 KMP_HNT( RunningAtMaxPriority ),
819 __kmp_msg_null
820 );
821 }; // if
822 }; // if
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000823 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 +0000824 }
825 #endif // KMP_REAL_TIME_FIX
826
827 KMP_MB(); /* Flush all pending memory write invalidates. */
828
829 if ( __kmp_monitor_wakeups == 1 ) {
830 interval.tv_sec = 1;
831 interval.tv_nsec = 0;
832 } else {
833 interval.tv_sec = 0;
834 interval.tv_nsec = (NSEC_PER_SEC / __kmp_monitor_wakeups);
835 }
836
837 KA_TRACE( 10, ("__kmp_launch_monitor: #2 monitor\n" ) );
838
839 if (__kmp_yield_cycle) {
840 __kmp_yielding_on = 0; /* Start out with yielding shut off */
841 yield_count = __kmp_yield_off_count;
842 } else {
843 __kmp_yielding_on = 1; /* Yielding is on permanently */
844 }
845
846 while( ! TCR_4( __kmp_global.g.g_done ) ) {
847 struct timespec now;
848 struct timeval tval;
849
850 /* This thread monitors the state of the system */
851
852 KA_TRACE( 15, ( "__kmp_launch_monitor: update\n" ) );
853
854 status = gettimeofday( &tval, NULL );
855 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
856 TIMEVAL_TO_TIMESPEC( &tval, &now );
857
858 now.tv_sec += interval.tv_sec;
859 now.tv_nsec += interval.tv_nsec;
860
861 if (now.tv_nsec >= NSEC_PER_SEC) {
862 now.tv_sec += 1;
863 now.tv_nsec -= NSEC_PER_SEC;
864 }
865
866 status = pthread_mutex_lock( & __kmp_wait_mx.m_mutex );
867 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie07ea89f2014-09-03 11:10:54 +0000868 // AC: the monitor should not fall asleep if g_done has been set
869 if ( !TCR_4(__kmp_global.g.g_done) ) { // check once more under mutex
870 status = pthread_cond_timedwait( &__kmp_wait_cv.c_cond, &__kmp_wait_mx.m_mutex, &now );
871 if ( status != 0 ) {
872 if ( status != ETIMEDOUT && status != EINTR ) {
873 KMP_SYSFAIL( "pthread_cond_timedwait", status );
874 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000875 };
876 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000877 status = pthread_mutex_unlock( & __kmp_wait_mx.m_mutex );
878 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
879
880 if (__kmp_yield_cycle) {
881 yield_cycles++;
882 if ( (yield_cycles % yield_count) == 0 ) {
883 if (__kmp_yielding_on) {
884 __kmp_yielding_on = 0; /* Turn it off now */
885 yield_count = __kmp_yield_off_count;
886 } else {
887 __kmp_yielding_on = 1; /* Turn it on now */
888 yield_count = __kmp_yield_on_count;
889 }
890 yield_cycles = 0;
891 }
892 } else {
893 __kmp_yielding_on = 1;
894 }
895
896 TCW_4( __kmp_global.g.g_time.dt.t_value,
897 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
898
899 KMP_MB(); /* Flush all pending memory write invalidates. */
900 }
901
902 KA_TRACE( 10, ("__kmp_launch_monitor: #3 cleanup\n" ) );
903
904#ifdef KMP_BLOCK_SIGNALS
905 status = sigfillset( & new_set );
906 KMP_CHECK_SYSFAIL_ERRNO( "sigfillset", status );
907 status = pthread_sigmask( SIG_UNBLOCK, & new_set, NULL );
908 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
909#endif /* KMP_BLOCK_SIGNALS */
910
911 KA_TRACE( 10, ("__kmp_launch_monitor: #4 finished\n" ) );
912
913 if( __kmp_global.g.g_abort != 0 ) {
914 /* now we need to terminate the worker threads */
915 /* the value of t_abort is the signal we caught */
916
917 int gtid;
918
919 KA_TRACE( 10, ("__kmp_launch_monitor: #5 terminate sig=%d\n", __kmp_global.g.g_abort ) );
920
921 /* terminate the OpenMP worker threads */
922 /* TODO this is not valid for sibling threads!!
923 * the uber master might not be 0 anymore.. */
924 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
925 __kmp_terminate_thread( gtid );
926
927 __kmp_cleanup();
928
929 KA_TRACE( 10, ("__kmp_launch_monitor: #6 raise sig=%d\n", __kmp_global.g.g_abort ) );
930
931 if (__kmp_global.g.g_abort > 0)
932 raise( __kmp_global.g.g_abort );
933
934 }
935
936 KA_TRACE( 10, ("__kmp_launch_monitor: #7 exit\n" ) );
937
938 return thr;
939}
940
941void
942__kmp_create_worker( int gtid, kmp_info_t *th, size_t stack_size )
943{
944 pthread_t handle;
945 pthread_attr_t thread_attr;
946 int status;
947
948
949 th->th.th_info.ds.ds_gtid = gtid;
950
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000951#if KMP_STATS_ENABLED
952 // sets up worker thread stats
953 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
954
955 // th->th.th_stats is used to transfer thread specific stats-pointer to __kmp_launch_worker
956 // So when thread is created (goes into __kmp_launch_worker) it will
957 // set it's __thread local pointer to th->th.th_stats
958 th->th.th_stats = __kmp_stats_list.push_back(gtid);
959 if(KMP_UBER_GTID(gtid)) {
960 __kmp_stats_start_time = tsc_tick_count::now();
961 __kmp_stats_thread_ptr = th->th.th_stats;
962 __kmp_stats_init();
963 KMP_START_EXPLICIT_TIMER(OMP_serial);
964 KMP_START_EXPLICIT_TIMER(OMP_start_end);
965 }
966 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
967
968#endif // KMP_STATS_ENABLED
969
Jim Cownie5e8470a2013-09-27 10:38:44 +0000970 if ( KMP_UBER_GTID(gtid) ) {
971 KA_TRACE( 10, ("__kmp_create_worker: uber thread (%d)\n", gtid ) );
972 th -> th.th_info.ds.ds_thread = pthread_self();
973 __kmp_set_stack_info( gtid, th );
974 __kmp_check_stack_overlap( th );
975 return;
976 }; // if
977
978 KA_TRACE( 10, ("__kmp_create_worker: try to create thread (%d)\n", gtid ) );
979
980 KMP_MB(); /* Flush all pending memory write invalidates. */
981
982#ifdef KMP_THREAD_ATTR
983 {
984 status = pthread_attr_init( &thread_attr );
985 if ( status != 0 ) {
986 __kmp_msg(
987 kmp_ms_fatal,
988 KMP_MSG( CantInitThreadAttrs ),
989 KMP_ERR( status ),
990 __kmp_msg_null
991 );
992 }; // if
993 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
994 if ( status != 0 ) {
995 __kmp_msg(
996 kmp_ms_fatal,
997 KMP_MSG( CantSetWorkerState ),
998 KMP_ERR( status ),
999 __kmp_msg_null
1000 );
1001 }; // if
1002
1003 /* Set stack size for this thread now. */
1004 stack_size += gtid * __kmp_stkoffset;
1005
1006 KA_TRACE( 10, ( "__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
1007 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
1008 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size ) );
1009
1010# ifdef _POSIX_THREAD_ATTR_STACKSIZE
1011 status = pthread_attr_setstacksize( & thread_attr, stack_size );
1012# ifdef KMP_BACKUP_STKSIZE
1013 if ( status != 0 ) {
1014 if ( ! __kmp_env_stksize ) {
1015 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
1016 __kmp_stksize = KMP_BACKUP_STKSIZE;
1017 KA_TRACE( 10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
1018 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
1019 "bytes\n",
1020 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size )
1021 );
1022 status = pthread_attr_setstacksize( &thread_attr, stack_size );
1023 }; // if
1024 }; // if
1025# endif /* KMP_BACKUP_STKSIZE */
1026 if ( status != 0 ) {
1027 __kmp_msg(
1028 kmp_ms_fatal,
1029 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1030 KMP_ERR( status ),
1031 KMP_HNT( ChangeWorkerStackSize ),
1032 __kmp_msg_null
1033 );
1034 }; // if
1035# endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1036 }
1037#endif /* KMP_THREAD_ATTR */
1038
1039 {
1040 status = pthread_create( & handle, & thread_attr, __kmp_launch_worker, (void *) th );
1041 if ( status != 0 || ! handle ) { // ??? Why do we check handle??
1042#ifdef _POSIX_THREAD_ATTR_STACKSIZE
1043 if ( status == EINVAL ) {
1044 __kmp_msg(
1045 kmp_ms_fatal,
1046 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1047 KMP_ERR( status ),
1048 KMP_HNT( IncreaseWorkerStackSize ),
1049 __kmp_msg_null
1050 );
1051 };
1052 if ( status == ENOMEM ) {
1053 __kmp_msg(
1054 kmp_ms_fatal,
1055 KMP_MSG( CantSetWorkerStackSize, stack_size ),
1056 KMP_ERR( status ),
1057 KMP_HNT( DecreaseWorkerStackSize ),
1058 __kmp_msg_null
1059 );
1060 };
1061#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1062 if ( status == EAGAIN ) {
1063 __kmp_msg(
1064 kmp_ms_fatal,
1065 KMP_MSG( NoResourcesForWorkerThread ),
1066 KMP_ERR( status ),
1067 KMP_HNT( Decrease_NUM_THREADS ),
1068 __kmp_msg_null
1069 );
1070 }; // if
1071 KMP_SYSFAIL( "pthread_create", status );
1072 }; // if
1073
1074 th->th.th_info.ds.ds_thread = handle;
1075 }
1076
1077#ifdef KMP_THREAD_ATTR
1078 {
1079 status = pthread_attr_destroy( & thread_attr );
1080 if ( status ) {
1081 __kmp_msg(
1082 kmp_ms_warning,
1083 KMP_MSG( CantDestroyThreadAttrs ),
1084 KMP_ERR( status ),
1085 __kmp_msg_null
1086 );
1087 }; // if
1088 }
1089#endif /* KMP_THREAD_ATTR */
1090
1091 KMP_MB(); /* Flush all pending memory write invalidates. */
1092
1093 KA_TRACE( 10, ("__kmp_create_worker: done creating thread (%d)\n", gtid ) );
1094
1095} // __kmp_create_worker
1096
1097
1098void
1099__kmp_create_monitor( kmp_info_t *th )
1100{
1101 pthread_t handle;
1102 pthread_attr_t thread_attr;
1103 size_t size;
1104 int status;
1105 int caller_gtid = __kmp_get_gtid();
1106 int auto_adj_size = FALSE;
1107
1108 KA_TRACE( 10, ("__kmp_create_monitor: try to create monitor\n" ) );
1109
1110 KMP_MB(); /* Flush all pending memory write invalidates. */
1111
1112 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1113 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1114 #if KMP_REAL_TIME_FIX
1115 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 +00001116 #else
1117 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001118 #endif // KMP_REAL_TIME_FIX
1119
1120 #ifdef KMP_THREAD_ATTR
1121 if ( __kmp_monitor_stksize == 0 ) {
1122 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1123 auto_adj_size = TRUE;
1124 }
1125 status = pthread_attr_init( &thread_attr );
1126 if ( status != 0 ) {
1127 __kmp_msg(
1128 kmp_ms_fatal,
1129 KMP_MSG( CantInitThreadAttrs ),
1130 KMP_ERR( status ),
1131 __kmp_msg_null
1132 );
1133 }; // if
1134 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1135 if ( status != 0 ) {
1136 __kmp_msg(
1137 kmp_ms_fatal,
1138 KMP_MSG( CantSetMonitorState ),
1139 KMP_ERR( status ),
1140 __kmp_msg_null
1141 );
1142 }; // if
1143
1144 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1145 status = pthread_attr_getstacksize( & thread_attr, & size );
1146 KMP_CHECK_SYSFAIL( "pthread_attr_getstacksize", status );
1147 #else
1148 size = __kmp_sys_min_stksize;
1149 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1150 #endif /* KMP_THREAD_ATTR */
1151
1152 if ( __kmp_monitor_stksize == 0 ) {
1153 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1154 }
1155 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1156 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1157 }
1158
1159 KA_TRACE( 10, ( "__kmp_create_monitor: default stacksize = %lu bytes,"
1160 "requested stacksize = %lu bytes\n",
1161 size, __kmp_monitor_stksize ) );
1162
1163 retry:
1164
1165 /* Set stack size for this thread now. */
1166
1167 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1168 KA_TRACE( 10, ( "__kmp_create_monitor: setting stacksize = %lu bytes,",
1169 __kmp_monitor_stksize ) );
1170 status = pthread_attr_setstacksize( & thread_attr, __kmp_monitor_stksize );
1171 if ( status != 0 ) {
1172 if ( auto_adj_size ) {
1173 __kmp_monitor_stksize *= 2;
1174 goto retry;
1175 }
1176 __kmp_msg(
1177 kmp_ms_warning, // should this be fatal? BB
1178 KMP_MSG( CantSetMonitorStackSize, (long int) __kmp_monitor_stksize ),
1179 KMP_ERR( status ),
1180 KMP_HNT( ChangeMonitorStackSize ),
1181 __kmp_msg_null
1182 );
1183 }; // if
1184 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1185
Jim Cownie5e8470a2013-09-27 10:38:44 +00001186 status = pthread_create( &handle, & thread_attr, __kmp_launch_monitor, (void *) th );
1187
1188 if ( status != 0 ) {
1189 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1190 if ( status == EINVAL ) {
1191 if ( auto_adj_size && ( __kmp_monitor_stksize < (size_t)0x40000000 ) ) {
1192 __kmp_monitor_stksize *= 2;
1193 goto retry;
1194 }
1195 __kmp_msg(
1196 kmp_ms_fatal,
1197 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1198 KMP_ERR( status ),
1199 KMP_HNT( IncreaseMonitorStackSize ),
1200 __kmp_msg_null
1201 );
1202 }; // if
1203 if ( status == ENOMEM ) {
1204 __kmp_msg(
1205 kmp_ms_fatal,
1206 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1207 KMP_ERR( status ),
1208 KMP_HNT( DecreaseMonitorStackSize ),
1209 __kmp_msg_null
1210 );
1211 }; // if
1212 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1213 if ( status == EAGAIN ) {
1214 __kmp_msg(
1215 kmp_ms_fatal,
1216 KMP_MSG( NoResourcesForMonitorThread ),
1217 KMP_ERR( status ),
1218 KMP_HNT( DecreaseNumberOfThreadsInUse ),
1219 __kmp_msg_null
1220 );
1221 }; // if
1222 KMP_SYSFAIL( "pthread_create", status );
1223 }; // if
1224
1225 th->th.th_info.ds.ds_thread = handle;
1226
1227 #if KMP_REAL_TIME_FIX
1228 // Wait for the monitor thread is really started and set its *priority*.
1229 KMP_DEBUG_ASSERT( sizeof( kmp_uint32 ) == sizeof( __kmp_global.g.g_time.dt.t_value ) );
1230 __kmp_wait_yield_4(
1231 (kmp_uint32 volatile *) & __kmp_global.g.g_time.dt.t_value, -1, & __kmp_neq_4, NULL
1232 );
1233 #endif // KMP_REAL_TIME_FIX
1234
1235 #ifdef KMP_THREAD_ATTR
1236 status = pthread_attr_destroy( & thread_attr );
1237 if ( status != 0 ) {
1238 __kmp_msg( //
1239 kmp_ms_warning,
1240 KMP_MSG( CantDestroyThreadAttrs ),
1241 KMP_ERR( status ),
1242 __kmp_msg_null
1243 );
1244 }; // if
1245 #endif
1246
1247 KMP_MB(); /* Flush all pending memory write invalidates. */
1248
1249 KA_TRACE( 10, ( "__kmp_create_monitor: monitor created %#.8lx\n", th->th.th_info.ds.ds_thread ) );
1250
1251} // __kmp_create_monitor
1252
1253void
1254__kmp_exit_thread(
1255 int exit_status
1256) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001257 pthread_exit( (void *)(intptr_t) exit_status );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001258} // __kmp_exit_thread
1259
Jim Cownie07ea89f2014-09-03 11:10:54 +00001260void __kmp_resume_monitor();
1261
Jim Cownie5e8470a2013-09-27 10:38:44 +00001262void
1263__kmp_reap_monitor( kmp_info_t *th )
1264{
1265 int status, i;
1266 void *exit_val;
1267
1268 KA_TRACE( 10, ("__kmp_reap_monitor: try to reap monitor thread with handle %#.8lx\n",
1269 th->th.th_info.ds.ds_thread ) );
1270
1271 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1272 // If both tid and gtid are 0, it means the monitor did not ever start.
1273 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1274 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1275 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
1276 return;
1277 }; // if
1278
1279 KMP_MB(); /* Flush all pending memory write invalidates. */
1280
1281
1282 /* First, check to see whether the monitor thread exists. This could prevent a hang,
1283 but if the monitor dies after the pthread_kill call and before the pthread_join
1284 call, it will still hang. */
1285
1286 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1287 if (status == ESRCH) {
1288
1289 KA_TRACE( 10, ("__kmp_reap_monitor: monitor does not exist, returning\n") );
1290
1291 } else
1292 {
Jim Cownie07ea89f2014-09-03 11:10:54 +00001293 __kmp_resume_monitor(); // Wake up the monitor thread
Jim Cownie5e8470a2013-09-27 10:38:44 +00001294 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1295 if (exit_val != th) {
1296 __kmp_msg(
1297 kmp_ms_fatal,
1298 KMP_MSG( ReapMonitorError ),
1299 KMP_ERR( status ),
1300 __kmp_msg_null
1301 );
1302 }
1303 }
1304
1305 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1306 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1307
1308 KA_TRACE( 10, ("__kmp_reap_monitor: done reaping monitor thread with handle %#.8lx\n",
1309 th->th.th_info.ds.ds_thread ) );
1310
1311 KMP_MB(); /* Flush all pending memory write invalidates. */
1312
1313}
1314
1315void
1316__kmp_reap_worker( kmp_info_t *th )
1317{
1318 int status;
1319 void *exit_val;
1320
1321 KMP_MB(); /* Flush all pending memory write invalidates. */
1322
1323 KA_TRACE( 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid ) );
1324
1325 /* First, check to see whether the worker thread exists. This could prevent a hang,
1326 but if the worker dies after the pthread_kill call and before the pthread_join
1327 call, it will still hang. */
1328
1329 {
1330 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1331 if (status == ESRCH) {
1332 KA_TRACE( 10, ("__kmp_reap_worker: worker T#%d does not exist, returning\n",
1333 th->th.th_info.ds.ds_gtid ) );
1334 }
1335 else {
1336 KA_TRACE( 10, ("__kmp_reap_worker: try to join with worker T#%d\n",
1337 th->th.th_info.ds.ds_gtid ) );
1338
1339 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1340#ifdef KMP_DEBUG
1341 /* Don't expose these to the user until we understand when they trigger */
1342 if ( status != 0 ) {
1343 __kmp_msg(
1344 kmp_ms_fatal,
1345 KMP_MSG( ReapWorkerError ),
1346 KMP_ERR( status ),
1347 __kmp_msg_null
1348 );
1349 }
1350 if ( exit_val != th ) {
1351 KA_TRACE( 10, ( "__kmp_reap_worker: worker T#%d did not reap properly, "
1352 "exit_val = %p\n",
1353 th->th.th_info.ds.ds_gtid, exit_val ) );
1354 }
1355#endif /* KMP_DEBUG */
1356 }
1357 }
1358
1359 KA_TRACE( 10, ("__kmp_reap_worker: done reaping T#%d\n", th->th.th_info.ds.ds_gtid ) );
1360
1361 KMP_MB(); /* Flush all pending memory write invalidates. */
1362}
1363
1364
1365/* ------------------------------------------------------------------------ */
1366/* ------------------------------------------------------------------------ */
1367
1368#if KMP_HANDLE_SIGNALS
1369
1370
1371static void
1372__kmp_null_handler( int signo )
1373{
1374 // Do nothing, for doing SIG_IGN-type actions.
1375} // __kmp_null_handler
1376
1377
1378static void
1379__kmp_team_handler( int signo )
1380{
1381 if ( __kmp_global.g.g_abort == 0 ) {
1382 /* Stage 1 signal handler, let's shut down all of the threads */
1383 #ifdef KMP_DEBUG
1384 __kmp_debug_printf( "__kmp_team_handler: caught signal = %d\n", signo );
1385 #endif
1386 switch ( signo ) {
1387 case SIGHUP :
1388 case SIGINT :
1389 case SIGQUIT :
1390 case SIGILL :
1391 case SIGABRT :
1392 case SIGFPE :
1393 case SIGBUS :
1394 case SIGSEGV :
1395 #ifdef SIGSYS
1396 case SIGSYS :
1397 #endif
1398 case SIGTERM :
1399 if ( __kmp_debug_buf ) {
1400 __kmp_dump_debug_buffer( );
1401 }; // if
1402 KMP_MB(); // Flush all pending memory write invalidates.
1403 TCW_4( __kmp_global.g.g_abort, signo );
1404 KMP_MB(); // Flush all pending memory write invalidates.
1405 TCW_4( __kmp_global.g.g_done, TRUE );
1406 KMP_MB(); // Flush all pending memory write invalidates.
1407 break;
1408 default:
1409 #ifdef KMP_DEBUG
1410 __kmp_debug_printf( "__kmp_team_handler: unknown signal type" );
1411 #endif
1412 break;
1413 }; // switch
1414 }; // if
1415} // __kmp_team_handler
1416
1417
1418static
1419void __kmp_sigaction( int signum, const struct sigaction * act, struct sigaction * oldact ) {
1420 int rc = sigaction( signum, act, oldact );
1421 KMP_CHECK_SYSFAIL_ERRNO( "sigaction", rc );
1422}
1423
1424
1425static void
1426__kmp_install_one_handler( int sig, sig_func_t handler_func, int parallel_init )
1427{
1428 KMP_MB(); // Flush all pending memory write invalidates.
1429 KB_TRACE( 60, ( "__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init ) );
1430 if ( parallel_init ) {
1431 struct sigaction new_action;
1432 struct sigaction old_action;
1433 new_action.sa_handler = handler_func;
1434 new_action.sa_flags = 0;
1435 sigfillset( & new_action.sa_mask );
1436 __kmp_sigaction( sig, & new_action, & old_action );
1437 if ( old_action.sa_handler == __kmp_sighldrs[ sig ].sa_handler ) {
1438 sigaddset( & __kmp_sigset, sig );
1439 } else {
1440 // Restore/keep user's handler if one previously installed.
1441 __kmp_sigaction( sig, & old_action, NULL );
1442 }; // if
1443 } else {
1444 // Save initial/system signal handlers to see if user handlers installed.
1445 __kmp_sigaction( sig, NULL, & __kmp_sighldrs[ sig ] );
1446 }; // if
1447 KMP_MB(); // Flush all pending memory write invalidates.
1448} // __kmp_install_one_handler
1449
1450
1451static void
1452__kmp_remove_one_handler( int sig )
1453{
1454 KB_TRACE( 60, ( "__kmp_remove_one_handler( %d )\n", sig ) );
1455 if ( sigismember( & __kmp_sigset, sig ) ) {
1456 struct sigaction old;
1457 KMP_MB(); // Flush all pending memory write invalidates.
1458 __kmp_sigaction( sig, & __kmp_sighldrs[ sig ], & old );
1459 if ( ( old.sa_handler != __kmp_team_handler ) && ( old.sa_handler != __kmp_null_handler ) ) {
1460 // Restore the users signal handler.
1461 KB_TRACE( 10, ( "__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1462 __kmp_sigaction( sig, & old, NULL );
1463 }; // if
1464 sigdelset( & __kmp_sigset, sig );
1465 KMP_MB(); // Flush all pending memory write invalidates.
1466 }; // if
1467} // __kmp_remove_one_handler
1468
1469
1470void
1471__kmp_install_signals( int parallel_init )
1472{
1473 KB_TRACE( 10, ( "__kmp_install_signals( %d )\n", parallel_init ) );
1474 if ( __kmp_handle_signals || ! parallel_init ) {
1475 // If ! parallel_init, we do not install handlers, just save original handlers.
1476 // Let us do it even __handle_signals is 0.
1477 sigemptyset( & __kmp_sigset );
1478 __kmp_install_one_handler( SIGHUP, __kmp_team_handler, parallel_init );
1479 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1480 __kmp_install_one_handler( SIGQUIT, __kmp_team_handler, parallel_init );
1481 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1482 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1483 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1484 __kmp_install_one_handler( SIGBUS, __kmp_team_handler, parallel_init );
1485 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1486 #ifdef SIGSYS
1487 __kmp_install_one_handler( SIGSYS, __kmp_team_handler, parallel_init );
1488 #endif // SIGSYS
1489 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1490 #ifdef SIGPIPE
1491 __kmp_install_one_handler( SIGPIPE, __kmp_team_handler, parallel_init );
1492 #endif // SIGPIPE
1493 }; // if
1494} // __kmp_install_signals
1495
1496
1497void
1498__kmp_remove_signals( void )
1499{
1500 int sig;
1501 KB_TRACE( 10, ( "__kmp_remove_signals()\n" ) );
1502 for ( sig = 1; sig < NSIG; ++ sig ) {
1503 __kmp_remove_one_handler( sig );
1504 }; // for sig
1505} // __kmp_remove_signals
1506
1507
1508#endif // KMP_HANDLE_SIGNALS
1509
1510/* ------------------------------------------------------------------------ */
1511/* ------------------------------------------------------------------------ */
1512
1513void
1514__kmp_enable( int new_state )
1515{
1516 #ifdef KMP_CANCEL_THREADS
1517 int status, old_state;
1518 status = pthread_setcancelstate( new_state, & old_state );
1519 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1520 KMP_DEBUG_ASSERT( old_state == PTHREAD_CANCEL_DISABLE );
1521 #endif
1522}
1523
1524void
1525__kmp_disable( int * old_state )
1526{
1527 #ifdef KMP_CANCEL_THREADS
1528 int status;
1529 status = pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, old_state );
1530 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1531 #endif
1532}
1533
1534/* ------------------------------------------------------------------------ */
1535/* ------------------------------------------------------------------------ */
1536
1537static void
1538__kmp_atfork_prepare (void)
1539{
1540 /* nothing to do */
1541}
1542
1543static void
1544__kmp_atfork_parent (void)
1545{
1546 /* nothing to do */
1547}
1548
1549/*
1550 Reset the library so execution in the child starts "all over again" with
1551 clean data structures in initial states. Don't worry about freeing memory
1552 allocated by parent, just abandon it to be safe.
1553*/
1554static void
1555__kmp_atfork_child (void)
1556{
1557 /* TODO make sure this is done right for nested/sibling */
1558 // ATT: Memory leaks are here? TODO: Check it and fix.
1559 /* KMP_ASSERT( 0 ); */
1560
1561 ++__kmp_fork_count;
1562
1563 __kmp_init_runtime = FALSE;
1564 __kmp_init_monitor = 0;
1565 __kmp_init_parallel = FALSE;
1566 __kmp_init_middle = FALSE;
1567 __kmp_init_serial = FALSE;
1568 TCW_4(__kmp_init_gtid, FALSE);
1569 __kmp_init_common = FALSE;
1570
1571 TCW_4(__kmp_init_user_locks, FALSE);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001572 __kmp_user_lock_table.used = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001573 __kmp_user_lock_table.allocated = 0;
1574 __kmp_user_lock_table.table = NULL;
1575 __kmp_lock_blocks = NULL;
1576
1577 __kmp_all_nth = 0;
1578 TCW_4(__kmp_nth, 0);
1579
1580 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate here
1581 so threadprivate doesn't use stale data */
1582 KA_TRACE( 10, ( "__kmp_atfork_child: checking cache address list %p\n",
1583 __kmp_threadpriv_cache_list ) );
1584
1585 while ( __kmp_threadpriv_cache_list != NULL ) {
1586
1587 if ( *__kmp_threadpriv_cache_list -> addr != NULL ) {
1588 KC_TRACE( 50, ( "__kmp_atfork_child: zeroing cache at address %p\n",
1589 &(*__kmp_threadpriv_cache_list -> addr) ) );
1590
1591 *__kmp_threadpriv_cache_list -> addr = NULL;
1592 }
1593 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list -> next;
1594 }
1595
1596 __kmp_init_runtime = FALSE;
1597
1598 /* reset statically initialized locks */
1599 __kmp_init_bootstrap_lock( &__kmp_initz_lock );
1600 __kmp_init_bootstrap_lock( &__kmp_stdio_lock );
1601 __kmp_init_bootstrap_lock( &__kmp_console_lock );
1602
1603 /* This is necessary to make sure no stale data is left around */
1604 /* AC: customers complain that we use unsafe routines in the atfork
1605 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1606 in dynamic_link when check the presence of shared tbbmalloc library.
1607 Suggestion is to make the library initialization lazier, similar
1608 to what done for __kmpc_begin(). */
1609 // TODO: synchronize all static initializations with regular library
1610 // startup; look at kmp_global.c and etc.
1611 //__kmp_internal_begin ();
1612
1613}
1614
1615void
1616__kmp_register_atfork(void) {
1617 if ( __kmp_need_register_atfork ) {
1618 int status = pthread_atfork( __kmp_atfork_prepare, __kmp_atfork_parent, __kmp_atfork_child );
1619 KMP_CHECK_SYSFAIL( "pthread_atfork", status );
1620 __kmp_need_register_atfork = FALSE;
1621 }
1622}
1623
1624void
1625__kmp_suspend_initialize( void )
1626{
1627 int status;
1628 status = pthread_mutexattr_init( &__kmp_suspend_mutex_attr );
1629 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
1630 status = pthread_condattr_init( &__kmp_suspend_cond_attr );
1631 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
1632}
1633
1634static void
1635__kmp_suspend_initialize_thread( kmp_info_t *th )
1636{
1637 if ( th->th.th_suspend_init_count <= __kmp_fork_count ) {
1638 /* this means we haven't initialized the suspension pthread objects for this thread
1639 in this instance of the process */
1640 int status;
1641 status = pthread_cond_init( &th->th.th_suspend_cv.c_cond, &__kmp_suspend_cond_attr );
1642 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
1643 status = pthread_mutex_init( &th->th.th_suspend_mx.m_mutex, & __kmp_suspend_mutex_attr );
1644 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
1645 *(volatile int*)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1646 };
1647}
1648
1649void
1650__kmp_suspend_uninitialize_thread( kmp_info_t *th )
1651{
1652 if(th->th.th_suspend_init_count > __kmp_fork_count) {
1653 /* this means we have initialize the suspension pthread objects for this thread
1654 in this instance of the process */
1655 int status;
1656
1657 status = pthread_cond_destroy( &th->th.th_suspend_cv.c_cond );
1658 if ( status != 0 && status != EBUSY ) {
1659 KMP_SYSFAIL( "pthread_cond_destroy", status );
1660 };
1661 status = pthread_mutex_destroy( &th->th.th_suspend_mx.m_mutex );
1662 if ( status != 0 && status != EBUSY ) {
1663 KMP_SYSFAIL( "pthread_mutex_destroy", status );
1664 };
1665 --th->th.th_suspend_init_count;
1666 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1667 }
1668}
1669
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001670/* This routine puts the calling thread to sleep after setting the
1671 * sleep bit for the indicated flag variable to true.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001672 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001673template <class C>
1674static inline void __kmp_suspend_template( int th_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001675{
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001676 KMP_TIME_BLOCK(USER_suspend);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001677 kmp_info_t *th = __kmp_threads[th_gtid];
1678 int status;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001679 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001680
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001681 KF_TRACE( 30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001682
1683 __kmp_suspend_initialize_thread( th );
1684
1685 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1686 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1687
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001688 KF_TRACE( 10, ( "__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1689 th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001690
1691 /* TODO: shouldn't this use release semantics to ensure that __kmp_suspend_initialize_thread
1692 gets called first?
1693 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001694 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001695
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001696 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%d\n",
1697 th_gtid, flag->get(), *(flag->get()) ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001698
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001699 if ( flag->done_check_val(old_spin) ) {
1700 old_spin = flag->unset_sleeping();
1701 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d false alarm, reset sleep bit for spin(%p)\n",
1702 th_gtid, flag->get()) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001703 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001704 /* Encapsulate in a loop as the documentation states that this may
1705 * "with low probability" return when the condition variable has
1706 * not been signaled or broadcast
1707 */
1708 int deactivated = FALSE;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001709 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1710 while ( flag->is_sleeping() ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001711#ifdef DEBUG_SUSPEND
1712 char buffer[128];
1713 __kmp_suspend_count++;
1714 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001715 __kmp_printf( "__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001716#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001717 // Mark the thread as no longer active (only in the first iteration of the loop).
Jim Cownie5e8470a2013-09-27 10:38:44 +00001718 if ( ! deactivated ) {
1719 th->th.th_active = FALSE;
1720 if ( th->th.th_active_in_pool ) {
1721 th->th.th_active_in_pool = FALSE;
1722 KMP_TEST_THEN_DEC32(
1723 (kmp_int32 *) &__kmp_thread_pool_active_nth );
1724 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
1725 }
1726 deactivated = TRUE;
1727
1728
1729 }
1730
1731#if USE_SUSPEND_TIMEOUT
1732 struct timespec now;
1733 struct timeval tval;
1734 int msecs;
1735
1736 status = gettimeofday( &tval, NULL );
1737 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1738 TIMEVAL_TO_TIMESPEC( &tval, &now );
1739
1740 msecs = (4*__kmp_dflt_blocktime) + 200;
1741 now.tv_sec += msecs / 1000;
1742 now.tv_nsec += (msecs % 1000)*1000;
1743
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001744 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_timedwait\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001745 th_gtid ) );
1746 status = pthread_cond_timedwait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex, & now );
1747#else
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001748 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_wait\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001749 th_gtid ) );
1750
1751 status = pthread_cond_wait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex );
1752#endif
1753
1754 if ( (status != 0) && (status != EINTR) && (status != ETIMEDOUT) ) {
1755 KMP_SYSFAIL( "pthread_cond_wait", status );
1756 }
1757#ifdef KMP_DEBUG
1758 if (status == ETIMEDOUT) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001759 if ( flag->is_sleeping() ) {
1760 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001761 } else {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001762 KF_TRACE( 2, ( "__kmp_suspend_template: T#%d timeout wakeup, sleep bit not set!\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001763 th_gtid ) );
1764 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001765 } else if ( flag->is_sleeping() ) {
1766 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001767 }
1768#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001769 } // while
1770
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001771 // Mark the thread as active again (if it was previous marked as inactive)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001772 if ( deactivated ) {
1773 th->th.th_active = TRUE;
1774 if ( TCR_4(th->th.th_in_pool) ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001775 KMP_TEST_THEN_INC32( (kmp_int32 *) &__kmp_thread_pool_active_nth );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001776 th->th.th_active_in_pool = TRUE;
1777 }
1778 }
1779 }
1780
1781#ifdef DEBUG_SUSPEND
1782 {
1783 char buffer[128];
1784 __kmp_print_cond( buffer, &th->th.th_suspend_cv);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001785 __kmp_printf( "__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001786 }
1787#endif
1788
1789
1790 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1791 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1792
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001793 KF_TRACE( 30, ("__kmp_suspend_template: T#%d exit\n", th_gtid ) );
1794}
1795
1796void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
1797 __kmp_suspend_template(th_gtid, flag);
1798}
1799void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
1800 __kmp_suspend_template(th_gtid, flag);
1801}
1802void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
1803 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001804}
1805
1806
1807/* This routine signals the thread specified by target_gtid to wake up
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001808 * after setting the sleep bit indicated by the flag argument to FALSE.
1809 * The target thread must already have called __kmp_suspend_template()
Jim Cownie5e8470a2013-09-27 10:38:44 +00001810 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001811template <class C>
1812static inline void __kmp_resume_template( int target_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001813{
1814 kmp_info_t *th = __kmp_threads[target_gtid];
1815 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001816
1817#ifdef KMP_DEBUG
1818 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1819#endif
1820
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001821 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 +00001822 KMP_DEBUG_ASSERT( gtid != target_gtid );
1823
1824 __kmp_suspend_initialize_thread( th );
1825
1826 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1827 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001828
1829 if (!flag) {
1830 flag = (C *)th->th.th_sleep_loc;
1831 }
1832
1833 if (!flag) {
1834 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p)\n",
1835 gtid, target_gtid, NULL ) );
1836 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1837 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1838 return;
1839 }
1840 else {
1841 typename C::flag_t old_spin = flag->unset_sleeping();
1842 if ( ! flag->is_sleeping_val(old_spin) ) {
1843 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p): "
1844 "%u => %u\n",
1845 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001846
1847 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1848 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1849 return;
1850 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001851 KF_TRACE( 5, ( "__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p): "
1852 "%u => %u\n",
1853 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001854 }
1855 TCW_PTR(th->th.th_sleep_loc, NULL);
1856
Jim Cownie5e8470a2013-09-27 10:38:44 +00001857
1858#ifdef DEBUG_SUSPEND
1859 {
1860 char buffer[128];
1861 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001862 __kmp_printf( "__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, target_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001863 }
1864#endif
1865
1866
1867 status = pthread_cond_signal( &th->th.th_suspend_cv.c_cond );
1868 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1869 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1870 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001871 KF_TRACE( 30, ( "__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001872 gtid, target_gtid ) );
1873}
1874
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001875void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
1876 __kmp_resume_template(target_gtid, flag);
1877}
1878void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
1879 __kmp_resume_template(target_gtid, flag);
1880}
1881void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
1882 __kmp_resume_template(target_gtid, flag);
1883}
1884
Jim Cownie07ea89f2014-09-03 11:10:54 +00001885void
1886__kmp_resume_monitor()
1887{
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001888 KMP_TIME_BLOCK(USER_resume);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001889 int status;
1890#ifdef KMP_DEBUG
1891 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1892 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n",
1893 gtid, KMP_GTID_MONITOR ) );
1894 KMP_DEBUG_ASSERT( gtid != KMP_GTID_MONITOR );
1895#endif
1896 status = pthread_mutex_lock( &__kmp_wait_mx.m_mutex );
1897 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1898#ifdef DEBUG_SUSPEND
1899 {
1900 char buffer[128];
1901 __kmp_print_cond( buffer, &__kmp_wait_cv.c_cond );
1902 __kmp_printf( "__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid, KMP_GTID_MONITOR, buffer );
1903 }
1904#endif
1905 status = pthread_cond_signal( &__kmp_wait_cv.c_cond );
1906 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1907 status = pthread_mutex_unlock( &__kmp_wait_mx.m_mutex );
1908 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1909 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d exiting after signaling wake up for T#%d\n",
1910 gtid, KMP_GTID_MONITOR ) );
1911}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001912
1913/* ------------------------------------------------------------------------ */
1914/* ------------------------------------------------------------------------ */
1915
1916void
1917__kmp_yield( int cond )
1918{
1919 if (cond && __kmp_yielding_on) {
1920 sched_yield();
1921 }
1922}
1923
1924/* ------------------------------------------------------------------------ */
1925/* ------------------------------------------------------------------------ */
1926
1927void
1928__kmp_gtid_set_specific( int gtid )
1929{
1930 int status;
1931 KMP_ASSERT( __kmp_init_runtime );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001932 status = pthread_setspecific( __kmp_gtid_threadprivate_key, (void*)(intptr_t)(gtid+1) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001933 KMP_CHECK_SYSFAIL( "pthread_setspecific", status );
1934}
1935
1936int
1937__kmp_gtid_get_specific()
1938{
1939 int gtid;
1940 if ( !__kmp_init_runtime ) {
1941 KA_TRACE( 50, ("__kmp_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
1942 return KMP_GTID_SHUTDOWN;
1943 }
1944 gtid = (int)(size_t)pthread_getspecific( __kmp_gtid_threadprivate_key );
1945 if ( gtid == 0 ) {
1946 gtid = KMP_GTID_DNE;
1947 }
1948 else {
1949 gtid--;
1950 }
1951 KA_TRACE( 50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1952 __kmp_gtid_threadprivate_key, gtid ));
1953 return gtid;
1954}
1955
1956/* ------------------------------------------------------------------------ */
1957/* ------------------------------------------------------------------------ */
1958
1959double
1960__kmp_read_cpu_time( void )
1961{
1962 /*clock_t t;*/
1963 struct tms buffer;
1964
1965 /*t =*/ times( & buffer );
1966
1967 return (buffer.tms_utime + buffer.tms_cutime) / (double) CLOCKS_PER_SEC;
1968}
1969
1970int
1971__kmp_read_system_info( struct kmp_sys_info *info )
1972{
1973 int status;
1974 struct rusage r_usage;
1975
1976 memset( info, 0, sizeof( *info ) );
1977
1978 status = getrusage( RUSAGE_SELF, &r_usage);
1979 KMP_CHECK_SYSFAIL_ERRNO( "getrusage", status );
1980
1981 info->maxrss = r_usage.ru_maxrss; /* the maximum resident set size utilized (in kilobytes) */
1982 info->minflt = r_usage.ru_minflt; /* the number of page faults serviced without any I/O */
1983 info->majflt = r_usage.ru_majflt; /* the number of page faults serviced that required I/O */
1984 info->nswap = r_usage.ru_nswap; /* the number of times a process was "swapped" out of memory */
1985 info->inblock = r_usage.ru_inblock; /* the number of times the file system had to perform input */
1986 info->oublock = r_usage.ru_oublock; /* the number of times the file system had to perform output */
1987 info->nvcsw = r_usage.ru_nvcsw; /* the number of times a context switch was voluntarily */
1988 info->nivcsw = r_usage.ru_nivcsw; /* the number of times a context switch was forced */
1989
1990 return (status != 0);
1991}
1992
1993/* ------------------------------------------------------------------------ */
1994/* ------------------------------------------------------------------------ */
1995
1996
1997void
1998__kmp_read_system_time( double *delta )
1999{
2000 double t_ns;
2001 struct timeval tval;
2002 struct timespec stop;
2003 int status;
2004
2005 status = gettimeofday( &tval, NULL );
2006 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
2007 TIMEVAL_TO_TIMESPEC( &tval, &stop );
2008 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
2009 *delta = (t_ns * 1e-9);
2010}
2011
2012void
2013__kmp_clear_system_time( void )
2014{
2015 struct timeval tval;
2016 int status;
2017 status = gettimeofday( &tval, NULL );
2018 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
2019 TIMEVAL_TO_TIMESPEC( &tval, &__kmp_sys_timer_data.start );
2020}
2021
2022/* ------------------------------------------------------------------------ */
2023/* ------------------------------------------------------------------------ */
2024
2025#ifdef BUILD_TV
2026
2027void
2028__kmp_tv_threadprivate_store( kmp_info_t *th, void *global_addr, void *thread_addr )
2029{
2030 struct tv_data *p;
2031
2032 p = (struct tv_data *) __kmp_allocate( sizeof( *p ) );
2033
2034 p->u.tp.global_addr = global_addr;
2035 p->u.tp.thread_addr = thread_addr;
2036
2037 p->type = (void *) 1;
2038
2039 p->next = th->th.th_local.tv_data;
2040 th->th.th_local.tv_data = p;
2041
2042 if ( p->next == 0 ) {
2043 int rc = pthread_setspecific( __kmp_tv_key, p );
2044 KMP_CHECK_SYSFAIL( "pthread_setspecific", rc );
2045 }
2046}
2047
2048#endif /* BUILD_TV */
2049
2050/* ------------------------------------------------------------------------ */
2051/* ------------------------------------------------------------------------ */
2052
2053static int
2054__kmp_get_xproc( void ) {
2055
2056 int r = 0;
2057
2058 #if KMP_OS_LINUX
2059
2060 r = sysconf( _SC_NPROCESSORS_ONLN );
2061
2062 #elif KMP_OS_DARWIN
2063
2064 // Bug C77011 High "OpenMP Threads and number of active cores".
2065
2066 // Find the number of available CPUs.
2067 kern_return_t rc;
2068 host_basic_info_data_t info;
2069 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
2070 rc = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) & info, & num );
2071 if ( rc == 0 && num == HOST_BASIC_INFO_COUNT ) {
2072 // Cannot use KA_TRACE() here because this code works before trace support is
2073 // initialized.
2074 r = info.avail_cpus;
2075 } else {
2076 KMP_WARNING( CantGetNumAvailCPU );
2077 KMP_INFORM( AssumedNumCPU );
2078 }; // if
2079
Alp Toker763b9392014-02-28 09:42:41 +00002080 #elif KMP_OS_FREEBSD
2081
2082 int mib[] = { CTL_HW, HW_NCPU };
2083 size_t len = sizeof( r );
2084 if ( sysctl( mib, 2, &r, &len, NULL, 0 ) < 0 ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002085 r = 0;
2086 KMP_WARNING( CantGetNumAvailCPU );
2087 KMP_INFORM( AssumedNumCPU );
Alp Toker763b9392014-02-28 09:42:41 +00002088 }
2089
Jim Cownie5e8470a2013-09-27 10:38:44 +00002090 #else
2091
2092 #error "Unknown or unsupported OS."
2093
2094 #endif
2095
2096 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
2097
2098} // __kmp_get_xproc
2099
Jim Cownie181b4bb2013-12-23 17:28:57 +00002100int
2101__kmp_read_from_file( char const *path, char const *format, ... )
2102{
2103 int result;
2104 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002105
Jim Cownie181b4bb2013-12-23 17:28:57 +00002106 va_start(args, format);
2107 FILE *f = fopen(path, "rb");
2108 if ( f == NULL )
2109 return 0;
2110 result = vfscanf(f, format, args);
2111 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002112
Jim Cownie5e8470a2013-09-27 10:38:44 +00002113 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00002114}
Jim Cownie5e8470a2013-09-27 10:38:44 +00002115
2116void
2117__kmp_runtime_initialize( void )
2118{
2119 int status;
2120 pthread_mutexattr_t mutex_attr;
2121 pthread_condattr_t cond_attr;
2122
2123 if ( __kmp_init_runtime ) {
2124 return;
2125 }; // if
2126
2127 #if ( KMP_ARCH_X86 || KMP_ARCH_X86_64 )
2128 if ( ! __kmp_cpuinfo.initialized ) {
2129 __kmp_query_cpuid( &__kmp_cpuinfo );
2130 }; // if
2131 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
2132
Jim Cownie5e8470a2013-09-27 10:38:44 +00002133 __kmp_xproc = __kmp_get_xproc();
2134
2135 if ( sysconf( _SC_THREADS ) ) {
2136
2137 /* Query the maximum number of threads */
2138 __kmp_sys_max_nth = sysconf( _SC_THREAD_THREADS_MAX );
2139 if ( __kmp_sys_max_nth == -1 ) {
2140 /* Unlimited threads for NPTL */
2141 __kmp_sys_max_nth = INT_MAX;
2142 }
2143 else if ( __kmp_sys_max_nth <= 1 ) {
2144 /* Can't tell, just use PTHREAD_THREADS_MAX */
2145 __kmp_sys_max_nth = KMP_MAX_NTH;
2146 }
2147
2148 /* Query the minimum stack size */
2149 __kmp_sys_min_stksize = sysconf( _SC_THREAD_STACK_MIN );
2150 if ( __kmp_sys_min_stksize <= 1 ) {
2151 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
2152 }
2153 }
2154
2155 /* Set up minimum number of threads to switch to TLS gtid */
2156 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
2157
2158
2159 #ifdef BUILD_TV
2160 {
2161 int rc = pthread_key_create( & __kmp_tv_key, 0 );
2162 KMP_CHECK_SYSFAIL( "pthread_key_create", rc );
2163 }
2164 #endif
2165
2166 status = pthread_key_create( &__kmp_gtid_threadprivate_key, __kmp_internal_end_dest );
2167 KMP_CHECK_SYSFAIL( "pthread_key_create", status );
2168 status = pthread_mutexattr_init( & mutex_attr );
2169 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
2170 status = pthread_mutex_init( & __kmp_wait_mx.m_mutex, & mutex_attr );
2171 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
2172 status = pthread_condattr_init( & cond_attr );
2173 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
2174 status = pthread_cond_init( & __kmp_wait_cv.c_cond, & cond_attr );
2175 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
2176#if USE_ITT_BUILD
2177 __kmp_itt_initialize();
2178#endif /* USE_ITT_BUILD */
2179
2180 __kmp_init_runtime = TRUE;
2181}
2182
2183void
2184__kmp_runtime_destroy( void )
2185{
2186 int status;
2187
2188 if ( ! __kmp_init_runtime ) {
2189 return; // Nothing to do.
2190 };
2191
2192#if USE_ITT_BUILD
2193 __kmp_itt_destroy();
2194#endif /* USE_ITT_BUILD */
2195
2196 status = pthread_key_delete( __kmp_gtid_threadprivate_key );
2197 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2198 #ifdef BUILD_TV
2199 status = pthread_key_delete( __kmp_tv_key );
2200 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2201 #endif
2202
2203 status = pthread_mutex_destroy( & __kmp_wait_mx.m_mutex );
2204 if ( status != 0 && status != EBUSY ) {
2205 KMP_SYSFAIL( "pthread_mutex_destroy", status );
2206 }
2207 status = pthread_cond_destroy( & __kmp_wait_cv.c_cond );
2208 if ( status != 0 && status != EBUSY ) {
2209 KMP_SYSFAIL( "pthread_cond_destroy", status );
2210 }
Alp Toker763b9392014-02-28 09:42:41 +00002211 #if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00002212 __kmp_affinity_uninitialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00002213 #endif
2214
2215 __kmp_init_runtime = FALSE;
2216}
2217
2218
2219/* Put the thread to sleep for a time period */
2220/* NOTE: not currently used anywhere */
2221void
2222__kmp_thread_sleep( int millis )
2223{
2224 sleep( ( millis + 500 ) / 1000 );
2225}
2226
2227/* Calculate the elapsed wall clock time for the user */
2228void
2229__kmp_elapsed( double *t )
2230{
2231 int status;
2232# ifdef FIX_SGI_CLOCK
2233 struct timespec ts;
2234
2235 status = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
2236 KMP_CHECK_SYSFAIL_ERRNO( "clock_gettime", status );
2237 *t = (double) ts.tv_nsec * (1.0 / (double) NSEC_PER_SEC) +
2238 (double) ts.tv_sec;
2239# else
2240 struct timeval tv;
2241
2242 status = gettimeofday( & tv, NULL );
2243 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
2244 *t = (double) tv.tv_usec * (1.0 / (double) USEC_PER_SEC) +
2245 (double) tv.tv_sec;
2246# endif
2247}
2248
2249/* Calculate the elapsed wall clock tick for the user */
2250void
2251__kmp_elapsed_tick( double *t )
2252{
2253 *t = 1 / (double) CLOCKS_PER_SEC;
2254}
2255
2256/*
2257 Determine whether the given address is mapped into the current address space.
2258*/
2259
2260int
2261__kmp_is_address_mapped( void * addr ) {
2262
2263 int found = 0;
2264 int rc;
2265
2266 #if KMP_OS_LINUX
2267
2268 /*
2269 On Linux* OS, read the /proc/<pid>/maps pseudo-file to get all the address ranges mapped
2270 into the address space.
2271 */
2272
2273 char * name = __kmp_str_format( "/proc/%d/maps", getpid() );
2274 FILE * file = NULL;
2275
2276 file = fopen( name, "r" );
2277 KMP_ASSERT( file != NULL );
2278
2279 for ( ; ; ) {
2280
2281 void * beginning = NULL;
2282 void * ending = NULL;
2283 char perms[ 5 ];
2284
2285 rc = fscanf( file, "%p-%p %4s %*[^\n]\n", & beginning, & ending, perms );
2286 if ( rc == EOF ) {
2287 break;
2288 }; // if
2289 KMP_ASSERT( rc == 3 && strlen( perms ) == 4 ); // Make sure all fields are read.
2290
2291 // Ending address is not included in the region, but beginning is.
2292 if ( ( addr >= beginning ) && ( addr < ending ) ) {
2293 perms[ 2 ] = 0; // 3th and 4th character does not matter.
2294 if ( strcmp( perms, "rw" ) == 0 ) {
2295 // Memory we are looking for should be readable and writable.
2296 found = 1;
2297 }; // if
2298 break;
2299 }; // if
2300
2301 }; // forever
2302
2303 // Free resources.
2304 fclose( file );
2305 KMP_INTERNAL_FREE( name );
2306
2307 #elif KMP_OS_DARWIN
2308
2309 /*
2310 On OS X*, /proc pseudo filesystem is not available. Try to read memory using vm
2311 interface.
2312 */
2313
2314 int buffer;
2315 vm_size_t count;
2316 rc =
2317 vm_read_overwrite(
2318 mach_task_self(), // Task to read memory of.
2319 (vm_address_t)( addr ), // Address to read from.
2320 1, // Number of bytes to be read.
2321 (vm_address_t)( & buffer ), // Address of buffer to save read bytes in.
2322 & count // Address of var to save number of read bytes in.
2323 );
2324 if ( rc == 0 ) {
2325 // Memory successfully read.
2326 found = 1;
2327 }; // if
2328
Alp Toker763b9392014-02-28 09:42:41 +00002329 #elif KMP_OS_FREEBSD
2330
Jim Cownie4cc4bb42014-10-07 16:25:50 +00002331 // FIXME(FreeBSD*): Implement this
Alp Toker763b9392014-02-28 09:42:41 +00002332 found = 1;
2333
Jim Cownie5e8470a2013-09-27 10:38:44 +00002334 #else
2335
2336 #error "Unknown or unsupported OS"
2337
2338 #endif
2339
2340 return found;
2341
2342} // __kmp_is_address_mapped
2343
2344#ifdef USE_LOAD_BALANCE
2345
2346
2347# if KMP_OS_DARWIN
2348
2349// The function returns the rounded value of the system load average
2350// during given time interval which depends on the value of
2351// __kmp_load_balance_interval variable (default is 60 sec, other values
2352// may be 300 sec or 900 sec).
2353// It returns -1 in case of error.
2354int
2355__kmp_get_load_balance( int max )
2356{
2357 double averages[3];
2358 int ret_avg = 0;
2359
2360 int res = getloadavg( averages, 3 );
2361
2362 //Check __kmp_load_balance_interval to determine which of averages to use.
2363 // getloadavg() may return the number of samples less than requested that is
2364 // less than 3.
2365 if ( __kmp_load_balance_interval < 180 && ( res >= 1 ) ) {
2366 ret_avg = averages[0];// 1 min
2367 } else if ( ( __kmp_load_balance_interval >= 180
2368 && __kmp_load_balance_interval < 600 ) && ( res >= 2 ) ) {
2369 ret_avg = averages[1];// 5 min
2370 } else if ( ( __kmp_load_balance_interval >= 600 ) && ( res == 3 ) ) {
2371 ret_avg = averages[2];// 15 min
Alp Toker8f2d3f02014-02-24 10:40:15 +00002372 } else {// Error occurred
Jim Cownie5e8470a2013-09-27 10:38:44 +00002373 return -1;
2374 }
2375
2376 return ret_avg;
2377}
2378
2379# else // Linux* OS
2380
2381// The fuction returns number of running (not sleeping) threads, or -1 in case of error.
2382// Error could be reported if Linux* OS kernel too old (without "/proc" support).
2383// Counting running threads stops if max running threads encountered.
2384int
2385__kmp_get_load_balance( int max )
2386{
2387 static int permanent_error = 0;
2388
2389 static int glb_running_threads = 0; /* Saved count of the running threads for the thread balance algortihm */
2390 static double glb_call_time = 0; /* Thread balance algorithm call time */
2391
2392 int running_threads = 0; // Number of running threads in the system.
2393
2394 DIR * proc_dir = NULL; // Handle of "/proc/" directory.
2395 struct dirent * proc_entry = NULL;
2396
2397 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2398 DIR * task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2399 struct dirent * task_entry = NULL;
2400 int task_path_fixed_len;
2401
2402 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2403 int stat_file = -1;
2404 int stat_path_fixed_len;
2405
2406 int total_processes = 0; // Total number of processes in system.
2407 int total_threads = 0; // Total number of threads in system.
2408
2409 double call_time = 0.0;
2410
2411 __kmp_str_buf_init( & task_path );
2412 __kmp_str_buf_init( & stat_path );
2413
2414 __kmp_elapsed( & call_time );
2415
2416 if ( glb_call_time &&
2417 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
2418 running_threads = glb_running_threads;
2419 goto finish;
2420 }
2421
2422 glb_call_time = call_time;
2423
2424 // Do not spend time on scanning "/proc/" if we have a permanent error.
2425 if ( permanent_error ) {
2426 running_threads = -1;
2427 goto finish;
2428 }; // if
2429
2430 if ( max <= 0 ) {
2431 max = INT_MAX;
2432 }; // if
2433
2434 // Open "/proc/" directory.
2435 proc_dir = opendir( "/proc" );
2436 if ( proc_dir == NULL ) {
2437 // Cannot open "/prroc/". Probably the kernel does not support it. Return an error now and
2438 // in subsequent calls.
2439 running_threads = -1;
2440 permanent_error = 1;
2441 goto finish;
2442 }; // if
2443
2444 // Initialize fixed part of task_path. This part will not change.
2445 __kmp_str_buf_cat( & task_path, "/proc/", 6 );
2446 task_path_fixed_len = task_path.used; // Remember number of used characters.
2447
2448 proc_entry = readdir( proc_dir );
2449 while ( proc_entry != NULL ) {
2450 // Proc entry is a directory and name starts with a digit. Assume it is a process'
2451 // directory.
2452 if ( proc_entry->d_type == DT_DIR && isdigit( proc_entry->d_name[ 0 ] ) ) {
2453
2454 ++ total_processes;
2455 // Make sure init process is the very first in "/proc", so we can replace
2456 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes == 1.
2457 // We are going to check that total_processes == 1 => d_name == "1" is true (where
2458 // "=>" is implication). Since C++ does not have => operator, let us replace it with its
2459 // equivalent: a => b == ! a || b.
2460 KMP_DEBUG_ASSERT( total_processes != 1 || strcmp( proc_entry->d_name, "1" ) == 0 );
2461
2462 // Construct task_path.
2463 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
2464 __kmp_str_buf_cat( & task_path, proc_entry->d_name, strlen( proc_entry->d_name ) );
2465 __kmp_str_buf_cat( & task_path, "/task", 5 );
2466
2467 task_dir = opendir( task_path.str );
2468 if ( task_dir == NULL ) {
2469 // Process can finish between reading "/proc/" directory entry and opening process'
2470 // "task/" directory. So, in general case we should not complain, but have to skip
2471 // this process and read the next one.
2472 // But on systems with no "task/" support we will spend lot of time to scan "/proc/"
2473 // tree again and again without any benefit. "init" process (its pid is 1) should
2474 // exist always, so, if we cannot open "/proc/1/task/" directory, it means "task/"
2475 // is not supported by kernel. Report an error now and in the future.
2476 if ( strcmp( proc_entry->d_name, "1" ) == 0 ) {
2477 running_threads = -1;
2478 permanent_error = 1;
2479 goto finish;
2480 }; // if
2481 } else {
2482 // Construct fixed part of stat file path.
2483 __kmp_str_buf_clear( & stat_path );
2484 __kmp_str_buf_cat( & stat_path, task_path.str, task_path.used );
2485 __kmp_str_buf_cat( & stat_path, "/", 1 );
2486 stat_path_fixed_len = stat_path.used;
2487
2488 task_entry = readdir( task_dir );
2489 while ( task_entry != NULL ) {
2490 // It is a directory and name starts with a digit.
2491 if ( proc_entry->d_type == DT_DIR && isdigit( task_entry->d_name[ 0 ] ) ) {
2492
2493 ++ total_threads;
2494
2495 // Consruct complete stat file path. Easiest way would be:
2496 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str, task_entry->d_name );
2497 // but seriae of __kmp_str_buf_cat works a bit faster.
2498 stat_path.used = stat_path_fixed_len; // Reset stat path to its fixed part.
2499 __kmp_str_buf_cat( & stat_path, task_entry->d_name, strlen( task_entry->d_name ) );
2500 __kmp_str_buf_cat( & stat_path, "/stat", 5 );
2501
2502 // Note: Low-level API (open/read/close) is used. High-level API
2503 // (fopen/fclose) works ~ 30 % slower.
2504 stat_file = open( stat_path.str, O_RDONLY );
2505 if ( stat_file == -1 ) {
2506 // We cannot report an error because task (thread) can terminate just
2507 // before reading this file.
2508 } else {
2509 /*
2510 Content of "stat" file looks like:
2511
2512 24285 (program) S ...
2513
2514 It is a single line (if program name does not include fanny
2515 symbols). First number is a thread id, then name of executable file
2516 name in paretheses, then state of the thread. We need just thread
2517 state.
2518
2519 Good news: Length of program name is 15 characters max. Longer
2520 names are truncated.
2521
2522 Thus, we need rather short buffer: 15 chars for program name +
2523 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
2524
2525 Bad news: Program name may contain special symbols like space,
2526 closing parenthesis, or even new line. This makes parsing "stat"
2527 file not 100 % reliable. In case of fanny program names parsing
2528 may fail (report incorrect thread state).
2529
2530 Parsing "status" file looks more promissing (due to different
2531 file structure and escaping special symbols) but reading and
2532 parsing of "status" file works slower.
2533
2534 -- ln
2535 */
2536 char buffer[ 65 ];
2537 int len;
2538 len = read( stat_file, buffer, sizeof( buffer ) - 1 );
2539 if ( len >= 0 ) {
2540 buffer[ len ] = 0;
2541 // Using scanf:
2542 // sscanf( buffer, "%*d (%*s) %c ", & state );
2543 // looks very nice, but searching for a closing parenthesis works a
2544 // bit faster.
2545 char * close_parent = strstr( buffer, ") " );
2546 if ( close_parent != NULL ) {
2547 char state = * ( close_parent + 2 );
2548 if ( state == 'R' ) {
2549 ++ running_threads;
2550 if ( running_threads >= max ) {
2551 goto finish;
2552 }; // if
2553 }; // if
2554 }; // if
2555 }; // if
2556 close( stat_file );
2557 stat_file = -1;
2558 }; // if
2559 }; // if
2560 task_entry = readdir( task_dir );
2561 }; // while
2562 closedir( task_dir );
2563 task_dir = NULL;
2564 }; // if
2565 }; // if
2566 proc_entry = readdir( proc_dir );
2567 }; // while
2568
2569 //
2570 // There _might_ be a timing hole where the thread executing this
2571 // code get skipped in the load balance, and running_threads is 0.
2572 // Assert in the debug builds only!!!
2573 //
2574 KMP_DEBUG_ASSERT( running_threads > 0 );
2575 if ( running_threads <= 0 ) {
2576 running_threads = 1;
2577 }
2578
2579 finish: // Clean up and exit.
2580 if ( proc_dir != NULL ) {
2581 closedir( proc_dir );
2582 }; // if
2583 __kmp_str_buf_free( & task_path );
2584 if ( task_dir != NULL ) {
2585 closedir( task_dir );
2586 }; // if
2587 __kmp_str_buf_free( & stat_path );
2588 if ( stat_file != -1 ) {
2589 close( stat_file );
2590 }; // if
2591
2592 glb_running_threads = running_threads;
2593
2594 return running_threads;
2595
2596} // __kmp_get_load_balance
2597
2598# endif // KMP_OS_DARWIN
2599
2600#endif // USE_LOAD_BALANCE
2601
Jim Cownie181b4bb2013-12-23 17:28:57 +00002602
Jim Cownie3051f972014-08-07 10:12:54 +00002603#if KMP_COMPILER_GCC && !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_PPC64)
Jim Cownie181b4bb2013-12-23 17:28:57 +00002604
2605int __kmp_invoke_microtask( microtask_t pkfn, int gtid, int tid, int argc,
2606 void *p_argv[] )
2607{
2608 int argc_full = argc + 2;
2609 int i;
2610 ffi_cif cif;
2611 ffi_type *types[argc_full];
2612 void *args[argc_full];
2613 void *idp[2];
2614
2615 /* We're only passing pointers to the target. */
2616 for (i = 0; i < argc_full; i++)
2617 types[i] = &ffi_type_pointer;
2618
2619 /* Ugly double-indirection, but that's how it goes... */
2620 idp[0] = &gtid;
2621 idp[1] = &tid;
2622 args[0] = &idp[0];
2623 args[1] = &idp[1];
2624
2625 for (i = 0; i < argc; i++)
2626 args[2 + i] = &p_argv[i];
2627
2628 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, argc_full,
2629 &ffi_type_void, types) != FFI_OK)
2630 abort();
2631
2632 ffi_call(&cif, (void (*)(void))pkfn, NULL, args);
2633
2634 return 1;
2635}
2636
Jim Cownie3051f972014-08-07 10:12:54 +00002637#endif // KMP_COMPILER_GCC && !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_PPC64)
2638
2639#if KMP_ARCH_PPC64
2640
2641// we really only need the case with 1 argument, because CLANG always build
2642// a struct of pointers to shared variables referenced in the outlined function
2643int
2644__kmp_invoke_microtask( microtask_t pkfn,
2645 int gtid, int tid,
2646 int argc, void *p_argv[] ) {
2647 switch (argc) {
2648 default:
2649 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2650 fflush(stderr);
2651 exit(-1);
2652 case 0:
2653 (*pkfn)(&gtid, &tid);
2654 break;
2655 case 1:
2656 (*pkfn)(&gtid, &tid, p_argv[0]);
2657 break;
2658 case 2:
2659 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2660 break;
2661 case 3:
2662 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2663 break;
2664 case 4:
2665 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2666 break;
2667 case 5:
2668 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2669 break;
2670 case 6:
2671 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2672 p_argv[5]);
2673 break;
2674 case 7:
2675 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2676 p_argv[5], p_argv[6]);
2677 break;
2678 case 8:
2679 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2680 p_argv[5], p_argv[6], p_argv[7]);
2681 break;
2682 case 9:
2683 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2684 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2685 break;
2686 case 10:
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], p_argv[7], p_argv[8], p_argv[9]);
2689 break;
2690 case 11:
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], p_argv[8], p_argv[9], p_argv[10]);
2693 break;
2694 case 12:
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], p_argv[9], p_argv[10],
2697 p_argv[11]);
2698 break;
2699 case 13:
2700 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2701 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2702 p_argv[11], p_argv[12]);
2703 break;
2704 case 14:
2705 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2706 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2707 p_argv[11], p_argv[12], p_argv[13]);
2708 break;
2709 case 15:
2710 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2711 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2712 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2713 break;
2714 }
2715
2716 return 1;
2717}
2718
2719#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002720
Jim Cownie5e8470a2013-09-27 10:38:44 +00002721// end of file //
2722