Skip to content

Commit

Permalink
Sleep fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed Dec 28, 2023
1 parent 9448783 commit 1d39d9b
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package net.emustudio.emulib.runtime.helpers;

import java.util.concurrent.locks.LockSupport;

/**
* Sleeping and time measurement utilities.
*/
Expand Down Expand Up @@ -60,13 +58,18 @@ public class SleepUtils {
*
* @param nanoDuration nanoseconds
*/
@SuppressWarnings("BusyWait")
public static void preciseSleepNanos(long nanoDuration) {
final long end = System.nanoTime() + nanoDuration;
long timeLeft = nanoDuration;

do {
if (timeLeft > SLEEP_PRECISION) {
LockSupport.parkNanos(timeLeft);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else if (timeLeft > SPIN_YIELD_PRECISION) {
Thread.onSpinWait();
}
Expand Down

0 comments on commit 1d39d9b

Please sign in to comment.