1 /* ----------------------------------------------------------------------------
3 * (c) The GHC Team, 2005
5 * Macros for THREADED_RTS support
7 * -------------------------------------------------------------------------- */
9 #ifndef RTS_STORAGE_SMPCLOSUREOPS_H
10 #define RTS_STORAGE_SMPCLOSUREOPS_H
14 #define unlockClosure(ptr,info) \
15 prim %write_barrier() []; \
16 StgHeader_info(ptr) = info;
20 EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p);
21 EXTERN_INLINE StgInfoTable *tryLockClosure(StgClosure *p);
22 EXTERN_INLINE void unlockClosure(StgClosure *p, const StgInfoTable *info);
24 #if defined(THREADED_RTS)
26 /* -----------------------------------------------------------------------------
27 * Locking/unlocking closures
29 * This is used primarily in the implementation of MVars.
30 * -------------------------------------------------------------------------- */
32 // We want a callable copy of lockClosure() so that we can refer to it
33 // from .cmm files compiled using the native codegen.
34 EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p)
40 info = xchg((P_)(void *)&p->header.info, (W_)&stg_WHITEHOLE_info);
41 if (info != (W_)&stg_WHITEHOLE_info) return (StgInfoTable *)info;
42 } while (++i < SPIN_COUNT);
47 EXTERN_INLINE StgInfoTable *tryLockClosure(StgClosure *p)
50 info = xchg((P_)(void *)&p->header.info, (W_)&stg_WHITEHOLE_info);
51 if (info != (W_)&stg_WHITEHOLE_info) {
52 return (StgInfoTable *)info;
58 #else /* !THREADED_RTS */
60 EXTERN_INLINE StgInfoTable *
61 lockClosure(StgClosure *p)
62 { return (StgInfoTable *)p->header.info; }
64 EXTERN_INLINE StgInfoTable *
65 tryLockClosure(StgClosure *p)
66 { return (StgInfoTable *)p->header.info; }
68 #endif /* THREADED_RTS */
70 EXTERN_INLINE void unlockClosure(StgClosure *p, const StgInfoTable *info)
72 // This is a strictly ordered write, so we need a write_barrier():
74 p->header.info = info;
77 // Handy specialised versions of lockClosure()/unlockClosure()
78 EXTERN_INLINE void lockTSO(StgTSO *tso);
79 EXTERN_INLINE void lockTSO(StgTSO *tso)
80 { lockClosure((StgClosure *)tso); }
82 EXTERN_INLINE void unlockTSO(StgTSO *tso);
83 EXTERN_INLINE void unlockTSO(StgTSO *tso)
84 { unlockClosure((StgClosure*)tso, (const StgInfoTable *)&stg_TSO_info); }
86 #endif /* CMINUSMINUS */
88 #endif /* RTS_STORAGE_SMPCLOSUREOPS_H */