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