diff --git a/src/pocketmine/scheduler/AsyncTask.php b/src/pocketmine/scheduler/AsyncTask.php index 360a9df3a..c26b80f11 100644 --- a/src/pocketmine/scheduler/AsyncTask.php +++ b/src/pocketmine/scheduler/AsyncTask.php @@ -126,29 +126,29 @@ abstract class AsyncTask extends Collectable{ } /** - * Gets something into the local thread store. - * You have to initialize this in some way from the task on run + * @see AsyncWorker::getFromThreadStore() * * @param string $identifier * @return mixed */ public function getFromThreadStore(string $identifier){ - global $store; - return ($this->isGarbage() or !isset($store[$identifier])) ? null : $store[$identifier]; + if($this->worker === null or $this->isGarbage()){ + throw new \BadMethodCallException("Objects stored in AsyncWorker thread-local storage can only be retrieved during task execution"); + } + return $this->worker->getFromThreadStore($identifier); } /** - * Saves something into the local thread store. - * This might get deleted at any moment. + * @see AsyncWorker::saveToThreadStore() * * @param string $identifier * @param mixed $value */ public function saveToThreadStore(string $identifier, $value){ - global $store; - if(!$this->isGarbage()){ - $store[$identifier] = $value; + if($this->worker === null or $this->isGarbage()){ + throw new \BadMethodCallException("Objects can only be added to AsyncWorker thread-local storage during task execution"); } + $this->worker->saveToThreadStore($identifier, $value); } /** diff --git a/src/pocketmine/scheduler/AsyncWorker.php b/src/pocketmine/scheduler/AsyncWorker.php index a4f20a6d2..b9adfa66b 100644 --- a/src/pocketmine/scheduler/AsyncWorker.php +++ b/src/pocketmine/scheduler/AsyncWorker.php @@ -26,6 +26,8 @@ namespace pocketmine\scheduler; use pocketmine\Worker; class AsyncWorker extends Worker{ + /** @var mixed[] */ + private static $store = []; private $logger; private $id; @@ -53,9 +55,6 @@ class AsyncWorker extends Worker{ ini_set('memory_limit', '-1'); $this->logger->debug("No memory limit set"); } - - global $store; - $store = []; } public function getLogger() : \ThreadedLogger{ @@ -73,4 +72,30 @@ class AsyncWorker extends Worker{ public function getAsyncWorkerId() : int{ return $this->id; } + + /** + * Saves mixed data into the worker's thread-local object store. This can be used to store objects which you + * want to use on this worker thread from multiple AsyncTasks. + * + * @param string $identifier + * @param mixed $value + */ + public function saveToThreadStore(string $identifier, $value) : void{ + self::$store[$identifier] = $value; + } + + /** + * Retrieves mixed data from the worker's thread-local object store. + * + * Note that the thread-local object store could be cleared and your data might not exist, so your code should + * account for the possibility that what you're trying to retrieve might not exist. + * + * Objects stored in this storage may ONLY be retrieved while the task is running. + * + * @param string $identifier + * @return mixed + */ + public function getFromThreadStore(string $identifier){ + return self::$store[$identifier] ?? null; + } } diff --git a/src/pocketmine/scheduler/DumpWorkerMemoryTask.php b/src/pocketmine/scheduler/DumpWorkerMemoryTask.php index ad297a051..ac3525e57 100644 --- a/src/pocketmine/scheduler/DumpWorkerMemoryTask.php +++ b/src/pocketmine/scheduler/DumpWorkerMemoryTask.php @@ -48,10 +48,8 @@ class DumpWorkerMemoryTask extends AsyncTask{ } public function onRun(){ - global $store; - MemoryManager::dumpMemory( - ["worker" => $this->worker, "store" => $store], + $this->worker, $this->outputFolder . DIRECTORY_SEPARATOR . "AsyncWorker#" . $this->worker->getAsyncWorkerId(), $this->maxNesting, $this->maxStringSize,