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){
178 $progressUpdates = $this->progressUpdates =
new ThreadSafeArray();
180 $progressUpdates[] = igbinary_serialize($progress) ??
throw new \InvalidArgumentException(
"Progress must be serializable");
188 $progressUpdates = $this->progressUpdates;
189 if($progressUpdates !==
null){
190 while(($progress = $progressUpdates->shift()) !==
null){
191 $this->onProgressUpdate(igbinary_unserialize($progress));
213 public function onError() : void{
231 protected function storeLocal(string $key, mixed $complexData) : void{
232 self::$threadLocalStorage[spl_object_id($this)][$key] = $complexData;
246 $id = spl_object_id($this);
247 if(!isset(self::$threadLocalStorage[$id]) || !array_key_exists($key, self::$threadLocalStorage[$id])){
248 throw new \InvalidArgumentException(
"No matching thread-local data found on this thread");
251 return self::$threadLocalStorage[$id][$key];
254 final public function __destruct(){
255 $this->reallyDestruct();
256 unset(self::$threadLocalStorage[spl_object_id($this)]);