… | |
… | |
19 | } |
19 | } |
20 | |
20 | |
21 | loop; |
21 | loop; |
22 | |
22 | |
23 | # wait for input on stdin for one second |
23 | # wait for input on stdin for one second |
24 | |
|
|
25 | Coro::Event::do_io (fd => \*STDIN, timeout => 1) & Event::Watcher::R |
24 | Coro::Event::do_io (fd => \*STDIN, timeout => 1) & Event::Watcher::R |
26 | or die "no input received"; |
25 | or die "no input received"; |
27 | |
26 | |
28 | # use a separate coroutine for event processing, if impossible in main: |
27 | # use a separate thread for event processing, if impossible in main: |
29 | Coro::async { Event::loop }; |
28 | Coro::async { Event::loop }; |
30 | |
29 | |
31 | =head1 DESCRIPTION |
30 | =head1 DESCRIPTION |
32 | |
31 | |
33 | This module enables you to create programs using the powerful Event model |
32 | This module enables you to create programs using the powerful Event model |
… | |
… | |
37 | This module provides a method and a function for every watcher type |
36 | This module provides a method and a function for every watcher type |
38 | (I<flavour>) (see L<Event>). The only difference between these and the |
37 | (I<flavour>) (see L<Event>). The only difference between these and the |
39 | watcher constructors from Event is that you do not specify a callback |
38 | watcher constructors from Event is that you do not specify a callback |
40 | function - it will be managed by this module. |
39 | function - it will be managed by this module. |
41 | |
40 | |
42 | Your application should just create all necessary coroutines and then call |
41 | Your application should just create all necessary threads and then call |
43 | Coro::Event::loop. |
42 | C<Event::loop>. |
44 | |
43 | |
45 | Please note that even programs or modules (such as |
44 | Please note that even programs or modules (such as L<Coro::Handle>) that |
46 | L<Coro::Handle|Coro::Handle>) that use "traditional" |
|
|
47 | event-based/continuation style will run more efficient with this module |
45 | use "traditional" event-based/continuation style will run more efficient |
48 | then when using only Event. |
46 | with this module then when using only Event. |
49 | |
47 | |
50 | =head1 WARNING |
48 | =head1 WARNING |
51 | |
49 | |
52 | Please note that Event does not support coroutines or threads. That |
50 | Please note that Event does not support multithreading. That means that |
53 | means that you B<MUST NOT> block in an event callback. Again: In Event |
51 | you B<MUST NOT> block in an event callback. Again: In Event callbacks, |
54 | callbacks, you I<must never ever> call a Coroutine function that blocks |
52 | you I<must never ever> call a Coro function that blocks the current |
55 | the current coroutine. |
53 | thread. |
56 | |
54 | |
57 | While this seems to work superficially, it will eventually cause memory |
55 | While this seems to work superficially, it will eventually cause memory |
58 | corruption and often results in deadlocks. |
56 | corruption and often results in deadlocks. |
59 | |
57 | |
60 | Best practise is to always use B<Coro::unblock_sub> for your callbacks. |
58 | Best practise is to always use B<Coro::unblock_sub> for your callbacks. |
61 | |
59 | |
62 | =head1 SEMANTICS |
60 | =head1 SEMANTICS |
63 | |
61 | |
64 | Whenever Event blocks (e.g. in a call to C<one_event>, C<loop> etc.), |
62 | Whenever Event blocks (e.g. in a call to C<one_event>, C<loop> etc.), |
65 | this module cede's to all other coroutines with the same or higher |
63 | this module cede's to all other threads with the same or higher |
66 | priority. When any coroutines of lower priority are ready, it will not |
64 | priority. When any threads of lower priority are ready, it will not |
67 | block but run one of them and then check for events. |
65 | block but run one of them and then check for events. |
68 | |
66 | |
69 | The effect is that coroutines with the same or higher priority than |
67 | The effect is that coroutines with the same or higher priority than |
70 | the blocking coroutine will keep Event from checking for events, while |
68 | the blocking coroutine will keep Event from checking for events, while |
71 | coroutines with lower priority are being run, but Event checks for new |
69 | coroutines with lower priority are being run, but Event checks for new |
72 | events after every cede. |
70 | events after every cede. Note that for this to work you actually need to |
|
|
71 | run the event loop in some thread. |
73 | |
72 | |
74 | =head1 FUNCTIONS |
73 | =head1 FUNCTIONS |
75 | |
74 | |
76 | =over 4 |
75 | =over 4 |
77 | |
76 | |
… | |
… | |
83 | |
82 | |
84 | use Carp; |
83 | use Carp; |
85 | no warnings; |
84 | no warnings; |
86 | |
85 | |
87 | use Coro; |
86 | use Coro; |
88 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
87 | use Event qw(loop unloop); # we are re-exporting this for historical reasons |
89 | |
88 | |
90 | use XSLoader; |
89 | use XSLoader; |
91 | |
90 | |
92 | use base Exporter::; |
91 | use base Exporter::; |
93 | |
92 | |
… | |
… | |
189 | |
188 | |
190 | sub sweep { |
189 | sub sweep { |
191 | Event::one_event 0; # for now |
190 | Event::one_event 0; # for now |
192 | } |
191 | } |
193 | |
192 | |
194 | =item $result = loop([$timeout]) |
|
|
195 | |
|
|
196 | This is the version of C<loop> you should use instead of C<Event::loop> |
|
|
197 | when using this module - it will ensure correct scheduling in the presence |
|
|
198 | of events. |
|
|
199 | |
|
|
200 | =item unloop([$result]) |
|
|
201 | |
|
|
202 | Same as Event::unloop (provided here for your convinience only). |
|
|
203 | |
|
|
204 | =cut |
|
|
205 | |
|
|
206 | # very inefficient |
193 | # very inefficient |
207 | our $IDLE = new Coro sub { |
194 | our $IDLE = new Coro sub { |
208 | while () { |
195 | while () { |
209 | &Event::one_event; |
196 | &Event::one_event; |
210 | &Coro::schedule; |
197 | &Coro::schedule; |