Skip to content

Commit 4347fa3

Browse files
committed
Change StreamSelectLoop::streamSelect null-timeout handling.
1 parent 08c709b commit 4347fa3

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/StreamSelectLoop.php

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,14 @@ private function waitForStreamActivity($timeout)
213213
}
214214

215215
/**
216-
* Returns integer amount of seconds in $time or null.
216+
* Returns integer amount of seconds in $time.
217217
*
218-
* @param float|null $time – time in seconds
218+
* @param float $time – time in seconds
219219
*
220-
* @return int|null
220+
* @return int
221221
*/
222222
private static function getSeconds($time)
223223
{
224-
if ($time === null) {
225-
return null;
226-
}
227-
228224
/*
229225
* Workaround for PHP int overflow:
230226
* (float)PHP_INT_MAX == PHP_INT_MAX => true
@@ -239,37 +235,30 @@ private static function getSeconds($time)
239235
}
240236

241237
/**
242-
* Returns integer amount of microseconds in $time or null.
238+
* Returns integer amount of microseconds in $time.
243239
*
244-
* @param float|null $time – time in seconds
240+
* @param float $time – time in seconds
245241
*
246-
* @return int|null
242+
* @return int
247243
*/
248244
private static function getMicroseconds($time)
249245
{
250-
if ($time === null) {
251-
return null;
252-
}
253246
$fractional = fmod($time, 1);
254247
$microseconds = round($fractional * self::MICROSECONDS_PER_SECOND);
255248

256249
return intval($microseconds);
257250
}
258251

259252
/**
260-
* Returns integer amount of nanoseconds in $time or null.
253+
* Returns integer amount of nanoseconds in $time.
261254
* The precision is 1 microsecond.
262255
*
263-
* @param float|null $time – time in seconds
256+
* @param float $time – time in seconds
264257
*
265-
* @return int|null
258+
* @return int
266259
*/
267260
private static function getNanoseconds($time)
268261
{
269-
if ($time === null) {
270-
return null;
271-
}
272-
273262
return intval(self::getMicroseconds($time) * self::NANOSECONDS_PER_MICROSECOND);
274263
}
275264

@@ -286,17 +275,19 @@ private static function getNanoseconds($time)
286275
*/
287276
protected function streamSelect(array &$read, array &$write, $timeout)
288277
{
289-
$seconds = self::getSeconds($timeout);
290-
$microseconds = self::getMicroseconds($timeout);
291-
$nanoseconds = self::getNanoseconds($timeout);
278+
$seconds = $timeout === null ? null : self::getSeconds($timeout);
279+
$microseconds = $timeout === null ? 0 : self::getMicroseconds($timeout);
280+
$nanoseconds = $timeout === null ? 0 : self::getNanoseconds($timeout);
292281

293282
if ($read || $write) {
294283
$except = [];
295284

296285
return $this->doSelectStream($read, $write, $except, $seconds, $microseconds);
297286
}
298287

299-
$this->sleep($seconds, $nanoseconds);
288+
if ($timeout !== null) {
289+
$this->sleep($seconds, $nanoseconds);
290+
}
300291

301292
return 0;
302293
}
@@ -308,11 +299,11 @@ protected function streamSelect(array &$read, array &$write, $timeout)
308299
* @param array $write
309300
* @param array $except
310301
* @param int|null $seconds
311-
* @param int|null $microseconds
302+
* @param int $microseconds
312303
*
313304
* @return int
314305
*/
315-
protected function doSelectStream(array &$read, array &$write, array &$except, $seconds, $microseconds = null)
306+
protected function doSelectStream(array &$read, array &$write, array &$except, $seconds, $microseconds)
316307
{
317308
// suppress warnings that occur, when stream_select is interrupted by a signal
318309
return @stream_select($read, $write, $except, $seconds, $microseconds);
@@ -321,14 +312,11 @@ protected function doSelectStream(array &$read, array &$write, array &$except, $
321312
/**
322313
* Sleeps for $seconds and $nanoseconds.
323314
*
324-
* @param int|null $seconds
325-
* @param int|null $nanoseconds
315+
* @param int $seconds
316+
* @param int $nanoseconds
326317
*/
327318
protected function sleep($seconds, $nanoseconds = 0)
328319
{
329-
if ($seconds === null || $nanoseconds === null) {
330-
return;
331-
}
332320
if ($seconds > 0 || $nanoseconds > 0) {
333321
time_nanosleep($seconds, $nanoseconds);
334322
}

0 commit comments

Comments
 (0)