… | |
… | |
17 | cede; |
17 | cede; |
18 | |
18 | |
19 | DESCRIPTION |
19 | DESCRIPTION |
20 | This module collection manages coroutines. Coroutines are similar to |
20 | This module collection manages coroutines. Coroutines are similar to |
21 | threads but don't run in parallel at the same time even on SMP machines. |
21 | threads but don't run in parallel at the same time even on SMP machines. |
22 | The specific flavor of coroutine use din this module also guarentees you |
22 | The specific flavor of coroutine used in this module also guarantees you |
23 | that it will not switch between coroutines unless necessary, at |
23 | that it will not switch between coroutines unless necessary, at |
24 | easily-identified points in your program, so locking and parallel access |
24 | easily-identified points in your program, so locking and parallel access |
25 | are rarely an issue, making coroutine programming much safer than |
25 | are rarely an issue, making coroutine programming much safer than |
26 | threads programming. |
26 | threads programming. |
27 | |
27 | |
… | |
… | |
41 | $current (or as function: current) |
41 | $current (or as function: current) |
42 | The current coroutine (the last coroutine switched to). The initial |
42 | The current coroutine (the last coroutine switched to). The initial |
43 | value is $main (of course). |
43 | value is $main (of course). |
44 | |
44 | |
45 | This variable is strictly *read-only*. It is provided for |
45 | This variable is strictly *read-only*. It is provided for |
46 | performance reasons. If performance is not essentiel you are |
46 | performance reasons. If performance is not essential you are |
47 | encouraged to use the "Coro::current" function instead. |
47 | encouraged to use the "Coro::current" function instead. |
48 | |
48 | |
49 | $idle |
49 | $idle |
50 | A callback that is called whenever the scheduler finds no ready |
50 | A callback that is called whenever the scheduler finds no ready |
51 | coroutines to run. The default implementation prints "FATAL: |
51 | coroutines to run. The default implementation prints "FATAL: |
… | |
… | |
118 | # wake up sleeping coroutine |
118 | # wake up sleeping coroutine |
119 | $current->ready; |
119 | $current->ready; |
120 | undef $current; |
120 | undef $current; |
121 | }; |
121 | }; |
122 | |
122 | |
123 | # call schedule until event occured. |
123 | # call schedule until event occurred. |
124 | # in case we are woken up for other reasons |
124 | # in case we are woken up for other reasons |
125 | # (current still defined), loop. |
125 | # (current still defined), loop. |
126 | Coro::schedule while $current; |
126 | Coro::schedule while $current; |
127 | } |
127 | } |
128 | |
128 | |
… | |
… | |
212 | with a coroutine. |
212 | with a coroutine. |
213 | |
213 | |
214 | GLOBAL FUNCTIONS |
214 | GLOBAL FUNCTIONS |
215 | Coro::nready |
215 | Coro::nready |
216 | Returns the number of coroutines that are currently in the ready |
216 | Returns the number of coroutines that are currently in the ready |
217 | state, i.e. that can be swicthed to. The value 0 means that the only |
217 | state, i.e. that can be switched to. The value 0 means that the only |
218 | runnable coroutine is the currently running one, so "cede" would |
218 | runnable coroutine is the currently running one, so "cede" would |
219 | have no effect, and "schedule" would cause a deadlock unless there |
219 | have no effect, and "schedule" would cause a deadlock unless there |
220 | is an idle handler that wakes up some coroutines. |
220 | is an idle handler that wakes up some coroutines. |
221 | |
221 | |
222 | my $guard = Coro::guard { ... } |
222 | my $guard = Coro::guard { ... } |
… | |
… | |
243 | it, returning the new coderef. This means that the new coderef will |
243 | it, returning the new coderef. This means that the new coderef will |
244 | return immediately without blocking, returning nothing, while the |
244 | return immediately without blocking, returning nothing, while the |
245 | original code ref will be called (with parameters) from within its |
245 | original code ref will be called (with parameters) from within its |
246 | own coroutine. |
246 | own coroutine. |
247 | |
247 | |
248 | The reason this fucntion exists is that many event libraries (such |
248 | The reason this function exists is that many event libraries (such |
249 | as the venerable Event module) are not coroutine-safe (a weaker form |
249 | as the venerable Event module) are not coroutine-safe (a weaker form |
250 | of thread-safety). This means you must not block within event |
250 | of thread-safety). This means you must not block within event |
251 | callbacks, otherwise you might suffer from crashes or worse. |
251 | callbacks, otherwise you might suffer from crashes or worse. |
252 | |
252 | |
253 | This function allows your callbacks to block by executing them in |
253 | This function allows your callbacks to block by executing them in |
… | |
… | |
261 | BUGS/LIMITATIONS |
261 | BUGS/LIMITATIONS |
262 | - you must make very sure that no coro is still active on global |
262 | - you must make very sure that no coro is still active on global |
263 | destruction. very bad things might happen otherwise (usually segfaults). |
263 | destruction. very bad things might happen otherwise (usually segfaults). |
264 | |
264 | |
265 | - this module is not thread-safe. You should only ever use this module |
265 | - this module is not thread-safe. You should only ever use this module |
266 | from the same thread (this requirement might be losened in the future |
266 | from the same thread (this requirement might be loosened in the future |
267 | to allow per-thread schedulers, but Coro::State does not yet allow |
267 | to allow per-thread schedulers, but Coro::State does not yet allow |
268 | this). |
268 | this). |
269 | |
269 | |
270 | SEE ALSO |
270 | SEE ALSO |
271 | Support/Utility: Coro::Cont, Coro::Specific, Coro::State, Coro::Util. |
271 | Support/Utility: Coro::Cont, Coro::Specific, Coro::State, Coro::Util. |