ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Timer.pm
Revision: 1.127
Committed: Mon Mar 16 11:12:53 2020 UTC (4 years, 2 months ago) by root
Branch: MAIN
CVS Tags: rel-6_57, HEAD
Changes since 1.126: +1 -1 lines
Log Message:
*** empty log message ***

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