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