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"
37 void exitLinker( void ); // there is no Linker.h file to include
39 #if defined(RTS_GTK_FRONTPANEL)
40 #include "FrontPanel.h"
43 #if defined(PROFILING)
44 # include "ProfHeap.h"
45 # include "RetainerProfile.h"
48 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
49 #include "win32/AsyncIO.h"
52 #if !defined(mingw32_HOST_OS)
53 #include "posix/TTY.h"
54 #include "posix/FileLock.h"
68 // Count of how many outstanding hs_init()s there have been.
69 static int hs_init_count = 0;
71 /* -----------------------------------------------------------------------------
72 Initialise floating point unit on x86 (currently disabled. why?)
73 (see comment in ghc/compiler/nativeGen/MachInstrs.lhs).
74 -------------------------------------------------------------------------- */
76 #define X86_INIT_FPU 0
82 __volatile unsigned short int fpu_cw;
84 // Grab the control word
85 __asm __volatile ("fnstcw %0" : "=m" (fpu_cw));
88 printf("fpu_cw: %x\n", fpu_cw);
91 // Set bits 8-9 to 10 (64-bit precision).
92 fpu_cw = (fpu_cw & 0xfcff) | 0x0200;
94 // Store the new control word back
95 __asm __volatile ("fldcw %0" : : "m" (fpu_cw));
99 /* -----------------------------------------------------------------------------
101 -------------------------------------------------------------------------- */
104 hs_init(int *argc, char **argv[])
107 if (hs_init_count > 1) {
108 // second and subsequent inits are ignored
112 setlocale(LC_CTYPE,"");
114 /* Initialise the stats department, phase 0 */
117 /* Next we do is grab the start time...just in case we're
118 * collecting timing statistics.
123 /* Start off by initialising the allocator debugging so we can
128 /* Set the RTS flags to default values. */
130 initRtsFlagsDefaults();
132 /* Call the user hook to reset defaults, if present */
135 /* Parse the flags, separating the RTS flags from the programs args */
136 if (argc != NULL && argv != NULL) {
137 setFullProgArgv(*argc,*argv);
138 setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
139 setProgArgv(*argc,*argv);
142 /* Initialise the stats department, phase 1 */
149 /* initTracing must be after setupRtsFlags() */
153 /* Dtrace events are always enabled
155 dtraceEventStartup();
157 /* initialise scheduler data structures (needs to be done before
162 /* initialize the storage manager */
165 /* initialise the stable pointer table */
166 initStablePtrTable();
168 /* Add some GC roots for things in the base package that the RTS
169 * knows about. We don't know whether these turn out to be CAFs
170 * or refer to CAFs, but we have to assume that they might.
172 getStablePtr((StgPtr)runIO_closure);
173 getStablePtr((StgPtr)runNonIO_closure);
175 getStablePtr((StgPtr)runFinalizerBatch_closure);
177 getStablePtr((StgPtr)stackOverflow_closure);
178 getStablePtr((StgPtr)heapOverflow_closure);
179 getStablePtr((StgPtr)unpackCString_closure);
180 getStablePtr((StgPtr)blockedIndefinitelyOnMVar_closure);
181 getStablePtr((StgPtr)nonTermination_closure);
182 getStablePtr((StgPtr)blockedIndefinitelyOnSTM_closure);
183 getStablePtr((StgPtr)nestedAtomically_closure);
185 getStablePtr((StgPtr)runSparks_closure);
186 getStablePtr((StgPtr)ensureIOManagerIsRunning_closure);
187 getStablePtr((StgPtr)runHandlers_closure);
189 /* initialise the shared Typeable store */
192 /* initialise file locking, if necessary */
193 #if !defined(mingw32_HOST_OS)
198 /* initialise thread label table (tso->char*) */
199 initThreadLabelTable();
204 /* start the virtual timer 'subsystem'. */
208 #if defined(RTS_USER_SIGNALS)
209 if (RtsFlags.MiscFlags.install_signal_handlers) {
210 /* Initialise the user signal handler set */
212 /* Set up handler to run on SIGINT, etc. */
213 initDefaultHandlers();
217 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
221 #ifdef RTS_GTK_FRONTPANEL
222 if (RtsFlags.GcFlags.frontpanel) {
231 /* Record initialization times */
235 // Compatibility interface
237 startupHaskell(int argc, char *argv[], void (*init_root)(void))
239 hs_init(&argc, &argv);
241 hs_add_root(init_root);
245 /* -----------------------------------------------------------------------------
246 Per-module initialisation
248 This process traverses all the compiled modules in the program
249 starting with "Main", and performing per-module initialisation for
252 So far, two things happen at initialisation time:
254 - we register stable names for each foreign-exported function
255 in that module. This prevents foreign-exported entities, and
256 things they depend on, from being garbage collected.
258 - we supply a unique integer to each statically declared cost
259 centre and cost centre stack in the program.
261 The code generator inserts a small function "__stginit_<module>" in each
262 module and calls the registration functions in each of the modules it
265 The init* functions are compiled in the same way as STG code,
266 i.e. without normal C call/return conventions. Hence we must use
267 StgRun to call this stuff.
268 -------------------------------------------------------------------------- */
270 /* The init functions use an explicit stack...
272 #define INIT_STACK_BLOCKS 4
273 static StgFunPtr *init_stack = NULL;
276 hs_add_root(void (*init_root)(void))
284 if (hs_init_count <= 0) {
285 barf("hs_add_root() must be called after hs_init()");
288 /* The initialisation stack grows downward, with sp pointing
289 to the last occupied word */
290 init_sp = INIT_STACK_BLOCKS*BLOCK_SIZE_W;
291 bd = allocGroup_lock(INIT_STACK_BLOCKS);
292 init_stack = (StgFunPtr *)bd->start;
293 init_stack[--init_sp] = (StgFunPtr)stg_init_finish;
294 if (init_root != NULL) {
295 init_stack[--init_sp] = (StgFunPtr)init_root;
298 cap->r.rSp = (P_)(init_stack + init_sp);
299 StgRun((StgFunPtr)stg_init, &cap->r);
305 // This must be done after module initialisation.
306 // ToDo: make this work in the presence of multiple hs_add_root()s.
312 #if defined(THREADED_RTS)
317 /* ----------------------------------------------------------------------------
318 * Shutting down the RTS
320 * The wait_foreign parameter means:
321 * True ==> wait for any threads doing foreign calls now.
322 * False ==> threads doing foreign calls may return in the
323 * future, but will immediately block on a mutex.
324 * (capability->lock).
326 * If this RTS is a DLL that we're about to unload, then you want
327 * safe=True, otherwise the thread might return to code that has been
328 * unloaded. If this is a standalone program that is about to exit,
329 * then you can get away with safe=False, which is better because we
330 * won't hang on exit if there is a blocked foreign call outstanding.
332 ------------------------------------------------------------------------- */
335 hs_exit_(rtsBool wait_foreign)
337 if (hs_init_count <= 0) {
338 errorBelch("warning: too many hs_exit()s");
342 if (hs_init_count > 0) {
343 // ignore until it's the last one
347 /* start timing the shutdown */
352 // Free the full argv storage
355 #if defined(THREADED_RTS)
359 /* stop all running tasks */
360 exitScheduler(wait_foreign);
362 /* run C finalizers for all active weak pointers */
363 runAllCFinalizers(weak_ptr_list);
365 #if defined(RTS_USER_SIGNALS)
366 if (RtsFlags.MiscFlags.install_signal_handlers) {
367 freeSignalHandlers();
371 /* stop the ticker */
375 // set the terminal settings back to what they were
376 #if !defined(mingw32_HOST_OS)
377 resetTerminalSettings();
380 // uninstall signal handlers
381 resetDefaultHandlers();
383 /* stop timing the shutdown, we're about to print stats */
386 /* shutdown the hpc support (if needed) */
389 // clean up things from the storage manager's point of view.
390 // also outputs the stats (+RTS -s) info.
396 /* free shared Typeable store */
399 /* free linker data */
402 /* free file locking tables, if necessary */
403 #if !defined(mingw32_HOST_OS)
407 /* free the stable pointer table */
408 exitStablePtrTable();
411 /* free the thread label table */
412 freeThreadLabelTable();
415 #ifdef RTS_GTK_FRONTPANEL
416 if (RtsFlags.GcFlags.frontpanel) {
421 #if defined(PROFILING)
422 reportCCSProfiling();
429 // Originally, this was in report_ccs_profiling(). Now, retainer
430 // profiling might tack some extra stuff on to the end of this file
431 // during endProfiling().
432 if (prof_file != NULL) fclose(prof_file);
440 #if defined(TICKY_TICKY)
441 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
444 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
445 shutdownAsyncIO(wait_foreign);
448 /* free hash table storage */
451 // Finally, free all our storage
455 /* and shut down the allocator debugging */
461 // The real hs_exit():
466 // be safe; this might be a DLL
469 // Compatibility interfaces
471 shutdownHaskell(void)
477 shutdownHaskellAndExit(int n)
479 // we're about to exit(), no need to wait for foreign calls to return.
482 if (hs_init_count == 0) {
487 #ifndef mingw32_HOST_OS
489 shutdownHaskellAndSignal(int sig)
497 * called from STG-land to exit the program
500 void (*exitFn)(int) = 0;