mixed data * @phpstan-var array> * * Used to store thread-local data to be used by onCompletion(). */ private static array $threadLocalStorage = []; /** * @phpstan-var ThreadSafeArray|null * @deprecated */ private ?ThreadSafeArray $progressUpdates = null; private ThreadSafe|string|int|bool|null|float $result = null; private bool $submitted = false; private bool $finished = false; public function run() : void{ $this->result = null; $timings = Timings::getAsyncTaskRunTimings($this); $timings->startTiming(); try{ $this->onRun(); }finally{ $timings->stopTiming(); } $this->finished = true; AsyncWorker::getNotifier()->wakeupSleeper(); AsyncWorker::maybeCollectCycles(); } /** * Returns whether this task has finished executing, whether successfully or not. This differs from isRunning() * because it is not true prior to task execution. */ public function isFinished() : bool{ return $this->finished || $this->isTerminated(); } public function hasResult() : bool{ return $this->result !== null; } /** * @return mixed */ public function getResult(){ if($this->result instanceof NonThreadSafeValue){ return $this->result->deserialize(); } return $this->result; } public function setResult(mixed $result) : void{ $this->result = is_scalar($result) || is_null($result) || $result instanceof ThreadSafe ? $result : new NonThreadSafeValue($result); } public function setSubmitted() : void{ $this->submitted = true; } public function isSubmitted() : bool{ return $this->submitted; } /** * Actions to execute when run */ abstract public function onRun() : void; /** * Actions to execute when completed (on main thread) * Implement this if you want to handle the data in your AsyncTask after it has been processed */ public function onCompletion() : void{ } /** * @deprecated * * Call this method from {@link AsyncTask::onRun} (AsyncTask execution thread) to schedule a call to * {@link AsyncTask::onProgressUpdate} from the main thread with the given progress parameter. * * @param mixed $progress A value that can be safely serialize()'ed. */ public function publishProgress(mixed $progress) : void{ $progressUpdates = $this->progressUpdates; if($progressUpdates === null){ $progressUpdates = $this->progressUpdates = new ThreadSafeArray(); } $progressUpdates[] = igbinary_serialize($progress) ?? throw new \InvalidArgumentException("Progress must be serializable"); } /** * @deprecated * @internal Only call from AsyncPool.php on the main thread */ public function checkProgressUpdates() : void{ $progressUpdates = $this->progressUpdates; if($progressUpdates !== null){ while(($progress = $progressUpdates->shift()) !== null){ $this->onProgressUpdate(igbinary_unserialize($progress)); } } } /** * @deprecated * * Called from the main thread after {@link AsyncTask::publishProgress} is called. * All {@link AsyncTask::publishProgress} calls should result in {@link AsyncTask::onProgressUpdate} calls before * {@link AsyncTask::onCompletion} is called. * * @param mixed $progress The parameter passed to {@link AsyncTask#publishProgress}. It is serialize()'ed * and then unserialize()'ed, as if it has been cloned. */ public function onProgressUpdate($progress) : void{ } /** * Saves mixed data in thread-local storage. Data stored using this storage is **only accessible from the thread it * was stored on**. Data stored using this method will **not** be serialized. * This can be used to store references to variables which you need later on on the same thread, but not others. * * For example, plugin references could be stored in the constructor of the async task (which is called on the main * thread) using this, and then fetched in onCompletion() (which is also called on the main thread), without them * becoming serialized. * * Scalar types can be stored directly in class properties instead of using this storage. * * Objects stored in this storage can be retrieved using fetchLocal() on the same thread that this method was called * from. */ protected function storeLocal(string $key, mixed $complexData) : void{ self::$threadLocalStorage[spl_object_id($this)][$key] = $complexData; } /** * Retrieves data stored in thread-local storage. * * If you used storeLocal(), you can use this on the same thread to fetch data stored. This should be used during * onProgressUpdate() and onCompletion() to fetch thread-local data stored on the parent thread. * * @return mixed * * @throws \InvalidArgumentException if no data were stored by this AsyncTask instance. */ protected function fetchLocal(string $key){ $id = spl_object_id($this); if(!isset(self::$threadLocalStorage[$id]) || !array_key_exists($key, self::$threadLocalStorage[$id])){ throw new \InvalidArgumentException("No matching thread-local data found on this thread"); } return self::$threadLocalStorage[$id][$key]; } final public function __destruct(){ $this->reallyDestruct(); unset(self::$threadLocalStorage[spl_object_id($this)]); } /** * Override this to do normal __destruct() cleanup from a child class. */ protected function reallyDestruct() : void{ } }