blob: 389f4d543db9391d59b15459296211a7f004091a [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * z_Linux_util.c -- platform specific routines.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
5
6//===----------------------------------------------------------------------===//
7//
8// The LLVM Compiler Infrastructure
9//
10// This file is dual licensed under the MIT and the University of Illinois Open
11// Source Licenses. See LICENSE.txt for details.
12//
13//===----------------------------------------------------------------------===//
14
15
16#include "kmp.h"
17#include "kmp_wrapper_getpid.h"
18#include "kmp_itt.h"
19#include "kmp_str.h"
20#include "kmp_i18n.h"
Paul Osmialowskifb043fd2016-05-16 09:44:11 +000021#include "kmp_lock.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000022#include "kmp_io.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000023#include "kmp_stats.h"
24#include "kmp_wait_release.h"
Jonathan Peyton1cdd87a2016-11-14 21:08:35 +000025#include "kmp_affinity.h"
Jim Cownie5e8470a2013-09-27 10:38:44 +000026
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +000027#if !KMP_OS_FREEBSD && !KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +000028# include <alloca.h>
29#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +000030#include <unistd.h>
31#include <math.h> // HUGE_VAL.
32#include <sys/time.h>
33#include <sys/times.h>
34#include <sys/resource.h>
35#include <sys/syscall.h>
36
Jim Cownie3051f972014-08-07 10:12:54 +000037#if KMP_OS_LINUX && !KMP_OS_CNK
Jim Cownie5e8470a2013-09-27 10:38:44 +000038# include <sys/sysinfo.h>
Paul Osmialowskifb043fd2016-05-16 09:44:11 +000039# if KMP_USE_FUTEX
Jim Cownie5e8470a2013-09-27 10:38:44 +000040// We should really include <futex.h>, but that causes compatibility problems on different
41// Linux* OS distributions that either require that you include (or break when you try to include)
42// <pci/types.h>.
43// Since all we need is the two macros below (which are part of the kernel ABI, so can't change)
44// we just define the constants here and don't include <futex.h>
45# ifndef FUTEX_WAIT
46# define FUTEX_WAIT 0
47# endif
48# ifndef FUTEX_WAKE
49# define FUTEX_WAKE 1
50# endif
51# endif
52#elif KMP_OS_DARWIN
53# include <sys/sysctl.h>
54# include <mach/mach.h>
Alp Toker763b9392014-02-28 09:42:41 +000055#elif KMP_OS_FREEBSD
Alp Toker763b9392014-02-28 09:42:41 +000056# include <pthread_np.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000057#endif
58
Jim Cownie5e8470a2013-09-27 10:38:44 +000059#include <dirent.h>
60#include <ctype.h>
61#include <fcntl.h>
62
Jonas Hahnfeld50fed042016-11-07 15:58:36 +000063#include "tsan_annotations.h"
64
Jim Cownie5e8470a2013-09-27 10:38:44 +000065/* ------------------------------------------------------------------------ */
66/* ------------------------------------------------------------------------ */
67
68struct kmp_sys_timer {
69 struct timespec start;
70};
71
72// Convert timespec to nanoseconds.
73#define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec)
74
75static struct kmp_sys_timer __kmp_sys_timer_data;
76
77#if KMP_HANDLE_SIGNALS
78 typedef void (* sig_func_t )( int );
79 STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[ NSIG ];
80 static sigset_t __kmp_sigset;
81#endif
82
83static int __kmp_init_runtime = FALSE;
84
85static int __kmp_fork_count = 0;
86
87static pthread_condattr_t __kmp_suspend_cond_attr;
88static pthread_mutexattr_t __kmp_suspend_mutex_attr;
89
90static kmp_cond_align_t __kmp_wait_cv;
91static kmp_mutex_align_t __kmp_wait_mx;
92
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +000093double __kmp_ticks_per_nsec;
94
Jim Cownie5e8470a2013-09-27 10:38:44 +000095/* ------------------------------------------------------------------------ */
96/* ------------------------------------------------------------------------ */
97
98#ifdef DEBUG_SUSPEND
99static void
100__kmp_print_cond( char *buffer, kmp_cond_align_t *cond )
101{
Andrey Churbanov74bf17b2015-04-02 13:27:08 +0000102 KMP_SNPRINTF( buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))",
Jim Cownie5e8470a2013-09-27 10:38:44 +0000103 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
104 cond->c_cond.__c_waiting );
105}
106#endif
107
108/* ------------------------------------------------------------------------ */
109/* ------------------------------------------------------------------------ */
110
Jim Cownie3051f972014-08-07 10:12:54 +0000111#if ( KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000112
113/*
114 * Affinity support
115 */
116
Jim Cownie5e8470a2013-09-27 10:38:44 +0000117void
118__kmp_affinity_bind_thread( int which )
119{
120 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
121 "Illegal set affinity operation when not capable");
122
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000123 kmp_affin_mask_t *mask;
124 KMP_CPU_ALLOC_ON_STACK(mask);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000125 KMP_CPU_ZERO(mask);
126 KMP_CPU_SET(which, mask);
127 __kmp_set_system_affinity(mask, TRUE);
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000128 KMP_CPU_FREE_FROM_STACK(mask);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000129}
130
131/*
132 * Determine if we can access affinity functionality on this version of
133 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
134 * __kmp_affin_mask_size to the appropriate value (0 means not capable).
135 */
136void
137__kmp_affinity_determine_capable(const char *env_var)
138{
139 //
140 // Check and see if the OS supports thread affinity.
141 //
142
143# define KMP_CPU_SET_SIZE_LIMIT (1024*1024)
144
145 int gCode;
146 int sCode;
Jonathan Peyton9e696962016-09-02 20:35:47 +0000147 unsigned char *buf;
148 buf = ( unsigned char * ) KMP_INTERNAL_MALLOC( KMP_CPU_SET_SIZE_LIMIT );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000149
150 // If Linux* OS:
151 // If the syscall fails or returns a suggestion for the size,
152 // then we don't have to search for an appropriate size.
153 gCode = syscall( __NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf );
154 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
Alp Toker8f2d3f02014-02-24 10:40:15 +0000155 "initial getaffinity call returned %d errno = %d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +0000156 gCode, errno));
157
158 //if ((gCode < 0) && (errno == ENOSYS))
159 if (gCode < 0) {
160 //
161 // System call not supported
162 //
163 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
164 && (__kmp_affinity_type != affinity_none)
165 && (__kmp_affinity_type != affinity_default)
166 && (__kmp_affinity_type != affinity_disabled))) {
167 int error = errno;
168 __kmp_msg(
169 kmp_ms_warning,
170 KMP_MSG( GetAffSysCallNotSupported, env_var ),
171 KMP_ERR( error ),
172 __kmp_msg_null
173 );
174 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000175 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000176 KMP_INTERNAL_FREE(buf);
177 return;
178 }
179 if (gCode > 0) { // Linux* OS only
180 // The optimal situation: the OS returns the size of the buffer
181 // it expects.
182 //
183 // A verification of correct behavior is that Isetaffinity on a NULL
184 // buffer with the same size fails with errno set to EFAULT.
185 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
186 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
187 "setaffinity for mask size %d returned %d errno = %d\n",
188 gCode, sCode, errno));
189 if (sCode < 0) {
190 if (errno == ENOSYS) {
191 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
192 && (__kmp_affinity_type != affinity_none)
193 && (__kmp_affinity_type != affinity_default)
194 && (__kmp_affinity_type != affinity_disabled))) {
195 int error = errno;
196 __kmp_msg(
197 kmp_ms_warning,
198 KMP_MSG( SetAffSysCallNotSupported, env_var ),
199 KMP_ERR( error ),
200 __kmp_msg_null
201 );
202 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000203 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000204 KMP_INTERNAL_FREE(buf);
205 }
206 if (errno == EFAULT) {
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000207 KMP_AFFINITY_ENABLE(gCode);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000208 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
209 "affinity supported (mask size %d)\n",
210 (int)__kmp_affin_mask_size));
211 KMP_INTERNAL_FREE(buf);
212 return;
213 }
214 }
215 }
216
217 //
218 // Call the getaffinity system call repeatedly with increasing set sizes
219 // until we succeed, or reach an upper bound on the search.
220 //
221 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
222 "searching for proper set size\n"));
223 int size;
224 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
225 gCode = syscall( __NR_sched_getaffinity, 0, size, buf );
226 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
227 "getaffinity for mask size %d returned %d errno = %d\n", size,
228 gCode, errno));
229
230 if (gCode < 0) {
231 if ( errno == ENOSYS )
232 {
233 //
234 // We shouldn't get here
235 //
236 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
237 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
238 size));
239 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
240 && (__kmp_affinity_type != affinity_none)
241 && (__kmp_affinity_type != affinity_default)
242 && (__kmp_affinity_type != affinity_disabled))) {
243 int error = errno;
244 __kmp_msg(
245 kmp_ms_warning,
246 KMP_MSG( GetAffSysCallNotSupported, env_var ),
247 KMP_ERR( error ),
248 __kmp_msg_null
249 );
250 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000251 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000252 KMP_INTERNAL_FREE(buf);
253 return;
254 }
255 continue;
256 }
257
258 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
259 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
260 "setaffinity for mask size %d returned %d errno = %d\n",
261 gCode, sCode, errno));
262 if (sCode < 0) {
263 if (errno == ENOSYS) { // Linux* OS only
264 //
265 // We shouldn't get here
266 //
267 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
268 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
269 size));
270 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
271 && (__kmp_affinity_type != affinity_none)
272 && (__kmp_affinity_type != affinity_default)
273 && (__kmp_affinity_type != affinity_disabled))) {
274 int error = errno;
275 __kmp_msg(
276 kmp_ms_warning,
277 KMP_MSG( SetAffSysCallNotSupported, env_var ),
278 KMP_ERR( error ),
279 __kmp_msg_null
280 );
281 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000282 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000283 KMP_INTERNAL_FREE(buf);
284 return;
285 }
286 if (errno == EFAULT) {
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000287 KMP_AFFINITY_ENABLE(gCode);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000288 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
289 "affinity supported (mask size %d)\n",
290 (int)__kmp_affin_mask_size));
291 KMP_INTERNAL_FREE(buf);
292 return;
293 }
294 }
295 }
296 //int error = errno; // save uncaught error code
297 KMP_INTERNAL_FREE(buf);
298 // errno = error; // restore uncaught error code, will be printed at the next KMP_WARNING below
299
300 //
301 // Affinity is not supported
302 //
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000303 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000304 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
305 "cannot determine mask size - affinity not supported\n"));
306 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
307 && (__kmp_affinity_type != affinity_none)
308 && (__kmp_affinity_type != affinity_default)
309 && (__kmp_affinity_type != affinity_disabled))) {
310 KMP_WARNING( AffCantGetMaskSize, env_var );
311 }
312}
313
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000314#endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000315
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000316/* ------------------------------------------------------------------------ */
317/* ------------------------------------------------------------------------ */
318
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000319#if KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000320
321int
322__kmp_futex_determine_capable()
323{
324 int loc = 0;
325 int rc = syscall( __NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0 );
326 int retval = ( rc == 0 ) || ( errno != ENOSYS );
327
328 KA_TRACE(10, ( "__kmp_futex_determine_capable: rc = %d errno = %d\n", rc,
329 errno ) );
330 KA_TRACE(10, ( "__kmp_futex_determine_capable: futex syscall%s supported\n",
331 retval ? "" : " not" ) );
332
333 return retval;
334}
335
Jonathan Peyton9d2412c2016-06-22 16:35:12 +0000336#endif // KMP_USE_FUTEX
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000337
338/* ------------------------------------------------------------------------ */
339/* ------------------------------------------------------------------------ */
340
341#if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000342/*
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000343 * Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
344 * use compare_and_store for these routines
Jim Cownie5e8470a2013-09-27 10:38:44 +0000345 */
346
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000347kmp_int8
348__kmp_test_then_or8( volatile kmp_int8 *p, kmp_int8 d )
349{
350 kmp_int8 old_value, new_value;
351
352 old_value = TCR_1( *p );
353 new_value = old_value | d;
354
355 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
356 {
357 KMP_CPU_PAUSE();
358 old_value = TCR_1( *p );
359 new_value = old_value | d;
360 }
361 return old_value;
362}
363
364kmp_int8
365__kmp_test_then_and8( volatile kmp_int8 *p, kmp_int8 d )
366{
367 kmp_int8 old_value, new_value;
368
369 old_value = TCR_1( *p );
370 new_value = old_value & d;
371
372 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
373 {
374 KMP_CPU_PAUSE();
375 old_value = TCR_1( *p );
376 new_value = old_value & d;
377 }
378 return old_value;
379}
380
Jim Cownie5e8470a2013-09-27 10:38:44 +0000381kmp_int32
382__kmp_test_then_or32( volatile kmp_int32 *p, kmp_int32 d )
383{
384 kmp_int32 old_value, new_value;
385
386 old_value = TCR_4( *p );
387 new_value = old_value | d;
388
Jim Cownie3051f972014-08-07 10:12:54 +0000389 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000390 {
391 KMP_CPU_PAUSE();
392 old_value = TCR_4( *p );
393 new_value = old_value | d;
394 }
395 return old_value;
396}
397
398kmp_int32
399__kmp_test_then_and32( volatile kmp_int32 *p, kmp_int32 d )
400{
401 kmp_int32 old_value, new_value;
402
403 old_value = TCR_4( *p );
404 new_value = old_value & d;
405
Jim Cownie3051f972014-08-07 10:12:54 +0000406 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000407 {
408 KMP_CPU_PAUSE();
409 old_value = TCR_4( *p );
410 new_value = old_value & d;
411 }
412 return old_value;
413}
414
Paul Osmialowski7e5e8682016-05-13 08:26:42 +0000415# if KMP_ARCH_X86 || KMP_ARCH_PPC64 || (KMP_OS_LINUX && KMP_ARCH_AARCH64)
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000416kmp_int8
417__kmp_test_then_add8( volatile kmp_int8 *p, kmp_int8 d )
418{
419 kmp_int8 old_value, new_value;
420
421 old_value = TCR_1( *p );
422 new_value = old_value + d;
423
424 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
425 {
426 KMP_CPU_PAUSE();
427 old_value = TCR_1( *p );
428 new_value = old_value + d;
429 }
430 return old_value;
431}
432
Jim Cownie5e8470a2013-09-27 10:38:44 +0000433kmp_int64
434__kmp_test_then_add64( volatile kmp_int64 *p, kmp_int64 d )
435{
436 kmp_int64 old_value, new_value;
437
438 old_value = TCR_8( *p );
439 new_value = old_value + d;
440
Jim Cownie3051f972014-08-07 10:12:54 +0000441 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000442 {
443 KMP_CPU_PAUSE();
444 old_value = TCR_8( *p );
445 new_value = old_value + d;
446 }
447 return old_value;
448}
Paul Osmialowski7e5e8682016-05-13 08:26:42 +0000449# endif /* KMP_ARCH_X86 || KMP_ARCH_PPC64 || (KMP_OS_LINUX && KMP_ARCH_AARCH64) */
Jim Cownie5e8470a2013-09-27 10:38:44 +0000450
451kmp_int64
452__kmp_test_then_or64( volatile kmp_int64 *p, kmp_int64 d )
453{
454 kmp_int64 old_value, new_value;
455
456 old_value = TCR_8( *p );
457 new_value = old_value | d;
Jim Cownie3051f972014-08-07 10:12:54 +0000458 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000459 {
460 KMP_CPU_PAUSE();
461 old_value = TCR_8( *p );
462 new_value = old_value | d;
463 }
464 return old_value;
465}
466
467kmp_int64
468__kmp_test_then_and64( volatile kmp_int64 *p, kmp_int64 d )
469{
470 kmp_int64 old_value, new_value;
471
472 old_value = TCR_8( *p );
473 new_value = old_value & d;
Jim Cownie3051f972014-08-07 10:12:54 +0000474 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000475 {
476 KMP_CPU_PAUSE();
477 old_value = TCR_8( *p );
478 new_value = old_value & d;
479 }
480 return old_value;
481}
482
483#endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
484
485void
486__kmp_terminate_thread( int gtid )
487{
488 int status;
489 kmp_info_t *th = __kmp_threads[ gtid ];
490
491 if ( !th ) return;
492
493 #ifdef KMP_CANCEL_THREADS
494 KA_TRACE( 10, ("__kmp_terminate_thread: kill (%d)\n", gtid ) );
495 status = pthread_cancel( th->th.th_info.ds.ds_thread );
496 if ( status != 0 && status != ESRCH ) {
497 __kmp_msg(
498 kmp_ms_fatal,
499 KMP_MSG( CantTerminateWorkerThread ),
500 KMP_ERR( status ),
501 __kmp_msg_null
502 );
503 }; // if
504 #endif
505 __kmp_yield( TRUE );
506} //
507
508/* ------------------------------------------------------------------------ */
509/* ------------------------------------------------------------------------ */
510
511/* ------------------------------------------------------------------------ */
512/* ------------------------------------------------------------------------ */
513
514/*
515 * Set thread stack info according to values returned by
516 * pthread_getattr_np().
517 * If values are unreasonable, assume call failed and use
518 * incremental stack refinement method instead.
519 * Returns TRUE if the stack parameters could be determined exactly,
520 * FALSE if incremental refinement is necessary.
521 */
522static kmp_int32
523__kmp_set_stack_info( int gtid, kmp_info_t *th )
524{
525 int stack_data;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000526#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000527 /* Linux* OS only -- no pthread_getattr_np support on OS X* */
528 pthread_attr_t attr;
529 int status;
530 size_t size = 0;
531 void * addr = 0;
532
533 /* Always do incremental stack refinement for ubermaster threads since the initial
534 thread stack range can be reduced by sibling thread creation so pthread_attr_getstack
535 may cause thread gtid aliasing */
536 if ( ! KMP_UBER_GTID(gtid) ) {
537
538 /* Fetch the real thread attributes */
539 status = pthread_attr_init( &attr );
540 KMP_CHECK_SYSFAIL( "pthread_attr_init", status );
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000541#if KMP_OS_FREEBSD || KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +0000542 status = pthread_attr_get_np( pthread_self(), &attr );
543 KMP_CHECK_SYSFAIL( "pthread_attr_get_np", status );
544#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000545 status = pthread_getattr_np( pthread_self(), &attr );
546 KMP_CHECK_SYSFAIL( "pthread_getattr_np", status );
Alp Toker763b9392014-02-28 09:42:41 +0000547#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000548 status = pthread_attr_getstack( &attr, &addr, &size );
549 KMP_CHECK_SYSFAIL( "pthread_attr_getstack", status );
550 KA_TRACE( 60, ( "__kmp_set_stack_info: T#%d pthread_attr_getstack returned size: %lu, "
551 "low addr: %p\n",
552 gtid, size, addr ));
553
554 status = pthread_attr_destroy( &attr );
555 KMP_CHECK_SYSFAIL( "pthread_attr_destroy", status );
556 }
557
558 if ( size != 0 && addr != 0 ) { /* was stack parameter determination successful? */
559 /* Store the correct base and size */
560 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size));
561 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
562 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
563 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000564 }
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000565#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD */
Alp Toker763b9392014-02-28 09:42:41 +0000566 /* Use incremental refinement starting from initial conservative estimate */
567 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
568 TCW_PTR(th -> th.th_info.ds.ds_stackbase, &stack_data);
569 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
570 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000571}
572
573static void*
574__kmp_launch_worker( void *thr )
575{
576 int status, old_type, old_state;
577#ifdef KMP_BLOCK_SIGNALS
578 sigset_t new_set, old_set;
579#endif /* KMP_BLOCK_SIGNALS */
580 void *exit_val;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000581#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Andrey Churbanov368b70e2015-08-05 11:12:45 +0000582 void * volatile padding = 0;
Jonathan Peyton2321d572015-06-08 19:25:25 +0000583#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000584 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000585
586 gtid = ((kmp_info_t*)thr) -> th.th_info.ds.ds_gtid;
587 __kmp_gtid_set_specific( gtid );
588#ifdef KMP_TDATA_GTID
589 __kmp_gtid = gtid;
590#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000591#if KMP_STATS_ENABLED
592 // set __thread local index to point to thread-specific stats
593 __kmp_stats_thread_ptr = ((kmp_info_t*)thr)->th.th_stats;
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000594 KMP_START_EXPLICIT_TIMER(OMP_worker_thread_life);
595 KMP_SET_THREAD_STATE(IDLE);
596 KMP_INIT_PARTITIONED_TIMERS(OMP_idle);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000597#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000598
599#if USE_ITT_BUILD
600 __kmp_itt_thread_name( gtid );
601#endif /* USE_ITT_BUILD */
602
Alp Toker763b9392014-02-28 09:42:41 +0000603#if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000604 __kmp_affinity_set_init_mask( gtid, FALSE );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000605#endif
606
607#ifdef KMP_CANCEL_THREADS
608 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
609 KMP_CHECK_SYSFAIL( "pthread_setcanceltype", status );
610 /* josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? */
611 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
612 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
613#endif
614
615#if KMP_ARCH_X86 || KMP_ARCH_X86_64
616 //
617 // Set the FP control regs to be a copy of
618 // the parallel initialization thread's.
619 //
620 __kmp_clear_x87_fpu_status_word();
621 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
622 __kmp_load_mxcsr( &__kmp_init_mxcsr );
623#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
624
625#ifdef KMP_BLOCK_SIGNALS
626 status = sigfillset( & new_set );
627 KMP_CHECK_SYSFAIL_ERRNO( "sigfillset", status );
628 status = pthread_sigmask( SIG_BLOCK, & new_set, & old_set );
629 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
630#endif /* KMP_BLOCK_SIGNALS */
631
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000632#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000633 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
Andrey Churbanov74bf17b2015-04-02 13:27:08 +0000634 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000635 }
636#endif
637
638 KMP_MB();
639 __kmp_set_stack_info( gtid, (kmp_info_t*)thr );
640
641 __kmp_check_stack_overlap( (kmp_info_t*)thr );
642
643 exit_val = __kmp_launch_thread( (kmp_info_t *) thr );
644
645#ifdef KMP_BLOCK_SIGNALS
646 status = pthread_sigmask( SIG_SETMASK, & old_set, NULL );
647 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
648#endif /* KMP_BLOCK_SIGNALS */
649
650 return exit_val;
651}
652
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000653#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000654/* The monitor thread controls all of the threads in the complex */
655
656static void*
657__kmp_launch_monitor( void *thr )
658{
659 int status, old_type, old_state;
660#ifdef KMP_BLOCK_SIGNALS
661 sigset_t new_set;
662#endif /* KMP_BLOCK_SIGNALS */
663 struct timespec interval;
664 int yield_count;
665 int yield_cycles = 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000666
667 KMP_MB(); /* Flush all pending memory write invalidates. */
668
669 KA_TRACE( 10, ("__kmp_launch_monitor: #1 launched\n" ) );
670
671 /* register us as the monitor thread */
672 __kmp_gtid_set_specific( KMP_GTID_MONITOR );
673#ifdef KMP_TDATA_GTID
674 __kmp_gtid = KMP_GTID_MONITOR;
675#endif
676
677 KMP_MB();
678
679#if USE_ITT_BUILD
680 __kmp_itt_thread_ignore(); // Instruct Intel(R) Threading Tools to ignore monitor thread.
681#endif /* USE_ITT_BUILD */
682
683 __kmp_set_stack_info( ((kmp_info_t*)thr)->th.th_info.ds.ds_gtid, (kmp_info_t*)thr );
684
685 __kmp_check_stack_overlap( (kmp_info_t*)thr );
686
687#ifdef KMP_CANCEL_THREADS
688 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
689 KMP_CHECK_SYSFAIL( "pthread_setcanceltype", status );
690 /* josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? */
691 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
692 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
693#endif
694
695 #if KMP_REAL_TIME_FIX
696 // This is a potential fix which allows application with real-time scheduling policy work.
697 // However, decision about the fix is not made yet, so it is disabled by default.
698 { // Are program started with real-time scheduling policy?
699 int sched = sched_getscheduler( 0 );
700 if ( sched == SCHED_FIFO || sched == SCHED_RR ) {
701 // Yes, we are a part of real-time application. Try to increase the priority of the
702 // monitor.
703 struct sched_param param;
704 int max_priority = sched_get_priority_max( sched );
705 int rc;
706 KMP_WARNING( RealTimeSchedNotSupported );
707 sched_getparam( 0, & param );
708 if ( param.sched_priority < max_priority ) {
709 param.sched_priority += 1;
710 rc = sched_setscheduler( 0, sched, & param );
711 if ( rc != 0 ) {
712 int error = errno;
713 __kmp_msg(
714 kmp_ms_warning,
715 KMP_MSG( CantChangeMonitorPriority ),
716 KMP_ERR( error ),
717 KMP_MSG( MonitorWillStarve ),
718 __kmp_msg_null
719 );
720 }; // if
721 } else {
722 // We cannot abort here, because number of CPUs may be enough for all the threads,
723 // including the monitor thread, so application could potentially work...
724 __kmp_msg(
725 kmp_ms_warning,
726 KMP_MSG( RunningAtMaxPriority ),
727 KMP_MSG( MonitorWillStarve ),
728 KMP_HNT( RunningAtMaxPriority ),
729 __kmp_msg_null
730 );
731 }; // if
732 }; // if
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000733 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 +0000734 }
735 #endif // KMP_REAL_TIME_FIX
736
737 KMP_MB(); /* Flush all pending memory write invalidates. */
738
739 if ( __kmp_monitor_wakeups == 1 ) {
740 interval.tv_sec = 1;
741 interval.tv_nsec = 0;
742 } else {
743 interval.tv_sec = 0;
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +0000744 interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000745 }
746
747 KA_TRACE( 10, ("__kmp_launch_monitor: #2 monitor\n" ) );
748
749 if (__kmp_yield_cycle) {
750 __kmp_yielding_on = 0; /* Start out with yielding shut off */
751 yield_count = __kmp_yield_off_count;
752 } else {
753 __kmp_yielding_on = 1; /* Yielding is on permanently */
754 }
755
756 while( ! TCR_4( __kmp_global.g.g_done ) ) {
757 struct timespec now;
758 struct timeval tval;
759
760 /* This thread monitors the state of the system */
761
762 KA_TRACE( 15, ( "__kmp_launch_monitor: update\n" ) );
763
764 status = gettimeofday( &tval, NULL );
765 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
766 TIMEVAL_TO_TIMESPEC( &tval, &now );
767
768 now.tv_sec += interval.tv_sec;
769 now.tv_nsec += interval.tv_nsec;
770
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +0000771 if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000772 now.tv_sec += 1;
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +0000773 now.tv_nsec -= KMP_NSEC_PER_SEC;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000774 }
775
776 status = pthread_mutex_lock( & __kmp_wait_mx.m_mutex );
777 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie07ea89f2014-09-03 11:10:54 +0000778 // AC: the monitor should not fall asleep if g_done has been set
779 if ( !TCR_4(__kmp_global.g.g_done) ) { // check once more under mutex
780 status = pthread_cond_timedwait( &__kmp_wait_cv.c_cond, &__kmp_wait_mx.m_mutex, &now );
781 if ( status != 0 ) {
782 if ( status != ETIMEDOUT && status != EINTR ) {
783 KMP_SYSFAIL( "pthread_cond_timedwait", status );
784 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000785 };
786 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000787 status = pthread_mutex_unlock( & __kmp_wait_mx.m_mutex );
788 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
789
790 if (__kmp_yield_cycle) {
791 yield_cycles++;
792 if ( (yield_cycles % yield_count) == 0 ) {
793 if (__kmp_yielding_on) {
794 __kmp_yielding_on = 0; /* Turn it off now */
795 yield_count = __kmp_yield_off_count;
796 } else {
797 __kmp_yielding_on = 1; /* Turn it on now */
798 yield_count = __kmp_yield_on_count;
799 }
800 yield_cycles = 0;
801 }
802 } else {
803 __kmp_yielding_on = 1;
804 }
805
806 TCW_4( __kmp_global.g.g_time.dt.t_value,
807 TCR_4( __kmp_global.g.g_time.dt.t_value ) + 1 );
808
809 KMP_MB(); /* Flush all pending memory write invalidates. */
810 }
811
812 KA_TRACE( 10, ("__kmp_launch_monitor: #3 cleanup\n" ) );
813
814#ifdef KMP_BLOCK_SIGNALS
815 status = sigfillset( & new_set );
816 KMP_CHECK_SYSFAIL_ERRNO( "sigfillset", status );
817 status = pthread_sigmask( SIG_UNBLOCK, & new_set, NULL );
818 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
819#endif /* KMP_BLOCK_SIGNALS */
820
821 KA_TRACE( 10, ("__kmp_launch_monitor: #4 finished\n" ) );
822
823 if( __kmp_global.g.g_abort != 0 ) {
824 /* now we need to terminate the worker threads */
825 /* the value of t_abort is the signal we caught */
826
827 int gtid;
828
829 KA_TRACE( 10, ("__kmp_launch_monitor: #5 terminate sig=%d\n", __kmp_global.g.g_abort ) );
830
831 /* terminate the OpenMP worker threads */
832 /* TODO this is not valid for sibling threads!!
833 * the uber master might not be 0 anymore.. */
834 for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
835 __kmp_terminate_thread( gtid );
836
837 __kmp_cleanup();
838
839 KA_TRACE( 10, ("__kmp_launch_monitor: #6 raise sig=%d\n", __kmp_global.g.g_abort ) );
840
841 if (__kmp_global.g.g_abort > 0)
842 raise( __kmp_global.g.g_abort );
843
844 }
845
846 KA_TRACE( 10, ("__kmp_launch_monitor: #7 exit\n" ) );
847
848 return thr;
849}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000850#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000851
852void
853__kmp_create_worker( int gtid, kmp_info_t *th, size_t stack_size )
854{
855 pthread_t handle;
856 pthread_attr_t thread_attr;
857 int status;
858
859
860 th->th.th_info.ds.ds_gtid = gtid;
861
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000862#if KMP_STATS_ENABLED
863 // sets up worker thread stats
864 __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
865
866 // th->th.th_stats is used to transfer thread specific stats-pointer to __kmp_launch_worker
867 // So when thread is created (goes into __kmp_launch_worker) it will
868 // set it's __thread local pointer to th->th.th_stats
869 th->th.th_stats = __kmp_stats_list.push_back(gtid);
870 if(KMP_UBER_GTID(gtid)) {
871 __kmp_stats_start_time = tsc_tick_count::now();
872 __kmp_stats_thread_ptr = th->th.th_stats;
873 __kmp_stats_init();
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000874 KMP_START_EXPLICIT_TIMER(OMP_worker_thread_life);
875 KMP_SET_THREAD_STATE(SERIAL_REGION);
876 KMP_INIT_PARTITIONED_TIMERS(OMP_serial);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000877 }
878 __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
879
880#endif // KMP_STATS_ENABLED
881
Jim Cownie5e8470a2013-09-27 10:38:44 +0000882 if ( KMP_UBER_GTID(gtid) ) {
883 KA_TRACE( 10, ("__kmp_create_worker: uber thread (%d)\n", gtid ) );
884 th -> th.th_info.ds.ds_thread = pthread_self();
885 __kmp_set_stack_info( gtid, th );
886 __kmp_check_stack_overlap( th );
887 return;
888 }; // if
889
890 KA_TRACE( 10, ("__kmp_create_worker: try to create thread (%d)\n", gtid ) );
891
892 KMP_MB(); /* Flush all pending memory write invalidates. */
893
894#ifdef KMP_THREAD_ATTR
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000895 status = pthread_attr_init( &thread_attr );
896 if ( status != 0 ) {
897 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantInitThreadAttrs ), KMP_ERR( status ), __kmp_msg_null);
898 }; // if
899 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
900 if ( status != 0 ) {
901 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantSetWorkerState ), KMP_ERR( status ), __kmp_msg_null);
902 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +0000903
Jonathan Peyton61118492016-05-20 19:03:38 +0000904 /* Set stack size for this thread now.
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000905 * The multiple of 2 is there because on some machines, requesting an unusual stacksize
906 * causes the thread to have an offset before the dummy alloca() takes place to create the
Jonathan Peyton61118492016-05-20 19:03:38 +0000907 * offset. Since we want the user to have a sufficient stacksize AND support a stack offset, we
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000908 * alloca() twice the offset so that the upcoming alloca() does not eliminate any premade
909 * offset, and also gives the user the stack space they requested for all threads */
910 stack_size += gtid * __kmp_stkoffset * 2;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000911
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000912 KA_TRACE( 10, ( "__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
913 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
914 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000915
916# ifdef _POSIX_THREAD_ATTR_STACKSIZE
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000917 status = pthread_attr_setstacksize( & thread_attr, stack_size );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000918# ifdef KMP_BACKUP_STKSIZE
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000919 if ( status != 0 ) {
920 if ( ! __kmp_env_stksize ) {
921 stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
922 __kmp_stksize = KMP_BACKUP_STKSIZE;
923 KA_TRACE( 10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
924 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
925 "bytes\n",
926 gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size )
927 );
928 status = pthread_attr_setstacksize( &thread_attr, stack_size );
929 }; // if
930 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +0000931# endif /* KMP_BACKUP_STKSIZE */
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000932 if ( status != 0 ) {
933 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantSetWorkerStackSize, stack_size ), KMP_ERR( status ),
934 KMP_HNT( ChangeWorkerStackSize ), __kmp_msg_null);
935 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +0000936# endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000937
Jim Cownie5e8470a2013-09-27 10:38:44 +0000938#endif /* KMP_THREAD_ATTR */
939
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000940 status = pthread_create( & handle, & thread_attr, __kmp_launch_worker, (void *) th );
941 if ( status != 0 || ! handle ) { // ??? Why do we check handle??
Jim Cownie5e8470a2013-09-27 10:38:44 +0000942#ifdef _POSIX_THREAD_ATTR_STACKSIZE
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000943 if ( status == EINVAL ) {
944 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantSetWorkerStackSize, stack_size ), KMP_ERR( status ),
945 KMP_HNT( IncreaseWorkerStackSize ), __kmp_msg_null);
946 };
947 if ( status == ENOMEM ) {
948 __kmp_msg(kmp_ms_fatal, KMP_MSG( CantSetWorkerStackSize, stack_size ), KMP_ERR( status ),
949 KMP_HNT( DecreaseWorkerStackSize ), __kmp_msg_null);
950 };
Jim Cownie5e8470a2013-09-27 10:38:44 +0000951#endif /* _POSIX_THREAD_ATTR_STACKSIZE */
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000952 if ( status == EAGAIN ) {
953 __kmp_msg(kmp_ms_fatal, KMP_MSG( NoResourcesForWorkerThread ), KMP_ERR( status ),
954 KMP_HNT( Decrease_NUM_THREADS ), __kmp_msg_null);
955 }; // if
956 KMP_SYSFAIL( "pthread_create", status );
957 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +0000958
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000959 th->th.th_info.ds.ds_thread = handle;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000960
961#ifdef KMP_THREAD_ATTR
Jonathan Peyton749b4d52016-01-27 21:02:04 +0000962 status = pthread_attr_destroy( & thread_attr );
963 if ( status ) {
964 __kmp_msg(kmp_ms_warning, KMP_MSG( CantDestroyThreadAttrs ), KMP_ERR( status ), __kmp_msg_null);
965 }; // if
Jim Cownie5e8470a2013-09-27 10:38:44 +0000966#endif /* KMP_THREAD_ATTR */
967
968 KMP_MB(); /* Flush all pending memory write invalidates. */
969
970 KA_TRACE( 10, ("__kmp_create_worker: done creating thread (%d)\n", gtid ) );
971
972} // __kmp_create_worker
973
974
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +0000975#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +0000976void
977__kmp_create_monitor( kmp_info_t *th )
978{
979 pthread_t handle;
980 pthread_attr_t thread_attr;
981 size_t size;
982 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000983 int auto_adj_size = FALSE;
984
Jonathan Peyton4fee5f62015-12-18 23:20:36 +0000985 if( __kmp_dflt_blocktime == KMP_MAX_BLOCKTIME ) {
986 // We don't need monitor thread in case of MAX_BLOCKTIME
987 KA_TRACE( 10, ("__kmp_create_monitor: skipping monitor thread because of MAX blocktime\n" ) );
988 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
989 th->th.th_info.ds.ds_gtid = 0;
990 return;
991 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000992 KA_TRACE( 10, ("__kmp_create_monitor: try to create monitor\n" ) );
993
994 KMP_MB(); /* Flush all pending memory write invalidates. */
995
996 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
997 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
998 #if KMP_REAL_TIME_FIX
999 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 +00001000 #else
1001 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001002 #endif // KMP_REAL_TIME_FIX
1003
1004 #ifdef KMP_THREAD_ATTR
1005 if ( __kmp_monitor_stksize == 0 ) {
1006 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1007 auto_adj_size = TRUE;
1008 }
1009 status = pthread_attr_init( &thread_attr );
1010 if ( status != 0 ) {
1011 __kmp_msg(
1012 kmp_ms_fatal,
1013 KMP_MSG( CantInitThreadAttrs ),
1014 KMP_ERR( status ),
1015 __kmp_msg_null
1016 );
1017 }; // if
1018 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1019 if ( status != 0 ) {
1020 __kmp_msg(
1021 kmp_ms_fatal,
1022 KMP_MSG( CantSetMonitorState ),
1023 KMP_ERR( status ),
1024 __kmp_msg_null
1025 );
1026 }; // if
1027
1028 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1029 status = pthread_attr_getstacksize( & thread_attr, & size );
1030 KMP_CHECK_SYSFAIL( "pthread_attr_getstacksize", status );
1031 #else
1032 size = __kmp_sys_min_stksize;
1033 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1034 #endif /* KMP_THREAD_ATTR */
1035
1036 if ( __kmp_monitor_stksize == 0 ) {
1037 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1038 }
1039 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1040 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1041 }
1042
1043 KA_TRACE( 10, ( "__kmp_create_monitor: default stacksize = %lu bytes,"
1044 "requested stacksize = %lu bytes\n",
1045 size, __kmp_monitor_stksize ) );
1046
1047 retry:
1048
1049 /* Set stack size for this thread now. */
1050
1051 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1052 KA_TRACE( 10, ( "__kmp_create_monitor: setting stacksize = %lu bytes,",
1053 __kmp_monitor_stksize ) );
1054 status = pthread_attr_setstacksize( & thread_attr, __kmp_monitor_stksize );
1055 if ( status != 0 ) {
1056 if ( auto_adj_size ) {
1057 __kmp_monitor_stksize *= 2;
1058 goto retry;
1059 }
1060 __kmp_msg(
1061 kmp_ms_warning, // should this be fatal? BB
1062 KMP_MSG( CantSetMonitorStackSize, (long int) __kmp_monitor_stksize ),
1063 KMP_ERR( status ),
1064 KMP_HNT( ChangeMonitorStackSize ),
1065 __kmp_msg_null
1066 );
1067 }; // if
1068 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1069
Jim Cownie5e8470a2013-09-27 10:38:44 +00001070 status = pthread_create( &handle, & thread_attr, __kmp_launch_monitor, (void *) th );
1071
1072 if ( status != 0 ) {
1073 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1074 if ( status == EINVAL ) {
1075 if ( auto_adj_size && ( __kmp_monitor_stksize < (size_t)0x40000000 ) ) {
1076 __kmp_monitor_stksize *= 2;
1077 goto retry;
1078 }
1079 __kmp_msg(
1080 kmp_ms_fatal,
1081 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1082 KMP_ERR( status ),
1083 KMP_HNT( IncreaseMonitorStackSize ),
1084 __kmp_msg_null
1085 );
1086 }; // if
1087 if ( status == ENOMEM ) {
1088 __kmp_msg(
1089 kmp_ms_fatal,
1090 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1091 KMP_ERR( status ),
1092 KMP_HNT( DecreaseMonitorStackSize ),
1093 __kmp_msg_null
1094 );
1095 }; // if
1096 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1097 if ( status == EAGAIN ) {
1098 __kmp_msg(
1099 kmp_ms_fatal,
1100 KMP_MSG( NoResourcesForMonitorThread ),
1101 KMP_ERR( status ),
1102 KMP_HNT( DecreaseNumberOfThreadsInUse ),
1103 __kmp_msg_null
1104 );
1105 }; // if
1106 KMP_SYSFAIL( "pthread_create", status );
1107 }; // if
1108
1109 th->th.th_info.ds.ds_thread = handle;
1110
1111 #if KMP_REAL_TIME_FIX
1112 // Wait for the monitor thread is really started and set its *priority*.
1113 KMP_DEBUG_ASSERT( sizeof( kmp_uint32 ) == sizeof( __kmp_global.g.g_time.dt.t_value ) );
1114 __kmp_wait_yield_4(
1115 (kmp_uint32 volatile *) & __kmp_global.g.g_time.dt.t_value, -1, & __kmp_neq_4, NULL
1116 );
1117 #endif // KMP_REAL_TIME_FIX
1118
1119 #ifdef KMP_THREAD_ATTR
1120 status = pthread_attr_destroy( & thread_attr );
1121 if ( status != 0 ) {
1122 __kmp_msg( //
1123 kmp_ms_warning,
1124 KMP_MSG( CantDestroyThreadAttrs ),
1125 KMP_ERR( status ),
1126 __kmp_msg_null
1127 );
1128 }; // if
1129 #endif
1130
1131 KMP_MB(); /* Flush all pending memory write invalidates. */
1132
1133 KA_TRACE( 10, ( "__kmp_create_monitor: monitor created %#.8lx\n", th->th.th_info.ds.ds_thread ) );
1134
1135} // __kmp_create_monitor
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001136#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001137
1138void
1139__kmp_exit_thread(
1140 int exit_status
1141) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001142 pthread_exit( (void *)(intptr_t) exit_status );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001143} // __kmp_exit_thread
1144
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001145#if KMP_USE_MONITOR
Jim Cownie07ea89f2014-09-03 11:10:54 +00001146void __kmp_resume_monitor();
1147
Jim Cownie5e8470a2013-09-27 10:38:44 +00001148void
1149__kmp_reap_monitor( kmp_info_t *th )
1150{
Jonathan Peyton7c4d66d2015-06-08 20:01:14 +00001151 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001152 void *exit_val;
1153
1154 KA_TRACE( 10, ("__kmp_reap_monitor: try to reap monitor thread with handle %#.8lx\n",
1155 th->th.th_info.ds.ds_thread ) );
1156
1157 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1158 // If both tid and gtid are 0, it means the monitor did not ever start.
1159 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1160 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1161 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
Jonathan Peyton4fee5f62015-12-18 23:20:36 +00001162 KA_TRACE( 10, ("__kmp_reap_monitor: monitor did not start, returning\n") );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001163 return;
1164 }; // if
1165
1166 KMP_MB(); /* Flush all pending memory write invalidates. */
1167
1168
Jonathan Peyton93495de2016-06-13 17:36:40 +00001169 /* First, check to see whether the monitor thread exists to wake it up. This is
1170 to avoid performance problem when the monitor sleeps during blocktime-size
1171 interval */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001172
1173 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
Jonathan Peyton93495de2016-06-13 17:36:40 +00001174 if (status != ESRCH) {
Jim Cownie07ea89f2014-09-03 11:10:54 +00001175 __kmp_resume_monitor(); // Wake up the monitor thread
Jonathan Peyton93495de2016-06-13 17:36:40 +00001176 }
1177 KA_TRACE( 10, ("__kmp_reap_monitor: try to join with monitor\n") );
1178 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1179 if (exit_val != th) {
1180 __kmp_msg(
1181 kmp_ms_fatal,
1182 KMP_MSG( ReapMonitorError ),
1183 KMP_ERR( status ),
1184 __kmp_msg_null
1185 );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001186 }
1187
1188 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1189 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1190
1191 KA_TRACE( 10, ("__kmp_reap_monitor: done reaping monitor thread with handle %#.8lx\n",
1192 th->th.th_info.ds.ds_thread ) );
1193
1194 KMP_MB(); /* Flush all pending memory write invalidates. */
1195
1196}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001197#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001198
1199void
1200__kmp_reap_worker( kmp_info_t *th )
1201{
1202 int status;
1203 void *exit_val;
1204
1205 KMP_MB(); /* Flush all pending memory write invalidates. */
1206
1207 KA_TRACE( 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid ) );
1208
Jonathan Peyton93495de2016-06-13 17:36:40 +00001209 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001210#ifdef KMP_DEBUG
Jonathan Peyton93495de2016-06-13 17:36:40 +00001211 /* Don't expose these to the user until we understand when they trigger */
1212 if ( status != 0 ) {
1213 __kmp_msg(kmp_ms_fatal, KMP_MSG( ReapWorkerError ), KMP_ERR( status ), __kmp_msg_null);
Jonathan Peyton749b4d52016-01-27 21:02:04 +00001214 }
Jonathan Peyton93495de2016-06-13 17:36:40 +00001215 if ( exit_val != th ) {
1216 KA_TRACE( 10, ( "__kmp_reap_worker: worker T#%d did not reap properly, exit_val = %p\n",
1217 th->th.th_info.ds.ds_gtid, exit_val ) );
1218 }
1219#endif /* KMP_DEBUG */
Jim Cownie5e8470a2013-09-27 10:38:44 +00001220
1221 KA_TRACE( 10, ("__kmp_reap_worker: done reaping T#%d\n", th->th.th_info.ds.ds_gtid ) );
1222
1223 KMP_MB(); /* Flush all pending memory write invalidates. */
1224}
1225
1226
1227/* ------------------------------------------------------------------------ */
1228/* ------------------------------------------------------------------------ */
1229
1230#if KMP_HANDLE_SIGNALS
1231
1232
1233static void
1234__kmp_null_handler( int signo )
1235{
1236 // Do nothing, for doing SIG_IGN-type actions.
1237} // __kmp_null_handler
1238
1239
1240static void
1241__kmp_team_handler( int signo )
1242{
1243 if ( __kmp_global.g.g_abort == 0 ) {
1244 /* Stage 1 signal handler, let's shut down all of the threads */
1245 #ifdef KMP_DEBUG
1246 __kmp_debug_printf( "__kmp_team_handler: caught signal = %d\n", signo );
1247 #endif
1248 switch ( signo ) {
1249 case SIGHUP :
1250 case SIGINT :
1251 case SIGQUIT :
1252 case SIGILL :
1253 case SIGABRT :
1254 case SIGFPE :
1255 case SIGBUS :
1256 case SIGSEGV :
1257 #ifdef SIGSYS
1258 case SIGSYS :
1259 #endif
1260 case SIGTERM :
1261 if ( __kmp_debug_buf ) {
1262 __kmp_dump_debug_buffer( );
1263 }; // if
1264 KMP_MB(); // Flush all pending memory write invalidates.
1265 TCW_4( __kmp_global.g.g_abort, signo );
1266 KMP_MB(); // Flush all pending memory write invalidates.
1267 TCW_4( __kmp_global.g.g_done, TRUE );
1268 KMP_MB(); // Flush all pending memory write invalidates.
1269 break;
1270 default:
1271 #ifdef KMP_DEBUG
1272 __kmp_debug_printf( "__kmp_team_handler: unknown signal type" );
1273 #endif
1274 break;
1275 }; // switch
1276 }; // if
1277} // __kmp_team_handler
1278
1279
1280static
1281void __kmp_sigaction( int signum, const struct sigaction * act, struct sigaction * oldact ) {
1282 int rc = sigaction( signum, act, oldact );
1283 KMP_CHECK_SYSFAIL_ERRNO( "sigaction", rc );
1284}
1285
1286
1287static void
1288__kmp_install_one_handler( int sig, sig_func_t handler_func, int parallel_init )
1289{
1290 KMP_MB(); // Flush all pending memory write invalidates.
1291 KB_TRACE( 60, ( "__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init ) );
1292 if ( parallel_init ) {
1293 struct sigaction new_action;
1294 struct sigaction old_action;
1295 new_action.sa_handler = handler_func;
1296 new_action.sa_flags = 0;
1297 sigfillset( & new_action.sa_mask );
1298 __kmp_sigaction( sig, & new_action, & old_action );
1299 if ( old_action.sa_handler == __kmp_sighldrs[ sig ].sa_handler ) {
1300 sigaddset( & __kmp_sigset, sig );
1301 } else {
1302 // Restore/keep user's handler if one previously installed.
1303 __kmp_sigaction( sig, & old_action, NULL );
1304 }; // if
1305 } else {
1306 // Save initial/system signal handlers to see if user handlers installed.
1307 __kmp_sigaction( sig, NULL, & __kmp_sighldrs[ sig ] );
1308 }; // if
1309 KMP_MB(); // Flush all pending memory write invalidates.
1310} // __kmp_install_one_handler
1311
1312
1313static void
1314__kmp_remove_one_handler( int sig )
1315{
1316 KB_TRACE( 60, ( "__kmp_remove_one_handler( %d )\n", sig ) );
1317 if ( sigismember( & __kmp_sigset, sig ) ) {
1318 struct sigaction old;
1319 KMP_MB(); // Flush all pending memory write invalidates.
1320 __kmp_sigaction( sig, & __kmp_sighldrs[ sig ], & old );
1321 if ( ( old.sa_handler != __kmp_team_handler ) && ( old.sa_handler != __kmp_null_handler ) ) {
1322 // Restore the users signal handler.
1323 KB_TRACE( 10, ( "__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1324 __kmp_sigaction( sig, & old, NULL );
1325 }; // if
1326 sigdelset( & __kmp_sigset, sig );
1327 KMP_MB(); // Flush all pending memory write invalidates.
1328 }; // if
1329} // __kmp_remove_one_handler
1330
1331
1332void
1333__kmp_install_signals( int parallel_init )
1334{
1335 KB_TRACE( 10, ( "__kmp_install_signals( %d )\n", parallel_init ) );
1336 if ( __kmp_handle_signals || ! parallel_init ) {
1337 // If ! parallel_init, we do not install handlers, just save original handlers.
1338 // Let us do it even __handle_signals is 0.
1339 sigemptyset( & __kmp_sigset );
1340 __kmp_install_one_handler( SIGHUP, __kmp_team_handler, parallel_init );
1341 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1342 __kmp_install_one_handler( SIGQUIT, __kmp_team_handler, parallel_init );
1343 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1344 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1345 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1346 __kmp_install_one_handler( SIGBUS, __kmp_team_handler, parallel_init );
1347 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1348 #ifdef SIGSYS
1349 __kmp_install_one_handler( SIGSYS, __kmp_team_handler, parallel_init );
1350 #endif // SIGSYS
1351 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1352 #ifdef SIGPIPE
1353 __kmp_install_one_handler( SIGPIPE, __kmp_team_handler, parallel_init );
1354 #endif // SIGPIPE
1355 }; // if
1356} // __kmp_install_signals
1357
1358
1359void
1360__kmp_remove_signals( void )
1361{
1362 int sig;
1363 KB_TRACE( 10, ( "__kmp_remove_signals()\n" ) );
1364 for ( sig = 1; sig < NSIG; ++ sig ) {
1365 __kmp_remove_one_handler( sig );
1366 }; // for sig
1367} // __kmp_remove_signals
1368
1369
1370#endif // KMP_HANDLE_SIGNALS
1371
1372/* ------------------------------------------------------------------------ */
1373/* ------------------------------------------------------------------------ */
1374
1375void
1376__kmp_enable( int new_state )
1377{
1378 #ifdef KMP_CANCEL_THREADS
1379 int status, old_state;
1380 status = pthread_setcancelstate( new_state, & old_state );
1381 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1382 KMP_DEBUG_ASSERT( old_state == PTHREAD_CANCEL_DISABLE );
1383 #endif
1384}
1385
1386void
1387__kmp_disable( int * old_state )
1388{
1389 #ifdef KMP_CANCEL_THREADS
1390 int status;
1391 status = pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, old_state );
1392 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1393 #endif
1394}
1395
1396/* ------------------------------------------------------------------------ */
1397/* ------------------------------------------------------------------------ */
1398
1399static void
1400__kmp_atfork_prepare (void)
1401{
1402 /* nothing to do */
1403}
1404
1405static void
1406__kmp_atfork_parent (void)
1407{
1408 /* nothing to do */
1409}
1410
1411/*
1412 Reset the library so execution in the child starts "all over again" with
1413 clean data structures in initial states. Don't worry about freeing memory
1414 allocated by parent, just abandon it to be safe.
1415*/
1416static void
1417__kmp_atfork_child (void)
1418{
1419 /* TODO make sure this is done right for nested/sibling */
1420 // ATT: Memory leaks are here? TODO: Check it and fix.
1421 /* KMP_ASSERT( 0 ); */
1422
1423 ++__kmp_fork_count;
1424
1425 __kmp_init_runtime = FALSE;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001426#if KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001427 __kmp_init_monitor = 0;
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001428#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001429 __kmp_init_parallel = FALSE;
1430 __kmp_init_middle = FALSE;
1431 __kmp_init_serial = FALSE;
1432 TCW_4(__kmp_init_gtid, FALSE);
1433 __kmp_init_common = FALSE;
1434
1435 TCW_4(__kmp_init_user_locks, FALSE);
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001436#if ! KMP_USE_DYNAMIC_LOCK
Jim Cownie07ea89f2014-09-03 11:10:54 +00001437 __kmp_user_lock_table.used = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001438 __kmp_user_lock_table.allocated = 0;
1439 __kmp_user_lock_table.table = NULL;
1440 __kmp_lock_blocks = NULL;
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001441#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001442
1443 __kmp_all_nth = 0;
1444 TCW_4(__kmp_nth, 0);
1445
1446 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate here
1447 so threadprivate doesn't use stale data */
1448 KA_TRACE( 10, ( "__kmp_atfork_child: checking cache address list %p\n",
1449 __kmp_threadpriv_cache_list ) );
1450
1451 while ( __kmp_threadpriv_cache_list != NULL ) {
1452
1453 if ( *__kmp_threadpriv_cache_list -> addr != NULL ) {
1454 KC_TRACE( 50, ( "__kmp_atfork_child: zeroing cache at address %p\n",
1455 &(*__kmp_threadpriv_cache_list -> addr) ) );
1456
1457 *__kmp_threadpriv_cache_list -> addr = NULL;
1458 }
1459 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list -> next;
1460 }
1461
1462 __kmp_init_runtime = FALSE;
1463
1464 /* reset statically initialized locks */
1465 __kmp_init_bootstrap_lock( &__kmp_initz_lock );
1466 __kmp_init_bootstrap_lock( &__kmp_stdio_lock );
1467 __kmp_init_bootstrap_lock( &__kmp_console_lock );
1468
1469 /* This is necessary to make sure no stale data is left around */
1470 /* AC: customers complain that we use unsafe routines in the atfork
1471 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1472 in dynamic_link when check the presence of shared tbbmalloc library.
1473 Suggestion is to make the library initialization lazier, similar
1474 to what done for __kmpc_begin(). */
1475 // TODO: synchronize all static initializations with regular library
1476 // startup; look at kmp_global.c and etc.
1477 //__kmp_internal_begin ();
1478
1479}
1480
1481void
1482__kmp_register_atfork(void) {
1483 if ( __kmp_need_register_atfork ) {
1484 int status = pthread_atfork( __kmp_atfork_prepare, __kmp_atfork_parent, __kmp_atfork_child );
1485 KMP_CHECK_SYSFAIL( "pthread_atfork", status );
1486 __kmp_need_register_atfork = FALSE;
1487 }
1488}
1489
1490void
1491__kmp_suspend_initialize( void )
1492{
1493 int status;
1494 status = pthread_mutexattr_init( &__kmp_suspend_mutex_attr );
1495 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
1496 status = pthread_condattr_init( &__kmp_suspend_cond_attr );
1497 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
1498}
1499
1500static void
1501__kmp_suspend_initialize_thread( kmp_info_t *th )
1502{
Jonas Hahnfeld50fed042016-11-07 15:58:36 +00001503 ANNOTATE_HAPPENS_AFTER(&th->th.th_suspend_init_count);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001504 if ( th->th.th_suspend_init_count <= __kmp_fork_count ) {
1505 /* this means we haven't initialized the suspension pthread objects for this thread
1506 in this instance of the process */
1507 int status;
1508 status = pthread_cond_init( &th->th.th_suspend_cv.c_cond, &__kmp_suspend_cond_attr );
1509 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
1510 status = pthread_mutex_init( &th->th.th_suspend_mx.m_mutex, & __kmp_suspend_mutex_attr );
1511 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
1512 *(volatile int*)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
Jonas Hahnfeld50fed042016-11-07 15:58:36 +00001513 ANNOTATE_HAPPENS_BEFORE(&th->th.th_suspend_init_count);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001514 };
1515}
1516
1517void
1518__kmp_suspend_uninitialize_thread( kmp_info_t *th )
1519{
1520 if(th->th.th_suspend_init_count > __kmp_fork_count) {
1521 /* this means we have initialize the suspension pthread objects for this thread
1522 in this instance of the process */
1523 int status;
1524
1525 status = pthread_cond_destroy( &th->th.th_suspend_cv.c_cond );
1526 if ( status != 0 && status != EBUSY ) {
1527 KMP_SYSFAIL( "pthread_cond_destroy", status );
1528 };
1529 status = pthread_mutex_destroy( &th->th.th_suspend_mx.m_mutex );
1530 if ( status != 0 && status != EBUSY ) {
1531 KMP_SYSFAIL( "pthread_mutex_destroy", status );
1532 };
1533 --th->th.th_suspend_init_count;
1534 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1535 }
1536}
1537
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001538/* This routine puts the calling thread to sleep after setting the
1539 * sleep bit for the indicated flag variable to true.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001540 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001541template <class C>
1542static inline void __kmp_suspend_template( int th_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001543{
Jonathan Peyton45be4502015-08-11 21:36:41 +00001544 KMP_TIME_DEVELOPER_BLOCK(USER_suspend);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001545 kmp_info_t *th = __kmp_threads[th_gtid];
1546 int status;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001547 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001548
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001549 KF_TRACE( 30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001550
1551 __kmp_suspend_initialize_thread( th );
1552
1553 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1554 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1555
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001556 KF_TRACE( 10, ( "__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1557 th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001558
1559 /* TODO: shouldn't this use release semantics to ensure that __kmp_suspend_initialize_thread
1560 gets called first?
1561 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001562 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001563
Jonathan Peytone03b62f2015-10-08 18:49:40 +00001564 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x, was %x\n",
1565 th_gtid, flag->get(), *(flag->get()), old_spin ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001566
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001567 if ( flag->done_check_val(old_spin) ) {
1568 old_spin = flag->unset_sleeping();
1569 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d false alarm, reset sleep bit for spin(%p)\n",
1570 th_gtid, flag->get()) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001571 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001572 /* Encapsulate in a loop as the documentation states that this may
1573 * "with low probability" return when the condition variable has
1574 * not been signaled or broadcast
1575 */
1576 int deactivated = FALSE;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001577 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1578 while ( flag->is_sleeping() ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001579#ifdef DEBUG_SUSPEND
1580 char buffer[128];
1581 __kmp_suspend_count++;
1582 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001583 __kmp_printf( "__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001584#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001585 // Mark the thread as no longer active (only in the first iteration of the loop).
Jim Cownie5e8470a2013-09-27 10:38:44 +00001586 if ( ! deactivated ) {
1587 th->th.th_active = FALSE;
1588 if ( th->th.th_active_in_pool ) {
1589 th->th.th_active_in_pool = FALSE;
1590 KMP_TEST_THEN_DEC32(
1591 (kmp_int32 *) &__kmp_thread_pool_active_nth );
1592 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
1593 }
1594 deactivated = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001595 }
1596
1597#if USE_SUSPEND_TIMEOUT
1598 struct timespec now;
1599 struct timeval tval;
1600 int msecs;
1601
1602 status = gettimeofday( &tval, NULL );
1603 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1604 TIMEVAL_TO_TIMESPEC( &tval, &now );
1605
1606 msecs = (4*__kmp_dflt_blocktime) + 200;
1607 now.tv_sec += msecs / 1000;
1608 now.tv_nsec += (msecs % 1000)*1000;
1609
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001610 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_timedwait\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001611 th_gtid ) );
1612 status = pthread_cond_timedwait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex, & now );
1613#else
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001614 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_wait\n",
Jonathan Peyton1bd61b42015-10-08 19:44:16 +00001615 th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001616 status = pthread_cond_wait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex );
1617#endif
1618
1619 if ( (status != 0) && (status != EINTR) && (status != ETIMEDOUT) ) {
1620 KMP_SYSFAIL( "pthread_cond_wait", status );
1621 }
1622#ifdef KMP_DEBUG
1623 if (status == ETIMEDOUT) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001624 if ( flag->is_sleeping() ) {
1625 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001626 } else {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001627 KF_TRACE( 2, ( "__kmp_suspend_template: T#%d timeout wakeup, sleep bit not set!\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001628 th_gtid ) );
1629 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001630 } else if ( flag->is_sleeping() ) {
1631 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001632 }
1633#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001634 } // while
1635
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001636 // Mark the thread as active again (if it was previous marked as inactive)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001637 if ( deactivated ) {
1638 th->th.th_active = TRUE;
1639 if ( TCR_4(th->th.th_in_pool) ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001640 KMP_TEST_THEN_INC32( (kmp_int32 *) &__kmp_thread_pool_active_nth );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001641 th->th.th_active_in_pool = TRUE;
1642 }
1643 }
1644 }
1645
1646#ifdef DEBUG_SUSPEND
1647 {
1648 char buffer[128];
1649 __kmp_print_cond( buffer, &th->th.th_suspend_cv);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001650 __kmp_printf( "__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001651 }
1652#endif
1653
Jim Cownie5e8470a2013-09-27 10:38:44 +00001654 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1655 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1656
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001657 KF_TRACE( 30, ("__kmp_suspend_template: T#%d exit\n", th_gtid ) );
1658}
1659
1660void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
1661 __kmp_suspend_template(th_gtid, flag);
1662}
1663void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
1664 __kmp_suspend_template(th_gtid, flag);
1665}
1666void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
1667 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001668}
1669
1670
1671/* This routine signals the thread specified by target_gtid to wake up
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001672 * after setting the sleep bit indicated by the flag argument to FALSE.
1673 * The target thread must already have called __kmp_suspend_template()
Jim Cownie5e8470a2013-09-27 10:38:44 +00001674 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001675template <class C>
1676static inline void __kmp_resume_template( int target_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001677{
Jonathan Peyton45be4502015-08-11 21:36:41 +00001678 KMP_TIME_DEVELOPER_BLOCK(USER_resume);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001679 kmp_info_t *th = __kmp_threads[target_gtid];
1680 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001681
1682#ifdef KMP_DEBUG
1683 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1684#endif
1685
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001686 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 +00001687 KMP_DEBUG_ASSERT( gtid != target_gtid );
1688
1689 __kmp_suspend_initialize_thread( th );
1690
1691 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1692 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001693
Jonathan Peyton3f5dfc22015-11-09 16:31:51 +00001694 if (!flag) { // coming from __kmp_null_resume_wrapper
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001695 flag = (C *)th->th.th_sleep_loc;
1696 }
1697
Jonathan Peyton3f5dfc22015-11-09 16:31:51 +00001698 // First, check if the flag is null or its type has changed. If so, someone else woke it up.
1699 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type simply shows what flag was cast to
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001700 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p)\n",
1701 gtid, target_gtid, NULL ) );
1702 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1703 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1704 return;
1705 }
Jonathan Peyton1bd61b42015-10-08 19:44:16 +00001706 else { // if multiple threads are sleeping, flag should be internally referring to a specific thread here
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001707 typename C::flag_t old_spin = flag->unset_sleeping();
1708 if ( ! flag->is_sleeping_val(old_spin) ) {
1709 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p): "
1710 "%u => %u\n",
1711 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001712 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1713 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1714 return;
1715 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001716 KF_TRACE( 5, ( "__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p): "
1717 "%u => %u\n",
1718 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001719 }
1720 TCW_PTR(th->th.th_sleep_loc, NULL);
1721
Jim Cownie5e8470a2013-09-27 10:38:44 +00001722
1723#ifdef DEBUG_SUSPEND
1724 {
1725 char buffer[128];
1726 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001727 __kmp_printf( "__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, target_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001728 }
1729#endif
1730
Jim Cownie5e8470a2013-09-27 10:38:44 +00001731 status = pthread_cond_signal( &th->th.th_suspend_cv.c_cond );
1732 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1733 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1734 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001735 KF_TRACE( 30, ( "__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001736 gtid, target_gtid ) );
1737}
1738
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001739void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
1740 __kmp_resume_template(target_gtid, flag);
1741}
1742void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
1743 __kmp_resume_template(target_gtid, flag);
1744}
1745void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
1746 __kmp_resume_template(target_gtid, flag);
1747}
1748
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001749#if KMP_USE_MONITOR
Jim Cownie07ea89f2014-09-03 11:10:54 +00001750void
1751__kmp_resume_monitor()
1752{
Jonathan Peyton11dc82f2016-05-05 16:15:57 +00001753 KMP_TIME_DEVELOPER_BLOCK(USER_resume);
Jim Cownie07ea89f2014-09-03 11:10:54 +00001754 int status;
1755#ifdef KMP_DEBUG
1756 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1757 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n",
1758 gtid, KMP_GTID_MONITOR ) );
1759 KMP_DEBUG_ASSERT( gtid != KMP_GTID_MONITOR );
1760#endif
1761 status = pthread_mutex_lock( &__kmp_wait_mx.m_mutex );
1762 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1763#ifdef DEBUG_SUSPEND
1764 {
1765 char buffer[128];
1766 __kmp_print_cond( buffer, &__kmp_wait_cv.c_cond );
1767 __kmp_printf( "__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid, KMP_GTID_MONITOR, buffer );
1768 }
1769#endif
1770 status = pthread_cond_signal( &__kmp_wait_cv.c_cond );
1771 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1772 status = pthread_mutex_unlock( &__kmp_wait_mx.m_mutex );
1773 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1774 KF_TRACE( 30, ( "__kmp_resume_monitor: T#%d exiting after signaling wake up for T#%d\n",
1775 gtid, KMP_GTID_MONITOR ) );
1776}
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001777#endif // KMP_USE_MONITOR
Jim Cownie5e8470a2013-09-27 10:38:44 +00001778
1779/* ------------------------------------------------------------------------ */
1780/* ------------------------------------------------------------------------ */
1781
1782void
1783__kmp_yield( int cond )
1784{
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00001785 if (cond
1786#if KMP_USE_MONITOR
1787 && __kmp_yielding_on
1788#endif
1789 ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001790 sched_yield();
1791 }
1792}
1793
1794/* ------------------------------------------------------------------------ */
1795/* ------------------------------------------------------------------------ */
1796
1797void
1798__kmp_gtid_set_specific( int gtid )
1799{
Jonathan Peytonf2520102016-04-18 21:33:01 +00001800 if( __kmp_init_gtid ) {
1801 int status;
1802 status = pthread_setspecific( __kmp_gtid_threadprivate_key, (void*)(intptr_t)(gtid+1) );
1803 KMP_CHECK_SYSFAIL( "pthread_setspecific", status );
1804 } else {
1805 KA_TRACE( 50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n" ) );
1806 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001807}
1808
1809int
1810__kmp_gtid_get_specific()
1811{
1812 int gtid;
Jonathan Peytonf2520102016-04-18 21:33:01 +00001813 if ( !__kmp_init_gtid ) {
1814 KA_TRACE( 50, ("__kmp_gtid_get_specific: runtime shutdown, returning KMP_GTID_SHUTDOWN\n" ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001815 return KMP_GTID_SHUTDOWN;
1816 }
1817 gtid = (int)(size_t)pthread_getspecific( __kmp_gtid_threadprivate_key );
1818 if ( gtid == 0 ) {
1819 gtid = KMP_GTID_DNE;
1820 }
1821 else {
1822 gtid--;
1823 }
1824 KA_TRACE( 50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1825 __kmp_gtid_threadprivate_key, gtid ));
1826 return gtid;
1827}
1828
1829/* ------------------------------------------------------------------------ */
1830/* ------------------------------------------------------------------------ */
1831
1832double
1833__kmp_read_cpu_time( void )
1834{
1835 /*clock_t t;*/
1836 struct tms buffer;
1837
1838 /*t =*/ times( & buffer );
1839
1840 return (buffer.tms_utime + buffer.tms_cutime) / (double) CLOCKS_PER_SEC;
1841}
1842
1843int
1844__kmp_read_system_info( struct kmp_sys_info *info )
1845{
1846 int status;
1847 struct rusage r_usage;
1848
1849 memset( info, 0, sizeof( *info ) );
1850
1851 status = getrusage( RUSAGE_SELF, &r_usage);
1852 KMP_CHECK_SYSFAIL_ERRNO( "getrusage", status );
1853
1854 info->maxrss = r_usage.ru_maxrss; /* the maximum resident set size utilized (in kilobytes) */
1855 info->minflt = r_usage.ru_minflt; /* the number of page faults serviced without any I/O */
1856 info->majflt = r_usage.ru_majflt; /* the number of page faults serviced that required I/O */
1857 info->nswap = r_usage.ru_nswap; /* the number of times a process was "swapped" out of memory */
1858 info->inblock = r_usage.ru_inblock; /* the number of times the file system had to perform input */
1859 info->oublock = r_usage.ru_oublock; /* the number of times the file system had to perform output */
1860 info->nvcsw = r_usage.ru_nvcsw; /* the number of times a context switch was voluntarily */
1861 info->nivcsw = r_usage.ru_nivcsw; /* the number of times a context switch was forced */
1862
1863 return (status != 0);
1864}
1865
1866/* ------------------------------------------------------------------------ */
1867/* ------------------------------------------------------------------------ */
1868
Jim Cownie5e8470a2013-09-27 10:38:44 +00001869void
1870__kmp_read_system_time( double *delta )
1871{
1872 double t_ns;
1873 struct timeval tval;
1874 struct timespec stop;
1875 int status;
1876
1877 status = gettimeofday( &tval, NULL );
1878 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1879 TIMEVAL_TO_TIMESPEC( &tval, &stop );
1880 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
1881 *delta = (t_ns * 1e-9);
1882}
1883
1884void
1885__kmp_clear_system_time( void )
1886{
1887 struct timeval tval;
1888 int status;
1889 status = gettimeofday( &tval, NULL );
1890 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1891 TIMEVAL_TO_TIMESPEC( &tval, &__kmp_sys_timer_data.start );
1892}
1893
1894/* ------------------------------------------------------------------------ */
1895/* ------------------------------------------------------------------------ */
1896
1897#ifdef BUILD_TV
1898
1899void
1900__kmp_tv_threadprivate_store( kmp_info_t *th, void *global_addr, void *thread_addr )
1901{
1902 struct tv_data *p;
1903
1904 p = (struct tv_data *) __kmp_allocate( sizeof( *p ) );
1905
1906 p->u.tp.global_addr = global_addr;
1907 p->u.tp.thread_addr = thread_addr;
1908
1909 p->type = (void *) 1;
1910
1911 p->next = th->th.th_local.tv_data;
1912 th->th.th_local.tv_data = p;
1913
1914 if ( p->next == 0 ) {
1915 int rc = pthread_setspecific( __kmp_tv_key, p );
1916 KMP_CHECK_SYSFAIL( "pthread_setspecific", rc );
1917 }
1918}
1919
1920#endif /* BUILD_TV */
1921
1922/* ------------------------------------------------------------------------ */
1923/* ------------------------------------------------------------------------ */
1924
1925static int
1926__kmp_get_xproc( void ) {
1927
1928 int r = 0;
1929
Joerg Sonnenberger7649cd42015-09-21 20:29:12 +00001930 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00001931
1932 r = sysconf( _SC_NPROCESSORS_ONLN );
1933
1934 #elif KMP_OS_DARWIN
1935
1936 // Bug C77011 High "OpenMP Threads and number of active cores".
1937
1938 // Find the number of available CPUs.
1939 kern_return_t rc;
1940 host_basic_info_data_t info;
1941 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
1942 rc = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) & info, & num );
1943 if ( rc == 0 && num == HOST_BASIC_INFO_COUNT ) {
1944 // Cannot use KA_TRACE() here because this code works before trace support is
1945 // initialized.
1946 r = info.avail_cpus;
1947 } else {
1948 KMP_WARNING( CantGetNumAvailCPU );
1949 KMP_INFORM( AssumedNumCPU );
1950 }; // if
1951
1952 #else
1953
1954 #error "Unknown or unsupported OS."
1955
1956 #endif
1957
1958 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
1959
1960} // __kmp_get_xproc
1961
Jim Cownie181b4bb2013-12-23 17:28:57 +00001962int
1963__kmp_read_from_file( char const *path, char const *format, ... )
1964{
1965 int result;
1966 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001967
Jim Cownie181b4bb2013-12-23 17:28:57 +00001968 va_start(args, format);
1969 FILE *f = fopen(path, "rb");
1970 if ( f == NULL )
1971 return 0;
1972 result = vfscanf(f, format, args);
1973 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001974
Jim Cownie5e8470a2013-09-27 10:38:44 +00001975 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00001976}
Jim Cownie5e8470a2013-09-27 10:38:44 +00001977
1978void
1979__kmp_runtime_initialize( void )
1980{
1981 int status;
1982 pthread_mutexattr_t mutex_attr;
1983 pthread_condattr_t cond_attr;
1984
1985 if ( __kmp_init_runtime ) {
1986 return;
1987 }; // if
1988
1989 #if ( KMP_ARCH_X86 || KMP_ARCH_X86_64 )
1990 if ( ! __kmp_cpuinfo.initialized ) {
1991 __kmp_query_cpuid( &__kmp_cpuinfo );
1992 }; // if
1993 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
1994
Jim Cownie5e8470a2013-09-27 10:38:44 +00001995 __kmp_xproc = __kmp_get_xproc();
1996
1997 if ( sysconf( _SC_THREADS ) ) {
1998
1999 /* Query the maximum number of threads */
2000 __kmp_sys_max_nth = sysconf( _SC_THREAD_THREADS_MAX );
2001 if ( __kmp_sys_max_nth == -1 ) {
2002 /* Unlimited threads for NPTL */
2003 __kmp_sys_max_nth = INT_MAX;
2004 }
2005 else if ( __kmp_sys_max_nth <= 1 ) {
2006 /* Can't tell, just use PTHREAD_THREADS_MAX */
2007 __kmp_sys_max_nth = KMP_MAX_NTH;
2008 }
2009
2010 /* Query the minimum stack size */
2011 __kmp_sys_min_stksize = sysconf( _SC_THREAD_STACK_MIN );
2012 if ( __kmp_sys_min_stksize <= 1 ) {
2013 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
2014 }
2015 }
2016
2017 /* Set up minimum number of threads to switch to TLS gtid */
2018 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
2019
Jim Cownie5e8470a2013-09-27 10:38:44 +00002020 #ifdef BUILD_TV
2021 {
2022 int rc = pthread_key_create( & __kmp_tv_key, 0 );
2023 KMP_CHECK_SYSFAIL( "pthread_key_create", rc );
2024 }
2025 #endif
2026
2027 status = pthread_key_create( &__kmp_gtid_threadprivate_key, __kmp_internal_end_dest );
2028 KMP_CHECK_SYSFAIL( "pthread_key_create", status );
2029 status = pthread_mutexattr_init( & mutex_attr );
2030 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
2031 status = pthread_mutex_init( & __kmp_wait_mx.m_mutex, & mutex_attr );
2032 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
2033 status = pthread_condattr_init( & cond_attr );
2034 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
2035 status = pthread_cond_init( & __kmp_wait_cv.c_cond, & cond_attr );
2036 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
2037#if USE_ITT_BUILD
2038 __kmp_itt_initialize();
2039#endif /* USE_ITT_BUILD */
2040
2041 __kmp_init_runtime = TRUE;
2042}
2043
2044void
2045__kmp_runtime_destroy( void )
2046{
2047 int status;
2048
2049 if ( ! __kmp_init_runtime ) {
2050 return; // Nothing to do.
2051 };
2052
2053#if USE_ITT_BUILD
2054 __kmp_itt_destroy();
2055#endif /* USE_ITT_BUILD */
2056
2057 status = pthread_key_delete( __kmp_gtid_threadprivate_key );
2058 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2059 #ifdef BUILD_TV
2060 status = pthread_key_delete( __kmp_tv_key );
2061 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2062 #endif
2063
2064 status = pthread_mutex_destroy( & __kmp_wait_mx.m_mutex );
2065 if ( status != 0 && status != EBUSY ) {
2066 KMP_SYSFAIL( "pthread_mutex_destroy", status );
2067 }
2068 status = pthread_cond_destroy( & __kmp_wait_cv.c_cond );
2069 if ( status != 0 && status != EBUSY ) {
2070 KMP_SYSFAIL( "pthread_cond_destroy", status );
2071 }
Alp Toker763b9392014-02-28 09:42:41 +00002072 #if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00002073 __kmp_affinity_uninitialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00002074 #endif
2075
2076 __kmp_init_runtime = FALSE;
2077}
2078
2079
2080/* Put the thread to sleep for a time period */
2081/* NOTE: not currently used anywhere */
2082void
2083__kmp_thread_sleep( int millis )
2084{
2085 sleep( ( millis + 500 ) / 1000 );
2086}
2087
2088/* Calculate the elapsed wall clock time for the user */
2089void
2090__kmp_elapsed( double *t )
2091{
2092 int status;
2093# ifdef FIX_SGI_CLOCK
2094 struct timespec ts;
2095
2096 status = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
2097 KMP_CHECK_SYSFAIL_ERRNO( "clock_gettime", status );
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +00002098 *t = (double) ts.tv_nsec * (1.0 / (double) KMP_NSEC_PER_SEC) +
Jim Cownie5e8470a2013-09-27 10:38:44 +00002099 (double) ts.tv_sec;
2100# else
2101 struct timeval tv;
2102
2103 status = gettimeofday( & tv, NULL );
2104 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +00002105 *t = (double) tv.tv_usec * (1.0 / (double) KMP_USEC_PER_SEC) +
Jim Cownie5e8470a2013-09-27 10:38:44 +00002106 (double) tv.tv_sec;
2107# endif
2108}
2109
2110/* Calculate the elapsed wall clock tick for the user */
2111void
2112__kmp_elapsed_tick( double *t )
2113{
2114 *t = 1 / (double) CLOCKS_PER_SEC;
2115}
2116
Jonathan Peyton377aa402016-04-14 16:00:37 +00002117/* Return the current time stamp in nsec */
2118kmp_uint64
2119__kmp_now_nsec()
2120{
2121 struct timeval t;
2122 gettimeofday(&t, NULL);
2123 return KMP_NSEC_PER_SEC*t.tv_sec + 1000*t.tv_usec;
2124}
2125
Jonathan Peytonb66d1aa2016-09-27 17:11:17 +00002126#if KMP_ARCH_X86 || KMP_ARCH_X86_64
2127/* Measure clock tick per nanosecond */
2128void
2129__kmp_initialize_system_tick()
2130{
2131 kmp_uint64 delay = 100000; // 50~100 usec on most machines.
2132 kmp_uint64 nsec = __kmp_now_nsec();
2133 kmp_uint64 goal = __kmp_hardware_timestamp() + delay;
2134 kmp_uint64 now;
2135 while ((now = __kmp_hardware_timestamp()) < goal);
2136 __kmp_ticks_per_nsec = 1.0 * (delay + (now - goal)) / (__kmp_now_nsec() - nsec);
2137}
2138#endif
2139
Jim Cownie5e8470a2013-09-27 10:38:44 +00002140/*
2141 Determine whether the given address is mapped into the current address space.
2142*/
2143
2144int
2145__kmp_is_address_mapped( void * addr ) {
2146
2147 int found = 0;
2148 int rc;
2149
Joerg Sonnenberger7649cd42015-09-21 20:29:12 +00002150 #if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00002151
2152 /*
2153 On Linux* OS, read the /proc/<pid>/maps pseudo-file to get all the address ranges mapped
2154 into the address space.
2155 */
2156
2157 char * name = __kmp_str_format( "/proc/%d/maps", getpid() );
2158 FILE * file = NULL;
2159
2160 file = fopen( name, "r" );
2161 KMP_ASSERT( file != NULL );
2162
2163 for ( ; ; ) {
2164
2165 void * beginning = NULL;
2166 void * ending = NULL;
2167 char perms[ 5 ];
2168
2169 rc = fscanf( file, "%p-%p %4s %*[^\n]\n", & beginning, & ending, perms );
2170 if ( rc == EOF ) {
2171 break;
2172 }; // if
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002173 KMP_ASSERT( rc == 3 && KMP_STRLEN( perms ) == 4 ); // Make sure all fields are read.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002174
2175 // Ending address is not included in the region, but beginning is.
2176 if ( ( addr >= beginning ) && ( addr < ending ) ) {
2177 perms[ 2 ] = 0; // 3th and 4th character does not matter.
2178 if ( strcmp( perms, "rw" ) == 0 ) {
2179 // Memory we are looking for should be readable and writable.
2180 found = 1;
2181 }; // if
2182 break;
2183 }; // if
2184
2185 }; // forever
2186
2187 // Free resources.
2188 fclose( file );
2189 KMP_INTERNAL_FREE( name );
2190
2191 #elif KMP_OS_DARWIN
2192
2193 /*
2194 On OS X*, /proc pseudo filesystem is not available. Try to read memory using vm
2195 interface.
2196 */
2197
2198 int buffer;
2199 vm_size_t count;
2200 rc =
2201 vm_read_overwrite(
2202 mach_task_self(), // Task to read memory of.
2203 (vm_address_t)( addr ), // Address to read from.
2204 1, // Number of bytes to be read.
2205 (vm_address_t)( & buffer ), // Address of buffer to save read bytes in.
2206 & count // Address of var to save number of read bytes in.
2207 );
2208 if ( rc == 0 ) {
2209 // Memory successfully read.
2210 found = 1;
2211 }; // if
2212
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +00002213 #elif KMP_OS_FREEBSD || KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +00002214
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +00002215 // FIXME(FreeBSD, NetBSD): Implement this
Alp Toker763b9392014-02-28 09:42:41 +00002216 found = 1;
2217
Jim Cownie5e8470a2013-09-27 10:38:44 +00002218 #else
2219
2220 #error "Unknown or unsupported OS"
2221
2222 #endif
2223
2224 return found;
2225
2226} // __kmp_is_address_mapped
2227
2228#ifdef USE_LOAD_BALANCE
2229
2230
2231# if KMP_OS_DARWIN
2232
2233// The function returns the rounded value of the system load average
2234// during given time interval which depends on the value of
2235// __kmp_load_balance_interval variable (default is 60 sec, other values
2236// may be 300 sec or 900 sec).
2237// It returns -1 in case of error.
2238int
2239__kmp_get_load_balance( int max )
2240{
2241 double averages[3];
2242 int ret_avg = 0;
2243
2244 int res = getloadavg( averages, 3 );
2245
2246 //Check __kmp_load_balance_interval to determine which of averages to use.
2247 // getloadavg() may return the number of samples less than requested that is
2248 // less than 3.
2249 if ( __kmp_load_balance_interval < 180 && ( res >= 1 ) ) {
2250 ret_avg = averages[0];// 1 min
2251 } else if ( ( __kmp_load_balance_interval >= 180
2252 && __kmp_load_balance_interval < 600 ) && ( res >= 2 ) ) {
2253 ret_avg = averages[1];// 5 min
2254 } else if ( ( __kmp_load_balance_interval >= 600 ) && ( res == 3 ) ) {
2255 ret_avg = averages[2];// 15 min
Alp Toker8f2d3f02014-02-24 10:40:15 +00002256 } else {// Error occurred
Jim Cownie5e8470a2013-09-27 10:38:44 +00002257 return -1;
2258 }
2259
2260 return ret_avg;
2261}
2262
2263# else // Linux* OS
2264
2265// The fuction returns number of running (not sleeping) threads, or -1 in case of error.
2266// Error could be reported if Linux* OS kernel too old (without "/proc" support).
2267// Counting running threads stops if max running threads encountered.
2268int
2269__kmp_get_load_balance( int max )
2270{
2271 static int permanent_error = 0;
2272
2273 static int glb_running_threads = 0; /* Saved count of the running threads for the thread balance algortihm */
2274 static double glb_call_time = 0; /* Thread balance algorithm call time */
2275
2276 int running_threads = 0; // Number of running threads in the system.
2277
2278 DIR * proc_dir = NULL; // Handle of "/proc/" directory.
2279 struct dirent * proc_entry = NULL;
2280
2281 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2282 DIR * task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2283 struct dirent * task_entry = NULL;
2284 int task_path_fixed_len;
2285
2286 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2287 int stat_file = -1;
2288 int stat_path_fixed_len;
2289
2290 int total_processes = 0; // Total number of processes in system.
2291 int total_threads = 0; // Total number of threads in system.
2292
2293 double call_time = 0.0;
2294
2295 __kmp_str_buf_init( & task_path );
2296 __kmp_str_buf_init( & stat_path );
2297
2298 __kmp_elapsed( & call_time );
2299
2300 if ( glb_call_time &&
2301 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
2302 running_threads = glb_running_threads;
2303 goto finish;
2304 }
2305
2306 glb_call_time = call_time;
2307
2308 // Do not spend time on scanning "/proc/" if we have a permanent error.
2309 if ( permanent_error ) {
2310 running_threads = -1;
2311 goto finish;
2312 }; // if
2313
2314 if ( max <= 0 ) {
2315 max = INT_MAX;
2316 }; // if
2317
2318 // Open "/proc/" directory.
2319 proc_dir = opendir( "/proc" );
2320 if ( proc_dir == NULL ) {
2321 // Cannot open "/prroc/". Probably the kernel does not support it. Return an error now and
2322 // in subsequent calls.
2323 running_threads = -1;
2324 permanent_error = 1;
2325 goto finish;
2326 }; // if
2327
2328 // Initialize fixed part of task_path. This part will not change.
2329 __kmp_str_buf_cat( & task_path, "/proc/", 6 );
2330 task_path_fixed_len = task_path.used; // Remember number of used characters.
2331
2332 proc_entry = readdir( proc_dir );
2333 while ( proc_entry != NULL ) {
2334 // Proc entry is a directory and name starts with a digit. Assume it is a process'
2335 // directory.
2336 if ( proc_entry->d_type == DT_DIR && isdigit( proc_entry->d_name[ 0 ] ) ) {
2337
2338 ++ total_processes;
2339 // Make sure init process is the very first in "/proc", so we can replace
2340 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes == 1.
2341 // We are going to check that total_processes == 1 => d_name == "1" is true (where
2342 // "=>" is implication). Since C++ does not have => operator, let us replace it with its
2343 // equivalent: a => b == ! a || b.
2344 KMP_DEBUG_ASSERT( total_processes != 1 || strcmp( proc_entry->d_name, "1" ) == 0 );
2345
2346 // Construct task_path.
2347 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002348 __kmp_str_buf_cat( & task_path, proc_entry->d_name, KMP_STRLEN( proc_entry->d_name ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002349 __kmp_str_buf_cat( & task_path, "/task", 5 );
2350
2351 task_dir = opendir( task_path.str );
2352 if ( task_dir == NULL ) {
2353 // Process can finish between reading "/proc/" directory entry and opening process'
2354 // "task/" directory. So, in general case we should not complain, but have to skip
2355 // this process and read the next one.
2356 // But on systems with no "task/" support we will spend lot of time to scan "/proc/"
2357 // tree again and again without any benefit. "init" process (its pid is 1) should
2358 // exist always, so, if we cannot open "/proc/1/task/" directory, it means "task/"
2359 // is not supported by kernel. Report an error now and in the future.
2360 if ( strcmp( proc_entry->d_name, "1" ) == 0 ) {
2361 running_threads = -1;
2362 permanent_error = 1;
2363 goto finish;
2364 }; // if
2365 } else {
2366 // Construct fixed part of stat file path.
2367 __kmp_str_buf_clear( & stat_path );
2368 __kmp_str_buf_cat( & stat_path, task_path.str, task_path.used );
2369 __kmp_str_buf_cat( & stat_path, "/", 1 );
2370 stat_path_fixed_len = stat_path.used;
2371
2372 task_entry = readdir( task_dir );
2373 while ( task_entry != NULL ) {
2374 // It is a directory and name starts with a digit.
2375 if ( proc_entry->d_type == DT_DIR && isdigit( task_entry->d_name[ 0 ] ) ) {
2376
2377 ++ total_threads;
2378
2379 // Consruct complete stat file path. Easiest way would be:
2380 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str, task_entry->d_name );
2381 // but seriae of __kmp_str_buf_cat works a bit faster.
2382 stat_path.used = stat_path_fixed_len; // Reset stat path to its fixed part.
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002383 __kmp_str_buf_cat( & stat_path, task_entry->d_name, KMP_STRLEN( task_entry->d_name ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002384 __kmp_str_buf_cat( & stat_path, "/stat", 5 );
2385
2386 // Note: Low-level API (open/read/close) is used. High-level API
2387 // (fopen/fclose) works ~ 30 % slower.
2388 stat_file = open( stat_path.str, O_RDONLY );
2389 if ( stat_file == -1 ) {
2390 // We cannot report an error because task (thread) can terminate just
2391 // before reading this file.
2392 } else {
2393 /*
2394 Content of "stat" file looks like:
2395
2396 24285 (program) S ...
2397
2398 It is a single line (if program name does not include fanny
2399 symbols). First number is a thread id, then name of executable file
2400 name in paretheses, then state of the thread. We need just thread
2401 state.
2402
2403 Good news: Length of program name is 15 characters max. Longer
2404 names are truncated.
2405
2406 Thus, we need rather short buffer: 15 chars for program name +
2407 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
2408
2409 Bad news: Program name may contain special symbols like space,
2410 closing parenthesis, or even new line. This makes parsing "stat"
2411 file not 100 % reliable. In case of fanny program names parsing
2412 may fail (report incorrect thread state).
2413
2414 Parsing "status" file looks more promissing (due to different
2415 file structure and escaping special symbols) but reading and
2416 parsing of "status" file works slower.
2417
2418 -- ln
2419 */
2420 char buffer[ 65 ];
2421 int len;
2422 len = read( stat_file, buffer, sizeof( buffer ) - 1 );
2423 if ( len >= 0 ) {
2424 buffer[ len ] = 0;
2425 // Using scanf:
2426 // sscanf( buffer, "%*d (%*s) %c ", & state );
2427 // looks very nice, but searching for a closing parenthesis works a
2428 // bit faster.
2429 char * close_parent = strstr( buffer, ") " );
2430 if ( close_parent != NULL ) {
2431 char state = * ( close_parent + 2 );
2432 if ( state == 'R' ) {
2433 ++ running_threads;
2434 if ( running_threads >= max ) {
2435 goto finish;
2436 }; // if
2437 }; // if
2438 }; // if
2439 }; // if
2440 close( stat_file );
2441 stat_file = -1;
2442 }; // if
2443 }; // if
2444 task_entry = readdir( task_dir );
2445 }; // while
2446 closedir( task_dir );
2447 task_dir = NULL;
2448 }; // if
2449 }; // if
2450 proc_entry = readdir( proc_dir );
2451 }; // while
2452
2453 //
2454 // There _might_ be a timing hole where the thread executing this
2455 // code get skipped in the load balance, and running_threads is 0.
2456 // Assert in the debug builds only!!!
2457 //
2458 KMP_DEBUG_ASSERT( running_threads > 0 );
2459 if ( running_threads <= 0 ) {
2460 running_threads = 1;
2461 }
2462
2463 finish: // Clean up and exit.
2464 if ( proc_dir != NULL ) {
2465 closedir( proc_dir );
2466 }; // if
2467 __kmp_str_buf_free( & task_path );
2468 if ( task_dir != NULL ) {
2469 closedir( task_dir );
2470 }; // if
2471 __kmp_str_buf_free( & stat_path );
2472 if ( stat_file != -1 ) {
2473 close( stat_file );
2474 }; // if
2475
2476 glb_running_threads = running_threads;
2477
2478 return running_threads;
2479
2480} // __kmp_get_load_balance
2481
2482# endif // KMP_OS_DARWIN
2483
2484#endif // USE_LOAD_BALANCE
2485
Hal Finkel91e19a32016-05-26 04:48:14 +00002486#if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || (KMP_OS_LINUX && KMP_ARCH_AARCH64) || KMP_ARCH_PPC64)
Jim Cownie3051f972014-08-07 10:12:54 +00002487
2488// we really only need the case with 1 argument, because CLANG always build
2489// a struct of pointers to shared variables referenced in the outlined function
2490int
2491__kmp_invoke_microtask( microtask_t pkfn,
2492 int gtid, int tid,
Jonathan Peyton61118492016-05-20 19:03:38 +00002493 int argc, void *p_argv[]
Jonathan Peyton122dd762015-07-13 18:55:45 +00002494#if OMPT_SUPPORT
2495 , void **exit_frame_ptr
2496#endif
Jonathan Peyton61118492016-05-20 19:03:38 +00002497)
Jonathan Peyton122dd762015-07-13 18:55:45 +00002498{
2499#if OMPT_SUPPORT
2500 *exit_frame_ptr = __builtin_frame_address(0);
2501#endif
2502
Jim Cownie3051f972014-08-07 10:12:54 +00002503 switch (argc) {
2504 default:
2505 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2506 fflush(stderr);
2507 exit(-1);
2508 case 0:
2509 (*pkfn)(&gtid, &tid);
2510 break;
2511 case 1:
2512 (*pkfn)(&gtid, &tid, p_argv[0]);
2513 break;
2514 case 2:
2515 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2516 break;
2517 case 3:
2518 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2519 break;
2520 case 4:
2521 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2522 break;
2523 case 5:
2524 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2525 break;
2526 case 6:
2527 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2528 p_argv[5]);
2529 break;
2530 case 7:
2531 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2532 p_argv[5], p_argv[6]);
2533 break;
2534 case 8:
2535 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2536 p_argv[5], p_argv[6], p_argv[7]);
2537 break;
2538 case 9:
2539 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2540 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2541 break;
2542 case 10:
2543 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2544 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2545 break;
2546 case 11:
2547 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2548 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2549 break;
2550 case 12:
2551 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2552 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2553 p_argv[11]);
2554 break;
2555 case 13:
2556 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2557 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2558 p_argv[11], p_argv[12]);
2559 break;
2560 case 14:
2561 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2562 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2563 p_argv[11], p_argv[12], p_argv[13]);
2564 break;
2565 case 15:
2566 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2567 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2568 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2569 break;
2570 }
2571
Jonathan Peyton122dd762015-07-13 18:55:45 +00002572#if OMPT_SUPPORT
2573 *exit_frame_ptr = 0;
2574#endif
2575
Jim Cownie3051f972014-08-07 10:12:54 +00002576 return 1;
2577}
2578
2579#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002580
Jim Cownie5e8470a2013-09-27 10:38:44 +00002581// end of file //
2582