-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathEventDriver.php
More file actions
244 lines (206 loc) · 7.29 KB
/
EventDriver.php
File metadata and controls
244 lines (206 loc) · 7.29 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
declare(strict_types=1);
/** @noinspection PhpComposerExtensionStubsInspection */
namespace Revolt\EventLoop\Driver;
use Revolt\EventLoop\FiberFactory;
use Revolt\EventLoop\Internal\AbstractDriver;
use Revolt\EventLoop\Internal\DriverCallback;
use Revolt\EventLoop\Internal\SignalCallback;
use Revolt\EventLoop\Internal\StreamCallback;
use Revolt\EventLoop\Internal\StreamReadableCallback;
use Revolt\EventLoop\Internal\StreamWritableCallback;
use Revolt\EventLoop\Internal\TimerCallback;
final class EventDriver extends AbstractDriver
{
/** @var array<string, \Event>|null */
private static ?array $activeSignals = null;
public static function isSupported(): bool
{
return \extension_loaded("event");
}
private \EventBase $handle;
/** @var array<string, \Event> */
private array $events = [];
private readonly \Closure $ioCallback;
private readonly \Closure $timerCallback;
private readonly \Closure $signalCallback;
/** @var array<string, \Event> */
private array $signals = [];
public function __construct(?FiberFactory $fiberFactory = null)
{
parent::__construct($fiberFactory);
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
$this->handle = new \EventBase();
if (self::$activeSignals === null) {
self::$activeSignals = &$this->signals;
}
$this->ioCallback = function ($resource, $what, StreamCallback $callback): void {
$this->enqueueCallback($callback);
};
$this->timerCallback = function ($resource, $what, TimerCallback $callback): void {
$this->enqueueCallback($callback);
};
$this->signalCallback = function ($signo, $what, SignalCallback $callback): void {
$this->enqueueCallback($callback);
};
}
/**
* {@inheritdoc}
*/
public function cancel(string $callbackId): void
{
parent::cancel($callbackId);
if (isset($this->events[$callbackId])) {
$this->events[$callbackId]->free();
unset($this->events[$callbackId]);
}
}
/**
* @codeCoverageIgnore
*/
public function __destruct()
{
foreach ($this->events as $event) {
if ($event !== null) { // Events may have been nulled in extension depending on destruct order.
$event->free();
}
}
// Unset here, otherwise $event->del() fails with a warning, because __destruct order isn't defined.
// See https://github.com/amphp/amp/issues/159.
$this->events = [];
// Manually free the loop handle to fully release loop resources.
// See https://github.com/amphp/amp/issues/177.
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (isset($this->handle)) {
$this->handle->free();
unset($this->handle);
}
}
/**
* {@inheritdoc}
*/
public function run(): void
{
$active = self::$activeSignals;
\assert($active !== null);
foreach ($active as $event) {
$event->del();
}
self::$activeSignals = &$this->signals;
foreach ($this->signals as $event) {
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
$event->add();
}
try {
parent::run();
} finally {
foreach ($this->signals as $event) {
$event->del();
}
self::$activeSignals = &$active;
foreach ($active as $event) {
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
$event->add();
}
}
}
/**
* {@inheritdoc}
*/
public function stop(): void
{
$this->handle->stop();
parent::stop();
}
/**
* {@inheritdoc}
*/
public function getHandle(): \EventBase
{
return $this->handle;
}
protected function now(): float
{
return (float) \hrtime(true) / 1_000_000_000;
}
/**
* {@inheritdoc}
*/
protected function dispatch(bool $blocking): void
{
$this->handle->loop($blocking ? \EventBase::LOOP_ONCE : \EventBase::LOOP_ONCE | \EventBase::LOOP_NONBLOCK);
}
/**
* {@inheritdoc}
*/
protected function activate(array $callbacks): void
{
$now = $this->now();
foreach ($callbacks as $callback) {
if (!isset($this->events[$id = $callback->id])) {
if ($callback instanceof StreamReadableCallback) {
\assert(\is_resource($callback->stream));
$this->events[$id] = new \Event(
$this->handle,
$callback->stream,
\Event::READ | \Event::PERSIST,
$this->ioCallback,
$callback
);
} elseif ($callback instanceof StreamWritableCallback) {
\assert(\is_resource($callback->stream));
$this->events[$id] = new \Event(
$this->handle,
$callback->stream,
\Event::WRITE | \Event::PERSIST,
$this->ioCallback,
$callback
);
} elseif ($callback instanceof TimerCallback) {
$this->events[$id] = new \Event(
$this->handle,
-1,
\Event::TIMEOUT,
$this->timerCallback,
$callback
);
} elseif ($callback instanceof SignalCallback) {
$this->events[$id] = new \Event(
$this->handle,
$callback->signal,
\Event::SIGNAL | \Event::PERSIST,
$this->signalCallback,
$callback
);
} else {
// @codeCoverageIgnoreStart
throw new \Error("Unknown callback type");
// @codeCoverageIgnoreEnd
}
}
if ($callback instanceof TimerCallback) {
$interval = \min(\max(0, $callback->expiration - $now), \PHP_INT_MAX / 2);
$this->events[$id]->add($interval > 0 ? $interval : 0);
} elseif ($callback instanceof SignalCallback) {
$this->signals[$id] = $this->events[$id];
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
$this->events[$id]->add();
} else {
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
$this->events[$id]->add();
}
}
}
/**
* {@inheritdoc}
*/
protected function deactivate(DriverCallback $callback): void
{
if (isset($this->events[$id = $callback->id])) {
$this->events[$id]->del();
if ($callback instanceof SignalCallback) {
unset($this->signals[$id]);
}
}
}
}