Thread/Worker run() are now final, to set up common environment things

This commit is contained in:
Dylan K. Taylor 2019-06-06 16:44:32 +01:00
parent e07f3e8e65
commit 44c791f03e
5 changed files with 31 additions and 14 deletions

View File

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace pocketmine; namespace pocketmine;
use function error_reporting;
/** /**
* This class must be extended by all custom threading classes * This class must be extended by all custom threading classes
*/ */
@ -73,6 +75,19 @@ abstract class Thread extends \Thread{
return parent::start($options); return parent::start($options);
} }
final public function run() : void{
error_reporting(-1);
$this->registerClassLoader();
//set this after the autoloader is registered
\ErrorUtils::setErrorExceptionHandler();
$this->onRun();
}
/**
* Runs code on the thread.
*/
abstract protected function onRun() : void;
/** /**
* Stops the thread using the best way possible. Try to stop it yourself before calling this. * Stops the thread using the best way possible. Try to stop it yourself before calling this.
*/ */

View File

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace pocketmine; namespace pocketmine;
use function error_reporting;
/** /**
* This class must be extended by all custom threading classes * This class must be extended by all custom threading classes
*/ */
@ -73,6 +75,17 @@ abstract class Worker extends \Worker{
return parent::start($options); return parent::start($options);
} }
final public function run() : void{
error_reporting(-1);
$this->registerClassLoader();
$this->onRun();
}
/**
* Runs code on the thread.
*/
abstract protected function onRun() : void;
/** /**
* Stops the thread using the best way possible. Try to stop it yourself before calling this. * Stops the thread using the best way possible. Try to stop it yourself before calling this.
*/ */

View File

@ -184,9 +184,7 @@ class CommandReader extends Thread{
return null; return null;
} }
public function run() : void{ protected function onRun() : void{
$this->registerClassLoader();
if($this->type !== self::TYPE_READLINE){ if($this->type !== self::TYPE_READLINE){
$this->initStdin(); $this->initStdin();
} }

View File

@ -25,7 +25,6 @@ namespace pocketmine\scheduler;
use pocketmine\utils\MainLogger; use pocketmine\utils\MainLogger;
use pocketmine\Worker; use pocketmine\Worker;
use function error_reporting;
use function gc_enable; use function gc_enable;
use function ini_set; use function ini_set;
@ -45,14 +44,7 @@ class AsyncWorker extends Worker{
$this->memoryLimit = $memoryLimit; $this->memoryLimit = $memoryLimit;
} }
public function run() : void{ protected function onRun() : void{
error_reporting(-1);
$this->registerClassLoader();
//set this after the autoloader is registered
\ErrorUtils::setErrorExceptionHandler();
\GlobalLogger::set($this->logger); \GlobalLogger::set($this->logger);
if($this->logger instanceof MainLogger){ if($this->logger instanceof MainLogger){
$this->logger->registerStatic(); $this->logger->registerStatic();

View File

@ -38,8 +38,7 @@ class ServerKiller extends Thread{
$this->time = $time; $this->time = $time;
} }
public function run() : void{ protected function onRun() : void{
$this->registerClassLoader();
$start = time(); $start = time();
$this->synchronized(function(){ $this->synchronized(function(){
if(!$this->stopped){ if(!$this->stopped){