… | |
… | |
21 | $main->transfer ($new); |
21 | $main->transfer ($new); |
22 | print "back in main\n"; |
22 | print "back in main\n"; |
23 | |
23 | |
24 | =head1 DESCRIPTION |
24 | =head1 DESCRIPTION |
25 | |
25 | |
26 | This module implements coro. Coros, similar to threads and continuations, |
26 | This module implements coro objects. Coros, similar to threads and |
27 | allow you to run more than one "thread of execution" in parallel. Unlike |
27 | continuations, allow you to run more than one "thread of execution" in |
28 | so-called "kernel" threads, there is no parallelism and only voluntary |
28 | parallel. Unlike so-called "kernel" threads, there is no parallelism |
29 | switching is used so locking problems are greatly reduced. The latter is |
29 | and only voluntary switching is used so locking problems are greatly |
30 | called "cooperative" threading as opposed to "preemptive" threading. |
30 | reduced. The latter is called "cooperative" threading as opposed to |
|
|
31 | "preemptive" threading. |
31 | |
32 | |
32 | This can be used to implement non-local jumps, exception handling, |
33 | This can be used to implement non-local jumps, exception handling, |
33 | continuation objects and more. |
34 | continuation objects and more. |
34 | |
35 | |
35 | This module provides only low-level functionality. See L<Coro> and related |
36 | This module provides only low-level functionality useful to build other |
|
|
37 | abstractions, such as threads, generators or coroutines. See L<Coro> |
36 | modules for a higher level threads abstraction including a scheduler. |
38 | and related modules for a higher level threads abstraction including a |
|
|
39 | scheduler. |
37 | |
40 | |
38 | =head2 MODEL |
41 | =head2 MODEL |
39 | |
42 | |
40 | Coro::State implements two different thread models: Perl and C. The C |
43 | Coro::State implements two different thread models: Perl and C. The C |
41 | threads (called cctx's) are basically simplified perl interpreters |
44 | threads (called cctx's) are basically simplified perl interpreters |
… | |
… | |
137 | |
140 | |
138 | Similar to above die hook, but augments C<$SIG{__WARN__}>. |
141 | Similar to above die hook, but augments C<$SIG{__WARN__}>. |
139 | |
142 | |
140 | =back |
143 | =back |
141 | |
144 | |
142 | =head2 FUNCTIONS |
145 | =head2 Coro::State METHODS |
143 | |
146 | |
144 | =over 4 |
147 | =over 4 |
145 | |
148 | |
146 | =item $coro = new Coro::State [$coderef[, @args...]] |
149 | =item $coro = new Coro::State [$coderef[, @args...]] |
147 | |
150 | |
… | |
… | |
229 | |
232 | |
230 | =item $state->is_new |
233 | =item $state->is_new |
231 | |
234 | |
232 | =item $state->is_zombie |
235 | =item $state->is_zombie |
233 | |
236 | |
234 | See the corresponding method for L<Coro> objects. |
237 | See the corresponding method(s) for L<Coro> objects. |
235 | |
238 | |
236 | =item $state->cancel |
239 | =item $state->cancel |
237 | |
240 | |
238 | Forcefully destructs the given Coro::State. While you can keep the |
241 | Forcefully destructs the given Coro::State. While you can keep the |
239 | reference, and some memory is still allocated, the Coro::State object is |
242 | reference, and some memory is still allocated, the Coro::State object is |
… | |
… | |
303 | You can also swap hashes and other values: |
306 | You can also swap hashes and other values: |
304 | |
307 | |
305 | my %private_hash; |
308 | my %private_hash; |
306 | $coro->swap_sv (\%some_hash, \%private_hash); |
309 | $coro->swap_sv (\%some_hash, \%private_hash); |
307 | |
310 | |
308 | =item $state->trace ($flags) |
|
|
309 | |
|
|
310 | Internal function to control tracing. I just mention this so you can stay |
|
|
311 | away from abusing it. |
|
|
312 | |
|
|
313 | =item $bytes = $state->rss |
311 | =item $bytes = $state->rss |
314 | |
312 | |
315 | Returns the memory allocated by the coro (which includes static |
313 | Returns the memory allocated by the coro (which includes static |
316 | structures, various perl stacks but NOT local variables, arguments or any |
314 | structures, various perl stacks but NOT local variables, arguments or any |
317 | C context data). This is a rough indication of how much memory it might |
315 | C context data). This is a rough indication of how much memory it might |
318 | use. |
316 | use. |
319 | |
317 | |
|
|
318 | =item ($real, $cpu) = $state->times |
|
|
319 | |
|
|
320 | Returns the real time and cpu times spent in the given C<$state>. See |
|
|
321 | C<Coro::State::enable_times> for more info. |
|
|
322 | |
|
|
323 | =item $state->trace ($flags) |
|
|
324 | |
|
|
325 | Internal function to control tracing. I just mention this so you can stay |
|
|
326 | away from abusing it. |
|
|
327 | |
|
|
328 | =back |
|
|
329 | |
|
|
330 | =head3 METHODS FOR C CONTEXTS |
|
|
331 | |
|
|
332 | Most coros only consist of some Perl data structures - transfering to a |
|
|
333 | coro just reconfigures the interpreter to continue somewhere else. |
|
|
334 | |
|
|
335 | However. this is not always possible: For example, when Perl calls a C/XS function |
|
|
336 | (such as an event loop), and C then invokes a Perl callback, reconfiguring |
|
|
337 | the interpreter is not enough. Coro::State detects these cases automatically, and |
|
|
338 | attaches a C-level thread to each such Coro::State object, for as long as necessary. |
|
|
339 | |
|
|
340 | The C-level thread structure is called "C context" (or cctxt for short), |
|
|
341 | and can be quite big, which is why Coro::State only creates them as needed |
|
|
342 | and can run many Coro::State's on a single cctxt. |
|
|
343 | |
|
|
344 | This is mostly transparent, so the following methods are rarely needed. |
|
|
345 | |
|
|
346 | =over 4 |
|
|
347 | |
320 | =item $state->has_cctx |
348 | =item $state->has_cctx |
321 | |
349 | |
322 | Returns whether the state currently uses a cctx/C context. An active |
350 | Returns whether the state currently uses a cctx/C context. An active |
323 | state always has a cctx, as well as the main program. Other states only |
351 | state always has a cctx, as well as the main program. Other states only |
324 | use a cctxts when needed. |
352 | use a cctxts when needed. |
325 | |
353 | |
326 | =item Coro::State::force_cctx |
354 | =item Coro::State::force_cctx |
327 | |
355 | |
328 | Forces the allocation of a C context for the currently running coro |
356 | Forces the allocation of a private cctxt for the currently executing |
329 | (if not already done). Apart from benchmarking there is little point |
357 | Coro::State even though it would not normally ned one. Apart from |
330 | in doing so, however. |
358 | benchmarking or testing Coro itself, there is little point in doing so, |
|
|
359 | however. |
331 | |
360 | |
332 | =item $ncctx = Coro::State::cctx_count |
361 | =item $ncctx = Coro::State::cctx_count |
333 | |
362 | |
334 | Returns the number of C-level thread contexts allocated. If this number is |
363 | Returns the number of C contexts allocated. If this number is very high |
335 | very high (more than a dozen) it might be beneficial to identify points of |
364 | (more than a dozen) it might be beneficial to identify points of C-level |
336 | C-level recursion (perl calls C/XS, which calls perl again which switches |
365 | recursion (Perl calls C/XS, which calls Perl again which switches coros |
337 | coros - this forces an allocation of a C-level thread context) in your |
366 | - this forces an allocation of a C context) in your code and moving this |
338 | code and moving this into a separate coro. |
367 | into a separate coro. |
339 | |
368 | |
340 | =item $nidle = Coro::State::cctx_idle |
369 | =item $nidle = Coro::State::cctx_idle |
341 | |
370 | |
342 | Returns the number of allocated but idle (currently unused and free for |
371 | Returns the number of allocated but idle (currently unused and free for |
343 | reuse) C level thread contexts. |
372 | reuse) C contexts. |
344 | |
373 | |
345 | =item $old = Coro::State::cctx_max_idle [$new_count] |
374 | =item $old = Coro::State::cctx_max_idle [$new_count] |
346 | |
375 | |
347 | Coro caches C contexts that are not in use currently, as creating them |
376 | Coro caches C contexts that are not in use currently, as creating them |
348 | from scratch has some overhead. |
377 | from scratch has some overhead. |
… | |
… | |
352 | default being C<4>. |
381 | default being C<4>. |
353 | |
382 | |
354 | =item $old = Coro::State::cctx_stacksize [$new_stacksize] |
383 | =item $old = Coro::State::cctx_stacksize [$new_stacksize] |
355 | |
384 | |
356 | Returns the current C stack size and optionally sets the new I<minimum> |
385 | Returns the current C stack size and optionally sets the new I<minimum> |
357 | stack size to C<$new_stacksize> I<long>s. Existing stacks will not |
386 | stack size to C<$new_stacksize> I<pointers>s. Existing stacks will not |
358 | be changed, but Coro will try to replace smaller stacks as soon as |
387 | be changed, but Coro will try to replace smaller stacks as soon as |
359 | possible. Any Coro::State that starts to use a stack after this call is |
388 | possible. Any Coro::State that starts to use a stack after this call is |
360 | guaranteed this minimum stack size. |
389 | guaranteed this minimum stack size. |
361 | |
390 | |
362 | Please note that coros will only need to use a C-level stack if the |
391 | Please note that coros will only need to use a C-level stack if the |
363 | interpreter recurses or calls a function in a module that calls back into |
392 | interpreter recurses or calls a function in a module that calls back into |
364 | the interpreter, so use of this feature is usually never needed. |
393 | the interpreter, so use of this feature is usually never needed. |
365 | |
394 | |
|
|
395 | =back |
|
|
396 | |
|
|
397 | =head2 FUNCTIONS |
|
|
398 | |
|
|
399 | =over 4 |
|
|
400 | |
366 | =item @states = Coro::State::list |
401 | =item @states = Coro::State::list |
367 | |
402 | |
368 | Returns a list of all states currently allocated. |
403 | Returns a list of all Coro::State objects currently allocated. This |
|
|
404 | includes all derived objects (such as L<Coro> threads). |
369 | |
405 | |
370 | =item $was_enabled = Coro::State::enable_times [$enable] |
406 | =item $was_enabled = Coro::State::enable_times [$enable] |
371 | |
407 | |
372 | Enables/disables/queries the current state of per-thread real and |
408 | Enables/disables/queries the current state of per-thread real and |
373 | cpu-time gathering. |
409 | cpu-time gathering. |
… | |
… | |
381 | |
417 | |
382 | Enabling time profiling slows down thread switching by a factor of 2 to |
418 | Enabling time profiling slows down thread switching by a factor of 2 to |
383 | 10, depending on platform on hardware. |
419 | 10, depending on platform on hardware. |
384 | |
420 | |
385 | The times will be displayed when running C<Coro::Debug::command "ps">, and |
421 | The times will be displayed when running C<Coro::Debug::command "ps">, and |
386 | cna be queried by calling C<< $state->times >>. |
422 | can be queried by calling C<< $state->times >>. |
387 | |
423 | |
388 | =item ($real, $cpu) = $state->times |
424 | =back |
389 | |
425 | |
390 | Returns the real time and cpu times spent in the given C<$state>. See |
426 | =head3 CLONING |
391 | C<Coro::State::enable_times> for more info. |
427 | |
|
|
428 | =over 4 |
392 | |
429 | |
393 | =item $clone = $state->clone |
430 | =item $clone = $state->clone |
394 | |
431 | |
395 | This exciting method takes a Coro::State object and clones it, i.e., it |
432 | This exciting method takes a Coro::State object and clones it, i.e., it |
396 | creates a copy. This makes it possible to restore a state more than once, |
433 | creates a copy. This makes it possible to restore a state more than once, |