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