… | |
… | |
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 |
… | |
… | |
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 = 0.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 |
… | |
… | |
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 | |