Fixed worker pool AsyncTask storage

This commit is contained in:
Shoghi Cervantes 2014-07-05 22:02:14 +02:00
parent 22552cdd72
commit 637d9da5a6
2 changed files with 17 additions and 10 deletions

View File

@ -1349,7 +1349,7 @@ class Level implements ChunkManager, Metadatable{
}else{
$task = new ChunkRequestTask($this, $x, $z);
$this->server->getScheduler()->scheduleAsyncTask($task);
$this->chunkSendTasks[$index] = $task;
$this->chunkSendTasks[$index] = true;
}
}
}

View File

@ -45,6 +45,9 @@ class ServerScheduler{
protected $asyncTasks = 0;
/** @var AsyncTask[] */
protected $asyncTaskStorage = [];
/** @var int */
private $ids = 1;
@ -75,6 +78,7 @@ class ServerScheduler{
*/
public function scheduleAsyncTask(AsyncTask $task){
$this->asyncPool->submit($task);
$this->asyncTaskStorage[spl_object_hash($task)] = $task;
++$this->asyncTasks;
}
@ -214,18 +218,21 @@ class ServerScheduler{
}
if($this->asyncTasks > 0){ //Garbage collector
$this->asyncPool->collect(function (AsyncTask $task){
if($task->isFinished() and !$task->isCompleted()){
--$this->asyncTasks;
$task->onCompletion(Server::getInstance());
$task->setCompleted();
return true;
}
return false;
});
$this->asyncPool->collect([$this, "collectAsyncTask"]);
}
}
public function collectAsyncTask(AsyncTask $task){
if($task->isFinished() and !$task->isCompleted()){
--$this->asyncTasks;
$task->onCompletion(Server::getInstance());
$task->setCompleted();
unset($this->asyncTaskStorage[spl_object_hash($task)]);
return true;
}
return false;
}
private function isReady($currentTicks){
return count($this->tasks) > 0 and $this->queue->current()->getNextRun() <= $currentTicks;
}