1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.39 2000/04/03 16:28:08 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 )
213 init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
214 init_stack[init_sp++] = (F_)stg_init_ret;
215 init_stack[init_sp++] = (F_)__init_Prelude;
216 if (init_root != NULL) {
217 init_stack[init_sp++] = (F_)init_root;
220 cap.rSp = (P_)(init_stack + init_sp);
221 StgRun((StgFunPtr)stg_init, &cap);
224 #endif /* !INTERPRETER */
226 /* -----------------------------------------------------------------------------
227 * Shutting down the RTS - two ways of doing this, one which
228 * calls exit(), one that doesn't.
230 * (shutdownHaskellAndExit() is called by System.exitWith).
231 * -----------------------------------------------------------------------------
234 shutdownHaskellAndExit(int n)
242 shutdownHaskell(void)
244 if (!rts_has_started_up)
247 /* start timing the shutdown */
251 /* Finalize any remaining weak pointers */
252 finalizeWeakPointersNow();
256 /* end_gr_simulation prints global stats if requested -- HWL */
257 if (!RtsFlags.GranFlags.GranSimStats.Suppressed)
261 /* stop all running tasks */
264 /* stop the ticker */
265 initialize_virtual_timer(0);
267 /* reset the standard file descriptors to blocking mode */
268 resetNonBlockingFd(0);
269 resetNonBlockingFd(1);
270 resetNonBlockingFd(2);
273 shutdownParallelSystem(0);
276 /* stop timing the shutdown, we're about to print stats */
279 /* clean up things from the storage manager's point of view.
280 * also outputs the stats (+RTS -s) info.
284 #if defined(PROFILING) || defined(DEBUG)
288 #if defined(PROFILING)
289 report_ccs_profiling();
292 #if defined(TICKY_TICKY)
293 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
296 rts_has_started_up=0;
301 * called from STG-land to exit the program