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.78 by root, Thu Nov 11 00:09:09 2010 UTC vs.
Revision 1.79 by root, Sat Feb 5 21:20:47 2011 UTC

1=head1 NAME 1=head1 NAME
2 2
3Coro::Timer - timers and timeouts, independent of any event loop 3Coro::Timer - timers and timeouts, independent of any event loop
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6
7 # This package is mostly obsoleted by Coro::AnyEvent.
6 8
7 use Coro::Timer qw(sleep timeout); 9 use Coro::Timer qw(sleep timeout);
8 # nothing exported by default 10 # nothing exported by default
9 11
10 sleep 10; 12 sleep 10;
11 13
12=head1 DESCRIPTION 14=head1 DESCRIPTION
13 15
14This package implements a simple timer callback system which works 16This package has been mostly obsoleted by L<Coro::AnyEvent>, the only
15independent of the event loop mechanism used. 17really useful function left in here is C<timeout>.
16 18
17=over 4 19=over 4
18 20
19=cut 21=cut
20 22
23use common::sense; 25use common::sense;
24 26
25use Carp (); 27use Carp ();
26use base Exporter::; 28use base Exporter::;
27 29
28use AnyEvent ();
29
30use Coro (); 30use Coro ();
31use Coro::AnyEvent (); 31use Coro::AnyEvent ();
32 32
33our $VERSION = 5.25; 33our $VERSION = 5.25;
34our @EXPORT_OK = qw(timeout sleep); 34our @EXPORT_OK = qw(timeout sleep);
35 35
36=item $flag = timeout $seconds; 36# compatibility with older programs
37*sleep = \&Coro::AnyEvent::sleep;
37 38
39=item $flag = timeout $seconds
40
38This function will wake up the current coroutine after $seconds 41This function will wake up the current coroutine after $seconds seconds
39seconds and sets $flag to true (it is false initially). If $flag goes 42and sets $flag to true (it is false initially). If $flag goes out
40out of scope earlier nothing happens. This is used to implement the 43of scope earlier then nothing happens.
41C<timed_down>, C<timed_wait> etc. primitives. It is used like this: 44
45This is used by Coro itself to implement the C<timed_down>, C<timed_wait>
46etc. primitives. It is used like this:
42 47
43 sub timed_wait { 48 sub timed_wait {
44 my $timeout = Coro::Timer::timeout 60; 49 my $timeout = Coro::Timer::timeout 60;
45 50
46 while (condition false) { 51 while (condition false) {
54=cut 59=cut
55 60
56sub timeout($) { 61sub timeout($) {
57 my $current = $Coro::current; 62 my $current = $Coro::current;
58 my $timeout; 63 my $timeout;
64
59 bless { 65 bless [
66 \$timeout,
60 timer => (AE::timer $_[0], 0, sub { 67 (AE::timer $_[0], 0, sub {
61 $timeout = 1; 68 $timeout = 1;
62 $current->ready; 69 $current->ready;
63 }), 70 }),
64 timeout => \$timeout,
65 }, "Coro::Timer::Timeout"; 71 ], "Coro::Timer::Timeout";
66} 72}
67 73
68package Coro::Timer::Timeout; 74package Coro::Timer::Timeout;
69 75
70sub bool { ${$_[0]{timeout}} } 76sub bool { ${ $_[0][0] } }
71 77
72use overload 'bool' => \&bool, '0+' => \&bool; 78use overload 'bool' => \&bool, '0+' => \&bool;
73
74package Coro::Timer;
75
76=item sleep $seconds
77
78This function works like the built-in sleep, except maybe more precise
79and, most important, without blocking other coroutines.
80
81=cut
82
83sub sleep {
84 my $timer = AE::timer $_[0], 0, Coro::rouse_cb;
85 Coro::rouse_wait;
86}
87 79
881; 801;
89 81
90=back 82=back
91 83

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines