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