1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2002
5 * Main function for a standalone Haskell program.
7 * ---------------------------------------------------------------------------*/
9 // PAPI uses caddr_t, which is not POSIX
11 #include "PosixSource.h"
18 #include "sm/Storage.h"
21 #include "Schedule.h" /* initScheduler */
22 #include "Stats.h" /* initStats */
23 #include "STM.h" /* initSTM */
24 #include "RtsSignals.h"
28 #include "Prelude.h" /* fixupRTStoPreludeRefs */
29 #include "ThreadLabels.h"
30 #include "sm/BlockAlloc.h"
34 #include "Profiling.h"
38 #if defined(RTS_GTK_FRONTPANEL)
39 #include "FrontPanel.h"
42 #if defined(PROFILING)
43 # include "ProfHeap.h"
44 # include "RetainerProfile.h"
47 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
48 #include "win32/AsyncIO.h"
51 #if !defined(mingw32_HOST_OS)
52 #include "posix/TTY.h"
53 #include "posix/FileLock.h"
67 // Count of how many outstanding hs_init()s there have been.
68 static int hs_init_count = 0;
70 /* -----------------------------------------------------------------------------
71 Initialise floating point unit on x86 (currently disabled. why?)
72 (see comment in ghc/compiler/nativeGen/MachInstrs.lhs).
73 -------------------------------------------------------------------------- */
75 #define X86_INIT_FPU 0
81 __volatile unsigned short int fpu_cw;
83 // Grab the control word
84 __asm __volatile ("fnstcw %0" : "=m" (fpu_cw));
87 printf("fpu_cw: %x\n", fpu_cw);
90 // Set bits 8-9 to 10 (64-bit precision).
91 fpu_cw = (fpu_cw & 0xfcff) | 0x0200;
93 // Store the new control word back
94 __asm __volatile ("fldcw %0" : : "m" (fpu_cw));
98 /* -----------------------------------------------------------------------------
100 -------------------------------------------------------------------------- */
103 hs_init(int *argc, char **argv[])
106 if (hs_init_count > 1) {
107 // second and subsequent inits are ignored
111 setlocale(LC_CTYPE,"");
113 /* Initialise the stats department, phase 0 */
116 /* Next we do is grab the start time...just in case we're
117 * collecting timing statistics.
122 /* Start off by initialising the allocator debugging so we can
127 /* Set the RTS flags to default values. */
129 initRtsFlagsDefaults();
131 /* Call the user hook to reset defaults, if present */
134 /* Parse the flags, separating the RTS flags from the programs args */
135 if (argc != NULL && argv != NULL) {
136 setFullProgArgv(*argc,*argv);
137 setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
138 setProgArgv(*argc,*argv);
141 /* Initialise the stats department, phase 1 */
148 /* initTracing must be after setupRtsFlags() */
153 /* initialise scheduler data structures (needs to be done before
158 /* initialize the storage manager */
161 /* initialise the stable pointer table */
162 initStablePtrTable();
164 /* Add some GC roots for things in the base package that the RTS
165 * knows about. We don't know whether these turn out to be CAFs
166 * or refer to CAFs, but we have to assume that they might.
168 getStablePtr((StgPtr)runIO_closure);
169 getStablePtr((StgPtr)runNonIO_closure);
170 getStablePtr((StgPtr)stackOverflow_closure);
171 getStablePtr((StgPtr)heapOverflow_closure);
172 getStablePtr((StgPtr)runFinalizerBatch_closure);
173 getStablePtr((StgPtr)unpackCString_closure);
174 getStablePtr((StgPtr)blockedIndefinitelyOnMVar_closure);
175 getStablePtr((StgPtr)nonTermination_closure);
176 getStablePtr((StgPtr)blockedIndefinitelyOnSTM_closure);
178 /* initialise the shared Typeable store */
181 /* initialise file locking, if necessary */
182 #if !defined(mingw32_HOST_OS)
187 /* initialise thread label table (tso->char*) */
188 initThreadLabelTable();
193 /* start the virtual timer 'subsystem'. */
197 #if defined(RTS_USER_SIGNALS)
198 if (RtsFlags.MiscFlags.install_signal_handlers) {
199 /* Initialise the user signal handler set */
201 /* Set up handler to run on SIGINT, etc. */
202 initDefaultHandlers();
206 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
210 #ifdef RTS_GTK_FRONTPANEL
211 if (RtsFlags.GcFlags.frontpanel) {
220 /* Record initialization times */
224 // Compatibility interface
226 startupHaskell(int argc, char *argv[], void (*init_root)(void))
228 hs_init(&argc, &argv);
230 hs_add_root(init_root);
234 /* -----------------------------------------------------------------------------
235 Per-module initialisation
237 This process traverses all the compiled modules in the program
238 starting with "Main", and performing per-module initialisation for
241 So far, two things happen at initialisation time:
243 - we register stable names for each foreign-exported function
244 in that module. This prevents foreign-exported entities, and
245 things they depend on, from being garbage collected.
247 - we supply a unique integer to each statically declared cost
248 centre and cost centre stack in the program.
250 The code generator inserts a small function "__stginit_<module>" in each
251 module and calls the registration functions in each of the modules it
254 The init* functions are compiled in the same way as STG code,
255 i.e. without normal C call/return conventions. Hence we must use
256 StgRun to call this stuff.
257 -------------------------------------------------------------------------- */
259 /* The init functions use an explicit stack...
261 #define INIT_STACK_BLOCKS 4
262 static StgFunPtr *init_stack = NULL;
265 hs_add_root(void (*init_root)(void))
273 if (hs_init_count <= 0) {
274 barf("hs_add_root() must be called after hs_init()");
277 /* The initialisation stack grows downward, with sp pointing
278 to the last occupied word */
279 init_sp = INIT_STACK_BLOCKS*BLOCK_SIZE_W;
280 bd = allocGroup_lock(INIT_STACK_BLOCKS);
281 init_stack = (StgFunPtr *)bd->start;
282 init_stack[--init_sp] = (StgFunPtr)stg_init_finish;
283 if (init_root != NULL) {
284 init_stack[--init_sp] = (StgFunPtr)init_root;
287 cap->r.rSp = (P_)(init_stack + init_sp);
288 StgRun((StgFunPtr)stg_init, &cap->r);
294 // This must be done after module initialisation.
295 // ToDo: make this work in the presence of multiple hs_add_root()s.
301 #if defined(THREADED_RTS)
306 /* ----------------------------------------------------------------------------
307 * Shutting down the RTS
309 * The wait_foreign parameter means:
310 * True ==> wait for any threads doing foreign calls now.
311 * False ==> threads doing foreign calls may return in the
312 * future, but will immediately block on a mutex.
313 * (capability->lock).
315 * If this RTS is a DLL that we're about to unload, then you want
316 * safe=True, otherwise the thread might return to code that has been
317 * unloaded. If this is a standalone program that is about to exit,
318 * then you can get away with safe=False, which is better because we
319 * won't hang on exit if there is a blocked foreign call outstanding.
321 ------------------------------------------------------------------------- */
324 hs_exit_(rtsBool wait_foreign)
326 if (hs_init_count <= 0) {
327 errorBelch("warning: too many hs_exit()s");
331 if (hs_init_count > 0) {
332 // ignore until it's the last one
336 /* start timing the shutdown */
341 #if defined(THREADED_RTS)
345 /* stop all running tasks */
346 exitScheduler(wait_foreign);
348 /* run C finalizers for all active weak pointers */
349 runAllCFinalizers(weak_ptr_list);
351 #if defined(RTS_USER_SIGNALS)
352 if (RtsFlags.MiscFlags.install_signal_handlers) {
353 freeSignalHandlers();
357 /* stop the ticker */
361 // set the terminal settings back to what they were
362 #if !defined(mingw32_HOST_OS)
363 resetTerminalSettings();
366 // uninstall signal handlers
367 resetDefaultHandlers();
369 /* stop timing the shutdown, we're about to print stats */
372 /* shutdown the hpc support (if needed) */
375 // clean up things from the storage manager's point of view.
376 // also outputs the stats (+RTS -s) info.
382 /* free shared Typeable store */
385 /* free file locking tables, if necessary */
386 #if !defined(mingw32_HOST_OS)
390 /* free the stable pointer table */
391 exitStablePtrTable();
394 /* free the thread label table */
395 freeThreadLabelTable();
398 #ifdef RTS_GTK_FRONTPANEL
399 if (RtsFlags.GcFlags.frontpanel) {
404 #if defined(PROFILING)
405 reportCCSProfiling();
412 // Originally, this was in report_ccs_profiling(). Now, retainer
413 // profiling might tack some extra stuff on to the end of this file
414 // during endProfiling().
415 if (prof_file != NULL) fclose(prof_file);
423 #if defined(TICKY_TICKY)
424 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
427 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
428 shutdownAsyncIO(wait_foreign);
431 /* free hash table storage */
434 // Finally, free all our storage
438 /* and shut down the allocator debugging */
444 // The real hs_exit():
449 // be safe; this might be a DLL
452 // Compatibility interfaces
454 shutdownHaskell(void)
460 shutdownHaskellAndExit(int n)
462 // we're about to exit(), no need to wait for foreign calls to return.
465 if (hs_init_count == 0) {
470 #ifndef mingw32_HOST_OS
472 shutdownHaskellAndSignal(int sig)
480 * called from STG-land to exit the program
483 void (*exitFn)(int) = 0;