AsyncPool: Remove Server dependency (API breaks included)

This brings two plugin-breaking changes: AsyncTask->onCompletion() and AsyncTask->onProgressUpdate() no longer accept Server parameters.

This now allows for the functionality of AsyncPool and AsyncTask to be tested outside of a Server.
This commit is contained in:
Dylan K. Taylor
2018-09-10 15:47:12 +01:00
parent 5ce2d5e072
commit d62e00cc74
11 changed files with 19 additions and 40 deletions

View File

@ -23,8 +23,6 @@ declare(strict_types=1);
namespace pocketmine\scheduler;
use pocketmine\Server;
/**
* Manages general-purpose worker threads used for processing asynchronous tasks, and the tasks submitted to those
* workers.
@ -32,9 +30,6 @@ use pocketmine\Server;
class AsyncPool{
private const WORKER_START_OPTIONS = PTHREADS_INHERIT_INI | PTHREADS_INHERIT_CONSTANTS;
/** @var Server */
private $server;
/** @var \ClassLoader */
private $classLoader;
/** @var \ThreadedLogger */
@ -59,8 +54,7 @@ class AsyncPool{
/** @var \Closure[] */
private $workerStartHooks = [];
public function __construct(Server $server, int $size, int $workerMemoryLimit, \ClassLoader $classLoader, \ThreadedLogger $logger){
$this->server = $server;
public function __construct(int $size, int $workerMemoryLimit, \ClassLoader $classLoader, \ThreadedLogger $logger){
$this->size = $size;
$this->workerMemoryLimit = $workerMemoryLimit;
$this->classLoader = $classLoader;
@ -259,7 +253,7 @@ class AsyncPool{
}
if(count($this->tasks) > 0){
Server::microSleep(25000);
usleep(25000);
}
}while(count($this->tasks) > 0);
@ -290,12 +284,12 @@ class AsyncPool{
public function collectTasks() : void{
foreach($this->tasks as $task){
if(!$task->isGarbage()){
$task->checkProgressUpdates($this->server);
$task->checkProgressUpdates();
}
if($task->isGarbage() and !$task->isRunning() and !$task->isCrashed()){
if(!$task->hasCancelledRun()){
try{
$task->onCompletion($this->server);
$task->onCompletion();
if($task->removeDanglingStoredObjects()){
$this->logger->notice("AsyncTask " . get_class($task) . " stored local complex data but did not remove them after completion");
}

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\scheduler;
use pocketmine\Collectable;
use pocketmine\Server;
/**
* Class used to run async tasks in other threads.
@ -174,11 +173,9 @@ abstract class AsyncTask extends Collectable{
* Actions to execute when completed (on main thread)
* Implement this if you want to handle the data in your AsyncTask after it has been processed
*
* @param Server $server
*
* @return void
*/
public function onCompletion(Server $server){
public function onCompletion(){
}
@ -194,13 +191,11 @@ abstract class AsyncTask extends Collectable{
/**
* @internal Only call from AsyncPool.php on the main thread
*
* @param Server $server
*/
public function checkProgressUpdates(Server $server){
public function checkProgressUpdates(){
while($this->progressUpdates->count() !== 0){
$progress = $this->progressUpdates->shift();
$this->onProgressUpdate($server, unserialize($progress));
$this->onProgressUpdate(unserialize($progress));
}
}
@ -209,11 +204,10 @@ abstract class AsyncTask extends Collectable{
* All {@link AsyncTask#publishProgress} calls should result in {@link AsyncTask#onProgressUpdate} calls before
* {@link AsyncTask#onCompletion} is called.
*
* @param Server $server
* @param mixed $progress The parameter passed to {@link AsyncTask#publishProgress}. It is serialize()'ed
* @param mixed $progress The parameter passed to {@link AsyncTask#publishProgress}. It is serialize()'ed
* and then unserialize()'ed, as if it has been cloned.
*/
public function onProgressUpdate(Server $server, $progress){
public function onProgressUpdate($progress){
}