ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Timer.pm
(Generate patch)

Comparing Coro/Coro/Timer.pm (file contents):
Revision 1.6 by root, Mon Dec 10 21:18:30 2001 UTC vs.
Revision 1.28 by root, Mon Dec 12 20:31:23 2005 UTC

2 2
3Coro::Timer - simple timer package, independent of used event loops 3Coro::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
11This package implements a simple timer callback system which works 14This package implements a simple timer callback system which works
12independent of the event loop mechanism used. If no event mechanism is 15independent of the event loop mechanism used. If no event mechanism is
19 22
20=cut 23=cut
21 24
22package Coro::Timer; 25package Coro::Timer;
23 26
24no warnings qw(uninitialized); 27BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") }
25 28
26use Carp (); 29use Carp ();
27use Exporter; 30use Exporter;
28 31
29use Coro (); 32use Coro ();
30 33
31BEGIN { 34BEGIN {
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
40This function will wake up the current coroutine after $seconds 43This function will wake up the current coroutine after $seconds
41seconds and sets $flag to true (it is false intiially). If $flag goes 44seconds and sets $flag to true (it is false initially). If $flag goes
42out of scope earlier nothing happens. This is used to implement the 45out of scope earlier nothing happens. This is used to implement the
43C<timed_down>, C<timed_wait> etc. primitives. It is used like this: 46C<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
68package Coro::timeout; 71package Coro::timeout;
69 72
70sub bool { !${${$_[0]}} } 73sub bool {
71sub DESTROY { ${${$_[0]}}->cancel } 74 !${${$_[0]}}
75}
76
77sub DESTROY {
78 ${${$_[0]}}->cancel;
79 undef ${${$_[0]}}; # without this it leaks like hell. breaks the circular reference inside the closure
80}
72 81
73use overload 'bool' => \&bool, '0+' => \&bool; 82use overload 'bool' => \&bool, '0+' => \&bool;
74 83
75package Coro::Timer; 84package Coro::Timer;
76 85
81 90
82=cut 91=cut
83 92
84sub sleep { 93sub 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
92Create a new timer. 102Create a new timer.
140 }; 150 };
141} 151}
142 152
143=item $timer->cancel 153=item $timer->cancel
144 154
145Cancel the timer (the callback will no longer be called). 155Cancel the timer (the callback will no longer be called). This method MUST
156be called to remove the timer from memory, otherwise it will never be
157freed!
146 158
147=cut 159=cut
148 160
1491; 1611;
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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines