68 private static array $threadLocalStorage = [];
71 private ?ThreadSafeArray $progressUpdates =
null;
73 private ThreadSafe|
string|
int|
bool|
null|
float $result =
null;
75 private bool $submitted =
false;
76 private bool $finished =
false;
78 public function run() :
void{
83 $this->finished =
true;
84 AsyncWorker::getNotifier()->wakeupSleeper();
92 return $this->finished || $this->isTerminated();
95 public function hasResult() : bool{
96 return $this->result !== null;
104 return $this->result->deserialize();
106 return $this->result;
109 public function setResult(mixed $result) : void{
110 $this->result = is_scalar($result) || is_null($result) || $result instanceof ThreadSafe ? $result : new
NonThreadSafeValue($result);
113 public function setSubmitted() : void{
114 $this->submitted = true;
117 public function isSubmitted() : bool{
118 return $this->submitted;
124 abstract public function onRun() : void;
140 public function publishProgress(mixed $progress) : void{
141 $progressUpdates = $this->progressUpdates;
142 if($progressUpdates ===
null){
143 $progressUpdates = $this->progressUpdates =
new ThreadSafeArray();
145 $progressUpdates[] = igbinary_serialize($progress) ??
throw new \InvalidArgumentException(
"Progress must be serializable");
151 public function checkProgressUpdates() : void{
152 $progressUpdates = $this->progressUpdates;
153 if($progressUpdates !==
null){
154 while(($progress = $progressUpdates->shift()) !==
null){
155 $this->onProgressUpdate(igbinary_unserialize($progress));
186 protected function storeLocal(string $key, mixed $complexData) : void{
187 self::$threadLocalStorage[spl_object_id($this)][$key] = $complexData;
201 $id = spl_object_id($this);
202 if(!isset(self::$threadLocalStorage[$id]) || !array_key_exists($key, self::$threadLocalStorage[$id])){
203 throw new \InvalidArgumentException(
"No matching thread-local data found on this thread");
206 return self::$threadLocalStorage[$id][$key];
209 final public function __destruct(){
210 $this->reallyDestruct();
211 unset(self::$threadLocalStorage[spl_object_id($this)]);