1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
root |
1.58 |
Coro::Timer - timers and timeouts, independent of any event loop |
4 |
root |
1.1 |
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
root |
1.79 |
# This package is mostly obsoleted by Coro::AnyEvent. |
8 |
|
|
|
9 |
root |
1.91 |
use Coro::Timer qw(timeout); |
10 |
root |
1.8 |
# nothing exported by default |
11 |
|
|
|
12 |
root |
1.1 |
=head1 DESCRIPTION |
13 |
|
|
|
14 |
root |
1.79 |
This package has been mostly obsoleted by L<Coro::AnyEvent>, the only |
15 |
|
|
really useful function left in here is C<timeout>. |
16 |
root |
1.4 |
|
17 |
root |
1.1 |
=over 4 |
18 |
|
|
|
19 |
|
|
=cut |
20 |
|
|
|
21 |
|
|
package Coro::Timer; |
22 |
|
|
|
23 |
root |
1.71 |
use common::sense; |
24 |
root |
1.1 |
|
25 |
|
|
use Carp (); |
26 |
root |
1.74 |
use base Exporter::; |
27 |
root |
1.1 |
|
28 |
|
|
use Coro (); |
29 |
root |
1.36 |
use Coro::AnyEvent (); |
30 |
root |
1.1 |
|
31 |
root |
1.96 |
our $VERSION = 6.23; |
32 |
root |
1.71 |
our @EXPORT_OK = qw(timeout sleep); |
33 |
root |
1.1 |
|
34 |
root |
1.79 |
# compatibility with older programs |
35 |
|
|
*sleep = \&Coro::AnyEvent::sleep; |
36 |
|
|
|
37 |
|
|
=item $flag = timeout $seconds |
38 |
|
|
|
39 |
|
|
This function will wake up the current coroutine after $seconds seconds |
40 |
|
|
and sets $flag to true (it is false initially). If $flag goes out |
41 |
|
|
of scope earlier then nothing happens. |
42 |
root |
1.3 |
|
43 |
root |
1.79 |
This is used by Coro itself to implement the C<timed_down>, C<timed_wait> |
44 |
|
|
etc. primitives. It is used like this: |
45 |
root |
1.4 |
|
46 |
|
|
sub timed_wait { |
47 |
|
|
my $timeout = Coro::Timer::timeout 60; |
48 |
|
|
|
49 |
|
|
while (condition false) { |
50 |
root |
1.34 |
Coro::schedule; # wait until woken up or timeout |
51 |
root |
1.4 |
return 0 if $timeout; # timed out |
52 |
|
|
} |
53 |
root |
1.34 |
|
54 |
root |
1.4 |
return 1; # condition satisfied |
55 |
|
|
} |
56 |
root |
1.3 |
|
57 |
|
|
=cut |
58 |
|
|
|
59 |
|
|
sub timeout($) { |
60 |
|
|
my $current = $Coro::current; |
61 |
root |
1.33 |
my $timeout; |
62 |
root |
1.79 |
|
63 |
|
|
bless [ |
64 |
|
|
\$timeout, |
65 |
|
|
(AE::timer $_[0], 0, sub { |
66 |
|
|
$timeout = 1; |
67 |
|
|
$current->ready; |
68 |
|
|
}), |
69 |
|
|
], "Coro::Timer::Timeout"; |
70 |
root |
1.3 |
} |
71 |
|
|
|
72 |
root |
1.33 |
package Coro::Timer::Timeout; |
73 |
root |
1.9 |
|
74 |
root |
1.79 |
sub bool { ${ $_[0][0] } } |
75 |
root |
1.3 |
|
76 |
|
|
use overload 'bool' => \&bool, '0+' => \&bool; |
77 |
|
|
|
78 |
root |
1.1 |
1; |
79 |
|
|
|
80 |
|
|
=back |
81 |
|
|
|
82 |
|
|
=head1 AUTHOR |
83 |
|
|
|
84 |
root |
1.23 |
Marc Lehmann <schmorp@schmorp.de> |
85 |
root |
1.21 |
http://home.schmorp.de/ |
86 |
root |
1.1 |
|
87 |
|
|
=cut |
88 |
|
|
|