Update to Snooze 0.5.0

This commit is contained in:
Dylan K. Taylor
2023-05-23 01:09:22 +01:00
parent 4aba9d9725
commit c66a3a8b3e
6 changed files with 48 additions and 44 deletions

View File

@ -26,7 +26,6 @@ namespace pocketmine\scheduler;
use pmmp\thread\Thread as NativeThread;
use pmmp\thread\ThreadSafeArray;
use pocketmine\snooze\SleeperHandler;
use pocketmine\snooze\SleeperNotifier;
use pocketmine\thread\log\ThreadSafeLogger;
use pocketmine\thread\ThreadSafeClassLoader;
use pocketmine\utils\Utils;
@ -52,8 +51,8 @@ class AsyncPool{
private array $taskQueues = [];
/**
* @var AsyncWorker[]
* @phpstan-var array<int, AsyncWorker>
* @var AsyncPoolWorkerEntry[]
* @phpstan-var array<int, AsyncPoolWorkerEntry>
*/
private array $workers = [];
/**
@ -132,13 +131,12 @@ class AsyncPool{
*/
private function getWorker(int $worker) : AsyncWorker{
if(!isset($this->workers[$worker])){
$notifier = new SleeperNotifier();
$this->workers[$worker] = new AsyncWorker($this->logger, $worker, $this->workerMemoryLimit, $notifier);
$this->eventLoop->addNotifier($notifier, function() use ($worker) : void{
$sleeperEntry = $this->eventLoop->addNotifier(function() use ($worker) : void{
$this->collectTasksFromWorker($worker);
});
$this->workers[$worker]->setClassLoaders([$this->classLoader]);
$this->workers[$worker]->start(self::WORKER_START_OPTIONS);
$this->workers[$worker] = new AsyncPoolWorkerEntry(new AsyncWorker($this->logger, $worker, $this->workerMemoryLimit, $sleeperEntry), $sleeperEntry->getNotifierId());
$this->workers[$worker]->worker->setClassLoaders([$this->classLoader]);
$this->workers[$worker]->worker->start(self::WORKER_START_OPTIONS);
$this->taskQueues[$worker] = new \SplQueue();
@ -147,7 +145,7 @@ class AsyncPool{
}
}
return $this->workers[$worker];
return $this->workers[$worker]->worker;
}
/**
@ -270,7 +268,7 @@ class AsyncPool{
break; //current task is still running, skip to next worker
}
}
$this->workers[$worker]->collect();
$this->workers[$worker]->worker->collect();
return $more;
}
@ -289,8 +287,8 @@ class AsyncPool{
$time = time();
foreach($this->taskQueues as $i => $queue){
if((!isset($this->workerLastUsed[$i]) || $this->workerLastUsed[$i] + 300 < $time) && $queue->isEmpty()){
$this->workers[$i]->quit();
$this->eventLoop->removeNotifier($this->workers[$i]->getNotifier());
$this->workers[$i]->worker->quit();
$this->eventLoop->removeNotifier($this->workers[$i]->sleeperNotifierId);
unset($this->workers[$i], $this->taskQueues[$i], $this->workerLastUsed[$i]);
$ret++;
}
@ -308,8 +306,8 @@ class AsyncPool{
}
foreach($this->workers as $worker){
$worker->quit();
$this->eventLoop->removeNotifier($worker->getNotifier());
$worker->worker->quit();
$this->eventLoop->removeNotifier($worker->sleeperNotifierId);
}
$this->workers = [];
$this->taskQueues = [];

View File

@ -24,9 +24,11 @@ declare(strict_types=1);
namespace pocketmine\scheduler;
use pmmp\thread\Thread as NativeThread;
use pocketmine\snooze\SleeperHandlerEntry;
use pocketmine\snooze\SleeperNotifier;
use pocketmine\thread\log\ThreadSafeLogger;
use pocketmine\thread\Worker;
use pocketmine\utils\AssumptionFailedError;
use function gc_enable;
use function ini_set;
@ -34,15 +36,21 @@ class AsyncWorker extends Worker{
/** @var mixed[] */
private static array $store = [];
private const TLS_KEY_NOTIFIER = self::class . "::notifier";
public function __construct(
private ThreadSafeLogger $logger,
private int $id,
private int $memoryLimit,
private SleeperNotifier $notifier
private SleeperHandlerEntry $sleeperEntry
){}
public function getNotifier() : SleeperNotifier{
return $this->notifier;
$notifier = $this->getFromThreadStore(self::TLS_KEY_NOTIFIER);
if(!$notifier instanceof SleeperNotifier){
throw new AssumptionFailedError("SleeperNotifier not found in thread-local storage");
}
return $notifier;
}
protected function onRun() : void{
@ -57,6 +65,8 @@ class AsyncWorker extends Worker{
ini_set('memory_limit', '-1');
$this->logger->debug("No memory limit set");
}
$this->saveToThreadStore(self::TLS_KEY_NOTIFIER, $this->sleeperEntry->createNotifier());
}
public function getLogger() : ThreadSafeLogger{