Removed debugging locks

This commit is contained in:
Mathias Hall-Andersen 2018-02-04 16:46:24 +01:00
parent a0f54cbe5a
commit 9c5083bd19
2 changed files with 44 additions and 53 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"github.com/sasha-s/go-deadlock"
"runtime" "runtime"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -16,31 +15,31 @@ type Device struct {
// synchronized resources (locks acquired in order) // synchronized resources (locks acquired in order)
state struct { state struct {
mutex deadlock.Mutex mutex sync.Mutex
changing AtomicBool changing AtomicBool
current bool current bool
} }
net struct { net struct {
mutex deadlock.RWMutex mutex sync.RWMutex
bind Bind // bind interface bind Bind // bind interface
port uint16 // listening port port uint16 // listening port
fwmark uint32 // mark value (0 = disabled) fwmark uint32 // mark value (0 = disabled)
} }
noise struct { noise struct {
mutex deadlock.RWMutex mutex sync.RWMutex
privateKey NoisePrivateKey privateKey NoisePrivateKey
publicKey NoisePublicKey publicKey NoisePublicKey
} }
routing struct { routing struct {
mutex deadlock.RWMutex mutex sync.RWMutex
table RoutingTable table RoutingTable
} }
peers struct { peers struct {
mutex deadlock.RWMutex mutex sync.RWMutex
keyMap map[NoisePublicKey]*Peer keyMap map[NoisePublicKey]*Peer
} }
@ -101,17 +100,15 @@ func deviceUpdateState(device *Device) {
return return
} }
func() {
// compare to current state of device // compare to current state of device
device.state.mutex.Lock() device.state.mutex.Lock()
defer device.state.mutex.Unlock()
newIsUp := device.isUp.Get() newIsUp := device.isUp.Get()
if newIsUp == device.state.current { if newIsUp == device.state.current {
device.state.changing.Set(false) device.state.changing.Set(false)
device.state.mutex.Unlock()
return return
} }
@ -123,31 +120,26 @@ func deviceUpdateState(device *Device) {
device.isUp.Set(false) device.isUp.Set(false)
break break
} }
device.peers.mutex.Lock() device.peers.mutex.Lock()
defer device.peers.mutex.Unlock()
for _, peer := range device.peers.keyMap { for _, peer := range device.peers.keyMap {
peer.Start() peer.Start()
} }
device.peers.mutex.Unlock()
case false: case false:
device.BindClose() device.BindClose()
device.peers.mutex.Lock() device.peers.mutex.Lock()
defer device.peers.mutex.Unlock()
for _, peer := range device.peers.keyMap { for _, peer := range device.peers.keyMap {
println("stopping peer")
peer.Stop() peer.Stop()
} }
device.peers.mutex.Unlock()
} }
// update state variables // update state variables
device.state.current = newIsUp device.state.current = newIsUp
device.state.changing.Set(false) device.state.changing.Set(false)
}() device.state.mutex.Unlock()
// check for state change in the mean time // check for state change in the mean time

View File

@ -4,7 +4,6 @@ import (
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt" "fmt"
"github.com/sasha-s/go-deadlock"
"sync" "sync"
"time" "time"
) )
@ -15,7 +14,7 @@ const (
type Peer struct { type Peer struct {
isRunning AtomicBool isRunning AtomicBool
mutex deadlock.RWMutex mutex sync.RWMutex
persistentKeepaliveInterval uint64 persistentKeepaliveInterval uint64
keyPairs KeyPairs keyPairs KeyPairs
handshake Handshake handshake Handshake
@ -29,7 +28,7 @@ type Peer struct {
} }
time struct { time struct {
mutex deadlock.RWMutex mutex sync.RWMutex
lastSend time.Time // last send message lastSend time.Time // last send message
lastHandshake time.Time // last completed handshake lastHandshake time.Time // last completed handshake
nextKeepalive time.Time nextKeepalive time.Time
@ -66,7 +65,7 @@ type Peer struct {
} }
routines struct { routines struct {
mutex deadlock.Mutex // held when stopping / starting routines mutex sync.Mutex // held when stopping / starting routines
starting sync.WaitGroup // routines pending start starting sync.WaitGroup // routines pending start
stopping sync.WaitGroup // routines pending stop stopping sync.WaitGroup // routines pending stop
stop Signal // size 0, stop all go-routines in peer stop Signal // size 0, stop all go-routines in peer