Main.c 2.46 KB
Newer Older
1
/* -----------------------------------------------------------------------------
sof's avatar
sof committed
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 7 8 9
 *
 * Main function for a standalone Haskell program.
 *
 * ---------------------------------------------------------------------------*/

sof's avatar
sof committed
10 11
#define COMPILING_RTS_MAIN

12 13
#include "Rts.h"
#include "RtsAPI.h"
sof's avatar
sof committed
14
#include "SchedAPI.h"
15
#include "RtsFlags.h"
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "Schedule.h"  /* for MainTSO */
#include "RtsUtils.h"

#ifdef DEBUG
#include "Printer.h"   /* for printing        */
#endif

#ifdef INTERPRETER
#include "Assembler.h"
#endif

#ifdef PAR
#include "ParInit.h"
#include "Parallel.h"
#include "LLC.h"
#endif

sof's avatar
sof committed
33 34 35 36 37 38 39
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif


#ifndef ENABLE_WIN32_DLL_SUPPORT

40 41 42
/* Hack: we assume that we're building a batch-mode system unless 
 * INTERPRETER is set
 */
sof's avatar
sof committed
43
# ifndef INTERPRETER /* Hack */
44 45 46 47 48
int main(int argc, char *argv[])
{
    SchedulerStatus status;
    startupHaskell(argc,argv);

sof's avatar
sof committed
49
#  ifndef PAR
50 51 52
    MainTSO = createIOThread(stg_max(BLOCK_SIZE_W,
				     RtsFlags.GcFlags.initialStkSize),
			     (StgClosure *)&mainIO_closure);
53
    status = schedule(MainTSO,NULL);
sof's avatar
sof committed
54
#  else
55 56 57 58
    if (IAmMainThread == rtsTrue) {
    /*Just to show we're alive */
      fprintf(stderr, "Main Thread Started ...\n");
     
59 60 61
      MainTSO = createIOThread(stg_max(BLOCK_SIZE_W,
				       RtsFlags.GcFlags.initialStkSize),
			       (StgClosure *)&mainIO_closure);
62 63 64 65 66
      status = schedule(MainTSO,NULL);
    } else {
      WaitForPEOp(PP_FINISH,SysManTask);
      exit(EXIT_SUCCESS);
    }
sof's avatar
sof committed
67
#  endif /* PAR */
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    switch (status) {
    case AllBlocked:
      barf("Scheduler stopped, all threads blocked");
    case Deadlock:
      shutdownHaskell();
      barf("No threads to run!  Deadlock?");
    case Killed:
      belch("%s: warning: main thread killed", prog_argv[0]);
    case Success:
    case Interrupted:
      /* carry on */
    }
    shutdownHaskell();
    stg_exit(EXIT_SUCCESS);
}
sof's avatar
sof committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
# endif /* BATCH_MODE */

#else   /* !ENABLE_WIN32_DLL_SUPPORT */

static char* args[] = { "ghcRts" };

BOOL
WINAPI
DllMain ( HINSTANCE hInstance
        , DWORD reason
	, LPVOID reserved
	)
{
  /*
    ToDo: let the user configure RTS options to use
          via the registry.
   */
  switch (reason) {
  case DLL_PROCESS_ATTACH:
sof's avatar
sof committed
102
    startupHaskell(1,args);
sof's avatar
sof committed
103 104 105 106 107 108 109 110 111
    /* ToDo: gracefully handle startupHaskell() failures.. */
    return TRUE;
  case DLL_PROCESS_DETACH:
    shutdownHaskell();
  }
  return TRUE;
}

#endif /* !ENABLE_WIN32_DLL_SUPPORT */