… | |
… | |
72 | |
72 | |
73 | =over 4 |
73 | =over 4 |
74 | |
74 | |
75 | =item 1. Creation |
75 | =item 1. Creation |
76 | |
76 | |
77 | The first thing in the life of a coro thread is it's creation - |
77 | The first thing in the life of a coro thread is its creation - |
78 | obviously. The typical way to create a thread is to call the C<async |
78 | obviously. The typical way to create a thread is to call the C<async |
79 | BLOCK> function: |
79 | BLOCK> function: |
80 | |
80 | |
81 | async { |
81 | async { |
82 | # thread code goes here |
82 | # thread code goes here |
… | |
… | |
91 | This creates a new coro thread and puts it into the ready queue, meaning |
91 | This creates a new coro thread and puts it into the ready queue, meaning |
92 | it will run as soon as the CPU is free for it. |
92 | it will run as soon as the CPU is free for it. |
93 | |
93 | |
94 | C<async> will return a Coro object - you can store this for future |
94 | C<async> will return a Coro object - you can store this for future |
95 | reference or ignore it - a thread that is running, ready to run or waiting |
95 | reference or ignore it - a thread that is running, ready to run or waiting |
96 | for some event is alive on it's own. |
96 | for some event is alive on its own. |
97 | |
97 | |
98 | Another way to create a thread is to call the C<new> constructor with a |
98 | Another way to create a thread is to call the C<new> constructor with a |
99 | code-reference: |
99 | code-reference: |
100 | |
100 | |
101 | new Coro sub { |
101 | new Coro sub { |
… | |
… | |
248 | implements an endless loop, the C<$guard> will not be cleaned up. However, |
248 | implements an endless loop, the C<$guard> will not be cleaned up. However, |
249 | since the thread object returned by C<async> is not stored anywhere, the |
249 | since the thread object returned by C<async> is not stored anywhere, the |
250 | thread is initially referenced because it is in the ready queue, when it |
250 | thread is initially referenced because it is in the ready queue, when it |
251 | runs it is referenced by C<$Coro::current>, but when it calls C<schedule>, |
251 | runs it is referenced by C<$Coro::current>, but when it calls C<schedule>, |
252 | it gets C<cancel>ed causing the guard object to be destroyed (see the next |
252 | it gets C<cancel>ed causing the guard object to be destroyed (see the next |
253 | section), and printing it's message. |
253 | section), and printing its message. |
254 | |
254 | |
255 | If this seems a bit drastic, remember that this only happens when nothing |
255 | If this seems a bit drastic, remember that this only happens when nothing |
256 | references the thread anymore, which means there is no way to further |
256 | references the thread anymore, which means there is no way to further |
257 | execute it, ever. The only options at this point are leaking the thread, |
257 | execute it, ever. The only options at this point are leaking the thread, |
258 | or cleaning it up, which brings us to... |
258 | or cleaning it up, which brings us to... |
… | |
… | |
261 | |
261 | |
262 | Threads will allocate various resources. Most but not all will be returned |
262 | Threads will allocate various resources. Most but not all will be returned |
263 | when a thread terminates, during clean-up. |
263 | when a thread terminates, during clean-up. |
264 | |
264 | |
265 | Cleanup is quite similar to throwing an uncaught exception: perl will |
265 | Cleanup is quite similar to throwing an uncaught exception: perl will |
266 | work it's way up through all subroutine calls and blocks. On it's way, it |
266 | work its way up through all subroutine calls and blocks. On its way, it |
267 | will release all C<my> variables, undo all C<local>'s and free any other |
267 | will release all C<my> variables, undo all C<local>'s and free any other |
268 | resources truly local to the thread. |
268 | resources truly local to the thread. |
269 | |
269 | |
270 | So, a common way to free resources is to keep them referenced only by my |
270 | So, a common way to free resources is to keep them referenced only by my |
271 | variables: |
271 | variables: |
… | |
… | |
293 | code blocks): |
293 | code blocks): |
294 | |
294 | |
295 | async { |
295 | async { |
296 | my $window = new Gtk2::Window "toplevel"; |
296 | my $window = new Gtk2::Window "toplevel"; |
297 | # The window will not be cleaned up automatically, even when $window |
297 | # The window will not be cleaned up automatically, even when $window |
298 | # gets freed, so use a guard to ensure it's destruction |
298 | # gets freed, so use a guard to ensure its destruction |
299 | # in case of an error: |
299 | # in case of an error: |
300 | my $window_guard = Guard::guard { $window->destroy }; |
300 | my $window_guard = Guard::guard { $window->destroy }; |
301 | |
301 | |
302 | # we are safe here |
302 | # we are safe here |
303 | }; |
303 | }; |
… | |
… | |
366 | |
366 | |
367 | our $idle; # idle handler |
367 | our $idle; # idle handler |
368 | our $main; # main coro |
368 | our $main; # main coro |
369 | our $current; # current coro |
369 | our $current; # current coro |
370 | |
370 | |
371 | our $VERSION = 6.513; |
371 | our $VERSION = 6.55; |
372 | |
372 | |
373 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); |
373 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); |
374 | our %EXPORT_TAGS = ( |
374 | our %EXPORT_TAGS = ( |
375 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
375 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
376 | ); |
376 | ); |
… | |
… | |
759 | automatically get assigned a perl interpreter when they are transferred to. |
759 | automatically get assigned a perl interpreter when they are transferred to. |
760 | |
760 | |
761 | =item $state->is_zombie |
761 | =item $state->is_zombie |
762 | |
762 | |
763 | Returns true iff the Coro object has been cancelled, i.e. |
763 | Returns true iff the Coro object has been cancelled, i.e. |
764 | it's resources freed because they were C<cancel>'ed, C<terminate>'d, |
764 | its resources freed because they were C<cancel>'ed, C<terminate>'d, |
765 | C<safe_cancel>'ed or simply went out of scope. |
765 | C<safe_cancel>'ed or simply went out of scope. |
766 | |
766 | |
767 | The name "zombie" stems from UNIX culture, where a process that has |
767 | The name "zombie" stems from UNIX culture, where a process that has |
768 | exited and only stores and exit status and no other resources is called a |
768 | exited and only stores and exit status and no other resources is called a |
769 | "zombie". |
769 | "zombie". |
… | |
… | |
782 | =item $is_suspended = $coro->is_suspended |
782 | =item $is_suspended = $coro->is_suspended |
783 | |
783 | |
784 | Returns true iff this Coro object has been suspended. Suspended Coros will |
784 | Returns true iff this Coro object has been suspended. Suspended Coros will |
785 | not ever be scheduled. |
785 | not ever be scheduled. |
786 | |
786 | |
787 | =item $coro->cancel (arg...) |
787 | =item $coro->cancel ($arg...) |
788 | |
788 | |
789 | Terminates the given Coro thread and makes it return the given arguments as |
789 | Terminate the given Coro thread and make it return the given arguments as |
790 | status (default: an empty list). Never returns if the Coro is the |
790 | status (default: an empty list). Never returns if the Coro is the |
791 | current Coro. |
791 | current Coro. |
792 | |
792 | |
793 | This is a rather brutal way to free a coro, with some limitations - if |
793 | This is a rather brutal way to free a coro, with some limitations - if |
794 | the thread is inside a C callback that doesn't expect to be canceled, |
794 | the thread is inside a C callback that doesn't expect to be canceled, |
… | |
… | |
830 | context and can block if they wish. The downside is that there is no |
830 | context and can block if they wish. The downside is that there is no |
831 | guarantee that the thread can be cancelled when you call this method, and |
831 | guarantee that the thread can be cancelled when you call this method, and |
832 | therefore, it might fail. It is also considerably slower than C<cancel> or |
832 | therefore, it might fail. It is also considerably slower than C<cancel> or |
833 | C<terminate>. |
833 | C<terminate>. |
834 | |
834 | |
835 | A thread is in a safe-cancellable state if it either hasn't been run yet, |
835 | A thread is in a safe-cancellable state if it either has never been run |
|
|
836 | yet, has already been canceled/terminated or otherwise destroyed, or has |
836 | or it has no C context attached and is inside an SLF function. |
837 | no C context attached and is inside an SLF function. |
837 | |
838 | |
|
|
839 | The first two states are trivial - a thread that hasnot started or has |
|
|
840 | already finished is safe to cancel. |
|
|
841 | |
838 | The latter two basically mean that the thread isn't currently inside a |
842 | The last state basically means that the thread isn't currently inside a |
839 | perl callback called from some C function (usually via some XS modules) |
843 | perl callback called from some C function (usually via some XS modules) |
840 | and isn't currently executing inside some C function itself (via Coro's XS |
844 | and isn't currently executing inside some C function itself (via Coro's XS |
841 | API). |
845 | API). |
842 | |
846 | |
843 | This call returns true when it could cancel the thread, or croaks with an |
847 | This call returns true when it could cancel the thread, or croaks with an |
… | |
… | |
915 | return once the C<$coro> terminates. |
919 | return once the C<$coro> terminates. |
916 | |
920 | |
917 | =item $coro->on_destroy (\&cb) |
921 | =item $coro->on_destroy (\&cb) |
918 | |
922 | |
919 | Registers a callback that is called when this coro thread gets destroyed, |
923 | Registers a callback that is called when this coro thread gets destroyed, |
920 | that is, after it's resources have been freed but before it is joined. The |
924 | that is, after its resources have been freed but before it is joined. The |
921 | callback gets passed the terminate/cancel arguments, if any, and I<must |
925 | callback gets passed the terminate/cancel arguments, if any, and I<must |
922 | not> die, under any circumstances. |
926 | not> die, under any circumstances. |
923 | |
927 | |
924 | There can be any number of C<on_destroy> callbacks per coro, and there is |
928 | There can be any number of C<on_destroy> callbacks per coro, and there is |
925 | currently no way to remove a callback once added. |
929 | currently no way to remove a callback once added. |
… | |
… | |
1081 | |
1085 | |
1082 | Create and return a "rouse callback". That's a code reference that, |
1086 | Create and return a "rouse callback". That's a code reference that, |
1083 | when called, will remember a copy of its arguments and notify the owner |
1087 | when called, will remember a copy of its arguments and notify the owner |
1084 | coro of the callback. |
1088 | coro of the callback. |
1085 | |
1089 | |
|
|
1090 | When the callback is invoked multiple times, only the arguments passed on |
|
|
1091 | the first call will be stored. |
|
|
1092 | |
1086 | See the next function. |
1093 | Also see the next function. |
1087 | |
1094 | |
1088 | =item @args = rouse_wait [$cb] |
1095 | =item @args = rouse_wait [$cb] |
1089 | |
1096 | |
1090 | Wait for the specified rouse callback (or the last one that was created in |
1097 | Wait for the specified rouse callback (or the last one that was created in |
1091 | this coro). |
1098 | this coro). |
… | |
… | |
1110 | |
1117 | |
1111 | # some modules have their new predefined in State.xs, some don't |
1118 | # some modules have their new predefined in State.xs, some don't |
1112 | *{"Coro::$module\::new"} = $old |
1119 | *{"Coro::$module\::new"} = $old |
1113 | if $old; |
1120 | if $old; |
1114 | |
1121 | |
1115 | goto &{"Coro::$module\::new"}; |
1122 | goto &{"Coro::$module\::new"} |
1116 | }; |
1123 | }; |
1117 | } |
1124 | } |
1118 | |
1125 | |
1119 | 1; |
1126 | 1; |
1120 | |
1127 | |