react-php/react — pull request #14 by nrk (2012-05-11 18:38:15+00:00)
Title: [EventLoop] Implement timers for the stream_select() based loop.

Timers can be one-shot or periodic and both are cancellable by using the signature returned when adding them to the event loop. Periodic timers can be cancelled also by returing FALSE from the provided callback.

The event loop will continue to run even without any registered stream until there is at least one timer scheduled for execution or the loop is explicitly stopped.

Certain aspects of timers are inspired by how EventMachine is internally designed. We also return a signature instead of a full-blown object for timers because this is probably the simplest way to abstract implementations that would greatly differ, such as with the libevent or libev extensions. A full-blown timer class can still be implemented on top of the event loop.

Timers can have a minimum resolution of 1ms, this value should be plenty enough. Even with 10 periodic timers set to fire an empty callback each 1ms the CPU usage is limited to ~6% using PHP 5.4.0 on the Linux VM running on my MacBook Pro.