testsuite: don't collect compiler stats in collect_runtime_residency
[ghc.git] / includes / rts / SpinLock.h
1 /* ----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2006-2009
4 *
5 * Spin locks
6 *
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.
11 *
12 * TODO: measure whether we really need these, or whether Mutexes
13 * would do (and be a bit safer if a CPU becomes loaded).
14 *
15 * Do not #include this file directly: #include "Rts.h" instead.
16 *
17 * To understand the structure of the RTS headers, see the wiki:
18 * https://gitlab.haskell.org/ghc/ghc/wikis/commentary/source-tree/includes
19 *
20 * -------------------------------------------------------------------------- */
21
22 #pragma once
23
24 #if defined(THREADED_RTS)
25
26 #if defined(PROF_SPIN)
27 typedef struct SpinLock_
28 {
29 StgWord lock;
30 StgWord64 spin; // incremented every time we spin in ACQUIRE_SPIN_LOCK
31 StgWord64 yield; // incremented every time we yield in ACQUIRE_SPIN_LOCK
32 } SpinLock;
33 #else
34 typedef StgWord SpinLock;
35 #endif
36
37 #if defined(PROF_SPIN)
38
39 // PROF_SPIN enables counting the number of times we spin on a lock
40
41 // acquire spin lock
42 INLINE_HEADER void ACQUIRE_SPIN_LOCK(SpinLock * p)
43 {
44 StgWord32 r = 0;
45 uint32_t i;
46 do {
47 for (i = 0; i < SPIN_COUNT; i++) {
48 r = cas((StgVolatilePtr)&(p->lock), 1, 0);
49 if (r != 0) return;
50 p->spin++;
51 busy_wait_nop();
52 }
53 p->yield++;
54 yieldThread();
55 } while (1);
56 }
57
58 // release spin lock
59 INLINE_HEADER void RELEASE_SPIN_LOCK(SpinLock * p)
60 {
61 write_barrier();
62 p->lock = 1;
63 }
64
65 // initialise spin lock
66 INLINE_HEADER void initSpinLock(SpinLock * p)
67 {
68 write_barrier();
69 p->lock = 1;
70 p->spin = 0;
71 p->yield = 0;
72 }
73
74 #else
75
76 // acquire spin lock
77 INLINE_HEADER void ACQUIRE_SPIN_LOCK(SpinLock * p)
78 {
79 StgWord32 r = 0;
80 uint32_t i;
81 do {
82 for (i = 0; i < SPIN_COUNT; i++) {
83 r = cas((StgVolatilePtr)p, 1, 0);
84 if (r != 0) return;
85 busy_wait_nop();
86 }
87 yieldThread();
88 } while (1);
89 }
90
91 // release spin lock
92 INLINE_HEADER void RELEASE_SPIN_LOCK(SpinLock * p)
93 {
94 write_barrier();
95 (*p) = 1;
96 }
97
98 // init spin lock
99 INLINE_HEADER void initSpinLock(SpinLock * p)
100 {
101 write_barrier();
102 (*p) = 1;
103 }
104
105 #endif /* PROF_SPIN */
106
107 #else /* !THREADED_RTS */
108
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 */
112
113 INLINE_HEADER void initSpinLock(void * p STG_UNUSED)
114 { /* nothing */ }
115
116 #endif /* THREADED_RTS */