From 1293607b42aa53367e71a76a02343e60de9f8cfd Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Sun, 28 Aug 2016 16:26:17 -0400 Subject: [PATCH] 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. --- .../shatteredpixeldungeon/actors/Actor.java | 12 +++++------- .../shatteredpixeldungeon/sprites/CharSprite.java | 13 +++++++++---- .../shatteredpixeldungeon/sprites/TenguSprite.java | 8 ++++++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java index 190de920d..4f7f82623 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java @@ -199,16 +199,14 @@ public abstract class Actor implements Bundlable { Actor acting = current; - if (acting instanceof Char && - ((Char) acting).sprite != null && ((Char)acting).sprite.isMoving) { + if (acting instanceof Char && ((Char) acting).sprite != null) { // If it's character's turn to act, but its sprite // is moving, wait till the movement is over try { - //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); + synchronized (((Char)acting).sprite) { + if (((Char)acting).sprite.isMoving) { + ((Char) acting).sprite.wait(); + } } } catch (InterruptedException e) { ShatteredPixelDungeon.reportException(e); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java index 5c40b4b93..90e2a2fd8 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java @@ -453,11 +453,16 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip } else if (tweener == motion) { - motion.killAndErase(); - motion = null; - ch.onMotionComplete(); + synchronized (this) { + isMoving = false; + + motion.killAndErase(); + motion = null; + ch.onMotionComplete(); + + notifyAll(); + } - isMoving = false; } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java index 6b921952f..686918331 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java @@ -102,8 +102,12 @@ public class TenguSprite extends MobSprite { @Override public void onComplete( Animation anim ) { if (anim == run) { - idle(); - isMoving = false; + synchronized (this){ + isMoving = false; + idle(); + + notifyAll(); + } } else { super.onComplete( anim ); }