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.8 |
use Coro::Timer qw(sleep timeout); |
8 |
|
|
# nothing exported by default |
9 |
|
|
|
10 |
|
|
sleep 10; |
11 |
root |
1.1 |
|
12 |
|
|
=head1 DESCRIPTION |
13 |
|
|
|
14 |
|
|
This package implements a simple timer callback system which works |
15 |
root |
1.36 |
independent of the event loop mechanism used. |
16 |
root |
1.4 |
|
17 |
root |
1.1 |
=over 4 |
18 |
|
|
|
19 |
|
|
=cut |
20 |
|
|
|
21 |
|
|
package Coro::Timer; |
22 |
|
|
|
23 |
root |
1.33 |
no warnings; |
24 |
root |
1.1 |
|
25 |
|
|
use Carp (); |
26 |
root |
1.4 |
use Exporter; |
27 |
root |
1.1 |
|
28 |
root |
1.36 |
use AnyEvent (); |
29 |
|
|
|
30 |
root |
1.1 |
use Coro (); |
31 |
root |
1.36 |
use Coro::AnyEvent (); |
32 |
root |
1.1 |
|
33 |
root |
1.68 |
$VERSION = 5.161; |
34 |
root |
1.4 |
@EXPORT_OK = qw(timeout sleep); |
35 |
root |
1.1 |
|
36 |
root |
1.4 |
=item $flag = timeout $seconds; |
37 |
root |
1.3 |
|
38 |
|
|
This function will wake up the current coroutine after $seconds |
39 |
pcg |
1.14 |
seconds and sets $flag to true (it is false initially). If $flag goes |
40 |
root |
1.3 |
out of scope earlier nothing happens. This is used to implement the |
41 |
root |
1.4 |
C<timed_down>, C<timed_wait> etc. primitives. It is used like this: |
42 |
|
|
|
43 |
|
|
sub timed_wait { |
44 |
|
|
my $timeout = Coro::Timer::timeout 60; |
45 |
|
|
|
46 |
|
|
while (condition false) { |
47 |
root |
1.34 |
Coro::schedule; # wait until woken up or timeout |
48 |
root |
1.4 |
return 0 if $timeout; # timed out |
49 |
|
|
} |
50 |
root |
1.34 |
|
51 |
root |
1.4 |
return 1; # condition satisfied |
52 |
|
|
} |
53 |
root |
1.3 |
|
54 |
|
|
=cut |
55 |
|
|
|
56 |
|
|
# deep magic, expecially the double indirection :(:( |
57 |
|
|
sub timeout($) { |
58 |
|
|
my $current = $Coro::current; |
59 |
root |
1.33 |
my $timeout; |
60 |
|
|
bless { |
61 |
|
|
timer => AnyEvent->timer (after => $_[0], cb => sub { |
62 |
|
|
$timeout = 1; |
63 |
|
|
$current->ready; |
64 |
|
|
}), |
65 |
|
|
timeout => \$timeout, |
66 |
|
|
}, "Coro::Timer::Timeout"; |
67 |
root |
1.3 |
} |
68 |
|
|
|
69 |
root |
1.33 |
package Coro::Timer::Timeout; |
70 |
root |
1.9 |
|
71 |
root |
1.33 |
sub bool { ${$_[0]{timeout}} } |
72 |
root |
1.3 |
|
73 |
|
|
use overload 'bool' => \&bool, '0+' => \&bool; |
74 |
|
|
|
75 |
|
|
package Coro::Timer; |
76 |
|
|
|
77 |
root |
1.4 |
=item sleep $seconds |
78 |
|
|
|
79 |
|
|
This function works like the built-in sleep, except maybe more precise |
80 |
|
|
and, most important, without blocking other coroutines. |
81 |
|
|
|
82 |
|
|
=cut |
83 |
|
|
|
84 |
|
|
sub sleep { |
85 |
root |
1.57 |
my $timer = AnyEvent->timer (after => $_[0], cb => Coro::rouse_cb); |
86 |
|
|
Coro::rouse_wait; |
87 |
root |
1.1 |
} |
88 |
|
|
|
89 |
|
|
1; |
90 |
|
|
|
91 |
|
|
=back |
92 |
|
|
|
93 |
|
|
=head1 AUTHOR |
94 |
|
|
|
95 |
root |
1.23 |
Marc Lehmann <schmorp@schmorp.de> |
96 |
root |
1.21 |
http://home.schmorp.de/ |
97 |
root |
1.1 |
|
98 |
|
|
=cut |
99 |
|
|
|