Revision: | 1.34 |
Committed: | Fri Dec 1 02:17:37 2006 UTC (17 years, 8 months ago) by root |
Branch: | MAIN |
CVS Tags: | rel-4_22, rel-4_21, rel-4_0, rel-4_3, rel-3_41, rel-4_13, rel-4_11, rel-3_55, rel-3_51, rel-4_01, rel-4_03, rel-4_02, rel-3_6, rel-3_62, rel-3_63, rel-3_61, rel-3_4, rel-3_1, rel-3_5, rel-3_3, rel-3_2, rel-3_0, rel-3_01, rel-4_50, rel-4_51, rel-4_4, rel-3_11, rel-4_45, rel-4_49, rel-4_48, rel-4_1, rel-4_2, rel-4_47, rel-4_46, rel-3_501, rel-4_31, rel-4_32, rel-4_33, rel-4_34, rel-4_35, rel-4_36, rel-4_37 |
Changes since 1.33: | +9 -3 lines |
Log Message: | *** empty log message *** |
# | Content |
---|---|
1 | =head1 NAME |
2 | |
3 | Coro::Timer - simple timer package, independent of used event loops |
4 | |
5 | =head1 SYNOPSIS |
6 | |
7 | use Coro::Timer qw(sleep timeout); |
8 | # nothing exported by default |
9 | |
10 | sleep 10; |
11 | |
12 | =head1 DESCRIPTION |
13 | |
14 | This package implements a simple timer callback system which works |
15 | independent of the event loop mechanism used. If no event mechanism is |
16 | used, it is emulated. The C<Coro::Event> module overwrites functions with |
17 | versions better suited. |
18 | |
19 | This module is not subclassable. |
20 | |
21 | =over 4 |
22 | |
23 | =cut |
24 | |
25 | package Coro::Timer; |
26 | |
27 | no warnings; |
28 | |
29 | use Carp (); |
30 | use Exporter; |
31 | |
32 | use Coro (); |
33 | use AnyEvent (); |
34 | |
35 | $VERSION = "2.0"; |
36 | @EXPORT_OK = qw(timeout sleep); |
37 | |
38 | =item $flag = timeout $seconds; |
39 | |
40 | This function will wake up the current coroutine after $seconds |
41 | seconds and sets $flag to true (it is false initially). If $flag goes |
42 | out of scope earlier nothing happens. This is used to implement the |
43 | C<timed_down>, C<timed_wait> etc. primitives. It is used like this: |
44 | |
45 | sub timed_wait { |
46 | my $timeout = Coro::Timer::timeout 60; |
47 | |
48 | while (condition false) { |
49 | Coro::schedule; # wait until woken up or timeout |
50 | return 0 if $timeout; # timed out |
51 | } |
52 | |
53 | return 1; # condition satisfied |
54 | } |
55 | |
56 | =cut |
57 | |
58 | # deep magic, expecially the double indirection :(:( |
59 | sub timeout($) { |
60 | my $current = $Coro::current; |
61 | my $timeout; |
62 | bless { |
63 | timer => AnyEvent->timer (after => $_[0], cb => sub { |
64 | $timeout = 1; |
65 | $current->ready; |
66 | }), |
67 | timeout => \$timeout, |
68 | }, "Coro::Timer::Timeout"; |
69 | } |
70 | |
71 | package Coro::Timer::Timeout; |
72 | |
73 | sub bool { ${$_[0]{timeout}} } |
74 | |
75 | use overload 'bool' => \&bool, '0+' => \&bool; |
76 | |
77 | package Coro::Timer; |
78 | |
79 | =item sleep $seconds |
80 | |
81 | This function works like the built-in sleep, except maybe more precise |
82 | and, most important, without blocking other coroutines. |
83 | |
84 | =cut |
85 | |
86 | sub sleep { |
87 | my $current = $Coro::current; |
88 | |
89 | my $timer = AnyEvent->timer (after => $_[0], cb => sub { |
90 | $current->ready; |
91 | undef $current; |
92 | }); |
93 | |
94 | do { &Coro::schedule } while $current; |
95 | } |
96 | |
97 | $Coro::idle = sub { |
98 | AnyEvent->one_event; |
99 | }; |
100 | |
101 | 1; |
102 | |
103 | =back |
104 | |
105 | =head1 AUTHOR |
106 | |
107 | Marc Lehmann <schmorp@schmorp.de> |
108 | http://home.schmorp.de/ |
109 | |
110 | =cut |
111 |