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"
20 #include "Schedule.h" /* initScheduler */
21 #include "Stats.h" /* initStats */
22 #include "STM.h" /* initSTM */
23 #include "RtsSignals.h"
27 #include "Prelude.h" /* fixupRTStoPreludeRefs */
28 #include "ThreadLabels.h"
29 #include "sm/BlockAlloc.h"
33 #include "Profiling.h"
37 #if defined(RTS_GTK_FRONTPANEL)
38 #include "FrontPanel.h"
41 #if defined(PROFILING)
42 # include "ProfHeap.h"
43 # include "RetainerProfile.h"
46 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
47 #include "win32/AsyncIO.h"
50 #if !defined(mingw32_HOST_OS)
51 #include "posix/TTY.h"
52 #include "posix/FileLock.h"
66 // Count of how many outstanding hs_init()s there have been.
67 static int hs_init_count = 0;
69 /* -----------------------------------------------------------------------------
70 Initialise floating point unit on x86 (currently disabled. why?)
71 (see comment in ghc/compiler/nativeGen/MachInstrs.lhs).
72 -------------------------------------------------------------------------- */
74 #define X86_INIT_FPU 0
80 __volatile unsigned short int fpu_cw;
82 // Grab the control word
83 __asm __volatile ("fnstcw %0" : "=m" (fpu_cw));
86 printf("fpu_cw: %x\n", fpu_cw);
89 // Set bits 8-9 to 10 (64-bit precision).
90 fpu_cw = (fpu_cw & 0xfcff) | 0x0200;
92 // Store the new control word back
93 __asm __volatile ("fldcw %0" : : "m" (fpu_cw));
97 /* -----------------------------------------------------------------------------
99 -------------------------------------------------------------------------- */
102 hs_init(int *argc, char **argv[])
105 if (hs_init_count > 1) {
106 // second and subsequent inits are ignored
110 setlocale(LC_CTYPE,"");
112 /* Initialise the stats department, phase 0 */
115 /* Next we do is grab the start time...just in case we're
116 * collecting timing statistics.
121 /* Start off by initialising the allocator debugging so we can
126 /* Set the RTS flags to default values. */
128 initRtsFlagsDefaults();
130 /* Call the user hook to reset defaults, if present */
133 /* Parse the flags, separating the RTS flags from the programs args */
134 if (argc != NULL && argv != NULL) {
135 setFullProgArgv(*argc,*argv);
136 setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
137 setProgArgv(*argc,*argv);
140 /* Initialise the stats department, phase 1 */
147 /* initTracing must be after setupRtsFlags() */
152 /* initialise scheduler data structures (needs to be done before
157 /* initialize the storage manager */
160 /* initialise the stable pointer table */
161 initStablePtrTable();
163 /* Add some GC roots for things in the base package that the RTS
164 * knows about. We don't know whether these turn out to be CAFs
165 * or refer to CAFs, but we have to assume that they might.
167 getStablePtr((StgPtr)base_GHCziTopHandler_runIO_closure);
168 getStablePtr((StgPtr)base_GHCziTopHandler_runNonIO_closure);
169 getStablePtr((StgPtr)stackOverflow_closure);
170 getStablePtr((StgPtr)heapOverflow_closure);
171 getStablePtr((StgPtr)runFinalizerBatch_closure);
172 getStablePtr((StgPtr)unpackCString_closure);
173 getStablePtr((StgPtr)blockedIndefinitelyOnMVar_closure);
174 getStablePtr((StgPtr)nonTermination_closure);
175 getStablePtr((StgPtr)blockedIndefinitelyOnSTM_closure);
177 /* initialise the shared Typeable store */
180 /* initialise file locking, if necessary */
181 #if !defined(mingw32_HOST_OS)
186 /* initialise thread label table (tso->char*) */
187 initThreadLabelTable();
192 /* start the virtual timer 'subsystem'. */
196 #if defined(RTS_USER_SIGNALS)
197 if (RtsFlags.MiscFlags.install_signal_handlers) {
198 /* Initialise the user signal handler set */
200 /* Set up handler to run on SIGINT, etc. */
201 initDefaultHandlers();
205 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
209 #ifdef RTS_GTK_FRONTPANEL
210 if (RtsFlags.GcFlags.frontpanel) {
219 /* Record initialization times */
223 // Compatibility interface
225 startupHaskell(int argc, char *argv[], void (*init_root)(void))
227 hs_init(&argc, &argv);
229 hs_add_root(init_root);
233 /* -----------------------------------------------------------------------------
234 Per-module initialisation
236 This process traverses all the compiled modules in the program
237 starting with "Main", and performing per-module initialisation for
240 So far, two things happen at initialisation time:
242 - we register stable names for each foreign-exported function
243 in that module. This prevents foreign-exported entities, and
244 things they depend on, from being garbage collected.
246 - we supply a unique integer to each statically declared cost
247 centre and cost centre stack in the program.
249 The code generator inserts a small function "__stginit_<module>" in each
250 module and calls the registration functions in each of the modules it
253 The init* functions are compiled in the same way as STG code,
254 i.e. without normal C call/return conventions. Hence we must use
255 StgRun to call this stuff.
256 -------------------------------------------------------------------------- */
258 /* The init functions use an explicit stack...
260 #define INIT_STACK_BLOCKS 4
261 static StgFunPtr *init_stack = NULL;
264 hs_add_root(void (*init_root)(void))
272 if (hs_init_count <= 0) {
273 barf("hs_add_root() must be called after hs_init()");
276 /* The initialisation stack grows downward, with sp pointing
277 to the last occupied word */
278 init_sp = INIT_STACK_BLOCKS*BLOCK_SIZE_W;
279 bd = allocGroup_lock(INIT_STACK_BLOCKS);
280 init_stack = (StgFunPtr *)bd->start;
281 init_stack[--init_sp] = (StgFunPtr)stg_init_finish;
282 if (init_root != NULL) {
283 init_stack[--init_sp] = (StgFunPtr)init_root;
286 cap->r.rSp = (P_)(init_stack + init_sp);
287 StgRun((StgFunPtr)stg_init, &cap->r);
293 // This must be done after module initialisation.
294 // ToDo: make this work in the presence of multiple hs_add_root()s.
300 #if defined(THREADED_RTS)
305 /* ----------------------------------------------------------------------------
306 * Shutting down the RTS
308 * The wait_foreign parameter means:
309 * True ==> wait for any threads doing foreign calls now.
310 * False ==> threads doing foreign calls may return in the
311 * future, but will immediately block on a mutex.
312 * (capability->lock).
314 * If this RTS is a DLL that we're about to unload, then you want
315 * safe=True, otherwise the thread might return to code that has been
316 * unloaded. If this is a standalone program that is about to exit,
317 * then you can get away with safe=False, which is better because we
318 * won't hang on exit if there is a blocked foreign call outstanding.
320 ------------------------------------------------------------------------- */
323 hs_exit_(rtsBool wait_foreign)
325 if (hs_init_count <= 0) {
326 errorBelch("warning: too many hs_exit()s");
330 if (hs_init_count > 0) {
331 // ignore until it's the last one
335 /* start timing the shutdown */
340 #if defined(THREADED_RTS)
344 /* stop all running tasks */
345 exitScheduler(wait_foreign);
347 /* run C finalizers for all active weak pointers */
348 runAllCFinalizers(weak_ptr_list);
350 #if defined(RTS_USER_SIGNALS)
351 if (RtsFlags.MiscFlags.install_signal_handlers) {
352 freeSignalHandlers();
356 /* stop the ticker */
360 // set the terminal settings back to what they were
361 #if !defined(mingw32_HOST_OS)
362 resetTerminalSettings();
365 // uninstall signal handlers
366 resetDefaultHandlers();
368 /* stop timing the shutdown, we're about to print stats */
371 /* shutdown the hpc support (if needed) */
374 // clean up things from the storage manager's point of view.
375 // also outputs the stats (+RTS -s) info.
381 /* free shared Typeable store */
384 /* free file locking tables, if necessary */
385 #if !defined(mingw32_HOST_OS)
389 /* free the stable pointer table */
390 exitStablePtrTable();
393 /* free the thread label table */
394 freeThreadLabelTable();
397 #ifdef RTS_GTK_FRONTPANEL
398 if (RtsFlags.GcFlags.frontpanel) {
403 #if defined(PROFILING)
404 reportCCSProfiling();
411 // Originally, this was in report_ccs_profiling(). Now, retainer
412 // profiling might tack some extra stuff on to the end of this file
413 // during endProfiling().
414 if (prof_file != NULL) fclose(prof_file);
422 #if defined(TICKY_TICKY)
423 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
426 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
427 shutdownAsyncIO(wait_foreign);
430 /* free hash table storage */
433 // Finally, free all our storage
437 /* and shut down the allocator debugging */
443 // The real hs_exit():
448 // be safe; this might be a DLL
451 // Compatibility interfaces
453 shutdownHaskell(void)
459 shutdownHaskellAndExit(int n)
461 // we're about to exit(), no need to wait for foreign calls to return.
464 if (hs_init_count == 0) {
469 #ifndef mingw32_HOST_OS
471 shutdownHaskellAndSignal(int sig)
479 * called from STG-land to exit the program
482 void (*exitFn)(int) = 0;