PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
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
33use function ini_set;
34
35class AsyncWorker extends Worker{
36 private static ?SleeperNotifier $notifier = null;
37 private static ?GarbageCollectorManager $cycleGcManager = null;
38
39 public function __construct(
40 private ThreadSafeLogger $logger,
41 private int $id,
42 private int $memoryLimit,
43 private SleeperHandlerEntry $sleeperEntry
44 ){}
45
46 public static function getNotifier() : SleeperNotifier{
47 if(self::$notifier !== null){
48 return self::$notifier;
49 }
50 throw new AssumptionFailedError("SleeperNotifier not found in thread-local storage");
51 }
52
53 public static function maybeCollectCycles() : void{
54 if(self::$cycleGcManager === null){
55 throw new AssumptionFailedError("GarbageCollectorManager not found in thread-local storage");
56 }
57 self::$cycleGcManager->maybeCollectCycles();
58 }
59
60 protected function onRun() : void{
61 \GlobalLogger::set($this->logger);
62
63 if($this->memoryLimit > 0){
64 ini_set('memory_limit', $this->memoryLimit . 'M');
65 $this->logger->debug("Set memory limit to " . $this->memoryLimit . " MB");
66 }else{
67 ini_set('memory_limit', '-1');
68 $this->logger->debug("No memory limit set");
69 }
70
71 self::$notifier = $this->sleeperEntry->createNotifier();
72 Timings::init();
73 self::$cycleGcManager = new GarbageCollectorManager($this->logger, Timings::$asyncTaskWorkers);
74 }
75
76 public function getLogger() : ThreadSafeLogger{
77 return $this->logger;
78 }
79
80 public function getThreadName() : string{
81 return "AsyncWorker#" . $this->id;
82 }
83
84 public function getAsyncWorkerId() : int{
85 return $this->id;
86 }
87}