1 |
use 5.005; |
2 |
|
3 |
use strict; |
4 |
use ExtUtils::MakeMaker; |
5 |
use Config; |
6 |
|
7 |
$|=1; |
8 |
|
9 |
my $DEFINE; |
10 |
my @LIBS = []; |
11 |
|
12 |
print <<EOF; |
13 |
|
14 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
15 |
|
16 |
Coro has a number of configuration options. Due to its maturity, the |
17 |
defaults that Coro chooses are usually fine, so you can decide to skip |
18 |
these questions. Only if something went wrong you should select 'n' |
19 |
here and manually configure Coro, and, of course, report this to the |
20 |
maintainer :) |
21 |
|
22 |
EOF |
23 |
|
24 |
if (prompt ("Skip further questions and use defaults (y/n)?", "y") =~ /[yY]/) { |
25 |
$ENV{PERL_MM_USE_DEFAULT} = 1; |
26 |
} |
27 |
|
28 |
|
29 |
$DEFINE .= " -DHAVE_MMAP" if $Config{d_mmap} eq "define" && $Config{d_munmap} eq "define"; |
30 |
|
31 |
my $iface; |
32 |
|
33 |
if (exists $ENV{CORO_INTERFACE}) { |
34 |
$iface = $ENV{CORO_INTERFACE}; |
35 |
|
36 |
} elsif ($^O =~ /win32/i or $^O =~ /cygwin/ or $^O =~ /mswin/) { |
37 |
$iface = 'w'; |
38 |
|
39 |
} elsif ($^O =~ /irix/) { |
40 |
$iface = "i"; |
41 |
|
42 |
} elsif ($^O =~ /linux/) { |
43 |
# default to assembly on x86 and x86_64, and setjmp on others |
44 |
$iface = $Config{archname} =~ /^(i[3456]86|amd64|x86_64)-/ && $Config{optimize} =~ /-O/ ? "a" : "s"; |
45 |
|
46 |
} elsif ($^O =~ /(free|net|open)bsd/) { |
47 |
# FreeBSD 4.x has ucontext.h but no makecontext et al. (see BUGS section of |
48 |
# man context). Assume the same problem for all other BSDs. |
49 |
|
50 |
# default to assembly on x86 and x86_64, and setjmp on others |
51 |
$iface = $Config{archname} =~ /^(i[3456]86|amd64|x86_64)-/ && $Config{optimize} =~ /-O/ ? "a" : "s"; |
52 |
|
53 |
} elsif ($^O =~ /solaris/) { |
54 |
$iface = "s"; |
55 |
|
56 |
} elsif ($^O =~ /darwin/) { |
57 |
$iface = "s"; |
58 |
|
59 |
} elsif (-e "/usr/include/ucontext.h") { # shame on this heuristic |
60 |
$iface = "u"; |
61 |
|
62 |
} else { |
63 |
$iface = "s"; |
64 |
} |
65 |
|
66 |
print <<EOF; |
67 |
|
68 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
69 |
|
70 |
Coro can use a number of methods to implement coroutines at the C |
71 |
level. The default chosen is based on your current confguration and is |
72 |
correct in most cases, but you still can chose between these alternatives: |
73 |
|
74 |
u The unix 'ucontext.h' functions are relatively new and not implemented |
75 |
or well-tested in older unices. They allow very fast coroutine creation |
76 |
and reasonably fast switching, and, most importantly, are very stable. |
77 |
It is, however, usually slower than the other alternatives due to an |
78 |
extra syscall done by swapcontext. |
79 |
|
80 |
s If the ucontext functions are not working or you don't want |
81 |
to use them for other reasons you can try a workaround using |
82 |
setjmp/longjmp/sigaltstack (also standard unix functions). Coroutine |
83 |
creation is rather slow, but switching is very fast (often much faster |
84 |
than with the ucontext functions). Unfortunately, glibc-2.1 and |
85 |
below don't even feature a working sigaltstack. You cannot use this |
86 |
implementation if some other code uses SIGUSR2 or you plan to |
87 |
create coroutines from an alternative signal stack, as both are being |
88 |
used for coroutine creation. |
89 |
|
90 |
a Handcoded assembly. This is the fastest and most compatible method |
91 |
with the least side effects, if it works, that is. It has been tested |
92 |
on GNU/Linux x86 and x86_64 systems and should work on all x86/x86_64 |
93 |
systems using the SVR ELF ABI (it is also reported to be working on |
94 |
Strawberry Perl for Windows using MinGW). This is the recommended |
95 |
method on supported platforms. Note that you usually have to compile |
96 |
this module with optimisation enabled for this method to work, and |
97 |
also more esoteric switches such as -fomit-leaf-frame-pointer might be |
98 |
required. When i doubt, use another method, such as (s)etjmp/longjmp. |
99 |
|
100 |
l GNU/Linux. Very old GNU/Linux systems (glibc-2.1 and below) need |
101 |
this hack. Since it is very linux-specific it is also quite fast and |
102 |
recommended even for newer versions; when it works, that is (currently |
103 |
x86 and a few others only. If it compiles, it's usually ok). Newer |
104 |
glibc versions (>= 2.5) stop working with this implementation again. |
105 |
|
106 |
i IRIX. For some reason, SGI really does not like to follow the single |
107 |
unix specification (does that surprise you?), so this workaround might |
108 |
be needed (it's fast), although [s] and [u] should also work now. |
109 |
|
110 |
w Microsoft Windows. Try this on Microsoft Windows when using Cygwin or |
111 |
the MSVC compilers (e.g. ActiveState Perl, but see "a" for Strawberry |
112 |
Perl), although, as there is no standard on how to do this under |
113 |
windows, different enviroments might work differently. Doh. |
114 |
|
115 |
p Use pthread API. Try to avoid this option, it was only created to make |
116 |
a point about the programming language shootout and might leak threads. |
117 |
It might work fine as a last resort, however. |
118 |
|
119 |
For most systems, the default chosen should be OK. If you experience |
120 |
problems then you should experiment with this setting and/or turn |
121 |
optimisations on or off (make OPTIMIZE=-O0). |
122 |
|
123 |
EOF |
124 |
|
125 |
retry: |
126 |
|
127 |
my $r = prompt "Use which implementation,\n" . |
128 |
"<s>et/longjump, <u>context, <a>ssembly, <i>rix, <l>inux or <w>indows?", |
129 |
$iface; |
130 |
$iface = lc $1 if $r =~ /(\S)/; |
131 |
|
132 |
if ($iface eq "u") { |
133 |
$DEFINE .= " -DCORO_UCONTEXT"; |
134 |
print "\nUsing ucontext implementation\n\n"; |
135 |
conftest("TEST_makecontext"); |
136 |
} elsif ($iface eq "s") { |
137 |
$DEFINE .= " -DCORO_SJLJ"; |
138 |
print "\nUsing setjmp/longjmp/sigaltstack implementation\n\n"; |
139 |
conftest("TEST_sigaltstack"); |
140 |
} elsif ($iface eq "l") { |
141 |
$DEFINE .= " -DCORO_LINUX"; |
142 |
print "\nUsing linux-specific implementation\n\n"; |
143 |
} elsif ($iface eq "i") { |
144 |
$DEFINE .= " -DCORO_IRIX"; |
145 |
print "\nUsing irix-specific implementation\n\n"; |
146 |
} elsif ($iface eq "w") { |
147 |
$DEFINE .= " -DCORO_LOSER"; |
148 |
print "\nUsing windows-specific implementation\n\n"; |
149 |
} elsif ($iface eq "a") { |
150 |
$DEFINE .= " -DCORO_ASM"; |
151 |
print "\nUsing handcoded assembly implementation\n\n"; |
152 |
} elsif ($iface eq "p") { |
153 |
$DEFINE .= " -DCORO_PTHREAD"; |
154 |
@LIBS = ["-lpthread"]; |
155 |
print "\nUsing pthread implementation\n\n"; |
156 |
} else { |
157 |
print "\nUnknown implementation \"$iface\"\n"; |
158 |
goto retry; |
159 |
} |
160 |
|
161 |
print <<EOF; |
162 |
|
163 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
164 |
|
165 |
Per-context stack size factor: Depending on your settings, Coro tries to |
166 |
share the C stack as much as possible, but sometimes it needs to allocate |
167 |
a new one. This setting controls the maximum size that gets allocated, |
168 |
and should not be set too high, as memory and address space still is |
169 |
wasted even if it's not fully used. The value entered will be multiplied |
170 |
by sizeof(long), which is usually 4 on 32-bit systems, and 8 on 64-bit |
171 |
systems. |
172 |
|
173 |
A setting of 16384 (the default) therefore corresponds to a 64k..128k |
174 |
stack, which usually is ample space (you might even want to try 8192 or |
175 |
lower if your program creates many coroutines). |
176 |
|
177 |
On systems supporting mmap and dynamic memory management, the actual |
178 |
memory usually gets allocated on demand, but with many large stacks you |
179 |
can still run out of address space on your typical 32 bit platform. |
180 |
|
181 |
Some perls (mostly threaded ones and perl compiled under linux 2.6) and |
182 |
some programs (inefficient regexes can use a lot of stack space) may |
183 |
need much, much more: If Coro segfaults with weird backtraces (e.g. in a |
184 |
function prologue) or in t/10_bugs.t, you might want to increase this to |
185 |
65536 or more. |
186 |
|
187 |
The default should be fine, and can be changed at runtime with |
188 |
Coro::State::cctx_stacksize. |
189 |
|
190 |
EOF |
191 |
|
192 |
my $stacksize = prompt ("C stack size factor?", "16384"); |
193 |
$DEFINE .= " -DCORO_STACKSIZE=$stacksize"; |
194 |
|
195 |
print "using a stacksize of $stacksize * sizeof(long)\n"; |
196 |
|
197 |
print <<EOF; |
198 |
|
199 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
200 |
|
201 |
Coro can optionally put a guard area before each stack segment. When the |
202 |
stack is too small and the access is not too far outside the stack (i.e. |
203 |
within the guard area), then the program will safely segfault instead of |
204 |
running into other data. The cost is some additional overhead with is |
205 |
usually negligible, and extra use of address space. |
206 |
|
207 |
The guard area size currently needs to be specified in pages (typical |
208 |
pagesizes are 4k and 8k). The guard area is only enabled on a few |
209 |
hardcoded architectures and is ignored on others. The actual preprocessor |
210 |
expression disables this feature if: |
211 |
|
212 |
!__i386 && !__x86_64 && !__powerpc && !__m68k \ |
213 |
&& !__alpha && !__mips && !__sparc64 |
214 |
|
215 |
The default, as usual, should be just fine. |
216 |
|
217 |
EOF |
218 |
|
219 |
my $stackguard = prompt ("Number of guard pages (0 disables)?", "4"); |
220 |
$DEFINE .= " -DCORO_STACKGUARD=$stackguard"; |
221 |
|
222 |
print <<EOF; |
223 |
|
224 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
225 |
|
226 |
Coro can tell valgrind about its stacks and so reduce spurious warnings |
227 |
where valgrind would otherwise complain about possible stack switches. |
228 |
|
229 |
Enabling this does not incur visible runtime or memory overhead, but it |
230 |
requires that you have the <valgrind/valgrind.h> header file available. |
231 |
|
232 |
Valgrind support is completely optional, so the default of disabling it is |
233 |
the safe choice. |
234 |
|
235 |
EOF |
236 |
|
237 |
my $valgrind = prompt ("Enable valgrind support (y/n)?", |
238 |
-r "/usr/include/valgrind/valgrind.h" ? "y" : "n"); |
239 |
$DEFINE .= " -DCORO_USE_VALGRIND=1" if $valgrind =~ /[yY]/; |
240 |
|
241 |
|
242 |
print <<EOF; |
243 |
|
244 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
245 |
|
246 |
Coro can use (or even trick) some perl functions into doing what it needs |
247 |
instead of relying on (some) of its own functions. This might increase |
248 |
chances that it compiles and works, but it could just as well result in |
249 |
memory leaks, crashes or silent data corruption. It certainly does result |
250 |
in slightly slower speed and higher memory consumption, though, so YOU |
251 |
SHOULD ENABLE IT ONLY AS A LAST RESORT. |
252 |
|
253 |
EOF |
254 |
|
255 |
my $use_internals = prompt ("Prefer perl functions over coro functions (y/n)?", "n"); |
256 |
$DEFINE .= " -DCORO_PREFER_PERL_FUNCTIONS=1" if $use_internals =~ /[yY]/; |
257 |
|
258 |
print <<EOF; |
259 |
|
260 |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** |
261 |
|
262 |
EOF |
263 |
|
264 |
WriteMakefile( |
265 |
NAME => "Coro::State", |
266 |
VERSION_FROM => "State.pm", |
267 |
DEFINE => $DEFINE, |
268 |
LIBS => @LIBS, |
269 |
DIR => [], |
270 |
); |
271 |
|
272 |
sub conftest { |
273 |
my $type = shift; |
274 |
|
275 |
print "\nTrying to detect stack growth direction (for $type)\n"; |
276 |
print "You might see some warnings, this should not concern you.\n\n"; |
277 |
system "$Config{cc} $Config{ccflags} -D$type libcoro/conftest.c"; |
278 |
|
279 |
my $res = qx<./a.out>; |
280 |
$res =~ s/\s+$//; |
281 |
my ($sp, $ss) = split /,/, $res; |
282 |
|
283 |
print "\n\n*****************************************************************************\n"; |
284 |
print "If the testsuite fails PLEASE provide the following information\n"; |
285 |
print "to Marc Lehmann <schmorp\@schmorp.de>: operating system name, version,\n"; |
286 |
print "architecture name and this string '$sp|$ss'. Thanks a lot!\n";#d# |
287 |
print "*****************************************************************************\n\n"; |
288 |
|
289 |
unlink "a.out"; |
290 |
unlink "conftestval"; |
291 |
} |
292 |
|