1 /* -----------------------------------------------------------------------------
2 * $Id: RtsStartup.c,v 1.11 1999/05/04 10:19:19 sof 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 */
21 #if defined(PROFILING)
24 # include "DebugProf.h"
36 struct RTS_FLAGS RtsFlags;
38 extern void startupHaskell(int argc, char *argv[])
40 static int rts_has_started_up = 0;
43 /* To avoid repeated initialisations of the RTS */
44 if (rts_has_started_up)
50 int nPEs = 0; /* Number of PEs */
53 /* The very first thing we do is grab the start time...just in case we're
54 * collecting timing statistics.
60 *The parallel system needs to be initialised and synchronised before
63 if (*argv[0] == '-') { /* Look to see whether we're the Main Thread */
64 IAmMainThread = rtsTrue;
65 argv++; argc--; /* Strip off flag argument */
66 /* fprintf(stderr, "I am Main Thread\n"); */
69 * Grab the number of PEs out of the argument vector, and
70 * eliminate it from further argument processing.
75 initEachPEHook(); /* HWL: hook to be execed on each PE */
79 /* Set the RTS flags to default values. */
80 initRtsFlagsDefaults();
82 /* Call the user hook to reset defaults, if present */
85 /* Parse the flags, separating the RTS flags from the programs args */
86 setupRtsFlags(&argc, argv, &rts_argc, rts_argv);
91 /* Initialise the parallel system -- before initHeap! */
93 /* And start GranSim profiling if required: omitted for now
94 *if (Rtsflags.ParFlags.granSimStats)
95 *init_gr_profiling(rts_argc, rts_argv, prog_argc, prog_argv);
99 /* initialize the storage manager */
102 /* initialise the stable pointer table */
103 initStablePtrTable();
105 #if defined(PROFILING) || defined(DEBUG)
109 /* Initialise the scheduler */
112 /* Initialise the stats department */
119 /* When the RTS and Prelude live in separate DLLs,
120 we need to patch up the char- and int-like tables
121 that the RTS keep after both DLLs have been loaded,
122 filling in the tables with references to where the
123 static info tables have been loaded inside the running
126 Ditto for Bool closure tbl.
128 #ifdef ENABLE_WIN32_DLL_SUPPORT
130 (CHARLIKE_closure[i]).header.info = (const StgInfoTable*)&Czh_static_info;
133 (INTLIKE_closure[i]).header.info = (const StgInfoTable*)&Izh_static_info;
136 /* Record initialization times */
141 shutdownHaskell(void)
143 /* Finalize any remaining weak pointers */
144 finalizeWeakPointersNow();
148 if (!RTSflags.GranFlags.granSimStats_suppressed)
152 /* clean up things from the storage manager's point of view */
155 #if defined(PROFILING) || defined(DEBUG)
159 #if defined(PROFILING)
160 report_ccs_profiling( );
163 #if defined(TICKY_TICKY)
164 if (RtsFlags.TickyFlags.showTickyStats) PrintTickyInfo();
168 This fflush is important, because: if "main" just returns,
169 then we will end up in pre-supplied exit code that will close
170 streams and flush buffers. In particular we have seen: it
171 will close fd 0 (stdin), then flush fd 1 (stdout), then <who
174 But if you're playing with sockets, that "close fd 0" might
175 suggest to the daemon that all is over, only to be presented
176 with more stuff on "fd 1" at the flush.
178 The fflush avoids this sad possibility.
185 * called from STG-land to exit the program cleanly