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