2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2014-08-29 14:20:48 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build darwin nacl netbsd openbsd plan9 solaris windows
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
2015-11-02 12:09:24 -07:00
|
|
|
import (
|
|
|
|
"runtime/internal/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2014-08-29 14:20:48 -06:00
|
|
|
|
|
|
|
// This implementation depends on OS-specific implementations of
|
|
|
|
//
|
2015-10-21 19:36:05 -06:00
|
|
|
// func semacreate(mp *m)
|
|
|
|
// Create a semaphore for mp, if it does not already have one.
|
2014-08-29 14:20:48 -06:00
|
|
|
//
|
2015-10-21 19:36:05 -06:00
|
|
|
// func semasleep(ns int64) int32
|
|
|
|
// If ns < 0, acquire m's semaphore and return 0.
|
|
|
|
// If ns >= 0, try to acquire m's semaphore for at most ns nanoseconds.
|
2014-08-29 14:20:48 -06:00
|
|
|
// Return 0 if the semaphore was acquired, -1 if interrupted or timed out.
|
|
|
|
//
|
2015-10-21 19:36:05 -06:00
|
|
|
// func semawakeup(mp *m)
|
|
|
|
// Wake up mp, which is or will soon be sleeping on its semaphore.
|
2014-08-29 14:20:48 -06:00
|
|
|
//
|
|
|
|
const (
|
|
|
|
locked uintptr = 1
|
|
|
|
|
|
|
|
active_spin = 4
|
|
|
|
active_spin_cnt = 30
|
|
|
|
passive_spin = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func lock(l *mutex) {
|
|
|
|
gp := getg()
|
|
|
|
if gp.m.locks < 0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime·lock: lock count")
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
|
|
|
gp.m.locks++
|
|
|
|
|
|
|
|
// Speculative grab for lock.
|
2015-11-02 12:09:24 -07:00
|
|
|
if atomic.Casuintptr(&l.key, 0, locked) {
|
2014-08-29 14:20:48 -06:00
|
|
|
return
|
|
|
|
}
|
2015-10-21 19:36:05 -06:00
|
|
|
semacreate(gp.m)
|
2014-08-29 14:20:48 -06:00
|
|
|
|
|
|
|
// On uniprocessor's, no point spinning.
|
|
|
|
// On multiprocessors, spin for ACTIVE_SPIN attempts.
|
|
|
|
spin := 0
|
|
|
|
if ncpu > 1 {
|
|
|
|
spin = active_spin
|
|
|
|
}
|
|
|
|
Loop:
|
|
|
|
for i := 0; ; i++ {
|
2015-11-02 12:09:24 -07:00
|
|
|
v := atomic.Loaduintptr(&l.key)
|
2014-08-29 14:20:48 -06:00
|
|
|
if v&locked == 0 {
|
|
|
|
// Unlocked. Try to lock.
|
2015-11-02 12:09:24 -07:00
|
|
|
if atomic.Casuintptr(&l.key, v, v|locked) {
|
2014-08-29 14:20:48 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
if i < spin {
|
|
|
|
procyield(active_spin_cnt)
|
|
|
|
} else if i < spin+passive_spin {
|
|
|
|
osyield()
|
|
|
|
} else {
|
|
|
|
// Someone else has it.
|
|
|
|
// l->waitm points to a linked list of M's waiting
|
|
|
|
// for this lock, chained through m->nextwaitm.
|
|
|
|
// Queue this M.
|
|
|
|
for {
|
2017-06-15 10:23:09 -06:00
|
|
|
gp.m.nextwaitm = muintptr(v &^ locked)
|
2015-11-02 12:09:24 -07:00
|
|
|
if atomic.Casuintptr(&l.key, v, uintptr(unsafe.Pointer(gp.m))|locked) {
|
2014-08-29 14:20:48 -06:00
|
|
|
break
|
|
|
|
}
|
2015-11-02 12:09:24 -07:00
|
|
|
v = atomic.Loaduintptr(&l.key)
|
2014-08-29 14:20:48 -06:00
|
|
|
if v&locked == 0 {
|
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v&locked != 0 {
|
2016-03-01 16:21:55 -07:00
|
|
|
// Queued. Wait.
|
2014-08-29 14:20:48 -06:00
|
|
|
semasleep(-1)
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 13:07:05 -06:00
|
|
|
//go:nowritebarrier
|
|
|
|
// We might not be holding a p in this code.
|
2014-08-29 14:20:48 -06:00
|
|
|
func unlock(l *mutex) {
|
|
|
|
gp := getg()
|
|
|
|
var mp *m
|
|
|
|
for {
|
2015-11-02 12:09:24 -07:00
|
|
|
v := atomic.Loaduintptr(&l.key)
|
2014-08-29 14:20:48 -06:00
|
|
|
if v == locked {
|
2015-11-02 12:09:24 -07:00
|
|
|
if atomic.Casuintptr(&l.key, locked, 0) {
|
2014-08-29 14:20:48 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Other M's are waiting for the lock.
|
|
|
|
// Dequeue an M.
|
2017-06-15 10:23:09 -06:00
|
|
|
mp = muintptr(v &^ locked).ptr()
|
|
|
|
if atomic.Casuintptr(&l.key, v, uintptr(mp.nextwaitm)) {
|
2014-08-29 14:20:48 -06:00
|
|
|
// Dequeued an M. Wake it.
|
|
|
|
semawakeup(mp)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gp.m.locks--
|
|
|
|
if gp.m.locks < 0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime·unlock: lock count")
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
|
|
|
if gp.m.locks == 0 && gp.preempt { // restore the preemption request in case we've cleared it in newstack
|
2015-01-05 09:29:21 -07:00
|
|
|
gp.stackguard0 = stackPreempt
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// One-time notifications.
|
|
|
|
func noteclear(n *note) {
|
|
|
|
n.key = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func notewakeup(n *note) {
|
|
|
|
var v uintptr
|
|
|
|
for {
|
2015-11-02 12:09:24 -07:00
|
|
|
v = atomic.Loaduintptr(&n.key)
|
|
|
|
if atomic.Casuintptr(&n.key, v, locked) {
|
2014-08-29 14:20:48 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Successfully set waitm to locked.
|
|
|
|
// What was it before?
|
|
|
|
switch {
|
|
|
|
case v == 0:
|
|
|
|
// Nothing was waiting. Done.
|
|
|
|
case v == locked:
|
2017-08-19 14:33:51 -06:00
|
|
|
// Two notewakeups! Not allowed.
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("notewakeup - double wakeup")
|
2014-08-29 14:20:48 -06:00
|
|
|
default:
|
2016-03-01 16:21:55 -07:00
|
|
|
// Must be the waiting m. Wake it up.
|
2014-08-29 14:20:48 -06:00
|
|
|
semawakeup((*m)(unsafe.Pointer(v)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func notesleep(n *note) {
|
|
|
|
gp := getg()
|
|
|
|
if gp != gp.m.g0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("notesleep not on g0")
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
2015-10-21 19:36:05 -06:00
|
|
|
semacreate(gp.m)
|
2015-11-02 12:09:24 -07:00
|
|
|
if !atomic.Casuintptr(&n.key, 0, uintptr(unsafe.Pointer(gp.m))) {
|
2014-08-29 14:20:48 -06:00
|
|
|
// Must be locked (got wakeup).
|
|
|
|
if n.key != locked {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("notesleep - waitm out of sync")
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2016-03-01 16:21:55 -07:00
|
|
|
// Queued. Sleep.
|
2014-08-29 14:20:48 -06:00
|
|
|
gp.m.blocked = true
|
2017-03-23 20:47:56 -06:00
|
|
|
if *cgo_yield == nil {
|
2017-01-19 14:09:10 -07:00
|
|
|
semasleep(-1)
|
|
|
|
} else {
|
|
|
|
// Sleep for an arbitrary-but-moderate interval to poll libc interceptors.
|
|
|
|
const ns = 10e6
|
|
|
|
for atomic.Loaduintptr(&n.key) == 0 {
|
|
|
|
semasleep(ns)
|
2017-03-23 20:47:56 -06:00
|
|
|
asmcgocall(*cgo_yield, nil)
|
2017-01-19 14:09:10 -07:00
|
|
|
}
|
|
|
|
}
|
2014-08-29 14:20:48 -06:00
|
|
|
gp.m.blocked = false
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
2014-09-03 21:10:15 -06:00
|
|
|
func notetsleep_internal(n *note, ns int64, gp *g, deadline int64) bool {
|
|
|
|
// gp and deadline are logically local variables, but they are written
|
|
|
|
// as parameters so that the stack space they require is charged
|
|
|
|
// to the caller.
|
|
|
|
// This reduces the nosplit footprint of notetsleep_internal.
|
|
|
|
gp = getg()
|
|
|
|
|
2014-08-29 14:20:48 -06:00
|
|
|
// Register for wakeup on n->waitm.
|
2015-11-02 12:09:24 -07:00
|
|
|
if !atomic.Casuintptr(&n.key, 0, uintptr(unsafe.Pointer(gp.m))) {
|
2014-08-29 14:20:48 -06:00
|
|
|
// Must be locked (got wakeup).
|
|
|
|
if n.key != locked {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("notetsleep - waitm out of sync")
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if ns < 0 {
|
2016-03-01 16:21:55 -07:00
|
|
|
// Queued. Sleep.
|
2014-08-29 14:20:48 -06:00
|
|
|
gp.m.blocked = true
|
2017-03-23 20:47:56 -06:00
|
|
|
if *cgo_yield == nil {
|
2017-01-19 14:09:10 -07:00
|
|
|
semasleep(-1)
|
|
|
|
} else {
|
2017-03-08 13:40:28 -07:00
|
|
|
// Sleep in arbitrary-but-moderate intervals to poll libc interceptors.
|
2017-01-19 14:09:10 -07:00
|
|
|
const ns = 10e6
|
2017-03-08 13:40:28 -07:00
|
|
|
for semasleep(ns) < 0 {
|
2017-03-23 20:47:56 -06:00
|
|
|
asmcgocall(*cgo_yield, nil)
|
2017-01-19 14:09:10 -07:00
|
|
|
}
|
|
|
|
}
|
2014-08-29 14:20:48 -06:00
|
|
|
gp.m.blocked = false
|
|
|
|
return true
|
|
|
|
}
|
2014-09-03 21:10:15 -06:00
|
|
|
|
|
|
|
deadline = nanotime() + ns
|
2014-08-29 14:20:48 -06:00
|
|
|
for {
|
2016-03-01 16:21:55 -07:00
|
|
|
// Registered. Sleep.
|
2014-08-29 14:20:48 -06:00
|
|
|
gp.m.blocked = true
|
2017-03-23 20:47:56 -06:00
|
|
|
if *cgo_yield != nil && ns > 10e6 {
|
2017-03-08 13:40:28 -07:00
|
|
|
ns = 10e6
|
|
|
|
}
|
2014-08-29 14:20:48 -06:00
|
|
|
if semasleep(ns) >= 0 {
|
|
|
|
gp.m.blocked = false
|
|
|
|
// Acquired semaphore, semawakeup unregistered us.
|
|
|
|
// Done.
|
|
|
|
return true
|
|
|
|
}
|
2017-03-23 20:47:56 -06:00
|
|
|
if *cgo_yield != nil {
|
|
|
|
asmcgocall(*cgo_yield, nil)
|
2017-03-08 13:40:28 -07:00
|
|
|
}
|
2014-08-29 14:20:48 -06:00
|
|
|
gp.m.blocked = false
|
2016-03-01 16:21:55 -07:00
|
|
|
// Interrupted or timed out. Still registered. Semaphore not acquired.
|
2014-08-29 14:20:48 -06:00
|
|
|
ns = deadline - nanotime()
|
|
|
|
if ns <= 0 {
|
|
|
|
break
|
|
|
|
}
|
2016-03-01 16:21:55 -07:00
|
|
|
// Deadline hasn't arrived. Keep sleeping.
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
|
|
|
|
2016-03-01 16:21:55 -07:00
|
|
|
// Deadline arrived. Still registered. Semaphore not acquired.
|
2014-08-29 14:20:48 -06:00
|
|
|
// Want to give up and return, but have to unregister first,
|
|
|
|
// so that any notewakeup racing with the return does not
|
|
|
|
// try to grant us the semaphore when we don't expect it.
|
|
|
|
for {
|
2015-11-02 12:09:24 -07:00
|
|
|
v := atomic.Loaduintptr(&n.key)
|
2014-08-29 14:20:48 -06:00
|
|
|
switch v {
|
|
|
|
case uintptr(unsafe.Pointer(gp.m)):
|
|
|
|
// No wakeup yet; unregister if possible.
|
2015-11-02 12:09:24 -07:00
|
|
|
if atomic.Casuintptr(&n.key, v, 0) {
|
2014-08-29 14:20:48 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
case locked:
|
|
|
|
// Wakeup happened so semaphore is available.
|
|
|
|
// Grab it to avoid getting out of sync.
|
|
|
|
gp.m.blocked = true
|
|
|
|
if semasleep(-1) < 0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime: unable to acquire - semaphore out of sync")
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
|
|
|
gp.m.blocked = false
|
|
|
|
return true
|
|
|
|
default:
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime: unexpected waitm - semaphore out of sync")
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func notetsleep(n *note, ns int64) bool {
|
|
|
|
gp := getg()
|
2015-01-30 13:30:41 -07:00
|
|
|
if gp != gp.m.g0 && gp.m.preemptoff != "" {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("notetsleep not on g0")
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
2015-10-21 19:36:05 -06:00
|
|
|
semacreate(gp.m)
|
2014-09-03 21:10:15 -06:00
|
|
|
return notetsleep_internal(n, ns, nil, 0)
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// same as runtime·notetsleep, but called on user g (not g0)
|
|
|
|
// calls only nosplit functions between entersyscallblock/exitsyscall
|
|
|
|
func notetsleepg(n *note, ns int64) bool {
|
|
|
|
gp := getg()
|
|
|
|
if gp == gp.m.g0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("notetsleepg on g0")
|
2014-08-29 14:20:48 -06:00
|
|
|
}
|
2015-10-21 19:36:05 -06:00
|
|
|
semacreate(gp.m)
|
2014-11-11 15:08:33 -07:00
|
|
|
entersyscallblock(0)
|
2014-09-03 21:10:15 -06:00
|
|
|
ok := notetsleep_internal(n, ns, nil, 0)
|
2014-11-11 15:08:33 -07:00
|
|
|
exitsyscall(0)
|
2014-08-29 14:20:48 -06:00
|
|
|
return ok
|
|
|
|
}
|