RtsAPI.h 13.2 KB
Newer Older
1
/* ----------------------------------------------------------------------------
2
 *
3
 * (c) The GHC Team, 1998-2004
4 5 6
 *
 * API for invoking Haskell functions via the RTS
 *
7
 * To understand the structure of the RTS headers, see the wiki:
8
 *   http://ghc.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
9
 *
10 11 12 13 14
 * --------------------------------------------------------------------------*/

#ifndef RTSAPI_H
#define RTSAPI_H

15 16 17 18
#ifdef __cplusplus
extern "C" {
#endif

19 20
#include "HsFFI.h"

sof's avatar
sof committed
21 22 23 24
/*
 * Running the scheduler
 */
typedef enum {
25
    NoStatus,    /* not finished yet */
26 27
    Success,     /* completed successfully */
    Killed,      /* uncaught exception */
28 29
    Interrupted, /* stopped in response to a call to interruptStgRts */
    HeapExhausted /* out of memory */
sof's avatar
sof committed
30
} SchedulerStatus;
31

Simon Marlow's avatar
Simon Marlow committed
32
typedef struct StgClosure_ *HaskellObj;
33

34 35 36 37 38 39
/*
 * An abstract type representing the token returned by rts_lock() and
 * used when allocating objects and threads in the RTS.
 */
typedef struct Capability_ Capability;

40 41 42 43 44 45 46 47 48
/*
 * The public view of a Capability: we can be sure it starts with
 * these two components (but it may have more private fields).
 */
typedef struct CapabilityPublic_ {
    StgFunTable f;
    StgRegTable r;
} CapabilityPublic;

49 50 51 52 53 54 55 56 57 58 59 60 61 62
/* ----------------------------------------------------------------------------
   RTS configuration settings, for passing to hs_init_ghc()
   ------------------------------------------------------------------------- */

typedef enum {
    RtsOptsNone,         // +RTS causes an error
    RtsOptsSafeOnly,     // safe RTS options allowed; others cause an error
    RtsOptsAll           // all RTS options allowed
  } RtsOptsEnabledEnum;

// The RtsConfig struct is passed (by value) to hs_init_ghc().  The
// reason for using a struct is extensibility: we can add more
// fields to this later without breaking existing client code.
typedef struct {
63 64

    // Whether to interpret +RTS options on the command line
65
    RtsOptsEnabledEnum rts_opts_enabled;
66

67 68 69
    // Whether to give RTS flag suggestions
    HsBool rts_opts_suggestions;

70
    // additional RTS options
71
    const char *rts_opts;
72 73

    // True if GHC was not passed -no-hs-main
74
    HsBool rts_hs_main;
75

76 77 78
    // Whether to retain CAFs (default: false)
    HsBool keep_cafs;

79 80 81 82 83 84 85 86 87 88 89 90 91 92
    // Called before processing command-line flags, so that default
    // settings for RtsFlags can be provided.
    void (* defaultsHook) (void);

    // Called just before exiting
    void (* onExitHook) (void);

    // Called on a stack overflow, before exiting
    void (* stackOverflowHook) (W_ stack_size);

    // Called on heap overflow, before exiting
    void (* outOfHeapHook) (W_ request_size, W_ heap_size);

    // Called when malloc() fails, before exiting
93
    void (* mallocFailHook) (W_ request_size /* in bytes */, const char *msg);
94 95 96 97 98 99 100 101 102 103 104

    // Called for every GC
    void (* gcDoneHook) (unsigned int gen,
                         W_ allocated_bytes, /* since last GC */
                         W_ live_bytes,
                         W_ copied_bytes,
                         W_ max_copied_per_thread_bytes,
                         W_ total_bytes,
                         W_ slop_bytes,
                         W_ sync_elapsed_ns, W_ elapsed_ns, W_ cpu_ns);

105 106 107 108 109 110 111
} RtsConfig;

// Clients should start with defaultRtsConfig and then customise it.
// Bah, I really wanted this to be a const struct value, but it seems
// you can't do that in C (it generates code).
extern const RtsConfig defaultRtsConfig;

112 113 114
/* ----------------------------------------------------------------------------
   Starting up and shutting down the Haskell RTS.
   ------------------------------------------------------------------------- */
115 116 117

/* DEPRECATED, use hs_init() or hs_init_ghc() instead  */
extern void startupHaskell         ( int argc, char *argv[],
118
                                     void (*init_root)(void) );
119 120

/* DEPRECATED, use hs_exit() instead  */
121
extern void shutdownHaskell        ( void );
122

123 124 125 126
/* Like hs_init(), but allows rtsopts. For more complicated usage,
 * use hs_init_ghc. */
extern void hs_init_with_rtsopts (int *argc, char **argv[]);

127 128 129 130 131 132 133 134 135
/*
 * GHC-specific version of hs_init() that allows specifying whether
 * +RTS ... -RTS options are allowed or not (default: only "safe"
 * options are allowed), and allows passing an option string that is
 * to be interpreted by the RTS only, not passed to the program.
 */
extern void hs_init_ghc (int *argc, char **argv[],   // program arguments
                         RtsConfig rts_config);      // RTS configuration

136 137
extern void shutdownHaskellAndExit (int exitCode, int fastExit)
    GNUC3_ATTRIBUTE(__noreturn__);
138 139

#ifndef mingw32_HOST_OS
140 141
extern void shutdownHaskellAndSignal (int sig, int fastExit)
     GNUC3_ATTRIBUTE(__noreturn__);
142 143
#endif

144
extern void getProgArgv            ( int *argc, char **argv[] );
145
extern void setProgArgv            ( int argc, char *argv[] );
146 147
extern void getFullProgArgv        ( int *argc, char **argv[] );
extern void setFullProgArgv        ( int argc, char *argv[] );
148
extern void freeFullProgArgv       ( void ) ;
149

150 151
/* exit() override */
extern void (*exitFn)(int);
152 153 154

/* ----------------------------------------------------------------------------
   Locking.
155

156
   You have to surround all access to the RtsAPI with these calls.
157
   ------------------------------------------------------------------------- */
158

159 160 161
// acquires a token which may be used to create new objects and
// evaluate them.
Capability *rts_lock (void);
162

163 164
// releases the token acquired with rts_lock().
void rts_unlock (Capability *token);
165

166 167 168 169 170 171 172 173 174
// If you are in a context where you know you have a current capability but
// do not know what it is, then use this to get it. Basically this only
// applies to "unsafe" foreign calls (as unsafe foreign calls are made with
// the capability held).
//
// WARNING: There is *no* guarantee this returns anything sensible (eg NULL)
// when there is no current capability.
Capability *rts_unsafeGetMyCapability (void);

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
/* ----------------------------------------------------------------------------
   Which cpu should the OS thread and Haskell thread run on?

   1. Run the current thread on the given capability:
     rts_setInCallCapability(cap, 0);

   2. Run the current thread on the given capability and set the cpu affinity
      for this thread:
     rts_setInCallCapability(cap, 1);

   3. Run the current thread on the given numa node:
     rts_pinThreadToNumaNode(node);

   4. Run the current thread on the given capability and on the given numa node:
     rts_setInCallCapability(cap, 0);
     rts_pinThreadToNumaNode(cap);
   ------------------------------------------------------------------------- */

Simon Marlow's avatar
Simon Marlow committed
193 194 195 196 197 198 199
// Specify the Capability that the current OS thread should run on when it calls
// into Haskell.  The actual capability will be calculated as the supplied
// value modulo the number of enabled Capabilities.
//
// Note that the thread may still be migrated by the RTS scheduler, but that
// will only happen if there are multiple threads running on one Capability and
// another Capability is free.
Simon Marlow's avatar
Simon Marlow committed
200 201 202 203 204
//
// If affinity is non-zero, the current thread will be bound to
// specific CPUs according to the prevailing affinity policy for the
// specified capability, set by either +RTS -qa or +RTS --numa.
void rts_setInCallCapability (int preferred_capability, int affinity);
Simon Marlow's avatar
Simon Marlow committed
205

206 207 208 209 210 211
// Specify the CPU Node that the current OS thread should run on when it calls
// into Haskell. The argument can be either a node number or capability number.
// The actual node will be calculated as the supplied value modulo the number
// of numa nodes.
void rts_pinThreadToNumaNode (int node);

212 213 214
/* ----------------------------------------------------------------------------
   Building Haskell objects from C datatypes.
   ------------------------------------------------------------------------- */
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
HaskellObj   rts_mkChar       ( Capability *, HsChar   c );
HaskellObj   rts_mkInt        ( Capability *, HsInt    i );
HaskellObj   rts_mkInt8       ( Capability *, HsInt8   i );
HaskellObj   rts_mkInt16      ( Capability *, HsInt16  i );
HaskellObj   rts_mkInt32      ( Capability *, HsInt32  i );
HaskellObj   rts_mkInt64      ( Capability *, HsInt64  i );
HaskellObj   rts_mkWord       ( Capability *, HsWord   w );
HaskellObj   rts_mkWord8      ( Capability *, HsWord8  w );
HaskellObj   rts_mkWord16     ( Capability *, HsWord16 w );
HaskellObj   rts_mkWord32     ( Capability *, HsWord32 w );
HaskellObj   rts_mkWord64     ( Capability *, HsWord64 w );
HaskellObj   rts_mkPtr        ( Capability *, HsPtr    a );
HaskellObj   rts_mkFunPtr     ( Capability *, HsFunPtr a );
HaskellObj   rts_mkFloat      ( Capability *, HsFloat  f );
HaskellObj   rts_mkDouble     ( Capability *, HsDouble f );
HaskellObj   rts_mkStablePtr  ( Capability *, HsStablePtr s );
HaskellObj   rts_mkBool       ( Capability *, HsBool   b );
HaskellObj   rts_mkString     ( Capability *, char    *s );

HaskellObj   rts_apply        ( Capability *, HaskellObj, HaskellObj );
235 236 237 238

/* ----------------------------------------------------------------------------
   Deconstructing Haskell objects
   ------------------------------------------------------------------------- */
239 240
HsChar       rts_getChar      ( HaskellObj );
HsInt        rts_getInt       ( HaskellObj );
sof's avatar
sof committed
241 242
HsInt8       rts_getInt8      ( HaskellObj );
HsInt16      rts_getInt16     ( HaskellObj );
243
HsInt32      rts_getInt32     ( HaskellObj );
sof's avatar
sof committed
244
HsInt64      rts_getInt64     ( HaskellObj );
245
HsWord       rts_getWord      ( HaskellObj );
sof's avatar
sof committed
246 247
HsWord8      rts_getWord8     ( HaskellObj );
HsWord16     rts_getWord16    ( HaskellObj );
248
HsWord32     rts_getWord32    ( HaskellObj );
sof's avatar
sof committed
249
HsWord64     rts_getWord64    ( HaskellObj );
250
HsPtr        rts_getPtr       ( HaskellObj );
251
HsFunPtr     rts_getFunPtr    ( HaskellObj );
252 253 254 255
HsFloat      rts_getFloat     ( HaskellObj );
HsDouble     rts_getDouble    ( HaskellObj );
HsStablePtr  rts_getStablePtr ( HaskellObj );
HsBool       rts_getBool      ( HaskellObj );
256 257 258 259 260

/* ----------------------------------------------------------------------------
   Evaluating Haskell expressions

   The versions ending in '_' allow you to specify an initial stack size.
261 262
   Note that these calls may cause Garbage Collection, so all HaskellObj
   references are rendered invalid by these calls.
263 264 265 266 267 268 269

   All of these functions take a (Capability **) - there is a
   Capability pointer both input and output.  We use an inout
   parameter because this is less error-prone for the client than a
   return value - the client could easily forget to use the return
   value, whereas incorrectly using an inout parameter will usually
   result in a type error.
270 271
   ------------------------------------------------------------------------- */

272 273 274 275 276 277 278 279
void rts_eval (/* inout */ Capability **,
               /* in    */ HaskellObj p,
               /* out */   HaskellObj *ret);

void rts_eval_ (/* inout */ Capability **,
                /* in    */ HaskellObj p,
                /* in    */ unsigned int stack_size,
                /* out   */ HaskellObj *ret);
280

281 282 283
void rts_evalIO (/* inout */ Capability **,
                 /* in    */ HaskellObj p,
                 /* out */   HaskellObj *ret);
284

285 286 287 288
void rts_evalStableIOMain (/* inout */ Capability **,
                           /* in    */ HsStablePtr s,
                           /* out */   HsStablePtr *ret);

289 290 291
void rts_evalStableIO (/* inout */ Capability **,
                       /* in    */ HsStablePtr s,
                       /* out */   HsStablePtr *ret);
292

293 294 295
void rts_evalLazyIO (/* inout */ Capability **,
                     /* in    */ HaskellObj p,
                     /* out */   HaskellObj *ret);
296

297 298 299 300
void rts_evalLazyIO_ (/* inout */ Capability **,
                      /* in    */ HaskellObj p,
                      /* in    */ unsigned int stack_size,
                      /* out   */ HaskellObj *ret);
301

302
void rts_checkSchedStatus (char* site, Capability *);
303

304
SchedulerStatus rts_getSchedStatus (Capability *cap);
305

Simon Marlow's avatar
Simon Marlow committed
306 307 308 309 310 311 312 313 314 315 316 317 318
/*
 * The RTS allocates some thread-local data when you make a call into
 * Haskell using one of the rts_eval() functions.  This data is not
 * normally freed until hs_exit().  If you want to free it earlier
 * than this, perhaps because the thread is about to exit, then call
 * rts_done() from the thread.
 *
 * It is safe to make more rts_eval() calls after calling rts_done(),
 * but the next one will cause allocation of the thread-local memory
 * again.
 */
void rts_done (void);

319 320 321 322 323 324
/* --------------------------------------------------------------------------
   Wrapper closures

   These are used by foreign export and foreign import "wrapper" stubs.
   ----------------------------------------------------------------------- */

325 326
// When producing Windows DLLs the we need to know which symbols are in the
//      local package/DLL vs external ones.
327
//
328 329
//      Note that RtsAPI.h is also included by foreign export stubs in
//      the base package itself.
330
//
331
#if defined(COMPILING_WINDOWS_DLL) && !defined(COMPILING_BASE_PACKAGE)
332 333
__declspec(dllimport) extern StgWord base_GHCziTopHandler_runIO_closure[];
__declspec(dllimport) extern StgWord base_GHCziTopHandler_runNonIO_closure[];
334
#else
335 336
extern StgWord base_GHCziTopHandler_runIO_closure[];
extern StgWord base_GHCziTopHandler_runNonIO_closure[];
337
#endif
338

339 340
#define runIO_closure     base_GHCziTopHandler_runIO_closure
#define runNonIO_closure  base_GHCziTopHandler_runNonIO_closure
341 342

/* ------------------------------------------------------------------------ */
343 344 345 346 347

#ifdef __cplusplus
}
#endif

348
#endif /* RTSAPI_H */