1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Coro::Timer - simple timer package, independent of used event loops |
3 | Coro::Timer - timers and timeouts, independent of any event loop |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use Coro::Timer qw(sleep timeout); |
7 | use Coro::Timer qw(sleep timeout); |
8 | # nothing exported by default |
8 | # nothing exported by default |
… | |
… | |
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 = 4.745; |
33 | $VERSION = 5.15; |
34 | @EXPORT_OK = qw(timeout sleep); |
34 | @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 |
… | |
… | |
80 | and, most important, without blocking other coroutines. |
80 | and, most important, without blocking other coroutines. |
81 | |
81 | |
82 | =cut |
82 | =cut |
83 | |
83 | |
84 | sub sleep { |
84 | sub sleep { |
85 | my $current = $Coro::current; |
|
|
86 | |
|
|
87 | my $timer = AnyEvent->timer (after => $_[0], cb => sub { |
85 | my $timer = AnyEvent->timer (after => $_[0], cb => Coro::rouse_cb); |
88 | $current->ready; |
86 | Coro::rouse_wait; |
89 | undef $current; |
|
|
90 | }); |
|
|
91 | |
|
|
92 | do { &Coro::schedule } while $current; |
|
|
93 | } |
87 | } |
94 | |
88 | |
95 | 1; |
89 | 1; |
96 | |
90 | |
97 | =back |
91 | =back |