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 |
… | |
… | |
10 | sleep 10; |
10 | sleep 10; |
11 | |
11 | |
12 | =head1 DESCRIPTION |
12 | =head1 DESCRIPTION |
13 | |
13 | |
14 | This package implements a simple timer callback system which works |
14 | This package implements a simple timer callback system which works |
15 | independent of the event loop mechanism used. If no event mechanism is |
15 | independent of the event loop mechanism used. |
16 | used, it is emulated. The C<Coro::Event> module overwrites functions with |
|
|
17 | versions better suited. |
|
|
18 | |
|
|
19 | This module is not subclassable. |
|
|
20 | |
16 | |
21 | =over 4 |
17 | =over 4 |
22 | |
18 | |
23 | =cut |
19 | =cut |
24 | |
20 | |
… | |
… | |
27 | no warnings; |
23 | no warnings; |
28 | |
24 | |
29 | use Carp (); |
25 | use Carp (); |
30 | use Exporter; |
26 | use Exporter; |
31 | |
27 | |
32 | use Coro (); |
|
|
33 | use AnyEvent (); |
28 | use AnyEvent (); |
34 | |
29 | |
|
|
30 | use Coro (); |
|
|
31 | use Coro::AnyEvent (); |
|
|
32 | |
35 | $VERSION = "2.0"; |
33 | $VERSION = 5.15; |
36 | @EXPORT_OK = qw(timeout sleep); |
34 | @EXPORT_OK = qw(timeout sleep); |
37 | |
35 | |
38 | =item $flag = timeout $seconds; |
36 | =item $flag = timeout $seconds; |
39 | |
37 | |
40 | This function will wake up the current coroutine after $seconds |
38 | This function will wake up the current coroutine after $seconds |
… | |
… | |
44 | |
42 | |
45 | sub timed_wait { |
43 | sub timed_wait { |
46 | my $timeout = Coro::Timer::timeout 60; |
44 | my $timeout = Coro::Timer::timeout 60; |
47 | |
45 | |
48 | while (condition false) { |
46 | while (condition false) { |
49 | schedule; # wait until woken up or timeout |
47 | Coro::schedule; # wait until woken up or timeout |
50 | return 0 if $timeout; # timed out |
48 | return 0 if $timeout; # timed out |
51 | } |
49 | } |
|
|
50 | |
52 | return 1; # condition satisfied |
51 | return 1; # condition satisfied |
53 | } |
52 | } |
54 | |
53 | |
55 | =cut |
54 | =cut |
56 | |
55 | |
… | |
… | |
81 | and, most important, without blocking other coroutines. |
80 | and, most important, without blocking other coroutines. |
82 | |
81 | |
83 | =cut |
82 | =cut |
84 | |
83 | |
85 | sub sleep { |
84 | sub sleep { |
86 | my $current = $Coro::current; |
|
|
87 | my $timer = AnyEvent->timer (after => $_[0], cb => sub { $current->ready }); |
85 | my $timer = AnyEvent->timer (after => $_[0], cb => Coro::rouse_cb); |
88 | Coro::schedule; |
86 | Coro::rouse_wait; |
89 | } |
87 | } |
90 | |
|
|
91 | $Coro::idle = sub { |
|
|
92 | AnyEvent->one_event; |
|
|
93 | }; |
|
|
94 | |
88 | |
95 | 1; |
89 | 1; |
96 | |
90 | |
97 | =back |
91 | =back |
98 | |
92 | |