d16d4ba64ec62d8719afe838b3b09931c742c76d
[ghc-hetmet.git] / ghc / 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     result = w;
37     __asm__ __volatile__ (
38           "xchg %1,%0"
39           :"+r" (result), "+m" (*p)
40           : /* no input-only operands */
41         );
42     return result;
43 }
44
45 /* 
46  * CMPXCHG - the single-word atomic compare-and-exchange instruction.  Used 
47  * in the STM implementation.
48  */
49 INLINE_HEADER StgWord
50 cas(StgVolatilePtr p, StgWord o, StgWord n)
51 {
52     __asm__ __volatile__ (
53           "lock cmpxchg %3,%1"
54           :"=a"(o), "=m" (*(volatile unsigned int *)p) 
55           :"0" (o), "r" (n));
56     return o;
57 }
58
59 /*
60  * Write barrier - ensure that all preceding writes have happened
61  * before all following writes.  
62  *
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).
68  */
69 INLINE_HEADER void
70 wb(void) {
71 #if i386_HOST_ARCH || x86_64_HOST_ARCH
72     __asm__ __volatile__ ("" : : : "memory");
73 #else
74 #error memory barriers unimplemented on this architecture
75 #endif
76 }
77
78 /*
79  * Locking/unlocking closures
80  *
81  * This is used primarily in the implementation of MVars.
82  */
83 #define SPIN_COUNT 4000
84
85 INLINE_HEADER StgInfoTable *
86 lockClosure(StgClosure *p)
87 {
88 #if i386_HOST_ARCH || x86_64_HOST_ARCH
89     StgWord info;
90     do {
91         nat i = 0;
92         do {
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);
96         yieldThread();
97     } while (1);
98 #else
99    ACQUIRE_SM_LOCK
100 #endif
101 }
102
103 INLINE_HEADER void
104 unlockClosure(StgClosure *p, StgInfoTable *info)
105 {
106 #if i386_HOST_ARCH || x86_64_HOST_ARCH
107     // This is a strictly ordered write, so we need a wb():
108     wb();
109     p->header.info = info;
110 #else
111     RELEASE_SM_LOCK;
112 #endif
113 }
114
115 #else /* !THREADED_RTS */
116
117 #define wb() /* nothing */
118
119 INLINE_HEADER StgWord
120 xchg(StgPtr p, StgWord w)
121 {
122     StgWord old = *p;
123     *p = w;
124     return old;
125 }
126
127 #endif /* !THREADED_RTS */
128
129 #endif /* SMP_H */