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)
37 __asm__ __volatile__ (
39 :"+r" (result), "+m" (*p)
40 : /* no input-only operands */
46 * CMPXCHG - the single-word atomic compare-and-exchange instruction. Used
47 * in the STM implementation.
50 cas(StgVolatilePtr p, StgWord o, StgWord n)
52 __asm__ __volatile__ (
54 :"=a"(o), "=m" (*(volatile unsigned int *)p)
60 * Write barrier - ensure that all preceding writes have happened
61 * before all following writes.
63 * We need to tell both the compiler AND the CPU about the barrier.
64 * This is a brute force solution; better results might be obtained by
65 * using volatile type declarations to get fine-grained ordering
66 * control in C, and optionally a memory barrier instruction on CPUs
67 * that require it (not x86 or x86_64).
71 #if i386_HOST_ARCH || x86_64_HOST_ARCH
72 __asm__ __volatile__ ("" : : : "memory");
74 #error memory barriers unimplemented on this architecture
79 * Locking/unlocking closures
81 * This is used primarily in the implementation of MVars.
83 #define SPIN_COUNT 4000
85 INLINE_HEADER StgInfoTable *
86 lockClosure(StgClosure *p)
88 #if i386_HOST_ARCH || x86_64_HOST_ARCH
93 info = xchg((P_)&p->header.info, (W_)&stg_WHITEHOLE_info);
94 if (info != (W_)&stg_WHITEHOLE_info) return (StgInfoTable *)info;
95 } while (++i < SPIN_COUNT);
104 unlockClosure(StgClosure *p, StgInfoTable *info)
106 #if i386_HOST_ARCH || x86_64_HOST_ARCH
107 // This is a strictly ordered write, so we need a wb():
109 p->header.info = info;
115 #else /* !THREADED_RTS */
117 #define wb() /* nothing */
119 INLINE_HEADER StgWord
120 xchg(StgPtr p, StgWord w)
127 #endif /* !THREADED_RTS */