libs: wrap mutexes for build flag with godeadlock (#5126)

## Description

This PR wraps the stdlib sync.(RW)Mutex & godeadlock.(RW)Mutex. This enables using go-deadlock via a build flag instead of using sed to replace sync with godeadlock in all files

Closes: #3242
This commit is contained in:
Marko
2020-07-20 09:55:09 +02:00
committed by GitHub
parent 8cdb53c811
commit 2ac5a559b4
61 changed files with 182 additions and 145 deletions

View File

@@ -14,6 +14,8 @@ to ensure garbage collection of removed elements.
import (
"fmt"
"sync"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
// MaxLength is the max allowed number of elements a linked list is
@@ -42,7 +44,7 @@ waiting on NextWait() (since it's just a read operation).
*/
type CElement struct {
mtx sync.RWMutex
mtx tmsync.RWMutex
prev *CElement
prevWg *sync.WaitGroup
prevWaitCh chan struct{}
@@ -218,7 +220,7 @@ func (e *CElement) SetRemoved() {
// Operations are goroutine-safe.
// Panics if length grows beyond the max.
type CList struct {
mtx sync.RWMutex
mtx tmsync.RWMutex
wg *sync.WaitGroup
waitCh chan struct{}
head *CElement // first element

View File

@@ -1,11 +1,13 @@
package cmap
import "sync"
import (
tmsync "github.com/tendermint/tendermint/libs/sync"
)
// CMap is a goroutine-safe map
type CMap struct {
m map[string]interface{}
l sync.Mutex
l tmsync.Mutex
}
func NewCMap() *CMap {

View File

@@ -3,9 +3,9 @@ package events
import (
"fmt"
"sync"
"github.com/tendermint/tendermint/libs/service"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
// ErrListenerWasRemoved is returned by AddEvent if the listener was removed.
@@ -54,7 +54,7 @@ type EventSwitch interface {
type eventSwitch struct {
service.BaseService
mtx sync.RWMutex
mtx tmsync.RWMutex
eventCells map[string]*eventCell
listeners map[string]*eventListener
}
@@ -162,7 +162,7 @@ func (evsw *eventSwitch) FireEvent(event string, data EventData) {
// eventCell handles keeping track of listener callbacks for a given event.
type eventCell struct {
mtx sync.RWMutex
mtx tmsync.RWMutex
listeners map[string]EventCallback
}
@@ -206,7 +206,7 @@ type EventCallback func(data EventData)
type eventListener struct {
id string
mtx sync.RWMutex
mtx tmsync.RWMutex
removed bool
events []string
}

View File

@@ -8,13 +8,14 @@ package flowrate
import (
"math"
"sync"
"time"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
// Monitor monitors and limits the transfer rate of a data stream.
type Monitor struct {
mu sync.Mutex // Mutex guarding access to all internal fields
mu tmsync.Mutex // Mutex guarding access to all internal fields
active bool // Flag indicating an active transfer
start time.Duration // Transfer start time (clock() value)
bytes int64 // Total number of bytes transferred

View File

@@ -4,8 +4,9 @@ import (
"fmt"
"reflect"
"strings"
"sync"
"unicode"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
var (
@@ -15,7 +16,7 @@ var (
// structCache is a cache of struct info.
type structInfoCache struct {
sync.RWMutex
tmsync.RWMutex
structInfos map[reflect.Type]*structInfo
}

View File

@@ -4,7 +4,8 @@ import (
"errors"
"fmt"
"reflect"
"sync"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
var (
@@ -38,7 +39,7 @@ type typeInfo struct {
// types is a type registry. It is safe for concurrent use.
type types struct {
sync.RWMutex
tmsync.RWMutex
byType map[reflect.Type]*typeInfo
byName map[string]*typeInfo
}

View File

@@ -38,9 +38,9 @@ import (
"context"
"errors"
"fmt"
"sync"
"github.com/tendermint/tendermint/libs/service"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
type operation int
@@ -96,7 +96,7 @@ type Server struct {
// check if we have subscription before
// subscribing or unsubscribing
mtx sync.RWMutex
mtx tmsync.RWMutex
subscriptions map[string]map[string]struct{} // subscriber -> query (string) -> empty struct
}

View File

@@ -2,7 +2,8 @@ package pubsub
import (
"errors"
"sync"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
var (
@@ -23,7 +24,7 @@ type Subscription struct {
out chan Message
cancelled chan struct{}
mtx sync.RWMutex
mtx tmsync.RWMutex
err error
}

View File

@@ -3,8 +3,9 @@ package rand
import (
crand "crypto/rand"
mrand "math/rand"
"sync"
"time"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
const (
@@ -19,7 +20,7 @@ const (
// All of the methods here are suitable for concurrent use.
// This is achieved by using a mutex lock on all of the provided methods.
type Rand struct {
sync.Mutex
tmsync.Mutex
rand *mrand.Rand
}

17
libs/sync/deadlock.go Normal file
View File

@@ -0,0 +1,17 @@
// +build deadlock
package sync
import (
deadlock "github.com/sasha-s/go-deadlock"
)
// A Mutex is a mutual exclusion lock.
type Mutex struct {
deadlock.Mutex
}
// An RWMutex is a reader/writer mutual exclusion lock.
type RWMutex struct {
deadlock.RWMutex
}

15
libs/sync/sync.go Normal file
View File

@@ -0,0 +1,15 @@
// +build !deadlock
package sync
import "sync"
// A Mutex is a mutual exclusion lock.
type Mutex struct {
sync.Mutex
}
// An RWMutex is a reader/writer mutual exclusion lock.
type RWMutex struct {
sync.RWMutex
}

View File

@@ -7,8 +7,9 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
const (
@@ -31,7 +32,7 @@ const (
var (
atomicWriteFileRand uint64
atomicWriteFileRandMu sync.Mutex
atomicWriteFileRandMu tmsync.Mutex
)
func writeFileRandReseed() uint64 {

View File

@@ -1,8 +1,9 @@
package timer
import (
"sync"
"time"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
/*
@@ -17,7 +18,7 @@ type ThrottleTimer struct {
quit chan struct{}
dur time.Duration
mtx sync.Mutex
mtx tmsync.Mutex
timer *time.Timer
isSet bool
}

View File

@@ -1,18 +1,19 @@
package timer
import (
"sync"
"testing"
"time"
// make govet noshadow happy...
asrt "github.com/stretchr/testify/assert"
tmsync "github.com/tendermint/tendermint/libs/sync"
)
type thCounter struct {
input chan struct{}
mtx sync.Mutex
mtx tmsync.Mutex
count int
}