1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2000
5 * Main function for a standalone Haskell program.
7 * ---------------------------------------------------------------------------*/
9 #define COMPILING_RTS_MAIN
11 #include "PosixSource.h"
23 # include "Printer.h" /* for printing */
27 # include "Parallel.h"
28 # include "ParallelRts.h"
32 #if defined(GRAN) || defined(PAR)
33 # include "GranSimRts.h"
40 extern void __stginit_ZCMain(void);
42 /* Hack: we assume that we're building a batch-mode system unless
45 #ifndef INTERPRETER /* Hack */
46 int main(int argc, char *argv[])
49 SchedulerStatus status;
50 /* all GranSim/GUM init is done in startupHaskell; sets IAmMainThread! */
52 startupHaskell(argc,argv,__stginit_ZCMain);
54 /* kick off the computation by creating the main thread with a pointer
55 to mainIO_closure representing the computation of the overall program;
56 then enter the scheduler with this thread and off we go;
58 the same for GranSim (we have only one instance of this code)
60 in a parallel setup, where we have many instances of this code
61 running on different PEs, we should do this only for the main PE
62 (IAmMainThread is set in startupHaskell)
68 { /* a wait loop to allow attachment of gdb to UNIX threads */
71 for (i=0, s=0; i<(nat)RtsFlags.ParFlags.wait; i++)
72 for (j=0; j<1000000; j++)
76 belch("Passed wait loop"));
79 if (IAmMainThread == rtsTrue) {
81 debugBelch("==== [%x] Main Thread Started ...\n", mytid));
83 /* ToDo: Dump event for the main thread */
84 status = rts_mainLazyIO((HaskellObj)mainIO_closure, NULL);
86 /* Just to show we're alive */
88 debugBelch("== [%x] Non-Main PE enters scheduler via taskStart() without work ...\n",
91 /* all non-main threads enter the scheduler without work */
93 status = Success; // declare victory (see shutdownParallelSystem)
98 /* ToDo: Dump event for the main thread */
99 status = rts_mainLazyIO(mainIO_closure, NULL);
101 # else /* !PAR && !GRAN */
103 /* ToDo: want to start with a larger stack size */
105 void *cap = rts_lock();
106 cap = rts_evalLazyIO(cap,(HaskellObj)mainIO_closure, NULL);
107 status = rts_getSchedStatus(cap);
111 # endif /* !PAR && !GRAN */
113 /* check the status of the entire Haskell computation */
116 errorBelch("main thread exited (uncaught exception)");
117 exit_status = EXIT_KILLED;
120 errorBelch("interrupted");
121 exit_status = EXIT_INTERRUPTED;
124 exit_status = EXIT_SUCCESS;
128 errorBelch("main thread PE killed; probably due to failure of another PE; check /tmp/pvml...");
129 exit_status = EXIT_KILLED;
133 barf("main thread completed with invalid status");
135 shutdownHaskellAndExit(exit_status);
136 return 0; /* never reached, keep gcc -Wall happy */
138 # endif /* BATCH_MODE */