66 private static array $threadLocalStorage = [];
68 private ThreadSafe|
string|
int|
bool|
null|
float $result =
null;
70 private bool $submitted =
false;
71 private bool $finished =
false;
73 public function run() :
void{
76 $timings = Timings::getAsyncTaskRunTimings($this);
77 $timings->startTiming();
82 $timings->stopTiming();
85 $this->finished =
true;
86 AsyncWorker::getNotifier()->wakeupSleeper();
87 AsyncWorker::maybeCollectCycles();
95 return $this->finished || $this->isTerminated();
98 public function hasResult() : bool{
99 return $this->result !== null;
107 return $this->result->deserialize();
109 return $this->result;
112 public function setResult(mixed $result) : void{
113 $this->result = is_scalar($result) || is_null($result) || $result instanceof ThreadSafe ? $result : new
NonThreadSafeValue($result);
116 public function setSubmitted() : void{
117 $this->submitted = true;
120 public function isSubmitted() : bool{
121 return $this->submitted;
127 abstract public function onRun() : void;
151 protected function storeLocal(string $key, mixed $complexData) : void{
152 self::$threadLocalStorage[spl_object_id($this)][$key] = $complexData;
166 $id = spl_object_id($this);
167 if(!isset(self::$threadLocalStorage[$id]) || !array_key_exists($key, self::$threadLocalStorage[$id])){
168 throw new \InvalidArgumentException(
"No matching thread-local data found on this thread");
171 return self::$threadLocalStorage[$id][$key];
174 final public function __destruct(){
175 $this->reallyDestruct();
176 unset(self::$threadLocalStorage[spl_object_id($this)]);