cpp: Use #pragma once instead of #ifndef guards
[ghc.git] / rts / sm / GCTDecl.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team 1998-2014
4 *
5 * Documentation on the architecture of the Garbage Collector can be
6 * found in the online commentary:
7 *
8 * http://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
9 *
10 * ---------------------------------------------------------------------------*/
11
12 #pragma once
13
14 #include "BeginPrivate.h"
15
16 /* The gct variable is thread-local and points to the current thread's
17 gc_thread structure. It is heavily accessed, and thus high
18 performance access is crucial to parallel (-threaded) workloads.
19
20 First, we try to use a 'global register variable' which is a GCC
21 extension. This reserves the register globally.
22
23 If that's not possible, then we need to use __thread, which is a
24 compiler/OS specific TLS storage mechanism (assumed to be Fast
25 Enough.)
26
27 BUT, some older versions of OS X compilers (llvm-gcc, older Clangs)
28 do not support __thread at all. Modern clang however, does - but on
29 OS X it's not as fast as the Linux (which can write directly into a
30 segment register - see #7602.)
31
32 If we don't support __thread then we do the absolute worst thing:
33 we just use pthread_getspecific and pthread_setspecific (which are
34 horribly slow.)
35 */
36
37 #define GCT_REG_DECL(type,name,reg) register type name REG(reg);
38
39
40 /* -------------------------------------------------------------------------- */
41
42 /* First: if we're not using the threaded RTS, it's easy: just fake it. */
43 #if !defined(THREADED_RTS)
44 extern StgWord8 the_gc_thread[];
45 #define gct ((gc_thread*)&the_gc_thread)
46 #define SET_GCT(to) /*nothing*/
47 #define DECLARE_GCT /*nothing*/
48
49 #else /* defined(THREADED_RTS) */
50
51 /* -------------------------------------------------------------------------- */
52
53 /* Now, llvm-gcc and some older Clang compilers do not support
54 __thread. So we have to fallback to the extremely slow case,
55 unfortunately. Note: clang_CC_FLAVOR implies llvm_CC_FLAVOR.
56
57 Also, the iOS Clang compiler doesn't support __thread either for
58 some bizarre reason, so there's not much we can do about that... */
59 #if defined(llvm_CC_FLAVOR) && (CC_SUPPORTS_TLS == 0)
60 #define gct ((gc_thread *)(pthread_getspecific(gctKey)))
61 #define SET_GCT(to) (pthread_setspecific(gctKey, to))
62 #define DECLARE_GCT ThreadLocalKey gctKey;
63
64 /* -------------------------------------------------------------------------- */
65
66 /* However, if we *are* using an LLVM based compiler with __thread
67 support, then use that (since LLVM doesn't support global register
68 variables.) */
69 #elif defined(llvm_CC_FLAVOR) && (CC_SUPPORTS_TLS == 1)
70 extern __thread gc_thread* gct;
71 #define SET_GCT(to) gct = (to)
72 #define DECLARE_GCT __thread gc_thread* gct;
73
74 /* -------------------------------------------------------------------------- */
75
76 /* Next up: Using __thread is better than stealing a register on
77 x86/Linux, because we have too few registers available. In my
78 tests it was worth about 5% in GC performance, but of course that
79 might change as gcc improves. -- SDM 2009/04/03 */
80 #elif (defined(i386_HOST_ARCH) && (defined(linux_HOST_OS) \
81 || defined(solaris2_HOST_OS)))
82 extern __thread gc_thread* gct;
83 #define SET_GCT(to) gct = (to)
84 #define DECLARE_GCT __thread gc_thread* gct;
85
86 /* -------------------------------------------------------------------------- */
87
88 /* Next up: On SPARC we can't pin gct to a register. Names like %l1
89 are just offsets into the register window, which change on each
90 function call.
91
92 There are eight global (non-window) registers, but they're used for other
93 purposes:
94
95 %g0 -- always zero
96 %g1 -- volatile over function calls, used by the linker
97 %g2-%g3 -- used as scratch regs by the C compiler (caller saves)
98 %g4 -- volatile over function calls, used by the linker
99 %g5-%g7 -- reserved by the OS
100 */
101 #elif defined(sparc_HOST_ARCH)
102 extern __thread gc_thread* gct;
103 #define SET_GCT(to) gct = (to)
104 #define DECLARE_GCT __thread gc_thread* gct;
105
106 /* -------------------------------------------------------------------------- */
107
108 /* Next up: generally, if REG_Base is defined and we're *not* using
109 i386, then actually declare the needed register. The catch for i386
110 here is that REG_Base is %ebx, but that is also used for -fPIC, so
111 it can't be stolen */
112 #elif defined(REG_Base) && !defined(i386_HOST_ARCH)
113 GCT_REG_DECL(gc_thread*, gct, REG_Base);
114 #define SET_GCT(to) gct = (to)
115 #define DECLARE_GCT /* nothing */
116
117 /* -------------------------------------------------------------------------- */
118
119 /* Next up: if REG_R1 is available after checking REG_Base, we're
120 gonna steal it in every case we can. */
121 #elif defined(REG_R1)
122 GCT_REG_DECL(gc_thread*, gct, REG_R1);
123 #define SET_GCT(to) gct = (to)
124 #define DECLARE_GCT /* nothing */
125
126 /* -------------------------------------------------------------------------- */
127
128 /* Finally, as an absolute fallback, if none of the above tests check
129 out but we *do* have __thread support, then use that. */
130 #elif CC_SUPPORTS_TLS == 1
131 extern __thread gc_thread* gct;
132 #define SET_GCT(to) gct = (to)
133 #define DECLARE_GCT __thread gc_thread* gct;
134
135 /* -------------------------------------------------------------------------- */
136
137 /* Impossible! */
138 #else
139 #error Cannot find a way to declare the thread-local gc variable!
140 #endif
141
142 #endif // THREADED_RTS
143
144 #include "EndPrivate.h"