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