line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Coro::Timer - timers and timeouts, independent of any event loop |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# This package is mostly obsoleted by Coro::AnyEvent. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Coro::Timer qw(timeout); |
10
|
|
|
|
|
|
|
# nothing exported by default |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 DESCRIPTION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
This package has been mostly obsoleted by L, the only |
15
|
|
|
|
|
|
|
really useful function left in here is C. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=over 4 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
package Coro::Timer; |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
509
|
use common::sense; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
4
|
|
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
1
|
|
39
|
use Carp (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
14
|
|
26
|
1
|
|
|
1
|
|
4
|
use base Exporter::; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
54
|
|
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
1
|
|
5
|
use Coro (); |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
12
|
|
29
|
1
|
|
|
1
|
|
3
|
use Coro::AnyEvent (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
167
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $VERSION = 6.513; |
32
|
|
|
|
|
|
|
our @EXPORT_OK = qw(timeout sleep); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
This is used by Coro itself to implement the C, C |
44
|
|
|
|
|
|
|
etc. primitives. It is used like this: |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub timed_wait { |
47
|
|
|
|
|
|
|
my $timeout = Coro::Timer::timeout 60; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
while (condition false) { |
50
|
|
|
|
|
|
|
Coro::schedule; # wait until woken up or timeout |
51
|
|
|
|
|
|
|
return 0 if $timeout; # timed out |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
return 1; # condition satisfied |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub timeout($) { |
60
|
0
|
|
|
0
|
1
|
|
my $current = $Coro::current; |
61
|
0
|
|
|
|
|
|
my $timeout; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
bless [ |
64
|
|
|
|
|
|
|
\$timeout, |
65
|
|
|
|
|
|
|
(AE::timer $_[0], 0, sub { |
66
|
0
|
|
|
0
|
|
|
$timeout = 1; |
67
|
0
|
|
|
|
|
|
$current->ready; |
68
|
0
|
|
|
|
|
|
}), |
69
|
|
|
|
|
|
|
], "Coro::Timer::Timeout"; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
package Coro::Timer::Timeout; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
0
|
|
|
sub bool { ${ $_[0][0] } } |
|
0
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
1
|
|
|
1
|
|
5
|
use overload 'bool' => \&bool, '0+' => \&bool; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=back |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 AUTHOR/SUPPORT/CONTACT |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Marc A. Lehmann |
85
|
|
|
|
|
|
|
http://software.schmorp.de/pkg/Coro.html |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|