ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.142 by root, Tue May 27 02:34:30 2008 UTC vs.
Revision 1.147 by root, Fri May 30 21:43:26 2008 UTC

233timers. 233timers.
234 234
235AnyEvent always prefers relative timers, if available, matching the 235AnyEvent always prefers relative timers, if available, matching the
236AnyEvent API. 236AnyEvent API.
237 237
238AnyEvent has two additional methods that return the "current time":
239
240=over 4
241
242=item AnyEvent->time
243
244This returns the "current wallclock time" as a fractional number of
245seconds since the Epoch (the same thing as C<time> or C<Time::HiRes::time>
246return, and the result is guaranteed to be compatible with those).
247
248It progresses independently of any event loop processing, i.e. each call
249will check the system clock, which usually gets updated frequently.
250
251=item AnyEvent->now
252
253This also returns the "current wallclock time", but unlike C<time>, above,
254this value might change only once per event loop iteration, depending on
255the event loop (most return the same time as C<time>, above). This is the
256time that AnyEvent's timers get scheduled against.
257
258I<In almost all cases (in all cases if you don't care), this is the
259function to call when you want to know the current time.>
260
261This function is also often faster then C<< AnyEvent->time >>, and
262thus the preferred method if you want some timestamp (for example,
263L<AnyEvent::Handle> uses this to update it's activity timeouts).
264
265The rest of this section is only of relevance if you try to be very exact
266with your timing, you can skip it without bad conscience.
267
268For a practical example of when these times differ, consider L<Event::Lib>
269and L<EV> and the following set-up:
270
271The event loop is running and has just invoked one of your callback at
272time=500 (assume no other callbacks delay processing). In your callback,
273you wait a second by executing C<sleep 1> (blocking the process for a
274second) and then (at time=501) you create a relative timer that fires
275after three seconds.
276
277With L<Event::Lib>, C<< AnyEvent->time >> and C<< AnyEvent->now >> will
278both return C<501>, because that is the current time, and the timer will
279be scheduled to fire at time=504 (C<501> + C<3>).
280
281With L<EV>, C<< AnyEvent->time >> returns C<501> (as that is the current
282time), but C<< AnyEvent->now >> returns C<500>, as that is the time the
283last event processing phase started. With L<EV>, your timer gets scheduled
284to run at time=503 (C<500> + C<3>).
285
286In one sense, L<Event::Lib> is more exact, as it uses the current time
287regardless of any delays introduced by event processing. However, most
288callbacks do not expect large delays in processing, so this causes a
289higher drift (and a lot more system calls to get the current time).
290
291In another sense, L<EV> is more exact, as your timer will be scheduled at
292the same time, regardless of how long event processing actually took.
293
294In either case, if you care (and in most cases, you don't), then you
295can get whatever behaviour you want with any event loop, by taking the
296difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into
297account.
298
299=back
300
238=head2 SIGNAL WATCHERS 301=head2 SIGNAL WATCHERS
239 302
240You can watch for signals using a signal watcher, C<signal> is the signal 303You can watch for signals using a signal watcher, C<signal> is the signal
241I<name> without any C<SIG> prefix, C<cb> is the Perl callback to 304I<name> without any C<SIG> prefix, C<cb> is the Perl callback to
242be invoked whenever a signal occurs. 305be invoked whenever a signal occurs.
737no warnings; 800no warnings;
738use strict; 801use strict;
739 802
740use Carp; 803use Carp;
741 804
742our $VERSION = '4.05'; 805our $VERSION = 4.11;
743our $MODEL; 806our $MODEL;
744 807
745our $AUTOLOAD; 808our $AUTOLOAD;
746our @ISA; 809our @ISA;
747 810
779 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza 842 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza
780 [Wx:: => AnyEvent::Impl::POE::], 843 [Wx:: => AnyEvent::Impl::POE::],
781 [Prima:: => AnyEvent::Impl::POE::], 844 [Prima:: => AnyEvent::Impl::POE::],
782); 845);
783 846
784our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); 847our %method = map +($_ => 1), qw(io timer time now signal child condvar one_event DESTROY);
785 848
786our @post_detect; 849our @post_detect;
787 850
788sub post_detect(&) { 851sub post_detect(&) {
789 my ($cb) = @_; 852 my ($cb) = @_;
873 $class->$func (@_); 936 $class->$func (@_);
874} 937}
875 938
876package AnyEvent::Base; 939package AnyEvent::Base;
877 940
941# default implementation for now and time
942
943use Time::HiRes ();
944
945sub time { Time::HiRes::time }
946sub now { Time::HiRes::time }
947
878# default implementation for ->condvar 948# default implementation for ->condvar
879 949
880sub condvar { 950sub condvar {
881 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: 951 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar::
882} 952}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines