move termios prototypes into a public header
[ghc-hetmet.git] / includes / Rts.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2009
4  *
5  * RTS external APIs.  This file declares everything that the GHC RTS
6  * exposes externally.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifndef RTS_H
11 #define RTS_H
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 #ifndef IN_STG_CODE
18 #define IN_STG_CODE 0
19 #endif
20 #include "Stg.h"
21
22 #include "HsFFI.h"
23 #include "RtsAPI.h"
24
25 // Turn off inlining when debugging - it obfuscates things
26 #ifdef DEBUG
27 # undef  STATIC_INLINE
28 # define STATIC_INLINE static
29 #endif
30
31 #include "rts/Types.h"
32
33 #if __GNUC__ >= 3
34 /* Assume that a flexible array member at the end of a struct
35  * can be defined thus: T arr[]; */
36 #define FLEXIBLE_ARRAY
37 #else
38 /* Assume that it must be defined thus: T arr[0]; */
39 #define FLEXIBLE_ARRAY 0
40 #endif
41
42 #if __GNUC__ >= 3
43 #define ATTRIBUTE_ALIGNED(n) __attribute__((aligned(n)))
44 #else
45 #define ATTRIBUTE_ALIGNED(n) /*nothing*/
46 #endif
47
48 /* Fix for mingw stat problem (done here so it's early enough) */
49 #ifdef mingw32_HOST_OS
50 #define __MSVCRT__ 1
51 #endif
52
53 /* Needed to get the macro version of errno on some OSs, and also to
54    get prototypes for the _r versions of C library functions. */
55 #ifndef _REENTRANT
56 #define _REENTRANT 1
57 #endif
58
59 /*
60  * We often want to know the size of something in units of an
61  * StgWord... (rounded up, of course!)
62  */
63 #define ROUNDUP_BYTES_TO_WDS(n) (((n) + sizeof(W_) - 1) / sizeof(W_))
64
65 #define sizeofW(t) ROUNDUP_BYTES_TO_WDS(sizeof(t))
66
67 /* -----------------------------------------------------------------------------
68    Assertions and Debuggery
69
70    CHECK(p)   evaluates p and terminates with an error if p is false
71    ASSERT(p)  like CHECK(p) if DEBUG is on, otherwise a no-op
72    -------------------------------------------------------------------------- */
73
74 void _assertFail(const char *filename, unsigned int linenum)
75    GNUC3_ATTRIBUTE(__noreturn__);
76
77 #define CHECK(predicate)                        \
78         if (predicate)                          \
79             /*null*/;                           \
80         else                                    \
81             _assertFail(__FILE__, __LINE__)
82
83 #ifndef DEBUG
84 #define ASSERT(predicate) /* nothing */
85 #else
86 #define ASSERT(predicate) CHECK(predicate)
87 #endif /* DEBUG */
88
89 /* 
90  * Use this on the RHS of macros which expand to nothing
91  * to make sure that the macro can be used in a context which
92  * demands a non-empty statement.
93  */
94
95 #define doNothing() do { } while (0)
96
97 #ifdef DEBUG
98 #define USED_IF_DEBUG
99 #define USED_IF_NOT_DEBUG STG_UNUSED
100 #else
101 #define USED_IF_DEBUG STG_UNUSED
102 #define USED_IF_NOT_DEBUG
103 #endif
104
105 #ifdef THREADED_RTS
106 #define USED_IF_THREADS
107 #define USED_IF_NOT_THREADS STG_UNUSED
108 #else
109 #define USED_IF_THREADS STG_UNUSED
110 #define USED_IF_NOT_THREADS
111 #endif
112
113 /*
114  * Getting printf formats right for platform-dependent typedefs
115  */
116 #if SIZEOF_LONG == 8
117 #define FMT_Word64 "lu"
118 #define FMT_Int64  "ld"
119 #else
120 #define FMT_Word64 "llu"
121 #define FMT_Int64  "lld"
122 #endif
123
124 /* -----------------------------------------------------------------------------
125    Include everything STG-ish
126    -------------------------------------------------------------------------- */
127
128 /* System headers: stdlib.h is eeded so that we can use NULL.  It must
129  * come after MachRegs.h, because stdlib.h might define some inline
130  * functions which may only be defined after register variables have
131  * been declared.
132  */
133 #include <stdlib.h>
134
135 #include "rts/Config.h"
136
137 /* Global constaints */
138 #include "rts/Constants.h"
139
140 /* Profiling information */
141 #include "rts/prof/CCS.h"
142 #include "rts/prof/LDV.h"
143
144 /* Parallel information */
145 #include "rts/OSThreads.h"
146 #include "rts/SpinLock.h"
147
148 #include "rts/Messages.h"
149
150 /* Storage format definitions */
151 #include "rts/storage/FunTypes.h"
152 #include "rts/storage/InfoTables.h"
153 #include "rts/storage/Closures.h"
154 #include "rts/storage/Liveness.h"
155 #include "rts/storage/ClosureTypes.h"
156 #include "rts/storage/TSO.h"
157 #include "stg/MiscClosures.h" /* InfoTables, closures etc. defined in the RTS */
158 #include "rts/storage/SMPClosureOps.h"
159 #include "rts/storage/Block.h"
160 #include "rts/storage/ClosureMacros.h"
161 #include "rts/storage/MBlock.h"
162 #include "rts/storage/GC.h"
163
164 /* Other RTS external APIs */
165 #include "rts/Parallel.h"
166 #include "rts/Hooks.h"
167 #include "rts/Signals.h"
168 #include "rts/Hpc.h"
169 #include "rts/Flags.h"
170 #include "rts/Adjustor.h"
171 #include "rts/FileLock.h"
172 #include "rts/Globals.h"
173 #include "rts/IOManager.h"
174 #include "rts/Linker.h"
175 #include "rts/Threads.h"
176 #include "rts/Ticky.h"
177 #include "rts/Timer.h"
178 #include "rts/Stable.h"
179 #include "rts/TTY.h"
180
181 /* Misc stuff without a home */
182 DLL_IMPORT_RTS extern char **prog_argv; /* so we can get at these from Haskell */
183 DLL_IMPORT_RTS extern int    prog_argc;
184 DLL_IMPORT_RTS extern char  *prog_name;
185
186 void stackOverflow(void);
187
188 void stg_exit(int n) GNU_ATTRIBUTE(__noreturn__);
189
190 #ifndef mingw32_HOST_OS
191 int stg_sig_install (int, int, void *);
192 #endif
193
194 /* -----------------------------------------------------------------------------
195    RTS Exit codes
196    -------------------------------------------------------------------------- */
197
198 /* 255 is allegedly used by dynamic linkers to report linking failure */
199 #define EXIT_INTERNAL_ERROR 254
200 #define EXIT_DEADLOCK       253
201 #define EXIT_INTERRUPTED    252
202 #define EXIT_HEAPOVERFLOW   251
203 #define EXIT_KILLED         250
204
205 /* -----------------------------------------------------------------------------
206    Miscellaneous garbage
207    -------------------------------------------------------------------------- */
208
209 /* declarations for runtime flags/values */
210 #define MAX_RTS_ARGS 32
211
212 #ifdef DEBUG
213 #define TICK_VAR(arity) \
214   extern StgInt SLOW_CALLS_##arity; \
215   extern StgInt RIGHT_ARITY_##arity; \
216   extern StgInt TAGGED_PTR_##arity;
217
218 extern StgInt TOTAL_CALLS;
219
220 TICK_VAR(1)
221 TICK_VAR(2)
222 #endif
223
224 /* -----------------------------------------------------------------------------
225    Assertions and Debuggery
226    -------------------------------------------------------------------------- */
227
228 #define IF_RTSFLAGS(c,s)  if (RtsFlags.c) { s; }
229
230 #ifdef DEBUG
231 #if IN_STG_CODE
232 #define IF_DEBUG(c,s)  if (RtsFlags[0].DebugFlags.c) { s; }
233 #else
234 #define IF_DEBUG(c,s)  if (RtsFlags.DebugFlags.c) { s; }
235 #endif
236 #else
237 #define IF_DEBUG(c,s)  doNothing()
238 #endif
239
240 #ifdef DEBUG
241 #define DEBUG_ONLY(s) s
242 #else
243 #define DEBUG_ONLY(s) doNothing()
244 #endif
245
246 /* -----------------------------------------------------------------------------
247    Useful macros and inline functions
248    -------------------------------------------------------------------------- */
249
250 #if defined(__GNUC__)
251 #define SUPPORTS_TYPEOF
252 #endif
253
254 #if defined(SUPPORTS_TYPEOF)
255 #define stg_min(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _a : _b; })
256 #define stg_max(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _b : _a; })
257 #else
258 #define stg_min(a,b) ((a) <= (b) ? (a) : (b))
259 #define stg_max(a,b) ((a) <= (b) ? (b) : (a))
260 #endif
261
262 /* -------------------------------------------------------------------------- */
263
264 #ifdef __cplusplus
265 }
266 #endif
267
268 #endif /* RTS_H */