public static function waitForActivity()

in src/channel/PhutilChannel.php [128:198]


  public static function waitForActivity(
    array $reads,
    array $writes,
    array $options = array()) {

    assert_instances_of($reads, __CLASS__);
    assert_instances_of($writes, __CLASS__);

    $read   = idx($options, 'read',     array());
    $write  = idx($options, 'write',    array());
    $except = idx($options, 'except',   array());
    $wait   = idx($options, 'timeout',  1);

    // TODO: It would be nice to just be able to categorically reject these as
    // unselectable.
    foreach (array($reads, $writes) as $channels) {
      foreach ($channels as $channel) {
        $r_sockets = $channel->getReadSockets();
        $w_sockets = $channel->getWriteSockets();

        // If any channel has no read sockets and no write sockets, assume it
        // isn't selectable and return immediately (effectively degrading to a
        // busy wait).

        if (!$r_sockets && !$w_sockets) {
          return false;
        }
      }
    }

    foreach ($reads as $channel) {
      // If any of the read channels have data in read buffers, return
      // immediately. If we don't, we risk running select() on a bunch of
      // sockets which won't  become readable because the data the application
      // expects is already in a read buffer.

      if (!$channel->isReadBufferEmpty()) {
        return;
      }

      $r_sockets = $channel->getReadSockets();
      foreach ($r_sockets as $socket) {
        $read[] = $socket;
        $except[] = $socket;
      }
    }

    foreach ($writes as $channel) {
      if ($channel->isWriteBufferEmpty()) {
        // If the channel's write buffer is empty, don't select the write
        // sockets, since they're writable immediately.
        $w_sockets = array();
      } else {
        $w_sockets = $channel->getWriteSockets();
      }

      foreach ($w_sockets as $socket) {
        $write[] = $socket;
        $except[] = $socket;
      }
    }

    if (!$read && !$write && !$except) {
      return false;
    }

    $wait_sec = (int)$wait;
    $wait_usec = 1000000 * ($wait - $wait_sec);

    @stream_select($read, $write, $except, $wait_sec, $wait_usec);
  }