1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.73 2003/04/01 15:05: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"
60 struct RTS_FLAGS RtsFlags;
62 // Count of how many outstanding hs_init()s there have been.
63 static int hs_init_count = 0;
65 /* -----------------------------------------------------------------------------
67 -------------------------------------------------------------------------- */
70 hs_init(int *argc, char **argv[])
73 if (hs_init_count > 1) {
74 // second and subsequent inits are ignored
78 /* The very first thing we do is grab the start time...just in case we're
79 * collecting timing statistics.
85 * The parallel system needs to be initialised and synchronised before
88 startupParallelSystem(argv);
90 if (*argv[0] == '-') { /* Strip off mainPE flag argument */
95 argv[1] = argv[0]; /* ignore the nPEs argument */
99 /* Set the RTS flags to default values. */
100 initRtsFlagsDefaults();
102 /* Call the user hook to reset defaults, if present */
105 /* Parse the flags, separating the RTS flags from the programs args */
106 if (argc != NULL && argv != NULL) {
107 setupRtsFlags(argc, *argv, &rts_argc, rts_argv);
113 /* NB: this really must be done after processing the RTS flags */
114 IF_PAR_DEBUG(verbose,
115 fprintf(stderr, "==== Synchronising system (%d PEs)\n", nPEs));
116 synchroniseSystem(); // calls initParallelSystem etc
119 /* initialise scheduler data structures (needs to be done before
125 /* And start GranSim profiling if required: */
126 if (RtsFlags.GranFlags.GranSimStats.Full)
127 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
129 /* And start GUM profiling if required: */
130 if (RtsFlags.ParFlags.ParStats.Full)
131 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
132 #endif /* PAR || GRAN */
134 /* initialize the storage manager */
137 /* initialise the stable pointer table */
138 initStablePtrTable();
140 /* initialise thread label table (tso->char*) */
141 initThreadLabelTable();
143 #if defined(PROFILING) || defined(DEBUG)
147 /* start the virtual timer 'subsystem'. */
148 startTimer(TICK_MILLISECS);
150 /* Initialise the stats department */
153 #if defined(RTS_USER_SIGNALS)
154 /* Initialise the user signal handler set */
156 /* Set up handler to run on SIGINT, etc. */
157 initDefaultHandlers();
160 #if defined(mingw32_TARGET_OS)
164 #ifdef RTS_GTK_FRONTPANEL
165 if (RtsFlags.GcFlags.frontpanel) {
170 /* Record initialization times */
174 // Compatibility interface
176 startupHaskell(int argc, char *argv[], void (*init_root)(void))
178 hs_init(&argc, &argv);
179 hs_add_root(init_root);
183 /* -----------------------------------------------------------------------------
184 Getting/Setting the program's arguments.
186 These are used by System.Environment.
187 -------------------------------------------------------------------------- */
190 getProgArgv(int *argc, char **argv[])
197 setProgArgv(int argc, char *argv[])
199 /* Usually this is done by startupHaskell, so we don't need to call this.
200 However, sometimes Hugs wants to change the arguments which Haskell
201 getArgs >>= ... will be fed. So you can do that by calling here
202 _after_ calling startupHaskell.
208 /* -----------------------------------------------------------------------------
209 Per-module initialisation
211 This process traverses all the compiled modules in the program
212 starting with "Main", and performing per-module initialisation for
215 So far, two things happen at initialisation time:
217 - we register stable names for each foreign-exported function
218 in that module. This prevents foreign-exported entities, and
219 things they depend on, from being garbage collected.
221 - we supply a unique integer to each statically declared cost
222 centre and cost centre stack in the program.
224 The code generator inserts a small function "__stginit_<module>" in each
225 module and calls the registration functions in each of the modules it
228 The init* functions are compiled in the same way as STG code,
229 i.e. without normal C call/return conventions. Hence we must use
230 StgRun to call this stuff.
231 -------------------------------------------------------------------------- */
233 /* The init functions use an explicit stack...
235 #define INIT_STACK_BLOCKS 4
236 static F_ *init_stack = NULL;
239 hs_add_root(void (*init_root)(void))
245 #define cap MainCapability
249 if (hs_init_count <= 0) {
250 barf("hs_add_root() must be called after hs_init()");
254 bd = allocGroup(INIT_STACK_BLOCKS);
255 init_stack = (F_ *)bd->start;
256 init_stack[init_sp++] = (F_)stg_init_ret;
257 if (init_root != NULL) {
258 init_stack[init_sp++] = (F_)init_root;
261 cap.r.rSp = (P_)(init_stack + init_sp);
262 StgRun((StgFunPtr)stg_init, &cap.r);
266 #if defined(PROFILING) || defined(DEBUG)
267 // This must be done after module initialisation.
268 // ToDo: make this work in the presence of multiple hs_add_root()s.
273 /* -----------------------------------------------------------------------------
274 Shutting down the RTS
275 -------------------------------------------------------------------------- */
280 if (hs_init_count <= 0) {
281 barf("too many hs_exit()s");
284 if (hs_init_count > 0) {
285 // ignore until it's the last one
289 /* start timing the shutdown */
292 /* stop all running tasks */
296 /* Finalize any remaining weak pointers */
297 finalizeWeakPointersNow();
301 /* end_gr_simulation prints global stats if requested -- HWL */
302 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
306 /* stop the ticker */
309 /* reset the standard file descriptors to blocking mode */
310 resetNonBlockingFd(0);
311 resetNonBlockingFd(1);
312 resetNonBlockingFd(2);
315 /* controlled exit; good thread! */
316 shutdownParallelSystem(0);
318 /* global statistics in parallel system */
322 /* stop timing the shutdown, we're about to print stats */
325 /* clean up things from the storage manager's point of view.
326 * also outputs the stats (+RTS -s) info.
330 #ifdef RTS_GTK_FRONTPANEL
331 if (RtsFlags.GcFlags.frontpanel) {
336 #if defined(PROFILING)
337 reportCCSProfiling();
340 #if defined(PROFILING) || defined(DEBUG)
345 // Originally, this was in report_ccs_profiling(). Now, retainer
346 // profiling might tack some extra stuff on to the end of this file
347 // during endProfiling().
351 #if defined(TICKY_TICKY)
352 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
355 #if defined(mingw32_TARGET_OS)
360 // Compatibility interfaces
362 shutdownHaskell(void)
368 shutdownHaskellAndExit(int n)
370 if (hs_init_count == 1) {
374 /* really exit (stg_exit() would call shutdownParallelSystem() again) */
383 * called from STG-land to exit the program
387 static int exit_started=rtsFalse;
394 /* HACK: avoid a loop when exiting due to a stupid error */
397 exit_started=rtsTrue;
399 IF_PAR_DEBUG(verbose, fprintf(stderr,"==-- stg_exit %d on [%x]...", n, mytid));
400 shutdownParallelSystem(n);