1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Coro::Timer - timers and timeouts, independent of any event loop |
3 | Coro::Timer - timers and timeouts, independent of any event loop |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
|
|
6 | |
|
|
7 | # This package is mostly obsoleted by Coro::AnyEvent. |
6 | |
8 | |
7 | use Coro::Timer qw(sleep timeout); |
9 | use Coro::Timer qw(sleep timeout); |
8 | # nothing exported by default |
10 | # nothing exported by default |
9 | |
11 | |
10 | sleep 10; |
12 | sleep 10; |
11 | |
13 | |
12 | =head1 DESCRIPTION |
14 | =head1 DESCRIPTION |
13 | |
15 | |
14 | This package implements a simple timer callback system which works |
16 | This package has been mostly obsoleted by L<Coro::AnyEvent>, the only |
15 | independent of the event loop mechanism used. |
17 | really useful function left in here is C<timeout>. |
16 | |
18 | |
17 | =over 4 |
19 | =over 4 |
18 | |
20 | |
19 | =cut |
21 | =cut |
20 | |
22 | |
… | |
… | |
23 | use common::sense; |
25 | use common::sense; |
24 | |
26 | |
25 | use Carp (); |
27 | use Carp (); |
26 | use base Exporter::; |
28 | use base Exporter::; |
27 | |
29 | |
28 | use AnyEvent (); |
|
|
29 | |
|
|
30 | use Coro (); |
30 | use Coro (); |
31 | use Coro::AnyEvent (); |
31 | use Coro::AnyEvent (); |
32 | |
32 | |
33 | our $VERSION = 5.25; |
33 | our $VERSION = 5.25; |
34 | our @EXPORT_OK = qw(timeout sleep); |
34 | our @EXPORT_OK = qw(timeout sleep); |
35 | |
35 | |
36 | =item $flag = timeout $seconds; |
36 | # compatibility with older programs |
|
|
37 | *sleep = \&Coro::AnyEvent::sleep; |
37 | |
38 | |
|
|
39 | =item $flag = timeout $seconds |
|
|
40 | |
38 | This function will wake up the current coroutine after $seconds |
41 | This function will wake up the current coroutine after $seconds seconds |
39 | seconds and sets $flag to true (it is false initially). If $flag goes |
42 | and sets $flag to true (it is false initially). If $flag goes out |
40 | out of scope earlier nothing happens. This is used to implement the |
43 | of scope earlier then nothing happens. |
41 | C<timed_down>, C<timed_wait> etc. primitives. It is used like this: |
44 | |
|
|
45 | This is used by Coro itself to implement the C<timed_down>, C<timed_wait> |
|
|
46 | etc. primitives. It is used like this: |
42 | |
47 | |
43 | sub timed_wait { |
48 | sub timed_wait { |
44 | my $timeout = Coro::Timer::timeout 60; |
49 | my $timeout = Coro::Timer::timeout 60; |
45 | |
50 | |
46 | while (condition false) { |
51 | while (condition false) { |
… | |
… | |
54 | =cut |
59 | =cut |
55 | |
60 | |
56 | sub timeout($) { |
61 | sub timeout($) { |
57 | my $current = $Coro::current; |
62 | my $current = $Coro::current; |
58 | my $timeout; |
63 | my $timeout; |
|
|
64 | |
59 | bless { |
65 | bless [ |
|
|
66 | \$timeout, |
60 | timer => (AE::timer $_[0], 0, sub { |
67 | (AE::timer $_[0], 0, sub { |
61 | $timeout = 1; |
68 | $timeout = 1; |
62 | $current->ready; |
69 | $current->ready; |
63 | }), |
70 | }), |
64 | timeout => \$timeout, |
|
|
65 | }, "Coro::Timer::Timeout"; |
71 | ], "Coro::Timer::Timeout"; |
66 | } |
72 | } |
67 | |
73 | |
68 | package Coro::Timer::Timeout; |
74 | package Coro::Timer::Timeout; |
69 | |
75 | |
70 | sub bool { ${$_[0]{timeout}} } |
76 | sub bool { ${ $_[0][0] } } |
71 | |
77 | |
72 | use overload 'bool' => \&bool, '0+' => \&bool; |
78 | use overload 'bool' => \&bool, '0+' => \&bool; |
73 | |
|
|
74 | package Coro::Timer; |
|
|
75 | |
|
|
76 | =item sleep $seconds |
|
|
77 | |
|
|
78 | This function works like the built-in sleep, except maybe more precise |
|
|
79 | and, most important, without blocking other coroutines. |
|
|
80 | |
|
|
81 | =cut |
|
|
82 | |
|
|
83 | sub sleep { |
|
|
84 | my $timer = AE::timer $_[0], 0, Coro::rouse_cb; |
|
|
85 | Coro::rouse_wait; |
|
|
86 | } |
|
|
87 | |
79 | |
88 | 1; |
80 | 1; |
89 | |
81 | |
90 | =back |
82 | =back |
91 | |
83 | |