… | |
… | |
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"; |
… | |
… | |
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 | |
… | |
… | |
246 | my $class = shift; |
269 | my $class = shift; |
247 | |
270 | |
248 | $class->SUPER::new (\&_new_coro, @_) |
271 | $class->SUPER::new (\&_new_coro, @_) |
249 | } |
272 | } |
250 | |
273 | |
251 | =item $process->ready |
274 | =item $success = $process->ready |
252 | |
275 | |
253 | 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. |
254 | |
279 | |
255 | =cut |
280 | =item $is_ready = $process->is_ready |
|
|
281 | |
|
|
282 | Return wether the process is currently the ready queue or not, |
256 | |
283 | |
257 | =item $process->cancel (arg...) |
284 | =item $process->cancel (arg...) |
258 | |
285 | |
259 | 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 |
260 | status (default: the empty list). |
287 | status (default: the empty list). |