… | |
… | |
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 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
25 | |
28 | |
26 | use Carp (); |
29 | use Carp (); |
27 | use Exporter; |
30 | use Exporter; |
28 | |
31 | |
29 | use Coro (); |
32 | use Coro (); |
30 | |
33 | |
31 | BEGIN { |
34 | BEGIN { |
32 | eval "use Time::HiRes 'time'"; |
35 | eval "use Time::HiRes 'time'"; |
33 | } |
36 | } |
34 | |
37 | |
35 | $VERSION = 0.531; |
38 | $VERSION = 1.6; |
36 | @EXPORT_OK = qw(timeout sleep); |
39 | @EXPORT_OK = qw(timeout sleep); |
37 | |
40 | |
38 | =item $flag = timeout $seconds; |
41 | =item $flag = timeout $seconds; |
39 | |
42 | |
40 | This function will wake up the current coroutine after $seconds |
43 | This function will wake up the current coroutine after $seconds |
41 | seconds and sets $flag to true (it is false intiially). If $flag goes |
44 | 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 |
45 | 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: |
46 | C<timed_down>, C<timed_wait> etc. primitives. It is used like this: |
44 | |
47 | |
45 | sub timed_wait { |
48 | sub timed_wait { |
46 | my $timeout = Coro::Timer::timeout 60; |
49 | my $timeout = Coro::Timer::timeout 60; |
… | |
… | |
65 | bless $self, Coro::timeout::; |
68 | bless $self, Coro::timeout::; |
66 | } |
69 | } |
67 | |
70 | |
68 | package Coro::timeout; |
71 | package Coro::timeout; |
69 | |
72 | |
70 | sub bool { !${${$_[0]}} } |
73 | sub bool { |
71 | sub DESTROY { ${${$_[0]}}->cancel } |
74 | !${${$_[0]}} |
|
|
75 | } |
|
|
76 | |
|
|
77 | sub DESTROY { |
|
|
78 | ${${$_[0]}}->cancel; |
|
|
79 | undef ${${$_[0]}}; # without this it leaks like hell. breaks the circular reference inside the closure |
|
|
80 | } |
72 | |
81 | |
73 | use overload 'bool' => \&bool, '0+' => \&bool; |
82 | use overload 'bool' => \&bool, '0+' => \&bool; |
74 | |
83 | |
75 | package Coro::Timer; |
84 | package Coro::Timer; |
76 | |
85 | |
… | |
… | |
81 | |
90 | |
82 | =cut |
91 | =cut |
83 | |
92 | |
84 | sub sleep { |
93 | sub sleep { |
85 | my $current = $Coro::current; |
94 | my $current = $Coro::current; |
86 | _new_timer(time + $_[0], sub { $current->ready }); |
95 | my $timer = _new_timer(time + $_[0], sub { $current->ready }); |
87 | Coro::schedule; |
96 | Coro::schedule; |
|
|
97 | $timer->cancel; |
88 | } |
98 | } |
89 | |
99 | |
90 | =item $timer = new Coro::Timer at/after => xxx, cb => \&yyy; |
100 | =item $timer = new Coro::Timer at/after => xxx, cb => \&yyy; |
91 | |
101 | |
92 | Create a new timer. |
102 | Create a new timer. |
… | |
… | |
140 | }; |
150 | }; |
141 | } |
151 | } |
142 | |
152 | |
143 | =item $timer->cancel |
153 | =item $timer->cancel |
144 | |
154 | |
145 | Cancel the timer (the callback will no longer be called). |
155 | Cancel the timer (the callback will no longer be called). This method MUST |
|
|
156 | be called to remove the timer from memory, otherwise it will never be |
|
|
157 | freed! |
146 | |
158 | |
147 | =cut |
159 | =cut |
148 | |
160 | |
149 | 1; |
161 | 1; |
150 | |
162 | |
151 | =back |
163 | =back |
152 | |
164 | |
153 | =head1 AUTHOR |
165 | =head1 AUTHOR |
154 | |
166 | |
155 | Marc Lehmann <pcg@goof.com> |
167 | Marc Lehmann <schmorp@schmorp.de> |
156 | http://www.goof.com/pcg/marc/ |
168 | http://home.schmorp.de/ |
157 | |
169 | |
158 | =cut |
170 | =cut |
159 | |
171 | |