From cde2c10c1d66e19f6b1fae4109dcba1579e156ee Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 27 Jul 2019 15:09:42 +0100 Subject: [PATCH] AsyncTask: partial backport of 6ac0c517f54dc83fc23f41a6797c14e1f4b457a1 (simplify TLS) - deprecated AsyncTask::peekLocal() - AsyncTask::fetchLocal() no longer deletes stored data --- src/pocketmine/scheduler/AsyncTask.php | 69 ++++++++------------------ 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/src/pocketmine/scheduler/AsyncTask.php b/src/pocketmine/scheduler/AsyncTask.php index c14fd09e0..0e3a4d991 100644 --- a/src/pocketmine/scheduler/AsyncTask.php +++ b/src/pocketmine/scheduler/AsyncTask.php @@ -51,7 +51,7 @@ abstract class AsyncTask extends Collectable{ * @var \SplObjectStorage|null * Used to store objects on the main thread which should not be serialized. */ - private static $localObjectStorage; + private static $threadLocalStorage; /** @var AsyncWorker $worker */ public $worker = null; @@ -229,9 +229,6 @@ abstract class AsyncTask extends Collectable{ * * Scalar types can be stored directly in class properties instead of using this storage. * - * Objects stored in this storage MUST be retrieved through {@link AsyncTask::fetchLocal} when {@link AsyncTask::onCompletion} is called. - * Otherwise, a NOTICE level message will be raised and the reference will be removed after onCompletion exits. - * * WARNING: THIS METHOD SHOULD ONLY BE CALLED FROM THE MAIN THREAD! * * @param mixed $complexData the data to store @@ -243,26 +240,19 @@ abstract class AsyncTask extends Collectable{ throw new \BadMethodCallException("Objects can only be stored from the parent thread"); } - if(self::$localObjectStorage === null){ - self::$localObjectStorage = new \SplObjectStorage(); //lazy init + if(self::$threadLocalStorage === null){ + self::$threadLocalStorage = new \SplObjectStorage(); //lazy init } - if(isset(self::$localObjectStorage[$this])){ + if(isset(self::$threadLocalStorage[$this])){ throw new \InvalidStateException("Already storing complex data for this async task"); } - self::$localObjectStorage[$this] = $complexData; + self::$threadLocalStorage[$this] = $complexData; } /** - * Returns and removes mixed data in thread-local storage on the parent thread. Call this method from - * {@link AsyncTask::onCompletion} to fetch the data stored in the object store, if any. - * - * If no data was stored in the local store, or if the data was already retrieved by a previous call to fetchLocal, - * do NOT call this method, or an exception will be thrown. - * - * Do not call this method from {@link AsyncTask::onProgressUpdate}, because this method deletes stored data, which - * means that you will not be able to retrieve it again afterwards. Use {@link AsyncTask::peekLocal} instead to - * retrieve stored data without removing it from the store. + * Returns data previously stored in thread-local storage on the parent thread. Use this during progress updates or + * task completion to retrieve data you stored using {@link AsyncTask::storeLocal}. * * WARNING: THIS METHOD SHOULD ONLY BE CALLED FROM THE MAIN THREAD! * @@ -272,25 +262,20 @@ abstract class AsyncTask extends Collectable{ * @throws \BadMethodCallException if called from any thread except the main thread */ protected function fetchLocal(){ - try{ - return $this->peekLocal(); - }finally{ - if(self::$localObjectStorage !== null){ - unset(self::$localObjectStorage[$this]); - } + if($this->worker !== null and $this->worker === \Thread::getCurrentThread()){ + throw new \BadMethodCallException("Objects can only be retrieved from the parent thread"); } + + if(self::$threadLocalStorage === null or !isset(self::$threadLocalStorage[$this])){ + throw new \InvalidStateException("No complex data stored for this async task"); + } + + return self::$threadLocalStorage[$this]; } /** - * Returns mixed data in thread-local storage on the parent thread **without clearing** it. Call this method from - * {@link AsyncTask::onProgressUpdate} to fetch the data stored if you need to be able to access the data later on, - * such as in another progress update. - * - * Use {@link AsyncTask::fetchLocal} instead from {@link AsyncTask::onCompletion}, because this method does not delete - * the data, and not clearing the data will result in a warning for memory leak after {@link AsyncTask::onCompletion} - * finished executing. - * - * WARNING: THIS METHOD SHOULD ONLY BE CALLED FROM THE MAIN THREAD! + * @deprecated + * @see AsyncTask::fetchLocal() * * @return mixed * @@ -298,27 +283,15 @@ abstract class AsyncTask extends Collectable{ * @throws \BadMethodCallException if called from any thread except the main thread */ protected function peekLocal(){ - if($this->worker !== null and $this->worker === \Thread::getCurrentThread()){ - throw new \BadMethodCallException("Objects can only be retrieved from the parent thread"); - } - - if(self::$localObjectStorage === null or !isset(self::$localObjectStorage[$this])){ - throw new \InvalidStateException("No complex data stored for this async task"); - } - - return self::$localObjectStorage[$this]; + return $this->fetchLocal(); } /** * @internal Called by the AsyncPool to destroy any leftover stored objects that this task failed to retrieve. - * @return bool */ - public function removeDanglingStoredObjects() : bool{ - if(self::$localObjectStorage !== null and isset(self::$localObjectStorage[$this])){ - unset(self::$localObjectStorage[$this]); - return true; + public function removeDanglingStoredObjects() : void{ + if(self::$threadLocalStorage !== null and isset(self::$threadLocalStorage[$this])){ + unset(self::$threadLocalStorage[$this]); } - - return false; } }