1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.51 2001/05/25 18:33:46 sof Exp $
4 * (c) The GHC Team, 1998-2000
6 * Main function for a standalone Haskell program.
8 * ---------------------------------------------------------------------------*/
14 #include "Storage.h" /* initStorage, exitStorage */
15 #include "StablePriv.h" /* initStablePtrTable */
16 #include "Schedule.h" /* initScheduler */
17 #include "Stats.h" /* initStats */
23 #include "StgStartup.h"
24 #include "Prelude.h" /* fixupRTStoPreludeRefs */
28 #if defined(RTS_GTK_FRONTPANEL)
29 #include "FrontPanel.h"
32 #if defined(PROFILING) || defined(DEBUG)
33 # include "Profiling.h"
34 # include "ProfHeap.h"
38 # include "GranSimRts.h"
41 #if defined(GRAN) || defined(PAR)
42 # include "ParallelRts.h"
46 # include "Parallel.h"
53 struct RTS_FLAGS RtsFlags;
55 static int rts_has_started_up = 0;
60 EXTFUN(__init_Prelude);
61 static void initModules ( void (*)(void) );
64 setProgArgv(int argc, char *argv[])
66 /* Usually this is done by startupHaskell, so we don't need to call this.
67 However, sometimes Hugs wants to change the arguments which Haskell
68 getArgs >>= ... will be fed. So you can do that by calling here
69 _after_ calling startupHaskell.
76 getProgArgv(int *argc, char **argv[])
84 startupHaskell(int argc, char *argv[], void (*init_root)(void))
86 /* To avoid repeated initialisations of the RTS */
87 if (rts_has_started_up)
92 /* The very first thing we do is grab the start time...just in case we're
93 * collecting timing statistics.
99 * The parallel system needs to be initialised and synchronised before
100 * the program is run.
102 startupParallelSystem(argv);
104 if (*argv[0] == '-') { /* Strip off mainPE flag argument */
109 argv[1] = argv[0]; /* ignore the nPEs argument */
113 /* Set the RTS flags to default values. */
114 initRtsFlagsDefaults();
116 /* Call the user hook to reset defaults, if present */
119 /* Parse the flags, separating the RTS flags from the programs args */
120 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
125 /* NB: this really must be done after processing the RTS flags */
126 IF_PAR_DEBUG(verbose,
127 fprintf(stderr, "==== Synchronising system (%d PEs)\n", nPEs));
128 synchroniseSystem(); // calls initParallelSystem etc
131 /* initialise scheduler data structures (needs to be done before
137 /* And start GranSim profiling if required: */
138 if (RtsFlags.GranFlags.GranSimStats.Full)
139 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
141 /* And start GUM profiling if required: */
142 if (RtsFlags.ParFlags.ParStats.Full)
143 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
144 #endif /* PAR || GRAN */
146 /* initialize the storage manager */
149 /* initialise the stable pointer table */
150 initStablePtrTable();
152 #if defined(PROFILING) || defined(DEBUG)
156 /* run the per-module initialisation code */
157 initModules(init_root);
159 #if defined(PROFILING) || defined(DEBUG)
163 /* start the ticker */
164 install_vtalrm_handler();
165 initialize_virtual_timer(TICK_MILLISECS);
167 /* start our haskell execution tasks */
172 /* Initialise the stats department */
175 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
176 /* Initialise the user signal handler set */
178 /* Set up handler to run on SIGINT, etc. */
179 init_default_handlers();
182 #ifdef RTS_GTK_FRONTPANEL
183 if (RtsFlags.GcFlags.frontpanel) {
188 /* Record initialization times */
192 /* -----------------------------------------------------------------------------
193 Per-module initialisation
195 This process traverses all the compiled modules in the program
196 starting with "Main", and performing per-module initialisation for
199 So far, two things happen at initialisation time:
201 - we register stable names for each foreign-exported function
202 in that module. This prevents foreign-exported entities, and
203 things they depend on, from being garbage collected.
205 - we supply a unique integer to each statically declared cost
206 centre and cost centre stack in the program.
208 The code generator inserts a small function "__init_<module>" in each
209 module and calls the registration functions in each of the modules
210 it imports. So, if we call "__init_PrelMain", each reachable module in the
211 program will be registered (because PrelMain.mainIO calls Main.main).
213 The init* functions are compiled in the same way as STG code,
214 i.e. without normal C call/return conventions. Hence we must use
215 StgRun to call this stuff.
216 -------------------------------------------------------------------------- */
218 /* The init functions use an explicit stack...
220 #define INIT_STACK_SIZE (BLOCK_SIZE * 4)
221 F_ *init_stack = NULL;
225 initModules ( void (*init_root)(void) )
230 #define cap MainRegTable
234 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
235 init_stack[init_sp++] = (F_)stg_init_ret;
236 init_stack[init_sp++] = (F_)__init_Prelude;
237 if (init_root != NULL) {
238 init_stack[init_sp++] = (F_)init_root;
241 cap.rSp = (P_)(init_stack + init_sp);
242 StgRun((StgFunPtr)stg_init, &cap);
245 /* -----------------------------------------------------------------------------
246 * Shutting down the RTS - two ways of doing this, one which
247 * calls exit(), one that doesn't.
249 * (shutdownHaskellAndExit() is called by System.exitWith).
250 * -----------------------------------------------------------------------------
253 shutdownHaskellAndExit(int n)
258 /* really exit (stg_exit() would call shutdownParallelSystem() again) */
266 shutdownHaskell(void)
268 if (!rts_has_started_up)
271 /* start timing the shutdown */
275 /* Finalize any remaining weak pointers */
276 finalizeWeakPointersNow();
280 /* end_gr_simulation prints global stats if requested -- HWL */
281 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
285 /* stop all running tasks */
288 /* stop the ticker */
289 initialize_virtual_timer(0);
291 /* reset the standard file descriptors to blocking mode */
292 resetNonBlockingFd(0);
293 resetNonBlockingFd(1);
294 resetNonBlockingFd(2);
297 /* controlled exit; good thread! */
298 shutdownParallelSystem(0);
300 /* global statistics in parallel system */
304 /* stop timing the shutdown, we're about to print stats */
307 /* clean up things from the storage manager's point of view.
308 * also outputs the stats (+RTS -s) info.
312 #ifdef RTS_GTK_FRONTPANEL
313 if (RtsFlags.GcFlags.frontpanel) {
318 #if defined(PROFILING) || defined(DEBUG)
322 #if defined(PROFILING)
323 report_ccs_profiling();
326 #if defined(TICKY_TICKY)
327 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
330 rts_has_started_up=0;
334 * called from STG-land to exit the program
338 static int exit_started=rtsFalse;
345 /* HACK: avoid a loop when exiting due to a stupid error */
348 exit_started=rtsTrue;
350 IF_PAR_DEBUG(verbose, fprintf(stderr,"==-- stg_exit %d on [%x]...", n, mytid));
351 shutdownParallelSystem(n);