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