582ec0e959e6821741f6a4969681efb727aee67e
[ghc-hetmet.git] / includes / rts / storage / SMPClosureOps.h
1 /* ----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2005
4  *
5  * Macros for THREADED_RTS support
6  *
7  * -------------------------------------------------------------------------- */
8
9 #ifndef RTS_STORAGE_SMPCLOSUREOPS_H
10 #define RTS_STORAGE_SMPCLOSUREOPS_H
11
12 #ifdef CMINUSMINUS
13
14 #define unlockClosure(ptr,info)                 \
15     prim %write_barrier() [];                   \
16     StgHeader_info(ptr) = info;    
17
18 #else
19
20 EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p);
21 EXTERN_INLINE void unlockClosure(StgClosure *p, const StgInfoTable *info);
22
23 #if defined(THREADED_RTS)
24
25 /* -----------------------------------------------------------------------------
26  * Locking/unlocking closures
27  *
28  * This is used primarily in the implementation of MVars.
29  * -------------------------------------------------------------------------- */
30
31 // We want a callable copy of lockClosure() so that we can refer to it
32 // from .cmm files compiled using the native codegen.
33 EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p)
34 {
35     StgWord info;
36     do {
37         nat i = 0;
38         do {
39             info = xchg((P_)(void *)&p->header.info, (W_)&stg_WHITEHOLE_info);
40             if (info != (W_)&stg_WHITEHOLE_info) return (StgInfoTable *)info;
41         } while (++i < SPIN_COUNT);
42         yieldThread();
43     } while (1);
44 }
45
46 EXTERN_INLINE void unlockClosure(StgClosure *p, const StgInfoTable *info)
47 {
48     // This is a strictly ordered write, so we need a write_barrier():
49     write_barrier();
50     p->header.info = info;
51 }
52
53 #else /* !THREADED_RTS */
54
55 EXTERN_INLINE StgInfoTable *
56 lockClosure(StgClosure *p)
57 { return (StgInfoTable *)p->header.info; }
58
59 EXTERN_INLINE void
60 unlockClosure(StgClosure *p STG_UNUSED, const StgInfoTable *info STG_UNUSED)
61 { /* nothing */ }
62
63 #endif /* THREADED_RTS */
64
65 // Handy specialised versions of lockClosure()/unlockClosure()
66 EXTERN_INLINE void lockTSO(StgTSO *tso);
67 EXTERN_INLINE void lockTSO(StgTSO *tso)
68 { lockClosure((StgClosure *)tso); }
69
70 EXTERN_INLINE void unlockTSO(StgTSO *tso);
71 EXTERN_INLINE void unlockTSO(StgTSO *tso)
72 { unlockClosure((StgClosure*)tso, (const StgInfoTable *)&stg_TSO_info); }
73
74 #endif /* CMINUSMINUS */
75
76 #endif /* RTS_STORAGE_SMPCLOSUREOPS_H */