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