blob: 703ad6daed0390e6f8a499da99ec18effe7893ae [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
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +000025#if !KMP_OS_FREEBSD && !KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +000026# 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
Alp Toker763b9392014-02-28 09:42:41 +000054# include <pthread_np.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000055#endif
56
Jim Cownie5e8470a2013-09-27 10:38:44 +000057#include <dirent.h>
58#include <ctype.h>
59#include <fcntl.h>
60
61/* ------------------------------------------------------------------------ */
62/* ------------------------------------------------------------------------ */
63
64struct kmp_sys_timer {
65 struct timespec start;
66};
67
68// Convert timespec to nanoseconds.
69#define TS2NS(timespec) (((timespec).tv_sec * 1e9) + (timespec).tv_nsec)
70
71static struct kmp_sys_timer __kmp_sys_timer_data;
72
73#if KMP_HANDLE_SIGNALS
74 typedef void (* sig_func_t )( int );
75 STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[ NSIG ];
76 static sigset_t __kmp_sigset;
77#endif
78
79static int __kmp_init_runtime = FALSE;
80
81static int __kmp_fork_count = 0;
82
83static pthread_condattr_t __kmp_suspend_cond_attr;
84static pthread_mutexattr_t __kmp_suspend_mutex_attr;
85
86static kmp_cond_align_t __kmp_wait_cv;
87static kmp_mutex_align_t __kmp_wait_mx;
88
89/* ------------------------------------------------------------------------ */
90/* ------------------------------------------------------------------------ */
91
92#ifdef DEBUG_SUSPEND
93static void
94__kmp_print_cond( char *buffer, kmp_cond_align_t *cond )
95{
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000096 KMP_SNPRINTF( buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))",
Jim Cownie5e8470a2013-09-27 10:38:44 +000097 cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
98 cond->c_cond.__c_waiting );
99}
100#endif
101
102/* ------------------------------------------------------------------------ */
103/* ------------------------------------------------------------------------ */
104
Jim Cownie3051f972014-08-07 10:12:54 +0000105#if ( KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000106
107/*
108 * Affinity support
109 */
110
111/*
112 * On some of the older OS's that we build on, these constants aren't present
113 * in <asm/unistd.h> #included from <sys.syscall.h>. They must be the same on
114 * all systems of the same arch where they are defined, and they cannot change.
115 * stone forever.
116 */
117
Jim Cownie181b4bb2013-12-23 17:28:57 +0000118# if KMP_ARCH_X86 || KMP_ARCH_ARM
Jim Cownie5e8470a2013-09-27 10:38:44 +0000119# ifndef __NR_sched_setaffinity
120# define __NR_sched_setaffinity 241
121# elif __NR_sched_setaffinity != 241
122# error Wrong code for setaffinity system call.
123# endif /* __NR_sched_setaffinity */
124# ifndef __NR_sched_getaffinity
125# define __NR_sched_getaffinity 242
126# elif __NR_sched_getaffinity != 242
127# error Wrong code for getaffinity system call.
128# endif /* __NR_sched_getaffinity */
129
Andrey Churbanovcbda8682015-01-13 14:43:35 +0000130# elif KMP_ARCH_AARCH64
131# ifndef __NR_sched_setaffinity
132# define __NR_sched_setaffinity 122
133# elif __NR_sched_setaffinity != 122
134# error Wrong code for setaffinity system call.
135# endif /* __NR_sched_setaffinity */
136# ifndef __NR_sched_getaffinity
137# define __NR_sched_getaffinity 123
138# elif __NR_sched_getaffinity != 123
139# error Wrong code for getaffinity system call.
140# endif /* __NR_sched_getaffinity */
141
Jim Cownie5e8470a2013-09-27 10:38:44 +0000142# elif KMP_ARCH_X86_64
143# ifndef __NR_sched_setaffinity
144# define __NR_sched_setaffinity 203
145# elif __NR_sched_setaffinity != 203
146# error Wrong code for setaffinity system call.
147# endif /* __NR_sched_setaffinity */
148# ifndef __NR_sched_getaffinity
149# define __NR_sched_getaffinity 204
150# elif __NR_sched_getaffinity != 204
151# error Wrong code for getaffinity system call.
152# endif /* __NR_sched_getaffinity */
153
Jim Cownie3051f972014-08-07 10:12:54 +0000154# elif KMP_ARCH_PPC64
155# ifndef __NR_sched_setaffinity
156# define __NR_sched_setaffinity 222
157# elif __NR_sched_setaffinity != 222
158# error Wrong code for setaffinity system call.
159# endif /* __NR_sched_setaffinity */
160# ifndef __NR_sched_getaffinity
161# define __NR_sched_getaffinity 223
162# elif __NR_sched_getaffinity != 223
163# error Wrong code for getaffinity system call.
164# endif /* __NR_sched_getaffinity */
165
166
Jim Cownie5e8470a2013-09-27 10:38:44 +0000167# else
168# error Unknown or unsupported architecture
169
170# endif /* KMP_ARCH_* */
171
172int
173__kmp_set_system_affinity( kmp_affin_mask_t const *mask, int abort_on_error )
174{
175 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
176 "Illegal set affinity operation when not capable");
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000177#if KMP_USE_HWLOC
178 int retval = hwloc_set_cpubind(__kmp_hwloc_topology, (hwloc_cpuset_t)mask, HWLOC_CPUBIND_THREAD);
179#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000180 int retval = syscall( __NR_sched_setaffinity, 0, __kmp_affin_mask_size, mask );
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000181#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000182 if (retval >= 0) {
183 return 0;
184 }
185 int error = errno;
186 if (abort_on_error) {
187 __kmp_msg(
188 kmp_ms_fatal,
189 KMP_MSG( FatalSysError ),
190 KMP_ERR( error ),
191 __kmp_msg_null
192 );
193 }
194 return error;
195}
196
197int
198__kmp_get_system_affinity( kmp_affin_mask_t *mask, int abort_on_error )
199{
200 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
201 "Illegal get affinity operation when not capable");
202
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000203#if KMP_USE_HWLOC
204 int retval = hwloc_get_cpubind(__kmp_hwloc_topology, (hwloc_cpuset_t)mask, HWLOC_CPUBIND_THREAD);
205#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000206 int retval = syscall( __NR_sched_getaffinity, 0, __kmp_affin_mask_size, mask );
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000207#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000208 if (retval >= 0) {
209 return 0;
210 }
211 int error = errno;
212 if (abort_on_error) {
213 __kmp_msg(
214 kmp_ms_fatal,
215 KMP_MSG( FatalSysError ),
216 KMP_ERR( error ),
217 __kmp_msg_null
218 );
219 }
220 return error;
221}
222
223void
224__kmp_affinity_bind_thread( int which )
225{
226 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
227 "Illegal set affinity operation when not capable");
228
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000229 kmp_affin_mask_t *mask;
230 KMP_CPU_ALLOC_ON_STACK(mask);
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);
Jonathan Peyton01dcf362015-11-30 20:02:59 +0000234 KMP_CPU_FREE_FROM_STACK(mask);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000235}
236
237/*
238 * Determine if we can access affinity functionality on this version of
239 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
240 * __kmp_affin_mask_size to the appropriate value (0 means not capable).
241 */
242void
243__kmp_affinity_determine_capable(const char *env_var)
244{
245 //
246 // Check and see if the OS supports thread affinity.
247 //
248
249# define KMP_CPU_SET_SIZE_LIMIT (1024*1024)
250
251 int gCode;
252 int sCode;
253 kmp_affin_mask_t *buf;
254 buf = ( kmp_affin_mask_t * ) KMP_INTERNAL_MALLOC( KMP_CPU_SET_SIZE_LIMIT );
255
256 // If Linux* OS:
257 // If the syscall fails or returns a suggestion for the size,
258 // then we don't have to search for an appropriate size.
259 gCode = syscall( __NR_sched_getaffinity, 0, KMP_CPU_SET_SIZE_LIMIT, buf );
260 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
Alp Toker8f2d3f02014-02-24 10:40:15 +0000261 "initial getaffinity call returned %d errno = %d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +0000262 gCode, errno));
263
264 //if ((gCode < 0) && (errno == ENOSYS))
265 if (gCode < 0) {
266 //
267 // System call not supported
268 //
269 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
270 && (__kmp_affinity_type != affinity_none)
271 && (__kmp_affinity_type != affinity_default)
272 && (__kmp_affinity_type != affinity_disabled))) {
273 int error = errno;
274 __kmp_msg(
275 kmp_ms_warning,
276 KMP_MSG( GetAffSysCallNotSupported, env_var ),
277 KMP_ERR( error ),
278 __kmp_msg_null
279 );
280 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000281 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000282 KMP_INTERNAL_FREE(buf);
283 return;
284 }
285 if (gCode > 0) { // Linux* OS only
286 // The optimal situation: the OS returns the size of the buffer
287 // it expects.
288 //
289 // A verification of correct behavior is that Isetaffinity on a NULL
290 // buffer with the same size fails with errno set to EFAULT.
291 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
292 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
293 "setaffinity for mask size %d returned %d errno = %d\n",
294 gCode, sCode, errno));
295 if (sCode < 0) {
296 if (errno == ENOSYS) {
297 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
298 && (__kmp_affinity_type != affinity_none)
299 && (__kmp_affinity_type != affinity_default)
300 && (__kmp_affinity_type != affinity_disabled))) {
301 int error = errno;
302 __kmp_msg(
303 kmp_ms_warning,
304 KMP_MSG( SetAffSysCallNotSupported, env_var ),
305 KMP_ERR( error ),
306 __kmp_msg_null
307 );
308 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000309 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000310 KMP_INTERNAL_FREE(buf);
311 }
312 if (errno == EFAULT) {
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000313 KMP_AFFINITY_ENABLE(gCode);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000314 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
315 "affinity supported (mask size %d)\n",
316 (int)__kmp_affin_mask_size));
317 KMP_INTERNAL_FREE(buf);
318 return;
319 }
320 }
321 }
322
323 //
324 // Call the getaffinity system call repeatedly with increasing set sizes
325 // until we succeed, or reach an upper bound on the search.
326 //
327 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
328 "searching for proper set size\n"));
329 int size;
330 for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
331 gCode = syscall( __NR_sched_getaffinity, 0, size, buf );
332 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
333 "getaffinity for mask size %d returned %d errno = %d\n", size,
334 gCode, errno));
335
336 if (gCode < 0) {
337 if ( errno == ENOSYS )
338 {
339 //
340 // We shouldn't get here
341 //
342 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
343 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
344 size));
345 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
346 && (__kmp_affinity_type != affinity_none)
347 && (__kmp_affinity_type != affinity_default)
348 && (__kmp_affinity_type != affinity_disabled))) {
349 int error = errno;
350 __kmp_msg(
351 kmp_ms_warning,
352 KMP_MSG( GetAffSysCallNotSupported, env_var ),
353 KMP_ERR( error ),
354 __kmp_msg_null
355 );
356 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000357 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000358 KMP_INTERNAL_FREE(buf);
359 return;
360 }
361 continue;
362 }
363
364 sCode = syscall( __NR_sched_setaffinity, 0, gCode, NULL );
365 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
366 "setaffinity for mask size %d returned %d errno = %d\n",
367 gCode, sCode, errno));
368 if (sCode < 0) {
369 if (errno == ENOSYS) { // Linux* OS only
370 //
371 // We shouldn't get here
372 //
373 KA_TRACE(30, ( "__kmp_affinity_determine_capable: "
374 "inconsistent OS call behavior: errno == ENOSYS for mask size %d\n",
375 size));
376 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
377 && (__kmp_affinity_type != affinity_none)
378 && (__kmp_affinity_type != affinity_default)
379 && (__kmp_affinity_type != affinity_disabled))) {
380 int error = errno;
381 __kmp_msg(
382 kmp_ms_warning,
383 KMP_MSG( SetAffSysCallNotSupported, env_var ),
384 KMP_ERR( error ),
385 __kmp_msg_null
386 );
387 }
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000388 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000389 KMP_INTERNAL_FREE(buf);
390 return;
391 }
392 if (errno == EFAULT) {
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000393 KMP_AFFINITY_ENABLE(gCode);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000394 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
395 "affinity supported (mask size %d)\n",
396 (int)__kmp_affin_mask_size));
397 KMP_INTERNAL_FREE(buf);
398 return;
399 }
400 }
401 }
402 //int error = errno; // save uncaught error code
403 KMP_INTERNAL_FREE(buf);
404 // errno = error; // restore uncaught error code, will be printed at the next KMP_WARNING below
405
406 //
407 // Affinity is not supported
408 //
Andrey Churbanov1f037e42015-03-10 09:15:26 +0000409 KMP_AFFINITY_DISABLE();
Jim Cownie5e8470a2013-09-27 10:38:44 +0000410 KA_TRACE(10, ( "__kmp_affinity_determine_capable: "
411 "cannot determine mask size - affinity not supported\n"));
412 if (__kmp_affinity_verbose || (__kmp_affinity_warnings
413 && (__kmp_affinity_type != affinity_none)
414 && (__kmp_affinity_type != affinity_default)
415 && (__kmp_affinity_type != affinity_disabled))) {
416 KMP_WARNING( AffCantGetMaskSize, env_var );
417 }
418}
419
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000420#endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000421
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000422/* ------------------------------------------------------------------------ */
423/* ------------------------------------------------------------------------ */
424
425#if KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64) && !KMP_OS_CNK
426
427int
428__kmp_futex_determine_capable()
429{
430 int loc = 0;
431 int rc = syscall( __NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0 );
432 int retval = ( rc == 0 ) || ( errno != ENOSYS );
433
434 KA_TRACE(10, ( "__kmp_futex_determine_capable: rc = %d errno = %d\n", rc,
435 errno ) );
436 KA_TRACE(10, ( "__kmp_futex_determine_capable: futex syscall%s supported\n",
437 retval ? "" : " not" ) );
438
439 return retval;
440}
441
442#endif // KMP_OS_LINUX && (KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_ARCH_ARM) && !KMP_OS_CNK
443
444/* ------------------------------------------------------------------------ */
445/* ------------------------------------------------------------------------ */
446
447#if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000448/*
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000449 * Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
450 * use compare_and_store for these routines
Jim Cownie5e8470a2013-09-27 10:38:44 +0000451 */
452
Andrey Churbanov7b2ab712015-03-10 09:03:42 +0000453kmp_int8
454__kmp_test_then_or8( volatile kmp_int8 *p, kmp_int8 d )
455{
456 kmp_int8 old_value, new_value;
457
458 old_value = TCR_1( *p );
459 new_value = old_value | d;
460
461 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
462 {
463 KMP_CPU_PAUSE();
464 old_value = TCR_1( *p );
465 new_value = old_value | d;
466 }
467 return old_value;
468}
469
470kmp_int8
471__kmp_test_then_and8( volatile kmp_int8 *p, kmp_int8 d )
472{
473 kmp_int8 old_value, new_value;
474
475 old_value = TCR_1( *p );
476 new_value = old_value & d;
477
478 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
479 {
480 KMP_CPU_PAUSE();
481 old_value = TCR_1( *p );
482 new_value = old_value & d;
483 }
484 return old_value;
485}
486
Jim Cownie5e8470a2013-09-27 10:38:44 +0000487kmp_int32
488__kmp_test_then_or32( volatile kmp_int32 *p, kmp_int32 d )
489{
490 kmp_int32 old_value, new_value;
491
492 old_value = TCR_4( *p );
493 new_value = old_value | d;
494
Jim Cownie3051f972014-08-07 10:12:54 +0000495 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000496 {
497 KMP_CPU_PAUSE();
498 old_value = TCR_4( *p );
499 new_value = old_value | d;
500 }
501 return old_value;
502}
503
504kmp_int32
505__kmp_test_then_and32( volatile kmp_int32 *p, kmp_int32 d )
506{
507 kmp_int32 old_value, new_value;
508
509 old_value = TCR_4( *p );
510 new_value = old_value & d;
511
Jim Cownie3051f972014-08-07 10:12:54 +0000512 while ( ! KMP_COMPARE_AND_STORE_REL32 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000513 {
514 KMP_CPU_PAUSE();
515 old_value = TCR_4( *p );
516 new_value = old_value & d;
517 }
518 return old_value;
519}
520
Andrey Churbanovcbda8682015-01-13 14:43:35 +0000521# if KMP_ARCH_X86 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64
Andrey Churbanovd39f11c2015-03-10 10:14:57 +0000522kmp_int8
523__kmp_test_then_add8( volatile kmp_int8 *p, kmp_int8 d )
524{
525 kmp_int8 old_value, new_value;
526
527 old_value = TCR_1( *p );
528 new_value = old_value + d;
529
530 while ( ! KMP_COMPARE_AND_STORE_REL8 ( p, old_value, new_value ) )
531 {
532 KMP_CPU_PAUSE();
533 old_value = TCR_1( *p );
534 new_value = old_value + d;
535 }
536 return old_value;
537}
538
Jim Cownie5e8470a2013-09-27 10:38:44 +0000539kmp_int64
540__kmp_test_then_add64( volatile kmp_int64 *p, kmp_int64 d )
541{
542 kmp_int64 old_value, new_value;
543
544 old_value = TCR_8( *p );
545 new_value = old_value + d;
546
Jim Cownie3051f972014-08-07 10:12:54 +0000547 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000548 {
549 KMP_CPU_PAUSE();
550 old_value = TCR_8( *p );
551 new_value = old_value + d;
552 }
553 return old_value;
554}
555# endif /* KMP_ARCH_X86 */
556
557kmp_int64
558__kmp_test_then_or64( volatile kmp_int64 *p, kmp_int64 d )
559{
560 kmp_int64 old_value, new_value;
561
562 old_value = TCR_8( *p );
563 new_value = old_value | d;
Jim Cownie3051f972014-08-07 10:12:54 +0000564 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000565 {
566 KMP_CPU_PAUSE();
567 old_value = TCR_8( *p );
568 new_value = old_value | d;
569 }
570 return old_value;
571}
572
573kmp_int64
574__kmp_test_then_and64( volatile kmp_int64 *p, kmp_int64 d )
575{
576 kmp_int64 old_value, new_value;
577
578 old_value = TCR_8( *p );
579 new_value = old_value & d;
Jim Cownie3051f972014-08-07 10:12:54 +0000580 while ( ! KMP_COMPARE_AND_STORE_REL64 ( p, old_value, new_value ) )
Jim Cownie5e8470a2013-09-27 10:38:44 +0000581 {
582 KMP_CPU_PAUSE();
583 old_value = TCR_8( *p );
584 new_value = old_value & d;
585 }
586 return old_value;
587}
588
589#endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
590
591void
592__kmp_terminate_thread( int gtid )
593{
594 int status;
595 kmp_info_t *th = __kmp_threads[ gtid ];
596
597 if ( !th ) return;
598
599 #ifdef KMP_CANCEL_THREADS
600 KA_TRACE( 10, ("__kmp_terminate_thread: kill (%d)\n", gtid ) );
601 status = pthread_cancel( th->th.th_info.ds.ds_thread );
602 if ( status != 0 && status != ESRCH ) {
603 __kmp_msg(
604 kmp_ms_fatal,
605 KMP_MSG( CantTerminateWorkerThread ),
606 KMP_ERR( status ),
607 __kmp_msg_null
608 );
609 }; // if
610 #endif
611 __kmp_yield( TRUE );
612} //
613
614/* ------------------------------------------------------------------------ */
615/* ------------------------------------------------------------------------ */
616
617/* ------------------------------------------------------------------------ */
618/* ------------------------------------------------------------------------ */
619
620/*
621 * Set thread stack info according to values returned by
622 * pthread_getattr_np().
623 * If values are unreasonable, assume call failed and use
624 * incremental stack refinement method instead.
625 * Returns TRUE if the stack parameters could be determined exactly,
626 * FALSE if incremental refinement is necessary.
627 */
628static kmp_int32
629__kmp_set_stack_info( int gtid, kmp_info_t *th )
630{
631 int stack_data;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000632#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000633 /* Linux* OS only -- no pthread_getattr_np support on OS X* */
634 pthread_attr_t attr;
635 int status;
636 size_t size = 0;
637 void * addr = 0;
638
639 /* Always do incremental stack refinement for ubermaster threads since the initial
640 thread stack range can be reduced by sibling thread creation so pthread_attr_getstack
641 may cause thread gtid aliasing */
642 if ( ! KMP_UBER_GTID(gtid) ) {
643
644 /* Fetch the real thread attributes */
645 status = pthread_attr_init( &attr );
646 KMP_CHECK_SYSFAIL( "pthread_attr_init", status );
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000647#if KMP_OS_FREEBSD || KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +0000648 status = pthread_attr_get_np( pthread_self(), &attr );
649 KMP_CHECK_SYSFAIL( "pthread_attr_get_np", status );
650#else
Jim Cownie5e8470a2013-09-27 10:38:44 +0000651 status = pthread_getattr_np( pthread_self(), &attr );
652 KMP_CHECK_SYSFAIL( "pthread_getattr_np", status );
Alp Toker763b9392014-02-28 09:42:41 +0000653#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000654 status = pthread_attr_getstack( &attr, &addr, &size );
655 KMP_CHECK_SYSFAIL( "pthread_attr_getstack", status );
656 KA_TRACE( 60, ( "__kmp_set_stack_info: T#%d pthread_attr_getstack returned size: %lu, "
657 "low addr: %p\n",
658 gtid, size, addr ));
659
660 status = pthread_attr_destroy( &attr );
661 KMP_CHECK_SYSFAIL( "pthread_attr_destroy", status );
662 }
663
664 if ( size != 0 && addr != 0 ) { /* was stack parameter determination successful? */
665 /* Store the correct base and size */
666 TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size));
667 TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
668 TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
669 return TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000670 }
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000671#endif /* KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD */
Alp Toker763b9392014-02-28 09:42:41 +0000672 /* Use incremental refinement starting from initial conservative estimate */
673 TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
674 TCW_PTR(th -> th.th_info.ds.ds_stackbase, &stack_data);
675 TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
676 return FALSE;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000677}
678
679static void*
680__kmp_launch_worker( void *thr )
681{
682 int status, old_type, old_state;
683#ifdef KMP_BLOCK_SIGNALS
684 sigset_t new_set, old_set;
685#endif /* KMP_BLOCK_SIGNALS */
686 void *exit_val;
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000687#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Andrey Churbanov368b70e2015-08-05 11:12:45 +0000688 void * volatile padding = 0;
Jonathan Peyton2321d572015-06-08 19:25:25 +0000689#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000690 int gtid;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000691
692 gtid = ((kmp_info_t*)thr) -> th.th_info.ds.ds_gtid;
693 __kmp_gtid_set_specific( gtid );
694#ifdef KMP_TDATA_GTID
695 __kmp_gtid = gtid;
696#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000697#if KMP_STATS_ENABLED
698 // set __thread local index to point to thread-specific stats
699 __kmp_stats_thread_ptr = ((kmp_info_t*)thr)->th.th_stats;
700#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +0000701
702#if USE_ITT_BUILD
703 __kmp_itt_thread_name( gtid );
704#endif /* USE_ITT_BUILD */
705
Alp Toker763b9392014-02-28 09:42:41 +0000706#if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +0000707 __kmp_affinity_set_init_mask( gtid, FALSE );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000708#endif
709
710#ifdef KMP_CANCEL_THREADS
711 status = pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, & old_type );
712 KMP_CHECK_SYSFAIL( "pthread_setcanceltype", status );
713 /* josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads? */
714 status = pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, & old_state );
715 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
716#endif
717
718#if KMP_ARCH_X86 || KMP_ARCH_X86_64
719 //
720 // Set the FP control regs to be a copy of
721 // the parallel initialization thread's.
722 //
723 __kmp_clear_x87_fpu_status_word();
724 __kmp_load_x87_fpu_control_word( &__kmp_init_x87_fpu_control_word );
725 __kmp_load_mxcsr( &__kmp_init_mxcsr );
726#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
727
728#ifdef KMP_BLOCK_SIGNALS
729 status = sigfillset( & new_set );
730 KMP_CHECK_SYSFAIL_ERRNO( "sigfillset", status );
731 status = pthread_sigmask( SIG_BLOCK, & new_set, & old_set );
732 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
733#endif /* KMP_BLOCK_SIGNALS */
734
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +0000735#if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +0000736 if ( __kmp_stkoffset > 0 && gtid > 0 ) {
Andrey Churbanov74bf17b2015-04-02 13:27:08 +0000737 padding = KMP_ALLOCA( gtid * __kmp_stkoffset );
Jim Cownie5e8470a2013-09-27 10:38:44 +0000738 }
739#endif
740
741 KMP_MB();
742 __kmp_set_stack_info( gtid, (kmp_info_t*)thr );
743
744 __kmp_check_stack_overlap( (kmp_info_t*)thr );
745
746 exit_val = __kmp_launch_thread( (kmp_info_t *) thr );
747
748#ifdef KMP_BLOCK_SIGNALS
749 status = pthread_sigmask( SIG_SETMASK, & old_set, NULL );
750 KMP_CHECK_SYSFAIL( "pthread_sigmask", status );
751#endif /* KMP_BLOCK_SIGNALS */
752
753 return exit_val;
754}
755
Jim Cownie5e8470a2013-09-27 10:38:44 +0000756/* 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;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001122 int auto_adj_size = FALSE;
1123
Jonathan Peyton4fee5f62015-12-18 23:20:36 +00001124 if( __kmp_dflt_blocktime == KMP_MAX_BLOCKTIME ) {
1125 // We don't need monitor thread in case of MAX_BLOCKTIME
1126 KA_TRACE( 10, ("__kmp_create_monitor: skipping monitor thread because of MAX blocktime\n" ) );
1127 th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
1128 th->th.th_info.ds.ds_gtid = 0;
1129 return;
1130 }
Jim Cownie5e8470a2013-09-27 10:38:44 +00001131 KA_TRACE( 10, ("__kmp_create_monitor: try to create monitor\n" ) );
1132
1133 KMP_MB(); /* Flush all pending memory write invalidates. */
1134
1135 th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
1136 th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
1137 #if KMP_REAL_TIME_FIX
1138 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 +00001139 #else
1140 TCW_4( __kmp_global.g.g_time.dt.t_value, 0 );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001141 #endif // KMP_REAL_TIME_FIX
1142
1143 #ifdef KMP_THREAD_ATTR
1144 if ( __kmp_monitor_stksize == 0 ) {
1145 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1146 auto_adj_size = TRUE;
1147 }
1148 status = pthread_attr_init( &thread_attr );
1149 if ( status != 0 ) {
1150 __kmp_msg(
1151 kmp_ms_fatal,
1152 KMP_MSG( CantInitThreadAttrs ),
1153 KMP_ERR( status ),
1154 __kmp_msg_null
1155 );
1156 }; // if
1157 status = pthread_attr_setdetachstate( & thread_attr, PTHREAD_CREATE_JOINABLE );
1158 if ( status != 0 ) {
1159 __kmp_msg(
1160 kmp_ms_fatal,
1161 KMP_MSG( CantSetMonitorState ),
1162 KMP_ERR( status ),
1163 __kmp_msg_null
1164 );
1165 }; // if
1166
1167 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1168 status = pthread_attr_getstacksize( & thread_attr, & size );
1169 KMP_CHECK_SYSFAIL( "pthread_attr_getstacksize", status );
1170 #else
1171 size = __kmp_sys_min_stksize;
1172 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1173 #endif /* KMP_THREAD_ATTR */
1174
1175 if ( __kmp_monitor_stksize == 0 ) {
1176 __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
1177 }
1178 if ( __kmp_monitor_stksize < __kmp_sys_min_stksize ) {
1179 __kmp_monitor_stksize = __kmp_sys_min_stksize;
1180 }
1181
1182 KA_TRACE( 10, ( "__kmp_create_monitor: default stacksize = %lu bytes,"
1183 "requested stacksize = %lu bytes\n",
1184 size, __kmp_monitor_stksize ) );
1185
1186 retry:
1187
1188 /* Set stack size for this thread now. */
1189
1190 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1191 KA_TRACE( 10, ( "__kmp_create_monitor: setting stacksize = %lu bytes,",
1192 __kmp_monitor_stksize ) );
1193 status = pthread_attr_setstacksize( & thread_attr, __kmp_monitor_stksize );
1194 if ( status != 0 ) {
1195 if ( auto_adj_size ) {
1196 __kmp_monitor_stksize *= 2;
1197 goto retry;
1198 }
1199 __kmp_msg(
1200 kmp_ms_warning, // should this be fatal? BB
1201 KMP_MSG( CantSetMonitorStackSize, (long int) __kmp_monitor_stksize ),
1202 KMP_ERR( status ),
1203 KMP_HNT( ChangeMonitorStackSize ),
1204 __kmp_msg_null
1205 );
1206 }; // if
1207 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1208
Jim Cownie5e8470a2013-09-27 10:38:44 +00001209 status = pthread_create( &handle, & thread_attr, __kmp_launch_monitor, (void *) th );
1210
1211 if ( status != 0 ) {
1212 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
1213 if ( status == EINVAL ) {
1214 if ( auto_adj_size && ( __kmp_monitor_stksize < (size_t)0x40000000 ) ) {
1215 __kmp_monitor_stksize *= 2;
1216 goto retry;
1217 }
1218 __kmp_msg(
1219 kmp_ms_fatal,
1220 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1221 KMP_ERR( status ),
1222 KMP_HNT( IncreaseMonitorStackSize ),
1223 __kmp_msg_null
1224 );
1225 }; // if
1226 if ( status == ENOMEM ) {
1227 __kmp_msg(
1228 kmp_ms_fatal,
1229 KMP_MSG( CantSetMonitorStackSize, __kmp_monitor_stksize ),
1230 KMP_ERR( status ),
1231 KMP_HNT( DecreaseMonitorStackSize ),
1232 __kmp_msg_null
1233 );
1234 }; // if
1235 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
1236 if ( status == EAGAIN ) {
1237 __kmp_msg(
1238 kmp_ms_fatal,
1239 KMP_MSG( NoResourcesForMonitorThread ),
1240 KMP_ERR( status ),
1241 KMP_HNT( DecreaseNumberOfThreadsInUse ),
1242 __kmp_msg_null
1243 );
1244 }; // if
1245 KMP_SYSFAIL( "pthread_create", status );
1246 }; // if
1247
1248 th->th.th_info.ds.ds_thread = handle;
1249
1250 #if KMP_REAL_TIME_FIX
1251 // Wait for the monitor thread is really started and set its *priority*.
1252 KMP_DEBUG_ASSERT( sizeof( kmp_uint32 ) == sizeof( __kmp_global.g.g_time.dt.t_value ) );
1253 __kmp_wait_yield_4(
1254 (kmp_uint32 volatile *) & __kmp_global.g.g_time.dt.t_value, -1, & __kmp_neq_4, NULL
1255 );
1256 #endif // KMP_REAL_TIME_FIX
1257
1258 #ifdef KMP_THREAD_ATTR
1259 status = pthread_attr_destroy( & thread_attr );
1260 if ( status != 0 ) {
1261 __kmp_msg( //
1262 kmp_ms_warning,
1263 KMP_MSG( CantDestroyThreadAttrs ),
1264 KMP_ERR( status ),
1265 __kmp_msg_null
1266 );
1267 }; // if
1268 #endif
1269
1270 KMP_MB(); /* Flush all pending memory write invalidates. */
1271
1272 KA_TRACE( 10, ( "__kmp_create_monitor: monitor created %#.8lx\n", th->th.th_info.ds.ds_thread ) );
1273
1274} // __kmp_create_monitor
1275
1276void
1277__kmp_exit_thread(
1278 int exit_status
1279) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001280 pthread_exit( (void *)(intptr_t) exit_status );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001281} // __kmp_exit_thread
1282
Jim Cownie07ea89f2014-09-03 11:10:54 +00001283void __kmp_resume_monitor();
1284
Jim Cownie5e8470a2013-09-27 10:38:44 +00001285void
1286__kmp_reap_monitor( kmp_info_t *th )
1287{
Jonathan Peyton7c4d66d2015-06-08 20:01:14 +00001288 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001289 void *exit_val;
1290
1291 KA_TRACE( 10, ("__kmp_reap_monitor: try to reap monitor thread with handle %#.8lx\n",
1292 th->th.th_info.ds.ds_thread ) );
1293
1294 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1295 // If both tid and gtid are 0, it means the monitor did not ever start.
1296 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1297 KMP_DEBUG_ASSERT( th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid );
1298 if ( th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR ) {
Jonathan Peyton4fee5f62015-12-18 23:20:36 +00001299 KA_TRACE( 10, ("__kmp_reap_monitor: monitor did not start, returning\n") );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001300 return;
1301 }; // if
1302
1303 KMP_MB(); /* Flush all pending memory write invalidates. */
1304
1305
1306 /* First, check to see whether the monitor thread exists. This could prevent a hang,
1307 but if the monitor dies after the pthread_kill call and before the pthread_join
1308 call, it will still hang. */
1309
1310 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1311 if (status == ESRCH) {
1312
1313 KA_TRACE( 10, ("__kmp_reap_monitor: monitor does not exist, returning\n") );
1314
1315 } else
1316 {
Jim Cownie07ea89f2014-09-03 11:10:54 +00001317 __kmp_resume_monitor(); // Wake up the monitor thread
Jim Cownie5e8470a2013-09-27 10:38:44 +00001318 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1319 if (exit_val != th) {
1320 __kmp_msg(
1321 kmp_ms_fatal,
1322 KMP_MSG( ReapMonitorError ),
1323 KMP_ERR( status ),
1324 __kmp_msg_null
1325 );
1326 }
1327 }
1328
1329 th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
1330 th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
1331
1332 KA_TRACE( 10, ("__kmp_reap_monitor: done reaping monitor thread with handle %#.8lx\n",
1333 th->th.th_info.ds.ds_thread ) );
1334
1335 KMP_MB(); /* Flush all pending memory write invalidates. */
1336
1337}
1338
1339void
1340__kmp_reap_worker( kmp_info_t *th )
1341{
1342 int status;
1343 void *exit_val;
1344
1345 KMP_MB(); /* Flush all pending memory write invalidates. */
1346
1347 KA_TRACE( 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid ) );
1348
1349 /* First, check to see whether the worker thread exists. This could prevent a hang,
1350 but if the worker dies after the pthread_kill call and before the pthread_join
1351 call, it will still hang. */
1352
1353 {
1354 status = pthread_kill( th->th.th_info.ds.ds_thread, 0 );
1355 if (status == ESRCH) {
1356 KA_TRACE( 10, ("__kmp_reap_worker: worker T#%d does not exist, returning\n",
1357 th->th.th_info.ds.ds_gtid ) );
1358 }
1359 else {
1360 KA_TRACE( 10, ("__kmp_reap_worker: try to join with worker T#%d\n",
1361 th->th.th_info.ds.ds_gtid ) );
1362
1363 status = pthread_join( th->th.th_info.ds.ds_thread, & exit_val);
1364#ifdef KMP_DEBUG
1365 /* Don't expose these to the user until we understand when they trigger */
1366 if ( status != 0 ) {
1367 __kmp_msg(
1368 kmp_ms_fatal,
1369 KMP_MSG( ReapWorkerError ),
1370 KMP_ERR( status ),
1371 __kmp_msg_null
1372 );
1373 }
1374 if ( exit_val != th ) {
1375 KA_TRACE( 10, ( "__kmp_reap_worker: worker T#%d did not reap properly, "
1376 "exit_val = %p\n",
1377 th->th.th_info.ds.ds_gtid, exit_val ) );
1378 }
1379#endif /* KMP_DEBUG */
1380 }
1381 }
1382
1383 KA_TRACE( 10, ("__kmp_reap_worker: done reaping T#%d\n", th->th.th_info.ds.ds_gtid ) );
1384
1385 KMP_MB(); /* Flush all pending memory write invalidates. */
1386}
1387
1388
1389/* ------------------------------------------------------------------------ */
1390/* ------------------------------------------------------------------------ */
1391
1392#if KMP_HANDLE_SIGNALS
1393
1394
1395static void
1396__kmp_null_handler( int signo )
1397{
1398 // Do nothing, for doing SIG_IGN-type actions.
1399} // __kmp_null_handler
1400
1401
1402static void
1403__kmp_team_handler( int signo )
1404{
1405 if ( __kmp_global.g.g_abort == 0 ) {
1406 /* Stage 1 signal handler, let's shut down all of the threads */
1407 #ifdef KMP_DEBUG
1408 __kmp_debug_printf( "__kmp_team_handler: caught signal = %d\n", signo );
1409 #endif
1410 switch ( signo ) {
1411 case SIGHUP :
1412 case SIGINT :
1413 case SIGQUIT :
1414 case SIGILL :
1415 case SIGABRT :
1416 case SIGFPE :
1417 case SIGBUS :
1418 case SIGSEGV :
1419 #ifdef SIGSYS
1420 case SIGSYS :
1421 #endif
1422 case SIGTERM :
1423 if ( __kmp_debug_buf ) {
1424 __kmp_dump_debug_buffer( );
1425 }; // if
1426 KMP_MB(); // Flush all pending memory write invalidates.
1427 TCW_4( __kmp_global.g.g_abort, signo );
1428 KMP_MB(); // Flush all pending memory write invalidates.
1429 TCW_4( __kmp_global.g.g_done, TRUE );
1430 KMP_MB(); // Flush all pending memory write invalidates.
1431 break;
1432 default:
1433 #ifdef KMP_DEBUG
1434 __kmp_debug_printf( "__kmp_team_handler: unknown signal type" );
1435 #endif
1436 break;
1437 }; // switch
1438 }; // if
1439} // __kmp_team_handler
1440
1441
1442static
1443void __kmp_sigaction( int signum, const struct sigaction * act, struct sigaction * oldact ) {
1444 int rc = sigaction( signum, act, oldact );
1445 KMP_CHECK_SYSFAIL_ERRNO( "sigaction", rc );
1446}
1447
1448
1449static void
1450__kmp_install_one_handler( int sig, sig_func_t handler_func, int parallel_init )
1451{
1452 KMP_MB(); // Flush all pending memory write invalidates.
1453 KB_TRACE( 60, ( "__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init ) );
1454 if ( parallel_init ) {
1455 struct sigaction new_action;
1456 struct sigaction old_action;
1457 new_action.sa_handler = handler_func;
1458 new_action.sa_flags = 0;
1459 sigfillset( & new_action.sa_mask );
1460 __kmp_sigaction( sig, & new_action, & old_action );
1461 if ( old_action.sa_handler == __kmp_sighldrs[ sig ].sa_handler ) {
1462 sigaddset( & __kmp_sigset, sig );
1463 } else {
1464 // Restore/keep user's handler if one previously installed.
1465 __kmp_sigaction( sig, & old_action, NULL );
1466 }; // if
1467 } else {
1468 // Save initial/system signal handlers to see if user handlers installed.
1469 __kmp_sigaction( sig, NULL, & __kmp_sighldrs[ sig ] );
1470 }; // if
1471 KMP_MB(); // Flush all pending memory write invalidates.
1472} // __kmp_install_one_handler
1473
1474
1475static void
1476__kmp_remove_one_handler( int sig )
1477{
1478 KB_TRACE( 60, ( "__kmp_remove_one_handler( %d )\n", sig ) );
1479 if ( sigismember( & __kmp_sigset, sig ) ) {
1480 struct sigaction old;
1481 KMP_MB(); // Flush all pending memory write invalidates.
1482 __kmp_sigaction( sig, & __kmp_sighldrs[ sig ], & old );
1483 if ( ( old.sa_handler != __kmp_team_handler ) && ( old.sa_handler != __kmp_null_handler ) ) {
1484 // Restore the users signal handler.
1485 KB_TRACE( 10, ( "__kmp_remove_one_handler: oops, not our handler, restoring: sig=%d\n", sig ) );
1486 __kmp_sigaction( sig, & old, NULL );
1487 }; // if
1488 sigdelset( & __kmp_sigset, sig );
1489 KMP_MB(); // Flush all pending memory write invalidates.
1490 }; // if
1491} // __kmp_remove_one_handler
1492
1493
1494void
1495__kmp_install_signals( int parallel_init )
1496{
1497 KB_TRACE( 10, ( "__kmp_install_signals( %d )\n", parallel_init ) );
1498 if ( __kmp_handle_signals || ! parallel_init ) {
1499 // If ! parallel_init, we do not install handlers, just save original handlers.
1500 // Let us do it even __handle_signals is 0.
1501 sigemptyset( & __kmp_sigset );
1502 __kmp_install_one_handler( SIGHUP, __kmp_team_handler, parallel_init );
1503 __kmp_install_one_handler( SIGINT, __kmp_team_handler, parallel_init );
1504 __kmp_install_one_handler( SIGQUIT, __kmp_team_handler, parallel_init );
1505 __kmp_install_one_handler( SIGILL, __kmp_team_handler, parallel_init );
1506 __kmp_install_one_handler( SIGABRT, __kmp_team_handler, parallel_init );
1507 __kmp_install_one_handler( SIGFPE, __kmp_team_handler, parallel_init );
1508 __kmp_install_one_handler( SIGBUS, __kmp_team_handler, parallel_init );
1509 __kmp_install_one_handler( SIGSEGV, __kmp_team_handler, parallel_init );
1510 #ifdef SIGSYS
1511 __kmp_install_one_handler( SIGSYS, __kmp_team_handler, parallel_init );
1512 #endif // SIGSYS
1513 __kmp_install_one_handler( SIGTERM, __kmp_team_handler, parallel_init );
1514 #ifdef SIGPIPE
1515 __kmp_install_one_handler( SIGPIPE, __kmp_team_handler, parallel_init );
1516 #endif // SIGPIPE
1517 }; // if
1518} // __kmp_install_signals
1519
1520
1521void
1522__kmp_remove_signals( void )
1523{
1524 int sig;
1525 KB_TRACE( 10, ( "__kmp_remove_signals()\n" ) );
1526 for ( sig = 1; sig < NSIG; ++ sig ) {
1527 __kmp_remove_one_handler( sig );
1528 }; // for sig
1529} // __kmp_remove_signals
1530
1531
1532#endif // KMP_HANDLE_SIGNALS
1533
1534/* ------------------------------------------------------------------------ */
1535/* ------------------------------------------------------------------------ */
1536
1537void
1538__kmp_enable( int new_state )
1539{
1540 #ifdef KMP_CANCEL_THREADS
1541 int status, old_state;
1542 status = pthread_setcancelstate( new_state, & old_state );
1543 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1544 KMP_DEBUG_ASSERT( old_state == PTHREAD_CANCEL_DISABLE );
1545 #endif
1546}
1547
1548void
1549__kmp_disable( int * old_state )
1550{
1551 #ifdef KMP_CANCEL_THREADS
1552 int status;
1553 status = pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, old_state );
1554 KMP_CHECK_SYSFAIL( "pthread_setcancelstate", status );
1555 #endif
1556}
1557
1558/* ------------------------------------------------------------------------ */
1559/* ------------------------------------------------------------------------ */
1560
1561static void
1562__kmp_atfork_prepare (void)
1563{
1564 /* nothing to do */
1565}
1566
1567static void
1568__kmp_atfork_parent (void)
1569{
1570 /* nothing to do */
1571}
1572
1573/*
1574 Reset the library so execution in the child starts "all over again" with
1575 clean data structures in initial states. Don't worry about freeing memory
1576 allocated by parent, just abandon it to be safe.
1577*/
1578static void
1579__kmp_atfork_child (void)
1580{
1581 /* TODO make sure this is done right for nested/sibling */
1582 // ATT: Memory leaks are here? TODO: Check it and fix.
1583 /* KMP_ASSERT( 0 ); */
1584
1585 ++__kmp_fork_count;
1586
1587 __kmp_init_runtime = FALSE;
1588 __kmp_init_monitor = 0;
1589 __kmp_init_parallel = FALSE;
1590 __kmp_init_middle = FALSE;
1591 __kmp_init_serial = FALSE;
1592 TCW_4(__kmp_init_gtid, FALSE);
1593 __kmp_init_common = FALSE;
1594
1595 TCW_4(__kmp_init_user_locks, FALSE);
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001596#if ! KMP_USE_DYNAMIC_LOCK
Jim Cownie07ea89f2014-09-03 11:10:54 +00001597 __kmp_user_lock_table.used = 1;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001598 __kmp_user_lock_table.allocated = 0;
1599 __kmp_user_lock_table.table = NULL;
1600 __kmp_lock_blocks = NULL;
Andrey Churbanov5c56fb52015-02-20 18:05:17 +00001601#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001602
1603 __kmp_all_nth = 0;
1604 TCW_4(__kmp_nth, 0);
1605
1606 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate here
1607 so threadprivate doesn't use stale data */
1608 KA_TRACE( 10, ( "__kmp_atfork_child: checking cache address list %p\n",
1609 __kmp_threadpriv_cache_list ) );
1610
1611 while ( __kmp_threadpriv_cache_list != NULL ) {
1612
1613 if ( *__kmp_threadpriv_cache_list -> addr != NULL ) {
1614 KC_TRACE( 50, ( "__kmp_atfork_child: zeroing cache at address %p\n",
1615 &(*__kmp_threadpriv_cache_list -> addr) ) );
1616
1617 *__kmp_threadpriv_cache_list -> addr = NULL;
1618 }
1619 __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list -> next;
1620 }
1621
1622 __kmp_init_runtime = FALSE;
1623
1624 /* reset statically initialized locks */
1625 __kmp_init_bootstrap_lock( &__kmp_initz_lock );
1626 __kmp_init_bootstrap_lock( &__kmp_stdio_lock );
1627 __kmp_init_bootstrap_lock( &__kmp_console_lock );
1628
1629 /* This is necessary to make sure no stale data is left around */
1630 /* AC: customers complain that we use unsafe routines in the atfork
1631 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1632 in dynamic_link when check the presence of shared tbbmalloc library.
1633 Suggestion is to make the library initialization lazier, similar
1634 to what done for __kmpc_begin(). */
1635 // TODO: synchronize all static initializations with regular library
1636 // startup; look at kmp_global.c and etc.
1637 //__kmp_internal_begin ();
1638
1639}
1640
1641void
1642__kmp_register_atfork(void) {
1643 if ( __kmp_need_register_atfork ) {
1644 int status = pthread_atfork( __kmp_atfork_prepare, __kmp_atfork_parent, __kmp_atfork_child );
1645 KMP_CHECK_SYSFAIL( "pthread_atfork", status );
1646 __kmp_need_register_atfork = FALSE;
1647 }
1648}
1649
1650void
1651__kmp_suspend_initialize( void )
1652{
1653 int status;
1654 status = pthread_mutexattr_init( &__kmp_suspend_mutex_attr );
1655 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
1656 status = pthread_condattr_init( &__kmp_suspend_cond_attr );
1657 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
1658}
1659
1660static void
1661__kmp_suspend_initialize_thread( kmp_info_t *th )
1662{
1663 if ( th->th.th_suspend_init_count <= __kmp_fork_count ) {
1664 /* this means we haven't initialized the suspension pthread objects for this thread
1665 in this instance of the process */
1666 int status;
1667 status = pthread_cond_init( &th->th.th_suspend_cv.c_cond, &__kmp_suspend_cond_attr );
1668 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
1669 status = pthread_mutex_init( &th->th.th_suspend_mx.m_mutex, & __kmp_suspend_mutex_attr );
1670 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
1671 *(volatile int*)&th->th.th_suspend_init_count = __kmp_fork_count + 1;
1672 };
1673}
1674
1675void
1676__kmp_suspend_uninitialize_thread( kmp_info_t *th )
1677{
1678 if(th->th.th_suspend_init_count > __kmp_fork_count) {
1679 /* this means we have initialize the suspension pthread objects for this thread
1680 in this instance of the process */
1681 int status;
1682
1683 status = pthread_cond_destroy( &th->th.th_suspend_cv.c_cond );
1684 if ( status != 0 && status != EBUSY ) {
1685 KMP_SYSFAIL( "pthread_cond_destroy", status );
1686 };
1687 status = pthread_mutex_destroy( &th->th.th_suspend_mx.m_mutex );
1688 if ( status != 0 && status != EBUSY ) {
1689 KMP_SYSFAIL( "pthread_mutex_destroy", status );
1690 };
1691 --th->th.th_suspend_init_count;
1692 KMP_DEBUG_ASSERT(th->th.th_suspend_init_count == __kmp_fork_count);
1693 }
1694}
1695
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001696/* This routine puts the calling thread to sleep after setting the
1697 * sleep bit for the indicated flag variable to true.
Jim Cownie5e8470a2013-09-27 10:38:44 +00001698 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001699template <class C>
1700static inline void __kmp_suspend_template( int th_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001701{
Jonathan Peyton45be4502015-08-11 21:36:41 +00001702 KMP_TIME_DEVELOPER_BLOCK(USER_suspend);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001703 kmp_info_t *th = __kmp_threads[th_gtid];
1704 int status;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001705 typename C::flag_t old_spin;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001706
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001707 KF_TRACE( 30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001708
1709 __kmp_suspend_initialize_thread( th );
1710
1711 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1712 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
1713
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001714 KF_TRACE( 10, ( "__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1715 th_gtid, flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001716
1717 /* TODO: shouldn't this use release semantics to ensure that __kmp_suspend_initialize_thread
1718 gets called first?
1719 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001720 old_spin = flag->set_sleeping();
Jim Cownie5e8470a2013-09-27 10:38:44 +00001721
Jonathan Peytone03b62f2015-10-08 18:49:40 +00001722 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x, was %x\n",
1723 th_gtid, flag->get(), *(flag->get()), old_spin ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001724
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001725 if ( flag->done_check_val(old_spin) ) {
1726 old_spin = flag->unset_sleeping();
1727 KF_TRACE( 5, ( "__kmp_suspend_template: T#%d false alarm, reset sleep bit for spin(%p)\n",
1728 th_gtid, flag->get()) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001729 } else {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001730 /* Encapsulate in a loop as the documentation states that this may
1731 * "with low probability" return when the condition variable has
1732 * not been signaled or broadcast
1733 */
1734 int deactivated = FALSE;
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001735 TCW_PTR(th->th.th_sleep_loc, (void *)flag);
1736 while ( flag->is_sleeping() ) {
Jim Cownie5e8470a2013-09-27 10:38:44 +00001737#ifdef DEBUG_SUSPEND
1738 char buffer[128];
1739 __kmp_suspend_count++;
1740 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001741 __kmp_printf( "__kmp_suspend_template: suspending T#%d: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001742#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001743 // Mark the thread as no longer active (only in the first iteration of the loop).
Jim Cownie5e8470a2013-09-27 10:38:44 +00001744 if ( ! deactivated ) {
1745 th->th.th_active = FALSE;
1746 if ( th->th.th_active_in_pool ) {
1747 th->th.th_active_in_pool = FALSE;
1748 KMP_TEST_THEN_DEC32(
1749 (kmp_int32 *) &__kmp_thread_pool_active_nth );
1750 KMP_DEBUG_ASSERT( TCR_4(__kmp_thread_pool_active_nth) >= 0 );
1751 }
1752 deactivated = TRUE;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001753 }
1754
1755#if USE_SUSPEND_TIMEOUT
1756 struct timespec now;
1757 struct timeval tval;
1758 int msecs;
1759
1760 status = gettimeofday( &tval, NULL );
1761 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
1762 TIMEVAL_TO_TIMESPEC( &tval, &now );
1763
1764 msecs = (4*__kmp_dflt_blocktime) + 200;
1765 now.tv_sec += msecs / 1000;
1766 now.tv_nsec += (msecs % 1000)*1000;
1767
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001768 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_timedwait\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001769 th_gtid ) );
1770 status = pthread_cond_timedwait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex, & now );
1771#else
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001772 KF_TRACE( 15, ( "__kmp_suspend_template: T#%d about to perform pthread_cond_wait\n",
Jonathan Peyton1bd61b42015-10-08 19:44:16 +00001773 th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001774 status = pthread_cond_wait( &th->th.th_suspend_cv.c_cond, &th->th.th_suspend_mx.m_mutex );
1775#endif
1776
1777 if ( (status != 0) && (status != EINTR) && (status != ETIMEDOUT) ) {
1778 KMP_SYSFAIL( "pthread_cond_wait", status );
1779 }
1780#ifdef KMP_DEBUG
1781 if (status == ETIMEDOUT) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001782 if ( flag->is_sleeping() ) {
1783 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001784 } else {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001785 KF_TRACE( 2, ( "__kmp_suspend_template: T#%d timeout wakeup, sleep bit not set!\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001786 th_gtid ) );
1787 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001788 } else if ( flag->is_sleeping() ) {
1789 KF_TRACE( 100, ( "__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001790 }
1791#endif
Jim Cownie5e8470a2013-09-27 10:38:44 +00001792 } // while
1793
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001794 // Mark the thread as active again (if it was previous marked as inactive)
Jim Cownie5e8470a2013-09-27 10:38:44 +00001795 if ( deactivated ) {
1796 th->th.th_active = TRUE;
1797 if ( TCR_4(th->th.th_in_pool) ) {
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001798 KMP_TEST_THEN_INC32( (kmp_int32 *) &__kmp_thread_pool_active_nth );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001799 th->th.th_active_in_pool = TRUE;
1800 }
1801 }
1802 }
1803
1804#ifdef DEBUG_SUSPEND
1805 {
1806 char buffer[128];
1807 __kmp_print_cond( buffer, &th->th.th_suspend_cv);
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001808 __kmp_printf( "__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001809 }
1810#endif
1811
Jim Cownie5e8470a2013-09-27 10:38:44 +00001812 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1813 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1814
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001815 KF_TRACE( 30, ("__kmp_suspend_template: T#%d exit\n", th_gtid ) );
1816}
1817
1818void __kmp_suspend_32(int th_gtid, kmp_flag_32 *flag) {
1819 __kmp_suspend_template(th_gtid, flag);
1820}
1821void __kmp_suspend_64(int th_gtid, kmp_flag_64 *flag) {
1822 __kmp_suspend_template(th_gtid, flag);
1823}
1824void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
1825 __kmp_suspend_template(th_gtid, flag);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001826}
1827
1828
1829/* This routine signals the thread specified by target_gtid to wake up
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001830 * after setting the sleep bit indicated by the flag argument to FALSE.
1831 * The target thread must already have called __kmp_suspend_template()
Jim Cownie5e8470a2013-09-27 10:38:44 +00001832 */
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001833template <class C>
1834static inline void __kmp_resume_template( int target_gtid, C *flag )
Jim Cownie5e8470a2013-09-27 10:38:44 +00001835{
Jonathan Peyton45be4502015-08-11 21:36:41 +00001836 KMP_TIME_DEVELOPER_BLOCK(USER_resume);
Jim Cownie5e8470a2013-09-27 10:38:44 +00001837 kmp_info_t *th = __kmp_threads[target_gtid];
1838 int status;
Jim Cownie5e8470a2013-09-27 10:38:44 +00001839
1840#ifdef KMP_DEBUG
1841 int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
1842#endif
1843
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001844 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 +00001845 KMP_DEBUG_ASSERT( gtid != target_gtid );
1846
1847 __kmp_suspend_initialize_thread( th );
1848
1849 status = pthread_mutex_lock( &th->th.th_suspend_mx.m_mutex );
1850 KMP_CHECK_SYSFAIL( "pthread_mutex_lock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001851
Jonathan Peyton3f5dfc22015-11-09 16:31:51 +00001852 if (!flag) { // coming from __kmp_null_resume_wrapper
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001853 flag = (C *)th->th.th_sleep_loc;
1854 }
1855
Jonathan Peyton3f5dfc22015-11-09 16:31:51 +00001856 // First, check if the flag is null or its type has changed. If so, someone else woke it up.
1857 if (!flag || flag->get_type() != flag->get_ptr_type()) { // get_ptr_type simply shows what flag was cast to
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001858 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p)\n",
1859 gtid, target_gtid, NULL ) );
1860 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1861 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1862 return;
1863 }
Jonathan Peyton1bd61b42015-10-08 19:44:16 +00001864 else { // if multiple threads are sleeping, flag should be internally referring to a specific thread here
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001865 typename C::flag_t old_spin = flag->unset_sleeping();
1866 if ( ! flag->is_sleeping_val(old_spin) ) {
1867 KF_TRACE( 5, ( "__kmp_resume_template: T#%d exiting, thread T#%d already awake: flag(%p): "
1868 "%u => %u\n",
1869 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001870 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1871 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
1872 return;
1873 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001874 KF_TRACE( 5, ( "__kmp_resume_template: T#%d about to wakeup T#%d, reset sleep bit for flag's loc(%p): "
1875 "%u => %u\n",
1876 gtid, target_gtid, flag->get(), old_spin, *flag->get() ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001877 }
1878 TCW_PTR(th->th.th_sleep_loc, NULL);
1879
Jim Cownie5e8470a2013-09-27 10:38:44 +00001880
1881#ifdef DEBUG_SUSPEND
1882 {
1883 char buffer[128];
1884 __kmp_print_cond( buffer, &th->th.th_suspend_cv );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001885 __kmp_printf( "__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid, target_gtid, buffer );
Jim Cownie5e8470a2013-09-27 10:38:44 +00001886 }
1887#endif
1888
Jim Cownie5e8470a2013-09-27 10:38:44 +00001889 status = pthread_cond_signal( &th->th.th_suspend_cv.c_cond );
1890 KMP_CHECK_SYSFAIL( "pthread_cond_signal", status );
1891 status = pthread_mutex_unlock( &th->th.th_suspend_mx.m_mutex );
1892 KMP_CHECK_SYSFAIL( "pthread_mutex_unlock", status );
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001893 KF_TRACE( 30, ( "__kmp_resume_template: T#%d exiting after signaling wake up for T#%d\n",
Jim Cownie5e8470a2013-09-27 10:38:44 +00001894 gtid, target_gtid ) );
1895}
1896
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001897void __kmp_resume_32(int target_gtid, kmp_flag_32 *flag) {
1898 __kmp_resume_template(target_gtid, flag);
1899}
1900void __kmp_resume_64(int target_gtid, kmp_flag_64 *flag) {
1901 __kmp_resume_template(target_gtid, flag);
1902}
1903void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
1904 __kmp_resume_template(target_gtid, flag);
1905}
1906
Jim Cownie07ea89f2014-09-03 11:10:54 +00001907void
1908__kmp_resume_monitor()
1909{
1910 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
Jim Cownie5e8470a2013-09-27 10:38:44 +00002017void
2018__kmp_read_system_time( double *delta )
2019{
2020 double t_ns;
2021 struct timeval tval;
2022 struct timespec stop;
2023 int status;
2024
2025 status = gettimeofday( &tval, NULL );
2026 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
2027 TIMEVAL_TO_TIMESPEC( &tval, &stop );
2028 t_ns = TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start);
2029 *delta = (t_ns * 1e-9);
2030}
2031
2032void
2033__kmp_clear_system_time( void )
2034{
2035 struct timeval tval;
2036 int status;
2037 status = gettimeofday( &tval, NULL );
2038 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
2039 TIMEVAL_TO_TIMESPEC( &tval, &__kmp_sys_timer_data.start );
2040}
2041
2042/* ------------------------------------------------------------------------ */
2043/* ------------------------------------------------------------------------ */
2044
2045#ifdef BUILD_TV
2046
2047void
2048__kmp_tv_threadprivate_store( kmp_info_t *th, void *global_addr, void *thread_addr )
2049{
2050 struct tv_data *p;
2051
2052 p = (struct tv_data *) __kmp_allocate( sizeof( *p ) );
2053
2054 p->u.tp.global_addr = global_addr;
2055 p->u.tp.thread_addr = thread_addr;
2056
2057 p->type = (void *) 1;
2058
2059 p->next = th->th.th_local.tv_data;
2060 th->th.th_local.tv_data = p;
2061
2062 if ( p->next == 0 ) {
2063 int rc = pthread_setspecific( __kmp_tv_key, p );
2064 KMP_CHECK_SYSFAIL( "pthread_setspecific", rc );
2065 }
2066}
2067
2068#endif /* BUILD_TV */
2069
2070/* ------------------------------------------------------------------------ */
2071/* ------------------------------------------------------------------------ */
2072
2073static int
2074__kmp_get_xproc( void ) {
2075
2076 int r = 0;
2077
Joerg Sonnenberger7649cd42015-09-21 20:29:12 +00002078 #if KMP_OS_LINUX || KMP_OS_FREEBSD || KMP_OS_NETBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00002079
2080 r = sysconf( _SC_NPROCESSORS_ONLN );
2081
2082 #elif KMP_OS_DARWIN
2083
2084 // Bug C77011 High "OpenMP Threads and number of active cores".
2085
2086 // Find the number of available CPUs.
2087 kern_return_t rc;
2088 host_basic_info_data_t info;
2089 mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
2090 rc = host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t) & info, & num );
2091 if ( rc == 0 && num == HOST_BASIC_INFO_COUNT ) {
2092 // Cannot use KA_TRACE() here because this code works before trace support is
2093 // initialized.
2094 r = info.avail_cpus;
2095 } else {
2096 KMP_WARNING( CantGetNumAvailCPU );
2097 KMP_INFORM( AssumedNumCPU );
2098 }; // if
2099
2100 #else
2101
2102 #error "Unknown or unsupported OS."
2103
2104 #endif
2105
2106 return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
2107
2108} // __kmp_get_xproc
2109
Jim Cownie181b4bb2013-12-23 17:28:57 +00002110int
2111__kmp_read_from_file( char const *path, char const *format, ... )
2112{
2113 int result;
2114 va_list args;
Jim Cownie5e8470a2013-09-27 10:38:44 +00002115
Jim Cownie181b4bb2013-12-23 17:28:57 +00002116 va_start(args, format);
2117 FILE *f = fopen(path, "rb");
2118 if ( f == NULL )
2119 return 0;
2120 result = vfscanf(f, format, args);
2121 fclose(f);
Jim Cownie5e8470a2013-09-27 10:38:44 +00002122
Jim Cownie5e8470a2013-09-27 10:38:44 +00002123 return result;
Jim Cownie181b4bb2013-12-23 17:28:57 +00002124}
Jim Cownie5e8470a2013-09-27 10:38:44 +00002125
2126void
2127__kmp_runtime_initialize( void )
2128{
2129 int status;
2130 pthread_mutexattr_t mutex_attr;
2131 pthread_condattr_t cond_attr;
2132
2133 if ( __kmp_init_runtime ) {
2134 return;
2135 }; // if
2136
2137 #if ( KMP_ARCH_X86 || KMP_ARCH_X86_64 )
2138 if ( ! __kmp_cpuinfo.initialized ) {
2139 __kmp_query_cpuid( &__kmp_cpuinfo );
2140 }; // if
2141 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
2142
Jim Cownie5e8470a2013-09-27 10:38:44 +00002143 __kmp_xproc = __kmp_get_xproc();
2144
2145 if ( sysconf( _SC_THREADS ) ) {
2146
2147 /* Query the maximum number of threads */
2148 __kmp_sys_max_nth = sysconf( _SC_THREAD_THREADS_MAX );
2149 if ( __kmp_sys_max_nth == -1 ) {
2150 /* Unlimited threads for NPTL */
2151 __kmp_sys_max_nth = INT_MAX;
2152 }
2153 else if ( __kmp_sys_max_nth <= 1 ) {
2154 /* Can't tell, just use PTHREAD_THREADS_MAX */
2155 __kmp_sys_max_nth = KMP_MAX_NTH;
2156 }
2157
2158 /* Query the minimum stack size */
2159 __kmp_sys_min_stksize = sysconf( _SC_THREAD_STACK_MIN );
2160 if ( __kmp_sys_min_stksize <= 1 ) {
2161 __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
2162 }
2163 }
2164
2165 /* Set up minimum number of threads to switch to TLS gtid */
2166 __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
2167
Jim Cownie5e8470a2013-09-27 10:38:44 +00002168 #ifdef BUILD_TV
2169 {
2170 int rc = pthread_key_create( & __kmp_tv_key, 0 );
2171 KMP_CHECK_SYSFAIL( "pthread_key_create", rc );
2172 }
2173 #endif
2174
2175 status = pthread_key_create( &__kmp_gtid_threadprivate_key, __kmp_internal_end_dest );
2176 KMP_CHECK_SYSFAIL( "pthread_key_create", status );
2177 status = pthread_mutexattr_init( & mutex_attr );
2178 KMP_CHECK_SYSFAIL( "pthread_mutexattr_init", status );
2179 status = pthread_mutex_init( & __kmp_wait_mx.m_mutex, & mutex_attr );
2180 KMP_CHECK_SYSFAIL( "pthread_mutex_init", status );
2181 status = pthread_condattr_init( & cond_attr );
2182 KMP_CHECK_SYSFAIL( "pthread_condattr_init", status );
2183 status = pthread_cond_init( & __kmp_wait_cv.c_cond, & cond_attr );
2184 KMP_CHECK_SYSFAIL( "pthread_cond_init", status );
2185#if USE_ITT_BUILD
2186 __kmp_itt_initialize();
2187#endif /* USE_ITT_BUILD */
2188
2189 __kmp_init_runtime = TRUE;
2190}
2191
2192void
2193__kmp_runtime_destroy( void )
2194{
2195 int status;
2196
2197 if ( ! __kmp_init_runtime ) {
2198 return; // Nothing to do.
2199 };
2200
2201#if USE_ITT_BUILD
2202 __kmp_itt_destroy();
2203#endif /* USE_ITT_BUILD */
2204
2205 status = pthread_key_delete( __kmp_gtid_threadprivate_key );
2206 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2207 #ifdef BUILD_TV
2208 status = pthread_key_delete( __kmp_tv_key );
2209 KMP_CHECK_SYSFAIL( "pthread_key_delete", status );
2210 #endif
2211
2212 status = pthread_mutex_destroy( & __kmp_wait_mx.m_mutex );
2213 if ( status != 0 && status != EBUSY ) {
2214 KMP_SYSFAIL( "pthread_mutex_destroy", status );
2215 }
2216 status = pthread_cond_destroy( & __kmp_wait_cv.c_cond );
2217 if ( status != 0 && status != EBUSY ) {
2218 KMP_SYSFAIL( "pthread_cond_destroy", status );
2219 }
Alp Toker763b9392014-02-28 09:42:41 +00002220 #if KMP_AFFINITY_SUPPORTED
Jim Cownie5e8470a2013-09-27 10:38:44 +00002221 __kmp_affinity_uninitialize();
Jim Cownie5e8470a2013-09-27 10:38:44 +00002222 #endif
2223
2224 __kmp_init_runtime = FALSE;
2225}
2226
2227
2228/* Put the thread to sleep for a time period */
2229/* NOTE: not currently used anywhere */
2230void
2231__kmp_thread_sleep( int millis )
2232{
2233 sleep( ( millis + 500 ) / 1000 );
2234}
2235
2236/* Calculate the elapsed wall clock time for the user */
2237void
2238__kmp_elapsed( double *t )
2239{
2240 int status;
2241# ifdef FIX_SGI_CLOCK
2242 struct timespec ts;
2243
2244 status = clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &ts );
2245 KMP_CHECK_SYSFAIL_ERRNO( "clock_gettime", status );
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +00002246 *t = (double) ts.tv_nsec * (1.0 / (double) KMP_NSEC_PER_SEC) +
Jim Cownie5e8470a2013-09-27 10:38:44 +00002247 (double) ts.tv_sec;
2248# else
2249 struct timeval tv;
2250
2251 status = gettimeofday( & tv, NULL );
2252 KMP_CHECK_SYSFAIL_ERRNO( "gettimeofday", status );
Jonathan Peyton1e7a1dd2015-06-04 17:29:13 +00002253 *t = (double) tv.tv_usec * (1.0 / (double) KMP_USEC_PER_SEC) +
Jim Cownie5e8470a2013-09-27 10:38:44 +00002254 (double) tv.tv_sec;
2255# endif
2256}
2257
2258/* Calculate the elapsed wall clock tick for the user */
2259void
2260__kmp_elapsed_tick( double *t )
2261{
2262 *t = 1 / (double) CLOCKS_PER_SEC;
2263}
2264
2265/*
2266 Determine whether the given address is mapped into the current address space.
2267*/
2268
2269int
2270__kmp_is_address_mapped( void * addr ) {
2271
2272 int found = 0;
2273 int rc;
2274
Joerg Sonnenberger7649cd42015-09-21 20:29:12 +00002275 #if KMP_OS_LINUX || KMP_OS_FREEBSD
Jim Cownie5e8470a2013-09-27 10:38:44 +00002276
2277 /*
2278 On Linux* OS, read the /proc/<pid>/maps pseudo-file to get all the address ranges mapped
2279 into the address space.
2280 */
2281
2282 char * name = __kmp_str_format( "/proc/%d/maps", getpid() );
2283 FILE * file = NULL;
2284
2285 file = fopen( name, "r" );
2286 KMP_ASSERT( file != NULL );
2287
2288 for ( ; ; ) {
2289
2290 void * beginning = NULL;
2291 void * ending = NULL;
2292 char perms[ 5 ];
2293
2294 rc = fscanf( file, "%p-%p %4s %*[^\n]\n", & beginning, & ending, perms );
2295 if ( rc == EOF ) {
2296 break;
2297 }; // if
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002298 KMP_ASSERT( rc == 3 && KMP_STRLEN( perms ) == 4 ); // Make sure all fields are read.
Jim Cownie5e8470a2013-09-27 10:38:44 +00002299
2300 // Ending address is not included in the region, but beginning is.
2301 if ( ( addr >= beginning ) && ( addr < ending ) ) {
2302 perms[ 2 ] = 0; // 3th and 4th character does not matter.
2303 if ( strcmp( perms, "rw" ) == 0 ) {
2304 // Memory we are looking for should be readable and writable.
2305 found = 1;
2306 }; // if
2307 break;
2308 }; // if
2309
2310 }; // forever
2311
2312 // Free resources.
2313 fclose( file );
2314 KMP_INTERNAL_FREE( name );
2315
2316 #elif KMP_OS_DARWIN
2317
2318 /*
2319 On OS X*, /proc pseudo filesystem is not available. Try to read memory using vm
2320 interface.
2321 */
2322
2323 int buffer;
2324 vm_size_t count;
2325 rc =
2326 vm_read_overwrite(
2327 mach_task_self(), // Task to read memory of.
2328 (vm_address_t)( addr ), // Address to read from.
2329 1, // Number of bytes to be read.
2330 (vm_address_t)( & buffer ), // Address of buffer to save read bytes in.
2331 & count // Address of var to save number of read bytes in.
2332 );
2333 if ( rc == 0 ) {
2334 // Memory successfully read.
2335 found = 1;
2336 }; // if
2337
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +00002338 #elif KMP_OS_FREEBSD || KMP_OS_NETBSD
Alp Toker763b9392014-02-28 09:42:41 +00002339
Joerg Sonnenberger1564f3c2015-09-21 20:02:45 +00002340 // FIXME(FreeBSD, NetBSD): Implement this
Alp Toker763b9392014-02-28 09:42:41 +00002341 found = 1;
2342
Jim Cownie5e8470a2013-09-27 10:38:44 +00002343 #else
2344
2345 #error "Unknown or unsupported OS"
2346
2347 #endif
2348
2349 return found;
2350
2351} // __kmp_is_address_mapped
2352
2353#ifdef USE_LOAD_BALANCE
2354
2355
2356# if KMP_OS_DARWIN
2357
2358// The function returns the rounded value of the system load average
2359// during given time interval which depends on the value of
2360// __kmp_load_balance_interval variable (default is 60 sec, other values
2361// may be 300 sec or 900 sec).
2362// It returns -1 in case of error.
2363int
2364__kmp_get_load_balance( int max )
2365{
2366 double averages[3];
2367 int ret_avg = 0;
2368
2369 int res = getloadavg( averages, 3 );
2370
2371 //Check __kmp_load_balance_interval to determine which of averages to use.
2372 // getloadavg() may return the number of samples less than requested that is
2373 // less than 3.
2374 if ( __kmp_load_balance_interval < 180 && ( res >= 1 ) ) {
2375 ret_avg = averages[0];// 1 min
2376 } else if ( ( __kmp_load_balance_interval >= 180
2377 && __kmp_load_balance_interval < 600 ) && ( res >= 2 ) ) {
2378 ret_avg = averages[1];// 5 min
2379 } else if ( ( __kmp_load_balance_interval >= 600 ) && ( res == 3 ) ) {
2380 ret_avg = averages[2];// 15 min
Alp Toker8f2d3f02014-02-24 10:40:15 +00002381 } else {// Error occurred
Jim Cownie5e8470a2013-09-27 10:38:44 +00002382 return -1;
2383 }
2384
2385 return ret_avg;
2386}
2387
2388# else // Linux* OS
2389
2390// The fuction returns number of running (not sleeping) threads, or -1 in case of error.
2391// Error could be reported if Linux* OS kernel too old (without "/proc" support).
2392// Counting running threads stops if max running threads encountered.
2393int
2394__kmp_get_load_balance( int max )
2395{
2396 static int permanent_error = 0;
2397
2398 static int glb_running_threads = 0; /* Saved count of the running threads for the thread balance algortihm */
2399 static double glb_call_time = 0; /* Thread balance algorithm call time */
2400
2401 int running_threads = 0; // Number of running threads in the system.
2402
2403 DIR * proc_dir = NULL; // Handle of "/proc/" directory.
2404 struct dirent * proc_entry = NULL;
2405
2406 kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
2407 DIR * task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
2408 struct dirent * task_entry = NULL;
2409 int task_path_fixed_len;
2410
2411 kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
2412 int stat_file = -1;
2413 int stat_path_fixed_len;
2414
2415 int total_processes = 0; // Total number of processes in system.
2416 int total_threads = 0; // Total number of threads in system.
2417
2418 double call_time = 0.0;
2419
2420 __kmp_str_buf_init( & task_path );
2421 __kmp_str_buf_init( & stat_path );
2422
2423 __kmp_elapsed( & call_time );
2424
2425 if ( glb_call_time &&
2426 ( call_time - glb_call_time < __kmp_load_balance_interval ) ) {
2427 running_threads = glb_running_threads;
2428 goto finish;
2429 }
2430
2431 glb_call_time = call_time;
2432
2433 // Do not spend time on scanning "/proc/" if we have a permanent error.
2434 if ( permanent_error ) {
2435 running_threads = -1;
2436 goto finish;
2437 }; // if
2438
2439 if ( max <= 0 ) {
2440 max = INT_MAX;
2441 }; // if
2442
2443 // Open "/proc/" directory.
2444 proc_dir = opendir( "/proc" );
2445 if ( proc_dir == NULL ) {
2446 // Cannot open "/prroc/". Probably the kernel does not support it. Return an error now and
2447 // in subsequent calls.
2448 running_threads = -1;
2449 permanent_error = 1;
2450 goto finish;
2451 }; // if
2452
2453 // Initialize fixed part of task_path. This part will not change.
2454 __kmp_str_buf_cat( & task_path, "/proc/", 6 );
2455 task_path_fixed_len = task_path.used; // Remember number of used characters.
2456
2457 proc_entry = readdir( proc_dir );
2458 while ( proc_entry != NULL ) {
2459 // Proc entry is a directory and name starts with a digit. Assume it is a process'
2460 // directory.
2461 if ( proc_entry->d_type == DT_DIR && isdigit( proc_entry->d_name[ 0 ] ) ) {
2462
2463 ++ total_processes;
2464 // Make sure init process is the very first in "/proc", so we can replace
2465 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes == 1.
2466 // We are going to check that total_processes == 1 => d_name == "1" is true (where
2467 // "=>" is implication). Since C++ does not have => operator, let us replace it with its
2468 // equivalent: a => b == ! a || b.
2469 KMP_DEBUG_ASSERT( total_processes != 1 || strcmp( proc_entry->d_name, "1" ) == 0 );
2470
2471 // Construct task_path.
2472 task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002473 __kmp_str_buf_cat( & task_path, proc_entry->d_name, KMP_STRLEN( proc_entry->d_name ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002474 __kmp_str_buf_cat( & task_path, "/task", 5 );
2475
2476 task_dir = opendir( task_path.str );
2477 if ( task_dir == NULL ) {
2478 // Process can finish between reading "/proc/" directory entry and opening process'
2479 // "task/" directory. So, in general case we should not complain, but have to skip
2480 // this process and read the next one.
2481 // But on systems with no "task/" support we will spend lot of time to scan "/proc/"
2482 // tree again and again without any benefit. "init" process (its pid is 1) should
2483 // exist always, so, if we cannot open "/proc/1/task/" directory, it means "task/"
2484 // is not supported by kernel. Report an error now and in the future.
2485 if ( strcmp( proc_entry->d_name, "1" ) == 0 ) {
2486 running_threads = -1;
2487 permanent_error = 1;
2488 goto finish;
2489 }; // if
2490 } else {
2491 // Construct fixed part of stat file path.
2492 __kmp_str_buf_clear( & stat_path );
2493 __kmp_str_buf_cat( & stat_path, task_path.str, task_path.used );
2494 __kmp_str_buf_cat( & stat_path, "/", 1 );
2495 stat_path_fixed_len = stat_path.used;
2496
2497 task_entry = readdir( task_dir );
2498 while ( task_entry != NULL ) {
2499 // It is a directory and name starts with a digit.
2500 if ( proc_entry->d_type == DT_DIR && isdigit( task_entry->d_name[ 0 ] ) ) {
2501
2502 ++ total_threads;
2503
2504 // Consruct complete stat file path. Easiest way would be:
2505 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str, task_entry->d_name );
2506 // but seriae of __kmp_str_buf_cat works a bit faster.
2507 stat_path.used = stat_path_fixed_len; // Reset stat path to its fixed part.
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00002508 __kmp_str_buf_cat( & stat_path, task_entry->d_name, KMP_STRLEN( task_entry->d_name ) );
Jim Cownie5e8470a2013-09-27 10:38:44 +00002509 __kmp_str_buf_cat( & stat_path, "/stat", 5 );
2510
2511 // Note: Low-level API (open/read/close) is used. High-level API
2512 // (fopen/fclose) works ~ 30 % slower.
2513 stat_file = open( stat_path.str, O_RDONLY );
2514 if ( stat_file == -1 ) {
2515 // We cannot report an error because task (thread) can terminate just
2516 // before reading this file.
2517 } else {
2518 /*
2519 Content of "stat" file looks like:
2520
2521 24285 (program) S ...
2522
2523 It is a single line (if program name does not include fanny
2524 symbols). First number is a thread id, then name of executable file
2525 name in paretheses, then state of the thread. We need just thread
2526 state.
2527
2528 Good news: Length of program name is 15 characters max. Longer
2529 names are truncated.
2530
2531 Thus, we need rather short buffer: 15 chars for program name +
2532 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
2533
2534 Bad news: Program name may contain special symbols like space,
2535 closing parenthesis, or even new line. This makes parsing "stat"
2536 file not 100 % reliable. In case of fanny program names parsing
2537 may fail (report incorrect thread state).
2538
2539 Parsing "status" file looks more promissing (due to different
2540 file structure and escaping special symbols) but reading and
2541 parsing of "status" file works slower.
2542
2543 -- ln
2544 */
2545 char buffer[ 65 ];
2546 int len;
2547 len = read( stat_file, buffer, sizeof( buffer ) - 1 );
2548 if ( len >= 0 ) {
2549 buffer[ len ] = 0;
2550 // Using scanf:
2551 // sscanf( buffer, "%*d (%*s) %c ", & state );
2552 // looks very nice, but searching for a closing parenthesis works a
2553 // bit faster.
2554 char * close_parent = strstr( buffer, ") " );
2555 if ( close_parent != NULL ) {
2556 char state = * ( close_parent + 2 );
2557 if ( state == 'R' ) {
2558 ++ running_threads;
2559 if ( running_threads >= max ) {
2560 goto finish;
2561 }; // if
2562 }; // if
2563 }; // if
2564 }; // if
2565 close( stat_file );
2566 stat_file = -1;
2567 }; // if
2568 }; // if
2569 task_entry = readdir( task_dir );
2570 }; // while
2571 closedir( task_dir );
2572 task_dir = NULL;
2573 }; // if
2574 }; // if
2575 proc_entry = readdir( proc_dir );
2576 }; // while
2577
2578 //
2579 // There _might_ be a timing hole where the thread executing this
2580 // code get skipped in the load balance, and running_threads is 0.
2581 // Assert in the debug builds only!!!
2582 //
2583 KMP_DEBUG_ASSERT( running_threads > 0 );
2584 if ( running_threads <= 0 ) {
2585 running_threads = 1;
2586 }
2587
2588 finish: // Clean up and exit.
2589 if ( proc_dir != NULL ) {
2590 closedir( proc_dir );
2591 }; // if
2592 __kmp_str_buf_free( & task_path );
2593 if ( task_dir != NULL ) {
2594 closedir( task_dir );
2595 }; // if
2596 __kmp_str_buf_free( & stat_path );
2597 if ( stat_file != -1 ) {
2598 close( stat_file );
2599 }; // if
2600
2601 glb_running_threads = running_threads;
2602
2603 return running_threads;
2604
2605} // __kmp_get_load_balance
2606
2607# endif // KMP_OS_DARWIN
2608
2609#endif // USE_LOAD_BALANCE
2610
Andrey Churbanovedc370e2015-08-05 11:23:10 +00002611#if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC)
Jim Cownie3051f972014-08-07 10:12:54 +00002612
2613// we really only need the case with 1 argument, because CLANG always build
2614// a struct of pointers to shared variables referenced in the outlined function
2615int
2616__kmp_invoke_microtask( microtask_t pkfn,
2617 int gtid, int tid,
Jonathan Peyton122dd762015-07-13 18:55:45 +00002618 int argc, void *p_argv[]
2619#if OMPT_SUPPORT
2620 , void **exit_frame_ptr
2621#endif
2622)
2623{
2624#if OMPT_SUPPORT
2625 *exit_frame_ptr = __builtin_frame_address(0);
2626#endif
2627
Jim Cownie3051f972014-08-07 10:12:54 +00002628 switch (argc) {
2629 default:
2630 fprintf(stderr, "Too many args to microtask: %d!\n", argc);
2631 fflush(stderr);
2632 exit(-1);
2633 case 0:
2634 (*pkfn)(&gtid, &tid);
2635 break;
2636 case 1:
2637 (*pkfn)(&gtid, &tid, p_argv[0]);
2638 break;
2639 case 2:
2640 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
2641 break;
2642 case 3:
2643 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
2644 break;
2645 case 4:
2646 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
2647 break;
2648 case 5:
2649 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
2650 break;
2651 case 6:
2652 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2653 p_argv[5]);
2654 break;
2655 case 7:
2656 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2657 p_argv[5], p_argv[6]);
2658 break;
2659 case 8:
2660 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2661 p_argv[5], p_argv[6], p_argv[7]);
2662 break;
2663 case 9:
2664 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2665 p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
2666 break;
2667 case 10:
2668 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2669 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
2670 break;
2671 case 11:
2672 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2673 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
2674 break;
2675 case 12:
2676 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2677 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2678 p_argv[11]);
2679 break;
2680 case 13:
2681 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2682 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2683 p_argv[11], p_argv[12]);
2684 break;
2685 case 14:
2686 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2687 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2688 p_argv[11], p_argv[12], p_argv[13]);
2689 break;
2690 case 15:
2691 (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
2692 p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
2693 p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
2694 break;
2695 }
2696
Jonathan Peyton122dd762015-07-13 18:55:45 +00002697#if OMPT_SUPPORT
2698 *exit_frame_ptr = 0;
2699#endif
2700
Jim Cownie3051f972014-08-07 10:12:54 +00002701 return 1;
2702}
2703
2704#endif
Jim Cownie181b4bb2013-12-23 17:28:57 +00002705
Jim Cownie5e8470a2013-09-27 10:38:44 +00002706// end of file //
2707