Massive patch for the first months work adding System FC to GHC #15
[ghc-hetmet.git] / rts / Main.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 1998-2000
4  *
5  * Main function for a standalone Haskell program.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #define COMPILING_RTS_MAIN
10
11 #include "PosixSource.h"
12 #include "Rts.h"
13 #include "RtsAPI.h"
14 #include "SchedAPI.h"
15 #include "Schedule.h"
16 #include "RtsFlags.h"
17 #include "RtsUtils.h"
18 #include "Prelude.h"
19 #include "Task.h"
20 #include <stdlib.h>
21
22 #ifdef DEBUG
23 # include "Printer.h"   /* for printing        */
24 #endif
25
26 #ifdef PAR
27 # include "Parallel.h"
28 # include "ParallelRts.h"
29 # include "LLC.h"
30 #endif
31
32 #if defined(GRAN) || defined(PAR)
33 # include "GranSimRts.h"
34 #endif
35
36 #ifdef HAVE_WINDOWS_H
37 # include <windows.h>
38 #endif
39
40 extern void __stginit_ZCMain(void);
41
42 /* Hack: we assume that we're building a batch-mode system unless 
43  * INTERPRETER is set
44  */
45 #ifndef INTERPRETER /* Hack */
46 int main(int argc, char *argv[])
47 {
48     int exit_status;
49     SchedulerStatus status;
50     /* all GranSim/GUM init is done in startupHaskell; sets IAmMainThread! */
51
52     startupHaskell(argc,argv,__stginit_ZCMain);
53
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;
57       
58        the same for GranSim (we have only one instance of this code)
59
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) 
63     */
64
65 #  if defined(PAR)
66
67 #   if defined(DEBUG)
68     { /* a wait loop to allow attachment of gdb to UNIX threads */
69       nat i, j, s;
70
71       for (i=0, s=0; i<(nat)RtsFlags.ParFlags.wait; i++)
72         for (j=0; j<1000000; j++) 
73           s += j % 65536;
74     }
75     IF_PAR_DEBUG(verbose,
76                  belch("Passed wait loop"));
77 #   endif
78
79     if (IAmMainThread == rtsTrue) {
80       IF_PAR_DEBUG(verbose,
81                    debugBelch("==== [%x] Main Thread Started ...\n", mytid));
82
83       /* ToDo: Dump event for the main thread */
84       status = rts_mainLazyIO((HaskellObj)mainIO_closure, NULL);
85     } else {
86       /* Just to show we're alive */
87       IF_PAR_DEBUG(verbose,
88                    debugBelch("== [%x] Non-Main PE enters scheduler via taskStart() without work ...\n",
89                            mytid));
90      
91       /* all non-main threads enter the scheduler without work */
92       taskStart();       
93       status = Success;  // declare victory (see shutdownParallelSystem)
94     }
95
96 #  elif defined(GRAN)
97
98     /* ToDo: Dump event for the main thread */
99     status = rts_mainLazyIO(mainIO_closure, NULL);
100
101 #  else /* !PAR && !GRAN */
102
103     /* ToDo: want to start with a larger stack size */
104     { 
105         void *cap = rts_lock();
106         cap = rts_evalLazyIO(cap,(HaskellObj)(void *)mainIO_closure, NULL);
107         status = rts_getSchedStatus(cap);
108         taskTimeStamp(myTask());
109         rts_unlock(cap);
110     }
111
112 #  endif /* !PAR && !GRAN */
113
114     /* check the status of the entire Haskell computation */
115     switch (status) {
116     case Killed:
117       errorBelch("main thread exited (uncaught exception)");
118       exit_status = EXIT_KILLED;
119       break;
120     case Interrupted:
121       errorBelch("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       errorBelch("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 */