mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 16:00:20 +00:00
Worked on scheduler
This commit is contained in:
parent
341717c89d
commit
7f85e37540
@ -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);
|
||||
|
||||
|
@ -82,7 +82,6 @@ class ChunkRequestTask extends AsyncTask{
|
||||
$meta = "";
|
||||
$blockLight = "";
|
||||
$skyLight = "";
|
||||
$biomeColors = "";
|
||||
|
||||
foreach($this->sections as $section){
|
||||
$ids .= $section->getIdArray();
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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){
|
||||
|
Loading…
x
Reference in New Issue
Block a user