v0.4.2: switch wait/notify to busy wait

This solves a race condition, yes using lock/semaphore would do this too, but that is a considerable amount of complexity for what amounts to an extremely simple bit of blocking logic. busy-wait works just as well and is insignificantly more expensive with the use of thread.sleep.
This commit is contained in:
Evan Debenham 2016-08-27 13:28:11 -04:00 committed by Evan Debenham
parent 22585dd223
commit 8d7eac940b
3 changed files with 8 additions and 11 deletions

View File

@ -204,8 +204,11 @@ public abstract class Actor implements Bundlable {
// If it's character's turn to act, but its sprite
// is moving, wait till the movement is over
try {
synchronized (((Char)acting).sprite) {
((Char) acting).sprite.wait();
//yes, we're busy-waiting. This is insignificantly slower than using
//a lock/semaphore but results in more readable code.
while (((Char)acting).sprite.isMoving) {
//tries every ~0.1 milliseconds
Thread.sleep(0, 100000);
}
} catch (InterruptedException e) {
ShatteredPixelDungeon.reportException(e);

View File

@ -453,14 +453,11 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip
} else if (tweener == motion) {
isMoving = false;
synchronized (this){
notify();
}
motion.killAndErase();
motion = null;
ch.onMotionComplete();
isMoving = false;
}
}

View File

@ -102,11 +102,8 @@ public class TenguSprite extends MobSprite {
@Override
public void onComplete( Animation anim ) {
if (anim == run) {
isMoving = false;
synchronized (this){
notify();
}
idle();
isMoving = false;
} else {
super.onComplete( anim );
}