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.9 by root, Thu Nov 21 13:08:06 2002 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
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 = 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
40This function will wake up the current coroutine after $seconds 43This function will wake up the current coroutine after $seconds
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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines