1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.75 2003/08/22 22:24:15 sof Exp $
4 * (c) The GHC Team, 1998-2002
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 */
20 #include "Timer.h" /* startTimer, stopTimer */
24 #include "StgStartup.h"
25 #include "Prelude.h" /* fixupRTStoPreludeRefs */
28 #include "ThreadLabels.h"
30 #if defined(RTS_GTK_FRONTPANEL)
31 #include "FrontPanel.h"
34 #if defined(PROFILING) || defined(DEBUG)
35 # include "Profiling.h"
36 # include "ProfHeap.h"
37 # include "RetainerProfile.h"
41 # include "GranSimRts.h"
44 #if defined(GRAN) || defined(PAR)
45 # include "ParallelRts.h"
49 # include "Parallel.h"
53 #if defined(mingw32_TARGET_OS)
54 #include "win32/AsyncIO.h"
64 struct RTS_FLAGS RtsFlags;
66 // Count of how many outstanding hs_init()s there have been.
67 static int hs_init_count = 0;
69 /* -----------------------------------------------------------------------------
71 -------------------------------------------------------------------------- */
74 hs_init(int *argc, char **argv[])
77 if (hs_init_count > 1) {
78 // second and subsequent inits are ignored
82 /* The very first thing we do is grab the start time...just in case we're
83 * collecting timing statistics.
89 * The parallel system needs to be initialised and synchronised before
92 startupParallelSystem(argv);
94 if (*argv[0] == '-') { /* Strip off mainPE flag argument */
99 argv[1] = argv[0]; /* ignore the nPEs argument */
103 /* Set the RTS flags to default values. */
104 initRtsFlagsDefaults();
106 /* Call the user hook to reset defaults, if present */
109 /* Parse the flags, separating the RTS flags from the programs args */
110 if (argc != NULL && argv != NULL) {
111 setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
117 /* NB: this really must be done after processing the RTS flags */
118 IF_PAR_DEBUG(verbose,
119 fprintf(stderr, "==== Synchronising system (%d PEs)\n", nPEs));
120 synchroniseSystem(); // calls initParallelSystem etc
123 /* initialise scheduler data structures (needs to be done before
129 /* And start GranSim profiling if required: */
130 if (RtsFlags.GranFlags.GranSimStats.Full)
131 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
133 /* And start GUM profiling if required: */
134 if (RtsFlags.ParFlags.ParStats.Full)
135 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
136 #endif /* PAR || GRAN */
138 /* initialize the storage manager */
141 /* initialise the stable pointer table */
142 initStablePtrTable();
144 /* initialise thread label table (tso->char*) */
145 initThreadLabelTable();
147 #if defined(PROFILING) || defined(DEBUG)
151 /* start the virtual timer 'subsystem'. */
152 startTimer(TICK_MILLISECS);
154 /* Initialise the stats department */
157 #if defined(RTS_USER_SIGNALS)
158 /* Initialise the user signal handler set */
160 /* Set up handler to run on SIGINT, etc. */
161 initDefaultHandlers();
164 #if defined(mingw32_TARGET_OS)
168 #ifdef RTS_GTK_FRONTPANEL
169 if (RtsFlags.GcFlags.frontpanel) {
175 setlocale(LC_ALL,"");
178 /* Record initialization times */
182 // Compatibility interface
184 startupHaskell(int argc, char *argv[], void (*init_root)(void))
186 hs_init(&argc, &argv);
187 hs_add_root(init_root);
191 /* -----------------------------------------------------------------------------
192 Getting/Setting the program's arguments.
194 These are used by System.Environment.
195 -------------------------------------------------------------------------- */
198 getProgArgv(int *argc, char **argv[])
200 if (argc) { *argc = prog_argc; }
201 if (argv) { *argv = prog_argv; }
205 setProgArgv(int argc, char *argv[])
207 /* Usually this is done by startupHaskell, so we don't need to call this.
208 However, sometimes Hugs wants to change the arguments which Haskell
209 getArgs >>= ... will be fed. So you can do that by calling here
210 _after_ calling startupHaskell.
216 /* -----------------------------------------------------------------------------
217 Per-module initialisation
219 This process traverses all the compiled modules in the program
220 starting with "Main", and performing per-module initialisation for
223 So far, two things happen at initialisation time:
225 - we register stable names for each foreign-exported function
226 in that module. This prevents foreign-exported entities, and
227 things they depend on, from being garbage collected.
229 - we supply a unique integer to each statically declared cost
230 centre and cost centre stack in the program.
232 The code generator inserts a small function "__stginit_<module>" in each
233 module and calls the registration functions in each of the modules it
236 The init* functions are compiled in the same way as STG code,
237 i.e. without normal C call/return conventions. Hence we must use
238 StgRun to call this stuff.
239 -------------------------------------------------------------------------- */
241 /* The init functions use an explicit stack...
243 #define INIT_STACK_BLOCKS 4
244 static F_ *init_stack = NULL;
247 hs_add_root(void (*init_root)(void))
253 #define cap MainCapability
257 if (hs_init_count <= 0) {
258 barf("hs_add_root() must be called after hs_init()");
262 bd = allocGroup(INIT_STACK_BLOCKS);
263 init_stack = (F_ *)bd->start;
264 init_stack[init_sp++] = (F_)stg_init_ret;
265 if (init_root != NULL) {
266 init_stack[init_sp++] = (F_)init_root;
269 cap.r.rSp = (P_)(init_stack + init_sp);
270 StgRun((StgFunPtr)stg_init, &cap.r);
274 #if defined(PROFILING) || defined(DEBUG)
275 // This must be done after module initialisation.
276 // ToDo: make this work in the presence of multiple hs_add_root()s.
281 /* -----------------------------------------------------------------------------
282 Shutting down the RTS
283 -------------------------------------------------------------------------- */
288 if (hs_init_count <= 0) {
289 barf("too many hs_exit()s");
292 if (hs_init_count > 0) {
293 // ignore until it's the last one
297 /* start timing the shutdown */
300 /* stop all running tasks */
304 /* Finalize any remaining weak pointers */
305 finalizeWeakPointersNow();
309 /* end_gr_simulation prints global stats if requested -- HWL */
310 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
314 /* stop the ticker */
317 /* reset the standard file descriptors to blocking mode */
318 resetNonBlockingFd(0);
319 resetNonBlockingFd(1);
320 resetNonBlockingFd(2);
323 /* controlled exit; good thread! */
324 shutdownParallelSystem(0);
326 /* global statistics in parallel system */
330 /* stop timing the shutdown, we're about to print stats */
333 /* clean up things from the storage manager's point of view.
334 * also outputs the stats (+RTS -s) info.
338 #ifdef RTS_GTK_FRONTPANEL
339 if (RtsFlags.GcFlags.frontpanel) {
344 #if defined(PROFILING)
345 reportCCSProfiling();
348 #if defined(PROFILING) || defined(DEBUG)
353 // Originally, this was in report_ccs_profiling(). Now, retainer
354 // profiling might tack some extra stuff on to the end of this file
355 // during endProfiling().
359 #if defined(TICKY_TICKY)
360 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
363 #if defined(mingw32_TARGET_OS)
368 // Compatibility interfaces
370 shutdownHaskell(void)
376 shutdownHaskellAndExit(int n)
378 if (hs_init_count == 1) {
382 /* really exit (stg_exit() would call shutdownParallelSystem() again) */
391 * called from STG-land to exit the program
395 static int exit_started=rtsFalse;
402 /* HACK: avoid a loop when exiting due to a stupid error */
405 exit_started=rtsTrue;
407 IF_PAR_DEBUG(verbose, fprintf(stderr,"==-- stg_exit %d on [%x]...", n, mytid));
408 shutdownParallelSystem(n);