… | |
… | |
2 | |
2 | |
3 | Coro::Timer - simple timer package, independent of used event loops |
3 | Coro::Timer - simple timer package, independent of used event loops |
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. If no event mechanism is |
… | |
… | |
19 | |
22 | |
20 | =cut |
23 | =cut |
21 | |
24 | |
22 | package Coro::Timer; |
25 | package Coro::Timer; |
23 | |
26 | |
24 | no warnings qw(uninitialized); |
27 | no warnings; |
25 | |
28 | |
26 | use Carp (); |
29 | use Carp (); |
27 | use Exporter; |
30 | use Exporter; |
28 | |
31 | |
29 | use Coro (); |
32 | use Coro (); |
|
|
33 | use AnyEvent (); |
30 | |
34 | |
31 | BEGIN { |
|
|
32 | eval "use Time::HiRes 'time'"; |
|
|
33 | } |
|
|
34 | |
|
|
35 | $VERSION = 0.531; |
35 | $VERSION = "2.0"; |
36 | @EXPORT_OK = qw(timeout sleep); |
36 | @EXPORT_OK = qw(timeout sleep); |
37 | |
37 | |
38 | =item $flag = timeout $seconds; |
38 | =item $flag = timeout $seconds; |
39 | |
39 | |
40 | This function will wake up the current coroutine after $seconds |
40 | This function will wake up the current coroutine after $seconds |
41 | seconds and sets $flag to true (it is false intiially). If $flag goes |
41 | 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 |
42 | 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: |
43 | C<timed_down>, C<timed_wait> etc. primitives. It is used like this: |
44 | |
44 | |
45 | sub timed_wait { |
45 | sub timed_wait { |
46 | my $timeout = Coro::Timer::timeout 60; |
46 | my $timeout = Coro::Timer::timeout 60; |
… | |
… | |
54 | |
54 | |
55 | =cut |
55 | =cut |
56 | |
56 | |
57 | # deep magic, expecially the double indirection :(:( |
57 | # deep magic, expecially the double indirection :(:( |
58 | sub timeout($) { |
58 | sub timeout($) { |
59 | my $self = \\my $timer; |
|
|
60 | my $current = $Coro::current; |
59 | my $current = $Coro::current; |
61 | $timer = _new_timer(time + $_[0], sub { |
60 | my $timeout; |
62 | undef $timer; # set flag |
61 | bless { |
|
|
62 | timer => AnyEvent->timer (after => $_[0], cb => sub { |
|
|
63 | $timeout = 1; |
63 | $current->ready; |
64 | $current->ready; |
64 | }); |
65 | }), |
65 | bless $self, Coro::timeout::; |
66 | timeout => \$timeout, |
|
|
67 | }, "Coro::Timer::Timeout"; |
66 | } |
68 | } |
67 | |
69 | |
68 | package Coro::timeout; |
70 | package Coro::Timer::Timeout; |
69 | |
71 | |
70 | sub bool { !${${$_[0]}} } |
72 | sub bool { ${$_[0]{timeout}} } |
71 | sub DESTROY { ${${$_[0]}}->cancel } |
|
|
72 | |
73 | |
73 | use overload 'bool' => \&bool, '0+' => \&bool; |
74 | use overload 'bool' => \&bool, '0+' => \&bool; |
74 | |
75 | |
75 | package Coro::Timer; |
76 | package Coro::Timer; |
76 | |
77 | |
… | |
… | |
81 | |
82 | |
82 | =cut |
83 | =cut |
83 | |
84 | |
84 | sub sleep { |
85 | sub sleep { |
85 | my $current = $Coro::current; |
86 | my $current = $Coro::current; |
86 | _new_timer(time + $_[0], sub { $current->ready }); |
87 | my $timer = AnyEvent->timer (after => $_[0], cb => sub { $current->ready }); |
87 | Coro::schedule; |
88 | Coro::schedule; |
88 | } |
89 | } |
89 | |
90 | |
90 | =item $timer = new Coro::Timer at/after => xxx, cb => \&yyy; |
91 | $Coro::idle = sub { |
91 | |
92 | AnyEvent->one_event; |
92 | Create a new timer. |
93 | }; |
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 | |
94 | |
149 | 1; |
95 | 1; |
150 | |
96 | |
151 | =back |
97 | =back |
152 | |
98 | |
153 | =head1 AUTHOR |
99 | =head1 AUTHOR |
154 | |
100 | |
155 | Marc Lehmann <pcg@goof.com> |
101 | Marc Lehmann <schmorp@schmorp.de> |
156 | http://www.goof.com/pcg/marc/ |
102 | http://home.schmorp.de/ |
157 | |
103 | |
158 | =cut |
104 | =cut |
159 | |
105 | |