ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
(Generate patch)

Comparing Coro/Coro.pm (file contents):
Revision 1.349 by root, Tue Aug 14 16:51:37 2018 UTC vs.
Revision 1.359 by root, Wed Jul 21 06:40:13 2021 UTC

72 72
73=over 4 73=over 4
74 74
75=item 1. Creation 75=item 1. Creation
76 76
77The first thing in the life of a coro thread is it's creation - 77The first thing in the life of a coro thread is its creation -
78obviously. The typical way to create a thread is to call the C<async 78obviously. The typical way to create a thread is to call the C<async
79BLOCK> function: 79BLOCK> function:
80 80
81 async { 81 async {
82 # thread code goes here 82 # thread code goes here
91This creates a new coro thread and puts it into the ready queue, meaning 91This creates a new coro thread and puts it into the ready queue, meaning
92it will run as soon as the CPU is free for it. 92it will run as soon as the CPU is free for it.
93 93
94C<async> will return a Coro object - you can store this for future 94C<async> will return a Coro object - you can store this for future
95reference or ignore it - a thread that is running, ready to run or waiting 95reference or ignore it - a thread that is running, ready to run or waiting
96for some event is alive on it's own. 96for some event is alive on its own.
97 97
98Another way to create a thread is to call the C<new> constructor with a 98Another way to create a thread is to call the C<new> constructor with a
99code-reference: 99code-reference:
100 100
101 new Coro sub { 101 new Coro sub {
188 188
189 my $hello_world = $coro->join; 189 my $hello_world = $coro->join;
190 190
191 print $hello_world; 191 print $hello_world;
192 192
193Another way to terminate is to call C<< Coro::terminate >>, which at any 193Another way to terminate is to call C<< Coro::terminate >>, the
194subroutine call nesting level: 194thread-equivalent of C<exit>, which works at any subroutine call nesting
195level:
195 196
196 async { 197 async {
197 Coro::terminate "return value 1", "return value 2"; 198 Coro::terminate "return value 1", "return value 2";
198 }; 199 };
199 200
248implements an endless loop, the C<$guard> will not be cleaned up. However, 249implements an endless loop, the C<$guard> will not be cleaned up. However,
249since the thread object returned by C<async> is not stored anywhere, the 250since the thread object returned by C<async> is not stored anywhere, the
250thread is initially referenced because it is in the ready queue, when it 251thread is initially referenced because it is in the ready queue, when it
251runs it is referenced by C<$Coro::current>, but when it calls C<schedule>, 252runs it is referenced by C<$Coro::current>, but when it calls C<schedule>,
252it gets C<cancel>ed causing the guard object to be destroyed (see the next 253it gets C<cancel>ed causing the guard object to be destroyed (see the next
253section), and printing it's message. 254section), and printing its message.
254 255
255If this seems a bit drastic, remember that this only happens when nothing 256If this seems a bit drastic, remember that this only happens when nothing
256references the thread anymore, which means there is no way to further 257references the thread anymore, which means there is no way to further
257execute it, ever. The only options at this point are leaking the thread, 258execute it, ever. The only options at this point are leaking the thread,
258or cleaning it up, which brings us to... 259or cleaning it up, which brings us to...
261 262
262Threads will allocate various resources. Most but not all will be returned 263Threads will allocate various resources. Most but not all will be returned
263when a thread terminates, during clean-up. 264when a thread terminates, during clean-up.
264 265
265Cleanup is quite similar to throwing an uncaught exception: perl will 266Cleanup is quite similar to throwing an uncaught exception: perl will
266work it's way up through all subroutine calls and blocks. On it's way, it 267work its way up through all subroutine calls and blocks. On its way, it
267will release all C<my> variables, undo all C<local>'s and free any other 268will release all C<my> variables, undo all C<local>'s and free any other
268resources truly local to the thread. 269resources truly local to the thread.
269 270
270So, a common way to free resources is to keep them referenced only by my 271So, a common way to free resources is to keep them referenced only by my
271variables: 272variables:
293code blocks): 294code blocks):
294 295
295 async { 296 async {
296 my $window = new Gtk2::Window "toplevel"; 297 my $window = new Gtk2::Window "toplevel";
297 # The window will not be cleaned up automatically, even when $window 298 # The window will not be cleaned up automatically, even when $window
298 # gets freed, so use a guard to ensure it's destruction 299 # gets freed, so use a guard to ensure its destruction
299 # in case of an error: 300 # in case of an error:
300 my $window_guard = Guard::guard { $window->destroy }; 301 my $window_guard = Guard::guard { $window->destroy };
301 302
302 # we are safe here 303 # we are safe here
303 }; 304 };
366 367
367our $idle; # idle handler 368our $idle; # idle handler
368our $main; # main coro 369our $main; # main coro
369our $current; # current coro 370our $current; # current coro
370 371
371our $VERSION = 6.52; 372our $VERSION = 6.57;
372 373
373our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); 374our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait);
374our %EXPORT_TAGS = ( 375our %EXPORT_TAGS = (
375 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 376 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
376); 377);
759automatically get assigned a perl interpreter when they are transferred to. 760automatically get assigned a perl interpreter when they are transferred to.
760 761
761=item $state->is_zombie 762=item $state->is_zombie
762 763
763Returns true iff the Coro object has been cancelled, i.e. 764Returns true iff the Coro object has been cancelled, i.e.
764it's resources freed because they were C<cancel>'ed, C<terminate>'d, 765its resources freed because they were C<cancel>'ed, C<terminate>'d,
765C<safe_cancel>'ed or simply went out of scope. 766C<safe_cancel>'ed or simply went out of scope.
766 767
767The name "zombie" stems from UNIX culture, where a process that has 768The name "zombie" stems from UNIX culture, where a process that has
768exited and only stores and exit status and no other resources is called a 769exited and only stores and exit status and no other resources is called a
769"zombie". 770"zombie".
834 835
835A thread is in a safe-cancellable state if it either has never been run 836A thread is in a safe-cancellable state if it either has never been run
836yet, has already been canceled/terminated or otherwise destroyed, or has 837yet, has already been canceled/terminated or otherwise destroyed, or has
837no C context attached and is inside an SLF function. 838no C context attached and is inside an SLF function.
838 839
839The first two states are trivial - a thread that hasnot started or has 840The first two states are trivial - a thread that has not started or has
840already finished is safe to cancel. 841already finished is safe to cancel.
841 842
842The last state basically means that the thread isn't currently inside a 843The last state basically means that the thread isn't currently inside a
843perl callback called from some C function (usually via some XS modules) 844perl callback called from some C function (usually via some XS modules)
844and isn't currently executing inside some C function itself (via Coro's XS 845and isn't currently executing inside some C function itself (via Coro's XS
919return once the C<$coro> terminates. 920return once the C<$coro> terminates.
920 921
921=item $coro->on_destroy (\&cb) 922=item $coro->on_destroy (\&cb)
922 923
923Registers a callback that is called when this coro thread gets destroyed, 924Registers a callback that is called when this coro thread gets destroyed,
924that is, after it's resources have been freed but before it is joined. The 925that is, after its resources have been freed but before it is joined. The
925callback gets passed the terminate/cancel arguments, if any, and I<must 926callback gets passed the terminate/cancel arguments, if any, and I<must
926not> die, under any circumstances. 927not> die, under any circumstances.
927 928
928There can be any number of C<on_destroy> callbacks per coro, and there is 929There can be any number of C<on_destroy> callbacks per coro, and there is
929currently no way to remove a callback once added. 930currently no way to remove a callback once added.
1085 1086
1086Create and return a "rouse callback". That's a code reference that, 1087Create and return a "rouse callback". That's a code reference that,
1087when called, will remember a copy of its arguments and notify the owner 1088when called, will remember a copy of its arguments and notify the owner
1088coro of the callback. 1089coro of the callback.
1089 1090
1091Only the first invocation will store agruments and signal any waiter -
1092further calls will effectively be ignored, but it is ok to try.
1093
1090See the next function. 1094Also see the next function.
1091 1095
1092=item @args = rouse_wait [$cb] 1096=item @args = rouse_wait [$cb]
1093 1097
1094Wait for the specified rouse callback (or the last one that was created in 1098Wait for the specified rouse callback to be invoked (or if the argument is
1095this coro). 1099missing, use the most recently created callback in the current coro).
1096 1100
1097As soon as the callback is invoked (or when the callback was invoked 1101As soon as the callback is invoked (or when the callback was invoked
1098before C<rouse_wait>), it will return the arguments originally passed to 1102before C<rouse_wait>), it will return the arguments originally passed to
1099the rouse callback. In scalar context, that means you get the I<last> 1103the rouse callback. In scalar context, that means you get the I<last>
1100argument, just as if C<rouse_wait> had a C<return ($a1, $a2, $a3...)> 1104argument, just as if C<rouse_wait> had a C<return ($a1, $a2, $a3...)>
1101statement at the end. 1105statement at the end.
1102 1106
1107You are only allowed to wait once for a given rouse callback.
1108
1103See the section B<HOW TO WAIT FOR A CALLBACK> for an actual usage example. 1109See the section B<HOW TO WAIT FOR A CALLBACK> for an actual usage example.
1110
1111As of Coro 6.57, you can reliably wait for a rouse callback in a different
1112thread than from where it was created.
1104 1113
1105=back 1114=back
1106 1115
1107=cut 1116=cut
1108 1117
1114 1123
1115 # some modules have their new predefined in State.xs, some don't 1124 # some modules have their new predefined in State.xs, some don't
1116 *{"Coro::$module\::new"} = $old 1125 *{"Coro::$module\::new"} = $old
1117 if $old; 1126 if $old;
1118 1127
1119 goto &{"Coro::$module\::new"}; 1128 goto &{"Coro::$module\::new"}
1120 }; 1129 };
1121} 1130}
1122 1131
11231; 11321;
1124 1133

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines