Merge branch 'master' of win:c:/ghc/git/cygwin/.
[ghc.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 * To understand the structure of the RTS headers, see the wiki:
9 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
10 *
11 * ---------------------------------------------------------------------------*/
12
13 #ifndef RTS_H
14 #define RTS_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 /* We include windows.h very early, as on Win64 the CONTEXT type has
21 fields "R8", "R9" and "R10", which goes bad if we've already
22 #define'd those names for our own purposes (in stg/Regs.h) */
23 #if defined(HAVE_WINDOWS_H)
24 #include <windows.h>
25 #endif
26
27 #ifndef IN_STG_CODE
28 #define IN_STG_CODE 0
29 #endif
30 #include "Stg.h"
31
32 #include "HsFFI.h"
33 #include "RtsAPI.h"
34
35 // Turn off inlining when debugging - it obfuscates things
36 #ifdef DEBUG
37 # undef STATIC_INLINE
38 # define STATIC_INLINE static
39 #endif
40
41 #include "rts/Types.h"
42
43 #if __GNUC__ >= 3
44 /* Assume that a flexible array member at the end of a struct
45 * can be defined thus: T arr[]; */
46 #define FLEXIBLE_ARRAY
47 #else
48 /* Assume that it must be defined thus: T arr[0]; */
49 #define FLEXIBLE_ARRAY 0
50 #endif
51
52 #if __GNUC__ >= 3
53 #define ATTRIBUTE_ALIGNED(n) __attribute__((aligned(n)))
54 #else
55 #define ATTRIBUTE_ALIGNED(n) /*nothing*/
56 #endif
57
58 // Symbols that are extern, but private to the RTS, are declared
59 // with visibility "hidden" to hide them outside the RTS shared
60 // library.
61 #if defined(HAS_VISIBILITY_HIDDEN)
62 #define RTS_PRIVATE GNUC3_ATTRIBUTE(visibility("hidden"))
63 #else
64 #define RTS_PRIVATE /* disabled: RTS_PRIVATE */
65 #endif
66
67 #if __GNUC__ >= 4
68 #define RTS_UNLIKELY(p) __builtin_expect((p),0)
69 #else
70 #define RTS_UNLIKELY(p) p
71 #endif
72
73 /* Fix for mingw stat problem (done here so it's early enough) */
74 #ifdef mingw32_HOST_OS
75 #define __MSVCRT__ 1
76 #endif
77
78 /* Needed to get the macro version of errno on some OSs, and also to
79 get prototypes for the _r versions of C library functions. */
80 #ifndef _REENTRANT
81 #define _REENTRANT 1
82 #endif
83
84 /*
85 * We often want to know the size of something in units of an
86 * StgWord... (rounded up, of course!)
87 */
88 #define ROUNDUP_BYTES_TO_WDS(n) (((n) + sizeof(W_) - 1) / sizeof(W_))
89
90 #define sizeofW(t) ROUNDUP_BYTES_TO_WDS(sizeof(t))
91
92 /* -----------------------------------------------------------------------------
93 Assertions and Debuggery
94
95 CHECK(p) evaluates p and terminates with an error if p is false
96 ASSERT(p) like CHECK(p) if DEBUG is on, otherwise a no-op
97 -------------------------------------------------------------------------- */
98
99 void _assertFail(const char *filename, unsigned int linenum)
100 GNUC3_ATTRIBUTE(__noreturn__);
101
102 #define CHECK(predicate) \
103 if (predicate) \
104 /*null*/; \
105 else \
106 _assertFail(__FILE__, __LINE__)
107
108 #define CHECKM(predicate, msg, ...) \
109 if (predicate) \
110 /*null*/; \
111 else \
112 barf(msg, ##__VA_ARGS__)
113
114 #ifndef DEBUG
115 #define ASSERT(predicate) /* nothing */
116 #define ASSERTM(predicate,msg,...) /* nothing */
117 #else
118 #define ASSERT(predicate) CHECK(predicate)
119 #define ASSERTM(predicate,msg,...) CHECKM(predicate,msg,##__VA_ARGS__)
120 #endif /* DEBUG */
121
122 /*
123 * Use this on the RHS of macros which expand to nothing
124 * to make sure that the macro can be used in a context which
125 * demands a non-empty statement.
126 */
127
128 #define doNothing() do { } while (0)
129
130 #ifdef DEBUG
131 #define USED_IF_DEBUG
132 #define USED_IF_NOT_DEBUG STG_UNUSED
133 #else
134 #define USED_IF_DEBUG STG_UNUSED
135 #define USED_IF_NOT_DEBUG
136 #endif
137
138 #ifdef THREADED_RTS
139 #define USED_IF_THREADS
140 #define USED_IF_NOT_THREADS STG_UNUSED
141 #else
142 #define USED_IF_THREADS STG_UNUSED
143 #define USED_IF_NOT_THREADS
144 #endif
145
146 #define FMT_SizeT "zu"
147 #define FMT_HexSizeT "zx"
148
149 /* -----------------------------------------------------------------------------
150 Time values in the RTS
151 -------------------------------------------------------------------------- */
152
153 // For most time values in the RTS we use a fixed resolution of nanoseconds,
154 // normalising the time we get from platform-dependent APIs to this
155 // resolution.
156 #define TIME_RESOLUTION 1000000000
157 typedef StgInt64 Time;
158
159 #if TIME_RESOLUTION == 1000000000
160 // I'm being lazy, but it's awkward to define fully general versions of these
161 #define TimeToUS(t) ((t) / 1000)
162 #define TimeToNS(t) (t)
163 #define USToTime(t) ((Time)(t) * 1000)
164 #define NSToTime(t) ((Time)(t))
165 #else
166 #error Fix TimeToNS(), TimeToUS() etc.
167 #endif
168
169 #define SecondsToTime(t) ((Time)(t) * TIME_RESOLUTION)
170 #define TimeToSeconds(t) ((t) / TIME_RESOLUTION)
171
172 // Use instead of SecondsToTime() when we have a floating-point
173 // seconds value, to avoid truncating it.
174 INLINE_HEADER Time fsecondsToTime (double t)
175 {
176 return (Time)(t * TIME_RESOLUTION);
177 }
178
179 /* -----------------------------------------------------------------------------
180 Include everything STG-ish
181 -------------------------------------------------------------------------- */
182
183 /* System headers: stdlib.h is eeded so that we can use NULL. It must
184 * come after MachRegs.h, because stdlib.h might define some inline
185 * functions which may only be defined after register variables have
186 * been declared.
187 */
188 #include <stdlib.h>
189
190 #include "rts/Config.h"
191
192 /* Global constaints */
193 #include "rts/Constants.h"
194
195 /* Profiling information */
196 #include "rts/prof/CCS.h"
197 #include "rts/prof/LDV.h"
198
199 /* Parallel information */
200 #include "rts/OSThreads.h"
201 #include "rts/SpinLock.h"
202
203 #include "rts/Messages.h"
204
205 /* Storage format definitions */
206 #include "rts/storage/FunTypes.h"
207 #include "rts/storage/InfoTables.h"
208 #include "rts/storage/Closures.h"
209 #include "rts/storage/Liveness.h"
210 #include "rts/storage/ClosureTypes.h"
211 #include "rts/storage/TSO.h"
212 #include "stg/MiscClosures.h" /* InfoTables, closures etc. defined in the RTS */
213 #include "rts/storage/SMPClosureOps.h"
214 #include "rts/storage/Block.h"
215 #include "rts/storage/ClosureMacros.h"
216 #include "rts/storage/MBlock.h"
217 #include "rts/storage/GC.h"
218
219 /* Other RTS external APIs */
220 #include "rts/Parallel.h"
221 #include "rts/Hooks.h"
222 #include "rts/Signals.h"
223 #include "rts/BlockSignals.h"
224 #include "rts/Hpc.h"
225 #include "rts/Flags.h"
226 #include "rts/Adjustor.h"
227 #include "rts/FileLock.h"
228 #include "rts/Globals.h"
229 #include "rts/IOManager.h"
230 #include "rts/Linker.h"
231 #include "rts/Threads.h"
232 #include "rts/Ticky.h"
233 #include "rts/Timer.h"
234 #include "rts/Stable.h"
235 #include "rts/TTY.h"
236 #include "rts/Utils.h"
237 #include "rts/PrimFloat.h"
238 #include "rts/Main.h"
239
240 /* Misc stuff without a home */
241 DLL_IMPORT_RTS extern char **prog_argv; /* so we can get at these from Haskell */
242 DLL_IMPORT_RTS extern int prog_argc;
243 DLL_IMPORT_RTS extern char *prog_name;
244
245 #ifdef mingw32_HOST_OS
246 // We need these two from Haskell too
247 void getWin32ProgArgv(int *argc, wchar_t **argv[]);
248 void setWin32ProgArgv(int argc, wchar_t *argv[]);
249 #endif
250
251 void stackOverflow(void);
252
253 void stg_exit(int n) GNU_ATTRIBUTE(__noreturn__);
254
255 #ifndef mingw32_HOST_OS
256 int stg_sig_install (int, int, void *);
257 #endif
258
259 /* -----------------------------------------------------------------------------
260 RTS Exit codes
261 -------------------------------------------------------------------------- */
262
263 /* 255 is allegedly used by dynamic linkers to report linking failure */
264 #define EXIT_INTERNAL_ERROR 254
265 #define EXIT_DEADLOCK 253
266 #define EXIT_INTERRUPTED 252
267 #define EXIT_HEAPOVERFLOW 251
268 #define EXIT_KILLED 250
269
270 /* -----------------------------------------------------------------------------
271 Miscellaneous garbage
272 -------------------------------------------------------------------------- */
273
274 #ifdef DEBUG
275 #define TICK_VAR(arity) \
276 extern StgInt SLOW_CALLS_##arity; \
277 extern StgInt RIGHT_ARITY_##arity; \
278 extern StgInt TAGGED_PTR_##arity;
279
280 extern StgInt TOTAL_CALLS;
281
282 TICK_VAR(1)
283 TICK_VAR(2)
284 #endif
285
286 /* -----------------------------------------------------------------------------
287 Assertions and Debuggery
288 -------------------------------------------------------------------------- */
289
290 #define IF_RTSFLAGS(c,s) if (RtsFlags.c) { s; }
291
292 #ifdef DEBUG
293 #if IN_STG_CODE
294 #define IF_DEBUG(c,s) if (RtsFlags[0].DebugFlags.c) { s; }
295 #else
296 #define IF_DEBUG(c,s) if (RtsFlags.DebugFlags.c) { s; }
297 #endif
298 #else
299 #define IF_DEBUG(c,s) doNothing()
300 #endif
301
302 #ifdef DEBUG
303 #define DEBUG_ONLY(s) s
304 #else
305 #define DEBUG_ONLY(s) doNothing()
306 #endif
307
308 /* -----------------------------------------------------------------------------
309 Useful macros and inline functions
310 -------------------------------------------------------------------------- */
311
312 #if defined(__GNUC__)
313 #define SUPPORTS_TYPEOF
314 #endif
315
316 #if defined(SUPPORTS_TYPEOF)
317 #define stg_min(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _a : _b; })
318 #define stg_max(a,b) ({typeof(a) _a = (a), _b = (b); _a <= _b ? _b : _a; })
319 #else
320 #define stg_min(a,b) ((a) <= (b) ? (a) : (b))
321 #define stg_max(a,b) ((a) <= (b) ? (b) : (a))
322 #endif
323
324 /* -------------------------------------------------------------------------- */
325
326 #ifdef __cplusplus
327 }
328 #endif
329
330 #endif /* RTS_H */