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