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

Comparing AnyEvent-MP/MP.pm (file contents):
Revision 1.40 by root, Sat Aug 8 00:22:16 2009 UTC vs.
Revision 1.43 by root, Sun Aug 9 16:08:16 2009 UTC

115 115
116use AE (); 116use AE ();
117 117
118use base "Exporter"; 118use base "Exporter";
119 119
120our $VERSION = '0.1'; 120our $VERSION = $AnyEvent::MP::Base::VERSION;
121
121our @EXPORT = qw( 122our @EXPORT = qw(
122 NODE $NODE *SELF node_of _any_ 123 NODE $NODE *SELF node_of _any_
123 resolve_node initialise_node 124 resolve_node initialise_node
124 snd rcv mon kil reg psub spawn 125 snd rcv mon kil reg psub spawn
125 port 126 port
501 502
502=item $guard = mon $port 503=item $guard = mon $port
503 504
504=item $guard = mon $port, $rcvport, @msg 505=item $guard = mon $port, $rcvport, @msg
505 506
506Monitor the given port and do something when the port is killed, and 507Monitor the given port and do something when the port is killed or
507optionally return a guard that can be used to stop monitoring again. 508messages to it were lost, and optionally return a guard that can be used
509to stop monitoring again.
510
511C<mon> effectively guarantees that, in the absence of hardware failures,
512that after starting the monitor, either all messages sent to the port
513will arrive, or the monitoring action will be invoked after possible
514message loss has been detected. No messages will be lost "in between"
515(after the first lost message no further messages will be received by the
516port). After the monitoring action was invoked, further messages might get
517delivered again.
508 518
509In the first form (callback), the callback is simply called with any 519In the first form (callback), the callback is simply called with any
510number of C<@reason> elements (no @reason means that the port was deleted 520number of C<@reason> elements (no @reason means that the port was deleted
511"normally"). Note also that I<< the callback B<must> never die >>, so use 521"normally"). Note also that I<< the callback B<must> never die >>, so use
512C<eval> if unsure. 522C<eval> if unsure.
513 523
514In the second form (another port given), the other port (C<$rcvport) 524In the second form (another port given), the other port (C<$rcvport>)
515will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on 525will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on
516"normal" kils nothing happens, while under all other conditions, the other 526"normal" kils nothing happens, while under all other conditions, the other
517port is killed with the same reason. 527port is killed with the same reason.
518 528
519The third form (kill self) is the same as the second form, except that 529The third form (kill self) is the same as the second form, except that
546sub mon { 556sub mon {
547 my ($noderef, $port) = split /#/, shift, 2; 557 my ($noderef, $port) = split /#/, shift, 2;
548 558
549 my $node = $NODE{$noderef} || add_node $noderef; 559 my $node = $NODE{$noderef} || add_node $noderef;
550 560
551 my $cb = @_ ? $_[0] : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,'; 561 my $cb = @_ ? shift : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,';
552 562
553 unless (ref $cb) { 563 unless (ref $cb) {
554 if (@_) { 564 if (@_) {
555 # send a kill info message 565 # send a kill info message
556 my (@msg) = @_; 566 my (@msg) = ($cb, @_);
557 $cb = sub { snd @msg, @_ }; 567 $cb = sub { snd @msg, @_ };
558 } else { 568 } else {
559 # simply kill other port 569 # simply kill other port
560 my $port = $cb; 570 my $port = $cb;
561 $cb = sub { kil $port, @_ if @_ }; 571 $cb = sub { kil $port, @_ if @_ };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines