1 |
=head1 NAME |
2 |
|
3 |
Coro::State - first class continuations |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro::State; |
8 |
|
9 |
$new = new Coro::State sub { |
10 |
print "in coro (called with @_), switching back\n"; |
11 |
$new->transfer ($main); |
12 |
print "in coro again, switching back\n"; |
13 |
$new->transfer ($main); |
14 |
}, 5; |
15 |
|
16 |
$main = new Coro::State; |
17 |
|
18 |
print "in main, switching to coro\n"; |
19 |
$main->transfer ($new); |
20 |
print "back in main, switch to coro again\n"; |
21 |
$main->transfer ($new); |
22 |
print "back in main\n"; |
23 |
|
24 |
=head1 DESCRIPTION |
25 |
|
26 |
This module implements coro. Coros, similar to threads and continuations, |
27 |
allow you to run more than one "thread of execution" in parallel. Unlike |
28 |
so-called "kernel" threads, there is no parallelism and only voluntary |
29 |
switching is used so locking problems are greatly reduced. The latter is |
30 |
called "cooperative" threading as opposed to "preemptive" threading. |
31 |
|
32 |
This can be used to implement non-local jumps, exception handling, |
33 |
continuation objects and more. |
34 |
|
35 |
This module provides only low-level functionality. See L<Coro> and related |
36 |
modules for a higher level threads abstraction including a scheduler. |
37 |
|
38 |
=head2 MODEL |
39 |
|
40 |
Coro::State implements two different thread models: Perl and C. The C |
41 |
threads (called cctx's) are basically simplified perl interpreters |
42 |
running/interpreting the Perl threads. A single interpreter can run any |
43 |
number of Perl threads, so usually there are very few C threads. |
44 |
|
45 |
When Perl code calls a C function (e.g. in an extension module) and that C |
46 |
function then calls back into Perl or transfers control to another thread, |
47 |
the C thread can no longer execute other Perl threads, so it stays tied to |
48 |
the specific thread until it returns to the original Perl caller, after |
49 |
which it is again available to run other Perl threads. |
50 |
|
51 |
The main program always has its own "C thread" (which really is |
52 |
*the* Perl interpreter running the whole program), so there will always |
53 |
be at least one additional C thread. You can use the debugger (see |
54 |
L<Coro::Debug>) to find out which threads are tied to their cctx and |
55 |
which aren't. |
56 |
|
57 |
=head2 MEMORY CONSUMPTION |
58 |
|
59 |
A newly created Coro::State that has not been used only allocates a |
60 |
relatively small (a hundred bytes) structure. Only on the first |
61 |
C<transfer> will perl allocate stacks (a few kb, 64 bit architetcures |
62 |
use twice as much, i.e. a few kb :) and optionally a C stack/thread |
63 |
(cctx) for threads that recurse through C functions. All this is very |
64 |
system-dependent. On my x86-pc-linux-gnu system this amounts to about 2k |
65 |
per (non-trivial but simple) Coro::State. |
66 |
|
67 |
You can view the actual memory consumption using Coro::Debug. Keep in mind |
68 |
that a for loop or other block constructs can easily consume 100-200 bytes |
69 |
per nesting level. |
70 |
|
71 |
=cut |
72 |
|
73 |
package Coro::State; |
74 |
|
75 |
use common::sense; |
76 |
|
77 |
use Carp; |
78 |
|
79 |
use Time::HiRes (); # currently only used for PerlIO::cede |
80 |
|
81 |
our $DIEHOOK; |
82 |
our $WARNHOOK; |
83 |
|
84 |
BEGIN { |
85 |
$DIEHOOK = sub { }; |
86 |
$WARNHOOK = sub { warn $_[0] }; |
87 |
} |
88 |
|
89 |
sub diehook { &$DIEHOOK } |
90 |
sub warnhook { &$WARNHOOK } |
91 |
|
92 |
use XSLoader; |
93 |
|
94 |
BEGIN { |
95 |
our $VERSION = 5.21; |
96 |
|
97 |
# must be done here because the xs part expects it to exist |
98 |
# it might exist already because Coro::Specific created it. |
99 |
$Coro::current ||= { }; |
100 |
|
101 |
{ |
102 |
# save/restore the handlers before/after overwriting %SIG magic |
103 |
local $SIG{__DIE__}; |
104 |
local $SIG{__WARN__}; |
105 |
|
106 |
XSLoader::load __PACKAGE__, $VERSION; |
107 |
} |
108 |
|
109 |
# need to do it after overwriting the %SIG magic |
110 |
$SIG{__DIE__} ||= \&diehook; |
111 |
$SIG{__WARN__} ||= \&warnhook; |
112 |
} |
113 |
|
114 |
use Exporter; |
115 |
use base Exporter::; |
116 |
|
117 |
=head2 GLOBAL VARIABLES |
118 |
|
119 |
=over 4 |
120 |
|
121 |
=item $Coro::State::DIEHOOK |
122 |
|
123 |
This works similarly to C<$SIG{__DIE__}> and is used as the default die |
124 |
hook for newly created Coro::States. This is useful if you want some generic |
125 |
logging function that works for all threads that don't set their own |
126 |
hook. |
127 |
|
128 |
When Coro::State is first loaded it will install these handlers for the |
129 |
main program, too, unless they have been overwritten already. |
130 |
|
131 |
The default handlers provided will behave like the built-in ones (as if |
132 |
they weren't there). |
133 |
|
134 |
If you don't want to exit your program on uncaught exceptions, you must |
135 |
not return from your die hook - call C<Coro::terminate> instead. |
136 |
|
137 |
Note 1: You I<must> store a valid code reference in these variables, |
138 |
C<undef> will I<not> do. |
139 |
|
140 |
Note 2: The value of this variable will be shared among all threads, so |
141 |
changing its value will change it in all threads that don't have their |
142 |
own die handler. |
143 |
|
144 |
=item $Coro::State::WARNHOOK |
145 |
|
146 |
Similar to above die hook, but augments C<$SIG{__WARN__}>. |
147 |
|
148 |
=back |
149 |
|
150 |
=head2 FUNCTIONS |
151 |
|
152 |
=over 4 |
153 |
|
154 |
=item $coro = new Coro::State [$coderef[, @args...]] |
155 |
|
156 |
Create a new Coro::State thread object and return it. The first |
157 |
C<transfer> call to this thread will start execution at the given |
158 |
coderef, with the given arguments. |
159 |
|
160 |
Note that the arguments will not be copied. Instead, as with normal |
161 |
function calls, the thread receives passed arguments by reference, so |
162 |
make sure you don't change them in unexpected ways. |
163 |
|
164 |
Returning from such a thread is I<NOT> supported. Neither is calling |
165 |
C<exit> or throwing an uncaught exception. The following paragraphs |
166 |
describe what happens in current versions of Coro. |
167 |
|
168 |
If the subroutine returns the program will be terminated as if execution |
169 |
of the main program ended. |
170 |
|
171 |
If it throws an exception the program will terminate unless the exception |
172 |
is caught, exactly like in the main program. |
173 |
|
174 |
Calling C<exit> in a thread does the same as calling it in the main |
175 |
program, but due to libc bugs on many BSDs, this doesn't work reliable |
176 |
everywhere. |
177 |
|
178 |
If the coderef is omitted this function will create a new "empty" |
179 |
thread, i.e. a thread that cannot be transfered to but can be used |
180 |
to save the current thread state in (note that this is dangerous, as no |
181 |
reference is taken to ensure that the "current thread state" survives, |
182 |
the caller is responsible to ensure that the cloned state does not go |
183 |
away). |
184 |
|
185 |
The returned object is an empty hash which can be used for any purpose |
186 |
whatsoever, for example when subclassing Coro::State. |
187 |
|
188 |
Certain variables are "localised" to each thread, that is, certain |
189 |
"global" variables are actually per thread. Not everything that would |
190 |
sensibly be localised currently is, and not everything that is localised |
191 |
makes sense for every application, and the future might bring changes. |
192 |
|
193 |
The following global variables can have different values per thread, |
194 |
and have the stated initial values: |
195 |
|
196 |
Variable Initial Value |
197 |
@_ whatever arguments were passed to the Coro |
198 |
$_ undef |
199 |
$@ undef |
200 |
$/ "\n" |
201 |
$SIG{__DIE__} aliased to $Coro::State::DIEHOOK(*) |
202 |
$SIG{__WARN__} aliased to $Coro::State::WARNHOOK(*) |
203 |
(default fh) *STDOUT |
204 |
$^H, %^H zero/empty. |
205 |
$1, $2... all regex results are initially undefined |
206 |
|
207 |
(*) reading the value from %SIG is not supported, but local'ising is. |
208 |
|
209 |
If you feel that something important is missing then tell me. Also |
210 |
remember that every function call that might call C<transfer> (such |
211 |
as C<Coro::Channel::put>) might clobber any global and/or special |
212 |
variables. Yes, this is by design ;) You can always create your own |
213 |
process abstraction model that saves these variables. |
214 |
|
215 |
The easiest way to do this is to create your own scheduling primitive like |
216 |
in the code below, and use it in your threads: |
217 |
|
218 |
sub my_cede { |
219 |
local ($;, ...); |
220 |
Coro::cede; |
221 |
} |
222 |
|
223 |
Another way is to use dynamic winders, see C<Coro::on_enter> and |
224 |
C<Coro::on_leave> for this. |
225 |
|
226 |
=item $prev->transfer ($next) |
227 |
|
228 |
Save the state of the current subroutine in C<$prev> and switch to the |
229 |
thread saved in C<$next>. |
230 |
|
231 |
The "state" of a subroutine includes the scope, i.e. lexical variables and |
232 |
the current execution state (subroutine, stack). |
233 |
|
234 |
=item $state->is_new |
235 |
|
236 |
Returns true iff this Coro::State object is "new", i.e. has never been run |
237 |
yet. Those states basically consist of only the code reference to call and |
238 |
the arguments, but consumes very little other resources. New states will |
239 |
automatically get assigned a perl interpreter when they are transfered to. |
240 |
|
241 |
=item $state->is_destroyed |
242 |
|
243 |
Returns true iff the Coro::State object has been destroyed (by |
244 |
C<cancel>), i.e. it's resources freed because they were C<cancel>'d (or |
245 |
C<terminate>'d). |
246 |
|
247 |
=item $state->cancel |
248 |
|
249 |
Forcefully destructs the given Coro::State. While you can keep the |
250 |
reference, and some memory is still allocated, the Coro::State object is |
251 |
effecticely dead, destructors have been freed, it cannot be transfered to |
252 |
anymore. |
253 |
|
254 |
=item $state->throw ([$scalar]) |
255 |
|
256 |
See L<< Coro->throw >>. |
257 |
|
258 |
=item $state->call ($coderef) |
259 |
|
260 |
Try to call the given C<$coderef> in the context of the given state. This |
261 |
works even when the state is currently within an XS function, and can |
262 |
be very dangerous. You can use it to acquire stack traces etc. (see the |
263 |
Coro::Debug module for more details). The coderef MUST NOT EVER transfer |
264 |
to another state. |
265 |
|
266 |
=item $state->eval ($string) |
267 |
|
268 |
Like C<call>, but eval's the string. Dangerous. |
269 |
|
270 |
=item $state->swap_defsv |
271 |
|
272 |
=item $state->swap_defav |
273 |
|
274 |
Swap the current C<$_> (swap_defsv) or C<@_> (swap_defav) with the |
275 |
equivalent in the saved state of C<$state>. This can be used to give the |
276 |
coro a defined content for C<@_> and C<$_> before transfer'ing to it. |
277 |
|
278 |
=item $state->swap_sv (\$sv, \$swap_sv) |
279 |
|
280 |
This (very advanced) function can be used to make I<any> variable local to |
281 |
a thread. |
282 |
|
283 |
It works by swapping the contents of C<$sv> and C<$swap_sv> each time the |
284 |
thread is entered and left again, i.e. it is similarly to: |
285 |
|
286 |
$tmp = $sv; $sv = $swap_sv; $swap_sv = $tmp; |
287 |
|
288 |
Except that it doesn't make an copies and works on hashes and even more |
289 |
exotic values (code references!). |
290 |
|
291 |
Needless to say, this function can be very very dangerous: you can easily |
292 |
swap a hash with a reference (i.e. C<%hash> I<becomes> a reference), and perl |
293 |
will not like this at all. |
294 |
|
295 |
It will also swap "magicalness" - so when swapping a builtin perl variable |
296 |
(such as C<$.>), it will lose it's magicalness, which, again, perl will |
297 |
not like, so don't do it. |
298 |
|
299 |
Lastly, the C<$swap_sv> itself will be used, not a copy, so make sure you |
300 |
give each thread it's own C<$swap_sv> instance. |
301 |
|
302 |
It is, however, quite safe to swap some normal variable with |
303 |
another. For example, L<PApp::SQL> stores the default database handle in |
304 |
C<$PApp::SQL::DBH>. To make this a per-thread variable, use this: |
305 |
|
306 |
my $private_dbh = ...; |
307 |
$coro->swap_sv (\$PApp::SQL::DBH, \$private_dbh); |
308 |
|
309 |
This results in C<$PApp::SQL::DBH> having the value of C<$private_dbh> |
310 |
while it executes, and whatever other value it had when it doesn't |
311 |
execute. |
312 |
|
313 |
You can also swap hashes and other values: |
314 |
|
315 |
my %private_hash; |
316 |
$coro->swap_sv (\%some_hash, \%private_hash); |
317 |
|
318 |
=item $state->trace ($flags) |
319 |
|
320 |
Internal function to control tracing. I just mention this so you can stay |
321 |
away from abusing it. |
322 |
|
323 |
=item $bytes = $state->rss |
324 |
|
325 |
Returns the memory allocated by the coro (which includes static |
326 |
structures, various perl stacks but NOT local variables, arguments or any |
327 |
C context data). This is a rough indication of how much memory it might |
328 |
use. |
329 |
|
330 |
=item $state->has_cctx |
331 |
|
332 |
Returns whether the state currently uses a cctx/C context. An active |
333 |
state always has a cctx, as well as the main program. Other states only |
334 |
use a cctxts when needed. |
335 |
|
336 |
=item Coro::State::force_cctx |
337 |
|
338 |
Forces the allocation of a C context for the currently running coro |
339 |
(if not already done). Apart from benchmarking there is little point |
340 |
in doing so, however. |
341 |
|
342 |
=item $ncctx = Coro::State::cctx_count |
343 |
|
344 |
Returns the number of C-level coro allocated. If this number is |
345 |
very high (more than a dozen) it might help to identify points of C-level |
346 |
recursion in your code and moving this into a separate coro. |
347 |
|
348 |
=item $nidle = Coro::State::cctx_idle |
349 |
|
350 |
Returns the number of allocated but idle (free for reuse) C level |
351 |
coro. Currently, Coro will limit the number of idle/unused cctxs to |
352 |
8. |
353 |
|
354 |
=item $old = Coro::State::cctx_stacksize [$new_stacksize] |
355 |
|
356 |
Returns the current C stack size and optionally sets the new I<minimum> |
357 |
stack size to C<$new_stacksize> I<long>s. Existing stacks will not |
358 |
be changed, but Coro will try to replace smaller stacks as soon as |
359 |
possible. Any Coro::State that starts to use a stack after this call is |
360 |
guaranteed this minimum stack size. |
361 |
|
362 |
Please note that coros will only need to use a C-level stack if the |
363 |
interpreter recurses or calls a function in a module that calls back into |
364 |
the interpreter, so use of this feature is usually never needed. |
365 |
|
366 |
=item $old = Coro::State::cctx_max_idle [$new_count] |
367 |
|
368 |
Coro caches C contexts that are not in use currently, as creating them |
369 |
from scratch has some overhead. |
370 |
|
371 |
This function returns the current maximum number of idle C contexts and |
372 |
optionally sets the new amount. The count must be at least C<1>, with the |
373 |
default being C<4>. |
374 |
|
375 |
=item @states = Coro::State::list |
376 |
|
377 |
Returns a list of all states currently allocated. |
378 |
|
379 |
=item $was_enabled = Coro::State::enable_times [$enable] |
380 |
|
381 |
Enables/disables/queries the current state of per-thread real and |
382 |
cpu-time gathering. |
383 |
|
384 |
When enabled, the real time and the cpu time (user + system time) |
385 |
spent in each thread is accumulated. If disabled, then the accumulated |
386 |
times will stay as they are (they start at 0). |
387 |
|
388 |
Currently, cpu time is only measured on GNU/Linux systems, all other |
389 |
systems only gather real time. |
390 |
|
391 |
Enabling time profiling slows down thread switching by a factor of 2 to |
392 |
10, depending on platform on hardware. |
393 |
|
394 |
The times will be displayed when running C<Coro::Debug::command "ps">, and |
395 |
cna be queried by calling C<< $state->times >>. |
396 |
|
397 |
=item ($real, $cpu) = $state->times |
398 |
|
399 |
Returns the real time and cpu times spent in the given C<$state>. See |
400 |
C<Coro::State::enable_times> for more info. |
401 |
|
402 |
=item $clone = $state->clone |
403 |
|
404 |
This exciting method takes a Coro::State object and clones it, i.e., it |
405 |
creates a copy. This makes it possible to restore a state more than once, |
406 |
and even return to states that have returned or have been terminated. |
407 |
|
408 |
Since its only known purpose is for intellectual self-gratification, and |
409 |
because it is a difficult piece of code, it is not enabled by default, and |
410 |
not supported. |
411 |
|
412 |
Here are a few little-known facts: First, coros *are* full/true/real |
413 |
continuations. Secondly Coro::State objects (without clone) *are* first |
414 |
class continuations. Thirdly, nobody has ever found a use for the full |
415 |
power of call/cc that isn't better (faster, easier, more efficiently) |
416 |
implemented differently, and nobody has yet found a useful control |
417 |
construct that can't be implemented without it already, just much faster |
418 |
and with fewer resources. And lastly, Scheme's call/cc doesn't support |
419 |
using call/cc to implement threads. |
420 |
|
421 |
Among the games you can play with this is implementing a scheme-like |
422 |
call-with-current-continuation, as the following code does (well, with |
423 |
small differences). |
424 |
|
425 |
# perl disassociates from local lexicals on frame exit, |
426 |
# so use a global variable for return values. |
427 |
my @ret; |
428 |
|
429 |
sub callcc($@) { |
430 |
my ($func, @arg) = @_; |
431 |
|
432 |
my $continuation = new Coro::State; |
433 |
$continuation->transfer (new Coro::State sub { |
434 |
my $escape = sub { |
435 |
@ret = @_; |
436 |
Coro::State->new->transfer ($continuation->clone); |
437 |
}; |
438 |
$escape->($func->($escape, @arg)); |
439 |
}); |
440 |
|
441 |
my @ret_ = @ret; @ret = (); |
442 |
wantarray ? @ret_ : pop @ret_ |
443 |
} |
444 |
|
445 |
Which could be used to implement a loop like this: |
446 |
|
447 |
async { |
448 |
my $n; |
449 |
my $l = callcc sub { $_[0] }; |
450 |
|
451 |
$n++; |
452 |
print "iteration $n\n"; |
453 |
|
454 |
$l->($l) unless $n == 10; |
455 |
}; |
456 |
|
457 |
If you find this confusing, then you already understand the coolness of |
458 |
call/cc: It can turn anything into spaghetti code real fast. |
459 |
|
460 |
Besides, call/cc is much less useful in a Perl-like dynamic language (with |
461 |
references, and its scoping rules) then in, say, scheme. |
462 |
|
463 |
Now, the known limitations of C<clone>: |
464 |
|
465 |
It probably only works on perl 5.10; it cannot clone a coro inside |
466 |
the substition operator (but windows perl can't fork from there either) |
467 |
and some other contexts, and C<abort ()> is the preferred mechanism to |
468 |
signal errors. It cannot clone a state that has a c context attached |
469 |
(implementing clone on the C level is too hard for me to even try), |
470 |
which rules out calling call/cc from the main coro. It cannot |
471 |
clone a context that hasn't even been started yet. It doesn't work with |
472 |
C<-DDEBUGGING> (but what does). It probably also leaks, and sometimes |
473 |
triggers a few assertions inside Coro. Most of these limitations *are* |
474 |
fixable with some effort, but that's pointless just to make a point that |
475 |
it could be done. |
476 |
|
477 |
The current implementation could without doubt be optimised to be a |
478 |
constant-time operation by doing lazy stack copying, if somebody were |
479 |
insane enough to invest the time. |
480 |
|
481 |
=cut |
482 |
|
483 |
# used by Coro::Debug only atm. |
484 |
sub debug_desc { |
485 |
$_[0]{desc} |
486 |
} |
487 |
|
488 |
# for very deep reasons, we must initialise $Coro::main here. |
489 |
|
490 |
{ |
491 |
package Coro; |
492 |
|
493 |
our $main; # main coro |
494 |
our $current; # current coro |
495 |
|
496 |
$main = Coro::new Coro::; |
497 |
|
498 |
$main->{desc} = "[main::]"; |
499 |
|
500 |
# maybe some other module used Coro::Specific before... |
501 |
$main->{_specific} = $current->{_specific} |
502 |
if $current; |
503 |
|
504 |
_set_current $main; |
505 |
} |
506 |
|
507 |
# we also make sure we have Coro::AnyEvent when AnyEvent is used, |
508 |
# without loading or initialising AnyEvent |
509 |
if (defined $AnyEvent::MODEL) { |
510 |
require Coro::AnyEvent; |
511 |
} else { |
512 |
push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; |
513 |
} |
514 |
|
515 |
1; |
516 |
|
517 |
=back |
518 |
|
519 |
=head1 BUGS |
520 |
|
521 |
This module is not thread-safe. You must only ever use this module from |
522 |
the same thread (this requirement might be removed in the future). |
523 |
|
524 |
=head1 SEE ALSO |
525 |
|
526 |
L<Coro>. |
527 |
|
528 |
=head1 AUTHOR |
529 |
|
530 |
Marc Lehmann <schmorp@schmorp.de> |
531 |
http://home.schmorp.de/ |
532 |
|
533 |
=cut |
534 |
|