PocketMine-MP 5.25.3 git-afc4a3c7f18d42b41cbfde84ab6a2e4dd7c03045
All Classes Namespaces Functions Variables Enumerations Enumerator Pages
AsyncWorker.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\Thread as NativeThread;
34use function ini_set;
35
36class AsyncWorker extends Worker{
38 private static array $store = [];
39
40 private static ?SleeperNotifier $notifier = null;
41 private static ?GarbageCollectorManager $cycleGcManager = null;
42
43 public function __construct(
44 private ThreadSafeLogger $logger,
45 private int $id,
46 private int $memoryLimit,
47 private SleeperHandlerEntry $sleeperEntry
48 ){}
49
50 public static function getNotifier() : SleeperNotifier{
51 if(self::$notifier !== null){
52 return self::$notifier;
53 }
54 throw new AssumptionFailedError("SleeperNotifier not found in thread-local storage");
55 }
56
57 public static function maybeCollectCycles() : void{
58 if(self::$cycleGcManager === null){
59 throw new AssumptionFailedError("GarbageCollectorManager not found in thread-local storage");
60 }
61 self::$cycleGcManager->maybeCollectCycles();
62 }
63
64 protected function onRun() : void{
65 \GlobalLogger::set($this->logger);
66
67 if($this->memoryLimit > 0){
68 ini_set('memory_limit', $this->memoryLimit . 'M');
69 $this->logger->debug("Set memory limit to " . $this->memoryLimit . " MB");
70 }else{
71 ini_set('memory_limit', '-1');
72 $this->logger->debug("No memory limit set");
73 }
74
75 self::$notifier = $this->sleeperEntry->createNotifier();
76 Timings::init();
77 self::$cycleGcManager = new GarbageCollectorManager($this->logger, Timings::$asyncTaskWorkers);
78 }
79
80 public function getLogger() : ThreadSafeLogger{
81 return $this->logger;
82 }
83
84 public function getThreadName() : string{
85 return "AsyncWorker#" . $this->id;
86 }
87
88 public function getAsyncWorkerId() : int{
89 return $this->id;
90 }
91
98 public function saveToThreadStore(string $identifier, mixed $value) : void{
99 if(NativeThread::getCurrentThread() !== $this){
100 throw new \LogicException("Thread-local data can only be stored in the thread context");
101 }
102 self::$store[$identifier] = $value;
103 }
104
115 public function getFromThreadStore(string $identifier) : mixed{
116 if(NativeThread::getCurrentThread() !== $this){
117 throw new \LogicException("Thread-local data can only be fetched in the thread context");
118 }
119 return self::$store[$identifier] ?? null;
120 }
121
127 public function removeFromThreadStore(string $identifier) : void{
128 if(NativeThread::getCurrentThread() !== $this){
129 throw new \LogicException("Thread-local data can only be removed in the thread context");
130 }
131 unset(self::$store[$identifier]);
132 }
133}
getFromThreadStore(string $identifier)
removeFromThreadStore(string $identifier)
saveToThreadStore(string $identifier, mixed $value)