ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Timer.pm
Revision: 1.82
Committed: Mon Feb 21 13:38:04 2011 UTC (13 years, 3 months ago) by root
Branch: MAIN
CVS Tags: rel-5_371
Changes since 1.81: +1 -1 lines
Log Message:
5.371

File Contents

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