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"
22 #include "Schedule.h" /* initScheduler */
23 #include "Stats.h" /* initStats */
24 #include "STM.h" /* initSTM */
25 #include "RtsSignals.h"
29 #include "Prelude.h" /* fixupRTStoPreludeRefs */
30 #include "ThreadLabels.h"
31 #include "sm/BlockAlloc.h"
35 #include "Profiling.h"
38 void exitLinker( void ); // there is no Linker.h file to include
40 #if defined(RTS_GTK_FRONTPANEL)
41 #include "FrontPanel.h"
44 #if defined(PROFILING)
45 # include "ProfHeap.h"
46 # include "RetainerProfile.h"
49 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
50 #include "win32/AsyncIO.h"
53 #if !defined(mingw32_HOST_OS)
54 #include "posix/TTY.h"
55 #include "posix/FileLock.h"
69 // Count of how many outstanding hs_init()s there have been.
70 static int hs_init_count = 0;
72 /* -----------------------------------------------------------------------------
73 Initialise floating point unit on x86 (currently disabled; See Note
74 [x86 Floating point precision] in compiler/nativeGen/X86/Instr.hs)
75 -------------------------------------------------------------------------- */
77 #define X86_INIT_FPU 0
83 __volatile unsigned short int fpu_cw;
85 // Grab the control word
86 __asm __volatile ("fnstcw %0" : "=m" (fpu_cw));
89 printf("fpu_cw: %x\n", fpu_cw);
92 // Set bits 8-9 to 10 (64-bit precision).
93 fpu_cw = (fpu_cw & 0xfcff) | 0x0200;
95 // Store the new control word back
96 __asm __volatile ("fldcw %0" : : "m" (fpu_cw));
100 /* -----------------------------------------------------------------------------
102 -------------------------------------------------------------------------- */
105 hs_init(int *argc, char **argv[])
108 if (hs_init_count > 1) {
109 // second and subsequent inits are ignored
113 setlocale(LC_CTYPE,"");
115 /* Initialise the stats department, phase 0 */
118 /* Next we do is grab the start time...just in case we're
119 * collecting timing statistics.
123 /* Set the RTS flags to default values. */
125 initRtsFlagsDefaults();
127 /* Call the user hook to reset defaults, if present */
130 /* Parse the flags, separating the RTS flags from the programs args */
131 if (argc != NULL && argv != NULL) {
132 setFullProgArgv(*argc,*argv);
133 setupRtsFlags(argc, *argv);
136 /* Initialise the stats department, phase 1 */
143 /* initTracing must be after setupRtsFlags() */
147 /* Dtrace events are always enabled
149 dtraceEventStartup();
151 /* Trace some basic information about the process
153 traceCapsetDetails(argc, argv);
155 /* initialise scheduler data structures (needs to be done before
160 /* initialize the storage manager */
163 /* initialise the stable pointer table */
164 initStablePtrTable();
166 /* Add some GC roots for things in the base package that the RTS
167 * knows about. We don't know whether these turn out to be CAFs
168 * or refer to CAFs, but we have to assume that they might.
170 getStablePtr((StgPtr)runIO_closure);
171 getStablePtr((StgPtr)runNonIO_closure);
173 getStablePtr((StgPtr)runFinalizerBatch_closure);
175 getStablePtr((StgPtr)stackOverflow_closure);
176 getStablePtr((StgPtr)heapOverflow_closure);
177 getStablePtr((StgPtr)unpackCString_closure);
178 getStablePtr((StgPtr)blockedIndefinitelyOnMVar_closure);
179 getStablePtr((StgPtr)nonTermination_closure);
180 getStablePtr((StgPtr)blockedIndefinitelyOnSTM_closure);
181 getStablePtr((StgPtr)nestedAtomically_closure);
183 getStablePtr((StgPtr)runSparks_closure);
184 getStablePtr((StgPtr)ensureIOManagerIsRunning_closure);
185 #ifndef mingw32_HOST_OS
186 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) {
233 // This must be done after module initialisation.
234 // ToDo: make this work in the presence of multiple hs_add_root()s.
238 #if defined(THREADED_RTS)
242 /* Record initialization times */
246 // Compatibility interface
248 startupHaskell(int argc, char *argv[], void (*init_root)(void) STG_UNUSED)
250 hs_init(&argc, &argv);
254 /* -----------------------------------------------------------------------------
255 hs_add_root: backwards compatibility. (see #3252)
256 -------------------------------------------------------------------------- */
259 hs_add_root(void (*init_root)(void) STG_UNUSED)
264 /* ----------------------------------------------------------------------------
265 * Shutting down the RTS
267 * The wait_foreign parameter means:
268 * True ==> wait for any threads doing foreign calls now.
269 * False ==> threads doing foreign calls may return in the
270 * future, but will immediately block on a mutex.
271 * (capability->lock).
273 * If this RTS is a DLL that we're about to unload, then you want
274 * safe=True, otherwise the thread might return to code that has been
275 * unloaded. If this is a standalone program that is about to exit,
276 * then you can get away with safe=False, which is better because we
277 * won't hang on exit if there is a blocked foreign call outstanding.
279 ------------------------------------------------------------------------- */
282 hs_exit_(rtsBool wait_foreign)
284 if (hs_init_count <= 0) {
285 errorBelch("warning: too many hs_exit()s");
289 if (hs_init_count > 0) {
290 // ignore until it's the last one
294 /* start timing the shutdown */
304 // Free the full argv storage
307 #if defined(THREADED_RTS)
311 /* stop all running tasks */
312 exitScheduler(wait_foreign);
314 /* run C finalizers for all active weak pointers */
315 runAllCFinalizers(weak_ptr_list);
317 #if defined(RTS_USER_SIGNALS)
318 if (RtsFlags.MiscFlags.install_signal_handlers) {
319 freeSignalHandlers();
323 /* stop the ticker */
325 exitTimer(wait_foreign);
327 // set the terminal settings back to what they were
328 #if !defined(mingw32_HOST_OS)
329 resetTerminalSettings();
332 // uninstall signal handlers
333 resetDefaultHandlers();
335 /* stop timing the shutdown, we're about to print stats */
338 /* shutdown the hpc support (if needed) */
341 // clean up things from the storage manager's point of view.
342 // also outputs the stats (+RTS -s) info.
348 /* free shared Typeable store */
351 /* free linker data */
354 /* free file locking tables, if necessary */
355 #if !defined(mingw32_HOST_OS)
359 /* free the stable pointer table */
360 exitStablePtrTable();
363 /* free the thread label table */
364 freeThreadLabelTable();
367 #ifdef RTS_GTK_FRONTPANEL
368 if (RtsFlags.GcFlags.frontpanel) {
373 #if defined(PROFILING)
374 reportCCSProfiling();
381 // Originally, this was in report_ccs_profiling(). Now, retainer
382 // profiling might tack some extra stuff on to the end of this file
383 // during endProfiling().
384 if (prof_file != NULL) fclose(prof_file);
392 #if defined(TICKY_TICKY)
393 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
396 #if defined(mingw32_HOST_OS) && !defined(THREADED_RTS)
397 shutdownAsyncIO(wait_foreign);
400 /* free hash table storage */
403 // Finally, free all our storage. However, we only free the heap
404 // memory if we have waited for foreign calls to complete;
405 // otherwise a foreign call in progress may still be referencing
406 // heap memory (e.g. by being passed a ByteArray#).
407 freeStorage(wait_foreign);
411 // The real hs_exit():
416 // be safe; this might be a DLL
419 // Compatibility interfaces
421 shutdownHaskell(void)
427 shutdownHaskellAndExit(int n)
429 // we're about to exit(), no need to wait for foreign calls to return.
432 if (hs_init_count == 0) {
437 #ifndef mingw32_HOST_OS
439 shutdownHaskellAndSignal(int sig)
447 * called from STG-land to exit the program
450 void (*exitFn)(int) = 0;