1 /* ----------------------------------------------------------------------------
3 * (c) The GHC Team, 2006-2008
7 * These are simple spin-only locks as opposed to Mutexes which
8 * probably spin for a while before blocking in the kernel. We use
9 * these when we are sure that all our threads are actively running on
10 * a CPU, eg. in the GC.
12 * TODO: measure whether we really need these, or whether Mutexes
13 * would do (and be a bit safer if a CPU becomes loaded).
15 * -------------------------------------------------------------------------- */
20 #if defined(THREADED_RTS)
22 #if defined(PROF_SPIN)
23 typedef struct SpinLock_
26 StgWord64 spin; // DEBUG version counts how much it spins
29 typedef StgWord SpinLock;
32 typedef lnat SpinLockCount;
35 #if defined(PROF_SPIN)
37 // PROF_SPIN enables counting the number of times we spin on a lock
40 INLINE_HEADER void ACQUIRE_SPIN_LOCK(SpinLock * p)
44 r = cas((StgVolatilePtr)&(p->lock), 1, 0);
52 INLINE_HEADER void RELEASE_SPIN_LOCK(SpinLock * p)
58 // initialise spin lock
59 INLINE_HEADER void initSpinLock(SpinLock * p)
69 INLINE_HEADER void ACQUIRE_SPIN_LOCK(SpinLock * p)
73 r = cas((StgVolatilePtr)p, 1, 0);
78 INLINE_HEADER void RELEASE_SPIN_LOCK(SpinLock * p)
85 INLINE_HEADER void initSpinLock(SpinLock * p)
91 #endif /* PROF_SPIN */
93 #else /* !THREADED_RTS */
95 // Using macros here means we don't have to ensure the argument is in scope
96 #define ACQUIRE_SPIN_LOCK(p) /* nothing */
97 #define RELEASE_SPIN_LOCK(p) /* nothing */
99 INLINE_HEADER void initSpinLock(void * p STG_UNUSED)
102 #endif /* THREADED_RTS */
104 #endif /* SPINLOCK_H */