ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.pm
(Generate patch)

Comparing Coro/Coro/State.pm (file contents):
Revision 1.128 by root, Sat Nov 22 05:15:17 2008 UTC vs.
Revision 1.129 by root, Sat Nov 22 05:25:20 2008 UTC

320 320
321Since its only known purpose is intellectual self-gratification, and 321Since its only known purpose is intellectual self-gratification, and
322because it is a difficult piece of code, it is not enabled by default, and 322because it is a difficult piece of code, it is not enabled by default, and
323not supported. 323not supported.
324 324
325Among the games you can play with this is implement a scheme-like call/cc: 325Among the games you can play with this is implement a scheme-like call/cc,
326as the following code does (well, with small differences).
326 327
327 sub callcc(&@) { 328 sub callcc(&@) {
328 my ($f, @arg) = @_; 329 my ($func, @arg) = @_;
329 330
330 my $c = new Coro::State; 331 my $state = new Coro::State;
331
332 my $F; $F = new Coro::State sub { 332 my $wrapper; $wrapper = new Coro::State sub {
333 $f->( 333 my $escape = sub {
334 sub {
335 @arg = @_; 334 @arg = @_;
336 $F->transfer ($c->clone); 335 $wrapper->transfer ($state->clone);
337 },
338 @arg
339 ); 336 };
337 $escape->($func->($escape, @arg));
340 }; 338 };
341 339
342 $c->transfer ($F); 340 $state->transfer ($wrapper);
343 @arg 341 @arg
344 } 342 }
345 343
346Here are a few little-known facts: First, coroutines *are* full/true/real 344Here are a few little-known facts: First, coroutines *are* full/true/real
347continuations. Secondly Coro::State objects (without clone) *are* first 345continuations. Secondly Coro::State objects (without clone) *are* first
355references, and its scoping rules) then in, say, scheme. 353references, and its scoping rules) then in, say, scheme.
356 354
357Now, limitations of clone: 355Now, limitations of clone:
358 356
359It probably only works on perl 5.10; it cannot clone a coroutine inside 357It probably only works on perl 5.10; it cannot clone a coroutine inside
360the substition opertaor (but windows perl can't fork from there either), 358the substition operator (but windows perl can't fork from there either)
361and C<abort ()> is the preferred mechanism to signal errors. It probbaly 359and some other contexts, and C<abort ()> is the preferred mechanism to
360signal 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), which
362rules out calling call/cc from the main coroutine. It probably also
362also leaks, and sometimes triggers a few assertions inside Coro. Most of 363leaks, and sometimes triggers a few assertions inside Coro. Most of these
363these limitations *are* fixable with some effort, but that's pointless 364limitations *are* fixable with some effort, but that's pointless just to
364just to make a point that it could be done. 365make a point that it could be done.
365 366
366=cut 367=cut
367 368
368# used by Coro::Debug only atm. 369# used by Coro::Debug only atm.
369sub debug_desc { 370sub debug_desc {
387 if $current; 388 if $current;
388 389
389 _set_current $main; 390 _set_current $main;
390} 391}
391 392
392use Carp; warn Carp::cluck;#d#
393
3941; 3931;
395 394
396=back 395=back
397 396
398=head1 BUGS 397=head1 BUGS

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines