src/hotspot/share/runtime/os.cpp
changeset 58095 adc72cd1d1f2
parent 58084 cddef3bde924
child 58282 03fce7b04b42
equal deleted inserted replaced
58094:0f6c749acd15 58095:adc72cd1d1f2
  1842     naked_short_sleep(limit);
  1842     naked_short_sleep(limit);
  1843     millis -= limit;
  1843     millis -= limit;
  1844   }
  1844   }
  1845   naked_short_sleep(millis);
  1845   naked_short_sleep(millis);
  1846 }
  1846 }
  1847 
       
  1848 int os::sleep(JavaThread* thread, jlong millis) {
       
  1849   assert(thread == Thread::current(),  "thread consistency check");
       
  1850 
       
  1851   ParkEvent * const slp = thread->_SleepEvent;
       
  1852   // Because there can be races with thread interruption sending an unpark()
       
  1853   // to the event, we explicitly reset it here to avoid an immediate return.
       
  1854   // The actual interrupt state will be checked before we park().
       
  1855   slp->reset();
       
  1856   // Thread interruption establishes a happens-before ordering in the
       
  1857   // Java Memory Model, so we need to ensure we synchronize with the
       
  1858   // interrupt state.
       
  1859   OrderAccess::fence();
       
  1860 
       
  1861   jlong prevtime = javaTimeNanos();
       
  1862 
       
  1863   for (;;) {
       
  1864     // interruption has precedence over timing out
       
  1865     if (os::is_interrupted(thread, true)) {
       
  1866       return OS_INTRPT;
       
  1867     }
       
  1868 
       
  1869     jlong newtime = javaTimeNanos();
       
  1870 
       
  1871     if (newtime - prevtime < 0) {
       
  1872       // time moving backwards, should only happen if no monotonic clock
       
  1873       // not a guarantee() because JVM should not abort on kernel/glibc bugs
       
  1874       assert(!os::supports_monotonic_clock(),
       
  1875              "unexpected time moving backwards detected in os::sleep()");
       
  1876     } else {
       
  1877       millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
       
  1878     }
       
  1879 
       
  1880     if (millis <= 0) {
       
  1881       return OS_OK;
       
  1882     }
       
  1883 
       
  1884     prevtime = newtime;
       
  1885 
       
  1886     {
       
  1887       ThreadBlockInVM tbivm(thread);
       
  1888       OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
       
  1889 
       
  1890       thread->set_suspend_equivalent();
       
  1891       // cleared by handle_special_suspend_equivalent_condition() or
       
  1892       // java_suspend_self() via check_and_wait_while_suspended()
       
  1893 
       
  1894       slp->park(millis);
       
  1895 
       
  1896       // were we externally suspended while we were waiting?
       
  1897       thread->check_and_wait_while_suspended();
       
  1898     }
       
  1899   }
       
  1900 }