summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/bukkit
diff options
context:
space:
mode:
authorWesley Wolfe <weswolf@aol.com>2012-10-07 15:08:21 -0500
committerWesley Wolfe <weswolf@aol.com>2012-10-14 17:26:53 -0500
commit05e889f3468b07b2897d64ee1df3e26e763408b1 (patch)
treedb61d53a6e329fbd5185bf2f2475fdf7aab3d5b0 /src/main/java/org/bukkit
parent93a79cd0e646318ee23db6842cbba0acb107c389 (diff)
downloadcraftbukkit-05e889f3468b07b2897d64ee1df3e26e763408b1.tar
craftbukkit-05e889f3468b07b2897d64ee1df3e26e763408b1.tar.gz
craftbukkit-05e889f3468b07b2897d64ee1df3e26e763408b1.tar.lz
craftbukkit-05e889f3468b07b2897d64ee1df3e26e763408b1.tar.xz
craftbukkit-05e889f3468b07b2897d64ee1df3e26e763408b1.zip
Queue tasks from secondary threads. Fixes BUKKIT-2546 and BUKKIT-2600
This change affects the old chat compatibility layer from an implementation only standpoint. It does not queue the 'event' to fire, but rather queues a runnable that allows the calling thread to wait for execution to finish. The other effect of this change is that rcon connects now have their commands queued to be run on next server tick using the same implementation. The internal implementation is in org.bukkit.craftbukkit.util.Waitable. It is very similar to a Future<T> task, but only contains minimal implementation with object.wait() and object.notify() calls under the hood of waitable.get() and waitable.run(). PlayerPreLoginEvent now properly implements thread-safe event execution by queuing the events similar to chat and rcon. This is still a poor way albeit proper way to implement thread-safety; PlayerPreLoginEvent will stay deprecated.
Diffstat (limited to 'src/main/java/org/bukkit')
-rw-r--r--src/main/java/org/bukkit/craftbukkit/util/Waitable.java46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/util/Waitable.java b/src/main/java/org/bukkit/craftbukkit/util/Waitable.java
new file mode 100644
index 00000000..5cd11543
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/util/Waitable.java
@@ -0,0 +1,46 @@
+package org.bukkit.craftbukkit.util;
+
+import java.util.concurrent.ExecutionException;
+
+
+public abstract class Waitable<T> implements Runnable {
+ private enum Status {
+ WAITING,
+ RUNNING,
+ FINISHED,
+ }
+ Throwable t = null;
+ T value = null;
+ Status status = Status.WAITING;
+
+ public final void run() {
+ synchronized (this) {
+ if (status != Status.WAITING) {
+ throw new IllegalStateException("Invalid state " + status);
+ }
+ status = Status.RUNNING;
+ }
+ try {
+ value = evaluate();
+ } catch (Throwable t) {
+ this.t = t;
+ } finally {
+ synchronized (this) {
+ status = Status.FINISHED;
+ this.notifyAll();
+ }
+ }
+ }
+
+ protected abstract T evaluate();
+
+ public synchronized T get() throws InterruptedException, ExecutionException {
+ while (status != Status.FINISHED) {
+ this.wait();
+ }
+ if (t != null) {
+ throw new ExecutionException(t);
+ }
+ return value;
+ }
+}