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.130 by root, Sat May 24 17:21:50 2008 UTC vs.
Revision 1.131 by root, Sat May 24 17:48:38 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.
914 925
915our @ISA = AnyEvent::CondVar::Base::; 926our @ISA = AnyEvent::CondVar::Base::;
916 927
917package AnyEvent::CondVar::Base; 928package AnyEvent::CondVar::Base;
918 929
930use overload
931 '&{}' => sub { my $self = shift; sub { $self->send (@_) } },
932 fallback => 1;
933
919sub _send { 934sub _send {
920 # nop 935 # nop
921} 936}
922 937
923sub send { 938sub send {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines