FIX part of #2301, and #1619
[ghc.git] / includes / RtsAPI.h
1 /* ----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2004
4 *
5 * API for invoking Haskell functions via the RTS
6 *
7 * --------------------------------------------------------------------------*/
8
9 #ifndef RTSAPI_H
10 #define RTSAPI_H
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 #include "HsFFI.h"
17
18 /*
19 * Running the scheduler
20 */
21 typedef enum {
22 NoStatus, /* not finished yet */
23 Success, /* completed successfully */
24 Killed, /* uncaught exception */
25 Interrupted /* stopped in response to a call to interruptStgRts */
26 } SchedulerStatus;
27
28 typedef StgClosure *HaskellObj;
29
30 /*
31 * An abstract type representing the token returned by rts_lock() and
32 * used when allocating objects and threads in the RTS.
33 */
34 typedef struct Capability_ Capability;
35
36 /* ----------------------------------------------------------------------------
37 Starting up and shutting down the Haskell RTS.
38 ------------------------------------------------------------------------- */
39 extern void startupHaskell ( int argc, char *argv[],
40 void (*init_root)(void) );
41 extern void shutdownHaskell ( void );
42 extern void shutdownHaskellAndExit ( int exitCode );
43 extern void getProgArgv ( int *argc, char **argv[] );
44 extern void setProgArgv ( int argc, char *argv[] );
45 extern void getFullProgArgv ( int *argc, char **argv[] );
46 extern void setFullProgArgv ( int argc, char *argv[] );
47
48 #ifndef mingw32_HOST_OS
49 extern void shutdownHaskellAndSignal (int sig);
50 #endif
51
52 /* exit() override */
53 extern void (*exitFn)(int);
54
55 /* ----------------------------------------------------------------------------
56 Locking.
57
58 You have to surround all access to the RtsAPI with these calls.
59 ------------------------------------------------------------------------- */
60
61 // acquires a token which may be used to create new objects and
62 // evaluate them.
63 Capability *rts_lock (void);
64
65 // releases the token acquired with rts_lock().
66 void rts_unlock (Capability *token);
67
68 /* ----------------------------------------------------------------------------
69 Building Haskell objects from C datatypes.
70 ------------------------------------------------------------------------- */
71 HaskellObj rts_mkChar ( Capability *, HsChar c );
72 HaskellObj rts_mkInt ( Capability *, HsInt i );
73 HaskellObj rts_mkInt8 ( Capability *, HsInt8 i );
74 HaskellObj rts_mkInt16 ( Capability *, HsInt16 i );
75 HaskellObj rts_mkInt32 ( Capability *, HsInt32 i );
76 HaskellObj rts_mkInt64 ( Capability *, HsInt64 i );
77 HaskellObj rts_mkWord ( Capability *, HsWord w );
78 HaskellObj rts_mkWord8 ( Capability *, HsWord8 w );
79 HaskellObj rts_mkWord16 ( Capability *, HsWord16 w );
80 HaskellObj rts_mkWord32 ( Capability *, HsWord32 w );
81 HaskellObj rts_mkWord64 ( Capability *, HsWord64 w );
82 HaskellObj rts_mkPtr ( Capability *, HsPtr a );
83 HaskellObj rts_mkFunPtr ( Capability *, HsFunPtr a );
84 HaskellObj rts_mkFloat ( Capability *, HsFloat f );
85 HaskellObj rts_mkDouble ( Capability *, HsDouble f );
86 HaskellObj rts_mkStablePtr ( Capability *, HsStablePtr s );
87 HaskellObj rts_mkBool ( Capability *, HsBool b );
88 HaskellObj rts_mkString ( Capability *, char *s );
89
90 HaskellObj rts_apply ( Capability *, HaskellObj, HaskellObj );
91
92 /* ----------------------------------------------------------------------------
93 Deconstructing Haskell objects
94 ------------------------------------------------------------------------- */
95 HsChar rts_getChar ( HaskellObj );
96 HsInt rts_getInt ( HaskellObj );
97 HsInt8 rts_getInt8 ( HaskellObj );
98 HsInt16 rts_getInt16 ( HaskellObj );
99 HsInt32 rts_getInt32 ( HaskellObj );
100 HsInt64 rts_getInt64 ( HaskellObj );
101 HsWord rts_getWord ( HaskellObj );
102 HsWord8 rts_getWord8 ( HaskellObj );
103 HsWord16 rts_getWord16 ( HaskellObj );
104 HsWord32 rts_getWord32 ( HaskellObj );
105 HsWord64 rts_getWord64 ( HaskellObj );
106 HsPtr rts_getPtr ( HaskellObj );
107 HsFunPtr rts_getFunPtr ( HaskellObj );
108 HsFloat rts_getFloat ( HaskellObj );
109 HsDouble rts_getDouble ( HaskellObj );
110 HsStablePtr rts_getStablePtr ( HaskellObj );
111 HsBool rts_getBool ( HaskellObj );
112
113 /* ----------------------------------------------------------------------------
114 Evaluating Haskell expressions
115
116 The versions ending in '_' allow you to specify an initial stack size.
117 Note that these calls may cause Garbage Collection, so all HaskellObj
118 references are rendered invalid by these calls.
119 ------------------------------------------------------------------------- */
120 Capability *
121 rts_eval (Capability *, HaskellObj p, /*out*/HaskellObj *ret);
122
123 Capability *
124 rts_eval_ (Capability *, HaskellObj p, unsigned int stack_size,
125 /*out*/HaskellObj *ret);
126
127 Capability *
128 rts_evalIO (Capability *, HaskellObj p, /*out*/HaskellObj *ret);
129
130 Capability *
131 rts_evalStableIO (Capability *, HsStablePtr s, /*out*/HsStablePtr *ret);
132
133 Capability *
134 rts_evalLazyIO (Capability *, HaskellObj p, /*out*/HaskellObj *ret);
135
136 Capability *
137 rts_evalLazyIO_ (Capability *, HaskellObj p, unsigned int stack_size,
138 /*out*/HaskellObj *ret);
139
140 void
141 rts_checkSchedStatus (char* site, Capability *);
142
143 SchedulerStatus
144 rts_getSchedStatus (Capability *cap);
145
146 /* --------------------------------------------------------------------------
147 Wrapper closures
148
149 These are used by foreign export and foreign import "wrapper" stubs.
150 ----------------------------------------------------------------------- */
151
152 extern StgWord base_GHCziTopHandler_runIO_closure[];
153 extern StgWord base_GHCziTopHandler_runNonIO_closure[];
154 #define runIO_closure base_GHCziTopHandler_runIO_closure
155 #define runNonIO_closure base_GHCziTopHandler_runNonIO_closure
156
157 /* ------------------------------------------------------------------------ */
158
159 #ifdef __cplusplus
160 }
161 #endif
162
163 #endif /* RTSAPI_H */