69 private static array $threadLocalStorage = [];
75 private ?ThreadSafeArray $progressUpdates =
null;
77 private ThreadSafe|
string|
int|
bool|
null|
float $result =
null;
79 private bool $submitted =
false;
80 private bool $finished =
false;
82 public function run() :
void{
85 $timings = Timings::getAsyncTaskRunTimings($this);
86 $timings->startTiming();
91 $timings->stopTiming();
94 $this->finished =
true;
95 AsyncWorker::getNotifier()->wakeupSleeper();
96 AsyncWorker::maybeCollectCycles();
103 return $this->isTerminated();
111 return $this->finished || $this->isTerminated();
114 public function hasResult() : bool{
115 return $this->result !== null;
123 return $this->result->deserialize();
125 return $this->result;
128 public function setResult(mixed $result) : void{
129 $this->result = is_scalar($result) || is_null($result) || $result instanceof ThreadSafe ? $result : new
NonThreadSafeValue($result);
142 public function hasCancelledRun() : bool{
146 public function setSubmitted() : void{
147 $this->submitted = true;
150 public function isSubmitted() : bool{
151 return $this->submitted;
157 abstract public function onRun() : void;
175 public function publishProgress(mixed $progress) : void{
176 $progressUpdates = $this->progressUpdates;
177 if($progressUpdates ===
null){
179 $progressUpdates =
new ThreadSafeArray();
180 $this->progressUpdates = $progressUpdates;
182 $progressUpdates[] = igbinary_serialize($progress) ??
throw new \InvalidArgumentException(
"Progress must be serializable");
190 $progressUpdates = $this->progressUpdates;
191 if($progressUpdates !==
null){
192 while(($progress = $progressUpdates->shift()) !==
null){
193 $this->onProgressUpdate(igbinary_unserialize($progress));
215 public function onError() : void{
233 protected function storeLocal(string $key, mixed $complexData) : void{
234 self::$threadLocalStorage[spl_object_id($this)][$key] = $complexData;
248 $id = spl_object_id($this);
249 if(!isset(self::$threadLocalStorage[$id]) || !array_key_exists($key, self::$threadLocalStorage[$id])){
250 throw new \InvalidArgumentException(
"No matching thread-local data found on this thread");
253 return self::$threadLocalStorage[$id][$key];
256 final public function __destruct(){
257 $this->reallyDestruct();
258 unset(self::$threadLocalStorage[spl_object_id($this)]);