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();
102 return $this->isTerminated();
110 return $this->finished || $this->isTerminated();
113 public function hasResult() : bool{
114 return $this->result !== null;
122 return $this->result->deserialize();
124 return $this->result;
127 public function setResult(mixed $result) : void{
128 $this->result = is_scalar($result) || is_null($result) || $result instanceof ThreadSafe ? $result : new
NonThreadSafeValue($result);
141 public function hasCancelledRun() : bool{
145 public function setSubmitted() : void{
146 $this->submitted = true;
149 public function isSubmitted() : bool{
150 return $this->submitted;
156 abstract public function onRun() : void;
174 public function publishProgress(mixed $progress) : void{
175 $progressUpdates = $this->progressUpdates;
176 if($progressUpdates ===
null){
177 $progressUpdates = $this->progressUpdates =
new ThreadSafeArray();
179 $progressUpdates[] = igbinary_serialize($progress) ??
throw new \InvalidArgumentException(
"Progress must be serializable");
187 $progressUpdates = $this->progressUpdates;
188 if($progressUpdates !==
null){
189 while(($progress = $progressUpdates->shift()) !==
null){
190 $this->onProgressUpdate(igbinary_unserialize($progress));
212 public function onError() : void{
230 protected function storeLocal(string $key, mixed $complexData) : void{
231 self::$threadLocalStorage[spl_object_id($this)][$key] = $complexData;
245 $id = spl_object_id($this);
246 if(!isset(self::$threadLocalStorage[$id]) || !array_key_exists($key, self::$threadLocalStorage[$id])){
247 throw new \InvalidArgumentException(
"No matching thread-local data found on this thread");
250 return self::$threadLocalStorage[$id][$key];
253 final public function __destruct(){
254 $this->reallyDestruct();
255 unset(self::$threadLocalStorage[spl_object_id($this)]);