libgo: export NetBSD-specific types in mksysinfo.sh
[gcc.git] / libgo / go / runtime / lock_futex.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build dragonfly freebsd linux
6
7 package runtime
8
9 import (
10 "runtime/internal/atomic"
11 "unsafe"
12 )
13
14 // For gccgo, while we still have C runtime code, use go:linkname to
15 // export some functions.
16 //
17 //go:linkname lock
18 //go:linkname unlock
19 //go:linkname noteclear
20 //go:linkname notewakeup
21 //go:linkname notesleep
22 //go:linkname notetsleep
23 //go:linkname notetsleepg
24
25 // This implementation depends on OS-specific implementations of
26 //
27 // futexsleep(addr *uint32, val uint32, ns int64)
28 // Atomically,
29 // if *addr == val { sleep }
30 // Might be woken up spuriously; that's allowed.
31 // Don't sleep longer than ns; ns < 0 means forever.
32 //
33 // futexwakeup(addr *uint32, cnt uint32)
34 // If any procs are sleeping on addr, wake up at most cnt.
35
36 const (
37 mutex_unlocked = 0
38 mutex_locked = 1
39 mutex_sleeping = 2
40
41 active_spin = 4
42 active_spin_cnt = 30
43 passive_spin = 1
44 )
45
46 // Possible lock states are mutex_unlocked, mutex_locked and mutex_sleeping.
47 // mutex_sleeping means that there is presumably at least one sleeping thread.
48 // Note that there can be spinning threads during all states - they do not
49 // affect mutex's state.
50
51 // We use the uintptr mutex.key and note.key as a uint32.
52 //go:nosplit
53 func key32(p *uintptr) *uint32 {
54 return (*uint32)(unsafe.Pointer(p))
55 }
56
57 func lock(l *mutex) {
58 lockWithRank(l, getLockRank(l))
59 }
60
61 func lock2(l *mutex) {
62 gp := getg()
63
64 if gp.m.locks < 0 {
65 throw("runtime·lock: lock count")
66 }
67 gp.m.locks++
68
69 // Speculative grab for lock.
70 v := atomic.Xchg(key32(&l.key), mutex_locked)
71 if v == mutex_unlocked {
72 return
73 }
74
75 // wait is either MUTEX_LOCKED or MUTEX_SLEEPING
76 // depending on whether there is a thread sleeping
77 // on this mutex. If we ever change l->key from
78 // MUTEX_SLEEPING to some other value, we must be
79 // careful to change it back to MUTEX_SLEEPING before
80 // returning, to ensure that the sleeping thread gets
81 // its wakeup call.
82 wait := v
83
84 // On uniprocessors, no point spinning.
85 // On multiprocessors, spin for ACTIVE_SPIN attempts.
86 spin := 0
87 if ncpu > 1 {
88 spin = active_spin
89 }
90 for {
91 // Try for lock, spinning.
92 for i := 0; i < spin; i++ {
93 for l.key == mutex_unlocked {
94 if atomic.Cas(key32(&l.key), mutex_unlocked, wait) {
95 return
96 }
97 }
98 procyield(active_spin_cnt)
99 }
100
101 // Try for lock, rescheduling.
102 for i := 0; i < passive_spin; i++ {
103 for l.key == mutex_unlocked {
104 if atomic.Cas(key32(&l.key), mutex_unlocked, wait) {
105 return
106 }
107 }
108 osyield()
109 }
110
111 // Sleep.
112 v = atomic.Xchg(key32(&l.key), mutex_sleeping)
113 if v == mutex_unlocked {
114 return
115 }
116 wait = mutex_sleeping
117 futexsleep(key32(&l.key), mutex_sleeping, -1)
118 }
119 }
120
121 func unlock(l *mutex) {
122 unlockWithRank(l)
123 }
124
125 func unlock2(l *mutex) {
126 v := atomic.Xchg(key32(&l.key), mutex_unlocked)
127 if v == mutex_unlocked {
128 throw("unlock of unlocked lock")
129 }
130 if v == mutex_sleeping {
131 futexwakeup(key32(&l.key), 1)
132 }
133
134 gp := getg()
135 gp.m.locks--
136 if gp.m.locks < 0 {
137 throw("runtime·unlock: lock count")
138 }
139 // if gp.m.locks == 0 && gp.preempt { // restore the preemption request in case we've cleared it in newstack
140 // gp.stackguard0 = stackPreempt
141 // }
142 }
143
144 // One-time notifications.
145 func noteclear(n *note) {
146 n.key = 0
147 }
148
149 func notewakeup(n *note) {
150 old := atomic.Xchg(key32(&n.key), 1)
151 if old != 0 {
152 print("notewakeup - double wakeup (", old, ")\n")
153 throw("notewakeup - double wakeup")
154 }
155 futexwakeup(key32(&n.key), 1)
156 }
157
158 func notesleep(n *note) {
159 gp := getg()
160 if gp != gp.m.g0 {
161 throw("notesleep not on g0")
162 }
163 ns := int64(-1)
164 if *cgo_yield != nil {
165 // Sleep for an arbitrary-but-moderate interval to poll libc interceptors.
166 ns = 10e6
167 }
168 for atomic.Load(key32(&n.key)) == 0 {
169 gp.m.blocked = true
170 futexsleep(key32(&n.key), 0, ns)
171 if *cgo_yield != nil {
172 asmcgocall(*cgo_yield, nil)
173 }
174 gp.m.blocked = false
175 }
176 }
177
178 // May run with m.p==nil if called from notetsleep, so write barriers
179 // are not allowed.
180 //
181 //go:nosplit
182 //go:nowritebarrier
183 func notetsleep_internal(n *note, ns int64) bool {
184 gp := getg()
185
186 if ns < 0 {
187 if *cgo_yield != nil {
188 // Sleep for an arbitrary-but-moderate interval to poll libc interceptors.
189 ns = 10e6
190 }
191 for atomic.Load(key32(&n.key)) == 0 {
192 gp.m.blocked = true
193 futexsleep(key32(&n.key), 0, ns)
194 if *cgo_yield != nil {
195 asmcgocall(*cgo_yield, nil)
196 }
197 gp.m.blocked = false
198 }
199 return true
200 }
201
202 if atomic.Load(key32(&n.key)) != 0 {
203 return true
204 }
205
206 deadline := nanotime() + ns
207 for {
208 if *cgo_yield != nil && ns > 10e6 {
209 ns = 10e6
210 }
211 gp.m.blocked = true
212 futexsleep(key32(&n.key), 0, ns)
213 if *cgo_yield != nil {
214 asmcgocall(*cgo_yield, nil)
215 }
216 gp.m.blocked = false
217 if atomic.Load(key32(&n.key)) != 0 {
218 break
219 }
220 now := nanotime()
221 if now >= deadline {
222 break
223 }
224 ns = deadline - now
225 }
226 return atomic.Load(key32(&n.key)) != 0
227 }
228
229 func notetsleep(n *note, ns int64) bool {
230 gp := getg()
231 if gp != gp.m.g0 && gp.m.preemptoff != "" {
232 throw("notetsleep not on g0")
233 }
234
235 return notetsleep_internal(n, ns)
236 }
237
238 // same as runtime·notetsleep, but called on user g (not g0)
239 // calls only nosplit functions between entersyscallblock/exitsyscall
240 func notetsleepg(n *note, ns int64) bool {
241 gp := getg()
242 if gp == gp.m.g0 {
243 throw("notetsleepg on g0")
244 }
245
246 entersyscallblock()
247 ok := notetsleep_internal(n, ns)
248 exitsyscall()
249 return ok
250 }
251
252 func beforeIdle(int64) (*g, bool) {
253 return nil, false
254 }
255
256 func checkTimeouts() {}