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.129 by elmex, Sat May 24 15:19:30 2008 UTC vs.
Revision 1.134 by root, Sun May 25 04:44:04 2008 UTC

311>> method, usually without arguments. The only argument pair allowed is 311>> method, usually without arguments. The only argument pair allowed is
312C<cb>, which specifies a callback to be called when the condition variable 312C<cb>, which specifies a callback to be called when the condition variable
313becomes true. 313becomes true.
314 314
315After creation, the condition variable is "false" until it becomes "true" 315After creation, the condition variable is "false" until it becomes "true"
316by calling the C<send> method. 316by calling the C<send> method (or calling the condition variable as if it
317were a callback).
317 318
318Condition variables are similar to callbacks, except that you can 319Condition variables are similar to callbacks, except that you can
319optionally wait for them. They can also be called merge points - points 320optionally wait for them. They can also be called merge points - points
320in time where multiple outstanding events have been processed. And yet 321in time where multiple outstanding events have been processed. And yet
321another way to call them is transactions - each condition variable can be 322another way to call them is transactions - each condition variable can be
347 348
348There are two "sides" to a condition variable - the "producer side" which 349There are two "sides" to a condition variable - the "producer side" which
349eventually calls C<< -> send >>, and the "consumer side", which waits 350eventually calls C<< -> send >>, and the "consumer side", which waits
350for the send to occur. 351for the send to occur.
351 352
352Example: 353Example: wait for a timer.
353 354
354 # wait till the result is ready 355 # wait till the result is ready
355 my $result_ready = AnyEvent->condvar; 356 my $result_ready = AnyEvent->condvar;
356 357
357 # do something such as adding a timer 358 # do something such as adding a timer
365 366
366 # this "blocks" (while handling events) till the callback 367 # this "blocks" (while handling events) till the callback
367 # calls send 368 # calls send
368 $result_ready->recv; 369 $result_ready->recv;
369 370
371Example: wait for a timer, but take advantage of the fact that
372condition variables are also code references.
373
374 my $done = AnyEvent->condvar;
375 my $delay = AnyEvent->timer (after => 5, cb => $done);
376 $done->recv;
377
370=head3 METHODS FOR PRODUCERS 378=head3 METHODS FOR PRODUCERS
371 379
372These methods should only be used by the producing side, i.e. the 380These methods should only be used by the producing side, i.e. the
373code/module that eventually sends the signal. Note that it is also 381code/module that eventually sends the signal. Note that it is also
374the producer side which creates the condvar in most cases, but it isn't 382the producer side which creates the condvar in most cases, but it isn't
385If a callback has been set on the condition variable, it is called 393If a callback has been set on the condition variable, it is called
386immediately from within send. 394immediately from within send.
387 395
388Any arguments passed to the C<send> call will be returned by all 396Any arguments passed to the C<send> call will be returned by all
389future C<< ->recv >> calls. 397future C<< ->recv >> calls.
398
399Condition variables are overloaded so one can call them directly (as a
400code reference). Calling them directly is the same as calling C<send>.
390 401
391=item $cv->croak ($error) 402=item $cv->croak ($error)
392 403
393Similar to send, but causes all call's to C<< ->recv >> to invoke 404Similar to send, but causes all call's to C<< ->recv >> to invoke
394C<Carp::croak> with the given error message/object/scalar. 405C<Carp::croak> with the given error message/object/scalar.
601 612
602If it doesn't care, it can just "use AnyEvent" and use it itself, or not 613If it doesn't care, it can just "use AnyEvent" and use it itself, or not
603do anything special (it does not need to be event-based) and let AnyEvent 614do anything special (it does not need to be event-based) and let AnyEvent
604decide which implementation to chose if some module relies on it. 615decide which implementation to chose if some module relies on it.
605 616
606If the main program relies on a specific event model. For example, in 617If the main program relies on a specific event model - for example, in
607Gtk2 programs you have to rely on the Glib module. You should load the 618Gtk2 programs you have to rely on the Glib module - you should load the
608event module before loading AnyEvent or any module that uses it: generally 619event module before loading AnyEvent or any module that uses it: generally
609speaking, you should load it as early as possible. The reason is that 620speaking, you should load it as early as possible. The reason is that
610modules might create watchers when they are loaded, and AnyEvent will 621modules might create watchers when they are loaded, and AnyEvent will
611decide on the event model to use as soon as it creates watchers, and it 622decide on the event model to use as soon as it creates watchers, and it
612might chose the wrong one unless you load the correct one yourself. 623might chose the wrong one unless you load the correct one yourself.
613 624
614You can chose to use a rather inefficient pure-perl implementation by 625You can chose to use a pure-perl implementation by loading the
615loading the C<AnyEvent::Impl::Perl> module, which gives you similar 626C<AnyEvent::Impl::Perl> module, which gives you similar behaviour
616behaviour everywhere, but letting AnyEvent chose is generally better. 627everywhere, but letting AnyEvent chose the model is generally better.
628
629=head2 MAINLOOP EMULATION
630
631Sometimes (often for short test scripts, or even standalone programs who
632only want to use AnyEvent), you do not want to run a specific event loop.
633
634In that case, you can use a condition variable like this:
635
636 AnyEvent->condvar->recv;
637
638This has the effect of entering the event loop and looping forever.
639
640Note that usually your program has some exit condition, in which case
641it is better to use the "traditional" approach of storing a condition
642variable somewhere, waiting for it, and sending it when the program should
643exit cleanly.
644
617 645
618=head1 OTHER MODULES 646=head1 OTHER MODULES
619 647
620The following is a non-exhaustive list of additional modules that use 648The following is a non-exhaustive list of additional modules that use
621AnyEvent and can therefore be mixed easily with other AnyEvent modules 649AnyEvent and can therefore be mixed easily with other AnyEvent modules
637 665
638Provides various utility functions for (internet protocol) sockets, 666Provides various utility functions for (internet protocol) sockets,
639addresses and name resolution. Also functions to create non-blocking tcp 667addresses and name resolution. Also functions to create non-blocking tcp
640connections or tcp servers, with IPv6 and SRV record support and more. 668connections or tcp servers, with IPv6 and SRV record support and more.
641 669
670=item L<AnyEvent::DNS>
671
672Provides rich asynchronous DNS resolver capabilities.
673
642=item L<AnyEvent::HTTPD> 674=item L<AnyEvent::HTTPD>
643 675
644Provides a simple web application server framework. 676Provides a simple web application server framework.
645
646=item L<AnyEvent::DNS>
647
648Provides rich asynchronous DNS resolver capabilities.
649 677
650=item L<AnyEvent::FastPing> 678=item L<AnyEvent::FastPing>
651 679
652The fastest ping in the west. 680The fastest ping in the west.
653 681
696no warnings; 724no warnings;
697use strict; 725use strict;
698 726
699use Carp; 727use Carp;
700 728
701our $VERSION = '3.6'; 729our $VERSION = '4.03';
702our $MODEL; 730our $MODEL;
703 731
704our $AUTOLOAD; 732our $AUTOLOAD;
705our @ISA; 733our @ISA;
706 734
914 942
915our @ISA = AnyEvent::CondVar::Base::; 943our @ISA = AnyEvent::CondVar::Base::;
916 944
917package AnyEvent::CondVar::Base; 945package AnyEvent::CondVar::Base;
918 946
947use overload
948 '&{}' => sub { my $self = shift; sub { $self->send (@_) } },
949 fallback => 1;
950
919sub _send { 951sub _send {
920 # nop 952 # nop
921} 953}
922 954
923sub send { 955sub send {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines