438110a1797d678b2ccd07507b21f0b40ecdd97f
[ghc-hetmet.git] / rts / RtsMain.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 "RtsFlags.h"
16 #include "RtsUtils.h"
17 #include "RtsMain.h"
18 #include "Prelude.h"
19 #include "Task.h"
20 #if defined(mingw32_HOST_OS)
21 #include "win32/seh_excn.h"
22 #endif
23 #include <stdlib.h>
24
25 #ifdef DEBUG
26 # include "Printer.h"   /* for printing        */
27 #endif
28
29 #ifdef HAVE_WINDOWS_H
30 # include <windows.h>
31 #endif
32
33 extern void __stginit_ZCMain(void);
34
35 /* Annoying global vars for passing parameters to real_main() below
36  * This is to get around problem with Windows SEH, see hs_main(). */
37 static int progargc;
38 static char **progargv;
39 static void (*progmain_init)(void);   /* This will be __stginit_ZCMain */
40 static StgClosure *progmain_closure;  /* This will be ZCMain_main_closure */
41
42 /* Hack: we assume that we're building a batch-mode system unless 
43  * INTERPRETER is set
44  */
45 #ifndef INTERPRETER /* Hack */
46 static void real_main(void)
47 {
48     int exit_status;
49     SchedulerStatus status;
50     /* all GranSim/GUM init is done in startupHaskell; sets IAmMainThread! */
51
52     startupHaskell(progargc,progargv,progmain_init);
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     /* ToDo: want to start with a larger stack size */
66     { 
67         Capability *cap = rts_lock();
68         cap = rts_evalLazyIO(cap,progmain_closure, NULL);
69         status = rts_getSchedStatus(cap);
70         taskTimeStamp(myTask());
71         rts_unlock(cap);
72     }
73
74     /* check the status of the entire Haskell computation */
75     switch (status) {
76     case Killed:
77       errorBelch("main thread exited (uncaught exception)");
78       exit_status = EXIT_KILLED;
79       break;
80     case Interrupted:
81       errorBelch("interrupted");
82       exit_status = EXIT_INTERRUPTED;
83       break;
84     case HeapExhausted:
85       exit_status = EXIT_HEAPOVERFLOW;
86       break;
87     case Success:
88       exit_status = EXIT_SUCCESS;
89       break;
90     default:
91       barf("main thread completed with invalid status");
92     }
93     shutdownHaskellAndExit(exit_status);
94 }
95
96 /* The rts entry point from a compiled program using a Haskell main function.
97  * This gets called from a tiny main function which gets linked into each
98  * compiled Haskell program that uses a Haskell main function.
99  *
100  * We expect the caller to pass __stginit_ZCMain for main_init and
101  * ZCMain_main_closure for main_closure. The reason we cannot refer to
102  * these symbols directly is because we're inside the rts and we do not know
103  * for sure that we'll be using a Haskell main function.
104  */
105 int hs_main(int argc, char *argv[], void (*main_init)(void), StgClosure *main_closure)
106 {
107     /* We do this dance with argc and argv as otherwise the SEH exception
108        stuff (the BEGIN/END CATCH below) on Windows gets confused */
109     progargc = argc;
110     progargv = argv;
111     progmain_init    = main_init;
112     progmain_closure = main_closure;
113
114 #if defined(mingw32_HOST_OS)
115     BEGIN_CATCH
116 #endif
117     real_main();
118 #if defined(mingw32_HOST_OS)
119     END_CATCH
120 #endif
121     return 0; /* not reached, but keeps gcc -Wall happy */
122 }
123 # endif /* BATCH_MODE */