[project @ 1999-07-14 11:16:43 by simonmar]
[ghc-hetmet.git] / ghc / rts / Main.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Main.c,v 1.9 1999/07/06 15:05:49 sof Exp $
3  *
4  * (c) The GHC Team 1998-1999
5  *
6  * Main function for a standalone Haskell program.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #define COMPILING_RTS_MAIN
11
12 #include "Rts.h"
13 #include "RtsAPI.h"
14 #include "SchedAPI.h"
15 #include "RtsFlags.h"
16 #include "Schedule.h"  /* for MainTSO */
17 #include "RtsUtils.h"
18
19 #ifdef DEBUG
20 #include "Printer.h"   /* for printing        */
21 #endif
22
23 #ifdef INTERPRETER
24 #include "Assembler.h"
25 #endif
26
27 #ifdef PAR
28 #include "ParInit.h"
29 #include "Parallel.h"
30 #include "LLC.h"
31 #endif
32
33 #ifdef HAVE_WINDOWS_H
34 #include <windows.h>
35 #endif
36
37
38 #ifndef ENABLE_WIN32_DLL_SUPPORT
39
40 /* Hack: we assume that we're building a batch-mode system unless 
41  * INTERPRETER is set
42  */
43 # ifndef INTERPRETER /* Hack */
44 int main(int argc, char *argv[])
45 {
46     SchedulerStatus status;
47     startupHaskell(argc,argv);
48
49 #  ifndef PAR
50     MainTSO = createIOThread(stg_max(BLOCK_SIZE_W,
51                                      RtsFlags.GcFlags.initialStkSize),
52                              (StgClosure *)&mainIO_closure);
53     status = schedule(MainTSO,NULL);
54 #  else
55     if (IAmMainThread == rtsTrue) {
56     /*Just to show we're alive */
57       fprintf(stderr, "Main Thread Started ...\n");
58      
59       MainTSO = createIOThread(stg_max(BLOCK_SIZE_W,
60                                        RtsFlags.GcFlags.initialStkSize),
61                                (StgClosure *)&mainIO_closure);
62       status = schedule(MainTSO,NULL);
63     } else {
64       WaitForPEOp(PP_FINISH,SysManTask);
65       exit(EXIT_SUCCESS);
66     }
67 #  endif /* PAR */
68     switch (status) {
69     case AllBlocked:
70       barf("Scheduler stopped, all threads blocked");
71     case Deadlock:
72       shutdownHaskell();
73       barf("No threads to run!  Deadlock?");
74     case Killed:
75       belch("%s: warning: main thread killed", prog_argv[0]);
76     case Success:
77     case Interrupted:
78       /* carry on */
79     }
80     shutdownHaskell();
81     stg_exit(EXIT_SUCCESS);
82 }
83 # endif /* BATCH_MODE */
84
85 #else   /* !ENABLE_WIN32_DLL_SUPPORT */
86
87 static char* args[] = { "ghcRts" };
88
89 BOOL
90 WINAPI
91 DllMain ( HINSTANCE hInstance
92         , DWORD reason
93         , LPVOID reserved
94         )
95 {
96   /*
97     ToDo: let the user configure RTS options to use
98           via the registry.
99    */
100   switch (reason) {
101   case DLL_PROCESS_ATTACH:
102     startupHaskell(1,args);
103     /* ToDo: gracefully handle startupHaskell() failures.. */
104     return TRUE;
105   case DLL_PROCESS_DETACH:
106     shutdownHaskell();
107   }
108   return TRUE;
109 }
110
111 #endif /* !ENABLE_WIN32_DLL_SUPPORT */