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; |
… | |
… | |
18 | } |
18 | } |
19 | } |
19 | } |
20 | |
20 | |
21 | loop; |
21 | loop; |
22 | |
22 | |
|
|
23 | # wait for input on stdin for one second |
|
|
24 | Coro::Event::do_io (fd => \*STDIN, timeout => 1) & Event::Watcher::R |
|
|
25 | or die "no input received"; |
|
|
26 | |
|
|
27 | # use a separate thread for event processing, if impossible in main: |
|
|
28 | Coro::async { Event::loop }; |
|
|
29 | |
23 | =head1 DESCRIPTION |
30 | =head1 DESCRIPTION |
24 | |
31 | |
25 | 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 |
26 | (and module), while retaining the linear style known from simple or |
33 | (and module), while retaining the linear style known from simple or |
27 | threaded programs. |
34 | threaded programs. |
… | |
… | |
29 | 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 |
30 | (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 |
31 | 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 |
32 | function - it will be managed by this module. |
39 | function - it will be managed by this module. |
33 | |
40 | |
34 | Your application should just create all necessary coroutines and then call |
41 | Your application should just create all necessary threads and then call |
35 | Coro::Event::loop. |
42 | C<Event::loop>. |
36 | |
43 | |
37 | Please note that even programs or modules (such as |
44 | Please note that even programs or modules (such as L<Coro::Handle>) that |
38 | L<Coro::Handle|Coro::Handle>) that use "traditional" |
|
|
39 | event-based/continuation style will run more efficient with this module |
45 | use "traditional" event-based/continuation style will run more efficient |
40 | then when using only Event. |
46 | with this module then when using only Event. |
41 | |
47 | |
42 | =head1 WARNING |
48 | =head1 WARNING |
43 | |
49 | |
44 | Please note that Event does not support coroutines or threads. That |
50 | Please note that Event does not support multithreading. That means that |
45 | 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, |
46 | callbacks, you I<must never ever> call a Coroutine fucntion that blocks |
52 | you I<must never ever> call a Coro function that blocks the current |
47 | the current coroutine. |
53 | thread. |
48 | |
54 | |
49 | While this seems to work superficially, it will eventually cause memory |
55 | While this seems to work superficially, it will eventually cause memory |
50 | corruption. |
56 | corruption and often results in deadlocks. |
|
|
57 | |
|
|
58 | Best practise is to always use B<Coro::unblock_sub> for your callbacks. |
51 | |
59 | |
52 | =head1 SEMANTICS |
60 | =head1 SEMANTICS |
53 | |
61 | |
54 | 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.), |
55 | 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 |
56 | 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 |
57 | block but run one of them and then check for events. |
65 | block but run one of them and then check for events. |
58 | |
66 | |
59 | 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 |
60 | the blocking coroutine will keep Event from checking for events, while |
68 | the blocking coroutine will keep Event from checking for events, while |
61 | 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 |
62 | 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. |
63 | |
72 | |
64 | =head1 FUNCTIONS |
73 | =head1 FUNCTIONS |
65 | |
74 | |
66 | =over 4 |
75 | =over 4 |
67 | |
76 | |
68 | =cut |
77 | =cut |
69 | |
78 | |
70 | package Coro::Event; |
79 | package Coro::Event; |
71 | |
80 | |
72 | no warnings; |
81 | use common::sense; |
73 | |
82 | |
74 | use Carp; |
83 | use Carp; |
75 | no warnings; |
|
|
76 | |
84 | |
77 | use Coro; |
85 | use Coro; |
78 | use Coro::Timer; |
|
|
79 | 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 |
80 | |
87 | |
81 | use XSLoader; |
88 | use XSLoader; |
82 | |
89 | |
83 | use base Exporter::; |
90 | use base Exporter::; |
84 | |
91 | |
85 | our @EXPORT = qw(loop unloop sweep); |
92 | our @EXPORT = qw(loop unloop sweep); |
86 | |
93 | |
87 | BEGIN { |
94 | BEGIN { |
88 | our $VERSION = '2.0'; |
95 | our $VERSION = 6.57; |
89 | |
96 | |
90 | local $^W = 0; # avoid redefine warning for Coro::ready; |
97 | local $^W = 0; # avoid redefine warning for Coro::ready; |
91 | XSLoader::load __PACKAGE__, $VERSION; |
98 | XSLoader::load __PACKAGE__, $VERSION; |
92 | } |
99 | } |
93 | |
100 | |
… | |
… | |
95 | |
102 | |
96 | Create and return a watcher of the given type. |
103 | Create and return a watcher of the given type. |
97 | |
104 | |
98 | Examples: |
105 | Examples: |
99 | |
106 | |
100 | my $reader = Coro::Event->io(fd => $filehandle, poll => 'r'); |
107 | my $reader = Coro::Event->io (fd => $filehandle, poll => 'r'); |
101 | $reader->next; |
108 | $reader->next; |
102 | |
109 | |
103 | =cut |
110 | =cut |
104 | |
111 | |
105 | =item $w->next |
112 | =item $w->next |
106 | |
113 | |
107 | Return the next event of the event queue of the watcher. |
114 | Wait for and return the next event of the event queue of the watcher. The |
|
|
115 | returned event objects support two methods only: C<hits> and C<got>, both |
|
|
116 | of which return integers: the number this watcher was hit for this event, |
|
|
117 | and the mask of poll events received. |
108 | |
118 | |
109 | =cut |
119 | =cut |
110 | |
120 | |
111 | =item do_flavour args... |
121 | =item do_flavour args... |
112 | |
122 | |
113 | Create a watcher of the given type and immediately call it's next |
123 | Create a watcher of the given type and immediately call its next method, |
|
|
124 | returning the event. |
|
|
125 | |
114 | method. This is less efficient then calling the constructor once and the |
126 | This is less efficient then calling the constructor once and the next |
115 | next method often, but it does save typing sometimes. |
127 | method often, but it does save typing sometimes. |
116 | |
128 | |
117 | =cut |
129 | =cut |
118 | |
130 | |
119 | for my $flavour (qw(idle var timer io signal)) { |
131 | for my $flavour (qw(idle var timer io signal)) { |
120 | push @EXPORT, "do_$flavour"; |
132 | push @EXPORT, "do_$flavour"; |
… | |
… | |
128 | |
140 | |
129 | shift eq Coro::Event:: |
141 | shift eq Coro::Event:: |
130 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
142 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
131 | |
143 | |
132 | my $w = $new->($class, |
144 | my $w = $new->($class, |
133 | desc => $flavour, |
145 | desc => $flavour, |
134 | @_, |
146 | @_, |
135 | parked => 1, |
147 | parked => 1, |
136 | ); |
148 | ); |
137 | |
149 | |
138 | _install_std_cb $w, $type; |
150 | _install_std_cb $w, $type; |
139 | |
151 | |
140 | # reblessing due to Event being broken |
152 | # reblessing due to Event being broken |
… | |
… | |
152 | |
164 | |
153 | # do schedule in perl to avoid forcing a stack allocation. |
165 | # do schedule in perl to avoid forcing a stack allocation. |
154 | # this is about 10% slower, though. |
166 | # this is about 10% slower, though. |
155 | sub next($) { |
167 | sub next($) { |
156 | &Coro::schedule while &_next; |
168 | &Coro::schedule while &_next; |
157 | |
|
|
158 | &_event |
169 | &_event |
159 | } |
170 | } |
160 | |
171 | |
|
|
172 | sub Coro::Event::Event::hits { $_[0][3] } |
161 | sub Coro::Event::w { $_[0] } |
173 | sub Coro::Event::Event::got { $_[0][4] } |
162 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
|
|
163 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
|
|
164 | sub Coro::Event::got { $_[0]{Coro::Event}[5] } |
|
|
165 | |
174 | |
166 | =item sweep |
175 | =item sweep |
167 | |
176 | |
168 | Similar to Event::one_event and Event::sweep: The idle task is called once |
177 | Similar to Event::one_event and Event::sweep: The idle task is called once |
169 | (this has the effect of jumping back into the Event loop once to serve new |
178 | (this has the effect of jumping back into the Event loop once to serve new |
… | |
… | |
178 | |
187 | |
179 | sub sweep { |
188 | sub sweep { |
180 | Event::one_event 0; # for now |
189 | Event::one_event 0; # for now |
181 | } |
190 | } |
182 | |
191 | |
183 | =item $result = loop([$timeout]) |
192 | # very inefficient |
|
|
193 | our $IDLE = new Coro sub { |
|
|
194 | while () { |
|
|
195 | Event::one_event; |
|
|
196 | Coro::schedule if Coro::nready; |
|
|
197 | } |
|
|
198 | }; |
|
|
199 | $IDLE->{desc} = "[Event idle thread]"; |
184 | |
200 | |
185 | This is the version of C<loop> you should use instead of C<Event::loop> |
201 | $Coro::idle = $IDLE; |
186 | when using this module - it will ensure correct scheduling in the presence |
|
|
187 | of events. |
|
|
188 | |
|
|
189 | =item unloop([$result]) |
|
|
190 | |
|
|
191 | Same as Event::unloop (provided here for your convinience only). |
|
|
192 | |
|
|
193 | =cut |
|
|
194 | |
|
|
195 | $Coro::idle = \&Event::one_event; # inefficient |
|
|
196 | |
202 | |
197 | 1; |
203 | 1; |
198 | |
204 | |
199 | =back |
205 | =back |
200 | |
206 | |
201 | =head1 AUTHOR |
207 | =head1 AUTHOR/SUPPORT/CONTACT |
202 | |
208 | |
203 | Marc Lehmann <schmorp@schmorp.de> |
209 | Marc A. Lehmann <schmorp@schmorp.de> |
204 | http://home.schmorp.de/ |
210 | http://software.schmorp.de/pkg/Coro.html |
205 | |
211 | |
206 | =cut |
212 | =cut |
207 | |
213 | |