diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-05-16 22:20:15 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-05-16 22:20:15 +0200 |
commit | 846d721dfd0cde953f2e9304d6ef50110de050eb (patch) | |
tree | 8de15914ab39d0aad1b50d03530b82fece54c740 /device.go | |
parent | 23eca94508d7cef0c1adbbc37c81050899ca1d60 (diff) |
Finer-grained start-stop synchronization
Diffstat (limited to 'device.go')
-rw-r--r-- | device.go | 12 |
1 files changed, 11 insertions, 1 deletions
@@ -15,6 +15,7 @@ import ( const ( DeviceRoutineNumberPerCPU = 3 + DeviceRoutineNumberAdditional = 2 ) type Device struct { @@ -25,6 +26,7 @@ type Device struct { // synchronized resources (locks acquired in order) state struct { + starting sync.WaitGroup stopping sync.WaitGroup mutex sync.Mutex changing AtomicBool @@ -297,7 +299,10 @@ func NewDevice(tun TUNDevice, logger *Logger) *Device { // start workers cpus := runtime.NumCPU() - device.state.stopping.Add(DeviceRoutineNumberPerCPU * cpus) + device.state.starting.Wait() + device.state.stopping.Wait() + device.state.stopping.Add(DeviceRoutineNumberPerCPU * cpus + DeviceRoutineNumberAdditional) + device.state.starting.Add(DeviceRoutineNumberPerCPU * cpus + DeviceRoutineNumberAdditional) for i := 0; i < cpus; i += 1 { go device.RoutineEncryption() go device.RoutineDecryption() @@ -307,6 +312,8 @@ func NewDevice(tun TUNDevice, logger *Logger) *Device { go device.RoutineReadFromTUN() go device.RoutineTUNEventReader() + device.state.starting.Wait() + return device } @@ -363,6 +370,9 @@ func (device *Device) Close() { if device.isClosed.Swap(true) { return } + + device.state.starting.Wait() + device.log.Info.Println("Device closing") device.state.changing.Set(true) device.state.mutex.Lock() |