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.33 by root, Fri Nov 24 15:34:33 2006 UTC vs.
Revision 1.65 by root, Tue Jun 30 08:28:55 2009 UTC

1=head1 NAME 1=head1 NAME
2 2
3Coro::Timer - simple timer package, independent of used event loops 3Coro::Timer - timers and timeouts, independent of any event loop
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Coro::Timer qw(sleep timeout); 7 use Coro::Timer qw(sleep timeout);
8 # nothing exported by default 8 # nothing exported by default
10 sleep 10; 10 sleep 10;
11 11
12=head1 DESCRIPTION 12=head1 DESCRIPTION
13 13
14This package implements a simple timer callback system which works 14This package implements a simple timer callback system which works
15independent of the event loop mechanism used. If no event mechanism is 15independent of the event loop mechanism used.
16used, it is emulated. The C<Coro::Event> module overwrites functions with
17versions better suited.
18
19This module is not subclassable.
20 16
21=over 4 17=over 4
22 18
23=cut 19=cut
24 20
27no warnings; 23no warnings;
28 24
29use Carp (); 25use Carp ();
30use Exporter; 26use Exporter;
31 27
32use Coro ();
33use AnyEvent (); 28use AnyEvent ();
34 29
30use Coro ();
31use Coro::AnyEvent ();
32
35$VERSION = "2.0"; 33$VERSION = 5.15;
36@EXPORT_OK = qw(timeout sleep); 34@EXPORT_OK = qw(timeout sleep);
37 35
38=item $flag = timeout $seconds; 36=item $flag = timeout $seconds;
39 37
40This function will wake up the current coroutine after $seconds 38This function will wake up the current coroutine after $seconds
44 42
45 sub timed_wait { 43 sub timed_wait {
46 my $timeout = Coro::Timer::timeout 60; 44 my $timeout = Coro::Timer::timeout 60;
47 45
48 while (condition false) { 46 while (condition false) {
49 schedule; # wait until woken up or timeout 47 Coro::schedule; # wait until woken up or timeout
50 return 0 if $timeout; # timed out 48 return 0 if $timeout; # timed out
51 } 49 }
50
52 return 1; # condition satisfied 51 return 1; # condition satisfied
53 } 52 }
54 53
55=cut 54=cut
56 55
81and, most important, without blocking other coroutines. 80and, most important, without blocking other coroutines.
82 81
83=cut 82=cut
84 83
85sub sleep { 84sub sleep {
86 my $current = $Coro::current;
87 my $timer = AnyEvent->timer (after => $_[0], cb => sub { $current->ready }); 85 my $timer = AnyEvent->timer (after => $_[0], cb => Coro::rouse_cb);
88 Coro::schedule; 86 Coro::rouse_wait;
89} 87}
90
91$Coro::idle = sub {
92 AnyEvent->one_event;
93};
94 88
951; 891;
96 90
97=back 91=back
98 92

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines