1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.38 2000/04/03 15:54:50 simonmar 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 */
26 #if defined(PROFILING) || defined(DEBUG)
27 # include "Profiling.h"
28 # include "ProfHeap.h"
32 #include "GranSimRts.h"
33 #include "ParallelRts.h"
45 struct RTS_FLAGS RtsFlags;
47 static int rts_has_started_up = 0;
49 static ullong startTime = 0;
52 EXTFUN(__init_Prelude);
53 static void initModules ( void * );
56 startupHaskell(int argc, char *argv[], void *init_root)
58 /* To avoid repeated initialisations of the RTS */
59 if (rts_has_started_up)
64 /* The very first thing we do is grab the start time...just in case we're
65 * collecting timing statistics.
71 * The parallel system needs to be initialised and synchronised before
74 fprintf(stderr, "startupHaskell: argv[0]=%s\n", argv[0]);
75 if (*argv[0] == '-') { /* Look to see whether we're the Main Thread */
76 IAmMainThread = rtsTrue;
77 argv++; argc--; /* Strip off flag argument */
78 // IF_PAR_DEBUG(verbose,
79 fprintf(stderr, "[%x] I am Main Thread\n", mytid);
82 * Grab the number of PEs out of the argument vector, and
83 * eliminate it from further argument processing.
88 initEachPEHook(); /* HWL: hook to be execed on each PE */
91 /* Set the RTS flags to default values. */
92 initRtsFlagsDefaults();
94 /* Call the user hook to reset defaults, if present */
97 /* Parse the flags, separating the RTS flags from the programs args */
98 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
103 /* NB: this really must be done after processing the RTS flags */
104 fprintf(stderr, "Synchronising system (%d PEs)\n", nPEs);
105 SynchroniseSystem(); // calls initParallelSystem etc
108 /* initialise scheduler data structures (needs to be done before
114 /* And start GranSim profiling if required: */
115 if (RtsFlags.GranFlags.GranSimStats.Full)
116 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
118 /* And start GUM profiling if required: */
119 if (RtsFlags.ParFlags.ParStats.Full)
120 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
121 #endif /* PAR || GRAN */
123 /* initialize the storage manager */
126 /* initialise the stable pointer table */
127 initStablePtrTable();
129 #if defined(PROFILING) || defined(DEBUG)
133 /* run the per-module initialisation code */
134 #if !defined(INTERPRETER)
135 initModules(init_root);
138 #if defined(PROFILING) || defined(DEBUG)
142 /* start the ticker */
143 install_vtalrm_handler();
144 initialize_virtual_timer(TICK_MILLISECS);
146 /* start our haskell execution tasks */
151 /* Initialise the stats department */
154 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
155 /* Initialise the user signal handler set */
157 /* Set up handler to run on SIGINT, etc. */
158 init_default_handlers();
161 #if !defined(INTERPRETER)
162 /* Initialise pointers from the RTS to the prelude.
163 Only for compiled code -- the interpreter
164 will call this itself later, so don't do so now.
166 fixupRTStoPreludeRefs(NULL);
169 /* Record initialization times */
173 /* -----------------------------------------------------------------------------
174 Per-module initialisation
176 This process traverses all the compiled modules in the program
177 starting with "Main", and performing per-module initialisation for
180 So far, two things happen at initialisation time:
182 - we register stable names for each foreign-exported function
183 in that module. This prevents foreign-exported entities, and
184 things they depend on, from being garbage collected.
186 - we supply a unique integer to each statically declared cost
187 centre and cost centre stack in the program.
189 The code generator inserts a small function "__init_<module>" in each
190 module and calls the registration functions in each of the modules
191 it imports. So, if we call "__init_PrelMain", each reachable module in the
192 program will be registered (because PrelMain.mainIO calls Main.main).
194 The init* functions are compiled in the same way as STG code,
195 i.e. without normal C call/return conventions. Hence we must use
196 StgRun to call this stuff.
197 -------------------------------------------------------------------------- */
201 /* The init functions use an explicit stack...
203 #define INIT_STACK_SIZE (BLOCK_SIZE * 4)
204 F_ *init_stack = NULL;
208 initModules ( void *init_root )
211 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
212 init_stack[init_sp++] = (F_)stg_init_ret;
213 init_stack[init_sp++] = (F_)__init_Prelude;
214 if (init_root != NULL) {
215 init_stack[init_sp++] = (F_)init_root;
218 MainRegTable.rSp = (P_)(init_stack + init_sp);
219 StgRun((StgFunPtr)stg_init, NULL/* no reg table */);
222 #endif /* !INTERPRETER */
224 /* -----------------------------------------------------------------------------
225 * Shutting down the RTS - two ways of doing this, one which
226 * calls exit(), one that doesn't.
228 * (shutdownHaskellAndExit() is called by System.exitWith).
229 * -----------------------------------------------------------------------------
232 shutdownHaskellAndExit(int n)
240 shutdownHaskell(void)
242 if (!rts_has_started_up)
245 /* start timing the shutdown */
249 /* Finalize any remaining weak pointers */
250 finalizeWeakPointersNow();
254 /* end_gr_simulation prints global stats if requested -- HWL */
255 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
259 /* stop all running tasks */
262 /* stop the ticker */
263 initialize_virtual_timer(0);
265 /* reset the standard file descriptors to blocking mode */
266 resetNonBlockingFd(0);
267 resetNonBlockingFd(1);
268 resetNonBlockingFd(2);
271 shutdownParallelSystem(0);
274 /* stop timing the shutdown, we're about to print stats */
277 /* clean up things from the storage manager's point of view.
278 * also outputs the stats (+RTS -s) info.
282 #if defined(PROFILING) || defined(DEBUG)
286 #if defined(PROFILING)
287 report_ccs_profiling();
290 #if defined(TICKY_TICKY)
291 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
294 rts_has_started_up=0;
299 * called from STG-land to exit the program