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