Hello,
'Coro' module by Lehmann looks so attractive that I started thinking about
re-design event loop approach of our simulator to utilize coroutines. It
would give completely new capability--users could write pure perl scripts
controlling our simulator having access to its internals. I looked at
'ithreads' earlier, but this model was completely useless as our application
is heavy with "state data".
However, the first trial script with Coro segfaults unexpectedly when I do
perl's 'do "SCRIPT"'. 'eval "CONTENTS"' also crashes in the same way. A
sample script reproducing the error is below. Note, that when I commented
out the lines:
eval q| --and--
|;
everything worked OK.
Below the script there are outputs from two runs: first with EVAL, second
without it.
I tried it with perl 5.8.4 compiled without ithreads on Solaris, and with
current distribution of perl 5.8.6-4 with ithreads on Cygwin, and got
exactly the same results, so big chances are the problem is fully
reproducible with the perl on your machine. In both cases it was latest ver
1.0 of Coro.
Do I do something wrong?
I'd be appreciated for any help.
Waldemar.
-----------------------------------
#!/usr/bin/perl -w
use strict;
use Coro;
use Coro::Event;
foreach (3, 5, 7) {
my $pid = new Coro sub {
my $count = $_[0];
eval q|
my $value = $count;
print "Start $value\n";
while ($value) {
print "value $count -> $value\n";
--$value;
cede;
}
|;
}, $_;
$pid->ready;
}
print "START\n";
loop;
print "DONE.\n";
-----------------------------------
### Output with EVAL (segfault)
START
Start 3
value 3 -> 3
Start 5
value 5 -> 5
Start 7
value 7 -> 7
value 3 -> 2
value 5 -> 4
value 7 -> 6
value 3 -> 1
value 5 -> 3
value 7 -> 5
value 5 -> 2
value 7 -> 4
value 5 -> 1
value 7 -> 3
Segmentation fault <----- :( :(
### Output with EVAL commented out (ok)
START
Start 3
value 3 -> 3
Start 5
value 5 -> 5
Start 7
value 7 -> 7
value 3 -> 2
value 5 -> 4
value 7 -> 6
value 3 -> 1
value 5 -> 3
value 7 -> 5
value 5 -> 2
value 7 -> 4
value 5 -> 1
value 7 -> 3
value 7 -> 2
value 7 -> 1
DONE.
The problem is that Coro cannot reliably work when you jump in/out of
currently-being-compiled code, as perl changes too much of global state
when compiling programs, and doesn't tolerate context switches at that
point.
You should be able to compile code, and use cede in it, but it has to be
done in seperate steps.
(i.e. eval { } instead of eval "").
The workaround for 'do ""' would be to read the string, compile it into a
sub, and run it.
It might be possible to support just that case, as compiling is almost
finished, and it might be possible to detetc this case and barf with a
better error message. I will have a look at it.
> Do I do something wrong?
Not really, you are just running into a limitation that perl sets, and is not
documented anyhwere :/
On a less serious level, yes, you do sth. wrong:
> eval q|
> |;
Of course, recompiling the code makes no sense, but I guess it's just for
the sake of the example. You _should_ be able to replace all occurrences
of that by seperate 'my $sub = eval "sub { ... }"; &$sub' invocations.
--
The choice of a
-----==- _GNU_
----==-- _ generation Marc Lehmann
---==---(_)__ __ ____ __ p...@goof.com
--==---/ / _ \/ // /\ \/ / http://schmorp.de/
-=====/_/_//_/\_,_/ /_/\_\ XX11-RIPE
I made an effort of supporting some cases of cede'ing while
compiling, the result is in Coro-1.1 (soon on CPAN, or at
http://data.plan9.de/Coro-1.1.tar.gz). It also has better amd64-linux
support.
Maybe that helps your case, but if there is a problem, try to avoid eval "..."
and use eval "sub {...}".