[project @ 1999-07-14 13:38:27 by simonmar]
[ghc-hetmet.git] / ghc / rts / Main.c
1 /* -----------------------------------------------------------------------------
2  * $Id: Main.c,v 1.10 1999/07/14 13:38:27 simonmar 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     shutdownHaskellAndExit(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 */