# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2006,2007 -- leonerd@leonerd.org.uk package IO::Async::TimeQueue; use strict; our $VERSION = '0.09'; use Carp; use Heap::Fibonacci; BEGIN { if ( eval { Time::HiRes::time(); 1 } ) { Time::HiRes->import( qw( time ) ); } } =head1 NAME C - a class which implements a queue of future timed event callbacks =head1 DESCRIPTION This class is not intended to be used by external code; it is used by C and C to implement the timer features. =cut =head1 CONSTRUCTOR =cut =head2 $queue = IO::Async::TimeQueue->new() =cut sub new { my $class = shift; my ( %params ) = @_; my $self = bless { heap => Heap::Fibonacci->new, }, $class; return $self; } =head1 METHODS =cut =head2 $time = $queue->next_time Returns the time of the next event on the queue, or C if no events are left. =cut sub next_time { my $self = shift; my $heap = $self->{heap}; my $top = $heap->top; return defined $top ? $top->time : undef; } =head2 $id = $queue->enqueue( %params ) Adds a new event to the queue. An ID value is returned, which may be passed to the C method to cancel this timer. This value may be an object reference, so care should be taken not to store it unless it is required. If it is stored, it should be released after the timer code has fired, or it has been canceled, in order to free the object itself. The C<%params> takes the following keys: =over 8 =item time => NUM The absolute system timestamp to run the event. =item delay => NUM The delay after now at which to run the event. =item now => NUM The time to consider as now; defaults to C if not specified. =item code => CODE CODE reference to the callback function to run at the allotted time. =back Either C