ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.pm
Revision: 1.144
Committed: Mon Jun 29 06:14:23 2009 UTC (14 years, 11 months ago) by root
Branch: MAIN
Changes since 1.143: +23 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 strict;
76 no warnings "uninitialized";
77
78 use Carp;
79
80 use Time::HiRes (); # currently only used for PerlIO::cede
81
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
93 use XSLoader;
94
95 BEGIN {
96 our $VERSION = 5.14;
97
98 # 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 {
103 # save/restore the handlers before/after overwriting %SIG magic
104 local $SIG{__DIE__};
105 local $SIG{__WARN__};
106
107 XSLoader::load __PACKAGE__, $VERSION;
108 }
109
110 # need to do it after overwriting the %SIG magic
111 $SIG{__DIE__} ||= \&diehook;
112 $SIG{__WARN__} ||= \&warnhook;
113 }
114
115 use Exporter;
116 use base Exporter::;
117
118 =head2 GLOBAL VARIABLES
119
120 =over 4
121
122 =item $Coro::State::DIEHOOK
123
124 This works similarly to C<$SIG{__DIE__}> and is used as the default die
125 hook for newly created Coro::States. This is useful if you want some generic
126 logging function that works for all threads that don't set their own
127 hook.
128
129 When Coro::State is first loaded it will install these handlers for the
130 main program, too, unless they have been overwritten already.
131
132 The default handlers provided will behave like the built-in ones (as if
133 they weren't there).
134
135 If you don't want to exit your program on uncaught exceptions, you can
136 must not return from your die hook - terminate instead.
137
138 Note 1: You I<must> store a valid code reference in these variables,
139 C<undef> will I<not> do.
140
141 Note 2: The value of this variable will be shared among all threads, so
142 changing its value will change it in all threads that don't have their
143 own die handler.
144
145 =item $Coro::State::WARNHOOK
146
147 Similar to above die hook, but augments C<$SIG{__WARN__}>.
148
149 =back
150
151 =head2 FUNCTIONS
152
153 =over 4
154
155 =item $coro = new Coro::State [$coderef[, @args...]]
156
157 Create a new Coro::State thread object and return it. The first
158 C<transfer> call to this thread will start execution at the given
159 coderef, with the given arguments.
160
161 Note that the arguments will not be copied. Instead, as with normal
162 function calls, the thread receives passed arguments by reference, so
163 make sure you don't change them in unexpected ways.
164
165 Returning from such a thread is I<NOT> supported. Neither is calling
166 C<exit> or throwing an uncaught exception. The following paragraphs
167 describe what happens in current versions of Coro.
168
169 If the subroutine returns the program will be terminated as if execution
170 of the main program ended.
171
172 If it throws an exception the program will terminate unless the exception
173 is caught, exactly like in the main program.
174
175 Calling C<exit> in a thread does the same as calling it in the main
176 program, but due to libc bugs on many BSDs, this doesn't work reliable
177 everywhere.
178
179 If the coderef is omitted this function will create a new "empty"
180 thread, i.e. a thread that cannot be transfered to but can be used
181 to save the current thread state in (note that this is dangerous, as no
182 reference is taken to ensure that the "current thread state" survives,
183 the caller is responsible to ensure that the cloned state does not go
184 away).
185
186 The returned object is an empty hash which can be used for any purpose
187 whatsoever, for example when subclassing Coro::State.
188
189 Certain variables are "localised" to each thread, that is, certain
190 "global" variables are actually per thread. Not everything that would
191 sensibly be localised currently is, and not everything that is localised
192 makes sense for every application, and the future might bring changes.
193
194 The following global variables can have different values per thread,
195 and have the stated initial values:
196
197 Variable Initial Value
198 @_ whatever arguments were passed to the Coro
199 $_ undef
200 $@ undef
201 $/ "\n"
202 $SIG{__DIE__} aliased to $Coro::State::DIEHOOK(*)
203 $SIG{__WARN__} aliased to $Coro::State::WARNHOOK(*)
204 (default fh) *STDOUT
205 $^H, %^H zero/empty.
206 $1, $2... all regex results are initially undefined
207
208 (*) reading the value from %SIG is not supported, but local'ising is.
209
210 If you feel that something important is missing then tell me. Also
211 remember that every function call that might call C<transfer> (such
212 as C<Coro::Channel::put>) might clobber any global and/or special
213 variables. Yes, this is by design ;) You can always create your own
214 process abstraction model that saves these variables.
215
216 The easiest way to do this is to create your own scheduling primitive like
217 in the code below, and use it in your threads:
218
219 sub my_cede {
220 local ($;, ...);
221 Coro::cede;
222 }
223
224 Another way is to use dynamic winders, see C<Coro::on_enter> and
225 C<Coro::on_leave> for this.
226
227 =item $prev->transfer ($next)
228
229 Save the state of the current subroutine in C<$prev> and switch to the
230 thread saved in C<$next>.
231
232 The "state" of a subroutine includes the scope, i.e. lexical variables and
233 the current execution state (subroutine, stack).
234
235 =item $state->is_new
236
237 Returns true iff this Coro::State object is "new", i.e. has never been run
238 yet. Those states basically consist of only the code reference to call and
239 the arguments, but consumes very little other resources. New states will
240 automatically get assigned a perl interpreter when they are transfered to.
241
242 =item $state->is_destroyed
243
244 Returns true iff the Coro::State object has been destroyed (by
245 C<cancel>), i.e. it's resources freed because they were C<cancel>'d (or
246 C<terminate>'d).
247
248 =item $state->cancel
249
250 Forcefully destructs the given Coro::State. While you can keep the
251 reference, and some memory is still allocated, the Coro::State object is
252 effecticely dead, destructors have been freed, it cannot be transfered to
253 anymore.
254
255 =item $state->throw ([$scalar])
256
257 See L<< Coro->throw >>.
258
259 =item $state->call ($coderef)
260
261 Try to call the given C<$coderef> in the context of the given state. This
262 works even when the state is currently within an XS function, and can
263 be very dangerous. You can use it to acquire stack traces etc. (see the
264 Coro::Debug module for more details). The coderef MUST NOT EVER transfer
265 to another state.
266
267 =item $state->eval ($string)
268
269 Like C<call>, but eval's the string. Dangerous.
270
271 =item $state->swap_defsv
272
273 =item $state->swap_defav
274
275 Swap the current C<$_> (swap_defsv) or C<@_> (swap_defav) with the
276 equivalent in the saved state of C<$state>. This can be used to give the
277 coro a defined content for C<@_> and C<$_> before transfer'ing to it.
278
279 =item $state->trace ($flags)
280
281 Internal function to control tracing. I just mention this so you can stay
282 away from abusing it.
283
284 =item $bytes = $state->rss
285
286 Returns the memory allocated by the coro (which includes static
287 structures, various perl stacks but NOT local variables, arguments or any
288 C context data). This is a rough indication of how much memory it might
289 use.
290
291 =item $state->has_cctx
292
293 Returns whether the state currently uses a cctx/C context. An active
294 state always has a cctx, as well as the main program. Other states only
295 use a cctxts when needed.
296
297 =item Coro::State::force_cctx
298
299 Forces the allocation of a C context for the currently running coro
300 (if not already done). Apart from benchmarking there is little point
301 in doing so, however.
302
303 =item $ncctx = Coro::State::cctx_count
304
305 Returns the number of C-level coro allocated. If this number is
306 very high (more than a dozen) it might help to identify points of C-level
307 recursion in your code and moving this into a separate coro.
308
309 =item $nidle = Coro::State::cctx_idle
310
311 Returns the number of allocated but idle (free for reuse) C level
312 coro. Currently, Coro will limit the number of idle/unused cctxs to
313 8.
314
315 =item $old = Coro::State::cctx_stacksize [$new_stacksize]
316
317 Returns the current C stack size and optionally sets the new I<minimum>
318 stack size to C<$new_stacksize> I<long>s. Existing stacks will not
319 be changed, but Coro will try to replace smaller stacks as soon as
320 possible. Any Coro::State that starts to use a stack after this call is
321 guaranteed this minimum stack size.
322
323 Please note that coros will only need to use a C-level stack if the
324 interpreter recurses or calls a function in a module that calls back into
325 the interpreter, so use of this feature is usually never needed.
326
327 =item $old = Coro::State::cctx_max_idle [$new_count]
328
329 Coro caches C contexts that are not in use currently, as creating them
330 from scratch has some overhead.
331
332 This function returns the current maximum number of idle C contexts and
333 optionally sets the new amount. The count must be at least C<1>, with the
334 default being C<4>.
335
336 =item @states = Coro::State::list
337
338 Returns a list of all states currently allocated.
339
340 =item $was_enabled = Coro::State::enable_times [$enable]
341
342 Enables/disables/queries the current state of per-thread real and
343 cpu-time gathering.
344
345 When enabled, the real time and the cpu time (user + system time)
346 spent in each thread is accumulated. If disabled, then the accumulated
347 times will stay as they are (they start at 0).
348
349 Currently, cpu time is only measured on GNU/Linux systems, all other
350 systems only gather real time.
351
352 Enabling time profiling slows down thread switching by a factor of 2 to
353 10, depending on platform on hardware.
354
355 The times will be displayed when running C<Coro::Debug::command "ps">, and
356 cna be queried by calling C<< $state->times >>.
357
358 =item ($real, $cpu) = $state->times
359
360 Returns the real time and cpu times spent in the given C<$state>. See
361 C<Coro::State::enable_times> for more info.
362
363 =item $clone = $state->clone
364
365 This exciting method takes a Coro::State object and clones it, i.e., it
366 creates a copy. This makes it possible to restore a state more than once,
367 and even return to states that have returned or have been terminated.
368
369 Since its only known purpose is for intellectual self-gratification, and
370 because it is a difficult piece of code, it is not enabled by default, and
371 not supported.
372
373 Here are a few little-known facts: First, coros *are* full/true/real
374 continuations. Secondly Coro::State objects (without clone) *are* first
375 class continuations. Thirdly, nobody has ever found a use for the full
376 power of call/cc that isn't better (faster, easier, more efficiently)
377 implemented differently, and nobody has yet found a useful control
378 construct that can't be implemented without it already, just much faster
379 and with fewer resources. And lastly, Scheme's call/cc doesn't support
380 using call/cc to implement threads.
381
382 Among the games you can play with this is implementing a scheme-like
383 call-with-current-continuation, as the following code does (well, with
384 small differences).
385
386 # perl disassociates from local lexicals on frame exit,
387 # so use a global variable for return values.
388 my @ret;
389
390 sub callcc($@) {
391 my ($func, @arg) = @_;
392
393 my $continuation = new Coro::State;
394 $continuation->transfer (new Coro::State sub {
395 my $escape = sub {
396 @ret = @_;
397 Coro::State->new->transfer ($continuation->clone);
398 };
399 $escape->($func->($escape, @arg));
400 });
401
402 my @ret_ = @ret; @ret = ();
403 wantarray ? @ret_ : pop @ret_
404 }
405
406 Which could be used to implement a loop like this:
407
408 async {
409 my $n;
410 my $l = callcc sub { $_[0] };
411
412 $n++;
413 print "iteration $n\n";
414
415 $l->($l) unless $n == 10;
416 };
417
418 If you find this confusing, then you already understand the coolness of
419 call/cc: It can turn anything into spaghetti code real fast.
420
421 Besides, call/cc is much less useful in a Perl-like dynamic language (with
422 references, and its scoping rules) then in, say, scheme.
423
424 Now, the known limitations of C<clone>:
425
426 It probably only works on perl 5.10; it cannot clone a coro inside
427 the substition operator (but windows perl can't fork from there either)
428 and some other contexts, and C<abort ()> is the preferred mechanism to
429 signal errors. It cannot clone a state that has a c context attached
430 (implementing clone on the C level is too hard for me to even try),
431 which rules out calling call/cc from the main coro. It cannot
432 clone a context that hasn't even been started yet. It doesn't work with
433 C<-DDEBUGGING> (but what does). It probably also leaks, and sometimes
434 triggers a few assertions inside Coro. Most of these limitations *are*
435 fixable with some effort, but that's pointless just to make a point that
436 it could be done.
437
438 The current implementation could without doubt be optimised to be a
439 constant-time operation by doing lazy stack copying, if somebody were
440 insane enough to invest the time.
441
442 =cut
443
444 # used by Coro::Debug only atm.
445 sub debug_desc {
446 $_[0]{desc}
447 }
448
449 # for very deep reasons, we must initialise $Coro::main here.
450
451 {
452 package Coro;
453
454 our $main; # main coro
455 our $current; # current coro
456
457 $main = Coro::State::new Coro::;
458
459 $main->{desc} = "[main::]";
460
461 # maybe some other module used Coro::Specific before...
462 $main->{_specific} = $current->{_specific}
463 if $current;
464
465 _set_current $main;
466 }
467
468 1;
469
470 =back
471
472 =head1 BUGS
473
474 This module is not thread-safe. You must only ever use this module from
475 the same thread (this requirement might be removed in the future).
476
477 =head1 SEE ALSO
478
479 L<Coro>.
480
481 =head1 AUTHOR
482
483 Marc Lehmann <schmorp@schmorp.de>
484 http://home.schmorp.de/
485
486 =cut
487