1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.52 2001/08/14 13:40:09 sewardj Exp $
4 * (c) The GHC Team, 1998-2000
6 * Main function for a standalone Haskell program.
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
15 #include "Storage.h" /* initStorage, exitStorage */
16 #include "StablePriv.h" /* initStablePtrTable */
17 #include "Schedule.h" /* initScheduler */
18 #include "Stats.h" /* initStats */
24 #include "StgStartup.h"
25 #include "Prelude.h" /* fixupRTStoPreludeRefs */
29 #if defined(RTS_GTK_FRONTPANEL)
30 #include "FrontPanel.h"
33 #if defined(PROFILING) || defined(DEBUG)
34 # include "Profiling.h"
35 # include "ProfHeap.h"
39 # include "GranSimRts.h"
42 #if defined(GRAN) || defined(PAR)
43 # include "ParallelRts.h"
47 # include "Parallel.h"
54 struct RTS_FLAGS RtsFlags;
56 static int rts_has_started_up = 0;
61 EXTFUN(__init_Prelude);
62 static void initModules ( void (*)(void) );
65 setProgArgv(int argc, char *argv[])
67 /* Usually this is done by startupHaskell, so we don't need to call this.
68 However, sometimes Hugs wants to change the arguments which Haskell
69 getArgs >>= ... will be fed. So you can do that by calling here
70 _after_ calling startupHaskell.
77 getProgArgv(int *argc, char **argv[])
85 startupHaskell(int argc, char *argv[], void (*init_root)(void))
87 /* To avoid repeated initialisations of the RTS */
88 if (rts_has_started_up)
93 /* The very first thing we do is grab the start time...just in case we're
94 * collecting timing statistics.
100 * The parallel system needs to be initialised and synchronised before
101 * the program is run.
103 startupParallelSystem(argv);
105 if (*argv[0] == '-') { /* Strip off mainPE flag argument */
110 argv[1] = argv[0]; /* ignore the nPEs argument */
114 /* Set the RTS flags to default values. */
115 initRtsFlagsDefaults();
117 /* Call the user hook to reset defaults, if present */
120 /* Parse the flags, separating the RTS flags from the programs args */
121 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
126 /* NB: this really must be done after processing the RTS flags */
127 IF_PAR_DEBUG(verbose,
128 fprintf(stderr, "==== Synchronising system (%d PEs)\n", nPEs));
129 synchroniseSystem(); // calls initParallelSystem etc
132 /* initialise scheduler data structures (needs to be done before
138 /* And start GranSim profiling if required: */
139 if (RtsFlags.GranFlags.GranSimStats.Full)
140 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
142 /* And start GUM profiling if required: */
143 if (RtsFlags.ParFlags.ParStats.Full)
144 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
145 #endif /* PAR || GRAN */
147 /* initialize the storage manager */
150 /* initialise the stable pointer table */
151 initStablePtrTable();
153 #if defined(PROFILING) || defined(DEBUG)
157 /* run the per-module initialisation code */
158 initModules(init_root);
160 #if defined(PROFILING) || defined(DEBUG)
164 /* start the ticker */
165 install_vtalrm_handler();
166 initialize_virtual_timer(TICK_MILLISECS);
168 /* start our haskell execution tasks */
173 /* Initialise the stats department */
176 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
177 /* Initialise the user signal handler set */
179 /* Set up handler to run on SIGINT, etc. */
180 init_default_handlers();
183 #ifdef RTS_GTK_FRONTPANEL
184 if (RtsFlags.GcFlags.frontpanel) {
189 /* Record initialization times */
193 /* -----------------------------------------------------------------------------
194 Per-module initialisation
196 This process traverses all the compiled modules in the program
197 starting with "Main", and performing per-module initialisation for
200 So far, two things happen at initialisation time:
202 - we register stable names for each foreign-exported function
203 in that module. This prevents foreign-exported entities, and
204 things they depend on, from being garbage collected.
206 - we supply a unique integer to each statically declared cost
207 centre and cost centre stack in the program.
209 The code generator inserts a small function "__init_<module>" in each
210 module and calls the registration functions in each of the modules
211 it imports. So, if we call "__init_PrelMain", each reachable module in the
212 program will be registered (because PrelMain.mainIO calls Main.main).
214 The init* functions are compiled in the same way as STG code,
215 i.e. without normal C call/return conventions. Hence we must use
216 StgRun to call this stuff.
217 -------------------------------------------------------------------------- */
219 /* The init functions use an explicit stack...
221 #define INIT_STACK_SIZE (BLOCK_SIZE * 4)
222 F_ *init_stack = NULL;
226 initModules ( void (*init_root)(void) )
231 #define cap MainRegTable
235 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
236 init_stack[init_sp++] = (F_)stg_init_ret;
237 init_stack[init_sp++] = (F_)__init_Prelude;
238 if (init_root != NULL) {
239 init_stack[init_sp++] = (F_)init_root;
242 cap.rSp = (P_)(init_stack + init_sp);
243 StgRun((StgFunPtr)stg_init, &cap);
246 /* -----------------------------------------------------------------------------
247 * Shutting down the RTS - two ways of doing this, one which
248 * calls exit(), one that doesn't.
250 * (shutdownHaskellAndExit() is called by System.exitWith).
251 * -----------------------------------------------------------------------------
254 shutdownHaskellAndExit(int n)
259 /* really exit (stg_exit() would call shutdownParallelSystem() again) */
267 shutdownHaskell(void)
269 if (!rts_has_started_up)
272 /* start timing the shutdown */
276 /* Finalize any remaining weak pointers */
277 finalizeWeakPointersNow();
281 /* end_gr_simulation prints global stats if requested -- HWL */
282 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
286 /* stop all running tasks */
289 /* stop the ticker */
290 initialize_virtual_timer(0);
292 /* reset the standard file descriptors to blocking mode */
293 resetNonBlockingFd(0);
294 resetNonBlockingFd(1);
295 resetNonBlockingFd(2);
298 /* controlled exit; good thread! */
299 shutdownParallelSystem(0);
301 /* global statistics in parallel system */
305 /* stop timing the shutdown, we're about to print stats */
308 /* clean up things from the storage manager's point of view.
309 * also outputs the stats (+RTS -s) info.
313 #ifdef RTS_GTK_FRONTPANEL
314 if (RtsFlags.GcFlags.frontpanel) {
319 #if defined(PROFILING) || defined(DEBUG)
323 #if defined(PROFILING)
324 report_ccs_profiling();
327 #if defined(TICKY_TICKY)
328 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
331 rts_has_started_up=0;
335 * called from STG-land to exit the program
339 static int exit_started=rtsFalse;
346 /* HACK: avoid a loop when exiting due to a stupid error */
349 exit_started=rtsTrue;
351 IF_PAR_DEBUG(verbose, fprintf(stderr,"==-- stg_exit %d on [%x]...", n, mytid));
352 shutdownParallelSystem(n);