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

Comparing Coro/Coro/Timer.pm (file contents):
Revision 1.65 by root, Tue Jun 30 08:28:55 2009 UTC vs.
Revision 1.71 by root, Thu Oct 1 23:16:27 2009 UTC

18 18
19=cut 19=cut
20 20
21package Coro::Timer; 21package Coro::Timer;
22 22
23no warnings; 23use common::sense;
24 24
25use Carp (); 25use Carp ();
26use Exporter; 26use Exporter;
27 27
28use AnyEvent (); 28use AnyEvent ();
29 29
30use Coro (); 30use Coro ();
31use Coro::AnyEvent (); 31use Coro::AnyEvent ();
32 32
33$VERSION = 5.15; 33our $VERSION = 5.17;
34@EXPORT_OK = qw(timeout sleep); 34our @EXPORT_OK = qw(timeout sleep);
35 35
36=item $flag = timeout $seconds; 36=item $flag = timeout $seconds;
37 37
38This function will wake up the current coroutine after $seconds 38This function will wake up the current coroutine after $seconds
39seconds and sets $flag to true (it is false initially). If $flag goes 39seconds and sets $flag to true (it is false initially). If $flag goes
51 return 1; # condition satisfied 51 return 1; # condition satisfied
52 } 52 }
53 53
54=cut 54=cut
55 55
56# deep magic, expecially the double indirection :(:(
57sub timeout($) { 56sub timeout($) {
58 my $current = $Coro::current; 57 my $current = $Coro::current;
59 my $timeout; 58 my $timeout;
60 bless { 59 bless {
61 timer => AnyEvent->timer (after => $_[0], cb => sub { 60 timer => (AE::timer $_[0], 0, sub {
62 $timeout = 1; 61 $timeout = 1;
63 $current->ready; 62 $current->ready;
64 }), 63 }),
65 timeout => \$timeout, 64 timeout => \$timeout,
66 }, "Coro::Timer::Timeout"; 65 }, "Coro::Timer::Timeout";
80and, most important, without blocking other coroutines. 79and, most important, without blocking other coroutines.
81 80
82=cut 81=cut
83 82
84sub sleep { 83sub sleep {
85 my $timer = AnyEvent->timer (after => $_[0], cb => Coro::rouse_cb); 84 my $timer = AE::timer $_[0], 0, Coro::rouse_cb;
86 Coro::rouse_wait; 85 Coro::rouse_wait;
87} 86}
88 87
891; 881;
90 89

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines