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 void unlockClosure(StgClosure *p, const StgInfoTable *info);
23 #if defined(THREADED_RTS)
25 /* -----------------------------------------------------------------------------
26 * Locking/unlocking closures
28 * This is used primarily in the implementation of MVars.
29 * -------------------------------------------------------------------------- */
31 #define SPIN_COUNT 4000
33 // We want a callable copy of lockClosure() so that we can refer to it
34 // from .cmm files compiled using the native codegen.
35 EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p)
41 info = xchg((P_)(void *)&p->header.info, (W_)&stg_WHITEHOLE_info);
42 if (info != (W_)&stg_WHITEHOLE_info) return (StgInfoTable *)info;
43 } while (++i < SPIN_COUNT);
48 EXTERN_INLINE void unlockClosure(StgClosure *p, const StgInfoTable *info)
50 // This is a strictly ordered write, so we need a write_barrier():
52 p->header.info = info;
55 #else /* !THREADED_RTS */
57 EXTERN_INLINE StgInfoTable *
58 lockClosure(StgClosure *p)
59 { return (StgInfoTable *)p->header.info; }
62 unlockClosure(StgClosure *p STG_UNUSED, const StgInfoTable *info STG_UNUSED)
65 #endif /* THREADED_RTS */
67 // Handy specialised versions of lockClosure()/unlockClosure()
68 EXTERN_INLINE void lockTSO(StgTSO *tso);
69 EXTERN_INLINE void lockTSO(StgTSO *tso)
70 { lockClosure((StgClosure *)tso); }
72 EXTERN_INLINE void unlockTSO(StgTSO *tso);
73 EXTERN_INLINE void unlockTSO(StgTSO *tso)
74 { unlockClosure((StgClosure*)tso, (const StgInfoTable *)&stg_TSO_info); }
76 #endif /* CMINUSMINUS */
78 #endif /* RTS_STORAGE_SMPCLOSUREOPS_H */