diff --git a/src/scheduler/AsyncTask.php b/src/scheduler/AsyncTask.php index b4c3ce20d..b0b64347a 100644 --- a/src/scheduler/AsyncTask.php +++ b/src/scheduler/AsyncTask.php @@ -24,12 +24,10 @@ declare(strict_types=1); namespace pocketmine\scheduler; use pmmp\thread\Runnable; -use pmmp\thread\Thread as NativeThread; use pmmp\thread\ThreadSafe; use pmmp\thread\ThreadSafeArray; use pocketmine\thread\NonThreadSafeValue; use function array_key_exists; -use function assert; use function igbinary_serialize; use function igbinary_unserialize; use function is_null; @@ -83,9 +81,7 @@ abstract class AsyncTask extends Runnable{ $this->onRun(); $this->finished = true; - $worker = NativeThread::getCurrentThread(); - assert($worker instanceof AsyncWorker); - $worker->getNotifier()->wakeupSleeper(); + AsyncWorker::getNotifier()->wakeupSleeper(); } /** diff --git a/src/scheduler/AsyncWorker.php b/src/scheduler/AsyncWorker.php index 919e3eedc..0f05dd896 100644 --- a/src/scheduler/AsyncWorker.php +++ b/src/scheduler/AsyncWorker.php @@ -36,7 +36,7 @@ class AsyncWorker extends Worker{ /** @var mixed[] */ private static array $store = []; - private const TLS_KEY_NOTIFIER = self::class . "::notifier"; + private static ?SleeperNotifier $notifier = null; public function __construct( private ThreadSafeLogger $logger, @@ -45,12 +45,11 @@ class AsyncWorker extends Worker{ private SleeperHandlerEntry $sleeperEntry ){} - public function getNotifier() : SleeperNotifier{ - $notifier = $this->getFromThreadStore(self::TLS_KEY_NOTIFIER); - if(!$notifier instanceof SleeperNotifier){ - throw new AssumptionFailedError("SleeperNotifier not found in thread-local storage"); + public static function getNotifier() : SleeperNotifier{ + if(self::$notifier !== null){ + return self::$notifier; } - return $notifier; + throw new AssumptionFailedError("SleeperNotifier not found in thread-local storage"); } protected function onRun() : void{ @@ -66,7 +65,7 @@ class AsyncWorker extends Worker{ $this->logger->debug("No memory limit set"); } - $this->saveToThreadStore(self::TLS_KEY_NOTIFIER, $this->sleeperEntry->createNotifier()); + self::$notifier = $this->sleeperEntry->createNotifier(); } public function getLogger() : ThreadSafeLogger{