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.35 by root, Thu Aug 6 10:21:48 2009 UTC vs.
Revision 1.36 by root, Thu Aug 6 10:46:48 2009 UTC

305 $port 305 $port
306} 306}
307 307
308=item reg $port, $name 308=item reg $port, $name
309 309
310Registers the given port under the name C<$name>. If the name already 310=item reg $name
311exists it is replaced. 311
312Registers the given port (or C<$SELF><<< if missing) under the name
313C<$name>. If the name already exists it is replaced.
312 314
313A port can only be registered under one well known name. 315A port can only be registered under one well known name.
314 316
315A port automatically becomes unregistered when it is killed. 317A port automatically becomes unregistered when it is killed.
316 318
317=cut 319=cut
318 320
319sub reg(@) { 321sub reg(@) {
320 my ($port, $name) = @_; 322 my $port = @_ > 1 ? shift : $SELF || Carp::croak 'reg: called with one argument only, but $SELF not set,';
321 323
322 $REG{$name} = $port; 324 $REG{$_[0]} = $port;
323} 325}
324 326
325=item rcv $port, $callback->(@msg) 327=item rcv $port, $callback->(@msg)
326 328
327Replaces the callback on the specified miniport (after converting it to 329Replaces the callback on the specified miniport (after converting it to
332=item rcv $port, $smartmatch => $callback->(@msg), ... 334=item rcv $port, $smartmatch => $callback->(@msg), ...
333 335
334=item rcv $port, [$smartmatch...] => $callback->(@msg), ... 336=item rcv $port, [$smartmatch...] => $callback->(@msg), ...
335 337
336Register callbacks to be called on matching messages on the given full 338Register callbacks to be called on matching messages on the given full
337port (after converting it to one if required). 339port (after converting it to one if required) and return the port.
338 340
339The callback has to return a true value when its work is done, after 341The callback has to return a true value when its work is done, after
340which is will be removed, or a false value in which case it will stay 342which is will be removed, or a false value in which case it will stay
341registered. 343registered.
342 344
354exported by this module) matches any single element of the message. 356exported by this module) matches any single element of the message.
355 357
356While not required, it is highly recommended that the first matching 358While not required, it is highly recommended that the first matching
357element is a string identifying the message. The one-string-only match is 359element is a string identifying the message. The one-string-only match is
358also the most efficient match (by far). 360also the most efficient match (by far).
361
362Example: create a port and bind receivers on it in one go.
363
364 my $port = rcv port,
365 msg1 => sub { ...; 0 },
366 msg2 => sub { ...; 0 },
367 ;
368
369Example: create a port, bind receivers and send it in a message elsewhere
370in one go:
371
372 snd $otherport, reply =>
373 rcv port,
374 msg1 => sub { ...; 0 },
375 ...
376 ;
359 377
360=cut 378=cut
361 379
362sub rcv($@) { 380sub rcv($@) {
363 my $port = shift; 381 my $port = shift;
470 } 488 }
471} 489}
472 490
473=item $guard = mon $port, $cb->(@reason) 491=item $guard = mon $port, $cb->(@reason)
474 492
475=item $guard = mon $port, $otherport 493=item $guard = mon $port, $rcvport
476 494
495=item $guard = mon $port
496
477=item $guard = mon $port, $otherport, @msg 497=item $guard = mon $port, $rcvport, @msg
478 498
479Monitor the given port and do something when the port is killed. 499Monitor the given port and do something when the port is killed, and
500optionally return a guard that can be used to stop monitoring again.
480 501
481In the first form, the callback is simply called with any number 502In the first form (callback), the callback is simply called with any
482of C<@reason> elements (no @reason means that the port was deleted 503number of C<@reason> elements (no @reason means that the port was deleted
483"normally"). Note also that I<< the callback B<must> never die >>, so use 504"normally"). Note also that I<< the callback B<must> never die >>, so use
484C<eval> if unsure. 505C<eval> if unsure.
485 506
486In the second form, the other port will be C<kil>'ed with C<@reason>, iff 507In the second form (another port given), the other port (C<$rcvport)
487a @reason was specified, i.e. on "normal" kils nothing happens, while 508will be C<kil>'ed with C<@reason>, iff a @reason was specified, i.e. on
488under all other conditions, the other port is killed with the same reason. 509"normal" kils nothing happens, while under all other conditions, the other
510port is killed with the same reason.
489 511
512The third form (kill self) is the same as the second form, except that
513C<$rvport> defaults to C<$SELF>.
514
490In the last form, a message of the form C<@msg, @reason> will be C<snd>. 515In the last form (message), a message of the form C<@msg, @reason> will be
516C<snd>.
491 517
492Example: call a given callback when C<$port> is killed. 518Example: call a given callback when C<$port> is killed.
493 519
494 mon $port, sub { warn "port died because of <@_>\n" }; 520 mon $port, sub { warn "port died because of <@_>\n" };
495 521
496Example: kill ourselves when C<$port> is killed abnormally. 522Example: kill ourselves when C<$port> is killed abnormally.
497 523
498 mon $port, $self; 524 mon $port;
499 525
500Example: send us a restart message another C<$port> is killed. 526Example: send us a restart message when another C<$port> is killed.
501 527
502 mon $port, $self => "restart"; 528 mon $port, $self => "restart";
503 529
504=cut 530=cut
505 531
506sub mon { 532sub mon {
507 my ($noderef, $port) = split /#/, shift, 2; 533 my ($noderef, $port) = split /#/, shift, 2;
508 534
509 my $node = $NODE{$noderef} || add_node $noderef; 535 my $node = $NODE{$noderef} || add_node $noderef;
510 536
511 my $cb = shift; 537 my $cb = @_ ? $_[0] : $SELF || Carp::croak 'mon: called with one argument only, but $SELF not set,';
512 538
513 unless (ref $cb) { 539 unless (ref $cb) {
514 if (@_) { 540 if (@_) {
515 # send a kill info message 541 # send a kill info message
516 my (@msg) = ($cb, @_); 542 my (@msg) = @_;
517 $cb = sub { snd @msg, @_ }; 543 $cb = sub { snd @msg, @_ };
518 } else { 544 } else {
519 # simply kill other port 545 # simply kill other port
520 my $port = $cb; 546 my $port = $cb;
521 $cb = sub { kil $port, @_ if @_ }; 547 $cb = sub { kil $port, @_ if @_ };
547=cut 573=cut
548 574
549sub mon_guard { 575sub mon_guard {
550 my ($port, @refs) = @_; 576 my ($port, @refs) = @_;
551 577
578 #TODO: mon-less form?
579
552 mon $port, sub { 0 && @refs } 580 mon $port, sub { 0 && @refs }
553} 581}
554 582
555=item lnk $port1, $port2 583=item lnk $port1, $port2
556 584
585=item lnk $otherport
586
557Link two ports. This is simply a shorthand for: 587Link two ports. This is simply a shorthand for:
558 588
559 mon $port1, $port2; 589 mon $port1, $port2;
560 mon $port2, $port1; 590 mon $port2, $port1;
561 591
562It means that if either one is killed abnormally, the other one gets 592It means that if either one is killed abnormally, the other one gets
563killed as well. 593killed as well.
594
595The one-argument form assumes that one port is C<$SELF>.
596
597=cut
598
599sub lnk {
600 my $port1 = shift;
601 my $port2 = @_ ? shift : $SELF || Carp::croak 'lnk: called with one argument only, but $SELF not set,';
602
603 mon $port1, $port2;
604 mon $port2, $port1;
605}
564 606
565=item kil $port[, @reason] 607=item kil $port[, @reason]
566 608
567Kill the specified port with the given C<@reason>. 609Kill the specified port with the given C<@reason>.
568 610

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines