AsyncWorker: Use statics for thread-local worker storage instead of globals

This commit is contained in:
Dylan K. Taylor
2018-06-07 09:59:32 +01:00
parent 73e56c8a36
commit ee787974f2
3 changed files with 38 additions and 15 deletions

View File

@@ -26,6 +26,8 @@ namespace pocketmine\scheduler;
use pocketmine\Worker;
class AsyncWorker extends Worker{
/** @var mixed[] */
private static $store = [];
private $logger;
private $id;
@@ -53,9 +55,6 @@ class AsyncWorker extends Worker{
ini_set('memory_limit', '-1');
$this->logger->debug("No memory limit set");
}
global $store;
$store = [];
}
public function getLogger() : \ThreadedLogger{
@@ -73,4 +72,30 @@ class AsyncWorker extends Worker{
public function getAsyncWorkerId() : int{
return $this->id;
}
/**
* Saves mixed data into the worker's thread-local object store. This can be used to store objects which you
* want to use on this worker thread from multiple AsyncTasks.
*
* @param string $identifier
* @param mixed $value
*/
public function saveToThreadStore(string $identifier, $value) : void{
self::$store[$identifier] = $value;
}
/**
* Retrieves mixed data from the worker's thread-local object store.
*
* Note that the thread-local object store could be cleared and your data might not exist, so your code should
* account for the possibility that what you're trying to retrieve might not exist.
*
* Objects stored in this storage may ONLY be retrieved while the task is running.
*
* @param string $identifier
* @return mixed
*/
public function getFromThreadStore(string $identifier){
return self::$store[$identifier] ?? null;
}
}