From bea6a1389655c2951eb765840a4b51eba8f67c15 Mon Sep 17 00:00:00 2001 From: bdew Date: Thu, 21 May 2015 18:05:33 +0300 Subject: [PATCH] Added async helpers and ServerTickExecutionContext (executes futures in server thread using tick event) --- src/net/bdew/lib/async/Async.scala | 30 +++++++++++ .../async/ServerTickExecutionContext.scala | 53 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/net/bdew/lib/async/Async.scala create mode 100644 src/net/bdew/lib/async/ServerTickExecutionContext.scala diff --git a/src/net/bdew/lib/async/Async.scala b/src/net/bdew/lib/async/Async.scala new file mode 100644 index 0000000..b687afa --- /dev/null +++ b/src/net/bdew/lib/async/Async.scala @@ -0,0 +1,30 @@ +/* + * Copyright (c) bdew, 2013 - 2015 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://bdew.net/minecraft-mod-public-license/ + */ + +package net.bdew.lib.async + +import scala.concurrent.{Future, Promise} + +object Async { + def inOtherThread[T](f: => T) = { + val p = Promise[T]() + val t = new Thread(new Runnable { + override def run(): Unit = + try { + p.success(f) + } catch { + case t: Throwable => p.failure(t) + } + }) + t.start() + p.future + } + + def inServerThread[T](f: => T) = Future(f)(ServerTickExecutionContext) +} diff --git a/src/net/bdew/lib/async/ServerTickExecutionContext.scala b/src/net/bdew/lib/async/ServerTickExecutionContext.scala new file mode 100644 index 0000000..89ade08 --- /dev/null +++ b/src/net/bdew/lib/async/ServerTickExecutionContext.scala @@ -0,0 +1,53 @@ +/* + * Copyright (c) bdew, 2013 - 2015 + * https://github.com/bdew/bdlib + * + * This mod is distributed under the terms of the Minecraft Mod Public + * License 1.0, or MMPL. Please check the contents of the license located in + * http://bdew.net/minecraft-mod-public-license/ + */ + +package net.bdew.lib.async + +import cpw.mods.fml.common.FMLCommonHandler +import cpw.mods.fml.common.eventhandler.SubscribeEvent +import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent +import net.bdew.lib.BdLib + +import scala.collection.mutable +import scala.concurrent.ExecutionContext + +object ServerTickExecutionContext extends ExecutionContext { + private val queue = mutable.Queue.empty[Runnable] + + FMLCommonHandler.instance.bus.register(this) + + BdLib.logDebug("Installed ServerTickExecutionContext") + + private def getNextBatch = this.synchronized { + if (queue.nonEmpty) { + val res = queue.toList + queue.clear() + res + } else List.empty + } + + override def execute(runnable: Runnable): Unit = + this.synchronized { + queue += runnable + } + + override def reportFailure(cause: Throwable): Unit = + BdLib.logErrorException("Error in execution context", cause) + + @SubscribeEvent + def onTick(ev: ServerTickEvent): Unit = { + for (r <- getNextBatch) { + try { + r.run() + } catch { + case t: Throwable => reportFailure(t) + } + } + } +}