8000 Merge pull request #182 from clue-labs/hrtime · reedy/reactphp-event-loop@f15ba6f · GitHub
[go: up one dir, main page]

Skip to content

Commit f15ba6f

Browse files
authored
Merge pull request reactphp#182 from clue-labs/hrtime
Use high resolution timer on PHP 7.3+
2 parents 7016d65 + 29bf39c commit f15ba6f

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,14 @@ It is commonly installed as part of many PHP distributions.
182182
If this extension is missing (or you're running on Windows), signal handling is
183183
not supported and throws a `BadMethodCallException` instead.
184184

185-
This event loop is known to rely on wall-clock time to schedule future
186-
timers, because a monotonic time source is not available in PHP by default.
185+
This event loop is known to rely on wall-clock time to schedule future timers
186+
when using any version before PHP 7.3, because a monotonic time source is
187+
only available as of PHP 7.3 (`hrtime()`).
187188
While this does not affect many common use cases, this is an important
188189
distinction for programs that rely on a high time precision or on systems
189190
that are subject to discontinuous time adjustments (time jumps).
190-
This means that if you schedule a timer to trigger in 30s and then adjust
191-
your system time forward by 20s, the timer may trigger in 10s.
191+
This means that if you schedule a timer to trigger in 30s on PHP < 7.3 and
192+
then adjust your system time forward by 20s, the timer may trigger in 10s.
192193
See also [`addTimer()`](#addtimer) for more details.
193194

194195
#### ExtEventLoop
@@ -360,8 +361,8 @@ same time (within its possible accuracy) is not guaranteed.
360361

361362
This interface suggests that event loop implementations SHOULD use a
362363
monotonic time source if available. Given that a monotonic time source is
363-
not available on PHP by default, event loop implementations MAY fall back
364-
to using wall-clock time.
364+
only available as of PHP 7.3 by default, event loop implementations MAY
365+
fall back to using wall-clock time.
365366
While this does not affect many common use cases, this is an important
366367
distinction for programs that rely on a high time precision or on systems
367368
that are subject to discontinuous time adjustments (time jumps).
@@ -433,8 +434,8 @@ same time (within its possible accuracy) is not guaranteed.
433434

434435
This interface suggests that event loop implementations SHOULD use a
435436
monotonic time source if available. Given that a monotonic time source is
436-
not available on PHP by default, event loop implementations MAY fall back
437-
to using wall-clock time.
437+
only available as of PHP 7.3 by default, event loop implementations MAY
438+
fall back to using wall-clock time.
438439
While this does not affect many common use cases, this is an important
439440
distinction for programs that rely on a high time precision or on systems
440441
that are subject to discontinuous time adjustments (time jumps).

src/LoopInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ public function removeWriteStream($stream);
185185
*
186186
* This interface suggests that event loop implementations SHOULD use a
187187
* monotonic time source if available. Given that a monotonic time source is
188-
* not available on PHP by default, event loop implementations MAY fall back
189-
* to using wall-clock time.
188+
* only available as of PHP 7.3 by default, event loop implementations MAY
189+
* fall back to using wall-clock time.
190190
* While this does not affect many common use cases, this is an important
191191
* distinction for programs that rely on a high time precision or on systems
192192
* that are subject to discontinuous time adjustments (time jumps).
@@ -263,8 +263,8 @@ public function addTimer($interval, $callback);
263263
*
264264
* This interface suggests that event loop implementations SHOULD use a
265265
* monotonic time source if available. Given that a monotonic time source is
266-
* not available on PHP by default, event loop implementations MAY fall back
267-
* to using wall-clock time.
266+
* only available as of PHP 7.3 by default, event loop implementations MAY
267+
* fall back to using wall-clock time.
268268
* While this does not affect many common use cases, this is an important
269269
* distinction for programs that rely on a high time precision or on systems
270270
* that are subject to discontinuous time adjustments (time jumps).

src/StreamSelectLoop.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
* If this extension is missing (or you're running on Windows), signal handling is
3939
* not supported and throws a `BadMethodCallException` instead.
4040
*
41-
* This event loop is known to rely on wall-clock time to schedule future
42-
* timers, because a monotonic time source is not available in PHP by default.
41+
* This event loop is known to rely on wall-clock time to schedule future timers
42+
* when using any version before PHP 7.3, because a monotonic time source is
43+
* only available as of PHP 7.3 (`hrtime()`).
4344
* While this does not affect many common use cases, this is an important
4445
* distinction for programs that rely on a high time precision or on systems
4546
* that are subject to discontinuous time adjustments (time jumps).
46-
* This means that if you schedule a timer to trigger in 30s and then adjust
47-
* your system time forward by 20s, the timer may trigger in 10s.
47+
* This means that if you schedule a timer to trigger in 30s on PHP < 7.3 and
48+
* then adjust your system time forward by 20s, the timer may trigger in 10s.
4849
* See also [`addTimer()`](#addtimer) for more details.
4950
*
5051
* @link http://php.net/manual/en/function.stream-select.php

src/Timer/Timers.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ final class Timers
1818
private $timers = array();
1919
private $schedule = array();
2020
private $sorted = true;
21+
private $useHighResolution;
22+
23+
public function __construct()
24+
{
25+
// prefer high-resolution timer, available as of PHP 7.3+
26+
$this->useHighResolution = \function_exists('hrtime');
27+
}
2128

2229
public function updateTime()
2330
{
24-
return $this->time = \microtime(true);
31+
return $this->time = $this->useHighResolution ? \hrtime(true) * 1e-9 : \microtime(true);
2532
}
2633

2734
public function getTime()
@@ -33,7 +40,7 @@ public function add(TimerInterface $timer)
3340
{
3441
$id = \spl_object_hash($timer);
3542
$this->timers[$id] = $timer;
36-
$this->schedule[$id] = $timer->getInterval() + \microtime(true);
43+
$this->schedule[$id] = $timer->getInterval() + $this->updateTime();
3744
$this->sorted = false;
3845
}
3946

0 commit comments

Comments
 (0)
0