Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 587 lines (471 sloc) 13.888 kB
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
1 /* -----------------------------------------------------------------------------
7f309f1 [project @ 1999-02-05 16:02:18 by simonm]
simonm authored
2 *
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
3 * (c) The GHC Team, 1998-2002
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
4 *
5 * Main function for a standalone Haskell program.
6 *
7 * ---------------------------------------------------------------------------*/
8
fe07f05 Addition of PAPI to RTS
mrchebas@gmail.com authored
9 // PAPI uses caddr_t, which is not POSIX
10 // #include "PosixSource.h"
11
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
12 #include "Rts.h"
13 #include "RtsAPI.h"
14 #include "RtsUtils.h"
15 #include "RtsFlags.h"
1621421 [project @ 2005-04-05 12:19:54 by simonmar]
simonmar authored
16 #include "OSThreads.h"
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
17 #include "Schedule.h" /* initScheduler */
18 #include "Stats.h" /* initStats */
b61f70c [project @ 2004-11-18 09:56:07 by tharris]
tharris authored
19 #include "STM.h" /* initSTM */
c6ab4bf [project @ 1999-08-25 16:11:43 by simonmar]
simonmar authored
20 #include "Signals.h"
eba7b66 Merge the smp and threaded RTS ways
Simon Marlow authored
21 #include "RtsSignals.h"
80a766f Add support for the IO manager thread on Windows
Simon Marlow authored
22 #include "ThrIOManager.h"
557947d [project @ 2003-02-22 04:51:50 by sof]
sof authored
23 #include "Timer.h" /* startTimer, stopTimer */
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
24 #include "Weak.h"
ec48c5a [project @ 1999-01-21 10:31:41 by simonm]
simonm authored
25 #include "Ticky.h"
de89640 [project @ 2000-03-08 17:48:24 by simonmar]
simonmar authored
26 #include "StgRun.h"
51464cf [project @ 2000-03-14 14:34:47 by sewardj]
sewardj authored
27 #include "Prelude.h" /* fixupRTStoPreludeRefs */
c850515 [project @ 2000-10-06 15:35:47 by simonmar]
simonmar authored
28 #include "HsFFI.h"
29 #include "Linker.h"
fbbed91 [project @ 2002-06-26 08:18:38 by stolz]
stolz authored
30 #include "ThreadLabels.h"
423d477 [project @ 2004-08-13 13:04:50 by simonmar]
simonmar authored
31 #include "BlockAlloc.h"
5a2769f New tracing interface
Simon Marlow authored
32 #include "Trace.h"
52589e0 Add shared Typeable support
Esa Ilari Vuokko authored
33 #include "RtsTypeable.h"
ab0e778 Split GC.c, and move storage manager into sm/ directory
Simon Marlow authored
34 #include "Stable.h"
b33fa57 Make all needed prototypes visible to avoid warnings
sven.panne@aedion.de authored
35 #include "Hpc.h"
1d02661 Move file locking into the RTS, fixing #629, #1109
Simon Marlow authored
36 #include "FileLock.h"
c850515 [project @ 2000-10-06 15:35:47 by simonmar]
simonmar authored
37
39a9917 [project @ 2000-11-01 11:41:47 by simonmar]
simonmar authored
38 #if defined(RTS_GTK_FRONTPANEL)
39 #include "FrontPanel.h"
40 #endif
41
b9d8ddb [project @ 2000-04-03 15:54:49 by simonmar]
simonmar authored
42 # include "Profiling.h"
cbeb99e Basic heap profile support without -prof
Simon Marlow authored
43
44 #if defined(PROFILING)
17315bf [project @ 1999-09-15 13:45:14 by simonmar]
simonmar authored
45 # include "ProfHeap.h"
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
46 # include "RetainerProfile.h"
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
47 #endif
48
1b28d4e [project @ 2000-01-13 14:33:57 by hwloidl]
hwloidl authored
49 #if defined(GRAN)
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
50 # include "GranSimRts.h"
51 #endif
52
53 #if defined(GRAN) || defined(PAR)
54 # include "ParallelRts.h"
1b28d4e [project @ 2000-01-13 14:33:57 by hwloidl]
hwloidl authored
55 #endif
56
57 #if defined(PAR)
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
58 # include "Parallel.h"
59 # include "LLC.h"
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
60 #endif
61
de6c8e5 Remove the Windows Async IO Manager completely in THREADED_RTS mode
Simon Marlow authored
62 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
5b4f5a6 [project @ 2003-02-21 05:34:12 by sof]
sof authored
63 #include "win32/AsyncIO.h"
64 #endif
65
7457757 [project @ 2002-07-17 09:21:48 by simonmar]
simonmar authored
66 #include <stdlib.h>
67
8fb92f9 [project @ 2003-09-24 10:32:11 by simonmar]
simonmar authored
68 #ifdef HAVE_TERMIOS_H
69 #include <termios.h>
70 #endif
71 #ifdef HAVE_SIGNAL_H
72 #include <signal.h>
73 #endif
addff19 @simonmar FIX part of #2301, and #1619
simonmar authored
74 #ifdef HAVE_UNISTD_H
75 #include <unistd.h>
76 #endif
8fb92f9 [project @ 2003-09-24 10:32:11 by simonmar]
simonmar authored
77
fe07f05 Addition of PAPI to RTS
mrchebas@gmail.com authored
78 #if USE_PAPI
79 #include "Papi.h"
80 #endif
81
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
82 // Count of how many outstanding hs_init()s there have been.
83 static int hs_init_count = 0;
674ad01 [project @ 2000-04-10 14:28:14 by sewardj]
sewardj authored
84
8fb92f9 [project @ 2003-09-24 10:32:11 by simonmar]
simonmar authored
85 // Here we save the terminal settings on the standard file
86 // descriptors, if we need to change them (eg. to support NoBuffering
87 // input).
22f5f69 [project @ 2003-09-25 15:14:41 by panne]
panne authored
88 static void *saved_termios[3] = {NULL,NULL,NULL};
89
90 void*
91 __hscore_get_saved_termios(int fd)
92 {
e8ffdfe [project @ 2003-09-26 12:12:35 by panne]
panne authored
93 return (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) ?
22f5f69 [project @ 2003-09-25 15:14:41 by panne]
panne authored
94 saved_termios[fd] : NULL;
95 }
96
97 void
98 __hscore_set_saved_termios(int fd, void* ts)
99 {
e8ffdfe [project @ 2003-09-26 12:12:35 by panne]
panne authored
100 if (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) {
22f5f69 [project @ 2003-09-25 15:14:41 by panne]
panne authored
101 saved_termios[fd] = ts;
102 }
103 }
8fb92f9 [project @ 2003-09-24 10:32:11 by simonmar]
simonmar authored
104
4bd153f [project @ 2004-09-12 12:12:18 by panne]
panne authored
105 /* -----------------------------------------------------------------------------
106 Initialise floating point unit on x86 (currently disabled. why?)
8a1f602 [project @ 2005-04-27 09:39:27 by simonmar]
simonmar authored
107 (see comment in ghc/compiler/nativeGen/MachInstrs.lhs).
4bd153f [project @ 2004-09-12 12:12:18 by panne]
panne authored
108 -------------------------------------------------------------------------- */
109
110 #define X86_INIT_FPU 0
111
112 #if X86_INIT_FPU
113 static void
114 x86_init_fpu ( void )
115 {
116 __volatile unsigned short int fpu_cw;
117
118 // Grab the control word
119 __asm __volatile ("fnstcw %0" : "=m" (fpu_cw));
120
121 #if 0
122 printf("fpu_cw: %x\n", fpu_cw);
123 #endif
124
125 // Set bits 8-9 to 10 (64-bit precision).
126 fpu_cw = (fpu_cw & 0xfcff) | 0x0200;
127
128 // Store the new control word back
129 __asm __volatile ("fldcw %0" : : "m" (fpu_cw));
130 }
423d477 [project @ 2004-08-13 13:04:50 by simonmar]
simonmar authored
131 #endif
132
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
133 /* -----------------------------------------------------------------------------
134 Starting up the RTS
135 -------------------------------------------------------------------------- */
674ad01 [project @ 2000-04-10 14:28:14 by sewardj]
sewardj authored
136
137 void
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
138 hs_init(int *argc, char **argv[])
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
139 {
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
140 hs_init_count++;
141 if (hs_init_count > 1) {
142 // second and subsequent inits are ignored
143 return;
b240523 [project @ 2001-12-06 07:07:12 by sof]
sof authored
144 }
5c089c2 [project @ 1999-03-03 19:10:37 by sof]
sof authored
145
1663532 calculate and report slop (wasted space at the end of blocks)
Simon Marlow authored
146 /* Initialise the stats department, phase 0 */
147 initStats0();
d526504 @igfoo When debugging, have the allocator help us a bit
igfoo authored
148
149 /* Next we do is grab the start time...just in case we're
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
150 * collecting timing statistics.
151 */
57c9c08 [project @ 2000-12-19 14:30:58 by simonmar]
simonmar authored
152 stat_startInit();
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
153
1663532 calculate and report slop (wasted space at the end of blocks)
Simon Marlow authored
154 #if defined(DEBUG)
155 /* Start off by initialising the allocator debugging so we can
156 * use it anywhere */
157 initAllocator();
158 #endif
159
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
160 #ifdef PAR
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
161 /*
162 * The parallel system needs to be initialised and synchronised before
163 * the program is run.
164 */
165 startupParallelSystem(argv);
166
167 if (*argv[0] == '-') { /* Strip off mainPE flag argument */
168 argv++;
169 argc--;
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
170 }
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
171
172 argv[1] = argv[0]; /* ignore the nPEs argument */
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
173 argv++; argc--;
174 #endif
175
176 /* Set the RTS flags to default values. */
fe07f05 Addition of PAPI to RTS
mrchebas@gmail.com authored
177
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
178 initRtsFlagsDefaults();
179
180 /* Call the user hook to reset defaults, if present */
181 defaultsHook();
182
183 /* Parse the flags, separating the RTS flags from the programs args */
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
184 if (argc != NULL && argv != NULL) {
8bac478 @igfoo Implement the RTS side of GHC.Environment.getFullArgs
igfoo authored
185 setFullProgArgv(*argc,*argv);
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
186 setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
4e48260 [project @ 2005-02-03 10:57:06 by simonmar]
simonmar authored
187 setProgArgv(*argc,*argv);
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
188 }
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
189
1663532 calculate and report slop (wasted space at the end of blocks)
Simon Marlow authored
190 /* Initialise the stats department, phase 1 */
191 initStats1();
192
8db56c8 improvements to PAPI support
simonmar@microsoft.com authored
193 #ifdef USE_PAPI
194 papi_init();
195 #endif
196
5a2769f New tracing interface
Simon Marlow authored
197 /* initTracing must be after setupRtsFlags() */
198 initTracing();
199
1b28d4e [project @ 2000-01-13 14:33:57 by hwloidl]
hwloidl authored
200 #if defined(PAR)
201 /* NB: this really must be done after processing the RTS flags */
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
202 IF_PAR_DEBUG(verbose,
95ca6bf [project @ 2004-09-03 15:28:18 by simonmar]
simonmar authored
203 debugBelch("==== Synchronising system (%d PEs)\n", nPEs));
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
204 synchroniseSystem(); // calls initParallelSystem etc
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
205 #endif /* PAR */
206
f669261 [project @ 1999-11-02 15:05:38 by simonmar]
simonmar authored
207 /* initialise scheduler data structures (needs to be done before
208 * initStorage()).
209 */
210 initScheduler();
211
1b28d4e [project @ 2000-01-13 14:33:57 by hwloidl]
hwloidl authored
212 #if defined(GRAN)
213 /* And start GranSim profiling if required: */
214 if (RtsFlags.GranFlags.GranSimStats.Full)
215 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
216 #elif defined(PAR)
217 /* And start GUM profiling if required: */
218 if (RtsFlags.ParFlags.ParStats.Full)
219 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
220 #endif /* PAR || GRAN */
221
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
222 /* initialize the storage manager */
223 initStorage();
224
225 /* initialise the stable pointer table */
226 initStablePtrTable();
227
52589e0 Add shared Typeable support
Esa Ilari Vuokko authored
228 /* initialise the shared Typeable store */
229 initTypeableStore();
230
1d02661 Move file locking into the RTS, fixing #629, #1109
Simon Marlow authored
231 /* initialise file locking, if necessary */
232 #if !defined(mingw32_HOST_OS)
233 initFileLocking();
234 #endif
235
f7db2c3 [project @ 2004-09-22 16:37:23 by sof]
sof authored
236 #if defined(DEBUG)
fbbed91 [project @ 2002-06-26 08:18:38 by stolz]
stolz authored
237 /* initialise thread label table (tso->char*) */
238 initThreadLabelTable();
f7db2c3 [project @ 2004-09-22 16:37:23 by sof]
sof authored
239 #endif
fbbed91 [project @ 2002-06-26 08:18:38 by stolz]
stolz authored
240
de89640 [project @ 2000-03-08 17:48:24 by simonmar]
simonmar authored
241 initProfiling1();
242
e30e0fb [project @ 2001-11-27 01:51:23 by sof]
sof authored
243 /* start the virtual timer 'subsystem'. */
8d71be7 FIX #1623: disable the timer signal when the system is idle (threaded…
Simon Marlow authored
244 initTimer();
93db199 @igfoo new RTS flag: -V to modify the resolution of the RTS timer
igfoo authored
245 startTimer();
c6ab4bf [project @ 1999-08-25 16:11:43 by simonmar]
simonmar authored
246
c49a6ca [project @ 2003-04-01 15:05:13 by sof]
sof authored
247 #if defined(RTS_USER_SIGNALS)
4cc37e5 @igfoo Add an --install-signal-handlers=<yes|no> RTS flag; fixes trac #804
igfoo authored
248 if (RtsFlags.MiscFlags.install_signal_handlers) {
249 /* Initialise the user signal handler set */
250 initUserSignals();
251 /* Set up handler to run on SIGINT, etc. */
252 initDefaultHandlers();
253 }
e1dffdb [project @ 1999-09-13 11:02:08 by sof]
sof authored
254 #endif
5c089c2 [project @ 1999-03-03 19:10:37 by sof]
sof authored
255
de6c8e5 Remove the Windows Async IO Manager completely in THREADED_RTS mode
Simon Marlow authored
256 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
5b4f5a6 [project @ 2003-02-21 05:34:12 by sof]
sof authored
257 startupAsyncIO();
258 #endif
259
39a9917 [project @ 2000-11-01 11:41:47 by simonmar]
simonmar authored
260 #ifdef RTS_GTK_FRONTPANEL
261 if (RtsFlags.GcFlags.frontpanel) {
262 initFrontPanel();
263 }
264 #endif
265
4bd153f [project @ 2004-09-12 12:12:18 by panne]
panne authored
266 #if X86_INIT_FPU
267 x86_init_fpu();
423d477 [project @ 2004-08-13 13:04:50 by simonmar]
simonmar authored
268 #endif
269
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
270 /* Record initialization times */
57c9c08 [project @ 2000-12-19 14:30:58 by simonmar]
simonmar authored
271 stat_endInit();
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
272 }
273
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
274 // Compatibility interface
275 void
276 startupHaskell(int argc, char *argv[], void (*init_root)(void))
277 {
278 hs_init(&argc, &argv);
eab7055 [project @ 2005-04-15 05:29:48 by wolfgang]
wolfgang authored
279 if(init_root)
280 hs_add_root(init_root);
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
281 }
282
283
284 /* -----------------------------------------------------------------------------
de89640 [project @ 2000-03-08 17:48:24 by simonmar]
simonmar authored
285 Per-module initialisation
286
287 This process traverses all the compiled modules in the program
288 starting with "Main", and performing per-module initialisation for
289 each one.
290
291 So far, two things happen at initialisation time:
292
293 - we register stable names for each foreign-exported function
294 in that module. This prevents foreign-exported entities, and
295 things they depend on, from being garbage collected.
296
297 - we supply a unique integer to each statically declared cost
298 centre and cost centre stack in the program.
299
fb7a723 [project @ 2001-09-04 18:29:20 by ken]
ken authored
300 The code generator inserts a small function "__stginit_<module>" in each
301 module and calls the registration functions in each of the modules it
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
302 imports.
de89640 [project @ 2000-03-08 17:48:24 by simonmar]
simonmar authored
303
304 The init* functions are compiled in the same way as STG code,
305 i.e. without normal C call/return conventions. Hence we must use
306 StgRun to call this stuff.
307 -------------------------------------------------------------------------- */
308
309 /* The init functions use an explicit stack...
310 */
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
311 #define INIT_STACK_BLOCKS 4
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
312 static F_ *init_stack = NULL;
de89640 [project @ 2000-03-08 17:48:24 by simonmar]
simonmar authored
313
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
314 void
315 hs_add_root(void (*init_root)(void))
de89640 [project @ 2000-03-08 17:48:24 by simonmar]
simonmar authored
316 {
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
317 bdescr *bd;
95371b0 [project @ 2002-08-16 13:29:05 by simonmar]
simonmar authored
318 nat init_sp;
9367b24 @simonmar hs_add_root: use use rts_lock()/rts_unlock() for a bit of extra safety
simonmar authored
319 Capability *cap;
320
321 cap = rts_lock();
b5bafea [project @ 2000-04-03 16:28:08 by simonmar]
simonmar authored
322
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
323 if (hs_init_count <= 0) {
324 barf("hs_add_root() must be called after hs_init()");
325 }
326
423d477 [project @ 2004-08-13 13:04:50 by simonmar]
simonmar authored
327 /* The initialisation stack grows downward, with sp pointing
328 to the last occupied word */
329 init_sp = INIT_STACK_BLOCKS*BLOCK_SIZE_W;
03a9ff0 [project @ 2005-10-21 14:02:17 by simonmar]
simonmar authored
330 bd = allocGroup_lock(INIT_STACK_BLOCKS);
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
331 init_stack = (F_ *)bd->start;
423d477 [project @ 2004-08-13 13:04:50 by simonmar]
simonmar authored
332 init_stack[--init_sp] = (F_)stg_init_finish;
b5bafea [project @ 2000-04-03 16:28:08 by simonmar]
simonmar authored
333 if (init_root != NULL) {
423d477 [project @ 2004-08-13 13:04:50 by simonmar]
simonmar authored
334 init_stack[--init_sp] = (F_)init_root;
b5bafea [project @ 2000-04-03 16:28:08 by simonmar]
simonmar authored
335 }
336
eba7b66 Merge the smp and threaded RTS ways
Simon Marlow authored
337 cap->r.rSp = (P_)(init_stack + init_sp);
338 StgRun((StgFunPtr)stg_init, &cap->r);
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
339
03a9ff0 [project @ 2005-10-21 14:02:17 by simonmar]
simonmar authored
340 freeGroup_lock(bd);
674e4a4 [project @ 2003-01-30 10:19:07 by simonmar]
simonmar authored
341
d5934bb Haskell Program Coverage
andy@galois.com authored
342 startupHpc();
343
674e4a4 [project @ 2003-01-30 10:19:07 by simonmar]
simonmar authored
344 // This must be done after module initialisation.
345 // ToDo: make this work in the presence of multiple hs_add_root()s.
346 initProfiling2();
50c4d03 Enable -prof -threaded (#886)
Simon Marlow authored
347
9367b24 @simonmar hs_add_root: use use rts_lock()/rts_unlock() for a bit of extra safety
simonmar authored
348 rts_unlock(cap);
349
50c4d03 Enable -prof -threaded (#886)
Simon Marlow authored
350 // ditto.
351 #if defined(THREADED_RTS)
352 ioManagerStart();
353 #endif
de89640 [project @ 2000-03-08 17:48:24 by simonmar]
simonmar authored
354 }
dac8c9b [project @ 2000-03-09 11:49:34 by simonmar]
simonmar authored
355
681aad9 hs_exit()/shutdownHaskell(): wait for outstanding foreign calls to co…
Simon Marlow authored
356 /* ----------------------------------------------------------------------------
357 * Shutting down the RTS
358 *
359 * The wait_foreign parameter means:
360 * True ==> wait for any threads doing foreign calls now.
361 * False ==> threads doing foreign calls may return in the
362 * future, but will immediately block on a mutex.
363 * (capability->lock).
364 *
365 * If this RTS is a DLL that we're about to unload, then you want
366 * safe=True, otherwise the thread might return to code that has been
367 * unloaded. If this is a standalone program that is about to exit,
368 * then you can get away with safe=False, which is better because we
369 * won't hang on exit if there is a blocked foreign call outstanding.
370 *
371 ------------------------------------------------------------------------- */
d1a47b3 [project @ 1999-07-03 18:39:40 by sof]
sof authored
372
681aad9 hs_exit()/shutdownHaskell(): wait for outstanding foreign calls to co…
Simon Marlow authored
373 static void
374 hs_exit_(rtsBool wait_foreign)
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
375 {
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
376 if (hs_init_count <= 0) {
4ae1107 Turn the "too many hs_exit()s" fatal error into a warning
Simon Marlow authored
377 errorBelch("warning: too many hs_exit()s");
378 return;
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
379 }
380 hs_init_count--;
381 if (hs_init_count > 0) {
382 // ignore until it's the last one
383 return;
384 }
32a2762 [project @ 2002-05-11 00:16:11 by sof]
sof authored
385
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
386 /* start timing the shutdown */
387 stat_startExit();
388
addff19 @simonmar FIX part of #2301, and #1619
simonmar authored
389 OnExitHook();
390
ed12b70 @igfoo Free more things that we allocate
igfoo authored
391 #if defined(RTS_USER_SIGNALS)
4cc37e5 @igfoo Add an --install-signal-handlers=<yes|no> RTS flag; fixes trac #804
igfoo authored
392 if (RtsFlags.MiscFlags.install_signal_handlers) {
393 freeSignalHandlers();
394 }
ed12b70 @igfoo Free more things that we allocate
igfoo authored
395 #endif
396
80a766f Add support for the IO manager thread on Windows
Simon Marlow authored
397 #if defined(THREADED_RTS)
7a1f8fb Better control of the IO manager thread; improvements to deadlock che…
Simon Marlow authored
398 ioManagerDie();
399 #endif
400
8fcae51 [project @ 2004-02-27 13:35:16 by simonmar]
simonmar authored
401 /* stop all running tasks */
681aad9 hs_exit()/shutdownHaskell(): wait for outstanding foreign calls to co…
Simon Marlow authored
402 exitScheduler(wait_foreign);
8fcae51 [project @ 2004-02-27 13:35:16 by simonmar]
simonmar authored
403
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
404 #if defined(GRAN)
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
405 /* end_gr_simulation prints global stats if requested -- HWL */
406 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
407 end_gr_simulation();
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
408 #endif
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
409
410 /* stop the ticker */
557947d [project @ 2003-02-22 04:51:50 by sof]
sof authored
411 stopTimer();
8d71be7 FIX #1623: disable the timer signal when the system is idle (threaded…
Simon Marlow authored
412 exitTimer();
5ddee76 @catamorphism Lightweight ticky-ticky profiling
catamorphism authored
413
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
414 /* reset the standard file descriptors to blocking mode */
415 resetNonBlockingFd(0);
416 resetNonBlockingFd(1);
417 resetNonBlockingFd(2);
ee6369a [project @ 1999-11-09 10:46:25 by simonmar]
simonmar authored
418
8fb92f9 [project @ 2003-09-24 10:32:11 by simonmar]
simonmar authored
419 #if HAVE_TERMIOS_H
420 // Reset the terminal settings on the standard file descriptors,
421 // if we changed them. See System.Posix.Internals.tcSetAttr for
422 // more details, including the reason we termporarily disable
423 // SIGTTOU here.
424 {
425 int fd;
426 sigset_t sigset, old_sigset;
427 sigemptyset(&sigset);
428 sigaddset(&sigset, SIGTTOU);
429 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
430 for (fd = 0; fd <= 2; fd++) {
22f5f69 [project @ 2003-09-25 15:14:41 by panne]
panne authored
431 struct termios* ts = (struct termios*)__hscore_get_saved_termios(fd);
432 if (ts != NULL) {
433 tcsetattr(fd,TCSANOW,ts);
8fb92f9 [project @ 2003-09-24 10:32:11 by simonmar]
simonmar authored
434 }
435 }
436 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
437 }
438 #endif
439
dd4c28a [project @ 2000-03-31 03:09:35 by hwloidl]
hwloidl authored
440 #if defined(PAR)
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
441 /* controlled exit; good thread! */
442 shutdownParallelSystem(0);
443
444 /* global statistics in parallel system */
445 PAR_TICKY_PAR_END();
dd4c28a [project @ 2000-03-31 03:09:35 by hwloidl]
hwloidl authored
446 #endif
447
addff19 @simonmar FIX part of #2301, and #1619
simonmar authored
448 // uninstall signal handlers
449 resetDefaultHandlers();
450
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
451 /* stop timing the shutdown, we're about to print stats */
452 stat_endExit();
453
d5934bb Haskell Program Coverage
andy@galois.com authored
454 /* shutdown the hpc support (if needed) */
455 exitHpc();
456
71d6db2 fix profiling on Win32
Simon Marlow authored
457 // clean up things from the storage manager's point of view.
458 // also outputs the stats (+RTS -s) info.
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
459 exitStorage();
460
cf6b495 @igfoo Add freeScheduler/freeTaskManager and call it later than exitScheduler
igfoo authored
461 /* free the tasks */
462 freeScheduler();
463
52589e0 Add shared Typeable support
Esa Ilari Vuokko authored
464 /* free shared Typeable store */
465 exitTypeableStore();
466
1d02661 Move file locking into the RTS, fixing #629, #1109
Simon Marlow authored
467 /* free file locking tables, if necessary */
468 #if !defined(mingw32_HOST_OS)
469 freeFileLocking();
470 #endif
471
221e74f @igfoo Free various things we allocate
igfoo authored
472 /* free the stable pointer table */
9f2ceb4 Remember to free() memory on exit
Simon Marlow authored
473 exitStablePtrTable();
474
221e74f @igfoo Free various things we allocate
igfoo authored
475 #if defined(DEBUG)
476 /* free the thread label table */
477 freeThreadLabelTable();
478 #endif
479
39a9917 [project @ 2000-11-01 11:41:47 by simonmar]
simonmar authored
480 #ifdef RTS_GTK_FRONTPANEL
481 if (RtsFlags.GcFlags.frontpanel) {
482 stopFrontPanel();
483 }
484 #endif
485
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
486 #if defined(PROFILING)
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
487 reportCCSProfiling();
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
488 #endif
489
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
490 endProfiling();
bb276c3 freeProfiling1() needs to be after endProfiling()
Simon Marlow authored
491 freeProfiling1();
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
492
db61851 [project @ 2001-11-22 14:25:11 by simonmar]
simonmar authored
493 #ifdef PROFILING
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
494 // Originally, this was in report_ccs_profiling(). Now, retainer
495 // profiling might tack some extra stuff on to the end of this file
496 // during endProfiling().
9ac03f0 @simonmar FIX #2234: don't generate <prog>.prof unless we're going to put somet…
simonmar authored
497 if (prof_file != NULL) fclose(prof_file);
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
498 #endif
71d6db2 fix profiling on Win32
Simon Marlow authored
499
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
500 #if defined(TICKY_TICKY)
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
501 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
502 #endif
5b4f5a6 [project @ 2003-02-21 05:34:12 by sof]
sof authored
503
de6c8e5 Remove the Windows Async IO Manager completely in THREADED_RTS mode
Simon Marlow authored
504 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
eb5791f FIX #1177, partially at least.
Simon Marlow authored
505 shutdownAsyncIO(wait_foreign);
5b4f5a6 [project @ 2003-02-21 05:34:12 by sof]
sof authored
506 #endif
71d6db2 fix profiling on Win32
Simon Marlow authored
507
897bcbf FIX: #1227 (biographical profiling broken)
Simon Marlow authored
508 /* free hash table storage */
509 exitHashTable();
510
d526504 @igfoo When debugging, have the allocator help us a bit
igfoo authored
511 // Finally, free all our storage
71d6db2 fix profiling on Win32
Simon Marlow authored
512 freeStorage();
d526504 @igfoo When debugging, have the allocator help us a bit
igfoo authored
513
514 #if defined(DEBUG)
515 /* and shut down the allocator debugging */
516 shutdownAllocator();
517 #endif
518
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
519 }
520
681aad9 hs_exit()/shutdownHaskell(): wait for outstanding foreign calls to co…
Simon Marlow authored
521 // The real hs_exit():
522 void
523 hs_exit(void)
524 {
525 hs_exit_(rtsTrue);
526 // be safe; this might be a DLL
527 }
528
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
529 // Compatibility interfaces
530 void
531 shutdownHaskell(void)
532 {
533 hs_exit();
534 }
535
536 void
537 shutdownHaskellAndExit(int n)
538 {
addff19 @simonmar FIX part of #2301, and #1619
simonmar authored
539 // we're about to exit(), no need to wait for foreign calls to return.
540 hs_exit_(rtsFalse);
541
542 if (hs_init_count == 0) {
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
543 #if defined(PAR)
544 /* really exit (stg_exit() would call shutdownParallelSystem() again) */
545 exit(n);
546 #else
547 stg_exit(n);
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
548 #endif
34b568c [project @ 2003-01-28 16:30:06 by simonmar]
simonmar authored
549 }
1b28d4e [project @ 2000-01-13 14:33:57 by hwloidl]
hwloidl authored
550 }
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
551
addff19 @simonmar FIX part of #2301, and #1619
simonmar authored
552 #ifndef mingw32_HOST_OS
553 void
554 shutdownHaskellAndSignal(int sig)
555 {
556 hs_exit_(rtsFalse);
557 kill(getpid(),sig);
558 }
559 #endif
560
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
561 /*
9903274 [project @ 1999-07-06 15:33:23 by simonmar]
simonmar authored
562 * called from STG-land to exit the program
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
563 */
564
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
565 #ifdef PAR
566 static int exit_started=rtsFalse;
567 #endif
568
7b1b327 make exit() overridable, for use in DLLs
Simon Marlow authored
569 void (*exitFn)(int) = 0;
570
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
571 void
0bffc41 [project @ 2002-12-11 15:36:20 by simonmar]
simonmar authored
572 stg_exit(int n)
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
573 {
574 #ifdef PAR
575 /* HACK: avoid a loop when exiting due to a stupid error */
576 if (exit_started)
577 return;
578 exit_started=rtsTrue;
579
95ca6bf [project @ 2004-09-03 15:28:18 by simonmar]
simonmar authored
580 IF_PAR_DEBUG(verbose, debugBelch("==-- stg_exit %d on [%x]...", n, mytid));
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
581 shutdownParallelSystem(n);
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
582 #endif
7b1b327 make exit() overridable, for use in DLLs
Simon Marlow authored
583 if (exitFn)
584 (*exitFn)(n);
20fc2f0 [project @ 2001-03-22 03:51:08 by hwloidl]
hwloidl authored
585 exit(n);
4385968 [project @ 1998-12-02 13:17:09 by simonm]
simonm authored
586 }
Something went wrong with that request. Please try again.