1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Coro::Event - do events the coro-way |
3 | Coro::Event - do events the coro-way, with Event |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use Coro; |
7 | use Coro; |
8 | use Coro::Event; |
8 | use Coro::Event; |
… | |
… | |
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 | |
78 | =cut |
77 | =cut |
79 | |
78 | |
80 | package Coro::Event; |
79 | package Coro::Event; |
81 | |
80 | |
82 | no warnings; |
81 | use common::sense; |
83 | |
82 | |
84 | use Carp; |
83 | use Carp; |
85 | no warnings; |
|
|
86 | |
84 | |
87 | use Coro; |
85 | use Coro; |
88 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
86 | use Event qw(loop unloop); # we are re-exporting this for historical reasons |
89 | |
87 | |
90 | use XSLoader; |
88 | use XSLoader; |
91 | |
89 | |
92 | use base Exporter::; |
90 | use base Exporter::; |
93 | |
91 | |
94 | our @EXPORT = qw(loop unloop sweep); |
92 | our @EXPORT = qw(loop unloop sweep); |
95 | |
93 | |
96 | BEGIN { |
94 | BEGIN { |
97 | our $VERSION = 4.803; |
95 | our $VERSION = 6.57; |
98 | |
96 | |
99 | local $^W = 0; # avoid redefine warning for Coro::ready; |
97 | local $^W = 0; # avoid redefine warning for Coro::ready; |
100 | XSLoader::load __PACKAGE__, $VERSION; |
98 | XSLoader::load __PACKAGE__, $VERSION; |
101 | } |
99 | } |
102 | |
100 | |
… | |
… | |
104 | |
102 | |
105 | Create and return a watcher of the given type. |
103 | Create and return a watcher of the given type. |
106 | |
104 | |
107 | Examples: |
105 | Examples: |
108 | |
106 | |
109 | my $reader = Coro::Event->io(fd => $filehandle, poll => 'r'); |
107 | my $reader = Coro::Event->io (fd => $filehandle, poll => 'r'); |
110 | $reader->next; |
108 | $reader->next; |
111 | |
109 | |
112 | =cut |
110 | =cut |
113 | |
111 | |
114 | =item $w->next |
112 | =item $w->next |
… | |
… | |
120 | |
118 | |
121 | =cut |
119 | =cut |
122 | |
120 | |
123 | =item do_flavour args... |
121 | =item do_flavour args... |
124 | |
122 | |
125 | Create a watcher of the given type and immediately call it's next method, |
123 | Create a watcher of the given type and immediately call its next method, |
126 | returning the event. |
124 | returning the event. |
127 | |
125 | |
128 | This is less efficient then calling the constructor once and the next |
126 | This is less efficient then calling the constructor once and the next |
129 | method often, but it does save typing sometimes. |
127 | method often, but it does save typing sometimes. |
130 | |
128 | |
… | |
… | |
189 | |
187 | |
190 | sub sweep { |
188 | sub sweep { |
191 | Event::one_event 0; # for now |
189 | Event::one_event 0; # for now |
192 | } |
190 | } |
193 | |
191 | |
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 |
192 | # very inefficient |
207 | our $event_idle = new Coro sub { |
193 | our $IDLE = new Coro sub { |
208 | while () { |
194 | while () { |
209 | &Event::one_event; |
195 | Event::one_event; |
210 | &Coro::schedule; |
196 | Coro::schedule if Coro::nready; |
211 | } |
197 | } |
212 | }; |
198 | }; |
213 | $event_idle->{desc} = "[Event idle process]"; |
199 | $IDLE->{desc} = "[Event idle thread]"; |
214 | |
200 | |
215 | $Coro::idle = sub { $event_idle->ready }; |
201 | $Coro::idle = $IDLE; |
216 | |
202 | |
217 | 1; |
203 | 1; |
218 | |
204 | |
219 | =back |
205 | =back |
220 | |
206 | |
221 | =head1 AUTHOR |
207 | =head1 AUTHOR/SUPPORT/CONTACT |
222 | |
208 | |
223 | Marc Lehmann <schmorp@schmorp.de> |
209 | Marc A. Lehmann <schmorp@schmorp.de> |
224 | http://home.schmorp.de/ |
210 | http://software.schmorp.de/pkg/Coro.html |
225 | |
211 | |
226 | =cut |
212 | =cut |
227 | |
213 | |