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