ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Makefile.PL
Revision: 1.87
Committed: Fri Dec 7 14:21:09 2012 UTC (11 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-6_32, rel-6_33, rel-6_31, rel-6_23, rel-6_29, rel-6_28
Changes since 1.86: +13 -15 lines
Log Message:
libecoro, erhm, stack management

File Contents

# User Rev Content
1 root 1.55 use strict;
2     use ExtUtils::MakeMaker;
3 root 1.2 use Config;
4    
5 root 1.6 $|=1;
6    
7 root 1.55 my $DEFINE;
8     my @LIBS = [];
9 root 1.2
10 root 1.69 my $threads = $Config{usethreads};
11 root 1.57
12 root 1.75 use Config;
13    
14 root 1.44 print <<EOF;
15    
16 root 1.86 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
17 root 1.44
18     Coro has a number of configuration options. Due to its maturity, the
19     defaults that Coro chooses are usually fine, so you can decide to skip
20     these questions. Only if something went wrong you should select 'n'
21     here and manually configure Coro, and, of course, report this to the
22     maintainer :)
23    
24     EOF
25    
26 root 1.45 if (prompt ("Skip further questions and use defaults (y/n)?", "y") =~ /[yY]/) {
27 root 1.44 $ENV{PERL_MM_USE_DEFAULT} = 1;
28     }
29    
30    
31 root 1.55 my $iface;
32    
33 root 1.65 # default to assembly on x86 and x86_64 sometimes
34     my $iface_asm = $Config{archname} =~ /^(i[3456]86|amd64|x86_64)-/ ? "a" : undef;
35    
36 root 1.83 # detect whether this perl is threaded, for those broken operating
37     # systems that need it.
38    
39     my $pthread = $Config{libs} =~ /-lpthread/
40     || $Config{ldflags} =~ /-pthread/
41     || $Config{archname} =~ /-thread/;
42    
43 root 1.49 if (exists $ENV{CORO_INTERFACE}) {
44     $iface = $ENV{CORO_INTERFACE};
45 root 1.50
46 root 1.84 } elsif ($^O =~ /mswin32/i) {
47 root 1.69 # nothing works, really, without deep hacks
48 root 1.86 $iface = "f";
49 root 1.50
50 root 1.84 } elsif ($^O =~ /cygwin/) {
51     # cygwin true to its form, be an order of magnitutde slower,
52 root 1.86 # while using twice the amount of ram. but it works! yeah!
53     $iface = "p";
54 root 1.84
55 root 1.14 } elsif ($^O =~ /irix/) {
56 root 1.69 # sigaltstack works like sigstack, i.e. expects stack pointer, not stack base
57     # but wikipeida lists it as 100% posix compliant. geeeee.
58 root 1.14 $iface = "i";
59 root 1.50
60 root 1.4 } elsif ($^O =~ /linux/) {
61 root 1.69 # everything "just works", as expected
62 root 1.65 $iface = $iface_asm || "s";
63 root 1.48
64 root 1.65 } elsif ($^O =~ /freebsd/) {
65 root 1.40 # FreeBSD 4.x has ucontext.h but no makecontext et al. (see BUGS section of
66 root 1.69 # man context).
67 root 1.65 #
68 root 1.69 # FreeBSD 6.2 has marginally working ucontext, setjmp and asm, but
69 root 1.68 # some 5.8.8's barf when threaded due to broken threading.
70 root 1.65
71     $iface = $iface_asm || "s";
72 root 1.50
73 root 1.65 } elsif ($^O =~ /netbsd/) {
74 root 1.75 # netbsd is totally broken (pthreads are incompatible with ucontext or
75 root 1.69 # other stack switching mechanisms) therefore, default to pthread -
76 root 1.75 # hey, it might actually work, with some hacks.
77 root 1.62 $iface = "p";
78 root 1.50
79 root 1.83 if (!$pthread) {
80 root 1.75 # uh-oh
81     print <<EOF;
82    
83     ***
84     *** WARNING: Your platform is known to have broken pthreads, which are
85     *** required for Coro because your platform is known to have broken
86     *** ucontext and setjmp/longjmp functions as well, which are broken
87     *** because your pthread library is broken. D'oh.
88     ***
89     *** Coro will try to fight this vicious circle of breakage, but YMMV. If
90     *** Coro fails, try to recompile your perl with -lpthread, which will work
91     *** around some of the pthread bugs. (You do not have to enable ithreads).
92     ***
93    
94     EOF
95     # ugh, pthreads need to be linked into the main program :/
96     $iface = $iface_asm || "s";
97     }
98    
99 root 1.73 } elsif ($^O =~ /(openbsd|mirbsd)/) {
100 root 1.81 # mirbsd seems to be bug-to-bug compatible openbsd fork,
101 root 1.73 # with the name change being the biggest difference.
102 root 1.83 if (!$pthread) {
103 root 1.82 # asm seems to work, setjmp might, ucontext is missing,
104     # threads lets not talk about
105 root 1.81 # try setjmp/longjmp on 4.4, but pthread on earlier
106     $iface = $iface_asm || ($Config{osvers} >= 4.4 ? "s" : "p");
107     } else {
108     # seems newer openbsd platforms have marginally working pthreads, but
109     # their pthreads break sigaltstack - reading the sigaltstack sources
110     # again shows how fundamentally clueless those people are (if no thread
111     # has ever been created, then the program is bound to a kernel-scheduled
112     # entity. get that? GET THAT?)
113     $iface = "p";
114     }
115 root 1.71
116 root 1.8 } elsif ($^O =~ /solaris/) {
117 root 1.66 # setjmp, ucontext seem to work, as well as asm
118     $iface = $iface_asm || "s";
119 root 1.50
120 root 1.25 } elsif ($^O =~ /darwin/) {
121 root 1.69 # assembler doesn't support .type
122     # ucontext is of course totally broken (it just crashes)
123     # surprisingly, pthreads seem to work
124 root 1.25 $iface = "s";
125 root 1.50
126 root 1.60 } elsif ($^O =~ /dragonfly/) {
127     # ucontext is totally broken on dragonfly bsd:
128     # Fatal error 'siglongjmp()ing between thread contexts is undefined by POSIX 1003.1
129     $iface = "s";
130    
131 root 1.40 } elsif (-e "/usr/include/ucontext.h") { # shame on this heuristic
132 root 1.3 $iface = "u";
133 root 1.50
134 root 1.2 } else {
135 root 1.3 $iface = "s";
136 root 1.2 }
137    
138 root 1.44 print <<EOF;
139 root 1.2
140 root 1.87 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
141 root 1.38
142 root 1.40 Coro can use a number of methods to implement coroutines at the C
143     level. The default chosen is based on your current confguration and is
144     correct in most cases, but you still can chose between these alternatives:
145 root 1.34
146     u The unix 'ucontext.h' functions are relatively new and not implemented
147 root 1.48 or well-tested in older unices. They allow very fast coroutine creation
148 root 1.78 and reasonably fast switching. They are, however, usually slower than
149 root 1.63 the other alternatives due to an extra syscall done by swapcontext. And
150     while nominally most portable (it's the only POSIX-standardised
151     interface for coroutines), ucontext functions are, as usual, broken on
152     most/all BSDs.
153 root 1.4
154     s If the ucontext functions are not working or you don't want
155 root 1.48 to use them for other reasons you can try a workaround using
156     setjmp/longjmp/sigaltstack (also standard unix functions). Coroutine
157 root 1.49 creation is rather slow, but switching is very fast (often much faster
158     than with the ucontext functions). Unfortunately, glibc-2.1 and
159     below don't even feature a working sigaltstack. You cannot use this
160 root 1.63 implementation if some other code uses SIGUSR2 or you plan to create
161     coroutines from an alternative signal stack, as both are being used for
162     coroutine creation.
163 root 1.49
164 root 1.63 a Handcoded assembly. This is the fastest and most compatible method,
165 root 1.53 with the least side effects, if it works, that is. It has been tested
166     on GNU/Linux x86 and x86_64 systems and should work on all x86/x86_64
167 root 1.56 systems using the SVR ELF ABI (it is also reported to be working on
168     Strawberry Perl for Windows using MinGW). This is the recommended
169 root 1.63 method on supported platforms. When it doesn't work, use another
170     method, such as (s)etjmp/longjmp.
171 root 1.4
172 root 1.38 l GNU/Linux. Very old GNU/Linux systems (glibc-2.1 and below) need
173 root 1.48 this hack. Since it is very linux-specific it is also quite fast and
174     recommended even for newer versions; when it works, that is (currently
175     x86 and a few others only. If it compiles, it's usually ok). Newer
176 root 1.63 glibc versions (>= 2.5) stop working with this implementation however.
177 root 1.14
178 root 1.63 i IRIX. For some reason, SGI really does not like to follow POSIX (does
179     that surprise you?), so this workaround might be needed (it's fast),
180     although [s] and [u] should also work now.
181 root 1.2
182 root 1.56 w Microsoft Windows. Try this on Microsoft Windows when using Cygwin or
183     the MSVC compilers (e.g. ActiveState Perl, but see "a" for Strawberry
184     Perl), although, as there is no standard on how to do this under
185 root 1.58 windows, different environments might work differently. Doh.
186 root 1.34
187 root 1.86 f Microsoft Windows. Try this on Microsoft Windows if w fails. It is slower
188     and uses a lot more memory, but should be working all the time.
189    
190 root 1.64 p Use pthread API. Try to avoid this option, it was only created to
191     make a point about the programming language shootout. It is unlikely
192     to work with perls that have windows process emulation enabled ("perl
193     threads"). It is also likely the slowest method of implementing
194     coroutines. It might work fine as a last resort, however, as the
195     pthread API is slightly better tested than ucontext functions for
196     example. Of course, not on BSDs, who usually have very broken pthread
197     implementations.
198 root 1.63
199     Coro tries hard to come up with a suitable default for most systems,
200     so pressing return at the prompt usually does the right thing. If you
201     experience problems (e.g. make test fails) then you should experiment with
202     this setting.
203 root 1.28
204 root 1.2 EOF
205 root 1.3
206     retry:
207 root 1.14
208 root 1.44 my $r = prompt "Use which implementation,\n" .
209 root 1.86 "<s>etjmp, <u>ctx, <a>sm, <i>rix, <l>inux, <p>threads, <w>indows, <f>iber?",
210 root 1.44 $iface;
211     $iface = lc $1 if $r =~ /(\S)/;
212    
213     if ($iface eq "u") {
214     $DEFINE .= " -DCORO_UCONTEXT";
215     print "\nUsing ucontext implementation\n\n";
216 root 1.58 conftest ("TEST_makecontext");
217 root 1.44 } elsif ($iface eq "s") {
218     $DEFINE .= " -DCORO_SJLJ";
219     print "\nUsing setjmp/longjmp/sigaltstack implementation\n\n";
220 root 1.58 conftest ("TEST_sigaltstack");
221 root 1.44 } elsif ($iface eq "l") {
222     $DEFINE .= " -DCORO_LINUX";
223     print "\nUsing linux-specific implementation\n\n";
224     } elsif ($iface eq "i") {
225     $DEFINE .= " -DCORO_IRIX";
226     print "\nUsing irix-specific implementation\n\n";
227     } elsif ($iface eq "w") {
228     $DEFINE .= " -DCORO_LOSER";
229     print "\nUsing windows-specific implementation\n\n";
230 root 1.86 } elsif ($iface eq "f") {
231     $DEFINE .= " -DCORO_FIBER";
232     print "\nUsing windows-specific fiber implementation\n\n";
233 root 1.49 } elsif ($iface eq "a") {
234     $DEFINE .= " -DCORO_ASM";
235 root 1.75 print "\nUsing handcoded assembler implementation\n\n";
236 root 1.55 } elsif ($iface eq "p") {
237     $DEFINE .= " -DCORO_PTHREAD";
238     @LIBS = ["-lpthread"];
239     print "\nUsing pthread implementation\n\n";
240 root 1.3 } else {
241 root 1.44 print "\nUnknown implementation \"$iface\"\n";
242     goto retry;
243 root 1.3 }
244 root 1.2
245 root 1.36 print <<EOF;
246    
247 root 1.87 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
248 root 1.38
249     Per-context stack size factor: Depending on your settings, Coro tries to
250 root 1.58 share the C stacks is creates as much as possible, but sometimes it needs
251     to allocate a new one. This setting controls the maximum size that gets
252     allocated, and should not be set too high, as memory and address space
253     still is wasted even if it's not fully used. The value entered will be
254 root 1.87 multiplied by sizeof(void *), which is usually 4 on 32-bit systems, and 8
255     on 64-bit systems.
256 root 1.37
257 root 1.38 A setting of 16384 (the default) therefore corresponds to a 64k..128k
258 root 1.40 stack, which usually is ample space (you might even want to try 8192 or
259 root 1.38 lower if your program creates many coroutines).
260 root 1.37
261 root 1.41 On systems supporting mmap and dynamic memory management, the actual
262     memory usually gets allocated on demand, but with many large stacks you
263 root 1.58 can still run out of address space on your typical 32 bit platform (not to
264     forget the pagetables).
265 root 1.41
266 root 1.40 Some perls (mostly threaded ones and perl compiled under linux 2.6) and
267     some programs (inefficient regexes can use a lot of stack space) may
268     need much, much more: If Coro segfaults with weird backtraces (e.g. in a
269     function prologue) or in t/10_bugs.t, you might want to increase this to
270     65536 or more.
271 root 1.36
272 root 1.47 The default should be fine, and can be changed at runtime with
273     Coro::State::cctx_stacksize.
274 root 1.41
275 root 1.36 EOF
276    
277 root 1.67 my $stacksize = $^O eq "linux" && $] < 5.008008 ? 128 * 1024 : 16384;
278    
279     $stacksize = prompt ("C stack size factor?", $stacksize);
280 root 1.46 $DEFINE .= " -DCORO_STACKSIZE=$stacksize";
281 root 1.36
282 root 1.87 print "using a stacksize of $stacksize * sizeof(void*)\n";
283 root 1.36
284 root 1.38 print <<EOF;
285    
286 root 1.87 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
287 root 1.38
288 root 1.58 Coro can optionally put a guard area before each stack segment: When the
289 root 1.41 stack is too small and the access is not too far outside the stack (i.e.
290     within the guard area), then the program will safely segfault instead of
291     running into other data. The cost is some additional overhead with is
292     usually negligible, and extra use of address space.
293    
294     The guard area size currently needs to be specified in pages (typical
295     pagesizes are 4k and 8k). The guard area is only enabled on a few
296     hardcoded architectures and is ignored on others. The actual preprocessor
297     expression disables this feature if:
298    
299     !__i386 && !__x86_64 && !__powerpc && !__m68k \
300     && !__alpha && !__mips && !__sparc64
301    
302     The default, as usual, should be just fine.
303    
304     EOF
305    
306 root 1.87 my $guardpages = prompt ("Number of guard pages (0 disables)?", "4");
307     $DEFINE .= " -DCORO_GUARDPAGES=$guardpages";
308 root 1.41
309     print <<EOF;
310    
311 root 1.87 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
312 root 1.41
313 root 1.43 Coro can tell valgrind about its stacks and so reduce spurious warnings
314     where valgrind would otherwise complain about possible stack switches.
315    
316 root 1.58 Enabling this does not incur noticable runtime or memory overhead, but it
317 root 1.43 requires that you have the <valgrind/valgrind.h> header file available.
318    
319 root 1.58 Valgrind support is completely optional, so disabling it is the safe
320     choice.
321 root 1.43
322     EOF
323    
324 root 1.45 my $valgrind = prompt ("Enable valgrind support (y/n)?",
325 root 1.44 -r "/usr/include/valgrind/valgrind.h" ? "y" : "n");
326 root 1.46 $DEFINE .= " -DCORO_USE_VALGRIND=1" if $valgrind =~ /[yY]/;
327 root 1.43
328 root 1.44
329     print <<EOF;
330    
331 root 1.87 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
332 root 1.44
333     Coro can use (or even trick) some perl functions into doing what it needs
334     instead of relying on (some) of its own functions. This might increase
335     chances that it compiles and works, but it could just as well result in
336     memory leaks, crashes or silent data corruption. It certainly does result
337 root 1.51 in slightly slower speed and higher memory consumption, though, so YOU
338 root 1.58 SHOULD ENABLE THIS OPTION ONLY AS A LAST RESORT.
339 root 1.44
340     EOF
341    
342 root 1.45 my $use_internals = prompt ("Prefer perl functions over coro functions (y/n)?", "n");
343 root 1.46 $DEFINE .= " -DCORO_PREFER_PERL_FUNCTIONS=1" if $use_internals =~ /[yY]/;
344 root 1.44
345 root 1.43 print <<EOF;
346    
347 root 1.87 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
348 root 1.43
349 root 1.78 Coro can use a simple JIT compiler to compile a part of the thread switch
350 root 1.79 function at runtime. On perls with windows process emulation (most!),
351     this results in a 50% speed improvement. On sane perls, the gain is much
352 root 1.80 less, usually around 5%. If you enable this option, then the JIT will
353     be enabled, on compatible operating systems and CPUs (currently only
354     x86/amd64 on certain unix clones). Otherwise, it will be disabled. It
355     should be safe to leave on - this setting is only here so you can switch
356     it off in case of problems.
357 root 1.78
358 root 1.85 Note that some broken kernels (often calling themselves "hardened") break
359     all JIT generation by manipulating some system calls. If you get bus
360     errors or segmentation faults immediately when the JIT is enabled but not
361     without, then note that disabling the JIT only fixes some symptoms, not
362     the underlying problem, and you might run into other problems later.
363    
364 root 1.78 EOF
365    
366     my $orgasm = $ENV{CORO_JIT} || "y";
367     $orgasm = prompt ("Try to use the JIT compiler, if available?", $orgasm);
368     $DEFINE .= " -DCORO_JIT=1" if $orgasm =~ /[yY]/;
369    
370     print <<EOF;
371    
372 root 1.87 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
373 root 1.78
374 root 1.70 Coro has experimental support for cloning states. This can be used
375     to implement a scheme-like call/cc. However, this doesn't add to the
376 root 1.76 expressiveness in general, and is likely perl-version specific (and perl
377     5.12 deliberately removed support for it). As such, it is disabled by
378     default. Enable it when you want to play around with it, but note that it
379     isn't supported, and unlikely ever will be. It exists mainly to prove that
380     it could be done - if only it were useful for something.
381 root 1.70
382     EOF
383    
384     my $masturbate = $ENV{CORO_CLONE} || "n";
385     $masturbate = prompt ("Implement Coro::State->clone method (y/n)?", $masturbate);
386     $DEFINE .= " -DCORO_CLONE=1" if $masturbate =~ /[yY]/;
387    
388     print <<EOF;
389    
390 root 1.87 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
391 root 1.70
392 root 1.38 EOF
393 root 1.36
394 root 1.1 WriteMakefile(
395 root 1.2 NAME => "Coro::State",
396     VERSION_FROM => "State.pm",
397     DEFINE => $DEFINE,
398 root 1.55 LIBS => @LIBS,
399 root 1.2 DIR => [],
400 root 1.62 depend => {
401 root 1.78 "State.c" => "state.h clone.c ecb.h libcoro/coro.h libcoro/coro.c",
402 root 1.62 },
403 root 1.1 );
404 root 1.2
405 root 1.15 sub conftest {
406     my $type = shift;
407    
408     print "\nTrying to detect stack growth direction (for $type)\n";
409     print "You might see some warnings, this should not concern you.\n\n";
410     system "$Config{cc} $Config{ccflags} -D$type libcoro/conftest.c";
411    
412     my $res = qx<./a.out>;
413     $res =~ s/\s+$//;
414     my ($sp, $ss) = split /,/, $res;
415    
416     print "\n\n*****************************************************************************\n";
417     print "If the testsuite fails PLEASE provide the following information\n";
418 root 1.32 print "to Marc Lehmann <schmorp\@schmorp.de>: operating system name, version,\n";
419 root 1.15 print "architecture name and this string '$sp|$ss'. Thanks a lot!\n";#d#
420     print "*****************************************************************************\n\n";
421    
422     unlink "a.out";
423 root 1.17 unlink "conftestval";
424 root 1.15 }
425 root 1.2