v0.4.2: returned to wait/notify with race condition fixes

thread.sleep has a bigger performance impact than anticipated, plus I figured out how to fix this while staying fairly clean.
This commit is contained in:
Evan Debenham 2016-08-28 16:26:17 -04:00 committed by Evan Debenham
parent 8dc0d56eb0
commit 1293607b42
3 changed files with 20 additions and 13 deletions

View File

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

View File

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

View File

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