ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.pm
Revision: 1.166
Committed: Thu May 12 23:55:39 2011 UTC (13 years ago) by root
Branch: MAIN
Changes since 1.165: +7 -16 lines
Log Message:
*** empty log message ***

File Contents

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