Worked on scheduler

This commit is contained in:
Shoghi Cervantes 2014-10-11 16:36:38 +02:00
parent 341717c89d
commit 7f85e37540
4 changed files with 18 additions and 35 deletions

View File

@ -58,7 +58,7 @@ interface LevelProvider{
* @param int $x * @param int $x
* @param int $z * @param int $z
* *
* @return \pocketmine\scheduler\AsyncTask * @return \pocketmine\scheduler\AsyncTask|null
*/ */
public function requestChunkTask($x, $z); public function requestChunkTask($x, $z);

View File

@ -82,7 +82,6 @@ class ChunkRequestTask extends AsyncTask{
$meta = ""; $meta = "";
$blockLight = ""; $blockLight = "";
$skyLight = ""; $skyLight = "";
$biomeColors = "";
foreach($this->sections as $section){ foreach($this->sections as $section){
$ids .= $section->getIdArray(); $ids .= $section->getIdArray();

View File

@ -26,24 +26,20 @@ use pocketmine\Server;
/** /**
* Class used to run async tasks in other threads. * 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; private $finished = null;
protected $finished = null; private $result = null;
protected $result = null; private $taskId = null;
protected $taskId = null;
public function run(){ public function run(){
$this->finished = false; $this->finished = false;
$this->complete = false;
$this->result = null; $this->result = null;
$this->onRun(); $this->onRun();
$this->finished = true; $this->finished = true;
} }
/** /**
@ -53,17 +49,6 @@ abstract class AsyncTask extends \Threaded{
return $this->finished === true; return $this->finished === true;
} }
/**
* @return bool
*/
public function isCompleted(){
return $this->complete === true;
}
public function setCompleted(){
$this->complete = true;
}
/** /**
* @return mixed * @return mixed
*/ */

View File

@ -43,11 +43,11 @@ class ServerScheduler{
/** @var \Pool */ /** @var \Pool */
protected $asyncPool; protected $asyncPool;
protected $asyncTasks = 0;
/** @var AsyncTask[] */ /** @var AsyncTask[] */
protected $asyncTaskStorage = []; protected $asyncTaskStorage = [];
protected $asyncTasks = 0;
/** @var int */ /** @var int */
private $ids = 1; private $ids = 1;
@ -69,8 +69,7 @@ class ServerScheduler{
} }
/** /**
* Submits a asynchronous task to the Pool * Submits an asynchronous task to the Worker Pool
* If the AsyncTask sets a result, you have to get it so it can be deleted
* *
* @param AsyncTask $task * @param AsyncTask $task
* *
@ -80,7 +79,7 @@ class ServerScheduler{
$id = $this->nextId(); $id = $this->nextId();
$task->setTaskId($id); $task->setTaskId($id);
$this->asyncPool->submit($task); $this->asyncPool->submit($task);
$this->asyncTaskStorage[$id] = $task; $this->asyncTaskStorage[$task->getTaskId()] = $task;
++$this->asyncTasks; ++$this->asyncTasks;
} }
@ -143,6 +142,7 @@ class ServerScheduler{
$task->cancel(); $task->cancel();
} }
$this->tasks = []; $this->tasks = [];
$this->asyncTaskStorage = [];
$this->asyncPool->shutdown(); $this->asyncPool->shutdown();
$this->asyncTasks = 0; $this->asyncTasks = 0;
$this->queue = new ReversePriorityQueue(); $this->queue = new ReversePriorityQueue();
@ -246,25 +246,24 @@ class ServerScheduler{
if($this->asyncTasks > 0){ //Garbage collector if($this->asyncTasks > 0){ //Garbage collector
$this->asyncPool->collect([$this, "collectAsyncTask"]); $this->asyncPool->collect([$this, "collectAsyncTask"]);
foreach($this->asyncTaskStorage as $asyncTask){ if($this->asyncTasks > 0){
if($asyncTask->isFinished() and !$asyncTask->isCompleted()){ //TODO: remove this workaround
$this->collectAsyncTask($asyncTask); foreach($this->asyncTaskStorage as $task){
$this->collectAsyncTask($task);
} }
} }
} }
} }
public function collectAsyncTask(AsyncTask $task){ public function collectAsyncTask(AsyncTask $task){
if($task->isFinished() and !$task->isCompleted()){ if($task->isFinished() and !$task->isGarbage()){
--$this->asyncTasks; --$this->asyncTasks;
$task->onCompletion(Server::getInstance()); $task->onCompletion(Server::getInstance());
$task->setCompleted(); $task->setGarbage();
unset($this->asyncTaskStorage[$task->getTaskId()]); unset($this->asyncTaskStorage[$task->getTaskId()]);
return true;
} }
return false; return $task->isGarbage();
} }
private function isReady($currentTicks){ private function isReady($currentTicks){