Main.c 3.28 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 * $Id: Main.c,v 1.16 2000/01/14 13:38:54 simonmar 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
#include "RtsUtils.h"

#ifdef DEBUG
19
# include "Printer.h"   /* for printing        */
20 21 22
#endif

#ifdef INTERPRETER
23
# include "Assembler.h"
24 25 26
#endif

#ifdef PAR
27 28 29 30 31 32 33
# include "ParInit.h"
# include "Parallel.h"
# include "LLC.h"
#endif

#if defined(GRAN) || defined(PAR)
# include "GranSimRts.h"
34 35
#endif

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


41 42 43
/* Hack: we assume that we're building a batch-mode system unless 
 * INTERPRETER is set
 */
sof's avatar
sof committed
44
# ifndef INTERPRETER /* Hack */
45 46
int main(int argc, char *argv[])
{
47
    int exit_status;
48
    SchedulerStatus status;
49 50
    /* all GranSim/GUM init is done in startupHaskell; sets IAmMainThread! */

51 52
    startupHaskell(argc,argv);

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    /* kick off the computation by creating the main thread with a pointer
       to mainIO_closure representing the computation of the overall program;
       then enter the scheduler with this thread and off we go;
      
       the same for GranSim (we have only one instance of this code)

       in a parallel setup, where we have many instances of this code
       running on different PEs, we should do this only for the main PE
       (IAmMainThread is set in startupHaskell) 
    */

#  if defined(PAR)

#   if DEBUG
    { /* a wait loop to allow attachment of gdb to UNIX threads */
      nat i, j, s;

70
      for (i=0, s=0; i<(nat)RtsFlags.ParFlags.wait; i++)
71 72 73 74 75 76 77
	for (j=0; j<1000000; j++) 
	  s += j % 65536;
    }
    IF_PAR_DEBUG(verbose,
		 belch("Passed wait loop"));
#   endif

78 79
    if (IAmMainThread == rtsTrue) {
      fprintf(stderr, "Main Thread Started ...\n");
80 81

      /* ToDo: Dump event for the main thread */
82
      status = rts_evalIO((StgClosure *)&mainIO_closure, NULL);
83
    } else {
84 85 86 87 88 89 90
      /* Just to show we're alive */
      IF_PAR_DEBUG(verbose,
		   fprintf(stderr, "== [%x] Non-Main PE enters scheduler without work ...\n",
			   mytid));
     
      /* all non-main threads enter the scheduler without work */
      status = schedule( /* nothing */ );
91
    }
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

#  elif defined(GRAN)

    /* ToDo: Dump event for the main thread */
    status = rts_evalIO((StgClosure *)&mainIO_closure, NULL);

#  else /* !PAR && !GRAN */

    /* ToDo: want to start with a larger stack size */
    status = rts_evalIO((StgClosure *)&mainIO_closure, NULL);

#  endif /* !PAR && !GRAN */

    // ToDo: update for parallel execution
    /* check the status of the entire Haskell computation */
107 108
    switch (status) {
    case Deadlock:
109 110 111
      prog_belch("no threads to run:  infinite loop or deadlock?");
      exit_status = EXIT_DEADLOCK;
      break;
112
    case Killed:
113 114 115
      prog_belch("main thread killed");
      exit_status = EXIT_KILLED;
      break;
116
    case Interrupted:
117 118 119 120 121 122
      prog_belch("interrupted");
      exit_status = EXIT_INTERRUPTED;
      break;
    case Success:
      exit_status = EXIT_SUCCESS;
      break;
123 124
    default:
      barf("main thread completed with invalid status");
125
    }
126
    shutdownHaskellAndExit(exit_status);
127
}
sof's avatar
sof committed
128
# endif /* BATCH_MODE */