< prev index next >

src/hotspot/os/posix/os_posix.cpp

Print this page




 607       if (strlen(lib_name) <= (prefix_len + suffix_len)) {
 608         return NULL;
 609       }
 610       lib_name += prefix_len;
 611       name_len = strlen(lib_name) - suffix_len;
 612     }
 613   }
 614   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
 615   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
 616   if (agent_entry_name == NULL) {
 617     return NULL;
 618   }
 619   strcpy(agent_entry_name, sym_name);
 620   if (lib_name != NULL) {
 621     strcat(agent_entry_name, "_");
 622     strncat(agent_entry_name, lib_name, name_len);
 623   }
 624   return agent_entry_name;
 625 }
 626 








































































 627 
 628 void os::naked_short_nanosleep(jlong ns) {
 629   struct timespec req;
 630   assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");
 631   req.tv_sec = 0;
 632   req.tv_nsec = ns;
 633   ::nanosleep(&req, NULL);
 634   return;
 635 }
 636 
 637 void os::naked_short_sleep(jlong ms) {
 638   assert(ms < MILLIUNITS, "Un-interruptable sleep, short time use only");
 639   os::naked_short_nanosleep(ms * (NANOUNITS / MILLIUNITS));
 640   return;
 641 }
 642 
 643 ////////////////////////////////////////////////////////////////////////////////
 644 // interrupt support
 645 
 646 void os::interrupt(Thread* thread) {


1361       outbuf[outbuflen - 1] = '\0';
1362       p = ::realpath(filename, outbuf);
1363       if (p != NULL) {
1364         guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected.");
1365         result = p;
1366       }
1367     }
1368   }
1369   return result;
1370 
1371 }
1372 
1373 int os::stat(const char *path, struct stat *sbuf) {
1374   return ::stat(path, sbuf);
1375 }
1376 
1377 char * os::native_path(char *path) {
1378   return path;
1379 }
1380 
1381 bool os::same_files(const char* file1, const char* file2) {
1382   if (strcmp(file1, file2) == 0) {
1383     return true;
1384   }
1385 
1386   bool is_same = false;
1387   struct stat st1;
1388   struct stat st2;
1389 
1390   if (os::stat(file1, &st1) < 0) {
1391     return false;
1392   }
1393 
1394   if (os::stat(file2, &st2) < 0) {
1395     return false;
1396   }
1397 
1398   if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) {
1399     // same files
1400     is_same = true;
1401   }
1402   return is_same;
1403 }
1404 
1405 // Check minimum allowable stack sizes for thread creation and to initialize
1406 // the java system classes, including StackOverflowError - depends on page
1407 // size.
1408 // The space needed for frames during startup is platform dependent. It
1409 // depends on word size, platform calling conventions, C frame layout and
1410 // interpreter/C1/C2 design decisions. Therefore this is given in a
1411 // platform (os/cpu) dependent constant.
1412 // To this, space for guard mechanisms is added, which depends on the
1413 // page size which again depends on the concrete system the VM is running
1414 // on. Space for libc guard pages is not included in this size.
1415 jint os::Posix::set_minimum_stack_sizes() {
1416   size_t os_min_stack_allowed = SOLARIS_ONLY(thr_min_stack()) NOT_SOLARIS(PTHREAD_STACK_MIN);
1417 
1418   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
1419                                    JavaThread::stack_guard_zone_size() +
1420                                    JavaThread::stack_shadow_zone_size();
1421 
1422   _java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());
1423   _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);
1424 


1606 // but otherwise whatever default is used by the platform - generally the
1607 // time-of-day clock.
1608 static pthread_condattr_t _condAttr[1];
1609 
1610 // Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not
1611 // all systems (e.g. FreeBSD) map the default to "normal".
1612 static pthread_mutexattr_t _mutexAttr[1];
1613 
1614 // common basic initialization that is always supported
1615 static void pthread_init_common(void) {
1616   int status;
1617   if ((status = pthread_condattr_init(_condAttr)) != 0) {
1618     fatal("pthread_condattr_init: %s", os::strerror(status));
1619   }
1620   if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) {
1621     fatal("pthread_mutexattr_init: %s", os::strerror(status));
1622   }
1623   if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) {
1624     fatal("pthread_mutexattr_settype: %s", os::strerror(status));
1625   }
1626   // Solaris has it's own PlatformMutex, distinct from the one for POSIX.
1627   NOT_SOLARIS(os::PlatformMutex::init();)
1628 }
1629 
1630 #ifndef SOLARIS
1631 sigset_t sigs;
1632 struct sigaction sigact[NSIG];
1633 
1634 struct sigaction* os::Posix::get_preinstalled_handler(int sig) {
1635   if (sigismember(&sigs, sig)) {
1636     return &sigact[sig];
1637   }
1638   return NULL;
1639 }
1640 
1641 void os::Posix::save_preinstalled_handler(int sig, struct sigaction& oldAct) {
1642   assert(sig > 0 && sig < NSIG, "vm signal out of expected range");
1643   sigact[sig] = oldAct;
1644   sigaddset(&sigs, sig);
1645 }
1646 #endif
1647 


2185   // must capture correct index before unlocking
2186   int index = _cur_index;
2187   status = pthread_mutex_unlock(_mutex);
2188   assert_status(status == 0, status, "invariant");
2189 
2190   // Note that we signal() *after* dropping the lock for "immortal" Events.
2191   // This is safe and avoids a common class of futile wakeups.  In rare
2192   // circumstances this can cause a thread to return prematurely from
2193   // cond_{timed}wait() but the spurious wakeup is benign and the victim
2194   // will simply re-test the condition and re-park itself.
2195   // This provides particular benefit if the underlying platform does not
2196   // provide wait morphing.
2197 
2198   if (s < 1 && index != -1) {
2199     // thread is definitely parked
2200     status = pthread_cond_signal(&_cond[index]);
2201     assert_status(status == 0, status, "invariant");
2202   }
2203 }
2204 
2205 // Platform Mutex/Monitor implementation
2206 
2207 #if PLATFORM_MONITOR_IMPL_INDIRECT
2208 
2209 os::PlatformMutex::Mutex::Mutex() : _next(NULL) {
2210   int status = pthread_mutex_init(&_mutex, _mutexAttr);


2211   assert_status(status == 0, status, "mutex_init");
2212 }
2213 
2214 os::PlatformMutex::Mutex::~Mutex() {
2215   int status = pthread_mutex_destroy(&_mutex);


2216   assert_status(status == 0, status, "mutex_destroy");
2217 }
2218 
2219 pthread_mutex_t os::PlatformMutex::_freelist_lock;
2220 os::PlatformMutex::Mutex* os::PlatformMutex::_mutex_freelist = NULL;


2221 
2222 void os::PlatformMutex::init() {
2223   int status = pthread_mutex_init(&_freelist_lock, _mutexAttr);
2224   assert_status(status == 0, status, "freelist lock init");
2225 }
2226 
2227 struct os::PlatformMutex::WithFreeListLocked : public StackObj {
2228   WithFreeListLocked() {
2229     int status = pthread_mutex_lock(&_freelist_lock);
2230     assert_status(status == 0, status, "freelist lock");
2231   }
2232 
2233   ~WithFreeListLocked() {
2234     int status = pthread_mutex_unlock(&_freelist_lock);
2235     assert_status(status == 0, status, "freelist unlock");
2236   }
2237 };
2238 
2239 os::PlatformMutex::PlatformMutex() {
2240   {
2241     WithFreeListLocked wfl;
2242     _impl = _mutex_freelist;
2243     if (_impl != NULL) {
2244       _mutex_freelist = _impl->_next;
2245       _impl->_next = NULL;
2246       return;
2247     }
2248   }
2249   _impl = new Mutex();
2250 }
2251 
2252 os::PlatformMutex::~PlatformMutex() {
2253   WithFreeListLocked wfl;
2254   assert(_impl->_next == NULL, "invariant");
2255   _impl->_next = _mutex_freelist;
2256   _mutex_freelist = _impl;
2257 }
2258 
2259 os::PlatformMonitor::Cond::Cond() : _next(NULL) {
2260   int status = pthread_cond_init(&_cond, _condAttr);
2261   assert_status(status == 0, status, "cond_init");
2262 }
2263 
2264 os::PlatformMonitor::Cond::~Cond() {
2265   int status = pthread_cond_destroy(&_cond);
2266   assert_status(status == 0, status, "cond_destroy");
2267 }
2268 
2269 os::PlatformMonitor::Cond* os::PlatformMonitor::_cond_freelist = NULL;
2270 
2271 os::PlatformMonitor::PlatformMonitor() {
2272   {
2273     WithFreeListLocked wfl;
2274     _impl = _cond_freelist;
2275     if (_impl != NULL) {
2276       _cond_freelist = _impl->_next;
2277       _impl->_next = NULL;
2278       return;
2279     }
2280   }
2281   _impl = new Cond();
2282 }
2283 
2284 os::PlatformMonitor::~PlatformMonitor() {
2285   WithFreeListLocked wfl;
2286   assert(_impl->_next == NULL, "invariant");
2287   _impl->_next = _cond_freelist;
2288   _cond_freelist = _impl;
2289 }
2290 
2291 #else
2292 
2293 os::PlatformMutex::PlatformMutex() {
2294   int status = pthread_mutex_init(&_mutex, _mutexAttr);
2295   assert_status(status == 0, status, "mutex_init");
2296 }
2297 
2298 os::PlatformMutex::~PlatformMutex() {
2299   int status = pthread_mutex_destroy(&_mutex);
2300   assert_status(status == 0, status, "mutex_destroy");
2301 }
2302 
2303 os::PlatformMonitor::PlatformMonitor() {
2304   int status = pthread_cond_init(&_cond, _condAttr);
2305   assert_status(status == 0, status, "cond_init");
2306 }
2307 
2308 os::PlatformMonitor::~PlatformMonitor() {
2309   int status = pthread_cond_destroy(&_cond);
2310   assert_status(status == 0, status, "cond_destroy");
2311 }
2312 
2313 #endif // PLATFORM_MONITOR_IMPL_INDIRECT
2314 
2315 // Must already be locked
2316 int os::PlatformMonitor::wait(jlong millis) {
2317   assert(millis >= 0, "negative timeout");
2318   if (millis > 0) {
2319     struct timespec abst;
2320     // We have to watch for overflow when converting millis to nanos,
2321     // but if millis is that large then we will end up limiting to
2322     // MAX_SECS anyway, so just do that here.
2323     if (millis / MILLIUNITS > MAX_SECS) {
2324       millis = jlong(MAX_SECS) * MILLIUNITS;
2325     }
2326     to_abstime(&abst, millis * (NANOUNITS / MILLIUNITS), false, false);
2327 
2328     int ret = OS_TIMEOUT;
2329     int status = pthread_cond_timedwait(cond(), mutex(), &abst);
2330     assert_status(status == 0 || status == ETIMEDOUT,


 607       if (strlen(lib_name) <= (prefix_len + suffix_len)) {
 608         return NULL;
 609       }
 610       lib_name += prefix_len;
 611       name_len = strlen(lib_name) - suffix_len;
 612     }
 613   }
 614   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
 615   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
 616   if (agent_entry_name == NULL) {
 617     return NULL;
 618   }
 619   strcpy(agent_entry_name, sym_name);
 620   if (lib_name != NULL) {
 621     strcat(agent_entry_name, "_");
 622     strncat(agent_entry_name, lib_name, name_len);
 623   }
 624   return agent_entry_name;
 625 }
 626 
 627 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
 628   assert(thread == Thread::current(),  "thread consistency check");
 629 
 630   ParkEvent * const slp = thread->_SleepEvent ;
 631   slp->reset() ;
 632   OrderAccess::fence() ;
 633 
 634   if (interruptible) {
 635     jlong prevtime = javaTimeNanos();
 636 
 637     for (;;) {
 638       if (os::is_interrupted(thread, true)) {
 639         return OS_INTRPT;
 640       }
 641 
 642       jlong newtime = javaTimeNanos();
 643 
 644       if (newtime - prevtime < 0) {
 645         // time moving backwards, should only happen if no monotonic clock
 646         // not a guarantee() because JVM should not abort on kernel/glibc bugs
 647         assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected in os::sleep(interruptible)");
 648       } else {
 649         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
 650       }
 651 
 652       if (millis <= 0) {
 653         return OS_OK;
 654       }
 655 
 656       prevtime = newtime;
 657 
 658       {
 659         assert(thread->is_Java_thread(), "sanity check");
 660         JavaThread *jt = (JavaThread *) thread;
 661         ThreadBlockInVM tbivm(jt);
 662         OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
 663 
 664         jt->set_suspend_equivalent();
 665         // cleared by handle_special_suspend_equivalent_condition() or
 666         // java_suspend_self() via check_and_wait_while_suspended()
 667 
 668         slp->park(millis);
 669 
 670         // were we externally suspended while we were waiting?
 671         jt->check_and_wait_while_suspended();
 672       }
 673     }
 674   } else {
 675     OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
 676     jlong prevtime = javaTimeNanos();
 677 
 678     for (;;) {
 679       // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
 680       // the 1st iteration ...
 681       jlong newtime = javaTimeNanos();
 682 
 683       if (newtime - prevtime < 0) {
 684         // time moving backwards, should only happen if no monotonic clock
 685         // not a guarantee() because JVM should not abort on kernel/glibc bugs
 686         assert(!os::supports_monotonic_clock(), "unexpected time moving backwards detected on os::sleep(!interruptible)");
 687       } else {
 688         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
 689       }
 690 
 691       if (millis <= 0) break ;
 692 
 693       prevtime = newtime;
 694       slp->park(millis);
 695     }
 696     return OS_OK ;
 697   }
 698 }
 699 
 700 void os::naked_short_nanosleep(jlong ns) {
 701   struct timespec req;
 702   assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");
 703   req.tv_sec = 0;
 704   req.tv_nsec = ns;
 705   ::nanosleep(&req, NULL);
 706   return;
 707 }
 708 
 709 void os::naked_short_sleep(jlong ms) {
 710   assert(ms < MILLIUNITS, "Un-interruptable sleep, short time use only");
 711   os::naked_short_nanosleep(ms * (NANOUNITS / MILLIUNITS));
 712   return;
 713 }
 714 
 715 ////////////////////////////////////////////////////////////////////////////////
 716 // interrupt support
 717 
 718 void os::interrupt(Thread* thread) {


1433       outbuf[outbuflen - 1] = '\0';
1434       p = ::realpath(filename, outbuf);
1435       if (p != NULL) {
1436         guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected.");
1437         result = p;
1438       }
1439     }
1440   }
1441   return result;
1442 
1443 }
1444 
1445 int os::stat(const char *path, struct stat *sbuf) {
1446   return ::stat(path, sbuf);
1447 }
1448 
1449 char * os::native_path(char *path) {
1450   return path;
1451 }
1452 
























1453 // Check minimum allowable stack sizes for thread creation and to initialize
1454 // the java system classes, including StackOverflowError - depends on page
1455 // size.
1456 // The space needed for frames during startup is platform dependent. It
1457 // depends on word size, platform calling conventions, C frame layout and
1458 // interpreter/C1/C2 design decisions. Therefore this is given in a
1459 // platform (os/cpu) dependent constant.
1460 // To this, space for guard mechanisms is added, which depends on the
1461 // page size which again depends on the concrete system the VM is running
1462 // on. Space for libc guard pages is not included in this size.
1463 jint os::Posix::set_minimum_stack_sizes() {
1464   size_t os_min_stack_allowed = SOLARIS_ONLY(thr_min_stack()) NOT_SOLARIS(PTHREAD_STACK_MIN);
1465 
1466   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
1467                                    JavaThread::stack_guard_zone_size() +
1468                                    JavaThread::stack_shadow_zone_size();
1469 
1470   _java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());
1471   _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);
1472 


1654 // but otherwise whatever default is used by the platform - generally the
1655 // time-of-day clock.
1656 static pthread_condattr_t _condAttr[1];
1657 
1658 // Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not
1659 // all systems (e.g. FreeBSD) map the default to "normal".
1660 static pthread_mutexattr_t _mutexAttr[1];
1661 
1662 // common basic initialization that is always supported
1663 static void pthread_init_common(void) {
1664   int status;
1665   if ((status = pthread_condattr_init(_condAttr)) != 0) {
1666     fatal("pthread_condattr_init: %s", os::strerror(status));
1667   }
1668   if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) {
1669     fatal("pthread_mutexattr_init: %s", os::strerror(status));
1670   }
1671   if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) {
1672     fatal("pthread_mutexattr_settype: %s", os::strerror(status));
1673   }
1674   // Solaris has it's own PlatformMonitor, distinct from the one for POSIX.
1675   NOT_SOLARIS(os::PlatformMonitor::init();)
1676 }
1677 
1678 #ifndef SOLARIS
1679 sigset_t sigs;
1680 struct sigaction sigact[NSIG];
1681 
1682 struct sigaction* os::Posix::get_preinstalled_handler(int sig) {
1683   if (sigismember(&sigs, sig)) {
1684     return &sigact[sig];
1685   }
1686   return NULL;
1687 }
1688 
1689 void os::Posix::save_preinstalled_handler(int sig, struct sigaction& oldAct) {
1690   assert(sig > 0 && sig < NSIG, "vm signal out of expected range");
1691   sigact[sig] = oldAct;
1692   sigaddset(&sigs, sig);
1693 }
1694 #endif
1695 


2233   // must capture correct index before unlocking
2234   int index = _cur_index;
2235   status = pthread_mutex_unlock(_mutex);
2236   assert_status(status == 0, status, "invariant");
2237 
2238   // Note that we signal() *after* dropping the lock for "immortal" Events.
2239   // This is safe and avoids a common class of futile wakeups.  In rare
2240   // circumstances this can cause a thread to return prematurely from
2241   // cond_{timed}wait() but the spurious wakeup is benign and the victim
2242   // will simply re-test the condition and re-park itself.
2243   // This provides particular benefit if the underlying platform does not
2244   // provide wait morphing.
2245 
2246   if (s < 1 && index != -1) {
2247     // thread is definitely parked
2248     status = pthread_cond_signal(&_cond[index]);
2249     assert_status(status == 0, status, "invariant");
2250   }
2251 }
2252 
2253 // Platform Monitor implementation


2254 
2255 os::PlatformMonitor::Impl::Impl() : _next(NULL) {
2256   int status = pthread_cond_init(&_cond, _condAttr);
2257   assert_status(status == 0, status, "cond_init");
2258   status = pthread_mutex_init(&_mutex, _mutexAttr);
2259   assert_status(status == 0, status, "mutex_init");
2260 }
2261 
2262 os::PlatformMonitor::Impl::~Impl() {
2263   int status = pthread_cond_destroy(&_cond);
2264   assert_status(status == 0, status, "cond_destroy");
2265   status = pthread_mutex_destroy(&_mutex);
2266   assert_status(status == 0, status, "mutex_destroy");
2267 }
2268 
2269 #if PLATFORM_MONITOR_IMPL_INDIRECT
2270 
2271 pthread_mutex_t os::PlatformMonitor::_freelist_lock;
2272 os::PlatformMonitor::Impl* os::PlatformMonitor::_freelist = NULL;
2273 
2274 void os::PlatformMonitor::init() {
2275   int status = pthread_mutex_init(&_freelist_lock, _mutexAttr);
2276   assert_status(status == 0, status, "freelist lock init");
2277 }
2278 
2279 struct os::PlatformMonitor::WithFreeListLocked : public StackObj {
2280   WithFreeListLocked() {
2281     int status = pthread_mutex_lock(&_freelist_lock);
2282     assert_status(status == 0, status, "freelist lock");
2283   }
2284 
2285   ~WithFreeListLocked() {
2286     int status = pthread_mutex_unlock(&_freelist_lock);
2287     assert_status(status == 0, status, "freelist unlock");
2288   }
2289 };
2290 
































2291 os::PlatformMonitor::PlatformMonitor() {
2292   {
2293     WithFreeListLocked wfl;
2294     _impl = _freelist;
2295     if (_impl != NULL) {
2296       _freelist = _impl->_next;
2297       _impl->_next = NULL;
2298       return;
2299     }
2300   }
2301   _impl = new Impl();
2302 }
2303 
2304 os::PlatformMonitor::~PlatformMonitor() {
2305   WithFreeListLocked wfl;
2306   assert(_impl->_next == NULL, "invariant");
2307   _impl->_next = _freelist;
2308   _freelist = _impl;






















2309 }
2310 
2311 #endif // PLATFORM_MONITOR_IMPL_INDIRECT
2312 
2313 // Must already be locked
2314 int os::PlatformMonitor::wait(jlong millis) {
2315   assert(millis >= 0, "negative timeout");
2316   if (millis > 0) {
2317     struct timespec abst;
2318     // We have to watch for overflow when converting millis to nanos,
2319     // but if millis is that large then we will end up limiting to
2320     // MAX_SECS anyway, so just do that here.
2321     if (millis / MILLIUNITS > MAX_SECS) {
2322       millis = jlong(MAX_SECS) * MILLIUNITS;
2323     }
2324     to_abstime(&abst, millis * (NANOUNITS / MILLIUNITS), false, false);
2325 
2326     int ret = OS_TIMEOUT;
2327     int status = pthread_cond_timedwait(cond(), mutex(), &abst);
2328     assert_status(status == 0 || status == ETIMEDOUT,
< prev index next >