… | |
… | |
411 | "terminate" or "cancel" functions. "join" can be called concurrently |
411 | "terminate" or "cancel" functions. "join" can be called concurrently |
412 | from multiple coro, and all will be resumed and given the status |
412 | from multiple coro, and all will be resumed and given the status |
413 | return once the $coro terminates. |
413 | return once the $coro terminates. |
414 | |
414 | |
415 | $coro->on_destroy (\&cb) |
415 | $coro->on_destroy (\&cb) |
416 | Registers a callback that is called when this coro gets destroyed, |
416 | Registers a callback that is called when this coro thread gets |
417 | but before it is joined. The callback gets passed the terminate |
417 | destroyed, but before it is joined. The callback gets passed the |
418 | arguments, if any, and *must not* die, under any circumstances. |
418 | terminate arguments, if any, and *must not* die, under any |
|
|
419 | circumstances. |
|
|
420 | |
|
|
421 | There can be any number of "on_destroy" callbacks per coro. |
419 | |
422 | |
420 | $oldprio = $coro->prio ($newprio) |
423 | $oldprio = $coro->prio ($newprio) |
421 | Sets (or gets, if the argument is missing) the priority of the coro. |
424 | Sets (or gets, if the argument is missing) the priority of the coro |
422 | Higher priority coro get run before lower priority coro. Priorities |
425 | thread. Higher priority coro get run before lower priority coros. |
423 | are small signed integers (currently -4 .. +3), that you can refer |
426 | Priorities are small signed integers (currently -4 .. +3), that you |
424 | to using PRIO_xxx constants (use the import tag :prio to get then): |
427 | can refer to using PRIO_xxx constants (use the import tag :prio to |
|
|
428 | get then): |
425 | |
429 | |
426 | PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN |
430 | PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN |
427 | 3 > 1 > 0 > -1 > -3 > -4 |
431 | 3 > 1 > 0 > -1 > -3 > -4 |
428 | |
432 | |
429 | # set priority to HIGH |
433 | # set priority to HIGH |
430 | current->prio (PRIO_HIGH); |
434 | current->prio (PRIO_HIGH); |
431 | |
435 | |
432 | The idle coro ($Coro::idle) always has a lower priority than any |
436 | The idle coro thread ($Coro::idle) always has a lower priority than |
433 | existing coro. |
437 | any existing coro. |
434 | |
438 | |
435 | Changing the priority of the current coro will take effect |
439 | Changing the priority of the current coro will take effect |
436 | immediately, but changing the priority of coro in the ready queue |
440 | immediately, but changing the priority of a coro in the ready queue |
437 | (but not running) will only take effect after the next schedule (of |
441 | (but not running) will only take effect after the next schedule (of |
438 | that coro). This is a bug that will be fixed in some future version. |
442 | that coro). This is a bug that will be fixed in some future version. |
439 | |
443 | |
440 | $newprio = $coro->nice ($change) |
444 | $newprio = $coro->nice ($change) |
441 | Similar to "prio", but subtract the given value from the priority |
445 | Similar to "prio", but subtract the given value from the priority |
442 | (i.e. higher values mean lower priority, just as in unix). |
446 | (i.e. higher values mean lower priority, just as in UNIX's nice |
|
|
447 | command). |
443 | |
448 | |
444 | $olddesc = $coro->desc ($newdesc) |
449 | $olddesc = $coro->desc ($newdesc) |
445 | Sets (or gets in case the argument is missing) the description for |
450 | Sets (or gets in case the argument is missing) the description for |
446 | this coro. This is just a free-form string you can associate with a |
451 | this coro thread. This is just a free-form string you can associate |
447 | coro. |
452 | with a coro. |
448 | |
453 | |
449 | This method simply sets the "$coro->{desc}" member to the given |
454 | This method simply sets the "$coro->{desc}" member to the given |
450 | string. You can modify this member directly if you wish. |
455 | string. You can modify this member directly if you wish, and in |
|
|
456 | fact, this is often preferred to indicate major processing states |
|
|
457 | that cna then be seen for example in a Coro::Debug session: |
|
|
458 | |
|
|
459 | sub my_long_function { |
|
|
460 | local $Coro::current->{desc} = "now in my_long_function"; |
|
|
461 | ... |
|
|
462 | $Coro::current->{desc} = "my_long_function: phase 1"; |
|
|
463 | ... |
|
|
464 | $Coro::current->{desc} = "my_long_function: phase 2"; |
|
|
465 | ... |
|
|
466 | } |
451 | |
467 | |
452 | GLOBAL FUNCTIONS |
468 | GLOBAL FUNCTIONS |
453 | Coro::nready |
469 | Coro::nready |
454 | Returns the number of coro that are currently in the ready state, |
470 | Returns the number of coro that are currently in the ready state, |
455 | i.e. that can be switched to by calling "schedule" directory or |
471 | i.e. that can be switched to by calling "schedule" directory or |
… | |
… | |
472 | The reason this function exists is that many event libraries (such |
488 | The reason this function exists is that many event libraries (such |
473 | as the venerable Event module) are not thread-safe (a weaker form of |
489 | as the venerable Event module) are not thread-safe (a weaker form of |
474 | reentrancy). This means you must not block within event callbacks, |
490 | reentrancy). This means you must not block within event callbacks, |
475 | otherwise you might suffer from crashes or worse. The only event |
491 | otherwise you might suffer from crashes or worse. The only event |
476 | library currently known that is safe to use without "unblock_sub" is |
492 | library currently known that is safe to use without "unblock_sub" is |
477 | EV. |
493 | EV (but you might still run into deadlocks if all event loops are |
|
|
494 | blocked). |
|
|
495 | |
|
|
496 | Coro will try to catch you when you block in the event loop |
|
|
497 | ("FATAL:$Coro::IDLE blocked itself"), but this is just best effort |
|
|
498 | and only works when you do not run your own event loop. |
478 | |
499 | |
479 | This function allows your callbacks to block by executing them in |
500 | This function allows your callbacks to block by executing them in |
480 | another coro where it is safe to block. One example where blocking |
501 | another coro where it is safe to block. One example where blocking |
481 | is handy is when you use the Coro::AIO functions to save results to |
502 | is handy is when you use the Coro::AIO functions to save results to |
482 | disk, for example. |
503 | disk, for example. |
… | |
… | |
595 | unix roughly halves perl performance, even when not used. |
616 | unix roughly halves perl performance, even when not used. |
596 | |
617 | |
597 | coro switching is not signal safe |
618 | coro switching is not signal safe |
598 | You must not switch to another coro from within a signal handler |
619 | You must not switch to another coro from within a signal handler |
599 | (only relevant with %SIG - most event libraries provide safe |
620 | (only relevant with %SIG - most event libraries provide safe |
600 | signals). |
621 | signals), *unless* you are sure you are not interrupting a Coro |
|
|
622 | function. |
601 | |
623 | |
602 | That means you *MUST NOT* call any function that might "block" the |
624 | That means you *MUST NOT* call any function that might "block" the |
603 | current coro - "cede", "schedule" "Coro::Semaphore->down" or |
625 | current coro - "cede", "schedule" "Coro::Semaphore->down" or |
604 | anything that calls those. Everything else, including calling |
626 | anything that calls those. Everything else, including calling |
605 | "ready", works. |
627 | "ready", works. |
… | |
… | |
611 | ithreads (for example, that memory or files would be shared), showing |
633 | ithreads (for example, that memory or files would be shared), showing |
612 | his lack of understanding of this area - if it is hard to understand for |
634 | his lack of understanding of this area - if it is hard to understand for |
613 | Chip, it is probably not obvious to everybody). |
635 | Chip, it is probably not obvious to everybody). |
614 | |
636 | |
615 | What follows is an ultra-condensed version of my talk about threads in |
637 | What follows is an ultra-condensed version of my talk about threads in |
616 | scripting languages given onthe perl workshop 2009: |
638 | scripting languages given on the perl workshop 2009: |
617 | |
639 | |
618 | The so-called "ithreads" were originally implemented for two reasons: |
640 | The so-called "ithreads" were originally implemented for two reasons: |
619 | first, to (badly) emulate unix processes on native win32 perls, and |
641 | first, to (badly) emulate unix processes on native win32 perls, and |
620 | secondly, to replace the older, real thread model ("5.005-threads"). |
642 | secondly, to replace the older, real thread model ("5.005-threads"). |
621 | |
643 | |