AsyncTask: remove ArrayObject hack

this is no longer a concern with pmmpthread + PHP 8.1 and up. The behaviour that caused statics to be inherited was caused by bugs in PHP 8.0 and below, which have now been fixed.
This commit is contained in:
Dylan K. Taylor 2023-06-22 13:58:48 +01:00
parent 0240d35c05
commit 36f52f1ade
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -61,12 +61,12 @@ use function spl_object_id;
*/
abstract class AsyncTask extends Runnable{
/**
* @var \ArrayObject|mixed[]|null object hash => mixed data
* @phpstan-var \ArrayObject<int, array<string, mixed>>|null
* @var mixed[][] object hash => mixed data
* @phpstan-var array<int, array<string, mixed>>
*
* Used to store objects which are only needed on one thread and should not be serialized.
* Used to store thread-local data to be used by onCompletion().
*/
private static ?\ArrayObject $threadLocalStorage = null;
private static array $threadLocalStorage = [];
/** @phpstan-var ThreadSafeArray<int, string>|null */
private ?ThreadSafeArray $progressUpdates = null;
@ -212,15 +212,6 @@ abstract class AsyncTask extends Runnable{
* from.
*/
protected function storeLocal(string $key, mixed $complexData) : void{
if(self::$threadLocalStorage === null){
/*
* It's necessary to use an object (not array) here because pthreads is stupid. Non-default array statics
* will be inherited when task classes are copied to the worker thread, which would cause unwanted
* inheritance of primitive thread-locals, which we really don't want for various reasons.
* It won't try to inherit objects though, so this is the easiest solution.
*/
self::$threadLocalStorage = new \ArrayObject();
}
self::$threadLocalStorage[spl_object_id($this)][$key] = $complexData;
}
@ -236,7 +227,7 @@ abstract class AsyncTask extends Runnable{
*/
protected function fetchLocal(string $key){
$id = spl_object_id($this);
if(self::$threadLocalStorage === null || !isset(self::$threadLocalStorage[$id][$key])){
if(!isset(self::$threadLocalStorage[$id][$key])){
throw new \InvalidArgumentException("No matching thread-local data found on this thread");
}
@ -245,12 +236,7 @@ abstract class AsyncTask extends Runnable{
final public function __destruct(){
$this->reallyDestruct();
if(self::$threadLocalStorage !== null && isset(self::$threadLocalStorage[$h = spl_object_id($this)])){
unset(self::$threadLocalStorage[$h]);
if(self::$threadLocalStorage->count() === 0){
self::$threadLocalStorage = null;
}
}
unset(self::$threadLocalStorage[spl_object_id($this)]);
}
/**