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