1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.31 2000/03/08 17:48:24 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
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"
25 #if defined(PROFILING) || defined(DEBUG)
27 # include "ProfHeap.h"
31 #include "GranSimRts.h"
32 #include "ParallelRts.h"
44 struct RTS_FLAGS RtsFlags;
46 static int rts_has_started_up = 0;
48 static ullong startTime = 0;
51 static void initModules ( void );
54 startupHaskell(int argc, char *argv[])
56 #ifdef ENABLE_WIN32_DLL_SUPPORT
60 /* To avoid repeated initialisations of the RTS */
61 if (rts_has_started_up)
66 /* The very first thing we do is grab the start time...just in case we're
67 * collecting timing statistics.
73 * The parallel system needs to be initialised and synchronised before
76 fprintf(stderr, "startupHaskell: argv[0]=%s\n", argv[0]);
77 if (*argv[0] == '-') { /* Look to see whether we're the Main Thread */
78 IAmMainThread = rtsTrue;
79 argv++; argc--; /* Strip off flag argument */
80 // IF_PAR_DEBUG(verbose,
81 fprintf(stderr, "[%x] I am Main Thread\n", mytid);
84 * Grab the number of PEs out of the argument vector, and
85 * eliminate it from further argument processing.
90 initEachPEHook(); /* HWL: hook to be execed on each PE */
93 /* Set the RTS flags to default values. */
94 initRtsFlagsDefaults();
96 /* Call the user hook to reset defaults, if present */
99 /* Parse the flags, separating the RTS flags from the programs args */
100 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
105 /* NB: this really must be done after processing the RTS flags */
106 fprintf(stderr, "Synchronising system (%d PEs)\n", nPEs);
107 SynchroniseSystem(); // calls initParallelSystem etc
110 /* initialise scheduler data structures (needs to be done before
116 /* And start GranSim profiling if required: */
117 if (RtsFlags.GranFlags.GranSimStats.Full)
118 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
120 /* And start GUM profiling if required: */
121 if (RtsFlags.ParFlags.ParStats.Full)
122 init_gr_simulation(rts_argc, rts_argv, prog_argc, prog_argv);
123 #endif /* PAR || GRAN */
125 /* initialize the storage manager */
128 /* initialise the stable pointer table */
129 initStablePtrTable();
131 #if defined(PROFILING) || defined(DEBUG)
135 /* run the per-module initialisation code */
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 /* When the RTS and Prelude live in separate DLLs,
162 we need to patch up the char- and int-like tables
163 that the RTS keep after both DLLs have been loaded,
164 filling in the tables with references to where the
165 static info tables have been loaded inside the running
168 #ifdef ENABLE_WIN32_DLL_SUPPORT
170 (CHARLIKE_closure[i]).header.info = (const StgInfoTable*)&Czh_static_info;
173 (INTLIKE_closure[i]).header.info = (const StgInfoTable*)&Izh_static_info;
176 /* Record initialization times */
180 /* -----------------------------------------------------------------------------
181 Per-module initialisation
183 This process traverses all the compiled modules in the program
184 starting with "Main", and performing per-module initialisation for
187 So far, two things happen at initialisation time:
189 - we register stable names for each foreign-exported function
190 in that module. This prevents foreign-exported entities, and
191 things they depend on, from being garbage collected.
193 - we supply a unique integer to each statically declared cost
194 centre and cost centre stack in the program.
196 The code generator inserts a small function "__init_<moddule>" in each
197 module and calls the registration functions in each of the modules
198 it imports. So, if we call "__init_Main", each reachable module in the
199 program will be registered.
201 The init* functions are compiled in the same way as STG code,
202 i.e. without normal C call/return conventions. Hence we must use
203 StgRun to call this stuff.
204 -------------------------------------------------------------------------- */
206 /* The init functions use an explicit stack...
208 #define INIT_STACK_SIZE (BLOCK_SIZE * 4)
214 /* this storage will be reclaimed by the garbage collector,
217 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
219 StgRun((StgFunPtr)stg_init, NULL/* no reg table */);
222 /* -----------------------------------------------------------------------------
223 * Shutting down the RTS - two ways of doing this, one which
224 * calls exit(), one that doesn't.
226 * (shutdownHaskellAndExit() is called by System.exitWith).
227 * -----------------------------------------------------------------------------
230 shutdownHaskellAndExit(int n)
238 shutdownHaskell(void)
240 if (!rts_has_started_up)
243 /* start timing the shutdown */
247 /* Finalize any remaining weak pointers */
248 finalizeWeakPointersNow();
252 /* end_gr_simulation prints global stats if requested -- HWL */
253 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
257 /* stop all running tasks */
260 /* stop the ticker */
261 initialize_virtual_timer(0);
263 /* reset the standard file descriptors to blocking mode */
264 resetNonBlockingFd(0);
265 resetNonBlockingFd(1);
266 resetNonBlockingFd(2);
268 /* stop timing the shutdown, we're about to print stats */
271 /* clean up things from the storage manager's point of view.
272 * also outputs the stats (+RTS -s) info.
276 #if defined(PROFILING) || defined(DEBUG)
280 #if defined(PROFILING)
281 report_ccs_profiling();
284 #if defined(TICKY_TICKY)
285 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
288 rts_has_started_up=0;
291 shutdownParallelSystem(0);
297 * called from STG-land to exit the program