mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-07 12:18:46 +00:00
AsyncTask: partial backport of 6ac0c517f54dc83fc23f41a6797c14e1f4b457a1 (simplify TLS)
- deprecated AsyncTask::peekLocal() - AsyncTask::fetchLocal() no longer deletes stored data
This commit is contained in:
parent
87fb42cabd
commit
cde2c10c1d
@ -51,7 +51,7 @@ abstract class AsyncTask extends Collectable{
|
|||||||
* @var \SplObjectStorage|null
|
* @var \SplObjectStorage|null
|
||||||
* Used to store objects on the main thread which should not be serialized.
|
* Used to store objects on the main thread which should not be serialized.
|
||||||
*/
|
*/
|
||||||
private static $localObjectStorage;
|
private static $threadLocalStorage;
|
||||||
|
|
||||||
/** @var AsyncWorker $worker */
|
/** @var AsyncWorker $worker */
|
||||||
public $worker = null;
|
public $worker = null;
|
||||||
@ -229,9 +229,6 @@ abstract class AsyncTask extends Collectable{
|
|||||||
*
|
*
|
||||||
* Scalar types can be stored directly in class properties instead of using this storage.
|
* Scalar types can be stored directly in class properties instead of using this storage.
|
||||||
*
|
*
|
||||||
* Objects stored in this storage MUST be retrieved through {@link AsyncTask::fetchLocal} when {@link AsyncTask::onCompletion} is called.
|
|
||||||
* Otherwise, a NOTICE level message will be raised and the reference will be removed after onCompletion exits.
|
|
||||||
*
|
|
||||||
* WARNING: THIS METHOD SHOULD ONLY BE CALLED FROM THE MAIN THREAD!
|
* WARNING: THIS METHOD SHOULD ONLY BE CALLED FROM THE MAIN THREAD!
|
||||||
*
|
*
|
||||||
* @param mixed $complexData the data to store
|
* @param mixed $complexData the data to store
|
||||||
@ -243,26 +240,19 @@ abstract class AsyncTask extends Collectable{
|
|||||||
throw new \BadMethodCallException("Objects can only be stored from the parent thread");
|
throw new \BadMethodCallException("Objects can only be stored from the parent thread");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(self::$localObjectStorage === null){
|
if(self::$threadLocalStorage === null){
|
||||||
self::$localObjectStorage = new \SplObjectStorage(); //lazy init
|
self::$threadLocalStorage = new \SplObjectStorage(); //lazy init
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isset(self::$localObjectStorage[$this])){
|
if(isset(self::$threadLocalStorage[$this])){
|
||||||
throw new \InvalidStateException("Already storing complex data for this async task");
|
throw new \InvalidStateException("Already storing complex data for this async task");
|
||||||
}
|
}
|
||||||
self::$localObjectStorage[$this] = $complexData;
|
self::$threadLocalStorage[$this] = $complexData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns and removes mixed data in thread-local storage on the parent thread. Call this method from
|
* Returns data previously stored in thread-local storage on the parent thread. Use this during progress updates or
|
||||||
* {@link AsyncTask::onCompletion} to fetch the data stored in the object store, if any.
|
* task completion to retrieve data you stored using {@link AsyncTask::storeLocal}.
|
||||||
*
|
|
||||||
* If no data was stored in the local store, or if the data was already retrieved by a previous call to fetchLocal,
|
|
||||||
* do NOT call this method, or an exception will be thrown.
|
|
||||||
*
|
|
||||||
* Do not call this method from {@link AsyncTask::onProgressUpdate}, because this method deletes stored data, which
|
|
||||||
* means that you will not be able to retrieve it again afterwards. Use {@link AsyncTask::peekLocal} instead to
|
|
||||||
* retrieve stored data without removing it from the store.
|
|
||||||
*
|
*
|
||||||
* WARNING: THIS METHOD SHOULD ONLY BE CALLED FROM THE MAIN THREAD!
|
* WARNING: THIS METHOD SHOULD ONLY BE CALLED FROM THE MAIN THREAD!
|
||||||
*
|
*
|
||||||
@ -272,25 +262,20 @@ abstract class AsyncTask extends Collectable{
|
|||||||
* @throws \BadMethodCallException if called from any thread except the main thread
|
* @throws \BadMethodCallException if called from any thread except the main thread
|
||||||
*/
|
*/
|
||||||
protected function fetchLocal(){
|
protected function fetchLocal(){
|
||||||
try{
|
if($this->worker !== null and $this->worker === \Thread::getCurrentThread()){
|
||||||
return $this->peekLocal();
|
throw new \BadMethodCallException("Objects can only be retrieved from the parent thread");
|
||||||
}finally{
|
|
||||||
if(self::$localObjectStorage !== null){
|
|
||||||
unset(self::$localObjectStorage[$this]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(self::$threadLocalStorage === null or !isset(self::$threadLocalStorage[$this])){
|
||||||
|
throw new \InvalidStateException("No complex data stored for this async task");
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$threadLocalStorage[$this];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns mixed data in thread-local storage on the parent thread **without clearing** it. Call this method from
|
* @deprecated
|
||||||
* {@link AsyncTask::onProgressUpdate} to fetch the data stored if you need to be able to access the data later on,
|
* @see AsyncTask::fetchLocal()
|
||||||
* such as in another progress update.
|
|
||||||
*
|
|
||||||
* Use {@link AsyncTask::fetchLocal} instead from {@link AsyncTask::onCompletion}, because this method does not delete
|
|
||||||
* the data, and not clearing the data will result in a warning for memory leak after {@link AsyncTask::onCompletion}
|
|
||||||
* finished executing.
|
|
||||||
*
|
|
||||||
* WARNING: THIS METHOD SHOULD ONLY BE CALLED FROM THE MAIN THREAD!
|
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
*
|
||||||
@ -298,27 +283,15 @@ abstract class AsyncTask extends Collectable{
|
|||||||
* @throws \BadMethodCallException if called from any thread except the main thread
|
* @throws \BadMethodCallException if called from any thread except the main thread
|
||||||
*/
|
*/
|
||||||
protected function peekLocal(){
|
protected function peekLocal(){
|
||||||
if($this->worker !== null and $this->worker === \Thread::getCurrentThread()){
|
return $this->fetchLocal();
|
||||||
throw new \BadMethodCallException("Objects can only be retrieved from the parent thread");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(self::$localObjectStorage === null or !isset(self::$localObjectStorage[$this])){
|
|
||||||
throw new \InvalidStateException("No complex data stored for this async task");
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$localObjectStorage[$this];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal Called by the AsyncPool to destroy any leftover stored objects that this task failed to retrieve.
|
* @internal Called by the AsyncPool to destroy any leftover stored objects that this task failed to retrieve.
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
public function removeDanglingStoredObjects() : bool{
|
public function removeDanglingStoredObjects() : void{
|
||||||
if(self::$localObjectStorage !== null and isset(self::$localObjectStorage[$this])){
|
if(self::$threadLocalStorage !== null and isset(self::$threadLocalStorage[$this])){
|
||||||
unset(self::$localObjectStorage[$this]);
|
unset(self::$threadLocalStorage[$this]);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user