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