os_posix.cpp revision 13242:fcb4803050e8
1/*
2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "utilities/globalDefinitions.hpp"
26#include "prims/jvm.h"
27#include "semaphore_posix.hpp"
28#include "runtime/frame.inline.hpp"
29#include "runtime/interfaceSupport.hpp"
30#include "runtime/os.hpp"
31#include "utilities/macros.hpp"
32#include "utilities/vmError.hpp"
33
34#include <dlfcn.h>
35#include <pthread.h>
36#include <semaphore.h>
37#include <signal.h>
38#include <sys/resource.h>
39#include <sys/utsname.h>
40#include <time.h>
41#include <unistd.h>
42
43// Todo: provide a os::get_max_process_id() or similar. Number of processes
44// may have been configured, can be read more accurately from proc fs etc.
45#ifndef MAX_PID
46#define MAX_PID INT_MAX
47#endif
48#define IS_VALID_PID(p) (p > 0 && p < MAX_PID)
49
50// Check core dump limit and report possible place where core can be found
51void os::check_dump_limit(char* buffer, size_t bufferSize) {
52  if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) {
53    jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line");
54    VMError::record_coredump_status(buffer, false);
55    return;
56  }
57
58  int n;
59  struct rlimit rlim;
60  bool success;
61
62  char core_path[PATH_MAX];
63  n = get_core_path(core_path, PATH_MAX);
64
65  if (n <= 0) {
66    jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id());
67    success = true;
68#ifdef LINUX
69  } else if (core_path[0] == '"') { // redirect to user process
70    jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path);
71    success = true;
72#endif
73  } else if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
74    jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path);
75    success = true;
76  } else {
77    switch(rlim.rlim_cur) {
78      case RLIM_INFINITY:
79        jio_snprintf(buffer, bufferSize, "%s", core_path);
80        success = true;
81        break;
82      case 0:
83        jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");
84        success = false;
85        break;
86      default:
87        jio_snprintf(buffer, bufferSize, "%s (max size " UINT64_FORMAT " kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", core_path, uint64_t(rlim.rlim_cur) / 1024);
88        success = true;
89        break;
90    }
91  }
92
93  VMError::record_coredump_status(buffer, success);
94}
95
96int os::get_native_stack(address* stack, int frames, int toSkip) {
97  int frame_idx = 0;
98  int num_of_frames;  // number of frames captured
99  frame fr = os::current_frame();
100  while (fr.pc() && frame_idx < frames) {
101    if (toSkip > 0) {
102      toSkip --;
103    } else {
104      stack[frame_idx ++] = fr.pc();
105    }
106    if (fr.fp() == NULL || fr.cb() != NULL ||
107        fr.sender_pc() == NULL || os::is_first_C_frame(&fr)) break;
108
109    if (fr.sender_pc() && !os::is_first_C_frame(&fr)) {
110      fr = os::get_sender_for_C_frame(&fr);
111    } else {
112      break;
113    }
114  }
115  num_of_frames = frame_idx;
116  for (; frame_idx < frames; frame_idx ++) {
117    stack[frame_idx] = NULL;
118  }
119
120  return num_of_frames;
121}
122
123
124bool os::unsetenv(const char* name) {
125  assert(name != NULL, "Null pointer");
126  return (::unsetenv(name) == 0);
127}
128
129int os::get_last_error() {
130  return errno;
131}
132
133bool os::is_debugger_attached() {
134  // not implemented
135  return false;
136}
137
138void os::wait_for_keypress_at_exit(void) {
139  // don't do anything on posix platforms
140  return;
141}
142
143// Multiple threads can race in this code, and can remap over each other with MAP_FIXED,
144// so on posix, unmap the section at the start and at the end of the chunk that we mapped
145// rather than unmapping and remapping the whole chunk to get requested alignment.
146char* os::reserve_memory_aligned(size_t size, size_t alignment) {
147  assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,
148      "Alignment must be a multiple of allocation granularity (page size)");
149  assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");
150
151  size_t extra_size = size + alignment;
152  assert(extra_size >= size, "overflow, size is too large to allow alignment");
153
154  char* extra_base = os::reserve_memory(extra_size, NULL, alignment);
155
156  if (extra_base == NULL) {
157    return NULL;
158  }
159
160  // Do manual alignment
161  char* aligned_base = align_ptr_up(extra_base, alignment);
162
163  // [  |                                       |  ]
164  // ^ extra_base
165  //    ^ extra_base + begin_offset == aligned_base
166  //     extra_base + begin_offset + size       ^
167  //                       extra_base + extra_size ^
168  // |<>| == begin_offset
169  //                              end_offset == |<>|
170  size_t begin_offset = aligned_base - extra_base;
171  size_t end_offset = (extra_base + extra_size) - (aligned_base + size);
172
173  if (begin_offset > 0) {
174      os::release_memory(extra_base, begin_offset);
175  }
176
177  if (end_offset > 0) {
178      os::release_memory(extra_base + begin_offset + size, end_offset);
179  }
180
181  return aligned_base;
182}
183
184int os::log_vsnprintf(char* buf, size_t len, const char* fmt, va_list args) {
185    return vsnprintf(buf, len, fmt, args);
186}
187
188int os::get_fileno(FILE* fp) {
189  return NOT_AIX(::)fileno(fp);
190}
191
192struct tm* os::gmtime_pd(const time_t* clock, struct tm*  res) {
193  return gmtime_r(clock, res);
194}
195
196void os::Posix::print_load_average(outputStream* st) {
197  st->print("load average:");
198  double loadavg[3];
199  os::loadavg(loadavg, 3);
200  st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
201  st->cr();
202}
203
204void os::Posix::print_rlimit_info(outputStream* st) {
205  st->print("rlimit:");
206  struct rlimit rlim;
207
208  st->print(" STACK ");
209  getrlimit(RLIMIT_STACK, &rlim);
210  if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
211  else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
212
213  st->print(", CORE ");
214  getrlimit(RLIMIT_CORE, &rlim);
215  if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
216  else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
217
218  // Isn't there on solaris
219#if defined(AIX)
220  st->print(", NPROC ");
221  st->print("%d", sysconf(_SC_CHILD_MAX));
222#elif !defined(SOLARIS)
223  st->print(", NPROC ");
224  getrlimit(RLIMIT_NPROC, &rlim);
225  if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
226  else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur));
227#endif
228
229  st->print(", NOFILE ");
230  getrlimit(RLIMIT_NOFILE, &rlim);
231  if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
232  else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur));
233
234  st->print(", AS ");
235  getrlimit(RLIMIT_AS, &rlim);
236  if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
237  else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
238
239  st->print(", DATA ");
240  getrlimit(RLIMIT_DATA, &rlim);
241  if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
242  else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
243
244  st->print(", FSIZE ");
245  getrlimit(RLIMIT_FSIZE, &rlim);
246  if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
247  else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
248
249  st->cr();
250}
251
252void os::Posix::print_uname_info(outputStream* st) {
253  // kernel
254  st->print("uname:");
255  struct utsname name;
256  uname(&name);
257  st->print("%s ", name.sysname);
258#ifdef ASSERT
259  st->print("%s ", name.nodename);
260#endif
261  st->print("%s ", name.release);
262  st->print("%s ", name.version);
263  st->print("%s", name.machine);
264  st->cr();
265}
266
267bool os::get_host_name(char* buf, size_t buflen) {
268  struct utsname name;
269  uname(&name);
270  jio_snprintf(buf, buflen, "%s", name.nodename);
271  return true;
272}
273
274bool os::has_allocatable_memory_limit(julong* limit) {
275  struct rlimit rlim;
276  int getrlimit_res = getrlimit(RLIMIT_AS, &rlim);
277  // if there was an error when calling getrlimit, assume that there is no limitation
278  // on virtual memory.
279  bool result;
280  if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) {
281    result = false;
282  } else {
283    *limit = (julong)rlim.rlim_cur;
284    result = true;
285  }
286#ifdef _LP64
287  return result;
288#else
289  // arbitrary virtual space limit for 32 bit Unices found by testing. If
290  // getrlimit above returned a limit, bound it with this limit. Otherwise
291  // directly use it.
292  const julong max_virtual_limit = (julong)3800*M;
293  if (result) {
294    *limit = MIN2(*limit, max_virtual_limit);
295  } else {
296    *limit = max_virtual_limit;
297  }
298
299  // bound by actually allocatable memory. The algorithm uses two bounds, an
300  // upper and a lower limit. The upper limit is the current highest amount of
301  // memory that could not be allocated, the lower limit is the current highest
302  // amount of memory that could be allocated.
303  // The algorithm iteratively refines the result by halving the difference
304  // between these limits, updating either the upper limit (if that value could
305  // not be allocated) or the lower limit (if the that value could be allocated)
306  // until the difference between these limits is "small".
307
308  // the minimum amount of memory we care about allocating.
309  const julong min_allocation_size = M;
310
311  julong upper_limit = *limit;
312
313  // first check a few trivial cases
314  if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {
315    *limit = upper_limit;
316  } else if (!is_allocatable(min_allocation_size)) {
317    // we found that not even min_allocation_size is allocatable. Return it
318    // anyway. There is no point to search for a better value any more.
319    *limit = min_allocation_size;
320  } else {
321    // perform the binary search.
322    julong lower_limit = min_allocation_size;
323    while ((upper_limit - lower_limit) > min_allocation_size) {
324      julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;
325      temp_limit = align_size_down_(temp_limit, min_allocation_size);
326      if (is_allocatable(temp_limit)) {
327        lower_limit = temp_limit;
328      } else {
329        upper_limit = temp_limit;
330      }
331    }
332    *limit = lower_limit;
333  }
334  return true;
335#endif
336}
337
338const char* os::get_current_directory(char *buf, size_t buflen) {
339  return getcwd(buf, buflen);
340}
341
342FILE* os::open(int fd, const char* mode) {
343  return ::fdopen(fd, mode);
344}
345
346void os::flockfile(FILE* fp) {
347  ::flockfile(fp);
348}
349
350void os::funlockfile(FILE* fp) {
351  ::funlockfile(fp);
352}
353
354// Builds a platform dependent Agent_OnLoad_<lib_name> function name
355// which is used to find statically linked in agents.
356// Parameters:
357//            sym_name: Symbol in library we are looking for
358//            lib_name: Name of library to look in, NULL for shared libs.
359//            is_absolute_path == true if lib_name is absolute path to agent
360//                                     such as "/a/b/libL.so"
361//            == false if only the base name of the library is passed in
362//               such as "L"
363char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
364                                    bool is_absolute_path) {
365  char *agent_entry_name;
366  size_t len;
367  size_t name_len;
368  size_t prefix_len = strlen(JNI_LIB_PREFIX);
369  size_t suffix_len = strlen(JNI_LIB_SUFFIX);
370  const char *start;
371
372  if (lib_name != NULL) {
373    name_len = strlen(lib_name);
374    if (is_absolute_path) {
375      // Need to strip path, prefix and suffix
376      if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {
377        lib_name = ++start;
378      }
379      if (strlen(lib_name) <= (prefix_len + suffix_len)) {
380        return NULL;
381      }
382      lib_name += prefix_len;
383      name_len = strlen(lib_name) - suffix_len;
384    }
385  }
386  len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
387  agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
388  if (agent_entry_name == NULL) {
389    return NULL;
390  }
391  strcpy(agent_entry_name, sym_name);
392  if (lib_name != NULL) {
393    strcat(agent_entry_name, "_");
394    strncat(agent_entry_name, lib_name, name_len);
395  }
396  return agent_entry_name;
397}
398
399int os::sleep(Thread* thread, jlong millis, bool interruptible) {
400  assert(thread == Thread::current(),  "thread consistency check");
401
402  ParkEvent * const slp = thread->_SleepEvent ;
403  slp->reset() ;
404  OrderAccess::fence() ;
405
406  if (interruptible) {
407    jlong prevtime = javaTimeNanos();
408
409    for (;;) {
410      if (os::is_interrupted(thread, true)) {
411        return OS_INTRPT;
412      }
413
414      jlong newtime = javaTimeNanos();
415
416      if (newtime - prevtime < 0) {
417        // time moving backwards, should only happen if no monotonic clock
418        // not a guarantee() because JVM should not abort on kernel/glibc bugs
419        assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected in os::sleep(interruptible)");
420      } else {
421        millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
422      }
423
424      if (millis <= 0) {
425        return OS_OK;
426      }
427
428      prevtime = newtime;
429
430      {
431        assert(thread->is_Java_thread(), "sanity check");
432        JavaThread *jt = (JavaThread *) thread;
433        ThreadBlockInVM tbivm(jt);
434        OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
435
436        jt->set_suspend_equivalent();
437        // cleared by handle_special_suspend_equivalent_condition() or
438        // java_suspend_self() via check_and_wait_while_suspended()
439
440        slp->park(millis);
441
442        // were we externally suspended while we were waiting?
443        jt->check_and_wait_while_suspended();
444      }
445    }
446  } else {
447    OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
448    jlong prevtime = javaTimeNanos();
449
450    for (;;) {
451      // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
452      // the 1st iteration ...
453      jlong newtime = javaTimeNanos();
454
455      if (newtime - prevtime < 0) {
456        // time moving backwards, should only happen if no monotonic clock
457        // not a guarantee() because JVM should not abort on kernel/glibc bugs
458        assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected on os::sleep(!interruptible)");
459      } else {
460        millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
461      }
462
463      if (millis <= 0) break ;
464
465      prevtime = newtime;
466      slp->park(millis);
467    }
468    return OS_OK ;
469  }
470}
471
472////////////////////////////////////////////////////////////////////////////////
473// interrupt support
474
475void os::interrupt(Thread* thread) {
476  assert(Thread::current() == thread || Threads_lock->owned_by_self(),
477    "possibility of dangling Thread pointer");
478
479  OSThread* osthread = thread->osthread();
480
481  if (!osthread->interrupted()) {
482    osthread->set_interrupted(true);
483    // More than one thread can get here with the same value of osthread,
484    // resulting in multiple notifications.  We do, however, want the store
485    // to interrupted() to be visible to other threads before we execute unpark().
486    OrderAccess::fence();
487    ParkEvent * const slp = thread->_SleepEvent ;
488    if (slp != NULL) slp->unpark() ;
489  }
490
491  // For JSR166. Unpark even if interrupt status already was set
492  if (thread->is_Java_thread())
493    ((JavaThread*)thread)->parker()->unpark();
494
495  ParkEvent * ev = thread->_ParkEvent ;
496  if (ev != NULL) ev->unpark() ;
497
498}
499
500bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
501  assert(Thread::current() == thread || Threads_lock->owned_by_self(),
502    "possibility of dangling Thread pointer");
503
504  OSThread* osthread = thread->osthread();
505
506  bool interrupted = osthread->interrupted();
507
508  // NOTE that since there is no "lock" around the interrupt and
509  // is_interrupted operations, there is the possibility that the
510  // interrupted flag (in osThread) will be "false" but that the
511  // low-level events will be in the signaled state. This is
512  // intentional. The effect of this is that Object.wait() and
513  // LockSupport.park() will appear to have a spurious wakeup, which
514  // is allowed and not harmful, and the possibility is so rare that
515  // it is not worth the added complexity to add yet another lock.
516  // For the sleep event an explicit reset is performed on entry
517  // to os::sleep, so there is no early return. It has also been
518  // recommended not to put the interrupted flag into the "event"
519  // structure because it hides the issue.
520  if (interrupted && clear_interrupted) {
521    osthread->set_interrupted(false);
522    // consider thread->_SleepEvent->reset() ... optional optimization
523  }
524
525  return interrupted;
526}
527
528
529
530static const struct {
531  int sig; const char* name;
532}
533 g_signal_info[] =
534  {
535  {  SIGABRT,     "SIGABRT" },
536#ifdef SIGAIO
537  {  SIGAIO,      "SIGAIO" },
538#endif
539  {  SIGALRM,     "SIGALRM" },
540#ifdef SIGALRM1
541  {  SIGALRM1,    "SIGALRM1" },
542#endif
543  {  SIGBUS,      "SIGBUS" },
544#ifdef SIGCANCEL
545  {  SIGCANCEL,   "SIGCANCEL" },
546#endif
547  {  SIGCHLD,     "SIGCHLD" },
548#ifdef SIGCLD
549  {  SIGCLD,      "SIGCLD" },
550#endif
551  {  SIGCONT,     "SIGCONT" },
552#ifdef SIGCPUFAIL
553  {  SIGCPUFAIL,  "SIGCPUFAIL" },
554#endif
555#ifdef SIGDANGER
556  {  SIGDANGER,   "SIGDANGER" },
557#endif
558#ifdef SIGDIL
559  {  SIGDIL,      "SIGDIL" },
560#endif
561#ifdef SIGEMT
562  {  SIGEMT,      "SIGEMT" },
563#endif
564  {  SIGFPE,      "SIGFPE" },
565#ifdef SIGFREEZE
566  {  SIGFREEZE,   "SIGFREEZE" },
567#endif
568#ifdef SIGGFAULT
569  {  SIGGFAULT,   "SIGGFAULT" },
570#endif
571#ifdef SIGGRANT
572  {  SIGGRANT,    "SIGGRANT" },
573#endif
574  {  SIGHUP,      "SIGHUP" },
575  {  SIGILL,      "SIGILL" },
576  {  SIGINT,      "SIGINT" },
577#ifdef SIGIO
578  {  SIGIO,       "SIGIO" },
579#endif
580#ifdef SIGIOINT
581  {  SIGIOINT,    "SIGIOINT" },
582#endif
583#ifdef SIGIOT
584// SIGIOT is there for BSD compatibility, but on most Unices just a
585// synonym for SIGABRT. The result should be "SIGABRT", not
586// "SIGIOT".
587#if (SIGIOT != SIGABRT )
588  {  SIGIOT,      "SIGIOT" },
589#endif
590#endif
591#ifdef SIGKAP
592  {  SIGKAP,      "SIGKAP" },
593#endif
594  {  SIGKILL,     "SIGKILL" },
595#ifdef SIGLOST
596  {  SIGLOST,     "SIGLOST" },
597#endif
598#ifdef SIGLWP
599  {  SIGLWP,      "SIGLWP" },
600#endif
601#ifdef SIGLWPTIMER
602  {  SIGLWPTIMER, "SIGLWPTIMER" },
603#endif
604#ifdef SIGMIGRATE
605  {  SIGMIGRATE,  "SIGMIGRATE" },
606#endif
607#ifdef SIGMSG
608  {  SIGMSG,      "SIGMSG" },
609#endif
610  {  SIGPIPE,     "SIGPIPE" },
611#ifdef SIGPOLL
612  {  SIGPOLL,     "SIGPOLL" },
613#endif
614#ifdef SIGPRE
615  {  SIGPRE,      "SIGPRE" },
616#endif
617  {  SIGPROF,     "SIGPROF" },
618#ifdef SIGPTY
619  {  SIGPTY,      "SIGPTY" },
620#endif
621#ifdef SIGPWR
622  {  SIGPWR,      "SIGPWR" },
623#endif
624  {  SIGQUIT,     "SIGQUIT" },
625#ifdef SIGRECONFIG
626  {  SIGRECONFIG, "SIGRECONFIG" },
627#endif
628#ifdef SIGRECOVERY
629  {  SIGRECOVERY, "SIGRECOVERY" },
630#endif
631#ifdef SIGRESERVE
632  {  SIGRESERVE,  "SIGRESERVE" },
633#endif
634#ifdef SIGRETRACT
635  {  SIGRETRACT,  "SIGRETRACT" },
636#endif
637#ifdef SIGSAK
638  {  SIGSAK,      "SIGSAK" },
639#endif
640  {  SIGSEGV,     "SIGSEGV" },
641#ifdef SIGSOUND
642  {  SIGSOUND,    "SIGSOUND" },
643#endif
644#ifdef SIGSTKFLT
645  {  SIGSTKFLT,    "SIGSTKFLT" },
646#endif
647  {  SIGSTOP,     "SIGSTOP" },
648  {  SIGSYS,      "SIGSYS" },
649#ifdef SIGSYSERROR
650  {  SIGSYSERROR, "SIGSYSERROR" },
651#endif
652#ifdef SIGTALRM
653  {  SIGTALRM,    "SIGTALRM" },
654#endif
655  {  SIGTERM,     "SIGTERM" },
656#ifdef SIGTHAW
657  {  SIGTHAW,     "SIGTHAW" },
658#endif
659  {  SIGTRAP,     "SIGTRAP" },
660#ifdef SIGTSTP
661  {  SIGTSTP,     "SIGTSTP" },
662#endif
663  {  SIGTTIN,     "SIGTTIN" },
664  {  SIGTTOU,     "SIGTTOU" },
665#ifdef SIGURG
666  {  SIGURG,      "SIGURG" },
667#endif
668  {  SIGUSR1,     "SIGUSR1" },
669  {  SIGUSR2,     "SIGUSR2" },
670#ifdef SIGVIRT
671  {  SIGVIRT,     "SIGVIRT" },
672#endif
673  {  SIGVTALRM,   "SIGVTALRM" },
674#ifdef SIGWAITING
675  {  SIGWAITING,  "SIGWAITING" },
676#endif
677#ifdef SIGWINCH
678  {  SIGWINCH,    "SIGWINCH" },
679#endif
680#ifdef SIGWINDOW
681  {  SIGWINDOW,   "SIGWINDOW" },
682#endif
683  {  SIGXCPU,     "SIGXCPU" },
684  {  SIGXFSZ,     "SIGXFSZ" },
685#ifdef SIGXRES
686  {  SIGXRES,     "SIGXRES" },
687#endif
688  { -1, NULL }
689};
690
691// Returned string is a constant. For unknown signals "UNKNOWN" is returned.
692const char* os::Posix::get_signal_name(int sig, char* out, size_t outlen) {
693
694  const char* ret = NULL;
695
696#ifdef SIGRTMIN
697  if (sig >= SIGRTMIN && sig <= SIGRTMAX) {
698    if (sig == SIGRTMIN) {
699      ret = "SIGRTMIN";
700    } else if (sig == SIGRTMAX) {
701      ret = "SIGRTMAX";
702    } else {
703      jio_snprintf(out, outlen, "SIGRTMIN+%d", sig - SIGRTMIN);
704      return out;
705    }
706  }
707#endif
708
709  if (sig > 0) {
710    for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) {
711      if (g_signal_info[idx].sig == sig) {
712        ret = g_signal_info[idx].name;
713        break;
714      }
715    }
716  }
717
718  if (!ret) {
719    if (!is_valid_signal(sig)) {
720      ret = "INVALID";
721    } else {
722      ret = "UNKNOWN";
723    }
724  }
725
726  if (out && outlen > 0) {
727    strncpy(out, ret, outlen);
728    out[outlen - 1] = '\0';
729  }
730  return out;
731}
732
733int os::Posix::get_signal_number(const char* signal_name) {
734  char tmp[30];
735  const char* s = signal_name;
736  if (s[0] != 'S' || s[1] != 'I' || s[2] != 'G') {
737    jio_snprintf(tmp, sizeof(tmp), "SIG%s", signal_name);
738    s = tmp;
739  }
740  for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) {
741    if (strcmp(g_signal_info[idx].name, s) == 0) {
742      return g_signal_info[idx].sig;
743    }
744  }
745  return -1;
746}
747
748int os::get_signal_number(const char* signal_name) {
749  return os::Posix::get_signal_number(signal_name);
750}
751
752// Returns true if signal number is valid.
753bool os::Posix::is_valid_signal(int sig) {
754  // MacOS not really POSIX compliant: sigaddset does not return
755  // an error for invalid signal numbers. However, MacOS does not
756  // support real time signals and simply seems to have just 33
757  // signals with no holes in the signal range.
758#ifdef __APPLE__
759  return sig >= 1 && sig < NSIG;
760#else
761  // Use sigaddset to check for signal validity.
762  sigset_t set;
763  sigemptyset(&set);
764  if (sigaddset(&set, sig) == -1 && errno == EINVAL) {
765    return false;
766  }
767  return true;
768#endif
769}
770
771// Returns:
772// NULL for an invalid signal number
773// "SIG<num>" for a valid but unknown signal number
774// signal name otherwise.
775const char* os::exception_name(int sig, char* buf, size_t size) {
776  if (!os::Posix::is_valid_signal(sig)) {
777    return NULL;
778  }
779  const char* const name = os::Posix::get_signal_name(sig, buf, size);
780  if (strcmp(name, "UNKNOWN") == 0) {
781    jio_snprintf(buf, size, "SIG%d", sig);
782  }
783  return buf;
784}
785
786#define NUM_IMPORTANT_SIGS 32
787// Returns one-line short description of a signal set in a user provided buffer.
788const char* os::Posix::describe_signal_set_short(const sigset_t* set, char* buffer, size_t buf_size) {
789  assert(buf_size == (NUM_IMPORTANT_SIGS + 1), "wrong buffer size");
790  // Note: for shortness, just print out the first 32. That should
791  // cover most of the useful ones, apart from realtime signals.
792  for (int sig = 1; sig <= NUM_IMPORTANT_SIGS; sig++) {
793    const int rc = sigismember(set, sig);
794    if (rc == -1 && errno == EINVAL) {
795      buffer[sig-1] = '?';
796    } else {
797      buffer[sig-1] = rc == 0 ? '0' : '1';
798    }
799  }
800  buffer[NUM_IMPORTANT_SIGS] = 0;
801  return buffer;
802}
803
804// Prints one-line description of a signal set.
805void os::Posix::print_signal_set_short(outputStream* st, const sigset_t* set) {
806  char buf[NUM_IMPORTANT_SIGS + 1];
807  os::Posix::describe_signal_set_short(set, buf, sizeof(buf));
808  st->print("%s", buf);
809}
810
811// Writes one-line description of a combination of sigaction.sa_flags into a user
812// provided buffer. Returns that buffer.
813const char* os::Posix::describe_sa_flags(int flags, char* buffer, size_t size) {
814  char* p = buffer;
815  size_t remaining = size;
816  bool first = true;
817  int idx = 0;
818
819  assert(buffer, "invalid argument");
820
821  if (size == 0) {
822    return buffer;
823  }
824
825  strncpy(buffer, "none", size);
826
827  const struct {
828    // NB: i is an unsigned int here because SA_RESETHAND is on some
829    // systems 0x80000000, which is implicitly unsigned.  Assignining
830    // it to an int field would be an overflow in unsigned-to-signed
831    // conversion.
832    unsigned int i;
833    const char* s;
834  } flaginfo [] = {
835    { SA_NOCLDSTOP, "SA_NOCLDSTOP" },
836    { SA_ONSTACK,   "SA_ONSTACK"   },
837    { SA_RESETHAND, "SA_RESETHAND" },
838    { SA_RESTART,   "SA_RESTART"   },
839    { SA_SIGINFO,   "SA_SIGINFO"   },
840    { SA_NOCLDWAIT, "SA_NOCLDWAIT" },
841    { SA_NODEFER,   "SA_NODEFER"   },
842#ifdef AIX
843    { SA_ONSTACK,   "SA_ONSTACK"   },
844    { SA_OLDSTYLE,  "SA_OLDSTYLE"  },
845#endif
846    { 0, NULL }
847  };
848
849  for (idx = 0; flaginfo[idx].s && remaining > 1; idx++) {
850    if (flags & flaginfo[idx].i) {
851      if (first) {
852        jio_snprintf(p, remaining, "%s", flaginfo[idx].s);
853        first = false;
854      } else {
855        jio_snprintf(p, remaining, "|%s", flaginfo[idx].s);
856      }
857      const size_t len = strlen(p);
858      p += len;
859      remaining -= len;
860    }
861  }
862
863  buffer[size - 1] = '\0';
864
865  return buffer;
866}
867
868// Prints one-line description of a combination of sigaction.sa_flags.
869void os::Posix::print_sa_flags(outputStream* st, int flags) {
870  char buffer[0x100];
871  os::Posix::describe_sa_flags(flags, buffer, sizeof(buffer));
872  st->print("%s", buffer);
873}
874
875// Helper function for os::Posix::print_siginfo_...():
876// return a textual description for signal code.
877struct enum_sigcode_desc_t {
878  const char* s_name;
879  const char* s_desc;
880};
881
882static bool get_signal_code_description(const siginfo_t* si, enum_sigcode_desc_t* out) {
883
884  const struct {
885    int sig; int code; const char* s_code; const char* s_desc;
886  } t1 [] = {
887    { SIGILL,  ILL_ILLOPC,   "ILL_ILLOPC",   "Illegal opcode." },
888    { SIGILL,  ILL_ILLOPN,   "ILL_ILLOPN",   "Illegal operand." },
889    { SIGILL,  ILL_ILLADR,   "ILL_ILLADR",   "Illegal addressing mode." },
890    { SIGILL,  ILL_ILLTRP,   "ILL_ILLTRP",   "Illegal trap." },
891    { SIGILL,  ILL_PRVOPC,   "ILL_PRVOPC",   "Privileged opcode." },
892    { SIGILL,  ILL_PRVREG,   "ILL_PRVREG",   "Privileged register." },
893    { SIGILL,  ILL_COPROC,   "ILL_COPROC",   "Coprocessor error." },
894    { SIGILL,  ILL_BADSTK,   "ILL_BADSTK",   "Internal stack error." },
895#if defined(IA64) && defined(LINUX)
896    { SIGILL,  ILL_BADIADDR, "ILL_BADIADDR", "Unimplemented instruction address" },
897    { SIGILL,  ILL_BREAK,    "ILL_BREAK",    "Application Break instruction" },
898#endif
899    { SIGFPE,  FPE_INTDIV,   "FPE_INTDIV",   "Integer divide by zero." },
900    { SIGFPE,  FPE_INTOVF,   "FPE_INTOVF",   "Integer overflow." },
901    { SIGFPE,  FPE_FLTDIV,   "FPE_FLTDIV",   "Floating-point divide by zero." },
902    { SIGFPE,  FPE_FLTOVF,   "FPE_FLTOVF",   "Floating-point overflow." },
903    { SIGFPE,  FPE_FLTUND,   "FPE_FLTUND",   "Floating-point underflow." },
904    { SIGFPE,  FPE_FLTRES,   "FPE_FLTRES",   "Floating-point inexact result." },
905    { SIGFPE,  FPE_FLTINV,   "FPE_FLTINV",   "Invalid floating-point operation." },
906    { SIGFPE,  FPE_FLTSUB,   "FPE_FLTSUB",   "Subscript out of range." },
907    { SIGSEGV, SEGV_MAPERR,  "SEGV_MAPERR",  "Address not mapped to object." },
908    { SIGSEGV, SEGV_ACCERR,  "SEGV_ACCERR",  "Invalid permissions for mapped object." },
909#ifdef AIX
910    // no explanation found what keyerr would be
911    { SIGSEGV, SEGV_KEYERR,  "SEGV_KEYERR",  "key error" },
912#endif
913#if defined(IA64) && !defined(AIX)
914    { SIGSEGV, SEGV_PSTKOVF, "SEGV_PSTKOVF", "Paragraph stack overflow" },
915#endif
916#if defined(__sparc) && defined(SOLARIS)
917// define Solaris Sparc M7 ADI SEGV signals
918#if !defined(SEGV_ACCADI)
919#define SEGV_ACCADI 3
920#endif
921    { SIGSEGV, SEGV_ACCADI,  "SEGV_ACCADI",  "ADI not enabled for mapped object." },
922#if !defined(SEGV_ACCDERR)
923#define SEGV_ACCDERR 4
924#endif
925    { SIGSEGV, SEGV_ACCDERR, "SEGV_ACCDERR", "ADI disrupting exception." },
926#if !defined(SEGV_ACCPERR)
927#define SEGV_ACCPERR 5
928#endif
929    { SIGSEGV, SEGV_ACCPERR, "SEGV_ACCPERR", "ADI precise exception." },
930#endif // defined(__sparc) && defined(SOLARIS)
931    { SIGBUS,  BUS_ADRALN,   "BUS_ADRALN",   "Invalid address alignment." },
932    { SIGBUS,  BUS_ADRERR,   "BUS_ADRERR",   "Nonexistent physical address." },
933    { SIGBUS,  BUS_OBJERR,   "BUS_OBJERR",   "Object-specific hardware error." },
934    { SIGTRAP, TRAP_BRKPT,   "TRAP_BRKPT",   "Process breakpoint." },
935    { SIGTRAP, TRAP_TRACE,   "TRAP_TRACE",   "Process trace trap." },
936    { SIGCHLD, CLD_EXITED,   "CLD_EXITED",   "Child has exited." },
937    { SIGCHLD, CLD_KILLED,   "CLD_KILLED",   "Child has terminated abnormally and did not create a core file." },
938    { SIGCHLD, CLD_DUMPED,   "CLD_DUMPED",   "Child has terminated abnormally and created a core file." },
939    { SIGCHLD, CLD_TRAPPED,  "CLD_TRAPPED",  "Traced child has trapped." },
940    { SIGCHLD, CLD_STOPPED,  "CLD_STOPPED",  "Child has stopped." },
941    { SIGCHLD, CLD_CONTINUED,"CLD_CONTINUED","Stopped child has continued." },
942#ifdef SIGPOLL
943    { SIGPOLL, POLL_OUT,     "POLL_OUT",     "Output buffers available." },
944    { SIGPOLL, POLL_MSG,     "POLL_MSG",     "Input message available." },
945    { SIGPOLL, POLL_ERR,     "POLL_ERR",     "I/O error." },
946    { SIGPOLL, POLL_PRI,     "POLL_PRI",     "High priority input available." },
947    { SIGPOLL, POLL_HUP,     "POLL_HUP",     "Device disconnected. [Option End]" },
948#endif
949    { -1, -1, NULL, NULL }
950  };
951
952  // Codes valid in any signal context.
953  const struct {
954    int code; const char* s_code; const char* s_desc;
955  } t2 [] = {
956    { SI_USER,      "SI_USER",     "Signal sent by kill()." },
957    { SI_QUEUE,     "SI_QUEUE",    "Signal sent by the sigqueue()." },
958    { SI_TIMER,     "SI_TIMER",    "Signal generated by expiration of a timer set by timer_settime()." },
959    { SI_ASYNCIO,   "SI_ASYNCIO",  "Signal generated by completion of an asynchronous I/O request." },
960    { SI_MESGQ,     "SI_MESGQ",    "Signal generated by arrival of a message on an empty message queue." },
961    // Linux specific
962#ifdef SI_TKILL
963    { SI_TKILL,     "SI_TKILL",    "Signal sent by tkill (pthread_kill)" },
964#endif
965#ifdef SI_DETHREAD
966    { SI_DETHREAD,  "SI_DETHREAD", "Signal sent by execve() killing subsidiary threads" },
967#endif
968#ifdef SI_KERNEL
969    { SI_KERNEL,    "SI_KERNEL",   "Signal sent by kernel." },
970#endif
971#ifdef SI_SIGIO
972    { SI_SIGIO,     "SI_SIGIO",    "Signal sent by queued SIGIO" },
973#endif
974
975#ifdef AIX
976    { SI_UNDEFINED, "SI_UNDEFINED","siginfo contains partial information" },
977    { SI_EMPTY,     "SI_EMPTY",    "siginfo contains no useful information" },
978#endif
979
980#ifdef __sun
981    { SI_NOINFO,    "SI_NOINFO",   "No signal information" },
982    { SI_RCTL,      "SI_RCTL",     "kernel generated signal via rctl action" },
983    { SI_LWP,       "SI_LWP",      "Signal sent via lwp_kill" },
984#endif
985
986    { -1, NULL, NULL }
987  };
988
989  const char* s_code = NULL;
990  const char* s_desc = NULL;
991
992  for (int i = 0; t1[i].sig != -1; i ++) {
993    if (t1[i].sig == si->si_signo && t1[i].code == si->si_code) {
994      s_code = t1[i].s_code;
995      s_desc = t1[i].s_desc;
996      break;
997    }
998  }
999
1000  if (s_code == NULL) {
1001    for (int i = 0; t2[i].s_code != NULL; i ++) {
1002      if (t2[i].code == si->si_code) {
1003        s_code = t2[i].s_code;
1004        s_desc = t2[i].s_desc;
1005      }
1006    }
1007  }
1008
1009  if (s_code == NULL) {
1010    out->s_name = "unknown";
1011    out->s_desc = "unknown";
1012    return false;
1013  }
1014
1015  out->s_name = s_code;
1016  out->s_desc = s_desc;
1017
1018  return true;
1019}
1020
1021void os::print_siginfo(outputStream* os, const void* si0) {
1022
1023  const siginfo_t* const si = (const siginfo_t*) si0;
1024
1025  char buf[20];
1026  os->print("siginfo:");
1027
1028  if (!si) {
1029    os->print(" <null>");
1030    return;
1031  }
1032
1033  const int sig = si->si_signo;
1034
1035  os->print(" si_signo: %d (%s)", sig, os::Posix::get_signal_name(sig, buf, sizeof(buf)));
1036
1037  enum_sigcode_desc_t ed;
1038  get_signal_code_description(si, &ed);
1039  os->print(", si_code: %d (%s)", si->si_code, ed.s_name);
1040
1041  if (si->si_errno) {
1042    os->print(", si_errno: %d", si->si_errno);
1043  }
1044
1045  // Output additional information depending on the signal code.
1046
1047  // Note: Many implementations lump si_addr, si_pid, si_uid etc. together as unions,
1048  // so it depends on the context which member to use. For synchronous error signals,
1049  // we print si_addr, unless the signal was sent by another process or thread, in
1050  // which case we print out pid or tid of the sender.
1051  if (si->si_code == SI_USER || si->si_code == SI_QUEUE) {
1052    const pid_t pid = si->si_pid;
1053    os->print(", si_pid: %ld", (long) pid);
1054    if (IS_VALID_PID(pid)) {
1055      const pid_t me = getpid();
1056      if (me == pid) {
1057        os->print(" (current process)");
1058      }
1059    } else {
1060      os->print(" (invalid)");
1061    }
1062    os->print(", si_uid: %ld", (long) si->si_uid);
1063    if (sig == SIGCHLD) {
1064      os->print(", si_status: %d", si->si_status);
1065    }
1066  } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
1067             sig == SIGTRAP || sig == SIGFPE) {
1068    os->print(", si_addr: " PTR_FORMAT, p2i(si->si_addr));
1069#ifdef SIGPOLL
1070  } else if (sig == SIGPOLL) {
1071    os->print(", si_band: %ld", si->si_band);
1072#endif
1073  }
1074
1075}
1076
1077int os::Posix::unblock_thread_signal_mask(const sigset_t *set) {
1078  return pthread_sigmask(SIG_UNBLOCK, set, NULL);
1079}
1080
1081address os::Posix::ucontext_get_pc(const ucontext_t* ctx) {
1082#if defined(AIX)
1083   return Aix::ucontext_get_pc(ctx);
1084#elif defined(BSD)
1085   return Bsd::ucontext_get_pc(ctx);
1086#elif defined(LINUX)
1087   return Linux::ucontext_get_pc(ctx);
1088#elif defined(SOLARIS)
1089   return Solaris::ucontext_get_pc(ctx);
1090#else
1091   VMError::report_and_die("unimplemented ucontext_get_pc");
1092#endif
1093}
1094
1095void os::Posix::ucontext_set_pc(ucontext_t* ctx, address pc) {
1096#if defined(AIX)
1097   Aix::ucontext_set_pc(ctx, pc);
1098#elif defined(BSD)
1099   Bsd::ucontext_set_pc(ctx, pc);
1100#elif defined(LINUX)
1101   Linux::ucontext_set_pc(ctx, pc);
1102#elif defined(SOLARIS)
1103   Solaris::ucontext_set_pc(ctx, pc);
1104#else
1105   VMError::report_and_die("unimplemented ucontext_get_pc");
1106#endif
1107}
1108
1109char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) {
1110  size_t stack_size = 0;
1111  size_t guard_size = 0;
1112  int detachstate = 0;
1113  pthread_attr_getstacksize(attr, &stack_size);
1114  pthread_attr_getguardsize(attr, &guard_size);
1115  // Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp.
1116  LINUX_ONLY(stack_size -= guard_size);
1117  pthread_attr_getdetachstate(attr, &detachstate);
1118  jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s",
1119    stack_size / 1024, guard_size / 1024,
1120    (detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable"));
1121  return buf;
1122}
1123
1124char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) {
1125
1126  if (filename == NULL || outbuf == NULL || outbuflen < 1) {
1127    assert(false, "os::Posix::realpath: invalid arguments.");
1128    errno = EINVAL;
1129    return NULL;
1130  }
1131
1132  char* result = NULL;
1133
1134  // This assumes platform realpath() is implemented according to POSIX.1-2008.
1135  // POSIX.1-2008 allows to specify NULL for the output buffer, in which case
1136  // output buffer is dynamically allocated and must be ::free()'d by the caller.
1137  char* p = ::realpath(filename, NULL);
1138  if (p != NULL) {
1139    if (strlen(p) < outbuflen) {
1140      strcpy(outbuf, p);
1141      result = outbuf;
1142    } else {
1143      errno = ENAMETOOLONG;
1144    }
1145    ::free(p); // *not* os::free
1146  } else {
1147    // Fallback for platforms struggling with modern Posix standards (AIX 5.3, 6.1). If realpath
1148    // returns EINVAL, this may indicate that realpath is not POSIX.1-2008 compatible and
1149    // that it complains about the NULL we handed down as user buffer.
1150    // In this case, use the user provided buffer but at least check whether realpath caused
1151    // a memory overwrite.
1152    if (errno == EINVAL) {
1153      outbuf[outbuflen - 1] = '\0';
1154      p = ::realpath(filename, outbuf);
1155      if (p != NULL) {
1156        guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected.");
1157        result = p;
1158      }
1159    }
1160  }
1161  return result;
1162
1163}
1164
1165
1166// Check minimum allowable stack sizes for thread creation and to initialize
1167// the java system classes, including StackOverflowError - depends on page
1168// size.
1169// The space needed for frames during startup is platform dependent. It
1170// depends on word size, platform calling conventions, C frame layout and
1171// interpreter/C1/C2 design decisions. Therefore this is given in a
1172// platform (os/cpu) dependent constant.
1173// To this, space for guard mechanisms is added, which depends on the
1174// page size which again depends on the concrete system the VM is running
1175// on. Space for libc guard pages is not included in this size.
1176jint os::Posix::set_minimum_stack_sizes() {
1177  size_t os_min_stack_allowed = SOLARIS_ONLY(thr_min_stack()) NOT_SOLARIS(PTHREAD_STACK_MIN);
1178
1179  _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
1180                                   JavaThread::stack_guard_zone_size() +
1181                                   JavaThread::stack_shadow_zone_size();
1182
1183  _java_thread_min_stack_allowed = align_size_up(_java_thread_min_stack_allowed, vm_page_size());
1184  _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);
1185
1186  size_t stack_size_in_bytes = ThreadStackSize * K;
1187  if (stack_size_in_bytes != 0 &&
1188      stack_size_in_bytes < _java_thread_min_stack_allowed) {
1189    // The '-Xss' and '-XX:ThreadStackSize=N' options both set
1190    // ThreadStackSize so we go with "Java thread stack size" instead
1191    // of "ThreadStackSize" to be more friendly.
1192    tty->print_cr("\nThe Java thread stack size specified is too small. "
1193                  "Specify at least " SIZE_FORMAT "k",
1194                  _java_thread_min_stack_allowed / K);
1195    return JNI_ERR;
1196  }
1197
1198  // Make the stack size a multiple of the page size so that
1199  // the yellow/red zones can be guarded.
1200  JavaThread::set_stack_size_at_create(round_to(stack_size_in_bytes, vm_page_size()));
1201
1202  // Reminder: a compiler thread is a Java thread.
1203  _compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +
1204                                       JavaThread::stack_guard_zone_size() +
1205                                       JavaThread::stack_shadow_zone_size();
1206
1207  _compiler_thread_min_stack_allowed = align_size_up(_compiler_thread_min_stack_allowed, vm_page_size());
1208  _compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed);
1209
1210  stack_size_in_bytes = CompilerThreadStackSize * K;
1211  if (stack_size_in_bytes != 0 &&
1212      stack_size_in_bytes < _compiler_thread_min_stack_allowed) {
1213    tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "
1214                  "Specify at least " SIZE_FORMAT "k",
1215                  _compiler_thread_min_stack_allowed / K);
1216    return JNI_ERR;
1217  }
1218
1219  _vm_internal_thread_min_stack_allowed = align_size_up(_vm_internal_thread_min_stack_allowed, vm_page_size());
1220  _vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed);
1221
1222  stack_size_in_bytes = VMThreadStackSize * K;
1223  if (stack_size_in_bytes != 0 &&
1224      stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {
1225    tty->print_cr("\nThe VMThreadStackSize specified is too small. "
1226                  "Specify at least " SIZE_FORMAT "k",
1227                  _vm_internal_thread_min_stack_allowed / K);
1228    return JNI_ERR;
1229  }
1230  return JNI_OK;
1231}
1232
1233// Called when creating the thread.  The minimum stack sizes have already been calculated
1234size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) {
1235  size_t stack_size;
1236  if (req_stack_size == 0) {
1237    stack_size = default_stack_size(thr_type);
1238  } else {
1239    stack_size = req_stack_size;
1240  }
1241
1242  switch (thr_type) {
1243  case os::java_thread:
1244    // Java threads use ThreadStackSize which default value can be
1245    // changed with the flag -Xss
1246    if (req_stack_size == 0 && JavaThread::stack_size_at_create() > 0) {
1247      // no requested size and we have a more specific default value
1248      stack_size = JavaThread::stack_size_at_create();
1249    }
1250    stack_size = MAX2(stack_size,
1251                      _java_thread_min_stack_allowed);
1252    break;
1253  case os::compiler_thread:
1254    if (req_stack_size == 0 && CompilerThreadStackSize > 0) {
1255      // no requested size and we have a more specific default value
1256      stack_size = (size_t)(CompilerThreadStackSize * K);
1257    }
1258    stack_size = MAX2(stack_size,
1259                      _compiler_thread_min_stack_allowed);
1260    break;
1261  case os::vm_thread:
1262  case os::pgc_thread:
1263  case os::cgc_thread:
1264  case os::watcher_thread:
1265  default:  // presume the unknown thr_type is a VM internal
1266    if (req_stack_size == 0 && VMThreadStackSize > 0) {
1267      // no requested size and we have a more specific default value
1268      stack_size = (size_t)(VMThreadStackSize * K);
1269    }
1270
1271    stack_size = MAX2(stack_size,
1272                      _vm_internal_thread_min_stack_allowed);
1273    break;
1274  }
1275
1276  // pthread_attr_setstacksize() may require that the size be rounded up to the OS page size.
1277  // Be careful not to round up to 0. Align down in that case.
1278  if (stack_size <= SIZE_MAX - vm_page_size()) {
1279    stack_size = align_size_up(stack_size, vm_page_size());
1280  } else {
1281    stack_size = align_size_down(stack_size, vm_page_size());
1282  }
1283
1284  return stack_size;
1285}
1286
1287os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
1288  assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
1289}
1290
1291/*
1292 * See the caveats for this class in os_posix.hpp
1293 * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
1294 * method and returns false. If none of the signals are raised, returns true.
1295 * The callback is supposed to provide the method that should be protected.
1296 */
1297bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
1298  sigset_t saved_sig_mask;
1299
1300  assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
1301  assert(!WatcherThread::watcher_thread()->has_crash_protection(),
1302      "crash_protection already set?");
1303
1304  // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
1305  // since on at least some systems (OS X) siglongjmp will restore the mask
1306  // for the process, not the thread
1307  pthread_sigmask(0, NULL, &saved_sig_mask);
1308  if (sigsetjmp(_jmpbuf, 0) == 0) {
1309    // make sure we can see in the signal handler that we have crash protection
1310    // installed
1311    WatcherThread::watcher_thread()->set_crash_protection(this);
1312    cb.call();
1313    // and clear the crash protection
1314    WatcherThread::watcher_thread()->set_crash_protection(NULL);
1315    return true;
1316  }
1317  // this happens when we siglongjmp() back
1318  pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);
1319  WatcherThread::watcher_thread()->set_crash_protection(NULL);
1320  return false;
1321}
1322
1323void os::WatcherThreadCrashProtection::restore() {
1324  assert(WatcherThread::watcher_thread()->has_crash_protection(),
1325      "must have crash protection");
1326
1327  siglongjmp(_jmpbuf, 1);
1328}
1329
1330void os::WatcherThreadCrashProtection::check_crash_protection(int sig,
1331    Thread* thread) {
1332
1333  if (thread != NULL &&
1334      thread->is_Watcher_thread() &&
1335      WatcherThread::watcher_thread()->has_crash_protection()) {
1336
1337    if (sig == SIGSEGV || sig == SIGBUS) {
1338      WatcherThread::watcher_thread()->crash_protection()->restore();
1339    }
1340  }
1341}
1342
1343#define check_with_errno(check_type, cond, msg)                             \
1344  do {                                                                      \
1345    int err = errno;                                                        \
1346    check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err),   \
1347               os::errno_name(err));                                        \
1348} while (false)
1349
1350#define assert_with_errno(cond, msg)    check_with_errno(assert, cond, msg)
1351#define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
1352
1353// POSIX unamed semaphores are not supported on OS X.
1354#ifndef __APPLE__
1355
1356PosixSemaphore::PosixSemaphore(uint value) {
1357  int ret = sem_init(&_semaphore, 0, value);
1358
1359  guarantee_with_errno(ret == 0, "Failed to initialize semaphore");
1360}
1361
1362PosixSemaphore::~PosixSemaphore() {
1363  sem_destroy(&_semaphore);
1364}
1365
1366void PosixSemaphore::signal(uint count) {
1367  for (uint i = 0; i < count; i++) {
1368    int ret = sem_post(&_semaphore);
1369
1370    assert_with_errno(ret == 0, "sem_post failed");
1371  }
1372}
1373
1374void PosixSemaphore::wait() {
1375  int ret;
1376
1377  do {
1378    ret = sem_wait(&_semaphore);
1379  } while (ret != 0 && errno == EINTR);
1380
1381  assert_with_errno(ret == 0, "sem_wait failed");
1382}
1383
1384bool PosixSemaphore::trywait() {
1385  int ret;
1386
1387  do {
1388    ret = sem_trywait(&_semaphore);
1389  } while (ret != 0 && errno == EINTR);
1390
1391  assert_with_errno(ret == 0 || errno == EAGAIN, "trywait failed");
1392
1393  return ret == 0;
1394}
1395
1396bool PosixSemaphore::timedwait(struct timespec ts) {
1397  while (true) {
1398    int result = sem_timedwait(&_semaphore, &ts);
1399    if (result == 0) {
1400      return true;
1401    } else if (errno == EINTR) {
1402      continue;
1403    } else if (errno == ETIMEDOUT) {
1404      return false;
1405    } else {
1406      assert_with_errno(false, "timedwait failed");
1407      return false;
1408    }
1409  }
1410}
1411
1412#endif // __APPLE__
1413
1414
1415// Shared pthread_mutex/cond based PlatformEvent implementation.
1416// Not currently usable by Solaris.
1417
1418#ifndef SOLARIS
1419
1420// Shared condattr object for use with relative timed-waits. Will be associated
1421// with CLOCK_MONOTONIC if available to avoid issues with time-of-day changes,
1422// but otherwise whatever default is used by the platform - generally the
1423// time-of-day clock.
1424static pthread_condattr_t _condAttr[1];
1425
1426// Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not
1427// all systems (e.g. FreeBSD) map the default to "normal".
1428static pthread_mutexattr_t _mutexAttr[1];
1429
1430// common basic initialization that is always supported
1431static void pthread_init_common(void) {
1432  int status;
1433  if ((status = pthread_condattr_init(_condAttr)) != 0) {
1434    fatal("pthread_condattr_init: %s", os::strerror(status));
1435  }
1436  if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) {
1437    fatal("pthread_mutexattr_init: %s", os::strerror(status));
1438  }
1439  if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) {
1440    fatal("pthread_mutexattr_settype: %s", os::strerror(status));
1441  }
1442}
1443
1444// Not all POSIX types and API's are available on all notionally "posix"
1445// platforms. If we have build-time support then we will check for actual
1446// runtime support via dlopen/dlsym lookup. This allows for running on an
1447// older OS version compared to the build platform. But if there is no
1448// build time support then there cannot be any runtime support as we do not
1449// know what the runtime types would be (for example clockid_t might be an
1450// int or int64_t).
1451//
1452#ifdef SUPPORTS_CLOCK_MONOTONIC
1453
1454// This means we have clockid_t, clock_gettime et al and CLOCK_MONOTONIC
1455
1456static int (*_clock_gettime)(clockid_t, struct timespec *);
1457static int (*_pthread_condattr_setclock)(pthread_condattr_t *, clockid_t);
1458
1459static bool _use_clock_monotonic_condattr;
1460
1461// Determine what POSIX API's are present and do appropriate
1462// configuration.
1463void os::Posix::init(void) {
1464
1465  // NOTE: no logging available when this is called. Put logging
1466  // statements in init_2().
1467
1468  // Copied from os::Linux::clock_init(). The duplication is temporary.
1469
1470  // 1. Check for CLOCK_MONOTONIC support.
1471
1472  void* handle = NULL;
1473
1474  // For linux we need librt, for other OS we can find
1475  // this function in regular libc.
1476#ifdef NEEDS_LIBRT
1477  // We do dlopen's in this particular order due to bug in linux
1478  // dynamic loader (see 6348968) leading to crash on exit.
1479  handle = dlopen("librt.so.1", RTLD_LAZY);
1480  if (handle == NULL) {
1481    handle = dlopen("librt.so", RTLD_LAZY);
1482  }
1483#endif
1484
1485  if (handle == NULL) {
1486    handle = RTLD_DEFAULT;
1487  }
1488
1489  _clock_gettime = NULL;
1490
1491  int (*clock_getres_func)(clockid_t, struct timespec*) =
1492    (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");
1493  int (*clock_gettime_func)(clockid_t, struct timespec*) =
1494    (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");
1495  if (clock_getres_func != NULL && clock_gettime_func != NULL) {
1496    // We assume that if both clock_gettime and clock_getres support
1497    // CLOCK_MONOTONIC then the OS provides true high-res monotonic clock.
1498    struct timespec res;
1499    struct timespec tp;
1500    if (clock_getres_func(CLOCK_MONOTONIC, &res) == 0 &&
1501        clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {
1502      // Yes, monotonic clock is supported.
1503      _clock_gettime = clock_gettime_func;
1504    } else {
1505#ifdef NEEDS_LIBRT
1506      // Close librt if there is no monotonic clock.
1507      if (handle != RTLD_DEFAULT) {
1508        dlclose(handle);
1509      }
1510#endif
1511    }
1512  }
1513
1514  // 2. Check for pthread_condattr_setclock support.
1515
1516  _pthread_condattr_setclock = NULL;
1517
1518  // libpthread is already loaded.
1519  int (*condattr_setclock_func)(pthread_condattr_t*, clockid_t) =
1520    (int (*)(pthread_condattr_t*, clockid_t))dlsym(RTLD_DEFAULT,
1521                                                   "pthread_condattr_setclock");
1522  if (condattr_setclock_func != NULL) {
1523    _pthread_condattr_setclock = condattr_setclock_func;
1524  }
1525
1526  // Now do general initialization.
1527
1528  pthread_init_common();
1529
1530  int status;
1531  if (_pthread_condattr_setclock != NULL && _clock_gettime != NULL) {
1532    if ((status = _pthread_condattr_setclock(_condAttr, CLOCK_MONOTONIC)) != 0) {
1533      if (status == EINVAL) {
1534        _use_clock_monotonic_condattr = false;
1535        warning("Unable to use monotonic clock with relative timed-waits" \
1536                " - changes to the time-of-day clock may have adverse affects");
1537      } else {
1538        fatal("pthread_condattr_setclock: %s", os::strerror(status));
1539      }
1540    } else {
1541      _use_clock_monotonic_condattr = true;
1542    }
1543  } else {
1544    _use_clock_monotonic_condattr = false;
1545  }
1546}
1547
1548void os::Posix::init_2(void) {
1549  log_info(os)("Use of CLOCK_MONOTONIC is%s supported",
1550               (_clock_gettime != NULL ? "" : " not"));
1551  log_info(os)("Use of pthread_condattr_setclock is%s supported",
1552               (_pthread_condattr_setclock != NULL ? "" : " not"));
1553  log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with %s",
1554               _use_clock_monotonic_condattr ? "CLOCK_MONOTONIC" : "the default clock");
1555}
1556
1557#else // !SUPPORTS_CLOCK_MONOTONIC
1558
1559void os::Posix::init(void) {
1560  pthread_init_common();
1561}
1562
1563void os::Posix::init_2(void) {
1564  log_info(os)("Use of CLOCK_MONOTONIC is not supported");
1565  log_info(os)("Use of pthread_condattr_setclock is not supported");
1566  log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with the default clock");
1567}
1568
1569#endif // SUPPORTS_CLOCK_MONOTONIC
1570
1571os::PlatformEvent::PlatformEvent() {
1572  int status = pthread_cond_init(_cond, _condAttr);
1573  assert_status(status == 0, status, "cond_init");
1574  status = pthread_mutex_init(_mutex, _mutexAttr);
1575  assert_status(status == 0, status, "mutex_init");
1576  _event   = 0;
1577  _nParked = 0;
1578}
1579
1580// Utility to convert the given timeout to an absolute timespec
1581// (based on the appropriate clock) to use with pthread_cond_timewait.
1582// The clock queried here must be the clock used to manage the
1583// timeout of the condition variable.
1584//
1585// The passed in timeout value is either a relative time in nanoseconds
1586// or an absolute time in milliseconds. A relative timeout will be
1587// associated with CLOCK_MONOTONIC if available; otherwise, or if absolute,
1588// the default time-of-day clock will be used.
1589
1590// Given time is a 64-bit value and the time_t used in the timespec is
1591// sometimes a signed-32-bit value we have to watch for overflow if times
1592// way in the future are given. Further on Solaris versions
1593// prior to 10 there is a restriction (see cond_timedwait) that the specified
1594// number of seconds, in abstime, is less than current_time + 100000000.
1595// As it will be over 20 years before "now + 100000000" will overflow we can
1596// ignore overflow and just impose a hard-limit on seconds using the value
1597// of "now + 100000000". This places a limit on the timeout of about 3.17
1598// years from "now".
1599//
1600#define MAX_SECS 100000000
1601
1602// Calculate a new absolute time that is "timeout" nanoseconds from "now".
1603// "unit" indicates the unit of "now_part_sec" (may be nanos or micros depending
1604// on which clock is being used).
1605static void calc_rel_time(timespec* abstime, jlong timeout, jlong now_sec,
1606                          jlong now_part_sec, jlong unit) {
1607  time_t max_secs = now_sec + MAX_SECS;
1608
1609  jlong seconds = timeout / NANOUNITS;
1610  timeout %= NANOUNITS; // remaining nanos
1611
1612  if (seconds >= MAX_SECS) {
1613    // More seconds than we can add, so pin to max_secs.
1614    abstime->tv_sec = max_secs;
1615    abstime->tv_nsec = 0;
1616  } else {
1617    abstime->tv_sec = now_sec  + seconds;
1618    long nanos = (now_part_sec * (NANOUNITS / unit)) + timeout;
1619    if (nanos >= NANOUNITS) { // overflow
1620      abstime->tv_sec += 1;
1621      nanos -= NANOUNITS;
1622    }
1623    abstime->tv_nsec = nanos;
1624  }
1625}
1626
1627// Unpack the given deadline in milliseconds since the epoch, into the given timespec.
1628// The current time in seconds is also passed in to enforce an upper bound as discussed above.
1629static void unpack_abs_time(timespec* abstime, jlong deadline, jlong now_sec) {
1630  time_t max_secs = now_sec + MAX_SECS;
1631
1632  jlong seconds = deadline / MILLIUNITS;
1633  jlong millis = deadline % MILLIUNITS;
1634
1635  if (seconds >= max_secs) {
1636    // Absolute seconds exceeds allowed max, so pin to max_secs.
1637    abstime->tv_sec = max_secs;
1638    abstime->tv_nsec = 0;
1639  } else {
1640    abstime->tv_sec = seconds;
1641    abstime->tv_nsec = millis * (NANOUNITS / MILLIUNITS);
1642  }
1643}
1644
1645static void to_abstime(timespec* abstime, jlong timeout, bool isAbsolute) {
1646  DEBUG_ONLY(int max_secs = MAX_SECS;)
1647
1648  if (timeout < 0) {
1649    timeout = 0;
1650  }
1651
1652#ifdef SUPPORTS_CLOCK_MONOTONIC
1653
1654  if (_use_clock_monotonic_condattr && !isAbsolute) {
1655    struct timespec now;
1656    int status = _clock_gettime(CLOCK_MONOTONIC, &now);
1657    assert_status(status == 0, status, "clock_gettime");
1658    calc_rel_time(abstime, timeout, now.tv_sec, now.tv_nsec, NANOUNITS);
1659    DEBUG_ONLY(max_secs += now.tv_sec;)
1660  } else {
1661
1662#else
1663
1664  { // Match the block scope.
1665
1666#endif // SUPPORTS_CLOCK_MONOTONIC
1667
1668    // Time-of-day clock is all we can reliably use.
1669    struct timeval now;
1670    int status = gettimeofday(&now, NULL);
1671    assert_status(status == 0, errno, "gettimeofday");
1672    if (isAbsolute) {
1673      unpack_abs_time(abstime, timeout, now.tv_sec);
1674    } else {
1675      calc_rel_time(abstime, timeout, now.tv_sec, now.tv_usec, MICROUNITS);
1676    }
1677    DEBUG_ONLY(max_secs += now.tv_sec;)
1678  }
1679
1680  assert(abstime->tv_sec >= 0, "tv_sec < 0");
1681  assert(abstime->tv_sec <= max_secs, "tv_sec > max_secs");
1682  assert(abstime->tv_nsec >= 0, "tv_nsec < 0");
1683  assert(abstime->tv_nsec < NANOUNITS, "tv_nsec >= NANOUNITS");
1684}
1685
1686// PlatformEvent
1687//
1688// Assumption:
1689//    Only one parker can exist on an event, which is why we allocate
1690//    them per-thread. Multiple unparkers can coexist.
1691//
1692// _event serves as a restricted-range semaphore.
1693//   -1 : thread is blocked, i.e. there is a waiter
1694//    0 : neutral: thread is running or ready,
1695//        could have been signaled after a wait started
1696//    1 : signaled - thread is running or ready
1697//
1698//    Having three states allows for some detection of bad usage - see
1699//    comments on unpark().
1700
1701void os::PlatformEvent::park() {       // AKA "down()"
1702  // Transitions for _event:
1703  //   -1 => -1 : illegal
1704  //    1 =>  0 : pass - return immediately
1705  //    0 => -1 : block; then set _event to 0 before returning
1706
1707  // Invariant: Only the thread associated with the PlatformEvent
1708  // may call park().
1709  assert(_nParked == 0, "invariant");
1710
1711  int v;
1712
1713  // atomically decrement _event
1714  for (;;) {
1715    v = _event;
1716    if (Atomic::cmpxchg(v - 1, &_event, v) == v) break;
1717  }
1718  guarantee(v >= 0, "invariant");
1719
1720  if (v == 0) { // Do this the hard way by blocking ...
1721    int status = pthread_mutex_lock(_mutex);
1722    assert_status(status == 0, status, "mutex_lock");
1723    guarantee(_nParked == 0, "invariant");
1724    ++_nParked;
1725    while (_event < 0) {
1726      // OS-level "spurious wakeups" are ignored
1727      status = pthread_cond_wait(_cond, _mutex);
1728      assert_status(status == 0, status, "cond_wait");
1729    }
1730    --_nParked;
1731
1732    _event = 0;
1733    status = pthread_mutex_unlock(_mutex);
1734    assert_status(status == 0, status, "mutex_unlock");
1735    // Paranoia to ensure our locked and lock-free paths interact
1736    // correctly with each other.
1737    OrderAccess::fence();
1738  }
1739  guarantee(_event >= 0, "invariant");
1740}
1741
1742int os::PlatformEvent::park(jlong millis) {
1743  // Transitions for _event:
1744  //   -1 => -1 : illegal
1745  //    1 =>  0 : pass - return immediately
1746  //    0 => -1 : block; then set _event to 0 before returning
1747
1748  // Invariant: Only the thread associated with the Event/PlatformEvent
1749  // may call park().
1750  assert(_nParked == 0, "invariant");
1751
1752  int v;
1753  // atomically decrement _event
1754  for (;;) {
1755    v = _event;
1756    if (Atomic::cmpxchg(v - 1, &_event, v) == v) break;
1757  }
1758  guarantee(v >= 0, "invariant");
1759
1760  if (v == 0) { // Do this the hard way by blocking ...
1761    struct timespec abst;
1762    to_abstime(&abst, millis * (NANOUNITS / MILLIUNITS), false);
1763
1764    int ret = OS_TIMEOUT;
1765    int status = pthread_mutex_lock(_mutex);
1766    assert_status(status == 0, status, "mutex_lock");
1767    guarantee(_nParked == 0, "invariant");
1768    ++_nParked;
1769
1770    while (_event < 0) {
1771      status = pthread_cond_timedwait(_cond, _mutex, &abst);
1772      assert_status(status == 0 || status == ETIMEDOUT,
1773                    status, "cond_timedwait");
1774      // OS-level "spurious wakeups" are ignored unless the archaic
1775      // FilterSpuriousWakeups is set false. That flag should be obsoleted.
1776      if (!FilterSpuriousWakeups) break;
1777      if (status == ETIMEDOUT) break;
1778    }
1779    --_nParked;
1780
1781    if (_event >= 0) {
1782      ret = OS_OK;
1783    }
1784
1785    _event = 0;
1786    status = pthread_mutex_unlock(_mutex);
1787    assert_status(status == 0, status, "mutex_unlock");
1788    // Paranoia to ensure our locked and lock-free paths interact
1789    // correctly with each other.
1790    OrderAccess::fence();
1791    return ret;
1792  }
1793  return OS_OK;
1794}
1795
1796void os::PlatformEvent::unpark() {
1797  // Transitions for _event:
1798  //    0 => 1 : just return
1799  //    1 => 1 : just return
1800  //   -1 => either 0 or 1; must signal target thread
1801  //         That is, we can safely transition _event from -1 to either
1802  //         0 or 1.
1803  // See also: "Semaphores in Plan 9" by Mullender & Cox
1804  //
1805  // Note: Forcing a transition from "-1" to "1" on an unpark() means
1806  // that it will take two back-to-back park() calls for the owning
1807  // thread to block. This has the benefit of forcing a spurious return
1808  // from the first park() call after an unpark() call which will help
1809  // shake out uses of park() and unpark() without checking state conditions
1810  // properly. This spurious return doesn't manifest itself in any user code
1811  // but only in the correctly written condition checking loops of ObjectMonitor,
1812  // Mutex/Monitor, Thread::muxAcquire and os::sleep
1813
1814  if (Atomic::xchg(1, &_event) >= 0) return;
1815
1816  int status = pthread_mutex_lock(_mutex);
1817  assert_status(status == 0, status, "mutex_lock");
1818  int anyWaiters = _nParked;
1819  assert(anyWaiters == 0 || anyWaiters == 1, "invariant");
1820  status = pthread_mutex_unlock(_mutex);
1821  assert_status(status == 0, status, "mutex_unlock");
1822
1823  // Note that we signal() *after* dropping the lock for "immortal" Events.
1824  // This is safe and avoids a common class of futile wakeups.  In rare
1825  // circumstances this can cause a thread to return prematurely from
1826  // cond_{timed}wait() but the spurious wakeup is benign and the victim
1827  // will simply re-test the condition and re-park itself.
1828  // This provides particular benefit if the underlying platform does not
1829  // provide wait morphing.
1830
1831  if (anyWaiters != 0) {
1832    status = pthread_cond_signal(_cond);
1833    assert_status(status == 0, status, "cond_signal");
1834  }
1835}
1836
1837// JSR166 support
1838
1839 os::PlatformParker::PlatformParker() {
1840  int status;
1841  status = pthread_cond_init(&_cond[REL_INDEX], _condAttr);
1842  assert_status(status == 0, status, "cond_init rel");
1843  status = pthread_cond_init(&_cond[ABS_INDEX], NULL);
1844  assert_status(status == 0, status, "cond_init abs");
1845  status = pthread_mutex_init(_mutex, _mutexAttr);
1846  assert_status(status == 0, status, "mutex_init");
1847  _cur_index = -1; // mark as unused
1848}
1849
1850// Parker::park decrements count if > 0, else does a condvar wait.  Unpark
1851// sets count to 1 and signals condvar.  Only one thread ever waits
1852// on the condvar. Contention seen when trying to park implies that someone
1853// is unparking you, so don't wait. And spurious returns are fine, so there
1854// is no need to track notifications.
1855
1856void Parker::park(bool isAbsolute, jlong time) {
1857
1858  // Optional fast-path check:
1859  // Return immediately if a permit is available.
1860  // We depend on Atomic::xchg() having full barrier semantics
1861  // since we are doing a lock-free update to _counter.
1862  if (Atomic::xchg(0, &_counter) > 0) return;
1863
1864  Thread* thread = Thread::current();
1865  assert(thread->is_Java_thread(), "Must be JavaThread");
1866  JavaThread *jt = (JavaThread *)thread;
1867
1868  // Optional optimization -- avoid state transitions if there's
1869  // an interrupt pending.
1870  if (Thread::is_interrupted(thread, false)) {
1871    return;
1872  }
1873
1874  // Next, demultiplex/decode time arguments
1875  struct timespec absTime;
1876  if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all
1877    return;
1878  }
1879  if (time > 0) {
1880    to_abstime(&absTime, time, isAbsolute);
1881  }
1882
1883  // Enter safepoint region
1884  // Beware of deadlocks such as 6317397.
1885  // The per-thread Parker:: mutex is a classic leaf-lock.
1886  // In particular a thread must never block on the Threads_lock while
1887  // holding the Parker:: mutex.  If safepoints are pending both the
1888  // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.
1889  ThreadBlockInVM tbivm(jt);
1890
1891  // Don't wait if cannot get lock since interference arises from
1892  // unparking. Also re-check interrupt before trying wait.
1893  if (Thread::is_interrupted(thread, false) ||
1894      pthread_mutex_trylock(_mutex) != 0) {
1895    return;
1896  }
1897
1898  int status;
1899  if (_counter > 0)  { // no wait needed
1900    _counter = 0;
1901    status = pthread_mutex_unlock(_mutex);
1902    assert_status(status == 0, status, "invariant");
1903    // Paranoia to ensure our locked and lock-free paths interact
1904    // correctly with each other and Java-level accesses.
1905    OrderAccess::fence();
1906    return;
1907  }
1908
1909  OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
1910  jt->set_suspend_equivalent();
1911  // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
1912
1913  assert(_cur_index == -1, "invariant");
1914  if (time == 0) {
1915    _cur_index = REL_INDEX; // arbitrary choice when not timed
1916    status = pthread_cond_wait(&_cond[_cur_index], _mutex);
1917    assert_status(status == 0, status, "cond_timedwait");
1918  }
1919  else {
1920    _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;
1921    status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime);
1922    assert_status(status == 0 || status == ETIMEDOUT,
1923                  status, "cond_timedwait");
1924  }
1925  _cur_index = -1;
1926
1927  _counter = 0;
1928  status = pthread_mutex_unlock(_mutex);
1929  assert_status(status == 0, status, "invariant");
1930  // Paranoia to ensure our locked and lock-free paths interact
1931  // correctly with each other and Java-level accesses.
1932  OrderAccess::fence();
1933
1934  // If externally suspended while waiting, re-suspend
1935  if (jt->handle_special_suspend_equivalent_condition()) {
1936    jt->java_suspend_self();
1937  }
1938}
1939
1940void Parker::unpark() {
1941  int status = pthread_mutex_lock(_mutex);
1942  assert_status(status == 0, status, "invariant");
1943  const int s = _counter;
1944  _counter = 1;
1945  // must capture correct index before unlocking
1946  int index = _cur_index;
1947  status = pthread_mutex_unlock(_mutex);
1948  assert_status(status == 0, status, "invariant");
1949
1950  // Note that we signal() *after* dropping the lock for "immortal" Events.
1951  // This is safe and avoids a common class of futile wakeups.  In rare
1952  // circumstances this can cause a thread to return prematurely from
1953  // cond_{timed}wait() but the spurious wakeup is benign and the victim
1954  // will simply re-test the condition and re-park itself.
1955  // This provides particular benefit if the underlying platform does not
1956  // provide wait morphing.
1957
1958  if (s < 1 && index != -1) {
1959    // thread is definitely parked
1960    status = pthread_cond_signal(&_cond[index]);
1961    assert_status(status == 0, status, "invariant");
1962  }
1963}
1964
1965
1966#endif // !SOLARIS
1967