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
22
declare(strict_types=1);
23
24
namespace
pocketmine\scheduler
;
25
26
use pmmp\thread\Thread as NativeThread;
27
use
pocketmine\snooze\SleeperHandlerEntry
;
28
use
pocketmine\snooze\SleeperNotifier
;
29
use
pocketmine\thread\log\ThreadSafeLogger
;
30
use
pocketmine\thread\Worker
;
31
use
pocketmine\utils\AssumptionFailedError
;
32
use
function
gc_enable;
33
use
function
ini_set;
34
35
class
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
}
pocketmine\scheduler\AsyncWorker
Definition
AsyncWorker.php:35
pocketmine\scheduler\AsyncWorker\getFromThreadStore
getFromThreadStore(string $identifier)
Definition
AsyncWorker.php:106
pocketmine\scheduler\AsyncWorker\removeFromThreadStore
removeFromThreadStore(string $identifier)
Definition
AsyncWorker.php:118
pocketmine\scheduler\AsyncWorker\saveToThreadStore
saveToThreadStore(string $identifier, mixed $value)
Definition
AsyncWorker.php:89
pocketmine\snooze\SleeperHandlerEntry
Definition
SleeperHandlerEntry.php:33
pocketmine\snooze\SleeperNotifier
Definition
SleeperNotifier.php:23
pocketmine\thread\Worker
Definition
Worker.php:39
pocketmine\thread\log\ThreadSafeLogger
Definition
ThreadSafeLogger.php:28
pocketmine\utils\AssumptionFailedError
Definition
AssumptionFailedError.php:31
pocketmine\scheduler
Definition
AsyncPool.php:24
src
scheduler
AsyncWorker.php
Generated by
1.12.0