ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.pm
Revision: 1.132
Committed: Sat Nov 22 07:30:46 2008 UTC (15 years, 6 months ago) by root
Branch: MAIN
Changes since 1.131: +1 -1 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 coroutine (called with @_), switching back\n";
11 $new->transfer ($main);
12 print "in coroutine again, switching back\n";
13 $new->transfer ($main);
14 }, 5;
15
16 $main = new Coro::State;
17
18 print "in main, switching to coroutine\n";
19 $main->transfer ($new);
20 print "back in main, switch to coroutine again\n";
21 $main->transfer ($new);
22 print "back in main\n";
23
24 =head1 DESCRIPTION
25
26 This module implements coroutines. Coroutines, similar to continuations,
27 allow you to run more than one "thread of execution" in parallel. Unlike
28 threads, there is no parallelism and only voluntary switching is used so
29 locking problems are greatly reduced.
30
31 This can be used to implement non-local jumps, exception handling,
32 (non-clonable) continuations and more.
33
34 This module provides only low-level functionality. See L<Coro> and related
35 modules for a higher level process abstraction including scheduling.
36
37 =head2 MODEL
38
39 Coro::State implements two different coroutine models: Perl and C. The
40 C coroutines (called cctx's) are basically simplified perl interpreters
41 running/interpreting the Perl coroutines. A single interpreter can run any
42 number of Perl coroutines, so usually there are very few C coroutines.
43
44 When Perl code calls a C function (e.g. in an extension module) and that
45 C function then calls back into Perl or does a coroutine switch the C
46 coroutine can no longer execute other Perl coroutines, so it stays tied to
47 the specific coroutine until it returns to the original Perl caller, after
48 which it is again available to run other Perl coroutines.
49
50 The main program always has its own "C coroutine" (which really is
51 *the* Perl interpreter running the whole program), so there will always
52 be at least one additional C coroutine. You can use the debugger (see
53 L<Coro::Debug>) to find out which coroutines are tied to their cctx and
54 which aren't.
55
56 =head2 MEMORY CONSUMPTION
57
58 A newly created coroutine that has not been used only allocates a
59 relatively small (a hundred bytes) structure. Only on the first
60 C<transfer> will perl allocate stacks (a few kb, 64 bit architetcures
61 use twice as much, i.e. a few kb :) and optionally a C stack/coroutine
62 (cctx) for coroutines that recurse through C functions. All this is very
63 system-dependent. On my x86-pc-linux-gnu system this amounts to about 2k
64 per (non-trivial but simple) coroutine.
65
66 You can view the actual memory consumption using Coro::Debug. Keep in mind
67 that a for loop or other block constructs can easily consume 100-200 bytes
68 per nesting level.
69
70 =cut
71
72 package Coro::State;
73
74 use strict;
75 no warnings "uninitialized";
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.0";
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 coroutines. This is useful if you want some generic
125 logging function that works for all coroutines 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 can
135 must not return from your die hook - 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 coroutines, so
141 changing its value will change it in all coroutines 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 coroutine and return it. The first C<transfer> call to this
157 coroutine will start execution at the given coderef, with the given
158 arguments.
159
160 Note that the arguments will not be copied. Instead, as with normal
161 function calls, the coroutine receives passed arguments by reference, so
162 make sure you don't change them in unexpected ways.
163
164 Returning from such a coroutine 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 coroutine does the same as calling it in the main
175 program.
176
177 If the coderef is omitted this function will create a new "empty"
178 coroutine, i.e. a coroutine that cannot be transfered to but can be used
179 to save the current coroutine state in (note that this is dangerous, as no
180 reference is taken to ensure that the "current coroutine state" survives,
181 the caller is responsible to ensure that the cloned state does not go
182 away).
183
184 The returned object is an empty hash which can be used for any purpose
185 whatsoever, for example when subclassing Coro::State.
186
187 Certain variables are "localised" to each coroutine, that is, certain
188 "global" variables are actually per coroutine. Not everything that would
189 sensibly be localised currently is, and not everything that is localised
190 makes sense for every application, and the future might bring changes.
191
192 The following global variables can have different values per coroutine,
193 and have the stated initial values:
194
195 Variable Initial Value
196 @_ whatever arguments were passed to the Coro
197 $_ undef
198 $@ undef
199 $/ "\n"
200 $SIG{__DIE__} aliased to $Coro::State::DIEHOOK(*)
201 $SIG{__WARN__} aliased to $Coro::State::WARNHOOK(*)
202 (default fh) *STDOUT
203 $1, $2... all regex results are initially undefined
204
205 (*) reading the value from %SIG is not supported, but local'ising is.
206
207 If you feel that something important is missing then tell me. Also
208 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
213 The easiest way to do this is to create your own scheduling primitive like
214 in the code below, and use it in your coroutines:
215
216 sub my_cede {
217 local ($;, ...);
218 Coro::cede;
219 }
220
221 =cut
222
223 =item $state->throw ([$scalar])
224
225 See L<< Coro->throw >>.
226
227 =item $state->call ($coderef)
228
229 Try to call the given C<$coderef> in the context of the given state. This
230 works even when the state is currently within an XS function, and can
231 be very dangerous. You can use it to acquire stack traces etc. (see the
232 Coro::Debug module for more details). The coderef MUST NOT EVER transfer
233 to another state.
234
235 =item $state->eval ($string)
236
237 Like C<call>, but eval's the string. Dangerous.
238
239 =item $state->swap_defsv
240
241 =item $state->swap_defav
242
243 Swap the current C<$_> (swap_defsv) or C<@_> (swap_defav) with the
244 equivalent in the saved state of C<$state>. This can be used to give the
245 coroutine a defined content for C<@_> and C<$_> before transfer'ing to it.
246
247 =item $state->trace ($flags)
248
249 Internal function to control tracing. I just mention this so you can stay
250 away from abusing it.
251
252 =item $prev->transfer ($next)
253
254 Save the state of the current subroutine in C<$prev> and switch to the
255 coroutine saved in C<$next>.
256
257 The "state" of a subroutine includes the scope, i.e. lexical variables and
258 the current execution state (subroutine, stack).
259
260 =item $bytes = $state->rss
261
262 Returns the memory allocated by the coroutine (which includes static
263 structures, various perl stacks but NOT local variables, arguments or any
264 C context data). This is a rough indication of how much memory it might
265 use.
266
267 =item $state->has_cctx
268
269 Returns whether the state currently uses a cctx/C context. An active
270 state always has a cctx, as well as the main program. Other states only
271 use a cctxts when needed.
272
273 =item Coro::State::force_cctx
274
275 Forces the allocation of a C context for the currently running coroutine
276 (if not already done). Apart from benchmarking there is little point
277 in doing so, however.
278
279 =item $ncctx = Coro::State::cctx_count
280
281 Returns the number of C-level coroutines allocated. If this number is
282 very high (more than a dozen) it might help to identify points of C-level
283 recursion in your code and moving this into a separate coroutine.
284
285 =item $nidle = Coro::State::cctx_idle
286
287 Returns the number of allocated but idle (free for reuse) C level
288 coroutines. Currently, Coro will limit the number of idle/unused cctxs to
289 8.
290
291 =item $old = Coro::State::cctx_stacksize [$new_stacksize]
292
293 Returns the current C stack size and optionally sets the new I<minimum>
294 stack size to C<$new_stacksize> I<long>s. Existing stacks will not
295 be changed, but Coro will try to replace smaller stacks as soon as
296 possible. Any Coro::State that starts to use a stack after this call is
297 guaranteed this minimum stack size.
298
299 Please note that Coroutines will only need to use a C-level stack if the
300 interpreter recurses or calls a function in a module that calls back into
301 the interpreter, so use of this feature is usually never needed.
302
303 =item $old = Coro::State::cctx_max_idle [$new_count]
304
305 Coro caches C contexts that are not in use currently, as creating them
306 from scratch has some overhead.
307
308 This function returns the current maximum number of idle C contexts and
309 optionally sets the new amount. The count must be at least C<1>, with the
310 default being C<4>.
311
312 =item @states = Coro::State::list
313
314 Returns a list of all states currently allocated.
315
316 =item $clone = $state->clone
317
318 This exciting method takes a Coro::State object and clones, i.e., creates
319 a copy. This makes it possible to restore a state more than once.
320
321 Since its only known purpose is intellectual self-gratification, and
322 because it is a difficult piece of code, it is not enabled by default, and
323 not supported.
324
325 Among the games you can play with this is implement a scheme-like call/cc,
326 as the following code does (well, with small differences).
327
328 sub callcc(&@) {
329 my ($func, @arg) = @_;
330
331 my $state = new Coro::State;
332 my $wrapper; $wrapper = new Coro::State sub {
333 my $escape = sub {
334 @arg = @_;
335 $wrapper->transfer ($state->clone);
336 };
337 $escape->($func->($escape, @arg));
338 };
339
340 $state->transfer ($wrapper);
341 @arg
342 }
343
344 Here are a few little-known facts: First, coroutines *are* full/true/real
345 continuations. Secondly Coro::State objects (without clone) *are* first
346 class continuations. Thirdly, nobody has ever found a use for the full
347 power of call/cc that isn't better (faster, easier, more efficient)
348 implemented differently, and nobody has yet found a useful control
349 construct that can't be implemented without it already, just much faster
350 and with fewer resources.
351
352 Besides, call/cc is much less useful in a Perl-like dynamic language (with
353 references, and its scoping rules) then in, say, scheme.
354
355 Now, the known limitations of C<clone>:
356
357 It probably only works on perl 5.10; it cannot clone a coroutine inside
358 the substition operator (but windows perl can't fork from there either)
359 and some other contexts, and C<abort ()> is the preferred mechanism to
360 signal errors. It cannot clone a state that has a c context attached
361 (implementing clone on the C level is too hard for me to even try),
362 which rules out calling call/cc from the main coroutine. It cannot
363 clone a context that hasn't even been started yet. It doesn't work with
364 C<-DDEBUGGING> (but what does). It probably also leaks, and sometimes
365 triggers a few assertions inside Coro. Most of these limitations *are*
366 fixable with some effort, but that's pointless just to make a point that
367 it could be done.
368
369 =cut
370
371 # used by Coro::Debug only atm.
372 sub debug_desc {
373 $_[0]{desc}
374 }
375
376 # for very deep reasons, we must initialise $Coro::main here.
377
378 {
379 package Coro;
380
381 our $main; # main coroutine
382 our $current; # current coroutine
383
384 $main = Coro::State::new Coro::;
385
386 $main->{desc} = "[main::]";
387
388 # maybe some other module used Coro::Specific before...
389 $main->{_specific} = $current->{_specific}
390 if $current;
391
392 _set_current $main;
393 }
394
395 1;
396
397 =back
398
399 =head1 BUGS
400
401 This module is not thread-safe. You must only ever use this module from
402 the same thread (this requirement might be removed in the future).
403
404 =head1 SEE ALSO
405
406 L<Coro>.
407
408 =head1 AUTHOR
409
410 Marc Lehmann <schmorp@schmorp.de>
411 http://home.schmorp.de/
412
413 =cut
414