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