… | |
… | |
18 | |
18 | |
19 | =cut |
19 | =cut |
20 | |
20 | |
21 | package Coro::Timer; |
21 | package Coro::Timer; |
22 | |
22 | |
23 | no warnings; |
23 | use common::sense; |
24 | |
24 | |
25 | use Carp (); |
25 | use Carp (); |
26 | use Exporter; |
26 | use Exporter; |
27 | |
27 | |
28 | use AnyEvent (); |
28 | use AnyEvent (); |
29 | |
29 | |
30 | use Coro (); |
30 | use Coro (); |
31 | use Coro::AnyEvent (); |
31 | use Coro::AnyEvent (); |
32 | |
32 | |
33 | $VERSION = 5.15; |
33 | our $VERSION = 5.17; |
34 | @EXPORT_OK = qw(timeout sleep); |
34 | our @EXPORT_OK = qw(timeout sleep); |
35 | |
35 | |
36 | =item $flag = timeout $seconds; |
36 | =item $flag = timeout $seconds; |
37 | |
37 | |
38 | This function will wake up the current coroutine after $seconds |
38 | This function will wake up the current coroutine after $seconds |
39 | seconds and sets $flag to true (it is false initially). If $flag goes |
39 | seconds 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 :(:( |
|
|
57 | sub timeout($) { |
56 | sub 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"; |
… | |
… | |
80 | and, most important, without blocking other coroutines. |
79 | and, most important, without blocking other coroutines. |
81 | |
80 | |
82 | =cut |
81 | =cut |
83 | |
82 | |
84 | sub sleep { |
83 | sub 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 | |
89 | 1; |
88 | 1; |
90 | |
89 | |