-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImmidiatePool.php
More file actions
79 lines (64 loc) · 1.78 KB
/
ImmidiatePool.php
File metadata and controls
79 lines (64 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace ReactParallel\Tests\Tests;
use ReactParallel\Contracts\ClosedException;
use ReactParallel\Contracts\PoolInterface;
use ReactParallel\EventLoop\EventLoopBridge;
use ReactParallel\Runtime\Runtime;
use WyriHaximus\PoolInfo\Info;
final class ImmidiatePool implements PoolInterface
{
private int $activeThreads = 0;
private bool $closed = false;
/** @var array<Runtime> */
private array $runtimes = [];
public function __construct(private readonly EventLoopBridge $eventLoopBridge)
{
}
/**
* {@inheritDoc}
*/
public function info(): iterable
{
yield Info::TOTAL => $this->activeThreads;
yield Info::BUSY => $this->activeThreads;
yield Info::CALLS => 0;
yield Info::IDLE => 0;
yield Info::SIZE => $this->activeThreads;
}
/**
* {@inheritDoc}
*/
public function run(\Closure $callable, array $args = []): mixed
{
if ($this->closed === true) {
throw ClosedException::create();
}
$runtime = Runtime::create($this->eventLoopBridge);
$this->runtimes[\spl_object_id($runtime)] = $runtime;
$this->activeThreads++;
try {
$result = $runtime->run($callable, $args);
} finally {
unset($this->runtimes[\spl_object_id($runtime)]);
}
$this->activeThreads--;
return $result;
}
public function close(): bool
{
$this->closed = true;
foreach ($this->runtimes as $runtime) {
$runtime->close();
}
return true;
}
public function kill(): bool
{
$this->closed = true;
foreach ($this->runtimes as $runtime) {
$runtime->kill();
}
return true;
}
}