PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
AsyncTask.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\scheduler;
25
26use pmmp\thread\Runnable;
27use pmmp\thread\ThreadSafe;
30use function array_key_exists;
31use function is_null;
32use function is_scalar;
33use function spl_object_id;
34
59abstract class AsyncTask extends Runnable{
66 private static array $threadLocalStorage = [];
67
68 private ThreadSafe|string|int|bool|null|float $result = null;
69
70 private bool $submitted = false;
71 private bool $finished = false;
72
73 public function run() : void{
74 $this->result = null;
75
76 $timings = Timings::getAsyncTaskRunTimings($this);
77 $timings->startTiming();
78
79 try{
80 $this->onRun();
81 }finally{
82 $timings->stopTiming();
83 }
84
85 $this->finished = true;
86 AsyncWorker::getNotifier()->wakeupSleeper();
87 AsyncWorker::maybeCollectCycles();
88 }
89
94 public function isFinished() : bool{
95 return $this->finished || $this->isTerminated();
96 }
97
98 public function hasResult() : bool{
99 return $this->result !== null;
100 }
101
105 public function getResult(){
106 if($this->result instanceof NonThreadSafeValue){
107 return $this->result->deserialize();
108 }
109 return $this->result;
110 }
111
112 public function setResult(mixed $result) : void{
113 $this->result = is_scalar($result) || is_null($result) || $result instanceof ThreadSafe ? $result : new NonThreadSafeValue($result);
114 }
115
116 public function setSubmitted() : void{
117 $this->submitted = true;
118 }
119
120 public function isSubmitted() : bool{
121 return $this->submitted;
122 }
123
127 abstract public function onRun() : void;
128
133 public function onCompletion() : void{
134
135 }
136
151 protected function storeLocal(string $key, mixed $complexData) : void{
152 self::$threadLocalStorage[spl_object_id($this)][$key] = $complexData;
153 }
154
165 protected function fetchLocal(string $key){
166 $id = spl_object_id($this);
167 if(!isset(self::$threadLocalStorage[$id]) || !array_key_exists($key, self::$threadLocalStorage[$id])){
168 throw new \InvalidArgumentException("No matching thread-local data found on this thread");
169 }
170
171 return self::$threadLocalStorage[$id][$key];
172 }
173
174 final public function __destruct(){
175 $this->reallyDestruct();
176 unset(self::$threadLocalStorage[spl_object_id($this)]);
177 }
178
182 protected function reallyDestruct() : void{
183
184 }
185}