1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.25 1999/12/20 10:34:37 simonpj 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 #if defined(PROFILING)
25 # include "ProfHeap.h"
37 struct RTS_FLAGS RtsFlags;
39 static int rts_has_started_up = 0;
42 startupHaskell(int argc, char *argv[])
44 #ifdef ENABLE_WIN32_DLL_SUPPORT
48 /* To avoid repeated initialisations of the RTS */
49 if (rts_has_started_up)
55 int nPEs = 0; /* Number of PEs */
58 /* The very first thing we do is grab the start time...just in case we're
59 * collecting timing statistics.
65 *The parallel system needs to be initialised and synchronised before
68 if (*argv[0] == '-') { /* Look to see whether we're the Main Thread */
69 IAmMainThread = rtsTrue;
70 argv++; argc--; /* Strip off flag argument */
71 /* fprintf(stderr, "I am Main Thread\n"); */
74 * Grab the number of PEs out of the argument vector, and
75 * eliminate it from further argument processing.
80 initEachPEHook(); /* HWL: hook to be execed on each PE */
84 /* Set the RTS flags to default values. */
85 initRtsFlagsDefaults();
87 /* Call the user hook to reset defaults, if present */
90 /* Parse the flags, separating the RTS flags from the programs args */
91 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
96 /* Initialise the parallel system -- before initHeap! */
98 /* And start GranSim profiling if required: omitted for now
99 *if (Rtsflags.ParFlags.granSimStats)
100 *init_gr_profiling(rts_argc, rts_argv, prog_argc, prog_argv);
104 /* initialise scheduler data structures (needs to be done before
109 /* initialize the storage manager */
112 /* initialise the stable pointer table */
113 initStablePtrTable();
115 #if defined(PROFILING) || defined(DEBUG)
119 /* start the ticker */
120 install_vtalrm_handler();
122 initialize_virtual_timer(TICK_MILLISECS);
125 /* start our haskell execution tasks */
130 /* Initialise the stats department */
133 #if !defined(mingw32_TARGET_OS) && !defined(PAR)
134 /* Initialise the user signal handler set */
136 /* Set up handler to run on SIGINT */
137 init_shutdown_handler();
140 /* When the RTS and Prelude live in separate DLLs,
141 we need to patch up the char- and int-like tables
142 that the RTS keep after both DLLs have been loaded,
143 filling in the tables with references to where the
144 static info tables have been loaded inside the running
147 #ifdef ENABLE_WIN32_DLL_SUPPORT
149 (CHARLIKE_closure[i]).header.info = (const StgInfoTable*)&Czh_static_info;
152 (INTLIKE_closure[i]).header.info = (const StgInfoTable*)&Izh_static_info;
155 /* Record initialization times */
160 * Shutting down the RTS - two ways of doing this, one which
161 * calls exit(), one that doesn't.
163 * (shutdownHaskellAndExit() is called by System.exitWith).
166 shutdownHaskellAndExit(int n)
174 shutdownHaskell(void)
176 if (!rts_has_started_up)
179 /* start timing the shutdown */
182 /* Finalize any remaining weak pointers */
183 finalizeWeakPointersNow();
187 if (!RTSflags.GranFlags.granSimStats_suppressed)
191 /* stop all running tasks */
194 /* stop the ticker */
195 initialize_virtual_timer(0);
197 /* reset the standard file descriptors to blocking mode */
198 resetNonBlockingFd(0);
199 resetNonBlockingFd(1);
200 resetNonBlockingFd(2);
202 /* stop timing the shutdown, we're about to print stats */
205 /* clean up things from the storage manager's point of view.
206 * also outputs the stats (+RTS -s) info.
210 #if defined(PROFILING) || defined(DEBUG)
214 #if defined(PROFILING)
215 report_ccs_profiling( );
218 #if defined(TICKY_TICKY)
219 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
222 rts_has_started_up=0;
227 * called from STG-land to exit the program