mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-05 17:36:12 +00:00
This commit is contained in:
@ -29,22 +29,18 @@ use pocketmine\Server;
|
||||
*/
|
||||
abstract class AsyncTask extends \Threaded{
|
||||
|
||||
private $complete = null;
|
||||
private $finished = null;
|
||||
private $result = null;
|
||||
public $complete = null;
|
||||
public $finished = null;
|
||||
public $result = null;
|
||||
|
||||
public function run(){
|
||||
$this->lock();
|
||||
$this->finished = false;
|
||||
$this->complete = false;
|
||||
$this->result = null;
|
||||
$this->unlock();
|
||||
|
||||
$this->onRun();
|
||||
|
||||
$this->lock();
|
||||
$this->finished = true;
|
||||
$this->unlock();
|
||||
|
||||
}
|
||||
|
||||
@ -52,7 +48,6 @@ abstract class AsyncTask extends \Threaded{
|
||||
* @return bool
|
||||
*/
|
||||
public function isFinished(){
|
||||
|
||||
return $this->finished === true;
|
||||
}
|
||||
|
||||
@ -88,6 +83,14 @@ abstract class AsyncTask extends \Threaded{
|
||||
$this->result = @serialize($result);
|
||||
}
|
||||
|
||||
public function setTaskId($taskId){
|
||||
$this->taskId = $taskId;
|
||||
}
|
||||
|
||||
public function getTaskId(){
|
||||
return $this->taskId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actions to execute when run
|
||||
*
|
||||
|
@ -77,8 +77,10 @@ class ServerScheduler{
|
||||
* @return void
|
||||
*/
|
||||
public function scheduleAsyncTask(AsyncTask $task){
|
||||
$id = $this->nextId();
|
||||
$task->setTaskId($id);
|
||||
$this->asyncPool->submit($task);
|
||||
$this->asyncTaskStorage[spl_object_hash($task)] = $task;
|
||||
$this->asyncTaskStorage[$id] = $task;
|
||||
++$this->asyncTasks;
|
||||
}
|
||||
|
||||
@ -222,6 +224,12 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -230,7 +238,7 @@ class ServerScheduler{
|
||||
--$this->asyncTasks;
|
||||
$task->onCompletion(Server::getInstance());
|
||||
$task->setCompleted();
|
||||
unset($this->asyncTaskStorage[spl_object_hash($task)]);
|
||||
unset($this->asyncTaskStorage[$task->getTaskId()]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user