ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/AnyEvent.pm
(Generate patch)

Comparing Coro/Coro/AnyEvent.pm (file contents):
Revision 1.19 by root, Tue Sep 23 00:02:20 2008 UTC vs.
Revision 1.33 by root, Mon Nov 24 07:55:28 2008 UTC

21 21
22This module integrates coroutines into any event loop supported by 22This module integrates coroutines into any event loop supported by
23AnyEvent, combining event-based programming with coroutine-based 23AnyEvent, combining event-based programming with coroutine-based
24programming in a natural way. 24programming in a natural way.
25 25
26All you have to do is C<use Coro::AnyEvent> and then you can run a 26All you have to do is C<use Coro::AnyEvent> and then you can run
27coroutines freely. 27coroutines freely.
28 28
29=head1 DESCRIPTION 29=head1 DESCRIPTION
30 30
31This module autodetects the event loop used (by relying on L<AnyEvent>) 31This module autodetects the event loop used (by relying on L<AnyEvent>)
32and will either automatically defer to the high-performance L<Coro::EV> or 32and will either automatically defer to the high-performance L<Coro::EV> or
33L<Coro::Event> modules, or will use a generic integration into any event 33L<Coro::Event> modules, or will use a generic integration into any event
34loop supported by L<AnyEvent>. 34loop supported by L<AnyEvent>.
35 35
36Unfortunately, few event loops (basically only L<EV> and L<Event>) support 36Unfortunately, few event loops (basically only L<EV> and L<Event>) support
37this kind of integration well, and therefore AnyEvent cannot offer the 37this kind of integration well, and consequently, AnyEvent cannot offer the
38required functionality. 38functionality required by this module, so we need to improvise.
39 39
40Here is what this module does when it has to work with other event loops: 40Here is what this module does when it has to work with other event loops:
41 41
42=over 4 42=over 4
43 43
50This creates something similar to an I<idle> watcher, i.e. a watcher 50This creates something similar to an I<idle> watcher, i.e. a watcher
51that keeps the event loop from blocking but still polls for new 51that keeps the event loop from blocking but still polls for new
52events. (Unfortunately, some badly designed event loops (e.g. Event::Lib) 52events. (Unfortunately, some badly designed event loops (e.g. Event::Lib)
53don't support a timeout of C<0> and will always block for a bit). 53don't support a timeout of C<0> and will always block for a bit).
54 54
55The callback for that timer will C<cede> to other coroutines of the same 55The callback for that timer will C<cede> to other coroutines of the
56or higher priority for as long as such coroutines exists. This has the 56same or higher priority for as long as such coroutines exists. This has
57effect of running all coroutines that have work to do will all coroutines 57the effect of running all coroutines that have work to do untill all
58block to wait for external events. 58coroutines block to wait for external events.
59 59
60If no coroutines of equal or higher priority are ready, it will cede 60If no coroutines of equal or higher priority are ready, it will cede
61to any coroutine, but only once. This has the effect of running 61to any coroutine, but only once. This has the effect of running
62lower-priority coroutines as well, but it will not keep higher priority 62lower-priority coroutines as well, but it will not keep higher priority
63coroutines from receiving new events. 63coroutines from receiving new events.
64 64
65The priority used is simply the priority of the coroutine that runs the 65The priority used is simply the priority of the coroutine that runs the
66event loop, usually the main program, and the priority is usually C<0>. 66event loop, usually the main program, which usually has a priority of
67C<0>.
67 68
68=item * provide a suitable idle callback. 69=item * provide a suitable idle callback.
69 70
70In addition to hooking into C<ready>, this module will also provide a 71In addition to hooking into C<ready>, this module will also provide a
71C<$Coro::idle> handler that runs the event loop. It is best not to rely on 72C<$Coro::idle> handler that runs the event loop. It is best not to take
72this, as this is rather inefficient. 73advantage of this too often, as this is rather inefficient, but it should
74work perfectly fine.
73 75
74=item * provide overrides for AnyEvent's condvars 76=item * provide overrides for AnyEvent's condvars
75 77
76This module installs overrides for AnyEvent's condvars. That is, when 78This module installs overrides for AnyEvent's condvars. That is, when
77the module is loaded it will provide its own condition variables. This 79the module is loaded it will provide its own condition variables. This
78makes the coroutine-safe, i.e. you can safely block on them from within a 80makes them coroutine-safe, i.e. you can safely block on them from within a
79coroutine. 81coroutine.
80 82
81=item * lead to data corruption or worse 83=item * lead to data corruption or worse
82 84
83As C<unblock_sub> cannot be by this module (as it is the module that 85As C<unblock_sub> cannot be used by this module (as it is the module
84implements it, basically), you must not call into the event loop 86that implements it, basically), you must not call into the event
85recursively from any coroutine. This is not usually a difficult 87loop recursively from any coroutine. This is not usually a difficult
86restriction to live with, just use condvars, C<unblock_sub> or other means 88restriction to live with, just use condvars, C<unblock_sub> or other means
87of inter-coroutine-communications. 89of inter-coroutine-communications.
88 90
89If you use a module that supports AnyEvent (or uses the same event loop 91If you use a module that supports AnyEvent (or uses the same event loop
90as AnyEvent, making the compatible), and it offers callbacks of any kind, 92as AnyEvent, making the compatible), and it offers callbacks of any kind,
105use strict; 107use strict;
106 108
107use Coro; 109use Coro;
108use AnyEvent (); 110use AnyEvent ();
109 111
110our $VERSION = 4.747; 112our $VERSION = 5.1;
111 113
112############################################################################# 114#############################################################################
113# idle handler 115# idle handler
114 116
115our $IDLE; 117our $IDLE;
138 require Coro::Event; 140 require Coro::Event;
139 } else { 141 } else {
140 Coro::_set_readyhook \&_activity; 142 Coro::_set_readyhook \&_activity;
141 143
142 $IDLE = new Coro sub { 144 $IDLE = new Coro sub {
145 my $one_event = AnyEvent->can ("one_event");
143 while () { 146 while () {
144 AnyEvent->one_event; 147 $one_event->();
145 &Coro::schedule; 148 Coro::schedule;
146 } 149 }
147 }; 150 };
148 $IDLE->{desc} = "[AnyEvent idle process]"; 151 $IDLE->{desc} = "[AnyEvent idle process]";
149 152
150 $Coro::idle = sub { 153 $Coro::idle = $IDLE;
151 local $ACTIVITY = 1; # hack to keep it from being set by the ready call
152 $IDLE->ready;
153 };
154 } 154 }
155}; 155};
156 156
157############################################################################# 157#############################################################################
158# override condvars 158# override condvars

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines