From 5fbc7681b04fe702fcbd43940b9d325e5c216707 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 15 Jul 2021 19:00:40 +0100 Subject: [PATCH] Allow registering multiple ClassLoaders for a thread --- src/scheduler/AsyncPool.php | 2 +- src/thread/CommonThreadPartsTrait.php | 56 ++++++++++++++++++--------- src/thread/Thread.php | 4 +- src/thread/Worker.php | 4 +- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/scheduler/AsyncPool.php b/src/scheduler/AsyncPool.php index 753e7e5a6..707a6e3f3 100644 --- a/src/scheduler/AsyncPool.php +++ b/src/scheduler/AsyncPool.php @@ -147,7 +147,7 @@ class AsyncPool{ $this->eventLoop->addNotifier($notifier, function() use ($worker) : void{ $this->collectTasksFromWorker($worker); }); - $this->workers[$worker]->setClassLoader($this->classLoader); + $this->workers[$worker]->setClassLoaders([$this->classLoader]); $this->workers[$worker]->start(self::WORKER_START_OPTIONS); $this->taskQueues[$worker] = new \SplQueue(); diff --git a/src/thread/CommonThreadPartsTrait.php b/src/thread/CommonThreadPartsTrait.php index 7ce63983a..3f609d833 100644 --- a/src/thread/CommonThreadPartsTrait.php +++ b/src/thread/CommonThreadPartsTrait.php @@ -28,46 +28,66 @@ use pocketmine\Server; use function error_reporting; trait CommonThreadPartsTrait{ - /** @var \ClassLoader|null */ - protected $classLoader; + /** @var \Threaded|\ClassLoader[]|null */ + private ?\Threaded $classLoaders = null; /** @var string|null */ protected $composerAutoloaderPath; /** @var bool */ protected $isKilled = false; - public function getClassLoader() : ?\ClassLoader{ - return $this->classLoader; - } - - public function setClassLoader(?\ClassLoader $loader = null) : void{ - $this->composerAutoloaderPath = \pocketmine\COMPOSER_AUTOLOADER_PATH; - - if($loader === null){ - $loader = Server::getInstance()->getLoader(); - } - $this->classLoader = $loader; + /** + * @return \ClassLoader[] + */ + public function getClassLoaders() : ?array{ + return $this->classLoaders !== null ? (array) $this->classLoaders : null; } /** - * Registers the class loader for this thread. + * @param \ClassLoader[] $autoloaders + */ + public function setClassLoaders(?array $autoloaders = null) : void{ + $this->composerAutoloaderPath = \pocketmine\COMPOSER_AUTOLOADER_PATH; + + if($autoloaders === null){ + $autoloaders = [Server::getInstance()->getLoader()]; + } + + if($this->classLoaders === null){ + $this->classLoaders = new \Threaded(); + }else{ + foreach($this->classLoaders as $k => $autoloader){ + unset($this->classLoaders[$k]); + } + } + foreach($autoloaders as $autoloader){ + $this->classLoaders[] = $autoloader; + } + } + + /** + * Registers the class loaders for this thread. * * WARNING: This method MUST be called from any descendent threads' run() method to make autoloading usable. * If you do not do this, you will not be able to use new classes that were not loaded when the thread was started * (unless you are using a custom autoloader). */ - public function registerClassLoader() : void{ + public function registerClassLoaders() : void{ if($this->composerAutoloaderPath !== null){ require $this->composerAutoloaderPath; } - if($this->classLoader !== null){ - $this->classLoader->register(false); + $autoloaders = $this->classLoaders; + if($autoloaders !== null){ + foreach($autoloaders as $autoloader){ + /** @var \ClassLoader $autoloader */ + $autoloader->register(false); + } } } final public function run() : void{ error_reporting(-1); - $this->registerClassLoader(); + $this->registerClassLoaders(); //set this after the autoloader is registered ErrorToExceptionHandler::set(); $this->onRun(); diff --git a/src/thread/Thread.php b/src/thread/Thread.php index 22f2e0986..66c88b106 100644 --- a/src/thread/Thread.php +++ b/src/thread/Thread.php @@ -35,8 +35,8 @@ abstract class Thread extends \Thread{ //this is intentionally not traitified ThreadManager::getInstance()->add($this); - if($this->getClassLoader() === null){ - $this->setClassLoader(); + if($this->getClassLoaders() === null){ + $this->setClassLoaders(); } return parent::start($options); } diff --git a/src/thread/Worker.php b/src/thread/Worker.php index 8af3659b0..cb31b1623 100644 --- a/src/thread/Worker.php +++ b/src/thread/Worker.php @@ -35,8 +35,8 @@ abstract class Worker extends \Worker{ //this is intentionally not traitified ThreadManager::getInstance()->add($this); - if($this->getClassLoader() === null){ - $this->setClassLoader(); + if($this->getClassLoaders() === null){ + $this->setClassLoaders(); } return parent::start($options); }