22declare(strict_types=1);
26use pmmp\thread\Thread as NativeThread;
32use
function gc_enable;
37 private static array $store = [];
41 public function __construct(
44 private int $memoryLimit,
49 if(self::$notifier !==
null){
50 return self::$notifier;
55 protected function onRun() :
void{
56 \GlobalLogger::set($this->logger);
60 if($this->memoryLimit > 0){
61 ini_set(
'memory_limit', $this->memoryLimit .
'M');
62 $this->logger->debug(
"Set memory limit to " . $this->memoryLimit .
" MB");
64 ini_set(
'memory_limit',
'-1');
65 $this->logger->debug(
"No memory limit set");
68 self::$notifier = $this->sleeperEntry->createNotifier();
75 public function getThreadName() :
string{
76 return "AsyncWorker#" . $this->id;
79 public function getAsyncWorkerId() :
int{
90 if(NativeThread::getCurrentThread() !== $this){
91 throw new \LogicException(
"Thread-local data can only be stored in the thread context");
93 self::$store[$identifier] = $value;
107 if(NativeThread::getCurrentThread() !== $this){
108 throw new \LogicException(
"Thread-local data can only be fetched in the thread context");
110 return self::$store[$identifier] ??
null;
119 if(NativeThread::getCurrentThread() !== $this){
120 throw new \LogicException(
"Thread-local data can only be removed in the thread context");
122 unset(self::$store[$identifier]);
getFromThreadStore(string $identifier)
removeFromThreadStore(string $identifier)
saveToThreadStore(string $identifier, mixed $value)