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)
23 typedef struct StgSync_
26 StgWord64 spin; // DEBUG version counts how much it spins
29 typedef StgWord StgSync;
32 typedef lnat StgSyncCount;
37 // Debug versions of spin locks maintain a spin count
40 // To use the debug veriosn of the spin locks, a debug version of the program
41 // can be run under a deugger with a break point on stat_exit. At exit time
42 // of the program one can examine the state the spin count counts of various
43 // spin locks to check for contention.
46 INLINE_HEADER void ACQUIRE_SPIN_LOCK(StgSync * p)
51 r = cas((StgVolatilePtr)&(p->lock), 1, 0);
57 INLINE_HEADER void RELEASE_SPIN_LOCK(StgSync * p)
63 // initialise spin lock
64 INLINE_HEADER void initSpinLock(StgSync * p)
74 INLINE_HEADER void ACQUIRE_SPIN_LOCK(StgSync * p)
78 r = cas((StgVolatilePtr)p, 1, 0);
83 INLINE_HEADER void RELEASE_SPIN_LOCK(StgSync * p)
90 INLINE_HEADER void initSpinLock(StgSync * p)
98 #else /* !THREADED_RTS */
100 // Using macros here means we don't have to ensure the argument is in scope
101 #define ACQUIRE_SPIN_LOCK(p) /* nothing */
102 #define RELEASE_SPIN_LOCK(p) /* nothing */
104 INLINE_HEADER void initSpinLock(void * p STG_UNUSED)
107 #endif /* THREADED_RTS */
109 #endif /* SPINLOCK_H */