2 * z_Linux_util.cpp -- platform specific routines.
5 //===----------------------------------------------------------------------===//
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8 // See https://llvm.org/LICENSE.txt for license information.
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11 //===----------------------------------------------------------------------===//
14 #include "kmp_affinity.h"
19 #include "kmp_stats.h"
21 #include "kmp_wait_release.h"
22 #include "kmp_wrapper_getpid.h"
24 #if !KMP_OS_DRAGONFLY && !KMP_OS_FREEBSD && !KMP_OS_NETBSD && !KMP_OS_OPENBSD
27 #include <math.h> // HUGE_VAL.
29 #include <semaphore.h>
30 #endif // KMP_OS_LINUX
31 #include <sys/resource.h>
32 #include <sys/syscall.h>
34 #include <sys/times.h>
38 #include <sys/sysinfo.h>
40 // We should really include <futex.h>, but that causes compatibility problems on
41 // different Linux* OS distributions that either require that you include (or
42 // break when you try to include) <pci/types.h>. Since all we need is the two
43 // macros below (which are part of the kernel ABI, so can't change) we just
44 // define the constants here and don't include <futex.h>
53 #include <mach/mach.h>
54 #include <sys/sysctl.h>
55 #elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD
56 #include <sys/types.h>
57 #include <sys/sysctl.h>
59 #include <pthread_np.h>
60 #elif KMP_OS_NETBSD || KMP_OS_OPENBSD
61 #include <sys/types.h>
62 #include <sys/sysctl.h>
64 #include <sys/loadavg.h>
71 struct kmp_sys_timer
{
72 struct timespec start
;
76 // Convert timeval to timespec.
77 #define TIMEVAL_TO_TIMESPEC(tv, ts) \
79 (ts)->tv_sec = (tv)->tv_sec; \
80 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
84 // Convert timespec to nanoseconds.
85 #define TS2NS(timespec) \
86 (((timespec).tv_sec * (long int)1e9) + (timespec).tv_nsec)
88 static struct kmp_sys_timer __kmp_sys_timer_data
;
90 #if KMP_HANDLE_SIGNALS
91 typedef void (*sig_func_t
)(int);
92 STATIC_EFI2_WORKAROUND
struct sigaction __kmp_sighldrs
[NSIG
];
93 static sigset_t __kmp_sigset
;
96 static int __kmp_init_runtime
= FALSE
;
98 static int __kmp_fork_count
= 0;
100 static pthread_condattr_t __kmp_suspend_cond_attr
;
101 static pthread_mutexattr_t __kmp_suspend_mutex_attr
;
103 static kmp_cond_align_t __kmp_wait_cv
;
104 static kmp_mutex_align_t __kmp_wait_mx
;
106 kmp_uint64 __kmp_ticks_per_msec
= 1000000;
107 kmp_uint64 __kmp_ticks_per_usec
= 1000;
110 static void __kmp_print_cond(char *buffer
, kmp_cond_align_t
*cond
) {
111 KMP_SNPRINTF(buffer
, 128, "(cond (lock (%ld, %d)), (descr (%p)))",
112 cond
->c_cond
.__c_lock
.__status
, cond
->c_cond
.__c_lock
.__spinlock
,
113 cond
->c_cond
.__c_waiting
);
117 #if ((KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED)
119 /* Affinity support */
121 void __kmp_affinity_bind_thread(int which
) {
122 KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
123 "Illegal set affinity operation when not capable");
125 kmp_affin_mask_t
*mask
;
126 KMP_CPU_ALLOC_ON_STACK(mask
);
128 KMP_CPU_SET(which
, mask
);
129 __kmp_set_system_affinity(mask
, TRUE
);
130 KMP_CPU_FREE_FROM_STACK(mask
);
133 /* Determine if we can access affinity functionality on this version of
134 * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
135 * __kmp_affin_mask_size to the appropriate value (0 means not capable). */
136 void __kmp_affinity_determine_capable(const char *env_var
) {
137 // Check and see if the OS supports thread affinity.
140 #define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024)
141 #define KMP_CPU_SET_TRY_SIZE CACHE_LINE
143 #define KMP_CPU_SET_SIZE_LIMIT (sizeof(cpuset_t))
146 int verbose
= __kmp_affinity
.flags
.verbose
;
147 int warnings
= __kmp_affinity
.flags
.warnings
;
148 enum affinity_type type
= __kmp_affinity
.type
;
153 buf
= (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT
);
155 // If the syscall returns a suggestion for the size,
156 // then we don't have to search for an appropriate size.
157 gCode
= syscall(__NR_sched_getaffinity
, 0, KMP_CPU_SET_TRY_SIZE
, buf
);
158 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
159 "initial getaffinity call returned %ld errno = %d\n",
162 if (gCode
< 0 && errno
!= EINVAL
) {
163 // System call not supported
165 (warnings
&& (type
!= affinity_none
) && (type
!= affinity_default
) &&
166 (type
!= affinity_disabled
))) {
168 kmp_msg_t err_code
= KMP_ERR(error
);
169 __kmp_msg(kmp_ms_warning
, KMP_MSG(GetAffSysCallNotSupported
, env_var
),
170 err_code
, __kmp_msg_null
);
171 if (__kmp_generate_warnings
== kmp_warnings_off
) {
172 __kmp_str_free(&err_code
.str
);
175 KMP_AFFINITY_DISABLE();
176 KMP_INTERNAL_FREE(buf
);
178 } else if (gCode
> 0) {
179 // The optimal situation: the OS returns the size of the buffer it expects.
180 KMP_AFFINITY_ENABLE(gCode
);
181 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
182 "affinity supported (mask size %d)\n",
183 (int)__kmp_affin_mask_size
));
184 KMP_INTERNAL_FREE(buf
);
188 // Call the getaffinity system call repeatedly with increasing set sizes
189 // until we succeed, or reach an upper bound on the search.
190 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
191 "searching for proper set size\n"));
193 for (size
= 1; size
<= KMP_CPU_SET_SIZE_LIMIT
; size
*= 2) {
194 gCode
= syscall(__NR_sched_getaffinity
, 0, size
, buf
);
195 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
196 "getaffinity for mask size %ld returned %ld errno = %d\n",
197 size
, gCode
, errno
));
200 if (errno
== ENOSYS
) {
201 // We shouldn't get here
202 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
203 "inconsistent OS call behavior: errno == ENOSYS for mask "
207 (warnings
&& (type
!= affinity_none
) &&
208 (type
!= affinity_default
) && (type
!= affinity_disabled
))) {
210 kmp_msg_t err_code
= KMP_ERR(error
);
211 __kmp_msg(kmp_ms_warning
, KMP_MSG(GetAffSysCallNotSupported
, env_var
),
212 err_code
, __kmp_msg_null
);
213 if (__kmp_generate_warnings
== kmp_warnings_off
) {
214 __kmp_str_free(&err_code
.str
);
217 KMP_AFFINITY_DISABLE();
218 KMP_INTERNAL_FREE(buf
);
224 KMP_AFFINITY_ENABLE(gCode
);
225 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
226 "affinity supported (mask size %d)\n",
227 (int)__kmp_affin_mask_size
));
228 KMP_INTERNAL_FREE(buf
);
234 buf
= (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT
);
235 gCode
= pthread_getaffinity_np(pthread_self(), KMP_CPU_SET_SIZE_LIMIT
,
236 reinterpret_cast<cpuset_t
*>(buf
));
237 KA_TRACE(30, ("__kmp_affinity_determine_capable: "
238 "initial getaffinity call returned %d errno = %d\n",
241 KMP_AFFINITY_ENABLE(KMP_CPU_SET_SIZE_LIMIT
);
242 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
243 "affinity supported (mask size %d)\n",
244 (int)__kmp_affin_mask_size
));
245 KMP_INTERNAL_FREE(buf
);
249 KMP_INTERNAL_FREE(buf
);
251 // Affinity is not supported
252 KMP_AFFINITY_DISABLE();
253 KA_TRACE(10, ("__kmp_affinity_determine_capable: "
254 "cannot determine mask size - affinity not supported\n"));
255 if (verbose
|| (warnings
&& (type
!= affinity_none
) &&
256 (type
!= affinity_default
) && (type
!= affinity_disabled
))) {
257 KMP_WARNING(AffCantGetMaskSize
, env_var
);
261 #endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
265 int __kmp_futex_determine_capable() {
267 long rc
= syscall(__NR_futex
, &loc
, FUTEX_WAKE
, 1, NULL
, NULL
, 0);
268 int retval
= (rc
== 0) || (errno
!= ENOSYS
);
271 ("__kmp_futex_determine_capable: rc = %d errno = %d\n", rc
, errno
));
272 KA_TRACE(10, ("__kmp_futex_determine_capable: futex syscall%s supported\n",
273 retval
? "" : " not"));
278 #endif // KMP_USE_FUTEX
280 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (!KMP_ASM_INTRINS)
281 /* Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
282 use compare_and_store for these routines */
284 kmp_int8
__kmp_test_then_or8(volatile kmp_int8
*p
, kmp_int8 d
) {
285 kmp_int8 old_value
, new_value
;
287 old_value
= TCR_1(*p
);
288 new_value
= old_value
| d
;
290 while (!KMP_COMPARE_AND_STORE_REL8(p
, old_value
, new_value
)) {
292 old_value
= TCR_1(*p
);
293 new_value
= old_value
| d
;
298 kmp_int8
__kmp_test_then_and8(volatile kmp_int8
*p
, kmp_int8 d
) {
299 kmp_int8 old_value
, new_value
;
301 old_value
= TCR_1(*p
);
302 new_value
= old_value
& d
;
304 while (!KMP_COMPARE_AND_STORE_REL8(p
, old_value
, new_value
)) {
306 old_value
= TCR_1(*p
);
307 new_value
= old_value
& d
;
312 kmp_uint32
__kmp_test_then_or32(volatile kmp_uint32
*p
, kmp_uint32 d
) {
313 kmp_uint32 old_value
, new_value
;
315 old_value
= TCR_4(*p
);
316 new_value
= old_value
| d
;
318 while (!KMP_COMPARE_AND_STORE_REL32(p
, old_value
, new_value
)) {
320 old_value
= TCR_4(*p
);
321 new_value
= old_value
| d
;
326 kmp_uint32
__kmp_test_then_and32(volatile kmp_uint32
*p
, kmp_uint32 d
) {
327 kmp_uint32 old_value
, new_value
;
329 old_value
= TCR_4(*p
);
330 new_value
= old_value
& d
;
332 while (!KMP_COMPARE_AND_STORE_REL32(p
, old_value
, new_value
)) {
334 old_value
= TCR_4(*p
);
335 new_value
= old_value
& d
;
341 kmp_int8
__kmp_test_then_add8(volatile kmp_int8
*p
, kmp_int8 d
) {
342 kmp_int8 old_value
, new_value
;
344 old_value
= TCR_1(*p
);
345 new_value
= old_value
+ d
;
347 while (!KMP_COMPARE_AND_STORE_REL8(p
, old_value
, new_value
)) {
349 old_value
= TCR_1(*p
);
350 new_value
= old_value
+ d
;
355 kmp_int64
__kmp_test_then_add64(volatile kmp_int64
*p
, kmp_int64 d
) {
356 kmp_int64 old_value
, new_value
;
358 old_value
= TCR_8(*p
);
359 new_value
= old_value
+ d
;
361 while (!KMP_COMPARE_AND_STORE_REL64(p
, old_value
, new_value
)) {
363 old_value
= TCR_8(*p
);
364 new_value
= old_value
+ d
;
368 #endif /* KMP_ARCH_X86 */
370 kmp_uint64
__kmp_test_then_or64(volatile kmp_uint64
*p
, kmp_uint64 d
) {
371 kmp_uint64 old_value
, new_value
;
373 old_value
= TCR_8(*p
);
374 new_value
= old_value
| d
;
375 while (!KMP_COMPARE_AND_STORE_REL64(p
, old_value
, new_value
)) {
377 old_value
= TCR_8(*p
);
378 new_value
= old_value
| d
;
383 kmp_uint64
__kmp_test_then_and64(volatile kmp_uint64
*p
, kmp_uint64 d
) {
384 kmp_uint64 old_value
, new_value
;
386 old_value
= TCR_8(*p
);
387 new_value
= old_value
& d
;
388 while (!KMP_COMPARE_AND_STORE_REL64(p
, old_value
, new_value
)) {
390 old_value
= TCR_8(*p
);
391 new_value
= old_value
& d
;
396 #endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
398 void __kmp_terminate_thread(int gtid
) {
400 kmp_info_t
*th
= __kmp_threads
[gtid
];
405 #ifdef KMP_CANCEL_THREADS
406 KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid
));
407 status
= pthread_cancel(th
->th
.th_info
.ds
.ds_thread
);
408 if (status
!= 0 && status
!= ESRCH
) {
409 __kmp_fatal(KMP_MSG(CantTerminateWorkerThread
), KMP_ERR(status
),
416 /* Set thread stack info according to values returned by pthread_getattr_np().
417 If values are unreasonable, assume call failed and use incremental stack
418 refinement method instead. Returns TRUE if the stack parameters could be
419 determined exactly, FALSE if incremental refinement is necessary. */
420 static kmp_int32
__kmp_set_stack_info(int gtid
, kmp_info_t
*th
) {
422 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
423 KMP_OS_HURD || KMP_OS_SOLARIS
429 /* Always do incremental stack refinement for ubermaster threads since the
430 initial thread stack range can be reduced by sibling thread creation so
431 pthread_attr_getstack may cause thread gtid aliasing */
432 if (!KMP_UBER_GTID(gtid
)) {
434 /* Fetch the real thread attributes */
435 status
= pthread_attr_init(&attr
);
436 KMP_CHECK_SYSFAIL("pthread_attr_init", status
);
437 #if KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD
438 status
= pthread_attr_get_np(pthread_self(), &attr
);
439 KMP_CHECK_SYSFAIL("pthread_attr_get_np", status
);
441 status
= pthread_getattr_np(pthread_self(), &attr
);
442 KMP_CHECK_SYSFAIL("pthread_getattr_np", status
);
444 status
= pthread_attr_getstack(&attr
, &addr
, &size
);
445 KMP_CHECK_SYSFAIL("pthread_attr_getstack", status
);
447 ("__kmp_set_stack_info: T#%d pthread_attr_getstack returned size:"
448 " %lu, low addr: %p\n",
450 status
= pthread_attr_destroy(&attr
);
451 KMP_CHECK_SYSFAIL("pthread_attr_destroy", status
);
454 if (size
!= 0 && addr
!= 0) { // was stack parameter determination successful?
455 /* Store the correct base and size */
456 TCW_PTR(th
->th
.th_info
.ds
.ds_stackbase
, (((char *)addr
) + size
));
457 TCW_PTR(th
->th
.th_info
.ds
.ds_stacksize
, size
);
458 TCW_4(th
->th
.th_info
.ds
.ds_stackgrow
, FALSE
);
461 #endif /* KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD \
462 || KMP_OS_HURD || KMP_OS_SOLARIS */
463 /* Use incremental refinement starting from initial conservative estimate */
464 TCW_PTR(th
->th
.th_info
.ds
.ds_stacksize
, 0);
465 TCW_PTR(th
->th
.th_info
.ds
.ds_stackbase
, &stack_data
);
466 TCW_4(th
->th
.th_info
.ds
.ds_stackgrow
, TRUE
);
470 static void *__kmp_launch_worker(void *thr
) {
471 int status
, old_type
, old_state
;
472 #ifdef KMP_BLOCK_SIGNALS
473 sigset_t new_set
, old_set
;
474 #endif /* KMP_BLOCK_SIGNALS */
476 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
477 KMP_OS_OPENBSD || KMP_OS_HURD || KMP_OS_SOLARIS
478 void *volatile padding
= 0;
482 gtid
= ((kmp_info_t
*)thr
)->th
.th_info
.ds
.ds_gtid
;
483 __kmp_gtid_set_specific(gtid
);
484 #ifdef KMP_TDATA_GTID
487 #if KMP_STATS_ENABLED
488 // set thread local index to point to thread-specific stats
489 __kmp_stats_thread_ptr
= ((kmp_info_t
*)thr
)->th
.th_stats
;
490 __kmp_stats_thread_ptr
->startLife();
491 KMP_SET_THREAD_STATE(IDLE
);
492 KMP_INIT_PARTITIONED_TIMERS(OMP_idle
);
496 __kmp_itt_thread_name(gtid
);
497 #endif /* USE_ITT_BUILD */
499 #if KMP_AFFINITY_SUPPORTED
500 __kmp_affinity_bind_init_mask(gtid
);
503 #ifdef KMP_CANCEL_THREADS
504 status
= pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, &old_type
);
505 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status
);
506 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
507 status
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &old_state
);
508 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status
);
511 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
512 // Set FP control regs to be a copy of the parallel initialization thread's.
513 __kmp_clear_x87_fpu_status_word();
514 __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word
);
515 __kmp_load_mxcsr(&__kmp_init_mxcsr
);
516 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
518 #ifdef KMP_BLOCK_SIGNALS
519 status
= sigfillset(&new_set
);
520 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status
);
521 status
= pthread_sigmask(SIG_BLOCK
, &new_set
, &old_set
);
522 KMP_CHECK_SYSFAIL("pthread_sigmask", status
);
523 #endif /* KMP_BLOCK_SIGNALS */
525 #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
526 KMP_OS_OPENBSD || KMP_OS_HURD || KMP_OS_SOLARIS
527 if (__kmp_stkoffset
> 0 && gtid
> 0) {
528 padding
= KMP_ALLOCA(gtid
* __kmp_stkoffset
);
534 __kmp_set_stack_info(gtid
, (kmp_info_t
*)thr
);
536 __kmp_check_stack_overlap((kmp_info_t
*)thr
);
538 exit_val
= __kmp_launch_thread((kmp_info_t
*)thr
);
540 #ifdef KMP_BLOCK_SIGNALS
541 status
= pthread_sigmask(SIG_SETMASK
, &old_set
, NULL
);
542 KMP_CHECK_SYSFAIL("pthread_sigmask", status
);
543 #endif /* KMP_BLOCK_SIGNALS */
549 /* The monitor thread controls all of the threads in the complex */
551 static void *__kmp_launch_monitor(void *thr
) {
552 int status
, old_type
, old_state
;
553 #ifdef KMP_BLOCK_SIGNALS
555 #endif /* KMP_BLOCK_SIGNALS */
556 struct timespec interval
;
558 KMP_MB(); /* Flush all pending memory write invalidates. */
560 KA_TRACE(10, ("__kmp_launch_monitor: #1 launched\n"));
562 /* register us as the monitor thread */
563 __kmp_gtid_set_specific(KMP_GTID_MONITOR
);
564 #ifdef KMP_TDATA_GTID
565 __kmp_gtid
= KMP_GTID_MONITOR
;
571 // Instruct Intel(R) Threading Tools to ignore monitor thread.
572 __kmp_itt_thread_ignore();
573 #endif /* USE_ITT_BUILD */
575 __kmp_set_stack_info(((kmp_info_t
*)thr
)->th
.th_info
.ds
.ds_gtid
,
578 __kmp_check_stack_overlap((kmp_info_t
*)thr
);
580 #ifdef KMP_CANCEL_THREADS
581 status
= pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, &old_type
);
582 KMP_CHECK_SYSFAIL("pthread_setcanceltype", status
);
583 // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
584 status
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &old_state
);
585 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status
);
588 #if KMP_REAL_TIME_FIX
589 // This is a potential fix which allows application with real-time scheduling
590 // policy work. However, decision about the fix is not made yet, so it is
591 // disabled by default.
592 { // Are program started with real-time scheduling policy?
593 int sched
= sched_getscheduler(0);
594 if (sched
== SCHED_FIFO
|| sched
== SCHED_RR
) {
595 // Yes, we are a part of real-time application. Try to increase the
596 // priority of the monitor.
597 struct sched_param param
;
598 int max_priority
= sched_get_priority_max(sched
);
600 KMP_WARNING(RealTimeSchedNotSupported
);
601 sched_getparam(0, ¶m
);
602 if (param
.sched_priority
< max_priority
) {
603 param
.sched_priority
+= 1;
604 rc
= sched_setscheduler(0, sched
, ¶m
);
607 kmp_msg_t err_code
= KMP_ERR(error
);
608 __kmp_msg(kmp_ms_warning
, KMP_MSG(CantChangeMonitorPriority
),
609 err_code
, KMP_MSG(MonitorWillStarve
), __kmp_msg_null
);
610 if (__kmp_generate_warnings
== kmp_warnings_off
) {
611 __kmp_str_free(&err_code
.str
);
615 // We cannot abort here, because number of CPUs may be enough for all
616 // the threads, including the monitor thread, so application could
617 // potentially work...
618 __kmp_msg(kmp_ms_warning
, KMP_MSG(RunningAtMaxPriority
),
619 KMP_MSG(MonitorWillStarve
), KMP_HNT(RunningAtMaxPriority
),
623 // AC: free thread that waits for monitor started
624 TCW_4(__kmp_global
.g
.g_time
.dt
.t_value
, 0);
626 #endif // KMP_REAL_TIME_FIX
628 KMP_MB(); /* Flush all pending memory write invalidates. */
630 if (__kmp_monitor_wakeups
== 1) {
632 interval
.tv_nsec
= 0;
635 interval
.tv_nsec
= (KMP_NSEC_PER_SEC
/ __kmp_monitor_wakeups
);
638 KA_TRACE(10, ("__kmp_launch_monitor: #2 monitor\n"));
640 while (!TCR_4(__kmp_global
.g
.g_done
)) {
644 /* This thread monitors the state of the system */
646 KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
648 status
= gettimeofday(&tval
, NULL
);
649 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status
);
650 TIMEVAL_TO_TIMESPEC(&tval
, &now
);
652 now
.tv_sec
+= interval
.tv_sec
;
653 now
.tv_nsec
+= interval
.tv_nsec
;
655 if (now
.tv_nsec
>= KMP_NSEC_PER_SEC
) {
657 now
.tv_nsec
-= KMP_NSEC_PER_SEC
;
660 status
= pthread_mutex_lock(&__kmp_wait_mx
.m_mutex
);
661 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status
);
662 // AC: the monitor should not fall asleep if g_done has been set
663 if (!TCR_4(__kmp_global
.g
.g_done
)) { // check once more under mutex
664 status
= pthread_cond_timedwait(&__kmp_wait_cv
.c_cond
,
665 &__kmp_wait_mx
.m_mutex
, &now
);
667 if (status
!= ETIMEDOUT
&& status
!= EINTR
) {
668 KMP_SYSFAIL("pthread_cond_timedwait", status
);
672 status
= pthread_mutex_unlock(&__kmp_wait_mx
.m_mutex
);
673 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status
);
675 TCW_4(__kmp_global
.g
.g_time
.dt
.t_value
,
676 TCR_4(__kmp_global
.g
.g_time
.dt
.t_value
) + 1);
678 KMP_MB(); /* Flush all pending memory write invalidates. */
681 KA_TRACE(10, ("__kmp_launch_monitor: #3 cleanup\n"));
683 #ifdef KMP_BLOCK_SIGNALS
684 status
= sigfillset(&new_set
);
685 KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status
);
686 status
= pthread_sigmask(SIG_UNBLOCK
, &new_set
, NULL
);
687 KMP_CHECK_SYSFAIL("pthread_sigmask", status
);
688 #endif /* KMP_BLOCK_SIGNALS */
690 KA_TRACE(10, ("__kmp_launch_monitor: #4 finished\n"));
692 if (__kmp_global
.g
.g_abort
!= 0) {
693 /* now we need to terminate the worker threads */
694 /* the value of t_abort is the signal we caught */
698 KA_TRACE(10, ("__kmp_launch_monitor: #5 terminate sig=%d\n",
699 __kmp_global
.g
.g_abort
));
701 /* terminate the OpenMP worker threads */
702 /* TODO this is not valid for sibling threads!!
703 * the uber master might not be 0 anymore.. */
704 for (gtid
= 1; gtid
< __kmp_threads_capacity
; ++gtid
)
705 __kmp_terminate_thread(gtid
);
709 KA_TRACE(10, ("__kmp_launch_monitor: #6 raise sig=%d\n",
710 __kmp_global
.g
.g_abort
));
712 if (__kmp_global
.g
.g_abort
> 0)
713 raise(__kmp_global
.g
.g_abort
);
716 KA_TRACE(10, ("__kmp_launch_monitor: #7 exit\n"));
720 #endif // KMP_USE_MONITOR
722 void __kmp_create_worker(int gtid
, kmp_info_t
*th
, size_t stack_size
) {
724 pthread_attr_t thread_attr
;
727 th
->th
.th_info
.ds
.ds_gtid
= gtid
;
729 #if KMP_STATS_ENABLED
730 // sets up worker thread stats
731 __kmp_acquire_tas_lock(&__kmp_stats_lock
, gtid
);
733 // th->th.th_stats is used to transfer thread-specific stats-pointer to
734 // __kmp_launch_worker. So when thread is created (goes into
735 // __kmp_launch_worker) it will set its thread local pointer to
737 if (!KMP_UBER_GTID(gtid
)) {
738 th
->th
.th_stats
= __kmp_stats_list
->push_back(gtid
);
740 // For root threads, __kmp_stats_thread_ptr is set in __kmp_register_root(),
741 // so set the th->th.th_stats field to it.
742 th
->th
.th_stats
= __kmp_stats_thread_ptr
;
744 __kmp_release_tas_lock(&__kmp_stats_lock
, gtid
);
746 #endif // KMP_STATS_ENABLED
748 if (KMP_UBER_GTID(gtid
)) {
749 KA_TRACE(10, ("__kmp_create_worker: uber thread (%d)\n", gtid
));
750 th
->th
.th_info
.ds
.ds_thread
= pthread_self();
751 __kmp_set_stack_info(gtid
, th
);
752 __kmp_check_stack_overlap(th
);
756 KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid
));
758 KMP_MB(); /* Flush all pending memory write invalidates. */
760 #ifdef KMP_THREAD_ATTR
761 status
= pthread_attr_init(&thread_attr
);
763 __kmp_fatal(KMP_MSG(CantInitThreadAttrs
), KMP_ERR(status
), __kmp_msg_null
);
765 status
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_JOINABLE
);
767 __kmp_fatal(KMP_MSG(CantSetWorkerState
), KMP_ERR(status
), __kmp_msg_null
);
770 /* Set stack size for this thread now.
771 The multiple of 2 is there because on some machines, requesting an unusual
772 stacksize causes the thread to have an offset before the dummy alloca()
773 takes place to create the offset. Since we want the user to have a
774 sufficient stacksize AND support a stack offset, we alloca() twice the
775 offset so that the upcoming alloca() does not eliminate any premade offset,
776 and also gives the user the stack space they requested for all threads */
777 stack_size
+= gtid
* __kmp_stkoffset
* 2;
779 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
780 "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
781 gtid
, KMP_DEFAULT_STKSIZE
, __kmp_stksize
, stack_size
));
783 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
784 status
= pthread_attr_setstacksize(&thread_attr
, stack_size
);
785 #ifdef KMP_BACKUP_STKSIZE
787 if (!__kmp_env_stksize
) {
788 stack_size
= KMP_BACKUP_STKSIZE
+ gtid
* __kmp_stkoffset
;
789 __kmp_stksize
= KMP_BACKUP_STKSIZE
;
790 KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
791 "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
793 gtid
, KMP_DEFAULT_STKSIZE
, __kmp_stksize
, stack_size
));
794 status
= pthread_attr_setstacksize(&thread_attr
, stack_size
);
797 #endif /* KMP_BACKUP_STKSIZE */
799 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize
, stack_size
), KMP_ERR(status
),
800 KMP_HNT(ChangeWorkerStackSize
), __kmp_msg_null
);
802 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
804 #endif /* KMP_THREAD_ATTR */
807 pthread_create(&handle
, &thread_attr
, __kmp_launch_worker
, (void *)th
);
808 if (status
!= 0 || !handle
) { // ??? Why do we check handle??
809 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
810 if (status
== EINVAL
) {
811 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize
, stack_size
), KMP_ERR(status
),
812 KMP_HNT(IncreaseWorkerStackSize
), __kmp_msg_null
);
814 if (status
== ENOMEM
) {
815 __kmp_fatal(KMP_MSG(CantSetWorkerStackSize
, stack_size
), KMP_ERR(status
),
816 KMP_HNT(DecreaseWorkerStackSize
), __kmp_msg_null
);
818 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
819 if (status
== EAGAIN
) {
820 __kmp_fatal(KMP_MSG(NoResourcesForWorkerThread
), KMP_ERR(status
),
821 KMP_HNT(Decrease_NUM_THREADS
), __kmp_msg_null
);
823 KMP_SYSFAIL("pthread_create", status
);
826 th
->th
.th_info
.ds
.ds_thread
= handle
;
828 #ifdef KMP_THREAD_ATTR
829 status
= pthread_attr_destroy(&thread_attr
);
831 kmp_msg_t err_code
= KMP_ERR(status
);
832 __kmp_msg(kmp_ms_warning
, KMP_MSG(CantDestroyThreadAttrs
), err_code
,
834 if (__kmp_generate_warnings
== kmp_warnings_off
) {
835 __kmp_str_free(&err_code
.str
);
838 #endif /* KMP_THREAD_ATTR */
840 KMP_MB(); /* Flush all pending memory write invalidates. */
842 KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid
));
844 } // __kmp_create_worker
847 void __kmp_create_monitor(kmp_info_t
*th
) {
849 pthread_attr_t thread_attr
;
852 int auto_adj_size
= FALSE
;
854 if (__kmp_dflt_blocktime
== KMP_MAX_BLOCKTIME
) {
855 // We don't need monitor thread in case of MAX_BLOCKTIME
856 KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
858 th
->th
.th_info
.ds
.ds_tid
= 0; // this makes reap_monitor no-op
859 th
->th
.th_info
.ds
.ds_gtid
= 0;
862 KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
864 KMP_MB(); /* Flush all pending memory write invalidates. */
866 th
->th
.th_info
.ds
.ds_tid
= KMP_GTID_MONITOR
;
867 th
->th
.th_info
.ds
.ds_gtid
= KMP_GTID_MONITOR
;
868 #if KMP_REAL_TIME_FIX
869 TCW_4(__kmp_global
.g
.g_time
.dt
.t_value
,
870 -1); // Will use it for synchronization a bit later.
872 TCW_4(__kmp_global
.g
.g_time
.dt
.t_value
, 0);
873 #endif // KMP_REAL_TIME_FIX
875 #ifdef KMP_THREAD_ATTR
876 if (__kmp_monitor_stksize
== 0) {
877 __kmp_monitor_stksize
= KMP_DEFAULT_MONITOR_STKSIZE
;
878 auto_adj_size
= TRUE
;
880 status
= pthread_attr_init(&thread_attr
);
882 __kmp_fatal(KMP_MSG(CantInitThreadAttrs
), KMP_ERR(status
), __kmp_msg_null
);
884 status
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_JOINABLE
);
886 __kmp_fatal(KMP_MSG(CantSetMonitorState
), KMP_ERR(status
), __kmp_msg_null
);
889 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
890 status
= pthread_attr_getstacksize(&thread_attr
, &size
);
891 KMP_CHECK_SYSFAIL("pthread_attr_getstacksize", status
);
893 size
= __kmp_sys_min_stksize
;
894 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
895 #endif /* KMP_THREAD_ATTR */
897 if (__kmp_monitor_stksize
== 0) {
898 __kmp_monitor_stksize
= KMP_DEFAULT_MONITOR_STKSIZE
;
900 if (__kmp_monitor_stksize
< __kmp_sys_min_stksize
) {
901 __kmp_monitor_stksize
= __kmp_sys_min_stksize
;
904 KA_TRACE(10, ("__kmp_create_monitor: default stacksize = %lu bytes,"
905 "requested stacksize = %lu bytes\n",
906 size
, __kmp_monitor_stksize
));
910 /* Set stack size for this thread now. */
911 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
912 KA_TRACE(10, ("__kmp_create_monitor: setting stacksize = %lu bytes,",
913 __kmp_monitor_stksize
));
914 status
= pthread_attr_setstacksize(&thread_attr
, __kmp_monitor_stksize
);
917 __kmp_monitor_stksize
*= 2;
920 kmp_msg_t err_code
= KMP_ERR(status
);
921 __kmp_msg(kmp_ms_warning
, // should this be fatal? BB
922 KMP_MSG(CantSetMonitorStackSize
, (long int)__kmp_monitor_stksize
),
923 err_code
, KMP_HNT(ChangeMonitorStackSize
), __kmp_msg_null
);
924 if (__kmp_generate_warnings
== kmp_warnings_off
) {
925 __kmp_str_free(&err_code
.str
);
928 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
931 pthread_create(&handle
, &thread_attr
, __kmp_launch_monitor
, (void *)th
);
934 #ifdef _POSIX_THREAD_ATTR_STACKSIZE
935 if (status
== EINVAL
) {
936 if (auto_adj_size
&& (__kmp_monitor_stksize
< (size_t)0x40000000)) {
937 __kmp_monitor_stksize
*= 2;
940 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize
, __kmp_monitor_stksize
),
941 KMP_ERR(status
), KMP_HNT(IncreaseMonitorStackSize
),
944 if (status
== ENOMEM
) {
945 __kmp_fatal(KMP_MSG(CantSetMonitorStackSize
, __kmp_monitor_stksize
),
946 KMP_ERR(status
), KMP_HNT(DecreaseMonitorStackSize
),
949 #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
950 if (status
== EAGAIN
) {
951 __kmp_fatal(KMP_MSG(NoResourcesForMonitorThread
), KMP_ERR(status
),
952 KMP_HNT(DecreaseNumberOfThreadsInUse
), __kmp_msg_null
);
954 KMP_SYSFAIL("pthread_create", status
);
957 th
->th
.th_info
.ds
.ds_thread
= handle
;
959 #if KMP_REAL_TIME_FIX
960 // Wait for the monitor thread is really started and set its *priority*.
961 KMP_DEBUG_ASSERT(sizeof(kmp_uint32
) ==
962 sizeof(__kmp_global
.g
.g_time
.dt
.t_value
));
963 __kmp_wait_4((kmp_uint32
volatile *)&__kmp_global
.g
.g_time
.dt
.t_value
, -1,
965 #endif // KMP_REAL_TIME_FIX
967 #ifdef KMP_THREAD_ATTR
968 status
= pthread_attr_destroy(&thread_attr
);
970 kmp_msg_t err_code
= KMP_ERR(status
);
971 __kmp_msg(kmp_ms_warning
, KMP_MSG(CantDestroyThreadAttrs
), err_code
,
973 if (__kmp_generate_warnings
== kmp_warnings_off
) {
974 __kmp_str_free(&err_code
.str
);
979 KMP_MB(); /* Flush all pending memory write invalidates. */
981 KA_TRACE(10, ("__kmp_create_monitor: monitor created %#.8lx\n",
982 th
->th
.th_info
.ds
.ds_thread
));
984 } // __kmp_create_monitor
985 #endif // KMP_USE_MONITOR
987 void __kmp_exit_thread(int exit_status
) {
988 pthread_exit((void *)(intptr_t)exit_status
);
989 } // __kmp_exit_thread
992 void __kmp_resume_monitor();
994 extern "C" void __kmp_reap_monitor(kmp_info_t
*th
) {
998 KA_TRACE(10, ("__kmp_reap_monitor: try to reap monitor thread with handle"
1000 th
->th
.th_info
.ds
.ds_thread
));
1002 // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
1003 // If both tid and gtid are 0, it means the monitor did not ever start.
1004 // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
1005 KMP_DEBUG_ASSERT(th
->th
.th_info
.ds
.ds_tid
== th
->th
.th_info
.ds
.ds_gtid
);
1006 if (th
->th
.th_info
.ds
.ds_gtid
!= KMP_GTID_MONITOR
) {
1007 KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
1011 KMP_MB(); /* Flush all pending memory write invalidates. */
1013 /* First, check to see whether the monitor thread exists to wake it up. This
1014 is to avoid performance problem when the monitor sleeps during
1015 blocktime-size interval */
1017 status
= pthread_kill(th
->th
.th_info
.ds
.ds_thread
, 0);
1018 if (status
!= ESRCH
) {
1019 __kmp_resume_monitor(); // Wake up the monitor thread
1021 KA_TRACE(10, ("__kmp_reap_monitor: try to join with monitor\n"));
1022 status
= pthread_join(th
->th
.th_info
.ds
.ds_thread
, &exit_val
);
1023 if (exit_val
!= th
) {
1024 __kmp_fatal(KMP_MSG(ReapMonitorError
), KMP_ERR(status
), __kmp_msg_null
);
1027 th
->th
.th_info
.ds
.ds_tid
= KMP_GTID_DNE
;
1028 th
->th
.th_info
.ds
.ds_gtid
= KMP_GTID_DNE
;
1030 KA_TRACE(10, ("__kmp_reap_monitor: done reaping monitor thread with handle"
1032 th
->th
.th_info
.ds
.ds_thread
));
1034 KMP_MB(); /* Flush all pending memory write invalidates. */
1037 // Empty symbol to export (see exports_so.txt) when
1038 // monitor thread feature is disabled
1039 extern "C" void __kmp_reap_monitor(kmp_info_t
*th
) {
1042 #endif // KMP_USE_MONITOR
1044 void __kmp_reap_worker(kmp_info_t
*th
) {
1048 KMP_MB(); /* Flush all pending memory write invalidates. */
1051 10, ("__kmp_reap_worker: try to reap T#%d\n", th
->th
.th_info
.ds
.ds_gtid
));
1053 status
= pthread_join(th
->th
.th_info
.ds
.ds_thread
, &exit_val
);
1055 /* Don't expose these to the user until we understand when they trigger */
1057 __kmp_fatal(KMP_MSG(ReapWorkerError
), KMP_ERR(status
), __kmp_msg_null
);
1059 if (exit_val
!= th
) {
1060 KA_TRACE(10, ("__kmp_reap_worker: worker T#%d did not reap properly, "
1062 th
->th
.th_info
.ds
.ds_gtid
, exit_val
));
1065 (void)status
; // unused variable
1066 #endif /* KMP_DEBUG */
1068 KA_TRACE(10, ("__kmp_reap_worker: done reaping T#%d\n",
1069 th
->th
.th_info
.ds
.ds_gtid
));
1071 KMP_MB(); /* Flush all pending memory write invalidates. */
1074 #if KMP_HANDLE_SIGNALS
1076 static void __kmp_null_handler(int signo
) {
1077 // Do nothing, for doing SIG_IGN-type actions.
1078 } // __kmp_null_handler
1080 static void __kmp_team_handler(int signo
) {
1081 if (__kmp_global
.g
.g_abort
== 0) {
1082 /* Stage 1 signal handler, let's shut down all of the threads */
1084 __kmp_debug_printf("__kmp_team_handler: caught signal = %d\n", signo
);
1099 if (__kmp_debug_buf
) {
1100 __kmp_dump_debug_buffer();
1102 __kmp_unregister_library(); // cleanup shared memory
1103 KMP_MB(); // Flush all pending memory write invalidates.
1104 TCW_4(__kmp_global
.g
.g_abort
, signo
);
1105 KMP_MB(); // Flush all pending memory write invalidates.
1106 TCW_4(__kmp_global
.g
.g_done
, TRUE
);
1107 KMP_MB(); // Flush all pending memory write invalidates.
1111 __kmp_debug_printf("__kmp_team_handler: unknown signal type");
1116 } // __kmp_team_handler
1118 static void __kmp_sigaction(int signum
, const struct sigaction
*act
,
1119 struct sigaction
*oldact
) {
1120 int rc
= sigaction(signum
, act
, oldact
);
1121 KMP_CHECK_SYSFAIL_ERRNO("sigaction", rc
);
1124 static void __kmp_install_one_handler(int sig
, sig_func_t handler_func
,
1125 int parallel_init
) {
1126 KMP_MB(); // Flush all pending memory write invalidates.
1128 ("__kmp_install_one_handler( %d, ..., %d )\n", sig
, parallel_init
));
1129 if (parallel_init
) {
1130 struct sigaction new_action
;
1131 struct sigaction old_action
;
1132 new_action
.sa_handler
= handler_func
;
1133 new_action
.sa_flags
= 0;
1134 sigfillset(&new_action
.sa_mask
);
1135 __kmp_sigaction(sig
, &new_action
, &old_action
);
1136 if (old_action
.sa_handler
== __kmp_sighldrs
[sig
].sa_handler
) {
1137 sigaddset(&__kmp_sigset
, sig
);
1139 // Restore/keep user's handler if one previously installed.
1140 __kmp_sigaction(sig
, &old_action
, NULL
);
1143 // Save initial/system signal handlers to see if user handlers installed.
1144 __kmp_sigaction(sig
, NULL
, &__kmp_sighldrs
[sig
]);
1146 KMP_MB(); // Flush all pending memory write invalidates.
1147 } // __kmp_install_one_handler
1149 static void __kmp_remove_one_handler(int sig
) {
1150 KB_TRACE(60, ("__kmp_remove_one_handler( %d )\n", sig
));
1151 if (sigismember(&__kmp_sigset
, sig
)) {
1152 struct sigaction old
;
1153 KMP_MB(); // Flush all pending memory write invalidates.
1154 __kmp_sigaction(sig
, &__kmp_sighldrs
[sig
], &old
);
1155 if ((old
.sa_handler
!= __kmp_team_handler
) &&
1156 (old
.sa_handler
!= __kmp_null_handler
)) {
1157 // Restore the users signal handler.
1158 KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
1159 "restoring: sig=%d\n",
1161 __kmp_sigaction(sig
, &old
, NULL
);
1163 sigdelset(&__kmp_sigset
, sig
);
1164 KMP_MB(); // Flush all pending memory write invalidates.
1166 } // __kmp_remove_one_handler
1168 void __kmp_install_signals(int parallel_init
) {
1169 KB_TRACE(10, ("__kmp_install_signals( %d )\n", parallel_init
));
1170 if (__kmp_handle_signals
|| !parallel_init
) {
1171 // If ! parallel_init, we do not install handlers, just save original
1172 // handlers. Let us do it even __handle_signals is 0.
1173 sigemptyset(&__kmp_sigset
);
1174 __kmp_install_one_handler(SIGHUP
, __kmp_team_handler
, parallel_init
);
1175 __kmp_install_one_handler(SIGINT
, __kmp_team_handler
, parallel_init
);
1176 __kmp_install_one_handler(SIGQUIT
, __kmp_team_handler
, parallel_init
);
1177 __kmp_install_one_handler(SIGILL
, __kmp_team_handler
, parallel_init
);
1178 __kmp_install_one_handler(SIGABRT
, __kmp_team_handler
, parallel_init
);
1179 __kmp_install_one_handler(SIGFPE
, __kmp_team_handler
, parallel_init
);
1180 __kmp_install_one_handler(SIGBUS
, __kmp_team_handler
, parallel_init
);
1181 __kmp_install_one_handler(SIGSEGV
, __kmp_team_handler
, parallel_init
);
1183 __kmp_install_one_handler(SIGSYS
, __kmp_team_handler
, parallel_init
);
1185 __kmp_install_one_handler(SIGTERM
, __kmp_team_handler
, parallel_init
);
1187 __kmp_install_one_handler(SIGPIPE
, __kmp_team_handler
, parallel_init
);
1190 } // __kmp_install_signals
1192 void __kmp_remove_signals(void) {
1194 KB_TRACE(10, ("__kmp_remove_signals()\n"));
1195 for (sig
= 1; sig
< NSIG
; ++sig
) {
1196 __kmp_remove_one_handler(sig
);
1198 } // __kmp_remove_signals
1200 #endif // KMP_HANDLE_SIGNALS
1202 void __kmp_enable(int new_state
) {
1203 #ifdef KMP_CANCEL_THREADS
1204 int status
, old_state
;
1205 status
= pthread_setcancelstate(new_state
, &old_state
);
1206 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status
);
1207 KMP_DEBUG_ASSERT(old_state
== PTHREAD_CANCEL_DISABLE
);
1211 void __kmp_disable(int *old_state
) {
1212 #ifdef KMP_CANCEL_THREADS
1214 status
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, old_state
);
1215 KMP_CHECK_SYSFAIL("pthread_setcancelstate", status
);
1219 static void __kmp_atfork_prepare(void) {
1220 __kmp_acquire_bootstrap_lock(&__kmp_initz_lock
);
1221 __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock
);
1224 static void __kmp_atfork_parent(void) {
1225 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock
);
1226 __kmp_release_bootstrap_lock(&__kmp_initz_lock
);
1229 /* Reset the library so execution in the child starts "all over again" with
1230 clean data structures in initial states. Don't worry about freeing memory
1231 allocated by parent, just abandon it to be safe. */
1232 static void __kmp_atfork_child(void) {
1233 __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock
);
1234 __kmp_release_bootstrap_lock(&__kmp_initz_lock
);
1235 /* TODO make sure this is done right for nested/sibling */
1236 // ATT: Memory leaks are here? TODO: Check it and fix.
1237 /* KMP_ASSERT( 0 ); */
1241 #if KMP_AFFINITY_SUPPORTED
1242 #if KMP_OS_LINUX || KMP_OS_FREEBSD
1243 // reset the affinity in the child to the initial thread
1244 // affinity in the parent
1245 kmp_set_thread_affinity_mask_initial();
1247 // Set default not to bind threads tightly in the child (we're expecting
1248 // over-subscription after the fork and this can improve things for
1249 // scripting languages that use OpenMP inside process-parallel code).
1250 if (__kmp_nested_proc_bind
.bind_types
!= NULL
) {
1251 __kmp_nested_proc_bind
.bind_types
[0] = proc_bind_false
;
1253 for (kmp_affinity_t
*affinity
: __kmp_affinities
)
1254 *affinity
= KMP_AFFINITY_INIT(affinity
->env_var
);
1255 __kmp_affin_fullMask
= nullptr;
1256 __kmp_affin_origMask
= nullptr;
1257 __kmp_topology
= nullptr;
1258 #endif // KMP_AFFINITY_SUPPORTED
1261 __kmp_init_monitor
= 0;
1263 __kmp_init_parallel
= FALSE
;
1264 __kmp_init_middle
= FALSE
;
1265 __kmp_init_serial
= FALSE
;
1266 TCW_4(__kmp_init_gtid
, FALSE
);
1267 __kmp_init_common
= FALSE
;
1269 TCW_4(__kmp_init_user_locks
, FALSE
);
1270 #if !KMP_USE_DYNAMIC_LOCK
1271 __kmp_user_lock_table
.used
= 1;
1272 __kmp_user_lock_table
.allocated
= 0;
1273 __kmp_user_lock_table
.table
= NULL
;
1274 __kmp_lock_blocks
= NULL
;
1278 TCW_4(__kmp_nth
, 0);
1280 __kmp_thread_pool
= NULL
;
1281 __kmp_thread_pool_insert_pt
= NULL
;
1282 __kmp_team_pool
= NULL
;
1284 /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate
1285 here so threadprivate doesn't use stale data */
1286 KA_TRACE(10, ("__kmp_atfork_child: checking cache address list %p\n",
1287 __kmp_threadpriv_cache_list
));
1289 while (__kmp_threadpriv_cache_list
!= NULL
) {
1291 if (*__kmp_threadpriv_cache_list
->addr
!= NULL
) {
1292 KC_TRACE(50, ("__kmp_atfork_child: zeroing cache at address %p\n",
1293 &(*__kmp_threadpriv_cache_list
->addr
)));
1295 *__kmp_threadpriv_cache_list
->addr
= NULL
;
1297 __kmp_threadpriv_cache_list
= __kmp_threadpriv_cache_list
->next
;
1300 __kmp_init_runtime
= FALSE
;
1302 /* reset statically initialized locks */
1303 __kmp_init_bootstrap_lock(&__kmp_initz_lock
);
1304 __kmp_init_bootstrap_lock(&__kmp_stdio_lock
);
1305 __kmp_init_bootstrap_lock(&__kmp_console_lock
);
1306 __kmp_init_bootstrap_lock(&__kmp_task_team_lock
);
1309 __kmp_itt_reset(); // reset ITT's global state
1310 #endif /* USE_ITT_BUILD */
1313 // Child process often get terminated without any use of OpenMP. That might
1314 // cause mapped shared memory file to be left unattended. Thus we postpone
1315 // library registration till middle initialization in the child process.
1316 __kmp_need_register_serial
= FALSE
;
1317 __kmp_serial_initialize();
1320 /* This is necessary to make sure no stale data is left around */
1321 /* AC: customers complain that we use unsafe routines in the atfork
1322 handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
1323 in dynamic_link when check the presence of shared tbbmalloc library.
1324 Suggestion is to make the library initialization lazier, similar
1325 to what done for __kmpc_begin(). */
1326 // TODO: synchronize all static initializations with regular library
1327 // startup; look at kmp_global.cpp and etc.
1328 //__kmp_internal_begin ();
1331 void __kmp_register_atfork(void) {
1332 if (__kmp_need_register_atfork
) {
1333 int status
= pthread_atfork(__kmp_atfork_prepare
, __kmp_atfork_parent
,
1334 __kmp_atfork_child
);
1335 KMP_CHECK_SYSFAIL("pthread_atfork", status
);
1336 __kmp_need_register_atfork
= FALSE
;
1340 void __kmp_suspend_initialize(void) {
1342 status
= pthread_mutexattr_init(&__kmp_suspend_mutex_attr
);
1343 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status
);
1344 status
= pthread_condattr_init(&__kmp_suspend_cond_attr
);
1345 KMP_CHECK_SYSFAIL("pthread_condattr_init", status
);
1348 void __kmp_suspend_initialize_thread(kmp_info_t
*th
) {
1349 int old_value
= KMP_ATOMIC_LD_RLX(&th
->th
.th_suspend_init_count
);
1350 int new_value
= __kmp_fork_count
+ 1;
1351 // Return if already initialized
1352 if (old_value
== new_value
)
1354 // Wait, then return if being initialized
1355 if (old_value
== -1 || !__kmp_atomic_compare_store(
1356 &th
->th
.th_suspend_init_count
, old_value
, -1)) {
1357 while (KMP_ATOMIC_LD_ACQ(&th
->th
.th_suspend_init_count
) != new_value
) {
1361 // Claim to be the initializer and do initializations
1363 status
= pthread_cond_init(&th
->th
.th_suspend_cv
.c_cond
,
1364 &__kmp_suspend_cond_attr
);
1365 KMP_CHECK_SYSFAIL("pthread_cond_init", status
);
1366 status
= pthread_mutex_init(&th
->th
.th_suspend_mx
.m_mutex
,
1367 &__kmp_suspend_mutex_attr
);
1368 KMP_CHECK_SYSFAIL("pthread_mutex_init", status
);
1369 KMP_ATOMIC_ST_REL(&th
->th
.th_suspend_init_count
, new_value
);
1373 void __kmp_suspend_uninitialize_thread(kmp_info_t
*th
) {
1374 if (KMP_ATOMIC_LD_ACQ(&th
->th
.th_suspend_init_count
) > __kmp_fork_count
) {
1375 /* this means we have initialize the suspension pthread objects for this
1376 thread in this instance of the process */
1379 status
= pthread_cond_destroy(&th
->th
.th_suspend_cv
.c_cond
);
1380 if (status
!= 0 && status
!= EBUSY
) {
1381 KMP_SYSFAIL("pthread_cond_destroy", status
);
1383 status
= pthread_mutex_destroy(&th
->th
.th_suspend_mx
.m_mutex
);
1384 if (status
!= 0 && status
!= EBUSY
) {
1385 KMP_SYSFAIL("pthread_mutex_destroy", status
);
1387 --th
->th
.th_suspend_init_count
;
1388 KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&th
->th
.th_suspend_init_count
) ==
1393 // return true if lock obtained, false otherwise
1394 int __kmp_try_suspend_mx(kmp_info_t
*th
) {
1395 return (pthread_mutex_trylock(&th
->th
.th_suspend_mx
.m_mutex
) == 0);
1398 void __kmp_lock_suspend_mx(kmp_info_t
*th
) {
1399 int status
= pthread_mutex_lock(&th
->th
.th_suspend_mx
.m_mutex
);
1400 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status
);
1403 void __kmp_unlock_suspend_mx(kmp_info_t
*th
) {
1404 int status
= pthread_mutex_unlock(&th
->th
.th_suspend_mx
.m_mutex
);
1405 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status
);
1408 /* This routine puts the calling thread to sleep after setting the
1409 sleep bit for the indicated flag variable to true. */
1411 static inline void __kmp_suspend_template(int th_gtid
, C
*flag
) {
1412 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_suspend
);
1413 kmp_info_t
*th
= __kmp_threads
[th_gtid
];
1415 typename
C::flag_t old_spin
;
1417 KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid
,
1420 __kmp_suspend_initialize_thread(th
);
1422 __kmp_lock_suspend_mx(th
);
1424 KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
1425 th_gtid
, flag
->get()));
1427 /* TODO: shouldn't this use release semantics to ensure that
1428 __kmp_suspend_initialize_thread gets called first? */
1429 old_spin
= flag
->set_sleeping();
1430 TCW_PTR(th
->th
.th_sleep_loc
, (void *)flag
);
1431 th
->th
.th_sleep_loc_type
= flag
->get_type();
1432 if (__kmp_dflt_blocktime
== KMP_MAX_BLOCKTIME
&&
1433 __kmp_pause_status
!= kmp_soft_paused
) {
1434 flag
->unset_sleeping();
1435 TCW_PTR(th
->th
.th_sleep_loc
, NULL
);
1436 th
->th
.th_sleep_loc_type
= flag_unset
;
1437 __kmp_unlock_suspend_mx(th
);
1440 KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x,"
1442 th_gtid
, flag
->get(), flag
->load(), old_spin
));
1444 if (flag
->done_check_val(old_spin
) || flag
->done_check()) {
1445 flag
->unset_sleeping();
1446 TCW_PTR(th
->th
.th_sleep_loc
, NULL
);
1447 th
->th
.th_sleep_loc_type
= flag_unset
;
1448 KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
1450 th_gtid
, flag
->get()));
1452 /* Encapsulate in a loop as the documentation states that this may
1453 "with low probability" return when the condition variable has
1454 not been signaled or broadcast */
1455 int deactivated
= FALSE
;
1457 while (flag
->is_sleeping()) {
1458 #ifdef DEBUG_SUSPEND
1460 __kmp_suspend_count
++;
1461 __kmp_print_cond(buffer
, &th
->th
.th_suspend_cv
);
1462 __kmp_printf("__kmp_suspend_template: suspending T#%d: %s\n", th_gtid
,
1465 // Mark the thread as no longer active (only in the first iteration of the
1468 th
->th
.th_active
= FALSE
;
1469 if (th
->th
.th_active_in_pool
) {
1470 th
->th
.th_active_in_pool
= FALSE
;
1471 KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth
);
1472 KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth
) >= 0);
1477 KMP_DEBUG_ASSERT(th
->th
.th_sleep_loc
);
1478 KMP_DEBUG_ASSERT(flag
->get_type() == th
->th
.th_sleep_loc_type
);
1480 #if USE_SUSPEND_TIMEOUT
1481 struct timespec now
;
1482 struct timeval tval
;
1485 status
= gettimeofday(&tval
, NULL
);
1486 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status
);
1487 TIMEVAL_TO_TIMESPEC(&tval
, &now
);
1489 msecs
= (4 * __kmp_dflt_blocktime
) + 200;
1490 now
.tv_sec
+= msecs
/ 1000;
1491 now
.tv_nsec
+= (msecs
% 1000) * 1000;
1493 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
1494 "pthread_cond_timedwait\n",
1496 status
= pthread_cond_timedwait(&th
->th
.th_suspend_cv
.c_cond
,
1497 &th
->th
.th_suspend_mx
.m_mutex
, &now
);
1499 KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform"
1500 " pthread_cond_wait\n",
1502 status
= pthread_cond_wait(&th
->th
.th_suspend_cv
.c_cond
,
1503 &th
->th
.th_suspend_mx
.m_mutex
);
1504 #endif // USE_SUSPEND_TIMEOUT
1506 if ((status
!= 0) && (status
!= EINTR
) && (status
!= ETIMEDOUT
)) {
1507 KMP_SYSFAIL("pthread_cond_wait", status
);
1510 KMP_DEBUG_ASSERT(flag
->get_type() == flag
->get_ptr_type());
1512 if (!flag
->is_sleeping() &&
1513 ((status
== EINTR
) || (status
== ETIMEDOUT
))) {
1514 // if interrupt or timeout, and thread is no longer sleeping, we need to
1515 // make sure sleep_loc gets reset; however, this shouldn't be needed if
1516 // we woke up with resume
1517 flag
->unset_sleeping();
1518 TCW_PTR(th
->th
.th_sleep_loc
, NULL
);
1519 th
->th
.th_sleep_loc_type
= flag_unset
;
1522 if (status
== ETIMEDOUT
) {
1523 if (flag
->is_sleeping()) {
1525 ("__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid
));
1527 KF_TRACE(2, ("__kmp_suspend_template: T#%d timeout wakeup, sleep bit "
1530 TCW_PTR(th
->th
.th_sleep_loc
, NULL
);
1531 th
->th
.th_sleep_loc_type
= flag_unset
;
1533 } else if (flag
->is_sleeping()) {
1535 ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid
));
1540 // Mark the thread as active again (if it was previous marked as inactive)
1542 th
->th
.th_active
= TRUE
;
1543 if (TCR_4(th
->th
.th_in_pool
)) {
1544 KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth
);
1545 th
->th
.th_active_in_pool
= TRUE
;
1549 // We may have had the loop variable set before entering the loop body;
1550 // so we need to reset sleep_loc.
1551 TCW_PTR(th
->th
.th_sleep_loc
, NULL
);
1552 th
->th
.th_sleep_loc_type
= flag_unset
;
1554 KMP_DEBUG_ASSERT(!flag
->is_sleeping());
1555 KMP_DEBUG_ASSERT(!th
->th
.th_sleep_loc
);
1556 #ifdef DEBUG_SUSPEND
1559 __kmp_print_cond(buffer
, &th
->th
.th_suspend_cv
);
1560 __kmp_printf("__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid
,
1565 __kmp_unlock_suspend_mx(th
);
1566 KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid
));
1569 template <bool C
, bool S
>
1570 void __kmp_suspend_32(int th_gtid
, kmp_flag_32
<C
, S
> *flag
) {
1571 __kmp_suspend_template(th_gtid
, flag
);
1573 template <bool C
, bool S
>
1574 void __kmp_suspend_64(int th_gtid
, kmp_flag_64
<C
, S
> *flag
) {
1575 __kmp_suspend_template(th_gtid
, flag
);
1577 template <bool C
, bool S
>
1578 void __kmp_atomic_suspend_64(int th_gtid
, kmp_atomic_flag_64
<C
, S
> *flag
) {
1579 __kmp_suspend_template(th_gtid
, flag
);
1581 void __kmp_suspend_oncore(int th_gtid
, kmp_flag_oncore
*flag
) {
1582 __kmp_suspend_template(th_gtid
, flag
);
1585 template void __kmp_suspend_32
<false, false>(int, kmp_flag_32
<false, false> *);
1586 template void __kmp_suspend_64
<false, true>(int, kmp_flag_64
<false, true> *);
1587 template void __kmp_suspend_64
<true, false>(int, kmp_flag_64
<true, false> *);
1589 __kmp_atomic_suspend_64
<false, true>(int, kmp_atomic_flag_64
<false, true> *);
1591 __kmp_atomic_suspend_64
<true, false>(int, kmp_atomic_flag_64
<true, false> *);
1593 /* This routine signals the thread specified by target_gtid to wake up
1594 after setting the sleep bit indicated by the flag argument to FALSE.
1595 The target thread must already have called __kmp_suspend_template() */
1597 static inline void __kmp_resume_template(int target_gtid
, C
*flag
) {
1598 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume
);
1599 kmp_info_t
*th
= __kmp_threads
[target_gtid
];
1603 int gtid
= TCR_4(__kmp_init_gtid
) ? __kmp_get_gtid() : -1;
1606 KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
1607 gtid
, target_gtid
));
1608 KMP_DEBUG_ASSERT(gtid
!= target_gtid
);
1610 __kmp_suspend_initialize_thread(th
);
1612 __kmp_lock_suspend_mx(th
);
1614 if (!flag
|| flag
!= th
->th
.th_sleep_loc
) {
1615 // coming from __kmp_null_resume_wrapper, or thread is now sleeping on a
1616 // different location; wake up at new location
1617 flag
= (C
*)CCAST(void *, th
->th
.th_sleep_loc
);
1620 // First, check if the flag is null or its type has changed. If so, someone
1622 if (!flag
) { // Thread doesn't appear to be sleeping on anything
1623 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1624 "awake: flag(%p)\n",
1625 gtid
, target_gtid
, (void *)NULL
));
1626 __kmp_unlock_suspend_mx(th
);
1628 } else if (flag
->get_type() != th
->th
.th_sleep_loc_type
) {
1629 // Flag type does not appear to match this function template; possibly the
1630 // thread is sleeping on something else. Try null resume again.
1633 ("__kmp_resume_template: T#%d retrying, thread T#%d Mismatch flag(%p), "
1634 "spin(%p) type=%d ptr_type=%d\n",
1635 gtid
, target_gtid
, flag
, flag
->get(), flag
->get_type(),
1636 th
->th
.th_sleep_loc_type
));
1637 __kmp_unlock_suspend_mx(th
);
1638 __kmp_null_resume_wrapper(th
);
1640 } else { // if multiple threads are sleeping, flag should be internally
1641 // referring to a specific thread here
1642 if (!flag
->is_sleeping()) {
1643 KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
1644 "awake: flag(%p): %u\n",
1645 gtid
, target_gtid
, flag
->get(), (unsigned int)flag
->load()));
1646 __kmp_unlock_suspend_mx(th
);
1650 KMP_DEBUG_ASSERT(flag
);
1651 flag
->unset_sleeping();
1652 TCW_PTR(th
->th
.th_sleep_loc
, NULL
);
1653 th
->th
.th_sleep_loc_type
= flag_unset
;
1655 KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset "
1656 "sleep bit for flag's loc(%p): %u\n",
1657 gtid
, target_gtid
, flag
->get(), (unsigned int)flag
->load()));
1659 #ifdef DEBUG_SUSPEND
1662 __kmp_print_cond(buffer
, &th
->th
.th_suspend_cv
);
1663 __kmp_printf("__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid
,
1664 target_gtid
, buffer
);
1667 status
= pthread_cond_signal(&th
->th
.th_suspend_cv
.c_cond
);
1668 KMP_CHECK_SYSFAIL("pthread_cond_signal", status
);
1669 __kmp_unlock_suspend_mx(th
);
1670 KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
1672 gtid
, target_gtid
));
1675 template <bool C
, bool S
>
1676 void __kmp_resume_32(int target_gtid
, kmp_flag_32
<C
, S
> *flag
) {
1677 __kmp_resume_template(target_gtid
, flag
);
1679 template <bool C
, bool S
>
1680 void __kmp_resume_64(int target_gtid
, kmp_flag_64
<C
, S
> *flag
) {
1681 __kmp_resume_template(target_gtid
, flag
);
1683 template <bool C
, bool S
>
1684 void __kmp_atomic_resume_64(int target_gtid
, kmp_atomic_flag_64
<C
, S
> *flag
) {
1685 __kmp_resume_template(target_gtid
, flag
);
1687 void __kmp_resume_oncore(int target_gtid
, kmp_flag_oncore
*flag
) {
1688 __kmp_resume_template(target_gtid
, flag
);
1691 template void __kmp_resume_32
<false, true>(int, kmp_flag_32
<false, true> *);
1692 template void __kmp_resume_32
<false, false>(int, kmp_flag_32
<false, false> *);
1693 template void __kmp_resume_64
<false, true>(int, kmp_flag_64
<false, true> *);
1695 __kmp_atomic_resume_64
<false, true>(int, kmp_atomic_flag_64
<false, true> *);
1698 void __kmp_resume_monitor() {
1699 KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume
);
1702 int gtid
= TCR_4(__kmp_init_gtid
) ? __kmp_get_gtid() : -1;
1703 KF_TRACE(30, ("__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n", gtid
,
1705 KMP_DEBUG_ASSERT(gtid
!= KMP_GTID_MONITOR
);
1707 status
= pthread_mutex_lock(&__kmp_wait_mx
.m_mutex
);
1708 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status
);
1709 #ifdef DEBUG_SUSPEND
1712 __kmp_print_cond(buffer
, &__kmp_wait_cv
.c_cond
);
1713 __kmp_printf("__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid
,
1714 KMP_GTID_MONITOR
, buffer
);
1717 status
= pthread_cond_signal(&__kmp_wait_cv
.c_cond
);
1718 KMP_CHECK_SYSFAIL("pthread_cond_signal", status
);
1719 status
= pthread_mutex_unlock(&__kmp_wait_mx
.m_mutex
);
1720 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status
);
1721 KF_TRACE(30, ("__kmp_resume_monitor: T#%d exiting after signaling wake up"
1723 gtid
, KMP_GTID_MONITOR
));
1725 #endif // KMP_USE_MONITOR
1727 void __kmp_yield() { sched_yield(); }
1729 void __kmp_gtid_set_specific(int gtid
) {
1730 if (__kmp_init_gtid
) {
1732 status
= pthread_setspecific(__kmp_gtid_threadprivate_key
,
1733 (void *)(intptr_t)(gtid
+ 1));
1734 KMP_CHECK_SYSFAIL("pthread_setspecific", status
);
1736 KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
1740 int __kmp_gtid_get_specific() {
1742 if (!__kmp_init_gtid
) {
1743 KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
1744 "KMP_GTID_SHUTDOWN\n"));
1745 return KMP_GTID_SHUTDOWN
;
1747 gtid
= (int)(size_t)pthread_getspecific(__kmp_gtid_threadprivate_key
);
1749 gtid
= KMP_GTID_DNE
;
1753 KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
1754 __kmp_gtid_threadprivate_key
, gtid
));
1758 double __kmp_read_cpu_time(void) {
1762 /*t =*/times(&buffer
);
1764 return (double)(buffer
.tms_utime
+ buffer
.tms_cutime
) /
1765 (double)CLOCKS_PER_SEC
;
1768 int __kmp_read_system_info(struct kmp_sys_info
*info
) {
1770 struct rusage r_usage
;
1772 memset(info
, 0, sizeof(*info
));
1774 status
= getrusage(RUSAGE_SELF
, &r_usage
);
1775 KMP_CHECK_SYSFAIL_ERRNO("getrusage", status
);
1777 // The maximum resident set size utilized (in kilobytes)
1778 info
->maxrss
= r_usage
.ru_maxrss
;
1779 // The number of page faults serviced without any I/O
1780 info
->minflt
= r_usage
.ru_minflt
;
1781 // The number of page faults serviced that required I/O
1782 info
->majflt
= r_usage
.ru_majflt
;
1783 // The number of times a process was "swapped" out of memory
1784 info
->nswap
= r_usage
.ru_nswap
;
1785 // The number of times the file system had to perform input
1786 info
->inblock
= r_usage
.ru_inblock
;
1787 // The number of times the file system had to perform output
1788 info
->oublock
= r_usage
.ru_oublock
;
1789 // The number of times a context switch was voluntarily
1790 info
->nvcsw
= r_usage
.ru_nvcsw
;
1791 // The number of times a context switch was forced
1792 info
->nivcsw
= r_usage
.ru_nivcsw
;
1794 return (status
!= 0);
1797 void __kmp_read_system_time(double *delta
) {
1799 struct timeval tval
;
1800 struct timespec stop
;
1803 status
= gettimeofday(&tval
, NULL
);
1804 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status
);
1805 TIMEVAL_TO_TIMESPEC(&tval
, &stop
);
1806 t_ns
= (double)(TS2NS(stop
) - TS2NS(__kmp_sys_timer_data
.start
));
1807 *delta
= (t_ns
* 1e-9);
1810 void __kmp_clear_system_time(void) {
1811 struct timeval tval
;
1813 status
= gettimeofday(&tval
, NULL
);
1814 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status
);
1815 TIMEVAL_TO_TIMESPEC(&tval
, &__kmp_sys_timer_data
.start
);
1818 static int __kmp_get_xproc(void) {
1824 __kmp_type_convert(sysconf(_SC_NPROCESSORS_CONF
), &(r
));
1826 #elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_OPENBSD || \
1827 KMP_OS_HURD || KMP_OS_SOLARIS
1829 __kmp_type_convert(sysconf(_SC_NPROCESSORS_ONLN
), &(r
));
1833 // Bug C77011 High "OpenMP Threads and number of active cores".
1835 // Find the number of available CPUs.
1837 host_basic_info_data_t info
;
1838 mach_msg_type_number_t num
= HOST_BASIC_INFO_COUNT
;
1839 rc
= host_info(mach_host_self(), HOST_BASIC_INFO
, (host_info_t
)&info
, &num
);
1840 if (rc
== 0 && num
== HOST_BASIC_INFO_COUNT
) {
1841 // Cannot use KA_TRACE() here because this code works before trace support
1843 r
= info
.avail_cpus
;
1845 KMP_WARNING(CantGetNumAvailCPU
);
1846 KMP_INFORM(AssumedNumCPU
);
1851 #error "Unknown or unsupported OS."
1855 return r
> 0 ? r
: 2; /* guess value of 2 if OS told us 0 */
1857 } // __kmp_get_xproc
1859 int __kmp_read_from_file(char const *path
, char const *format
, ...) {
1863 va_start(args
, format
);
1864 FILE *f
= fopen(path
, "rb");
1869 result
= vfscanf(f
, format
, args
);
1876 void __kmp_runtime_initialize(void) {
1878 pthread_mutexattr_t mutex_attr
;
1879 pthread_condattr_t cond_attr
;
1881 if (__kmp_init_runtime
) {
1885 #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
1886 if (!__kmp_cpuinfo
.initialized
) {
1887 __kmp_query_cpuid(&__kmp_cpuinfo
);
1889 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
1891 __kmp_xproc
= __kmp_get_xproc();
1893 #if !KMP_32_BIT_ARCH
1895 // read stack size of calling thread, save it as default for worker threads;
1896 // this should be done before reading environment variables
1897 status
= getrlimit(RLIMIT_STACK
, &rlim
);
1898 if (status
== 0) { // success?
1899 __kmp_stksize
= rlim
.rlim_cur
;
1900 __kmp_check_stksize(&__kmp_stksize
); // check value and adjust if needed
1902 #endif /* KMP_32_BIT_ARCH */
1904 if (sysconf(_SC_THREADS
)) {
1906 /* Query the maximum number of threads */
1907 __kmp_type_convert(sysconf(_SC_THREAD_THREADS_MAX
), &(__kmp_sys_max_nth
));
1909 if (__kmp_sys_max_nth
== -1) {
1910 // VE's pthread supports only up to 64 threads per a VE process.
1911 // So we use that KMP_MAX_NTH (predefined as 64) here.
1912 __kmp_sys_max_nth
= KMP_MAX_NTH
;
1915 if (__kmp_sys_max_nth
== -1) {
1916 /* Unlimited threads for NPTL */
1917 __kmp_sys_max_nth
= INT_MAX
;
1918 } else if (__kmp_sys_max_nth
<= 1) {
1919 /* Can't tell, just use PTHREAD_THREADS_MAX */
1920 __kmp_sys_max_nth
= KMP_MAX_NTH
;
1924 /* Query the minimum stack size */
1925 __kmp_sys_min_stksize
= sysconf(_SC_THREAD_STACK_MIN
);
1926 if (__kmp_sys_min_stksize
<= 1) {
1927 __kmp_sys_min_stksize
= KMP_MIN_STKSIZE
;
1931 /* Set up minimum number of threads to switch to TLS gtid */
1932 __kmp_tls_gtid_min
= KMP_TLS_GTID_MIN
;
1934 status
= pthread_key_create(&__kmp_gtid_threadprivate_key
,
1935 __kmp_internal_end_dest
);
1936 KMP_CHECK_SYSFAIL("pthread_key_create", status
);
1937 status
= pthread_mutexattr_init(&mutex_attr
);
1938 KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status
);
1939 status
= pthread_mutex_init(&__kmp_wait_mx
.m_mutex
, &mutex_attr
);
1940 KMP_CHECK_SYSFAIL("pthread_mutex_init", status
);
1941 status
= pthread_mutexattr_destroy(&mutex_attr
);
1942 KMP_CHECK_SYSFAIL("pthread_mutexattr_destroy", status
);
1943 status
= pthread_condattr_init(&cond_attr
);
1944 KMP_CHECK_SYSFAIL("pthread_condattr_init", status
);
1945 status
= pthread_cond_init(&__kmp_wait_cv
.c_cond
, &cond_attr
);
1946 KMP_CHECK_SYSFAIL("pthread_cond_init", status
);
1947 status
= pthread_condattr_destroy(&cond_attr
);
1948 KMP_CHECK_SYSFAIL("pthread_condattr_destroy", status
);
1950 __kmp_itt_initialize();
1951 #endif /* USE_ITT_BUILD */
1953 __kmp_init_runtime
= TRUE
;
1956 void __kmp_runtime_destroy(void) {
1959 if (!__kmp_init_runtime
) {
1960 return; // Nothing to do.
1964 __kmp_itt_destroy();
1965 #endif /* USE_ITT_BUILD */
1967 status
= pthread_key_delete(__kmp_gtid_threadprivate_key
);
1968 KMP_CHECK_SYSFAIL("pthread_key_delete", status
);
1970 status
= pthread_mutex_destroy(&__kmp_wait_mx
.m_mutex
);
1971 if (status
!= 0 && status
!= EBUSY
) {
1972 KMP_SYSFAIL("pthread_mutex_destroy", status
);
1974 status
= pthread_cond_destroy(&__kmp_wait_cv
.c_cond
);
1975 if (status
!= 0 && status
!= EBUSY
) {
1976 KMP_SYSFAIL("pthread_cond_destroy", status
);
1978 #if KMP_AFFINITY_SUPPORTED
1979 __kmp_affinity_uninitialize();
1982 __kmp_init_runtime
= FALSE
;
1985 /* Put the thread to sleep for a time period */
1986 /* NOTE: not currently used anywhere */
1987 void __kmp_thread_sleep(int millis
) { sleep((millis
+ 500) / 1000); }
1989 /* Calculate the elapsed wall clock time for the user */
1990 void __kmp_elapsed(double *t
) {
1992 #ifdef FIX_SGI_CLOCK
1995 status
= clock_gettime(CLOCK_PROCESS_CPUTIME_ID
, &ts
);
1996 KMP_CHECK_SYSFAIL_ERRNO("clock_gettime", status
);
1998 (double)ts
.tv_nsec
* (1.0 / (double)KMP_NSEC_PER_SEC
) + (double)ts
.tv_sec
;
2002 status
= gettimeofday(&tv
, NULL
);
2003 KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status
);
2005 (double)tv
.tv_usec
* (1.0 / (double)KMP_USEC_PER_SEC
) + (double)tv
.tv_sec
;
2009 /* Calculate the elapsed wall clock tick for the user */
2010 void __kmp_elapsed_tick(double *t
) { *t
= 1 / (double)CLOCKS_PER_SEC
; }
2012 /* Return the current time stamp in nsec */
2013 kmp_uint64
__kmp_now_nsec() {
2015 gettimeofday(&t
, NULL
);
2016 kmp_uint64 nsec
= (kmp_uint64
)KMP_NSEC_PER_SEC
* (kmp_uint64
)t
.tv_sec
+
2017 (kmp_uint64
)1000 * (kmp_uint64
)t
.tv_usec
;
2021 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
2022 /* Measure clock ticks per millisecond */
2023 void __kmp_initialize_system_tick() {
2024 kmp_uint64 now
, nsec2
, diff
;
2025 kmp_uint64 delay
= 1000000; // ~450 usec on most machines.
2026 kmp_uint64 nsec
= __kmp_now_nsec();
2027 kmp_uint64 goal
= __kmp_hardware_timestamp() + delay
;
2028 while ((now
= __kmp_hardware_timestamp()) < goal
)
2030 nsec2
= __kmp_now_nsec();
2031 diff
= nsec2
- nsec
;
2033 double tpus
= 1000.0 * (double)(delay
+ (now
- goal
)) / (double)diff
;
2035 __kmp_ticks_per_msec
= (kmp_uint64
)(tpus
* 1000.0);
2036 __kmp_ticks_per_usec
= (kmp_uint64
)tpus
;
2042 /* Determine whether the given address is mapped into the current address
2045 int __kmp_is_address_mapped(void *addr
) {
2050 #if KMP_OS_LINUX || KMP_OS_HURD
2052 /* On GNUish OSes, read the /proc/<pid>/maps pseudo-file to get all the
2053 address ranges mapped into the address space. */
2055 char *name
= __kmp_str_format("/proc/%d/maps", getpid());
2058 file
= fopen(name
, "r");
2059 KMP_ASSERT(file
!= NULL
);
2063 void *beginning
= NULL
;
2064 void *ending
= NULL
;
2067 rc
= fscanf(file
, "%p-%p %4s %*[^\n]\n", &beginning
, &ending
, perms
);
2071 KMP_ASSERT(rc
== 3 &&
2072 KMP_STRLEN(perms
) == 4); // Make sure all fields are read.
2074 // Ending address is not included in the region, but beginning is.
2075 if ((addr
>= beginning
) && (addr
< ending
)) {
2076 perms
[2] = 0; // 3th and 4th character does not matter.
2077 if (strcmp(perms
, "rw") == 0) {
2078 // Memory we are looking for should be readable and writable.
2087 KMP_INTERNAL_FREE(name
);
2088 #elif KMP_OS_FREEBSD
2091 int mib
[] = {CTL_KERN
, KERN_PROC
, KERN_PROC_VMMAP
, getpid()};
2092 rc
= sysctl(mib
, 4, NULL
, &lstsz
, NULL
, 0);
2095 // We pass from number of vm entry's semantic
2096 // to size of whole entry map list.
2097 lstsz
= lstsz
* 4 / 3;
2098 buf
= reinterpret_cast<char *>(kmpc_malloc(lstsz
));
2099 rc
= sysctl(mib
, 4, buf
, &lstsz
, NULL
, 0);
2106 char *up
= buf
+ lstsz
;
2109 struct kinfo_vmentry
*cur
= reinterpret_cast<struct kinfo_vmentry
*>(lw
);
2110 size_t cursz
= cur
->kve_structsize
;
2113 void *start
= reinterpret_cast<void *>(cur
->kve_start
);
2114 void *end
= reinterpret_cast<void *>(cur
->kve_end
);
2115 // Readable/Writable addresses within current map entry
2116 if ((addr
>= start
) && (addr
< end
)) {
2117 if ((cur
->kve_protection
& KVME_PROT_READ
) != 0 &&
2118 (cur
->kve_protection
& KVME_PROT_WRITE
) != 0) {
2129 /* On OS X*, /proc pseudo filesystem is not available. Try to read memory
2130 using vm interface. */
2134 rc
= vm_read_overwrite(
2135 mach_task_self(), // Task to read memory of.
2136 (vm_address_t
)(addr
), // Address to read from.
2137 1, // Number of bytes to be read.
2138 (vm_address_t
)(&buffer
), // Address of buffer to save read bytes in.
2139 &count
// Address of var to save number of read bytes in.
2142 // Memory successfully read.
2151 mib
[2] = VM_PROC_MAP
;
2153 mib
[4] = sizeof(struct kinfo_vmentry
);
2156 rc
= sysctl(mib
, __arraycount(mib
), NULL
, &size
, NULL
, 0);
2160 size
= size
* 4 / 3;
2161 struct kinfo_vmentry
*kiv
= (struct kinfo_vmentry
*)KMP_INTERNAL_MALLOC(size
);
2164 rc
= sysctl(mib
, __arraycount(mib
), kiv
, &size
, NULL
, 0);
2168 for (size_t i
= 0; i
< size
; i
++) {
2169 if (kiv
[i
].kve_start
>= (uint64_t)addr
&&
2170 kiv
[i
].kve_end
<= (uint64_t)addr
) {
2175 KMP_INTERNAL_FREE(kiv
);
2176 #elif KMP_OS_OPENBSD
2180 mib
[1] = KERN_PROC_VMMAP
;
2185 rc
= sysctl(mib
, 3, NULL
, &size
, NULL
, 0);
2190 struct kinfo_vmentry kiv
= {.kve_start
= 0};
2192 while ((rc
= sysctl(mib
, 3, &kiv
, &size
, NULL
, 0)) == 0) {
2194 if (kiv
.kve_end
== end
)
2197 if (kiv
.kve_start
>= (uint64_t)addr
&& kiv
.kve_end
<= (uint64_t)addr
) {
2203 #elif KMP_OS_DRAGONFLY || KMP_OS_SOLARIS
2205 // FIXME(DragonFly, Solaris): Implement this
2210 #error "Unknown or unsupported OS"
2216 } // __kmp_is_address_mapped
2218 #ifdef USE_LOAD_BALANCE
2220 #if KMP_OS_DARWIN || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
2221 KMP_OS_OPENBSD || KMP_OS_SOLARIS
2223 // The function returns the rounded value of the system load average
2224 // during given time interval which depends on the value of
2225 // __kmp_load_balance_interval variable (default is 60 sec, other values
2226 // may be 300 sec or 900 sec).
2227 // It returns -1 in case of error.
2228 int __kmp_get_load_balance(int max
) {
2232 int res
= getloadavg(averages
, 3);
2234 // Check __kmp_load_balance_interval to determine which of averages to use.
2235 // getloadavg() may return the number of samples less than requested that is
2237 if (__kmp_load_balance_interval
< 180 && (res
>= 1)) {
2238 ret_avg
= (int)averages
[0]; // 1 min
2239 } else if ((__kmp_load_balance_interval
>= 180 &&
2240 __kmp_load_balance_interval
< 600) &&
2242 ret_avg
= (int)averages
[1]; // 5 min
2243 } else if ((__kmp_load_balance_interval
>= 600) && (res
== 3)) {
2244 ret_avg
= (int)averages
[2]; // 15 min
2245 } else { // Error occurred
2254 // The function returns number of running (not sleeping) threads, or -1 in case
2255 // of error. Error could be reported if Linux* OS kernel too old (without
2256 // "/proc" support). Counting running threads stops if max running threads
2258 int __kmp_get_load_balance(int max
) {
2259 static int permanent_error
= 0;
2260 static int glb_running_threads
= 0; // Saved count of the running threads for
2261 // the thread balance algorithm
2262 static double glb_call_time
= 0; /* Thread balance algorithm call time */
2264 int running_threads
= 0; // Number of running threads in the system.
2266 DIR *proc_dir
= NULL
; // Handle of "/proc/" directory.
2267 struct dirent
*proc_entry
= NULL
;
2269 kmp_str_buf_t task_path
; // "/proc/<pid>/task/<tid>/" path.
2270 DIR *task_dir
= NULL
; // Handle of "/proc/<pid>/task/<tid>/" directory.
2271 struct dirent
*task_entry
= NULL
;
2272 int task_path_fixed_len
;
2274 kmp_str_buf_t stat_path
; // "/proc/<pid>/task/<tid>/stat" path.
2276 int stat_path_fixed_len
;
2279 int total_processes
= 0; // Total number of processes in system.
2282 double call_time
= 0.0;
2284 __kmp_str_buf_init(&task_path
);
2285 __kmp_str_buf_init(&stat_path
);
2287 __kmp_elapsed(&call_time
);
2289 if (glb_call_time
&&
2290 (call_time
- glb_call_time
< __kmp_load_balance_interval
)) {
2291 running_threads
= glb_running_threads
;
2295 glb_call_time
= call_time
;
2297 // Do not spend time on scanning "/proc/" if we have a permanent error.
2298 if (permanent_error
) {
2299 running_threads
= -1;
2307 // Open "/proc/" directory.
2308 proc_dir
= opendir("/proc");
2309 if (proc_dir
== NULL
) {
2310 // Cannot open "/prroc/". Probably the kernel does not support it. Return an
2311 // error now and in subsequent calls.
2312 running_threads
= -1;
2313 permanent_error
= 1;
2317 // Initialize fixed part of task_path. This part will not change.
2318 __kmp_str_buf_cat(&task_path
, "/proc/", 6);
2319 task_path_fixed_len
= task_path
.used
; // Remember number of used characters.
2321 proc_entry
= readdir(proc_dir
);
2322 while (proc_entry
!= NULL
) {
2323 // Proc entry is a directory and name starts with a digit. Assume it is a
2324 // process' directory.
2325 if (proc_entry
->d_type
== DT_DIR
&& isdigit(proc_entry
->d_name
[0])) {
2330 // Make sure init process is the very first in "/proc", so we can replace
2331 // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes ==
2332 // 1. We are going to check that total_processes == 1 => d_name == "1" is
2333 // true (where "=>" is implication). Since C++ does not have => operator,
2334 // let us replace it with its equivalent: a => b == ! a || b.
2335 KMP_DEBUG_ASSERT(total_processes
!= 1 ||
2336 strcmp(proc_entry
->d_name
, "1") == 0);
2338 // Construct task_path.
2339 task_path
.used
= task_path_fixed_len
; // Reset task_path to "/proc/".
2340 __kmp_str_buf_cat(&task_path
, proc_entry
->d_name
,
2341 KMP_STRLEN(proc_entry
->d_name
));
2342 __kmp_str_buf_cat(&task_path
, "/task", 5);
2344 task_dir
= opendir(task_path
.str
);
2345 if (task_dir
== NULL
) {
2346 // Process can finish between reading "/proc/" directory entry and
2347 // opening process' "task/" directory. So, in general case we should not
2348 // complain, but have to skip this process and read the next one. But on
2349 // systems with no "task/" support we will spend lot of time to scan
2350 // "/proc/" tree again and again without any benefit. "init" process
2351 // (its pid is 1) should exist always, so, if we cannot open
2352 // "/proc/1/task/" directory, it means "task/" is not supported by
2353 // kernel. Report an error now and in the future.
2354 if (strcmp(proc_entry
->d_name
, "1") == 0) {
2355 running_threads
= -1;
2356 permanent_error
= 1;
2360 // Construct fixed part of stat file path.
2361 __kmp_str_buf_clear(&stat_path
);
2362 __kmp_str_buf_cat(&stat_path
, task_path
.str
, task_path
.used
);
2363 __kmp_str_buf_cat(&stat_path
, "/", 1);
2364 stat_path_fixed_len
= stat_path
.used
;
2366 task_entry
= readdir(task_dir
);
2367 while (task_entry
!= NULL
) {
2368 // It is a directory and name starts with a digit.
2369 if (proc_entry
->d_type
== DT_DIR
&& isdigit(task_entry
->d_name
[0])) {
2371 // Construct complete stat file path. Easiest way would be:
2372 // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str,
2373 // task_entry->d_name );
2374 // but seriae of __kmp_str_buf_cat works a bit faster.
2376 stat_path_fixed_len
; // Reset stat path to its fixed part.
2377 __kmp_str_buf_cat(&stat_path
, task_entry
->d_name
,
2378 KMP_STRLEN(task_entry
->d_name
));
2379 __kmp_str_buf_cat(&stat_path
, "/stat", 5);
2381 // Note: Low-level API (open/read/close) is used. High-level API
2382 // (fopen/fclose) works ~ 30 % slower.
2383 stat_file
= open(stat_path
.str
, O_RDONLY
);
2384 if (stat_file
== -1) {
2385 // We cannot report an error because task (thread) can terminate
2386 // just before reading this file.
2388 /* Content of "stat" file looks like:
2389 24285 (program) S ...
2391 It is a single line (if program name does not include funny
2392 symbols). First number is a thread id, then name of executable
2393 file name in paretheses, then state of the thread. We need just
2396 Good news: Length of program name is 15 characters max. Longer
2397 names are truncated.
2399 Thus, we need rather short buffer: 15 chars for program name +
2400 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
2402 Bad news: Program name may contain special symbols like space,
2403 closing parenthesis, or even new line. This makes parsing
2404 "stat" file not 100 % reliable. In case of fanny program names
2405 parsing may fail (report incorrect thread state).
2407 Parsing "status" file looks more promissing (due to different
2408 file structure and escaping special symbols) but reading and
2409 parsing of "status" file works slower.
2414 len
= read(stat_file
, buffer
, sizeof(buffer
) - 1);
2418 // sscanf( buffer, "%*d (%*s) %c ", & state );
2419 // looks very nice, but searching for a closing parenthesis
2420 // works a bit faster.
2421 char *close_parent
= strstr(buffer
, ") ");
2422 if (close_parent
!= NULL
) {
2423 char state
= *(close_parent
+ 2);
2426 if (running_threads
>= max
) {
2436 task_entry
= readdir(task_dir
);
2442 proc_entry
= readdir(proc_dir
);
2445 // There _might_ be a timing hole where the thread executing this
2446 // code get skipped in the load balance, and running_threads is 0.
2447 // Assert in the debug builds only!!!
2448 KMP_DEBUG_ASSERT(running_threads
> 0);
2449 if (running_threads
<= 0) {
2450 running_threads
= 1;
2453 finish
: // Clean up and exit.
2454 if (proc_dir
!= NULL
) {
2457 __kmp_str_buf_free(&task_path
);
2458 if (task_dir
!= NULL
) {
2461 __kmp_str_buf_free(&stat_path
);
2462 if (stat_file
!= -1) {
2466 glb_running_threads
= running_threads
;
2468 return running_threads
;
2470 } // __kmp_get_load_balance
2472 #endif // KMP_OS_DARWIN
2474 #endif // USE_LOAD_BALANCE
2476 #if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \
2477 ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || \
2478 KMP_ARCH_PPC64 || KMP_ARCH_RISCV64 || KMP_ARCH_LOONGARCH64 || \
2479 KMP_ARCH_ARM || KMP_ARCH_VE || KMP_ARCH_S390X)
2481 // we really only need the case with 1 argument, because CLANG always build
2482 // a struct of pointers to shared variables referenced in the outlined function
2483 int __kmp_invoke_microtask(microtask_t pkfn
, int gtid
, int tid
, int argc
,
2487 void **exit_frame_ptr
2491 *exit_frame_ptr
= OMPT_GET_FRAME_ADDRESS(0);
2496 fprintf(stderr
, "Too many args to microtask: %d!\n", argc
);
2500 (*pkfn
)(>id
, &tid
);
2503 (*pkfn
)(>id
, &tid
, p_argv
[0]);
2506 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1]);
2509 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2]);
2512 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3]);
2515 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4]);
2518 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2522 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2523 p_argv
[5], p_argv
[6]);
2526 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2527 p_argv
[5], p_argv
[6], p_argv
[7]);
2530 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2531 p_argv
[5], p_argv
[6], p_argv
[7], p_argv
[8]);
2534 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2535 p_argv
[5], p_argv
[6], p_argv
[7], p_argv
[8], p_argv
[9]);
2538 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2539 p_argv
[5], p_argv
[6], p_argv
[7], p_argv
[8], p_argv
[9], p_argv
[10]);
2542 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2543 p_argv
[5], p_argv
[6], p_argv
[7], p_argv
[8], p_argv
[9], p_argv
[10],
2547 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2548 p_argv
[5], p_argv
[6], p_argv
[7], p_argv
[8], p_argv
[9], p_argv
[10],
2549 p_argv
[11], p_argv
[12]);
2552 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2553 p_argv
[5], p_argv
[6], p_argv
[7], p_argv
[8], p_argv
[9], p_argv
[10],
2554 p_argv
[11], p_argv
[12], p_argv
[13]);
2557 (*pkfn
)(>id
, &tid
, p_argv
[0], p_argv
[1], p_argv
[2], p_argv
[3], p_argv
[4],
2558 p_argv
[5], p_argv
[6], p_argv
[7], p_argv
[8], p_argv
[9], p_argv
[10],
2559 p_argv
[11], p_argv
[12], p_argv
[13], p_argv
[14]);
2569 // Functions for hidden helper task
2571 // Condition variable for initializing hidden helper team
2572 pthread_cond_t hidden_helper_threads_initz_cond_var
;
2573 pthread_mutex_t hidden_helper_threads_initz_lock
;
2574 volatile int hidden_helper_initz_signaled
= FALSE
;
2576 // Condition variable for deinitializing hidden helper team
2577 pthread_cond_t hidden_helper_threads_deinitz_cond_var
;
2578 pthread_mutex_t hidden_helper_threads_deinitz_lock
;
2579 volatile int hidden_helper_deinitz_signaled
= FALSE
;
2581 // Condition variable for the wrapper function of main thread
2582 pthread_cond_t hidden_helper_main_thread_cond_var
;
2583 pthread_mutex_t hidden_helper_main_thread_lock
;
2584 volatile int hidden_helper_main_thread_signaled
= FALSE
;
2586 // Semaphore for worker threads. We don't use condition variable here in case
2587 // that when multiple signals are sent at the same time, only one thread might
2589 sem_t hidden_helper_task_sem
;
2592 void __kmp_hidden_helper_worker_thread_wait() {
2593 int status
= sem_wait(&hidden_helper_task_sem
);
2594 KMP_CHECK_SYSFAIL("sem_wait", status
);
2597 void __kmp_do_initialize_hidden_helper_threads() {
2598 // Initialize condition variable
2600 pthread_cond_init(&hidden_helper_threads_initz_cond_var
, nullptr);
2601 KMP_CHECK_SYSFAIL("pthread_cond_init", status
);
2603 status
= pthread_cond_init(&hidden_helper_threads_deinitz_cond_var
, nullptr);
2604 KMP_CHECK_SYSFAIL("pthread_cond_init", status
);
2606 status
= pthread_cond_init(&hidden_helper_main_thread_cond_var
, nullptr);
2607 KMP_CHECK_SYSFAIL("pthread_cond_init", status
);
2609 status
= pthread_mutex_init(&hidden_helper_threads_initz_lock
, nullptr);
2610 KMP_CHECK_SYSFAIL("pthread_mutex_init", status
);
2612 status
= pthread_mutex_init(&hidden_helper_threads_deinitz_lock
, nullptr);
2613 KMP_CHECK_SYSFAIL("pthread_mutex_init", status
);
2615 status
= pthread_mutex_init(&hidden_helper_main_thread_lock
, nullptr);
2616 KMP_CHECK_SYSFAIL("pthread_mutex_init", status
);
2618 // Initialize the semaphore
2619 status
= sem_init(&hidden_helper_task_sem
, 0, 0);
2620 KMP_CHECK_SYSFAIL("sem_init", status
);
2622 // Create a new thread to finish initialization
2624 status
= pthread_create(
2626 [](void *) -> void * {
2627 __kmp_hidden_helper_threads_initz_routine();
2631 KMP_CHECK_SYSFAIL("pthread_create", status
);
2634 void __kmp_hidden_helper_threads_initz_wait() {
2635 // Initial thread waits here for the completion of the initialization. The
2636 // condition variable will be notified by main thread of hidden helper teams.
2637 int status
= pthread_mutex_lock(&hidden_helper_threads_initz_lock
);
2638 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status
);
2640 if (!TCR_4(hidden_helper_initz_signaled
)) {
2641 status
= pthread_cond_wait(&hidden_helper_threads_initz_cond_var
,
2642 &hidden_helper_threads_initz_lock
);
2643 KMP_CHECK_SYSFAIL("pthread_cond_wait", status
);
2646 status
= pthread_mutex_unlock(&hidden_helper_threads_initz_lock
);
2647 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status
);
2650 void __kmp_hidden_helper_initz_release() {
2651 // After all initialization, reset __kmp_init_hidden_helper_threads to false.
2652 int status
= pthread_mutex_lock(&hidden_helper_threads_initz_lock
);
2653 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status
);
2655 status
= pthread_cond_signal(&hidden_helper_threads_initz_cond_var
);
2656 KMP_CHECK_SYSFAIL("pthread_cond_wait", status
);
2658 TCW_SYNC_4(hidden_helper_initz_signaled
, TRUE
);
2660 status
= pthread_mutex_unlock(&hidden_helper_threads_initz_lock
);
2661 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status
);
2664 void __kmp_hidden_helper_main_thread_wait() {
2665 // The main thread of hidden helper team will be blocked here. The
2666 // condition variable can only be signal in the destructor of RTL.
2667 int status
= pthread_mutex_lock(&hidden_helper_main_thread_lock
);
2668 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status
);
2670 if (!TCR_4(hidden_helper_main_thread_signaled
)) {
2671 status
= pthread_cond_wait(&hidden_helper_main_thread_cond_var
,
2672 &hidden_helper_main_thread_lock
);
2673 KMP_CHECK_SYSFAIL("pthread_cond_wait", status
);
2676 status
= pthread_mutex_unlock(&hidden_helper_main_thread_lock
);
2677 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status
);
2680 void __kmp_hidden_helper_main_thread_release() {
2681 // The initial thread of OpenMP RTL should call this function to wake up the
2682 // main thread of hidden helper team.
2683 int status
= pthread_mutex_lock(&hidden_helper_main_thread_lock
);
2684 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status
);
2686 status
= pthread_cond_signal(&hidden_helper_main_thread_cond_var
);
2687 KMP_CHECK_SYSFAIL("pthread_cond_signal", status
);
2689 // The hidden helper team is done here
2690 TCW_SYNC_4(hidden_helper_main_thread_signaled
, TRUE
);
2692 status
= pthread_mutex_unlock(&hidden_helper_main_thread_lock
);
2693 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status
);
2696 void __kmp_hidden_helper_worker_thread_signal() {
2697 int status
= sem_post(&hidden_helper_task_sem
);
2698 KMP_CHECK_SYSFAIL("sem_post", status
);
2701 void __kmp_hidden_helper_threads_deinitz_wait() {
2702 // Initial thread waits here for the completion of the deinitialization. The
2703 // condition variable will be notified by main thread of hidden helper teams.
2704 int status
= pthread_mutex_lock(&hidden_helper_threads_deinitz_lock
);
2705 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status
);
2707 if (!TCR_4(hidden_helper_deinitz_signaled
)) {
2708 status
= pthread_cond_wait(&hidden_helper_threads_deinitz_cond_var
,
2709 &hidden_helper_threads_deinitz_lock
);
2710 KMP_CHECK_SYSFAIL("pthread_cond_wait", status
);
2713 status
= pthread_mutex_unlock(&hidden_helper_threads_deinitz_lock
);
2714 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status
);
2717 void __kmp_hidden_helper_threads_deinitz_release() {
2718 int status
= pthread_mutex_lock(&hidden_helper_threads_deinitz_lock
);
2719 KMP_CHECK_SYSFAIL("pthread_mutex_lock", status
);
2721 status
= pthread_cond_signal(&hidden_helper_threads_deinitz_cond_var
);
2722 KMP_CHECK_SYSFAIL("pthread_cond_wait", status
);
2724 TCW_SYNC_4(hidden_helper_deinitz_signaled
, TRUE
);
2726 status
= pthread_mutex_unlock(&hidden_helper_threads_deinitz_lock
);
2727 KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status
);
2729 #else // KMP_OS_LINUX
2730 void __kmp_hidden_helper_worker_thread_wait() {
2731 KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
2734 void __kmp_do_initialize_hidden_helper_threads() {
2735 KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
2738 void __kmp_hidden_helper_threads_initz_wait() {
2739 KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
2742 void __kmp_hidden_helper_initz_release() {
2743 KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
2746 void __kmp_hidden_helper_main_thread_wait() {
2747 KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
2750 void __kmp_hidden_helper_main_thread_release() {
2751 KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
2754 void __kmp_hidden_helper_worker_thread_signal() {
2755 KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
2758 void __kmp_hidden_helper_threads_deinitz_wait() {
2759 KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
2762 void __kmp_hidden_helper_threads_deinitz_release() {
2763 KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
2765 #endif // KMP_OS_LINUX
2767 bool __kmp_detect_shm() {
2768 DIR *dir
= opendir("/dev/shm");
2769 if (dir
) { // /dev/shm exists
2772 } else if (ENOENT
== errno
) { // /dev/shm does not exist
2774 } else { // opendir() failed
2779 bool __kmp_detect_tmp() {
2780 DIR *dir
= opendir("/tmp");
2781 if (dir
) { // /tmp exists
2784 } else if (ENOENT
== errno
) { // /tmp does not exist
2786 } else { // opendir() failed