1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Coro::Timer - simple timer package, independent of used event loops |
3 | Coro::Timer - timers and timeouts, independent of any event loop |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use Coro::Timer; |
7 | use Coro::Timer qw(sleep timeout); |
|
|
8 | # nothing exported by default |
|
|
9 | |
|
|
10 | sleep 10; |
8 | |
11 | |
9 | =head1 DESCRIPTION |
12 | =head1 DESCRIPTION |
10 | |
13 | |
11 | This package implements a simple timer callback system which works |
14 | This package implements a simple timer callback system which works |
12 | independent of the event loop mechanism used. If no event mechanism is |
15 | independent of the event loop mechanism used. |
13 | used, it is emulated. The C<Coro::Event> module overwrites functions with |
|
|
14 | versions better suited. |
|
|
15 | |
|
|
16 | This module is not subclassable. |
|
|
17 | |
16 | |
18 | =over 4 |
17 | =over 4 |
19 | |
18 | |
20 | =cut |
19 | =cut |
21 | |
20 | |
22 | package Coro::Timer; |
21 | package Coro::Timer; |
23 | |
22 | |
24 | no warnings qw(uninitialized); |
23 | no warnings; |
25 | |
24 | |
26 | use Carp (); |
25 | use Carp (); |
27 | use Exporter; |
26 | use Exporter; |
28 | |
27 | |
|
|
28 | use AnyEvent (); |
|
|
29 | |
29 | use Coro (); |
30 | use Coro (); |
|
|
31 | use Coro::AnyEvent (); |
30 | |
32 | |
31 | BEGIN { |
|
|
32 | eval "use Time::HiRes 'time'"; |
|
|
33 | } |
|
|
34 | |
|
|
35 | $VERSION = 0.531; |
33 | $VERSION = "5.0"; |
36 | @EXPORT_OK = qw(timeout sleep); |
34 | @EXPORT_OK = qw(timeout sleep); |
37 | |
35 | |
38 | =item $flag = timeout $seconds; |
36 | =item $flag = timeout $seconds; |
39 | |
37 | |
40 | This function will wake up the current coroutine after $seconds |
38 | This function will wake up the current coroutine after $seconds |
41 | seconds and sets $flag to true (it is false intiially). If $flag goes |
39 | seconds and sets $flag to true (it is false initially). If $flag goes |
42 | out of scope earlier nothing happens. This is used to implement the |
40 | out of scope earlier nothing happens. This is used to implement the |
43 | C<timed_down>, C<timed_wait> etc. primitives. It is used like this: |
41 | C<timed_down>, C<timed_wait> etc. primitives. It is used like this: |
44 | |
42 | |
45 | sub timed_wait { |
43 | sub timed_wait { |
46 | my $timeout = Coro::Timer::timeout 60; |
44 | my $timeout = Coro::Timer::timeout 60; |
47 | |
45 | |
48 | while (condition false) { |
46 | while (condition false) { |
49 | schedule; # wait until woken up or timeout |
47 | Coro::schedule; # wait until woken up or timeout |
50 | return 0 if $timeout; # timed out |
48 | return 0 if $timeout; # timed out |
51 | } |
49 | } |
|
|
50 | |
52 | return 1; # condition satisfied |
51 | return 1; # condition satisfied |
53 | } |
52 | } |
54 | |
53 | |
55 | =cut |
54 | =cut |
56 | |
55 | |
57 | # deep magic, expecially the double indirection :(:( |
56 | # deep magic, expecially the double indirection :(:( |
58 | sub timeout($) { |
57 | sub timeout($) { |
59 | my $self = \\my $timer; |
|
|
60 | my $current = $Coro::current; |
58 | my $current = $Coro::current; |
61 | $timer = _new_timer(time + $_[0], sub { |
59 | my $timeout; |
62 | undef $timer; # set flag |
60 | bless { |
|
|
61 | timer => AnyEvent->timer (after => $_[0], cb => sub { |
|
|
62 | $timeout = 1; |
63 | $current->ready; |
63 | $current->ready; |
64 | }); |
64 | }), |
65 | bless $self, Coro::timeout::; |
65 | timeout => \$timeout, |
|
|
66 | }, "Coro::Timer::Timeout"; |
66 | } |
67 | } |
67 | |
68 | |
68 | package Coro::timeout; |
69 | package Coro::Timer::Timeout; |
69 | |
70 | |
70 | sub bool { !${${$_[0]}} } |
71 | sub bool { ${$_[0]{timeout}} } |
71 | sub DESTROY { ${${$_[0]}}->cancel } |
|
|
72 | |
72 | |
73 | use overload 'bool' => \&bool, '0+' => \&bool; |
73 | use overload 'bool' => \&bool, '0+' => \&bool; |
74 | |
74 | |
75 | package Coro::Timer; |
75 | package Coro::Timer; |
76 | |
76 | |
… | |
… | |
80 | and, most important, without blocking other coroutines. |
80 | and, most important, without blocking other coroutines. |
81 | |
81 | |
82 | =cut |
82 | =cut |
83 | |
83 | |
84 | sub sleep { |
84 | sub sleep { |
85 | my $current = $Coro::current; |
85 | my $timer = AnyEvent->timer (after => $_[0], cb => Coro::rouse_cb); |
86 | _new_timer(time + $_[0], sub { $current->ready }); |
86 | Coro::rouse_wait; |
87 | Coro::schedule; |
|
|
88 | } |
87 | } |
89 | |
|
|
90 | =item $timer = new Coro::Timer at/after => xxx, cb => \&yyy; |
|
|
91 | |
|
|
92 | Create a new timer. |
|
|
93 | |
|
|
94 | =cut |
|
|
95 | |
|
|
96 | sub new { |
|
|
97 | my $class = shift; |
|
|
98 | my %arg = @_; |
|
|
99 | |
|
|
100 | $arg{at} = time + delete $arg{after} if exists $arg{after}; |
|
|
101 | |
|
|
102 | _new_timer($arg{at}, $arg{cb}); |
|
|
103 | } |
|
|
104 | |
|
|
105 | my $timer; |
|
|
106 | my @timer; |
|
|
107 | |
|
|
108 | unless ($override) { |
|
|
109 | $override = 1; |
|
|
110 | *_new_timer = sub { |
|
|
111 | my $self = bless [$_[0], $_[1]], Coro::Timer::simple; |
|
|
112 | |
|
|
113 | # my version of rapid prototyping. guys, use a real event module! |
|
|
114 | @timer = sort { $a->[0] cmp $b->[0] } @timer, $self; |
|
|
115 | |
|
|
116 | unless ($timer) { |
|
|
117 | $timer = new Coro sub { |
|
|
118 | my $NOW = time; |
|
|
119 | while (@timer) { |
|
|
120 | Coro::cede; |
|
|
121 | if ($NOW >= $timer[0][0]) { |
|
|
122 | my $next = shift @timer; |
|
|
123 | $next->[1] and $next->[1]->(); |
|
|
124 | } else { |
|
|
125 | select undef, undef, undef, $timer[0][0] - $NOW; |
|
|
126 | $NOW = time; |
|
|
127 | } |
|
|
128 | }; |
|
|
129 | undef $timer; |
|
|
130 | }; |
|
|
131 | $timer->prio(Coro::PRIO_MIN); |
|
|
132 | $timer->ready; |
|
|
133 | } |
|
|
134 | |
|
|
135 | $self; |
|
|
136 | }; |
|
|
137 | |
|
|
138 | *Coro::Timer::simple::cancel = sub { |
|
|
139 | @{$_[0]} = (); |
|
|
140 | }; |
|
|
141 | } |
|
|
142 | |
|
|
143 | =item $timer->cancel |
|
|
144 | |
|
|
145 | Cancel the timer (the callback will no longer be called). |
|
|
146 | |
|
|
147 | =cut |
|
|
148 | |
88 | |
149 | 1; |
89 | 1; |
150 | |
90 | |
151 | =back |
91 | =back |
152 | |
92 | |
153 | =head1 AUTHOR |
93 | =head1 AUTHOR |
154 | |
94 | |
155 | Marc Lehmann <pcg@goof.com> |
95 | Marc Lehmann <schmorp@schmorp.de> |
156 | http://www.goof.com/pcg/marc/ |
96 | http://home.schmorp.de/ |
157 | |
97 | |
158 | =cut |
98 | =cut |
159 | |
99 | |