From b69843a8bd2e72f16ee2f82a2842cfc1a0ca0b7a Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 21 Dec 2023 12:56:51 +0000 Subject: [PATCH] CommonThreadPartsTrait: add common implementation of quit() there's no need for the worker specialization here (isShutdown and shutdown are aliased to isJoined and join respectively), and the unstacking is not really desirable either as we previously learned with AsyncPool. --- src/thread/CommonThreadPartsTrait.php | 14 ++++++++++++++ src/thread/Thread.php | 14 -------------- src/thread/Worker.php | 17 ----------------- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/thread/CommonThreadPartsTrait.php b/src/thread/CommonThreadPartsTrait.php index f01a979b0..9cd2b68f9 100644 --- a/src/thread/CommonThreadPartsTrait.php +++ b/src/thread/CommonThreadPartsTrait.php @@ -120,6 +120,20 @@ trait CommonThreadPartsTrait{ $this->isKilled = true; } + /** + * Stops the thread using the best way possible. Try to stop it yourself before calling this. + */ + public function quit() : void{ + $this->isKilled = true; + + if(!$this->isJoined()){ + $this->notify(); + $this->join(); + } + + ThreadManager::getInstance()->remove($this); + } + /** * Called by set_exception_handler() when an uncaught exception is thrown. */ diff --git a/src/thread/Thread.php b/src/thread/Thread.php index 2f0204022..a6c36e14c 100644 --- a/src/thread/Thread.php +++ b/src/thread/Thread.php @@ -37,18 +37,4 @@ use pocketmine\scheduler\AsyncTask; */ abstract class Thread extends NativeThread{ use CommonThreadPartsTrait; - - /** - * Stops the thread using the best way possible. Try to stop it yourself before calling this. - */ - public function quit() : void{ - $this->isKilled = true; - - if(!$this->isJoined()){ - $this->notify(); - $this->join(); - } - - ThreadManager::getInstance()->remove($this); - } } diff --git a/src/thread/Worker.php b/src/thread/Worker.php index 7d9ca72c3..cc0b56046 100644 --- a/src/thread/Worker.php +++ b/src/thread/Worker.php @@ -38,21 +38,4 @@ use pocketmine\scheduler\AsyncTask; */ abstract class Worker extends NativeWorker{ use CommonThreadPartsTrait; - - /** - * Stops the thread using the best way possible. Try to stop it yourself before calling this. - */ - public function quit() : void{ - $this->isKilled = true; - - if(!$this->isShutdown()){ - $this->synchronized(function() : void{ - while($this->unstack() !== null); - }); - $this->notify(); - $this->shutdown(); - } - - ThreadManager::getInstance()->remove($this); - } }