… | |
… | |
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 { |
… | |
… | |
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 | |
193 | Another way to terminate is to call C<< Coro::terminate >>, which at any |
193 | Another way to terminate is to call C<< Coro::terminate >>, the |
194 | subroutine call nesting level: |
194 | thread-equivalent of C<exit>, which works at any subroutine call nesting |
|
|
195 | level: |
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 | |
… | |
… | |
248 | implements an endless loop, the C<$guard> will not be cleaned up. However, |
249 | 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 |
250 | 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 |
251 | 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>, |
252 | 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 |
253 | it gets C<cancel>ed causing the guard object to be destroyed (see the next |
253 | section), and printing it's message. |
254 | section), and printing its message. |
254 | |
255 | |
255 | If this seems a bit drastic, remember that this only happens when nothing |
256 | 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 |
257 | 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, |
258 | execute it, ever. The only options at this point are leaking the thread, |
258 | or cleaning it up, which brings us to... |
259 | or cleaning it up, which brings us to... |
… | |
… | |
261 | |
262 | |
262 | Threads will allocate various resources. Most but not all will be returned |
263 | Threads will allocate various resources. Most but not all will be returned |
263 | when a thread terminates, during clean-up. |
264 | when a thread terminates, during clean-up. |
264 | |
265 | |
265 | Cleanup is quite similar to throwing an uncaught exception: perl will |
266 | 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 |
267 | 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 |
268 | will release all C<my> variables, undo all C<local>'s and free any other |
268 | resources truly local to the thread. |
269 | resources truly local to the thread. |
269 | |
270 | |
270 | So, a common way to free resources is to keep them referenced only by my |
271 | So, a common way to free resources is to keep them referenced only by my |
271 | variables: |
272 | variables: |
… | |
… | |
293 | code blocks): |
294 | code 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 | |
367 | our $idle; # idle handler |
368 | our $idle; # idle handler |
368 | our $main; # main coro |
369 | our $main; # main coro |
369 | our $current; # current coro |
370 | our $current; # current coro |
370 | |
371 | |
371 | our $VERSION = 6.48; |
372 | our $VERSION = 6.57; |
372 | |
373 | |
373 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); |
374 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); |
374 | our %EXPORT_TAGS = ( |
375 | our %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 | ); |
… | |
… | |
498 | C<async> does. As the coro is being reused, stuff like C<on_destroy> |
499 | C<async> does. As the coro is being reused, stuff like C<on_destroy> |
499 | will not work in the expected way, unless you call terminate or cancel, |
500 | will not work in the expected way, unless you call terminate or cancel, |
500 | which somehow defeats the purpose of pooling (but is fine in the |
501 | which somehow defeats the purpose of pooling (but is fine in the |
501 | exceptional case). |
502 | exceptional case). |
502 | |
503 | |
503 | The priority will be reset to C<0> after each run, tracing will be |
504 | The priority will be reset to C<0> after each run, all C<swap_sv> calls |
504 | disabled, the description will be reset and the default output filehandle |
505 | will be undone, tracing will be disabled, the description will be reset |
505 | gets restored, so you can change all these. Otherwise the coro will |
506 | and the default output filehandle gets restored, so you can change all |
506 | be re-used "as-is": most notably if you change other per-coro global |
507 | these. Otherwise the coro will be re-used "as-is": most notably if you |
507 | stuff such as C<$/> you I<must needs> revert that change, which is most |
508 | change other per-coro global stuff such as C<$/> you I<must needs> revert |
508 | simply done by using local as in: C<< local $/ >>. |
509 | that change, which is most simply done by using local as in: C<< local $/ |
|
|
510 | >>. |
509 | |
511 | |
510 | The idle pool size is limited to C<8> idle coros (this can be |
512 | The idle pool size is limited to C<8> idle coros (this can be |
511 | adjusted by changing $Coro::POOL_SIZE), but there can be as many non-idle |
513 | adjusted by changing $Coro::POOL_SIZE), but there can be as many non-idle |
512 | coros as required. |
514 | coros as required. |
513 | |
515 | |
… | |
… | |
637 | # at this place, the timezone is Antarctica/South_Pole, |
639 | # at this place, the timezone is Antarctica/South_Pole, |
638 | # without disturbing the TZ of any other coro. |
640 | # without disturbing the TZ of any other coro. |
639 | }; |
641 | }; |
640 | |
642 | |
641 | This can be used to localise about any resource (locale, uid, current |
643 | This can be used to localise about any resource (locale, uid, current |
642 | working directory etc.) to a block, despite the existance of other |
644 | working directory etc.) to a block, despite the existence of other |
643 | coros. |
645 | coros. |
644 | |
646 | |
645 | Another interesting example implements time-sliced multitasking using |
647 | Another interesting example implements time-sliced multitasking using |
646 | interval timers (this could obviously be optimised, but does the job): |
648 | interval timers (this could obviously be optimised, but does the job): |
647 | |
649 | |
… | |
… | |
753 | =item $state->is_new |
755 | =item $state->is_new |
754 | |
756 | |
755 | Returns true iff this Coro object is "new", i.e. has never been run |
757 | Returns true iff this Coro object is "new", i.e. has never been run |
756 | yet. Those states basically consist of only the code reference to call and |
758 | yet. Those states basically consist of only the code reference to call and |
757 | the arguments, but consumes very little other resources. New states will |
759 | the arguments, but consumes very little other resources. New states will |
758 | automatically get assigned a perl interpreter when they are transfered to. |
760 | automatically get assigned a perl interpreter when they are transferred to. |
759 | |
761 | |
760 | =item $state->is_zombie |
762 | =item $state->is_zombie |
761 | |
763 | |
762 | Returns true iff the Coro object has been cancelled, i.e. |
764 | Returns true iff the Coro object has been cancelled, i.e. |
763 | it's resources freed because they were C<cancel>'ed, C<terminate>'d, |
765 | its resources freed because they were C<cancel>'ed, C<terminate>'d, |
764 | C<safe_cancel>'ed or simply went out of scope. |
766 | C<safe_cancel>'ed or simply went out of scope. |
765 | |
767 | |
766 | The name "zombie" stems from UNIX culture, where a process that has |
768 | The name "zombie" stems from UNIX culture, where a process that has |
767 | exited and only stores and exit status and no other resources is called a |
769 | exited and only stores and exit status and no other resources is called a |
768 | "zombie". |
770 | "zombie". |
… | |
… | |
781 | =item $is_suspended = $coro->is_suspended |
783 | =item $is_suspended = $coro->is_suspended |
782 | |
784 | |
783 | Returns true iff this Coro object has been suspended. Suspended Coros will |
785 | Returns true iff this Coro object has been suspended. Suspended Coros will |
784 | not ever be scheduled. |
786 | not ever be scheduled. |
785 | |
787 | |
786 | =item $coro->cancel (arg...) |
788 | =item $coro->cancel ($arg...) |
787 | |
789 | |
788 | Terminates the given Coro thread and makes it return the given arguments as |
790 | Terminate the given Coro thread and make it return the given arguments as |
789 | status (default: an empty list). Never returns if the Coro is the |
791 | status (default: an empty list). Never returns if the Coro is the |
790 | current Coro. |
792 | current Coro. |
791 | |
793 | |
792 | This is a rather brutal way to free a coro, with some limitations - if |
794 | This is a rather brutal way to free a coro, with some limitations - if |
793 | the thread is inside a C callback that doesn't expect to be canceled, |
795 | the thread is inside a C callback that doesn't expect to be canceled, |
… | |
… | |
829 | context and can block if they wish. The downside is that there is no |
831 | context and can block if they wish. The downside is that there is no |
830 | guarantee that the thread can be cancelled when you call this method, and |
832 | guarantee that the thread can be cancelled when you call this method, and |
831 | therefore, it might fail. It is also considerably slower than C<cancel> or |
833 | therefore, it might fail. It is also considerably slower than C<cancel> or |
832 | C<terminate>. |
834 | C<terminate>. |
833 | |
835 | |
834 | A thread is in a safe-cancellable state if it either hasn't been run yet, |
836 | A thread is in a safe-cancellable state if it either has never been run |
|
|
837 | yet, has already been canceled/terminated or otherwise destroyed, or has |
835 | or it has no C context attached and is inside an SLF function. |
838 | no C context attached and is inside an SLF function. |
836 | |
839 | |
|
|
840 | The first two states are trivial - a thread that has not started or has |
|
|
841 | already finished is safe to cancel. |
|
|
842 | |
837 | The latter two basically mean that the thread isn't currently inside a |
843 | The last state basically means that the thread isn't currently inside a |
838 | perl callback called from some C function (usually via some XS modules) |
844 | perl callback called from some C function (usually via some XS modules) |
839 | and isn't currently executing inside some C function itself (via Coro's XS |
845 | and isn't currently executing inside some C function itself (via Coro's XS |
840 | API). |
846 | API). |
841 | |
847 | |
842 | This call returns true when it could cancel the thread, or croaks with an |
848 | This call returns true when it could cancel the thread, or croaks with an |
… | |
… | |
914 | return once the C<$coro> terminates. |
920 | return once the C<$coro> terminates. |
915 | |
921 | |
916 | =item $coro->on_destroy (\&cb) |
922 | =item $coro->on_destroy (\&cb) |
917 | |
923 | |
918 | Registers a callback that is called when this coro thread gets destroyed, |
924 | Registers a callback that is called when this coro thread gets destroyed, |
919 | that is, after it's resources have been freed but before it is joined. The |
925 | that is, after its resources have been freed but before it is joined. The |
920 | callback gets passed the terminate/cancel arguments, if any, and I<must |
926 | callback gets passed the terminate/cancel arguments, if any, and I<must |
921 | not> die, under any circumstances. |
927 | not> die, under any circumstances. |
922 | |
928 | |
923 | There can be any number of C<on_destroy> callbacks per coro, and there is |
929 | There can be any number of C<on_destroy> callbacks per coro, and there is |
924 | currently no way to remove a callback once added. |
930 | currently no way to remove a callback once added. |
… | |
… | |
1080 | |
1086 | |
1081 | Create and return a "rouse callback". That's a code reference that, |
1087 | Create and return a "rouse callback". That's a code reference that, |
1082 | when called, will remember a copy of its arguments and notify the owner |
1088 | when called, will remember a copy of its arguments and notify the owner |
1083 | coro of the callback. |
1089 | coro of the callback. |
1084 | |
1090 | |
|
|
1091 | Only the first invocation will store agruments and signal any waiter - |
|
|
1092 | further calls will effectively be ignored, but it is ok to try. |
|
|
1093 | |
1085 | See the next function. |
1094 | Also see the next function. |
1086 | |
1095 | |
1087 | =item @args = rouse_wait [$cb] |
1096 | =item @args = rouse_wait [$cb] |
1088 | |
1097 | |
1089 | Wait for the specified rouse callback (or the last one that was created in |
1098 | Wait for the specified rouse callback to be invoked (or if the argument is |
1090 | this coro). |
1099 | missing, use the most recently created callback in the current coro). |
1091 | |
1100 | |
1092 | As soon as the callback is invoked (or when the callback was invoked |
1101 | As soon as the callback is invoked (or when the callback was invoked |
1093 | before C<rouse_wait>), it will return the arguments originally passed to |
1102 | before C<rouse_wait>), it will return the arguments originally passed to |
1094 | the rouse callback. In scalar context, that means you get the I<last> |
1103 | the rouse callback. In scalar context, that means you get the I<last> |
1095 | argument, just as if C<rouse_wait> had a C<return ($a1, $a2, $a3...)> |
1104 | argument, just as if C<rouse_wait> had a C<return ($a1, $a2, $a3...)> |
1096 | statement at the end. |
1105 | statement at the end. |
1097 | |
1106 | |
|
|
1107 | You are only allowed to wait once for a given rouse callback. |
|
|
1108 | |
1098 | See the section B<HOW TO WAIT FOR A CALLBACK> for an actual usage example. |
1109 | See the section B<HOW TO WAIT FOR A CALLBACK> for an actual usage example. |
|
|
1110 | |
|
|
1111 | As of Coro 6.57, you can reliably wait for a rouse callback in a different |
|
|
1112 | thread than from where it was created. |
1099 | |
1113 | |
1100 | =back |
1114 | =back |
1101 | |
1115 | |
1102 | =cut |
1116 | =cut |
1103 | |
1117 | |
… | |
… | |
1109 | |
1123 | |
1110 | # 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 |
1111 | *{"Coro::$module\::new"} = $old |
1125 | *{"Coro::$module\::new"} = $old |
1112 | if $old; |
1126 | if $old; |
1113 | |
1127 | |
1114 | goto &{"Coro::$module\::new"}; |
1128 | goto &{"Coro::$module\::new"} |
1115 | }; |
1129 | }; |
1116 | } |
1130 | } |
1117 | |
1131 | |
1118 | 1; |
1132 | 1; |
1119 | |
1133 | |
… | |
… | |
1122 | It is very common for a coro to wait for some callback to be |
1136 | It is very common for a coro to wait for some callback to be |
1123 | called. This occurs naturally when you use coro in an otherwise |
1137 | called. This occurs naturally when you use coro in an otherwise |
1124 | event-based program, or when you use event-based libraries. |
1138 | event-based program, or when you use event-based libraries. |
1125 | |
1139 | |
1126 | These typically register a callback for some event, and call that callback |
1140 | These typically register a callback for some event, and call that callback |
1127 | when the event occured. In a coro, however, you typically want to |
1141 | when the event occurred. In a coro, however, you typically want to |
1128 | just wait for the event, simplyifying things. |
1142 | just wait for the event, simplyifying things. |
1129 | |
1143 | |
1130 | For example C<< AnyEvent->child >> registers a callback to be called when |
1144 | For example C<< AnyEvent->child >> registers a callback to be called when |
1131 | a specific child has exited: |
1145 | a specific child has exited: |
1132 | |
1146 | |
… | |
… | |
1261 | processes. What makes it so bad is that on non-windows platforms, you can |
1275 | processes. What makes it so bad is that on non-windows platforms, you can |
1262 | actually take advantage of custom hardware for this purpose (as evidenced |
1276 | actually take advantage of custom hardware for this purpose (as evidenced |
1263 | by the forks module, which gives you the (i-) threads API, just much |
1277 | by the forks module, which gives you the (i-) threads API, just much |
1264 | faster). |
1278 | faster). |
1265 | |
1279 | |
1266 | Sharing data is in the i-threads model is done by transfering data |
1280 | Sharing data is in the i-threads model is done by transferring data |
1267 | structures between threads using copying semantics, which is very slow - |
1281 | structures between threads using copying semantics, which is very slow - |
1268 | shared data simply does not exist. Benchmarks using i-threads which are |
1282 | shared data simply does not exist. Benchmarks using i-threads which are |
1269 | communication-intensive show extremely bad behaviour with i-threads (in |
1283 | communication-intensive show extremely bad behaviour with i-threads (in |
1270 | fact, so bad that Coro, which cannot take direct advantage of multiple |
1284 | fact, so bad that Coro, which cannot take direct advantage of multiple |
1271 | CPUs, is often orders of magnitude faster because it shares data using |
1285 | CPUs, is often orders of magnitude faster because it shares data using |