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