1 /* ----------------------------------------------------------------------------
3 * (c) The GHC Team, 2006-2009
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 * Do not #include this file directly: #include "Rts.h" instead.
17 * To understand the structure of the RTS headers, see the wiki:
18 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
20 * -------------------------------------------------------------------------- */
22 #ifndef RTS_SPINLOCK_H
23 #define RTS_SPINLOCK_H
25 #if defined(THREADED_RTS)
27 #if defined(PROF_SPIN)
28 typedef struct SpinLock_
31 StgWord64 spin; // DEBUG version counts how much it spins
34 typedef StgWord SpinLock;
37 typedef lnat SpinLockCount;
39 #if defined(PROF_SPIN)
41 // PROF_SPIN enables counting the number of times we spin on a lock
44 INLINE_HEADER void ACQUIRE_SPIN_LOCK(SpinLock * p)
49 for (i = 0; i < SPIN_COUNT; i++) {
50 r = cas((StgVolatilePtr)&(p->lock), 1, 0);
60 INLINE_HEADER void RELEASE_SPIN_LOCK(SpinLock * p)
66 // initialise spin lock
67 INLINE_HEADER void initSpinLock(SpinLock * p)
77 INLINE_HEADER void ACQUIRE_SPIN_LOCK(SpinLock * p)
82 for (i = 0; i < SPIN_COUNT; i++) {
83 r = cas((StgVolatilePtr)p, 1, 0);
92 INLINE_HEADER void RELEASE_SPIN_LOCK(SpinLock * p)
99 INLINE_HEADER void initSpinLock(SpinLock * p)
105 #endif /* PROF_SPIN */
107 #else /* !THREADED_RTS */
109 // Using macros here means we don't have to ensure the argument is in scope
110 #define ACQUIRE_SPIN_LOCK(p) /* nothing */
111 #define RELEASE_SPIN_LOCK(p) /* nothing */
113 INLINE_HEADER void initSpinLock(void * p STG_UNUSED)
116 #endif /* THREADED_RTS */
118 #endif /* RTS_SPINLOCK_H */