1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2002
5 * Main function for a standalone Haskell program.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
14 #include "Storage.h" /* initStorage, exitStorage */
15 #include "Schedule.h" /* initScheduler */
16 #include "Stats.h" /* initStats */
17 #include "STM.h" /* initSTM */
19 #include "Timer.h" /* startTimer, stopTimer */
23 #include "Prelude.h" /* fixupRTStoPreludeRefs */
26 #include "ThreadLabels.h"
27 #include "BlockAlloc.h"
29 #if defined(RTS_GTK_FRONTPANEL)
30 #include "FrontPanel.h"
33 #if defined(PROFILING) || defined(DEBUG)
34 # include "Profiling.h"
35 # include "ProfHeap.h"
36 # include "RetainerProfile.h"
40 # include "GranSimRts.h"
43 #if defined(GRAN) || defined(PAR)
44 # include "ParallelRts.h"
48 # include "Parallel.h"
52 #if defined(mingw32_HOST_OS)
53 #include "win32/AsyncIO.h"
69 // Count of how many outstanding hs_init()s there have been.
70 static int hs_init_count = 0;
72 // Here we save the terminal settings on the standard file
73 // descriptors, if we need to change them (eg. to support NoBuffering
75 static void *saved_termios[3] = {NULL,NULL,NULL};
78 __hscore_get_saved_termios(int fd)
80 return (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) ?
81 saved_termios[fd] : NULL;
85 __hscore_set_saved_termios(int fd, void* ts)
87 if (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) {
88 saved_termios[fd] = ts;
92 /* -----------------------------------------------------------------------------
93 Initialise floating point unit on x86 (currently disabled. why?)
94 -------------------------------------------------------------------------- */
96 #define X86_INIT_FPU 0
100 x86_init_fpu ( void )
102 __volatile unsigned short int fpu_cw;
104 // Grab the control word
105 __asm __volatile ("fnstcw %0" : "=m" (fpu_cw));
108 printf("fpu_cw: %x\n", fpu_cw);
111 // Set bits 8-9 to 10 (64-bit precision).
112 fpu_cw = (fpu_cw & 0xfcff) | 0x0200;
114 // Store the new control word back
115 __asm __volatile ("fldcw %0" : : "m" (fpu_cw));
119 /* -----------------------------------------------------------------------------
121 -------------------------------------------------------------------------- */
124 hs_init(int *argc, char **argv[])
127 if (hs_init_count > 1) {
128 // second and subsequent inits are ignored
132 /* The very first thing we do is grab the start time...just in case we're
133 * collecting timing statistics.
139 * The parallel system needs to be initialised and synchronised before
140 * the program is run.
142 startupParallelSystem(argv);
144 if (*argv[0] == '-') { /* Strip off mainPE flag argument */
149 argv[1] = argv[0]; /* ignore the nPEs argument */
153 /* Set the RTS flags to default values. */
154 initRtsFlagsDefaults();
156 /* Call the user hook to reset defaults, if present */
159 /* Parse the flags, separating the RTS flags from the programs args */
160 if (argc != NULL && argv != NULL) {
161 setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
162 setProgArgv(*argc,*argv);
166 /* NB: this really must be done after processing the RTS flags */
167 IF_PAR_DEBUG(verbose,
168 debugBelch("==== Synchronising system (%d PEs)\n", nPEs));
169 synchroniseSystem(); // calls initParallelSystem etc
172 /* Perform initialisation of adjustor thunk layer. */
175 /* initialise scheduler data structures (needs to be done before
181 /* And start GranSim profiling if required: */
182 if (RtsFlags.GranFlags.GranSimStats.Full)
183 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
185 /* And start GUM profiling if required: */
186 if (RtsFlags.ParFlags.ParStats.Full)
187 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
188 #endif /* PAR || GRAN */
190 /* initialize the storage manager */
193 /* initialise the stable pointer table */
194 initStablePtrTable();
197 /* initialise thread label table (tso->char*) */
198 initThreadLabelTable();
201 #if defined(PROFILING) || defined(DEBUG)
205 /* start the virtual timer 'subsystem'. */
206 startTimer(TICK_MILLISECS);
208 /* Initialise the stats department */
211 #if defined(RTS_USER_SIGNALS)
212 /* Initialise the user signal handler set */
214 /* Set up handler to run on SIGINT, etc. */
215 initDefaultHandlers();
218 #if defined(mingw32_HOST_OS)
224 #ifdef RTS_GTK_FRONTPANEL
225 if (RtsFlags.GcFlags.frontpanel) {
231 setlocale(LC_CTYPE,"");
238 /* Record initialization times */
242 // Compatibility interface
244 startupHaskell(int argc, char *argv[], void (*init_root)(void))
246 hs_init(&argc, &argv);
247 hs_add_root(init_root);
251 /* -----------------------------------------------------------------------------
252 Per-module initialisation
254 This process traverses all the compiled modules in the program
255 starting with "Main", and performing per-module initialisation for
258 So far, two things happen at initialisation time:
260 - we register stable names for each foreign-exported function
261 in that module. This prevents foreign-exported entities, and
262 things they depend on, from being garbage collected.
264 - we supply a unique integer to each statically declared cost
265 centre and cost centre stack in the program.
267 The code generator inserts a small function "__stginit_<module>" in each
268 module and calls the registration functions in each of the modules it
271 The init* functions are compiled in the same way as STG code,
272 i.e. without normal C call/return conventions. Hence we must use
273 StgRun to call this stuff.
274 -------------------------------------------------------------------------- */
276 /* The init functions use an explicit stack...
278 #define INIT_STACK_BLOCKS 4
279 static F_ *init_stack = NULL;
282 hs_add_root(void (*init_root)(void))
288 #define cap MainCapability
292 if (hs_init_count <= 0) {
293 barf("hs_add_root() must be called after hs_init()");
296 /* The initialisation stack grows downward, with sp pointing
297 to the last occupied word */
298 init_sp = INIT_STACK_BLOCKS*BLOCK_SIZE_W;
299 bd = allocGroup(INIT_STACK_BLOCKS);
300 init_stack = (F_ *)bd->start;
301 init_stack[--init_sp] = (F_)stg_init_finish;
302 if (init_root != NULL) {
303 init_stack[--init_sp] = (F_)init_root;
306 cap.r.rSp = (P_)(init_stack + init_sp);
307 StgRun((StgFunPtr)stg_init, &cap.r);
311 #if defined(PROFILING) || defined(DEBUG)
312 // This must be done after module initialisation.
313 // ToDo: make this work in the presence of multiple hs_add_root()s.
318 /* -----------------------------------------------------------------------------
319 Shutting down the RTS
320 -------------------------------------------------------------------------- */
325 if (hs_init_count <= 0) {
326 barf("too many hs_exit()s");
329 if (hs_init_count > 0) {
330 // ignore until it's the last one
334 /* start timing the shutdown */
337 /* stop all running tasks */
341 /* end_gr_simulation prints global stats if requested -- HWL */
342 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
346 /* stop the ticker */
349 /* reset the standard file descriptors to blocking mode */
350 resetNonBlockingFd(0);
351 resetNonBlockingFd(1);
352 resetNonBlockingFd(2);
355 // Reset the terminal settings on the standard file descriptors,
356 // if we changed them. See System.Posix.Internals.tcSetAttr for
357 // more details, including the reason we termporarily disable
361 sigset_t sigset, old_sigset;
362 sigemptyset(&sigset);
363 sigaddset(&sigset, SIGTTOU);
364 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
365 for (fd = 0; fd <= 2; fd++) {
366 struct termios* ts = (struct termios*)__hscore_get_saved_termios(fd);
368 tcsetattr(fd,TCSANOW,ts);
371 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
376 /* controlled exit; good thread! */
377 shutdownParallelSystem(0);
379 /* global statistics in parallel system */
383 /* stop timing the shutdown, we're about to print stats */
386 /* clean up things from the storage manager's point of view.
387 * also outputs the stats (+RTS -s) info.
391 #ifdef RTS_GTK_FRONTPANEL
392 if (RtsFlags.GcFlags.frontpanel) {
397 #if defined(PROFILING)
398 reportCCSProfiling();
401 #if defined(PROFILING) || defined(DEBUG)
406 // Originally, this was in report_ccs_profiling(). Now, retainer
407 // profiling might tack some extra stuff on to the end of this file
408 // during endProfiling().
412 #if defined(TICKY_TICKY)
413 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
416 #if defined(mingw32_HOST_OS)
421 // Compatibility interfaces
423 shutdownHaskell(void)
429 shutdownHaskellAndExit(int n)
431 if (hs_init_count == 1) {
435 /* really exit (stg_exit() would call shutdownParallelSystem() again) */
444 * called from STG-land to exit the program
448 static int exit_started=rtsFalse;
455 /* HACK: avoid a loop when exiting due to a stupid error */
458 exit_started=rtsTrue;
460 IF_PAR_DEBUG(verbose, debugBelch("==-- stg_exit %d on [%x]...", n, mytid));
461 shutdownParallelSystem(n);