title: Libevent timeout loop exit
body: <p>I'm having some difficulties getting the PHP libevent extension to break out of a loop on a timeout.  Here's what I've got so far based on the demos on the PHP.net docs pages:</p>
  
  <pre><code>// From here: http://www.php.net/manual/en/libevent.examples.php
  
  function print_line($fd, $events, $arg) {
      static $max_requests = 0;
      $max_requests++;
  
      printf("Received event: %s after %s\n%s", implode(getEventFlags($events)), getTimer(), fgets($fd));
  
      if ($max_requests == 10) {
          // exit loop after 3 writes
          echo " [EXIT]\n";
          event_base_loopexit($arg[1]);
      }
  }
  
  // create base and event
  $base = event_base_new();
  $event = event_new();
  
  
  getTimer(); // Initialise time
  
  $fd = STDIN;
  event_set($event, $fd, EV_READ | EV_PERSIST, "print_line", array($event, $base));
  event_base_set($event, $base);
  event_add($event, 2000000);
  event_base_loop($base);
  
  // extract flags from bitmask
  function getEventFlags ($ebm) {
      $expFlags = array('EV_TIMEOUT', 'EV_SIGNAL', 'EV_READ', 'EV_WRITE', 'EV_PERSIST');
      $ret = array();
      foreach ($expFlags as $exf) {
          if ($ebm &amp; constant($exf)) {
              $ret[] = $exf;
          }
      }
      return $ret;
  }
  
  // Used to track time!
  function getTimer () {
      static $ts;
      if (is_null($ts)) {
          $ts = microtime(true);
          return "Timer initialised";
      }
      $newts = microtime(true);
      $r = sprintf("Delta: %.3f", $newts - $ts);
      $ts = $newts;
      return $r;
  }
  </code></pre>
  
  <p>I can see that the timeout value passed to <code>event_add</code> effects the events passed to print_line(), if these events are any more than 2 seconds apart I get an <code>EV_TIMEOUT</code> instead of an <code>EV_READ</code>.  What I want however is for libevent to call <code>print_line</code> as soon as the timeout is reached rather than waiting for the next event in order to give me the timeout.</p>
  
  <p>I've tried using <code>event_base_loopexit($base, 2000000)</code>, this causes the event loop to exit immediately without blocking for events.  I've also tried passing <code>EV_TIMEOUT</code> to <code>event_set</code>, this seems to have no effect at all.</p>
  
  <p>Has anyone managed to get this working before?  I know that the event_buffer_* stuff works with timeouts, however I want to use the standard <code>event_base</code> functions.  One of the <a href="https://bugs.php.net/bug.php?id=59627" rel="nofollow">PECL bugs</a> talks about event_timer_* functions and these functions do exist on my system, however they're not documented at all.</p>