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