1 /* ----------------------------------------------------------------------------
3 * (c) The GHC Team, 2005
5 * Macros for THREADED_RTS support
7 * -------------------------------------------------------------------------- */
12 /* THREADED_RTS is currently not compatible with the following options:
14 * PROFILING (but only 1 CPU supported)
16 * Unregisterised builds are ok, but only 1 CPU supported.
19 #if defined(THREADED_RTS)
21 #if defined(TICKY_TICKY)
22 #error Build options incompatible with THREADED_RTS.
26 * XCHG - the atomic exchange instruction. Used for locking closures
27 * during updates (see lockClosure() below) and the MVar primops.
29 * NB: the xchg instruction is implicitly locked, so we do not need
33 xchg(StgPtr p, StgWord w)
36 #if i386_HOST_ARCH || x86_64_HOST_ARCH
38 __asm__ __volatile__ (
40 :"+r" (result), "+m" (*p)
41 : /* no input-only operands */
43 #elif powerpc_HOST_ARCH
44 __asm__ __volatile__ (
45 "1: lwarx %0, 0, %2\n"
52 #error xchg() unimplemented on this architecture
58 * CMPXCHG - the single-word atomic compare-and-exchange instruction. Used
59 * in the STM implementation.
62 cas(StgVolatilePtr p, StgWord o, StgWord n)
64 #if i386_HOST_ARCH || x86_64_HOST_ARCH
65 __asm__ __volatile__ (
67 :"=a"(o), "=m" (*(volatile unsigned int *)p)
70 #elif powerpc_HOST_ARCH
72 __asm__ __volatile__ (
73 "1: lwarx %0, 0, %3\n"
80 :"r" (o), "r" (n), "r" (p)
85 #error cas() unimplemented on this architecture
90 * Write barrier - ensure that all preceding writes have happened
91 * before all following writes.
93 * We need to tell both the compiler AND the CPU about the barrier.
94 * This is a brute force solution; better results might be obtained by
95 * using volatile type declarations to get fine-grained ordering
96 * control in C, and optionally a memory barrier instruction on CPUs
97 * that require it (not x86 or x86_64).
100 write_barrier(void) {
101 #if i386_HOST_ARCH || x86_64_HOST_ARCH
102 __asm__ __volatile__ ("" : : : "memory");
103 #elif powerpc_HOST_ARCH
104 __asm__ __volatile__ ("lwsync" : : : "memory");
106 #error memory barriers unimplemented on this architecture
111 * Locking/unlocking closures
113 * This is used primarily in the implementation of MVars.
115 #define SPIN_COUNT 4000
117 INLINE_HEADER StgInfoTable *
118 lockClosure(StgClosure *p)
120 #if i386_HOST_ARCH || x86_64_HOST_ARCH || powerpc_HOST_ARCH
125 info = xchg((P_)(void *)&p->header.info, (W_)&stg_WHITEHOLE_info);
126 if (info != (W_)&stg_WHITEHOLE_info) return (StgInfoTable *)info;
127 } while (++i < SPIN_COUNT);
136 unlockClosure(StgClosure *p, StgInfoTable *info)
138 #if i386_HOST_ARCH || x86_64_HOST_ARCH || powerpc_HOST_ARCH
139 // This is a strictly ordered write, so we need a wb():
141 p->header.info = info;
147 #else /* !THREADED_RTS */
149 #define write_barrier() /* nothing */
151 INLINE_HEADER StgWord
152 xchg(StgPtr p, StgWord w)
159 INLINE_HEADER StgInfoTable *
160 lockClosure(StgClosure *p)
161 { return (StgInfoTable *)p->header.info; }
164 unlockClosure(StgClosure *p STG_UNUSED, StgInfoTable *info STG_UNUSED)
167 #endif /* !THREADED_RTS */
169 // Handy specialised versions of lockClosure()/unlockClosure()
170 INLINE_HEADER void lockTSO(StgTSO *tso)
171 { lockClosure((StgClosure *)tso); }
173 INLINE_HEADER void unlockTSO(StgTSO *tso)
174 { unlockClosure((StgClosure*)tso, (StgInfoTable*)&stg_TSO_info); }