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