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