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