Skip to content

Commit 5490280

Browse files
committed
Introduce preemptable coroutines
- Includes an alternate PL_runops that will cede automatically - $coro->preempt and $coro->no_preempt can be used to control preemption at any time. - 3 different strategies for timesharing (background timer posix thread, opcode counting, and opcode counting + a call to nvtime())
1 parent 9cf27d1 commit 5490280

File tree

3 files changed

+473
-17
lines changed

3 files changed

+473
-17
lines changed

Coro.pm

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,28 @@ coro). This is a bug that will be fixed in some future version.
628628
Similar to C<prio>, but subtract the given value from the priority (i.e.
629629
higher values mean lower priority, just as in unix).
630630
631+
=item $coro->preempt
632+
633+
Enable automatic cedeing for a coroutine.
634+
635+
Preempting allows CPU bound code which does not call cede to still be used
636+
without starving the other coroutines:
637+
638+
my $coro = async { factorial(5000) };
639+
$coro->preempt;
640+
cede;
641+
642+
This cannot be enabled on the main coroutine because runops is already recursed
643+
into.
644+
645+
=item $coro->no_preempt
646+
647+
Disable preemption. Can be used to protect critical sections:
648+
649+
current->no_preempt;
650+
... will not cede automatically ...
651+
current->preempt;
652+
631653
=item $olddesc = $coro->desc ($newdesc)
632654
633655
Sets (or gets in case the argument is missing) the description for this

0 commit comments

Comments
 (0)