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