… | |
… | |
41 | |
41 | |
42 | our $idle; # idle handler |
42 | our $idle; # idle handler |
43 | our $main; # main coroutine |
43 | our $main; # main coroutine |
44 | our $current; # current coroutine |
44 | our $current; # current coroutine |
45 | |
45 | |
46 | our $VERSION = '2.5'; |
46 | our $VERSION = '3.0'; |
47 | |
47 | |
48 | our @EXPORT = qw(async cede schedule terminate current); |
48 | our @EXPORT = qw(async cede schedule terminate current); |
49 | our %EXPORT_TAGS = ( |
49 | our %EXPORT_TAGS = ( |
50 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
50 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
51 | ); |
51 | ); |
… | |
… | |
117 | |
117 | |
118 | =item $idle |
118 | =item $idle |
119 | |
119 | |
120 | A callback that is called whenever the scheduler finds no ready coroutines |
120 | A callback that is called whenever the scheduler finds no ready coroutines |
121 | to run. The default implementation prints "FATAL: deadlock detected" and |
121 | to run. The default implementation prints "FATAL: deadlock detected" and |
122 | exits. |
122 | exits, because the program has no other way to continue. |
123 | |
123 | |
124 | This hook is overwritten by modules such as C<Coro::Timer> and |
124 | This hook is overwritten by modules such as C<Coro::Timer> and |
125 | C<Coro::Event> to wait on an external event that hopefully wakes up some |
125 | C<Coro::Event> to wait on an external event that hopefully wake up a |
126 | coroutine. |
126 | coroutine so the scheduler can run it. |
|
|
127 | |
|
|
128 | Please note that if your callback recursively invokes perl (e.g. for event |
|
|
129 | handlers), then it must be prepared to be called recursively. |
127 | |
130 | |
128 | =cut |
131 | =cut |
129 | |
132 | |
130 | $idle = sub { |
133 | $idle = sub { |
131 | print STDERR "FATAL: deadlock detected\n"; |
134 | print STDERR "FATAL: deadlock detected\n"; |
… | |
… | |
133 | }; |
136 | }; |
134 | |
137 | |
135 | # this coroutine is necessary because a coroutine |
138 | # this coroutine is necessary because a coroutine |
136 | # cannot destroy itself. |
139 | # cannot destroy itself. |
137 | my @destroy; |
140 | my @destroy; |
138 | my $manager; |
|
|
139 | $manager = new Coro sub { |
141 | my $manager; $manager = new Coro sub { |
140 | while () { |
142 | while () { |
141 | # by overwriting the state object with the manager we destroy it |
143 | # by overwriting the state object with the manager we destroy it |
142 | # while still being able to schedule this coroutine (in case it has |
144 | # while still being able to schedule this coroutine (in case it has |
143 | # been readied multiple times. this is harmless since the manager |
145 | # been readied multiple times. this is harmless since the manager |
144 | # can be called as many times as neccessary and will always |
146 | # can be called as many times as neccessary and will always |
… | |
… | |
172 | |
174 | |
173 | Create a new asynchronous process and return it's process object |
175 | Create a new asynchronous process and return it's process object |
174 | (usually unused). When the sub returns the new process is automatically |
176 | (usually unused). When the sub returns the new process is automatically |
175 | terminated. |
177 | terminated. |
176 | |
178 | |
|
|
179 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
|
|
180 | |
177 | When the coroutine dies, the program will exit, just as in the main |
181 | When the coroutine dies, the program will exit, just as in the main |
178 | program. |
182 | program. |
179 | |
183 | |
180 | # create a new coroutine that just prints its arguments |
184 | # create a new coroutine that just prints its arguments |
181 | async { |
185 | async { |
… | |
… | |
184 | |
188 | |
185 | =cut |
189 | =cut |
186 | |
190 | |
187 | sub async(&@) { |
191 | sub async(&@) { |
188 | my $pid = new Coro @_; |
192 | my $pid = new Coro @_; |
189 | $manager->ready; # this ensures that the stack is cloned from the manager |
|
|
190 | $pid->ready; |
193 | $pid->ready; |
191 | $pid; |
194 | $pid |
192 | } |
195 | } |
193 | |
196 | |
194 | =item schedule |
197 | =item schedule |
195 | |
198 | |
196 | Calls the scheduler. Please note that the current process will not be put |
199 | Calls the scheduler. Please note that the current process will not be put |
197 | into the ready queue, so calling this function usually means you will |
200 | into the ready queue, so calling this function usually means you will |
198 | never be called again. |
201 | never be called again unless something else (e.g. an event handler) calls |
|
|
202 | ready. |
|
|
203 | |
|
|
204 | The canonical way to wait on external events is this: |
|
|
205 | |
|
|
206 | { |
|
|
207 | # remember current process |
|
|
208 | my $current = $Coro::current; |
|
|
209 | |
|
|
210 | # register a hypothetical event handler |
|
|
211 | on_event_invoke sub { |
|
|
212 | # wake up sleeping coroutine |
|
|
213 | $current->ready; |
|
|
214 | undef $current; |
|
|
215 | }; |
|
|
216 | |
|
|
217 | # call schedule until event occured. |
|
|
218 | # in case we are woken up for other reasons |
|
|
219 | # (current still defined), loop. |
|
|
220 | Coro::schedule while $current; |
|
|
221 | } |
199 | |
222 | |
200 | =cut |
223 | =cut |
201 | |
224 | |
202 | =item cede |
225 | =item cede |
203 | |
226 | |
… | |
… | |
232 | Create a new process and return it. When the sub returns the process |
255 | Create a new process and return it. When the sub returns the process |
233 | automatically terminates as if C<terminate> with the returned values were |
256 | automatically terminates as if C<terminate> with the returned values were |
234 | called. To make the process run you must first put it into the ready queue |
257 | called. To make the process run you must first put it into the ready queue |
235 | by calling the ready method. |
258 | by calling the ready method. |
236 | |
259 | |
|
|
260 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
|
|
261 | |
237 | =cut |
262 | =cut |
238 | |
263 | |
239 | sub _new_coro { |
264 | sub _new_coro { |
240 | # $current->_clear_idle_sp; # set the idle sp on the following cede |
|
|
241 | _set_cede_self; # ensures that cede cede's us first |
|
|
242 | cede; |
|
|
243 | terminate &{+shift}; |
265 | terminate &{+shift}; |
244 | } |
266 | } |
245 | |
267 | |
246 | sub new { |
268 | sub new { |
247 | my $class = shift; |
269 | my $class = shift; |
248 | |
270 | |
249 | $class->SUPER::new (\&_new_coro, @_) |
271 | $class->SUPER::new (\&_new_coro, @_) |
250 | } |
272 | } |
251 | |
273 | |
252 | =item $process->ready |
274 | =item $success = $process->ready |
253 | |
275 | |
254 | Put the given process into the ready queue. |
276 | Put the given process into the ready queue (according to it's priority) |
|
|
277 | and return true. If the process is already in the ready queue, do nothing |
|
|
278 | and return false. |
255 | |
279 | |
256 | =cut |
280 | =item $is_ready = $process->is_ready |
|
|
281 | |
|
|
282 | Return wether the process is currently the ready queue or not, |
257 | |
283 | |
258 | =item $process->cancel (arg...) |
284 | =item $process->cancel (arg...) |
259 | |
285 | |
260 | Terminates the given process and makes it return the given arguments as |
286 | Terminates the given process and makes it return the given arguments as |
261 | status (default: the empty list). |
287 | status (default: the empty list). |