< prev index next >

src/hotspot/share/runtime/os.cpp

Print this page




1831   os::SuspendResume::State result = Atomic::cmpxchg(to, &_state, from);
1832   if (result == from) {
1833     // success
1834     return to;
1835   }
1836   return result;
1837 }
1838 #endif
1839 
1840 // Convenience wrapper around naked_short_sleep to allow for longer sleep
1841 // times. Only for use by non-JavaThreads.
1842 void os::naked_sleep(jlong millis) {
1843   assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
1844   const jlong limit = 999;
1845   while (millis > limit) {
1846     naked_short_sleep(limit);
1847     millis -= limit;
1848   }
1849   naked_short_sleep(millis);
1850 }
1851 
1852 int os::sleep(JavaThread* thread, jlong millis) {
1853   assert(thread == Thread::current(),  "thread consistency check");
1854 
1855   ParkEvent * const slp = thread->_SleepEvent;
1856   // Because there can be races with thread interruption sending an unpark()
1857   // to the event, we explicitly reset it here to avoid an immediate return.
1858   // The actual interrupt state will be checked before we park().
1859   slp->reset();
1860   // Thread interruption establishes a happens-before ordering in the
1861   // Java Memory Model, so we need to ensure we synchronize with the
1862   // interrupt state.
1863   OrderAccess::fence();
1864 
1865   jlong prevtime = javaTimeNanos();
1866 
1867   for (;;) {
1868     // interruption has precedence over timing out
1869     if (os::is_interrupted(thread, true)) {
1870       return OS_INTRPT;
1871     }
1872 
1873     jlong newtime = javaTimeNanos();
1874 
1875     if (newtime - prevtime < 0) {
1876       // time moving backwards, should only happen if no monotonic clock
1877       // not a guarantee() because JVM should not abort on kernel/glibc bugs
1878       assert(!os::supports_monotonic_clock(),
1879              "unexpected time moving backwards detected in os::sleep()");
1880     } else {
1881       millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
1882     }
1883 
1884     if (millis <= 0) {
1885       return OS_OK;
1886     }
1887 
1888     prevtime = newtime;
1889 
1890     {
1891       ThreadBlockInVM tbivm(thread);
1892       OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
1893 
1894       thread->set_suspend_equivalent();
1895       // cleared by handle_special_suspend_equivalent_condition() or
1896       // java_suspend_self() via check_and_wait_while_suspended()
1897 
1898       slp->park(millis);
1899 
1900       // were we externally suspended while we were waiting?
1901       thread->check_and_wait_while_suspended();
1902     }
1903   }
1904 }


1831   os::SuspendResume::State result = Atomic::cmpxchg(to, &_state, from);
1832   if (result == from) {
1833     // success
1834     return to;
1835   }
1836   return result;
1837 }
1838 #endif
1839 
1840 // Convenience wrapper around naked_short_sleep to allow for longer sleep
1841 // times. Only for use by non-JavaThreads.
1842 void os::naked_sleep(jlong millis) {
1843   assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
1844   const jlong limit = 999;
1845   while (millis > limit) {
1846     naked_short_sleep(limit);
1847     millis -= limit;
1848   }
1849   naked_short_sleep(millis);
1850 }






















































< prev index next >