PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
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;
32use function gc_enable;
33use function ini_set;
34
35class AsyncWorker extends Worker{
37 private static array $store = [];
38
39 private static ?SleeperNotifier $notifier = null;
40
41 public function __construct(
42 private ThreadSafeLogger $logger,
43 private int $id,
44 private int $memoryLimit,
45 private SleeperHandlerEntry $sleeperEntry
46 ){}
47
48 public static function getNotifier() : SleeperNotifier{
49 if(self::$notifier !== null){
50 return self::$notifier;
51 }
52 throw new AssumptionFailedError("SleeperNotifier not found in thread-local storage");
53 }
54
55 protected function onRun() : void{
56 \GlobalLogger::set($this->logger);
57
58 gc_enable();
59
60 if($this->memoryLimit > 0){
61 ini_set('memory_limit', $this->memoryLimit . 'M');
62 $this->logger->debug("Set memory limit to " . $this->memoryLimit . " MB");
63 }else{
64 ini_set('memory_limit', '-1');
65 $this->logger->debug("No memory limit set");
66 }
67
68 self::$notifier = $this->sleeperEntry->createNotifier();
69 }
70
71 public function getLogger() : ThreadSafeLogger{
72 return $this->logger;
73 }
74
75 public function getThreadName() : string{
76 return "AsyncWorker#" . $this->id;
77 }
78
79 public function getAsyncWorkerId() : int{
80 return $this->id;
81 }
82
89 public function saveToThreadStore(string $identifier, mixed $value) : void{
90 if(NativeThread::getCurrentThread() !== $this){
91 throw new \LogicException("Thread-local data can only be stored in the thread context");
92 }
93 self::$store[$identifier] = $value;
94 }
95
106 public function getFromThreadStore(string $identifier) : mixed{
107 if(NativeThread::getCurrentThread() !== $this){
108 throw new \LogicException("Thread-local data can only be fetched in the thread context");
109 }
110 return self::$store[$identifier] ?? null;
111 }
112
118 public function removeFromThreadStore(string $identifier) : void{
119 if(NativeThread::getCurrentThread() !== $this){
120 throw new \LogicException("Thread-local data can only be removed in the thread context");
121 }
122 unset(self::$store[$identifier]);
123 }
124}
getFromThreadStore(string $identifier)
removeFromThreadStore(string $identifier)
saveToThreadStore(string $identifier, mixed $value)