From 7f85e37540953b390875020e2356191d06b6ad3e Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sat, 11 Oct 2014 16:36:38 +0200 Subject: [PATCH] Worked on scheduler --- src/pocketmine/level/format/LevelProvider.php | 2 +- .../level/format/anvil/ChunkRequestTask.php | 1 - src/pocketmine/scheduler/AsyncTask.php | 25 ++++--------------- src/pocketmine/scheduler/ServerScheduler.php | 25 +++++++++---------- 4 files changed, 18 insertions(+), 35 deletions(-) diff --git a/src/pocketmine/level/format/LevelProvider.php b/src/pocketmine/level/format/LevelProvider.php index 2cb2879ca..bce3f4a10 100644 --- a/src/pocketmine/level/format/LevelProvider.php +++ b/src/pocketmine/level/format/LevelProvider.php @@ -58,7 +58,7 @@ interface LevelProvider{ * @param int $x * @param int $z * - * @return \pocketmine\scheduler\AsyncTask + * @return \pocketmine\scheduler\AsyncTask|null */ public function requestChunkTask($x, $z); diff --git a/src/pocketmine/level/format/anvil/ChunkRequestTask.php b/src/pocketmine/level/format/anvil/ChunkRequestTask.php index 1d5c8007e..9fa648009 100644 --- a/src/pocketmine/level/format/anvil/ChunkRequestTask.php +++ b/src/pocketmine/level/format/anvil/ChunkRequestTask.php @@ -82,7 +82,6 @@ class ChunkRequestTask extends AsyncTask{ $meta = ""; $blockLight = ""; $skyLight = ""; - $biomeColors = ""; foreach($this->sections as $section){ $ids .= $section->getIdArray(); diff --git a/src/pocketmine/scheduler/AsyncTask.php b/src/pocketmine/scheduler/AsyncTask.php index c1adfa1db..fe8e8c29a 100644 --- a/src/pocketmine/scheduler/AsyncTask.php +++ b/src/pocketmine/scheduler/AsyncTask.php @@ -26,24 +26,20 @@ use pocketmine\Server; /** * Class used to run async tasks in other threads. * - * WARNING: Do not call PocketMine-MP API methods from other Threads!! + * WARNING: Do not call PocketMine-MP API methods, or save objects from/on other Threads!! */ -abstract class AsyncTask extends \Threaded{ +abstract class AsyncTask extends \Collectable{ - protected $complete = null; - protected $finished = null; - protected $result = null; - protected $taskId = null; + private $finished = null; + private $result = null; + private $taskId = null; public function run(){ $this->finished = false; - $this->complete = false; $this->result = null; $this->onRun(); - $this->finished = true; - } /** @@ -53,17 +49,6 @@ abstract class AsyncTask extends \Threaded{ return $this->finished === true; } - /** - * @return bool - */ - public function isCompleted(){ - return $this->complete === true; - } - - public function setCompleted(){ - $this->complete = true; - } - /** * @return mixed */ diff --git a/src/pocketmine/scheduler/ServerScheduler.php b/src/pocketmine/scheduler/ServerScheduler.php index 696ac2a7e..5ad84a392 100644 --- a/src/pocketmine/scheduler/ServerScheduler.php +++ b/src/pocketmine/scheduler/ServerScheduler.php @@ -43,11 +43,11 @@ class ServerScheduler{ /** @var \Pool */ protected $asyncPool; - protected $asyncTasks = 0; - /** @var AsyncTask[] */ protected $asyncTaskStorage = []; + protected $asyncTasks = 0; + /** @var int */ private $ids = 1; @@ -69,8 +69,7 @@ class ServerScheduler{ } /** - * Submits a asynchronous task to the Pool - * If the AsyncTask sets a result, you have to get it so it can be deleted + * Submits an asynchronous task to the Worker Pool * * @param AsyncTask $task * @@ -80,7 +79,7 @@ class ServerScheduler{ $id = $this->nextId(); $task->setTaskId($id); $this->asyncPool->submit($task); - $this->asyncTaskStorage[$id] = $task; + $this->asyncTaskStorage[$task->getTaskId()] = $task; ++$this->asyncTasks; } @@ -143,6 +142,7 @@ class ServerScheduler{ $task->cancel(); } $this->tasks = []; + $this->asyncTaskStorage = []; $this->asyncPool->shutdown(); $this->asyncTasks = 0; $this->queue = new ReversePriorityQueue(); @@ -246,25 +246,24 @@ class ServerScheduler{ if($this->asyncTasks > 0){ //Garbage collector $this->asyncPool->collect([$this, "collectAsyncTask"]); - foreach($this->asyncTaskStorage as $asyncTask){ - if($asyncTask->isFinished() and !$asyncTask->isCompleted()){ - $this->collectAsyncTask($asyncTask); + if($this->asyncTasks > 0){ + //TODO: remove this workaround + foreach($this->asyncTaskStorage as $task){ + $this->collectAsyncTask($task); } } } } public function collectAsyncTask(AsyncTask $task){ - if($task->isFinished() and !$task->isCompleted()){ + if($task->isFinished() and !$task->isGarbage()){ --$this->asyncTasks; $task->onCompletion(Server::getInstance()); - $task->setCompleted(); + $task->setGarbage(); unset($this->asyncTaskStorage[$task->getTaskId()]); - - return true; } - return false; + return $task->isGarbage(); } private function isReady($currentTicks){