Use message-passing to implement throwTo in the RTS
[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 StgInfoTable *tryLockClosure(StgClosure *p);
22 EXTERN_INLINE void unlockClosure(StgClosure *p, const StgInfoTable *info);
23
24 #if defined(THREADED_RTS)
25
26 /* -----------------------------------------------------------------------------
27  * Locking/unlocking closures
28  *
29  * This is used primarily in the implementation of MVars.
30  * -------------------------------------------------------------------------- */
31
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)
35 {
36     StgWord info;
37     do {
38         nat i = 0;
39         do {
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);
43         yieldThread();
44     } while (1);
45 }
46
47 EXTERN_INLINE StgInfoTable *tryLockClosure(StgClosure *p)
48 {
49     StgWord info;
50     info = xchg((P_)(void *)&p->header.info, (W_)&stg_WHITEHOLE_info);
51     if (info != (W_)&stg_WHITEHOLE_info) {
52         return (StgInfoTable *)info;
53     } else {
54         return NULL;
55     }
56 }
57
58 #else /* !THREADED_RTS */
59
60 EXTERN_INLINE StgInfoTable *
61 lockClosure(StgClosure *p)
62 { return (StgInfoTable *)p->header.info; }
63
64 EXTERN_INLINE StgInfoTable *
65 tryLockClosure(StgClosure *p)
66 { return (StgInfoTable *)p->header.info; }
67
68 #endif /* THREADED_RTS */
69
70 EXTERN_INLINE void unlockClosure(StgClosure *p, const StgInfoTable *info)
71 {
72     // This is a strictly ordered write, so we need a write_barrier():
73     write_barrier();
74     p->header.info = info;
75 }
76
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); }
81
82 EXTERN_INLINE void unlockTSO(StgTSO *tso);
83 EXTERN_INLINE void unlockTSO(StgTSO *tso)
84 { unlockClosure((StgClosure*)tso, (const StgInfoTable *)&stg_TSO_info); }
85
86 #endif /* CMINUSMINUS */
87
88 #endif /* RTS_STORAGE_SMPCLOSUREOPS_H */