Don't let Display.sync* sleep with the global lock held
This commit is contained in:
parent
7e05654cd2
commit
bc09bd11e3
|
@ -351,16 +351,19 @@ public final class Display {
|
||||||
* @param fps The desired frame rate, in frames per second
|
* @param fps The desired frame rate, in frames per second
|
||||||
*/
|
*/
|
||||||
public static void sync3(int fps) {
|
public static void sync3(int fps) {
|
||||||
|
long savedTimeThen;
|
||||||
|
long timeNow;
|
||||||
synchronized (GlobalLock.lock) {
|
synchronized (GlobalLock.lock) {
|
||||||
float frameTime = 1.0f / (fps > 1 ? fps - 1 : 1);
|
timeNow = Sys.getTime();
|
||||||
long timeNow = Sys.getTime();
|
savedTimeThen = timeThen;
|
||||||
while (timeNow > timeThen && (float) (timeNow - timeThen) / (float) Sys.getTimerResolution() < frameTime) {
|
|
||||||
// This is a system-friendly way of allowing other stuff to use CPU if it wants to
|
|
||||||
Thread.yield();
|
|
||||||
timeNow = Sys.getTime();
|
|
||||||
}
|
|
||||||
timeThen = timeNow;
|
timeThen = timeNow;
|
||||||
}
|
}
|
||||||
|
float frameTime = 1.0f / (fps > 1 ? fps - 1 : 1);
|
||||||
|
while (timeNow > savedTimeThen && (float) (timeNow - savedTimeThen) / (float) Sys.getTimerResolution() < frameTime) {
|
||||||
|
// This is a system-friendly way of allowing other stuff to use CPU if it wants to
|
||||||
|
Thread.yield();
|
||||||
|
timeNow = Sys.getTime();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long timeLate;
|
private static long timeLate;
|
||||||
|
@ -371,15 +374,21 @@ public final class Display {
|
||||||
* @param fps The desired frame rate, in frames per second
|
* @param fps The desired frame rate, in frames per second
|
||||||
*/
|
*/
|
||||||
public static void sync2(int fps) {
|
public static void sync2(int fps) {
|
||||||
|
long timeNow;
|
||||||
|
long gapTo;
|
||||||
|
long savedTimeLate;
|
||||||
synchronized (GlobalLock.lock) {
|
synchronized (GlobalLock.lock) {
|
||||||
long gapTo = Sys.getTimerResolution() / fps + timeThen;
|
gapTo = Sys.getTimerResolution() / fps + timeThen;
|
||||||
long timeNow = Sys.getTime();
|
timeNow = Sys.getTime();
|
||||||
|
savedTimeLate = timeLate;
|
||||||
|
}
|
||||||
|
|
||||||
while (gapTo > timeNow + timeLate) {
|
while (gapTo > timeNow + savedTimeLate) {
|
||||||
Thread.yield();
|
Thread.yield();
|
||||||
timeNow = Sys.getTime();
|
timeNow = Sys.getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized (GlobalLock.lock) {
|
||||||
if (gapTo < timeNow)
|
if (gapTo < timeNow)
|
||||||
timeLate = timeNow - gapTo;
|
timeLate = timeNow - gapTo;
|
||||||
else
|
else
|
||||||
|
@ -395,18 +404,24 @@ public final class Display {
|
||||||
* @param fps The desired frame rate, in frames per second
|
* @param fps The desired frame rate, in frames per second
|
||||||
*/
|
*/
|
||||||
public static void sync(int fps) {
|
public static void sync(int fps) {
|
||||||
|
long timeNow;
|
||||||
|
long gapTo;
|
||||||
|
long savedTimeLate;
|
||||||
synchronized (GlobalLock.lock) {
|
synchronized (GlobalLock.lock) {
|
||||||
long gapTo = Sys.getTimerResolution() / fps + timeThen;
|
gapTo = Sys.getTimerResolution() / fps + timeThen;
|
||||||
long timeNow = Sys.getTime();
|
timeNow = Sys.getTime();
|
||||||
|
savedTimeLate = timeLate;
|
||||||
|
}
|
||||||
|
|
||||||
while (gapTo > timeNow + timeLate) {
|
while (gapTo > timeNow + savedTimeLate) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1);
|
Thread.sleep(1);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
}
|
|
||||||
timeNow = Sys.getTime();
|
|
||||||
}
|
}
|
||||||
|
timeNow = Sys.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (GlobalLock.lock) {
|
||||||
if (gapTo < timeNow)
|
if (gapTo < timeNow)
|
||||||
timeLate = timeNow - gapTo;
|
timeLate = timeNow - gapTo;
|
||||||
else
|
else
|
||||||
|
@ -415,7 +430,7 @@ public final class Display {
|
||||||
timeThen = timeNow;
|
timeThen = timeNow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the X coordinate of the window (always 0 for fullscreen)
|
* @return the X coordinate of the window (always 0 for fullscreen)
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue