Fix unregisterised builds, and building on non-x86/amd64/powerpc
[ghc-hetmet.git] / includes / SMP.h
1 /* ----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2005
4  *
5  * Macros for THREADED_RTS support
6  *
7  * -------------------------------------------------------------------------- */
8
9 #ifndef SMP_H
10 #define SMP_H
11
12 /* THREADED_RTS is currently not compatible with the following options:
13  *
14  *      PROFILING (but only 1 CPU supported)
15  *      TICKY_TICKY
16  *      Unregisterised builds are ok, but only 1 CPU supported.
17  */
18
19 #if defined(THREADED_RTS)
20
21 #if  defined(TICKY_TICKY)
22 #error Build options incompatible with THREADED_RTS.
23 #endif
24
25 /* 
26  * XCHG - the atomic exchange instruction.  Used for locking closures
27  * during updates (see lockClosure() below) and the MVar primops.
28  *
29  * NB: the xchg instruction is implicitly locked, so we do not need
30  * a lock prefix here. 
31  */
32 INLINE_HEADER StgWord
33 xchg(StgPtr p, StgWord w)
34 {
35     StgWord result;
36 #if i386_HOST_ARCH || x86_64_HOST_ARCH
37     result = w;
38     __asm__ __volatile__ (
39           "xchg %1,%0"
40           :"+r" (result), "+m" (*p)
41           : /* no input-only operands */
42         );
43 #elif powerpc_HOST_ARCH
44     __asm__ __volatile__ (
45         "1:     lwarx     %0, 0, %2\n"
46         "       stwcx.    %1, 0, %2\n"
47         "       bne-      1b"
48         :"=r" (result)
49         :"r" (w), "r" (p)
50     );
51 #elif !defined(WITHSMP)
52     result = *p;
53     *p = w;
54 #else
55 #error xchg() unimplemented on this architecture
56 #endif
57     return result;
58 }
59
60 /* 
61  * CMPXCHG - the single-word atomic compare-and-exchange instruction.  Used 
62  * in the STM implementation.
63  */
64 INLINE_HEADER StgWord
65 cas(StgVolatilePtr p, StgWord o, StgWord n)
66 {
67 #if i386_HOST_ARCH || x86_64_HOST_ARCH
68     __asm__ __volatile__ (
69           "lock/cmpxchg %3,%1"
70           :"=a"(o), "=m" (*(volatile unsigned int *)p) 
71           :"0" (o), "r" (n));
72     return o;
73 #elif powerpc_HOST_ARCH
74     StgWord result;
75     __asm__ __volatile__ (
76         "1:     lwarx     %0, 0, %3\n"
77         "       cmpw      %0, %1\n"
78         "       bne       2f\n"
79         "       stwcx.    %2, 0, %3\n"
80         "       bne-      1b\n"
81         "2:"
82         :"=&r" (result)
83         :"r" (o), "r" (n), "r" (p)
84         :"cc", "memory"
85     );
86     return result;
87 #elif !defined(WITHSMP)
88     StgWord result;
89     result = *p;
90     if (result == o) {
91         *p = n;
92     }
93     return result;
94 #else
95 #error cas() unimplemented on this architecture
96 #endif
97 }
98
99 /*
100  * Write barrier - ensure that all preceding writes have happened
101  * before all following writes.  
102  *
103  * We need to tell both the compiler AND the CPU about the barrier.
104  * This is a brute force solution; better results might be obtained by
105  * using volatile type declarations to get fine-grained ordering
106  * control in C, and optionally a memory barrier instruction on CPUs
107  * that require it (not x86 or x86_64).
108  */
109 INLINE_HEADER void
110 write_barrier(void) {
111 #if i386_HOST_ARCH || x86_64_HOST_ARCH
112     __asm__ __volatile__ ("" : : : "memory");
113 #elif powerpc_HOST_ARCH
114     __asm__ __volatile__ ("lwsync" : : : "memory");
115 #elif !defined(WITHSMP)
116     return;
117 #else
118 #error memory barriers unimplemented on this architecture
119 #endif
120 }
121
122 /*
123  * Locking/unlocking closures
124  *
125  * This is used primarily in the implementation of MVars.
126  */
127 #define SPIN_COUNT 4000
128
129 INLINE_HEADER StgInfoTable *
130 lockClosure(StgClosure *p)
131 {
132     StgWord info;
133     do {
134         nat i = 0;
135         do {
136             info = xchg((P_)(void *)&p->header.info, (W_)&stg_WHITEHOLE_info);
137             if (info != (W_)&stg_WHITEHOLE_info) return (StgInfoTable *)info;
138         } while (++i < SPIN_COUNT);
139         yieldThread();
140     } while (1);
141 }
142
143 INLINE_HEADER void
144 unlockClosure(StgClosure *p, StgInfoTable *info)
145 {
146     // This is a strictly ordered write, so we need a wb():
147     write_barrier();
148     p->header.info = info;
149 }
150
151 #else /* !THREADED_RTS */
152
153 #define write_barrier() /* nothing */
154
155 INLINE_HEADER StgWord
156 xchg(StgPtr p, StgWord w)
157 {
158     StgWord old = *p;
159     *p = w;
160     return old;
161 }
162
163 INLINE_HEADER StgInfoTable *
164 lockClosure(StgClosure *p)
165 { return (StgInfoTable *)p->header.info; }
166
167 INLINE_HEADER void
168 unlockClosure(StgClosure *p STG_UNUSED, StgInfoTable *info STG_UNUSED)
169 { /* nothing */ }
170
171 #endif /* !THREADED_RTS */
172
173 // Handy specialised versions of lockClosure()/unlockClosure()
174 INLINE_HEADER void lockTSO(StgTSO *tso)
175 { lockClosure((StgClosure *)tso); }
176
177 INLINE_HEADER void unlockTSO(StgTSO *tso)
178 { unlockClosure((StgClosure*)tso, (StgInfoTable*)&stg_TSO_info); }
179
180 #endif /* SMP_H */