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