3556 void os::naked_short_sleep(jlong ms) { |
3556 void os::naked_short_sleep(jlong ms) { |
3557 assert(ms < 1000, "Un-interruptable sleep, short time use only"); |
3557 assert(ms < 1000, "Un-interruptable sleep, short time use only"); |
3558 Sleep(ms); |
3558 Sleep(ms); |
3559 } |
3559 } |
3560 |
3560 |
|
3561 // Windows does not provide sleep functionality with nanosecond resolution, so we |
|
3562 // try to approximate this with spinning combined with yielding if another thread |
|
3563 // is ready to run on the current processor. |
3561 void os::naked_short_nanosleep(jlong ns) { |
3564 void os::naked_short_nanosleep(jlong ns) { |
3562 assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only"); |
3565 assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only"); |
3563 LARGE_INTEGER hundreds_nanos = { 0 }; |
3566 |
3564 HANDLE wait_timer = ::CreateWaitableTimer(NULL /* attributes*/, |
3567 int64_t start = os::javaTimeNanos(); |
3565 true /* manual reset */, |
3568 do { |
3566 NULL /* name */ ); |
3569 if (SwitchToThread() == 0) { |
3567 if (wait_timer == NULL) { |
3570 // Nothing else is ready to run on this cpu, spin a little |
3568 log_warning(os)("Failed to CreateWaitableTimer: %u", GetLastError()); |
3571 SpinPause(); |
3569 return; |
3572 } |
3570 } |
3573 } while (os::javaTimeNanos() - start < ns); |
3571 |
|
3572 // We need a minimum of one hundred nanos. |
|
3573 ns = ns > 100 ? ns : 100; |
|
3574 |
|
3575 // Round ns to the nearst hundred of nanos. |
|
3576 // Negative values indicate relative time. |
|
3577 hundreds_nanos.QuadPart = -((ns + 50) / 100); |
|
3578 |
|
3579 if (::SetWaitableTimer(wait_timer /* handle */, |
|
3580 &hundreds_nanos /* due time */, |
|
3581 0 /* period */, |
|
3582 NULL /* comp func */, |
|
3583 NULL /* comp func args */, |
|
3584 FALSE /* resume */)) { |
|
3585 DWORD res = ::WaitForSingleObject(wait_timer /* handle */, INFINITE /* timeout */); |
|
3586 if (res != WAIT_OBJECT_0) { |
|
3587 if (res == WAIT_FAILED) { |
|
3588 log_warning(os)("Failed to WaitForSingleObject: %u", GetLastError()); |
|
3589 } else { |
|
3590 log_warning(os)("Unexpected return from WaitForSingleObject: %s", |
|
3591 res == WAIT_ABANDONED ? "WAIT_ABANDONED" : "WAIT_TIMEOUT"); |
|
3592 } |
|
3593 } |
|
3594 } |
|
3595 ::CloseHandle(wait_timer /* handle */); |
|
3596 } |
3574 } |
3597 |
3575 |
3598 // Sleep forever; naked call to OS-specific sleep; use with CAUTION |
3576 // Sleep forever; naked call to OS-specific sleep; use with CAUTION |
3599 void os::infinite_sleep() { |
3577 void os::infinite_sleep() { |
3600 while (true) { // sleep forever ... |
3578 while (true) { // sleep forever ... |