Prevent undefined behaviour when accessing async worker thread-store from outside the worker itself

This commit is contained in:
Dylan K. Taylor 2018-10-08 13:01:12 +01:00
parent a0f3c03b50
commit 24677e1d79

View File

@ -90,6 +90,9 @@ class AsyncWorker extends Worker{
* @param mixed $value
*/
public function saveToThreadStore(string $identifier, $value) : void{
if(\Thread::getCurrentThread() !== $this){
throw new \InvalidStateException("Thread-local data can only be stored in the thread context");
}
self::$store[$identifier] = $value;
}
@ -105,6 +108,9 @@ class AsyncWorker extends Worker{
* @return mixed
*/
public function getFromThreadStore(string $identifier){
if(\Thread::getCurrentThread() !== $this){
throw new \InvalidStateException("Thread-local data can only be fetched in the thread context");
}
return self::$store[$identifier] ?? null;
}
@ -114,6 +120,9 @@ class AsyncWorker extends Worker{
* @param string $identifier
*/
public function removeFromThreadStore(string $identifier) : void{
if(\Thread::getCurrentThread() !== $this){
throw new \InvalidStateException("Thread-local data can only be removed in the thread context");
}
unset(self::$store[$identifier]);
}
}