… | |
… | |
127 | call terminate or join on it (although you are allowed to), and you |
127 | call terminate or join on it (although you are allowed to), and you |
128 | get a coroutine that might have executed other code already (which |
128 | get a coroutine that might have executed other code already (which |
129 | can be good or bad :). |
129 | can be good or bad :). |
130 | |
130 | |
131 | On the plus side, this function is faster than creating (and |
131 | On the plus side, this function is faster than creating (and |
132 | destroying) a completely new coroutine, so if you need a lot of |
132 | destroying) a completly new coroutine, so if you need a lot of |
133 | generic coroutines in quick successsion, use "async_pool", not |
133 | generic coroutines in quick successsion, use "async_pool", not |
134 | "async". |
134 | "async". |
135 | |
135 | |
136 | The code block is executed in an "eval" context and a warning will |
136 | The code block is executed in an "eval" context and a warning will |
137 | be issued in case of an exception instead of terminating the |
137 | be issued in case of an exception instead of terminating the |
… | |
… | |
142 | |
142 | |
143 | The priority will be reset to 0 after each run, tracing will be |
143 | The priority will be reset to 0 after each run, tracing will be |
144 | disabled, the description will be reset and the default output |
144 | disabled, the description will be reset and the default output |
145 | filehandle gets restored, so you can change all these. Otherwise the |
145 | filehandle gets restored, so you can change all these. Otherwise the |
146 | coroutine will be re-used "as-is": most notably if you change other |
146 | coroutine will be re-used "as-is": most notably if you change other |
147 | per-coroutine global stuff such as $/ you *must needs* to revert |
147 | per-coroutine global stuff such as $/ you *must needs* revert that |
148 | that change, which is most simply done by using local as in: " local |
148 | change, which is most simply done by using local as in: "local $/". |
149 | $/ ". |
|
|
150 | |
149 | |
151 | The pool size is limited to 8 idle coroutines (this can be adjusted |
150 | The idle pool size is limited to 8 idle coroutines (this can be |
152 | by changing $Coro::POOL_SIZE), and there can be as many non-idle |
151 | adjusted by changing $Coro::POOL_SIZE), but there can be as many |
153 | coros as required. |
152 | non-idle coros as required. |
154 | |
153 | |
155 | If you are concerned about pooled coroutines growing a lot because a |
154 | If you are concerned about pooled coroutines growing a lot because a |
156 | single "async_pool" used a lot of stackspace you can e.g. |
155 | single "async_pool" used a lot of stackspace you can e.g. |
157 | "async_pool { terminate }" once per second or so to slowly replenish |
156 | "async_pool { terminate }" once per second or so to slowly replenish |
158 | the pool. In addition to that, when the stacks used by a handler |
157 | the pool. In addition to that, when the stacks used by a handler |
… | |
… | |
259 | $coroutine->cancel (arg...) |
258 | $coroutine->cancel (arg...) |
260 | Terminates the given coroutine and makes it return the given |
259 | Terminates the given coroutine and makes it return the given |
261 | arguments as status (default: the empty list). Never returns if the |
260 | arguments as status (default: the empty list). Never returns if the |
262 | coroutine is the current coroutine. |
261 | coroutine is the current coroutine. |
263 | |
262 | |
|
|
263 | $coroutine->throw ([$scalar]) |
|
|
264 | If $throw is specified and defined, it will be thrown as an |
|
|
265 | exception inside the coroutine at the next convenient point in time |
|
|
266 | (usually after it gains control at the next schedule/transfer/cede). |
|
|
267 | Otherwise clears the exception object. |
|
|
268 | |
|
|
269 | The exception object will be thrown "as is" with the specified |
|
|
270 | scalar in $@, i.e. if it is a string, no line number or newline will |
|
|
271 | be appended (unlike with "die"). |
|
|
272 | |
|
|
273 | This can be used as a softer means than "cancel" to ask a coroutine |
|
|
274 | to end itself, although there is no guarantee that the exception |
|
|
275 | will lead to termination, and if the exception isn't caught it might |
|
|
276 | well end the whole program. |
|
|
277 | |
|
|
278 | You might also think of "throw" as being the moral equivalent of |
|
|
279 | "kill"ing a coroutine with a signal (in this case, a scalar). |
|
|
280 | |
264 | $coroutine->join |
281 | $coroutine->join |
265 | Wait until the coroutine terminates and return any values given to |
282 | Wait until the coroutine terminates and return any values given to |
266 | the "terminate" or "cancel" functions. "join" can be called |
283 | the "terminate" or "cancel" functions. "join" can be called |
267 | concurrently from multiple coroutines, and all will be resumed and |
284 | concurrently from multiple coroutines, and all will be resumed and |
268 | given the status return once the $coroutine terminates. |
285 | given the status return once the $coroutine terminates. |
… | |
… | |
304 | this coroutine. This is just a free-form string you can associate |
321 | this coroutine. This is just a free-form string you can associate |
305 | with a coroutine. |
322 | with a coroutine. |
306 | |
323 | |
307 | This method simply sets the "$coroutine->{desc}" member to the given |
324 | This method simply sets the "$coroutine->{desc}" member to the given |
308 | string. You can modify this member directly if you wish. |
325 | string. You can modify this member directly if you wish. |
309 | |
|
|
310 | $coroutine->throw ([$scalar]) |
|
|
311 | If $throw is specified and defined, it will be thrown as an |
|
|
312 | exception inside the coroutine at the next convinient point in time |
|
|
313 | (usually after it gains control at the next schedule/transfer/cede). |
|
|
314 | Otherwise clears the exception object. |
|
|
315 | |
|
|
316 | The exception object will be thrown "as is" with the specified |
|
|
317 | scalar in $@, i.e. if it is a string, no line number or newline will |
|
|
318 | be appended (unlike with "die"). |
|
|
319 | |
|
|
320 | This can be used as a softer means than "cancel" to ask a coroutine |
|
|
321 | to end itself, although there is no guarentee that the exception |
|
|
322 | will lead to termination, and if the exception isn't caught it might |
|
|
323 | well end the whole program. |
|
|
324 | |
326 | |
325 | GLOBAL FUNCTIONS |
327 | GLOBAL FUNCTIONS |
326 | Coro::nready |
328 | Coro::nready |
327 | Returns the number of coroutines that are currently in the ready |
329 | Returns the number of coroutines that are currently in the ready |
328 | state, i.e. that can be switched to by calling "schedule" directory |
330 | state, i.e. that can be switched to by calling "schedule" directory |