2015-04-22 22:54:31 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-04-22 22:54:31 +00:00
|
|
|
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2016-10-18 20:00:01 +00:00
|
|
|
"strconv"
|
2015-04-23 10:29:23 +00:00
|
|
|
"strings"
|
2015-04-22 22:54:31 +00:00
|
|
|
"sync"
|
2015-04-23 10:29:23 +00:00
|
|
|
"sync/atomic"
|
2015-04-22 22:54:31 +00:00
|
|
|
"time"
|
2016-11-05 02:24:53 +00:00
|
|
|
|
|
|
|
"github.com/sasha-s/go-deadlock"
|
2015-04-22 22:54:31 +00:00
|
|
|
)
|
|
|
|
|
2021-11-08 14:12:57 +00:00
|
|
|
var timeNow = time.Now
|
2017-04-01 11:03:11 +00:00
|
|
|
|
2015-04-22 22:54:31 +00:00
|
|
|
type Mutex interface {
|
|
|
|
Lock()
|
|
|
|
Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
type RWMutex interface {
|
|
|
|
Mutex
|
|
|
|
RLock()
|
|
|
|
RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
type WaitGroup interface {
|
|
|
|
Add(int)
|
|
|
|
Done()
|
|
|
|
Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMutex() Mutex {
|
2016-11-05 02:24:53 +00:00
|
|
|
if useDeadlock {
|
2016-11-05 02:31:52 +00:00
|
|
|
return &deadlock.Mutex{}
|
2016-11-05 02:24:53 +00:00
|
|
|
}
|
2015-04-22 22:54:31 +00:00
|
|
|
if debug {
|
2016-11-03 21:33:33 +00:00
|
|
|
mutex := &loggedMutex{}
|
|
|
|
mutex.holder.Store(holder{})
|
|
|
|
return mutex
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
|
|
|
return &sync.Mutex{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRWMutex() RWMutex {
|
2016-11-05 02:24:53 +00:00
|
|
|
if useDeadlock {
|
2016-11-05 02:31:52 +00:00
|
|
|
return &deadlock.RWMutex{}
|
2016-11-05 02:24:53 +00:00
|
|
|
}
|
2015-04-22 22:54:31 +00:00
|
|
|
if debug {
|
2016-11-03 21:33:33 +00:00
|
|
|
mutex := &loggedRWMutex{
|
|
|
|
readHolders: make(map[int][]holder),
|
|
|
|
unlockers: make(chan holder, 1024),
|
2015-04-23 10:29:23 +00:00
|
|
|
}
|
2016-11-03 21:33:33 +00:00
|
|
|
mutex.holder.Store(holder{})
|
|
|
|
return mutex
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
|
|
|
return &sync.RWMutex{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWaitGroup() WaitGroup {
|
|
|
|
if debug {
|
|
|
|
return &loggedWaitGroup{}
|
|
|
|
}
|
|
|
|
return &sync.WaitGroup{}
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:33:33 +00:00
|
|
|
type holder struct {
|
|
|
|
at string
|
|
|
|
time time.Time
|
|
|
|
goid int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h holder) String() string {
|
|
|
|
if h.at == "" {
|
|
|
|
return "not held"
|
|
|
|
}
|
2021-11-08 14:12:57 +00:00
|
|
|
return fmt.Sprintf("at %s goid: %d for %s", h.at, h.goid, timeNow().Sub(h.time))
|
2016-11-03 21:33:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 22:54:31 +00:00
|
|
|
type loggedMutex struct {
|
|
|
|
sync.Mutex
|
2016-11-03 21:33:33 +00:00
|
|
|
holder atomic.Value
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *loggedMutex) Lock() {
|
|
|
|
m.Mutex.Lock()
|
2016-11-03 21:33:33 +00:00
|
|
|
m.holder.Store(getHolder())
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *loggedMutex) Unlock() {
|
2016-11-03 21:33:33 +00:00
|
|
|
currentHolder := m.holder.Load().(holder)
|
2021-11-08 14:12:57 +00:00
|
|
|
duration := timeNow().Sub(currentHolder.time)
|
2015-04-22 22:54:31 +00:00
|
|
|
if duration >= threshold {
|
2016-11-03 21:33:33 +00:00
|
|
|
l.Debugf("Mutex held for %v. Locked at %s unlocked at %s", duration, currentHolder.at, getHolder().at)
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
2016-11-03 21:33:33 +00:00
|
|
|
m.holder.Store(holder{})
|
2015-04-22 22:54:31 +00:00
|
|
|
m.Mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:33:33 +00:00
|
|
|
func (m *loggedMutex) Holders() string {
|
|
|
|
return m.holder.Load().(holder).String()
|
2016-10-18 20:00:01 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 22:54:31 +00:00
|
|
|
type loggedRWMutex struct {
|
|
|
|
sync.RWMutex
|
2016-11-03 21:33:33 +00:00
|
|
|
holder atomic.Value
|
2015-04-23 10:29:23 +00:00
|
|
|
|
2016-11-03 21:33:33 +00:00
|
|
|
readHolders map[int][]holder
|
|
|
|
readHoldersMut sync.Mutex
|
2015-04-23 10:29:23 +00:00
|
|
|
|
2023-02-07 11:07:34 +00:00
|
|
|
logUnlockers atomic.Bool
|
2016-11-03 21:33:33 +00:00
|
|
|
unlockers chan holder
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *loggedRWMutex) Lock() {
|
2021-11-08 14:12:57 +00:00
|
|
|
start := timeNow()
|
2015-04-22 22:54:31 +00:00
|
|
|
|
2023-02-07 11:07:34 +00:00
|
|
|
m.logUnlockers.Store(true)
|
2015-04-22 22:54:31 +00:00
|
|
|
m.RWMutex.Lock()
|
2023-02-07 11:07:34 +00:00
|
|
|
m.logUnlockers.Store(false)
|
2015-04-22 22:54:31 +00:00
|
|
|
|
2016-11-03 21:33:33 +00:00
|
|
|
holder := getHolder()
|
|
|
|
m.holder.Store(holder)
|
|
|
|
|
|
|
|
duration := holder.time.Sub(start)
|
2015-04-22 22:54:31 +00:00
|
|
|
|
|
|
|
if duration > threshold {
|
2016-11-03 21:33:33 +00:00
|
|
|
var unlockerStrings []string
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case holder := <-m.unlockers:
|
|
|
|
unlockerStrings = append(unlockerStrings, holder.String())
|
|
|
|
default:
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Debugf("RWMutex took %v to lock. Locked at %s. RUnlockers while locking:\n%s", duration, holder.at, strings.Join(unlockerStrings, "\n"))
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *loggedRWMutex) Unlock() {
|
2016-11-03 21:33:33 +00:00
|
|
|
currentHolder := m.holder.Load().(holder)
|
2021-11-08 14:12:57 +00:00
|
|
|
duration := timeNow().Sub(currentHolder.time)
|
2015-04-22 22:54:31 +00:00
|
|
|
if duration >= threshold {
|
2016-11-03 21:33:33 +00:00
|
|
|
l.Debugf("RWMutex held for %v. Locked at %s unlocked at %s", duration, currentHolder.at, getHolder().at)
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
2016-11-03 21:33:33 +00:00
|
|
|
m.holder.Store(holder{})
|
2015-04-22 22:54:31 +00:00
|
|
|
m.RWMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:33:33 +00:00
|
|
|
func (m *loggedRWMutex) RLock() {
|
|
|
|
m.RWMutex.RLock()
|
|
|
|
holder := getHolder()
|
|
|
|
m.readHoldersMut.Lock()
|
|
|
|
m.readHolders[holder.goid] = append(m.readHolders[holder.goid], holder)
|
|
|
|
m.readHoldersMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-04-23 10:29:23 +00:00
|
|
|
func (m *loggedRWMutex) RUnlock() {
|
2016-11-03 21:33:33 +00:00
|
|
|
id := goid()
|
|
|
|
m.readHoldersMut.Lock()
|
|
|
|
current := m.readHolders[id]
|
|
|
|
if len(current) > 0 {
|
|
|
|
m.readHolders[id] = current[:len(current)-1]
|
|
|
|
}
|
|
|
|
m.readHoldersMut.Unlock()
|
2023-02-07 11:07:34 +00:00
|
|
|
if m.logUnlockers.Load() {
|
2016-11-03 21:33:33 +00:00
|
|
|
holder := getHolder()
|
|
|
|
select {
|
|
|
|
case m.unlockers <- holder:
|
|
|
|
default:
|
|
|
|
l.Debugf("Dropped holder %s as channel full", holder)
|
|
|
|
}
|
2015-04-23 10:29:23 +00:00
|
|
|
}
|
|
|
|
m.RWMutex.RUnlock()
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:33:33 +00:00
|
|
|
func (m *loggedRWMutex) Holders() string {
|
|
|
|
output := m.holder.Load().(holder).String() + " (writer)"
|
|
|
|
m.readHoldersMut.Lock()
|
|
|
|
for _, holders := range m.readHolders {
|
|
|
|
for _, holder := range holders {
|
|
|
|
output += "\n" + holder.String() + " (reader)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.readHoldersMut.Unlock()
|
|
|
|
return output
|
2016-10-29 23:14:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 22:54:31 +00:00
|
|
|
type loggedWaitGroup struct {
|
|
|
|
sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:09:56 +00:00
|
|
|
func (wg *loggedWaitGroup) Wait() {
|
2021-11-08 14:12:57 +00:00
|
|
|
start := timeNow()
|
2015-04-23 17:09:56 +00:00
|
|
|
wg.WaitGroup.Wait()
|
2021-11-08 14:12:57 +00:00
|
|
|
duration := timeNow().Sub(start)
|
2015-04-23 17:09:56 +00:00
|
|
|
if duration >= threshold {
|
2016-11-03 21:33:33 +00:00
|
|
|
l.Debugf("WaitGroup took %v at %s", duration, getHolder())
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:33:33 +00:00
|
|
|
func getHolder() holder {
|
2015-04-23 17:09:56 +00:00
|
|
|
_, file, line, _ := runtime.Caller(2)
|
2015-04-22 22:54:31 +00:00
|
|
|
file = filepath.Join(filepath.Base(filepath.Dir(file)), filepath.Base(file))
|
2016-11-03 21:33:33 +00:00
|
|
|
return holder{
|
|
|
|
at: fmt.Sprintf("%s:%d", file, line),
|
|
|
|
goid: goid(),
|
2021-11-08 14:12:57 +00:00
|
|
|
time: timeNow(),
|
2016-11-03 21:33:33 +00:00
|
|
|
}
|
2015-04-22 22:54:31 +00:00
|
|
|
}
|
2016-10-18 20:00:01 +00:00
|
|
|
|
|
|
|
func goid() int {
|
|
|
|
var buf [64]byte
|
|
|
|
n := runtime.Stack(buf[:], false)
|
|
|
|
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
|
|
|
|
id, err := strconv.Atoi(idField)
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
2017-01-31 12:04:29 +00:00
|
|
|
|
|
|
|
// TimeoutCond is a variant on Cond. It has roughly the same semantics regarding 'L' - it must be held
|
|
|
|
// both when broadcasting and when calling TimeoutCondWaiter.Wait()
|
|
|
|
// Call Broadcast() to broadcast to all waiters on the TimeoutCond. Call SetupWait to create a
|
|
|
|
// TimeoutCondWaiter configured with the given timeout, which can then be used to listen for
|
|
|
|
// broadcasts.
|
|
|
|
type TimeoutCond struct {
|
|
|
|
L sync.Locker
|
|
|
|
ch chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimeoutCondWaiter is a type allowing a consumer to wait on a TimeoutCond with a timeout. Wait() may be called multiple times,
|
|
|
|
// and will return true every time that the TimeoutCond is broadcast to. Once the configured timeout
|
|
|
|
// expires, Wait() will return false.
|
|
|
|
// Call Stop() to release resources once this TimeoutCondWaiter is no longer needed.
|
|
|
|
type TimeoutCondWaiter struct {
|
|
|
|
c *TimeoutCond
|
|
|
|
timer *time.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTimeoutCond(l sync.Locker) *TimeoutCond {
|
|
|
|
return &TimeoutCond{
|
|
|
|
L: l,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TimeoutCond) Broadcast() {
|
|
|
|
// ch.L must be locked when calling this function
|
|
|
|
|
|
|
|
if c.ch != nil {
|
|
|
|
close(c.ch)
|
|
|
|
c.ch = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TimeoutCond) SetupWait(timeout time.Duration) *TimeoutCondWaiter {
|
|
|
|
timer := time.NewTimer(timeout)
|
|
|
|
|
|
|
|
return &TimeoutCondWaiter{
|
|
|
|
c: c,
|
|
|
|
timer: timer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *TimeoutCondWaiter) Wait() bool {
|
|
|
|
// ch.L must be locked when calling this function
|
|
|
|
|
|
|
|
// Ensure that the channel exists, since we're going to be waiting on it
|
|
|
|
if w.c.ch == nil {
|
|
|
|
w.c.ch = make(chan struct{})
|
|
|
|
}
|
|
|
|
ch := w.c.ch
|
|
|
|
|
|
|
|
w.c.L.Unlock()
|
|
|
|
defer w.c.L.Lock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-w.timer.C:
|
|
|
|
return false
|
|
|
|
case <-ch:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *TimeoutCondWaiter) Stop() {
|
|
|
|
w.timer.Stop()
|
|
|
|
}
|