From 1dd590b91b893a413666b6daaed848d89bab7f05 Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Sat, 13 Jan 2018 09:00:37 +0100 Subject: Work on timer teardown + bug fixes Added waitgroups to peer struct for routine start / stop synchronisation --- src/device.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src/device.go') diff --git a/src/device.go b/src/device.go index f4a087c..5f8e91b 100644 --- a/src/device.go +++ b/src/device.go @@ -1,6 +1,7 @@ package main import ( + "github.com/sasha-s/go-deadlock" "runtime" "sync" "sync/atomic" @@ -21,12 +22,12 @@ type Device struct { messageBuffers sync.Pool } net struct { - mutex sync.RWMutex + mutex deadlock.RWMutex bind Bind // bind interface port uint16 // listening port fwmark uint32 // mark value (0 = disabled) } - mutex sync.RWMutex + mutex deadlock.RWMutex privateKey NoisePrivateKey publicKey NoisePublicKey routingTable RoutingTable @@ -49,8 +50,15 @@ func (device *Device) Up() { device.mutex.Lock() defer device.mutex.Unlock() - device.isUp.Set(true) - updateBind(device) + device.net.mutex.Lock() + defer device.net.mutex.Unlock() + + if device.isUp.Swap(true) { + return + } + + unsafeUpdateBind(device) + for _, peer := range device.peers { peer.Start() } @@ -60,8 +68,12 @@ func (device *Device) Down() { device.mutex.Lock() defer device.mutex.Unlock() - device.isUp.Set(false) + if !device.isUp.Swap(false) { + return + } + closeBind(device) + for _, peer := range device.peers { peer.Stop() } @@ -75,7 +87,6 @@ func removePeerUnsafe(device *Device, key NoisePublicKey) { if !ok { return } - peer.mutex.Lock() peer.Stop() device.routingTable.RemovePeer(peer) delete(device.peers, key) -- cgit v1.2.3