[project @ 2004-02-12 02:04:59 by mthomas]
[ghc-hetmet.git] / ghc / rts / Main.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Main.c,v 1.40 2003/09/21 22:20:55 wolfgang Exp $
3  *
4  * (c) The GHC Team 1998-2000
5  *
6  * Main function for a standalone Haskell program.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #define COMPILING_RTS_MAIN
11
12 #include "PosixSource.h"
13 #include "Rts.h"
14 #include "RtsAPI.h"
15 #include "SchedAPI.h"
16 #include "Schedule.h"
17 #include "RtsFlags.h"
18 #include "RtsUtils.h"
19 #include "Prelude.h"
20 #include <stdlib.h>
21
22 #ifdef DEBUG
23 # include "Printer.h"   /* for printing        */
24 #endif
25
26 #ifdef INTERPRETER
27 # include "Assembler.h"
28 #endif
29
30 #ifdef PAR
31 # include "Parallel.h"
32 # include "ParallelRts.h"
33 # include "LLC.h"
34 #endif
35
36 #if defined(GRAN) || defined(PAR)
37 # include "GranSimRts.h"
38 #endif
39
40 #ifdef HAVE_WINDOWS_H
41 # include <windows.h>
42 #endif
43
44 extern void __stginit_ZCMain(void);
45
46 /* Hack: we assume that we're building a batch-mode system unless 
47  * INTERPRETER is set
48  */
49 #ifndef INTERPRETER /* Hack */
50 int main(int argc, char *argv[])
51 {
52     int exit_status;
53     SchedulerStatus status;
54     /* all GranSim/GUM init is done in startupHaskell; sets IAmMainThread! */
55
56     startupHaskell(argc,argv,__stginit_ZCMain);
57
58     /* kick off the computation by creating the main thread with a pointer
59        to mainIO_closure representing the computation of the overall program;
60        then enter the scheduler with this thread and off we go;
61       
62        the same for GranSim (we have only one instance of this code)
63
64        in a parallel setup, where we have many instances of this code
65        running on different PEs, we should do this only for the main PE
66        (IAmMainThread is set in startupHaskell) 
67     */
68
69 #  if defined(PAR)
70
71 #   if defined(DEBUG)
72     { /* a wait loop to allow attachment of gdb to UNIX threads */
73       nat i, j, s;
74
75       for (i=0, s=0; i<(nat)RtsFlags.ParFlags.wait; i++)
76         for (j=0; j<1000000; j++) 
77           s += j % 65536;
78     }
79     IF_PAR_DEBUG(verbose,
80                  belch("Passed wait loop"));
81 #   endif
82
83     if (IAmMainThread == rtsTrue) {
84       IF_PAR_DEBUG(verbose,
85                    fprintf(stderr, "==== [%x] Main Thread Started ...\n", mytid));
86
87       /* ToDo: Dump event for the main thread */
88       status = rts_mainLazyIO((HaskellObj)mainIO_closure, NULL);
89     } else {
90       /* Just to show we're alive */
91       IF_PAR_DEBUG(verbose,
92                    fprintf(stderr, "== [%x] Non-Main PE enters scheduler via taskStart() without work ...\n",
93                            mytid));
94      
95       /* all non-main threads enter the scheduler without work */
96       taskStart();       
97       status = Success;  // declare victory (see shutdownParallelSystem)
98     }
99
100 #  elif defined(GRAN)
101
102     /* ToDo: Dump event for the main thread */
103     status = rts_mainLazyIO(mainIO_closure, NULL);
104
105 #  else /* !PAR && !GRAN */
106
107     /* ToDo: want to start with a larger stack size */
108     rts_lock();
109     status = rts_evalLazyIO((HaskellObj)mainIO_closure, NULL);
110     rts_unlock();
111
112 #  endif /* !PAR && !GRAN */
113
114     /* check the status of the entire Haskell computation */
115     switch (status) {
116     case Killed:
117       prog_belch("main thread exited (uncaught exception)");
118       exit_status = EXIT_KILLED;
119       break;
120     case Interrupted:
121       prog_belch("interrupted");
122       exit_status = EXIT_INTERRUPTED;
123       break;
124     case Success:
125       exit_status = EXIT_SUCCESS;
126       break;
127 #if defined(PAR)
128     case NoStatus:
129       prog_belch("main thread PE killed; probably due to failure of another PE; check /tmp/pvml...");
130       exit_status = EXIT_KILLED;
131       break;
132 #endif 
133     default:
134       barf("main thread completed with invalid status");
135     }
136     shutdownHaskellAndExit(exit_status);
137     return 0; /* never reached, keep gcc -Wall happy */
138 }
139 # endif /* BATCH_MODE */