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