From 8d7eac940bec88896ea0ecd977223ebe9ef41938 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Sat, 27 Aug 2016 13:28:11 -0400 Subject: [PATCH] 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. --- .../shatteredpixel/shatteredpixeldungeon/actors/Actor.java | 7 +++++-- .../shatteredpixeldungeon/sprites/CharSprite.java | 7 ++----- .../shatteredpixeldungeon/sprites/TenguSprite.java | 5 +---- 3 files changed, 8 insertions(+), 11 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 ffcdaf8a0..190de920d 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/Actor.java @@ -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); 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 26db8c256..5c40b4b93 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/CharSprite.java @@ -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; } } 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 eb455a25a..6b921952f 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/TenguSprite.java @@ -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 ); }