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