… | |
… | |
74 | |
74 | |
75 | our $idle; # idle handler |
75 | our $idle; # idle handler |
76 | our $main; # main coroutine |
76 | our $main; # main coroutine |
77 | our $current; # current coroutine |
77 | our $current; # current coroutine |
78 | |
78 | |
79 | our $VERSION = 5.1; |
79 | our $VERSION = 5.12; |
80 | |
80 | |
81 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
81 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); |
82 | our %EXPORT_TAGS = ( |
82 | our %EXPORT_TAGS = ( |
83 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
83 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
84 | ); |
84 | ); |
… | |
… | |
175 | |
175 | |
176 | =over 4 |
176 | =over 4 |
177 | |
177 | |
178 | =item async { ... } [@args...] |
178 | =item async { ... } [@args...] |
179 | |
179 | |
180 | Create a new coroutine and return it's coroutine object (usually |
180 | Create a new coroutine and return its coroutine object (usually |
181 | unused). The coroutine will be put into the ready queue, so |
181 | unused). The coroutine will be put into the ready queue, so |
182 | it will start running automatically on the next scheduler run. |
182 | it will start running automatically on the next scheduler run. |
183 | |
183 | |
184 | The first argument is a codeblock/closure that should be executed in the |
184 | The first argument is a codeblock/closure that should be executed in the |
185 | coroutine. When it returns argument returns the coroutine is automatically |
185 | coroutine. When it returns argument returns the coroutine is automatically |
… | |
… | |
352 | See C<async> and C<Coro::State::new> for additional info about the |
352 | See C<async> and C<Coro::State::new> for additional info about the |
353 | coroutine environment. |
353 | coroutine environment. |
354 | |
354 | |
355 | =cut |
355 | =cut |
356 | |
356 | |
357 | sub _terminate { |
357 | sub _coro_run { |
358 | terminate &{+shift}; |
358 | terminate &{+shift}; |
359 | } |
359 | } |
360 | |
360 | |
361 | =item $success = $coroutine->ready |
361 | =item $success = $coroutine->ready |
362 | |
362 | |
… | |
… | |
537 | would cause a deadlock unless there is an idle handler that wakes up some |
537 | would cause a deadlock unless there is an idle handler that wakes up some |
538 | coroutines. |
538 | coroutines. |
539 | |
539 | |
540 | =item my $guard = Coro::guard { ... } |
540 | =item my $guard = Coro::guard { ... } |
541 | |
541 | |
542 | This creates and returns a guard object. Nothing happens until the object |
542 | This function still exists, but is deprecated. Please use the |
543 | gets destroyed, in which case the codeblock given as argument will be |
543 | C<Guard::guard> function instead. |
544 | executed. This is useful to free locks or other resources in case of a |
|
|
545 | runtime error or when the coroutine gets canceled, as in both cases the |
|
|
546 | guard block will be executed. The guard object supports only one method, |
|
|
547 | C<< ->cancel >>, which will keep the codeblock from being executed. |
|
|
548 | |
544 | |
549 | Example: set some flag and clear it again when the coroutine gets canceled |
|
|
550 | or the function returns: |
|
|
551 | |
|
|
552 | sub do_something { |
|
|
553 | my $guard = Coro::guard { $busy = 0 }; |
|
|
554 | $busy = 1; |
|
|
555 | |
|
|
556 | # do something that requires $busy to be true |
|
|
557 | } |
|
|
558 | |
|
|
559 | =cut |
545 | =cut |
560 | |
546 | |
561 | sub guard(&) { |
547 | BEGIN { *guard = \&Guard::guard } |
562 | bless \(my $cb = $_[0]), "Coro::guard" |
|
|
563 | } |
|
|
564 | |
|
|
565 | sub Coro::guard::cancel { |
|
|
566 | ${$_[0]} = sub { }; |
|
|
567 | } |
|
|
568 | |
|
|
569 | sub Coro::guard::DESTROY { |
|
|
570 | ${$_[0]}->(); |
|
|
571 | } |
|
|
572 | |
|
|
573 | |
548 | |
574 | =item unblock_sub { ... } |
549 | =item unblock_sub { ... } |
575 | |
550 | |
576 | This utility function takes a BLOCK or code reference and "unblocks" it, |
551 | This utility function takes a BLOCK or code reference and "unblocks" it, |
577 | returning a new coderef. Unblocking means that calling the new coderef |
552 | returning a new coderef. Unblocking means that calling the new coderef |
… | |
… | |
681 | |
656 | |
682 | Coro offers two functions specifically designed to make this easy, |
657 | Coro offers two functions specifically designed to make this easy, |
683 | C<Coro::rouse_cb> and C<Coro::rouse_wait>. |
658 | C<Coro::rouse_cb> and C<Coro::rouse_wait>. |
684 | |
659 | |
685 | The first function, C<rouse_cb>, generates and returns a callback that, |
660 | The first function, C<rouse_cb>, generates and returns a callback that, |
686 | when invoked, will save it's arguments and notify the coroutine that |
661 | when invoked, will save its arguments and notify the coroutine that |
687 | created the callback. |
662 | created the callback. |
688 | |
663 | |
689 | The second function, C<rouse_wait>, waits for the callback to be called |
664 | The second function, C<rouse_wait>, waits for the callback to be called |
690 | (by calling C<schedule> to go to sleep) and returns the arguments |
665 | (by calling C<schedule> to go to sleep) and returns the arguments |
691 | originally passed to the callback. |
666 | originally passed to the callback. |