Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e92865

Browse files
josharianzx2c4
authored andcommitted
device: remove starting waitgroups
In each case, the starting waitgroup did nothing but ensure that the goroutine has launched. Nothing downstream depends on the order in which goroutines launch, and if the Go runtime scheduler is so broken that goroutines don't get launched reasonably promptly, we have much deeper problems. Given all that, simplify the code. Passed a race-enabled stress test 25,000 times without failure. Signed-off-by: Josh Bleecher Snyder <[email protected]>
1 parent a5a9c0d commit 5e92865

File tree

5 files changed

+1
-29
lines changed

5 files changed

+1
-29
lines changed

device/device.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ type Device struct {
2727
// synchronized resources (locks acquired in order)
2828

2929
state struct {
30-
starting sync.WaitGroup
3130
stopping sync.WaitGroup
3231
sync.Mutex
3332
changing AtomicBool
3433
current bool
3534
}
3635

3736
net struct {
38-
starting sync.WaitGroup
3937
stopping sync.WaitGroup
4038
sync.RWMutex
4139
bind conn.Bind // bind interface
@@ -297,23 +295,18 @@ func NewDevice(tunDevice tun.Device, logger *Logger) *Device {
297295
// start workers
298296

299297
cpus := runtime.NumCPU()
300-
device.state.starting.Wait()
301298
device.state.stopping.Wait()
302299
for i := 0; i < cpus; i += 1 {
303-
device.state.starting.Add(3)
304300
device.state.stopping.Add(3)
305301
go device.RoutineEncryption()
306302
go device.RoutineDecryption()
307303
go device.RoutineHandshake()
308304
}
309305

310-
device.state.starting.Add(2)
311306
device.state.stopping.Add(2)
312307
go device.RoutineReadFromTUN()
313308
go device.RoutineTUNEventReader()
314309

315-
device.state.starting.Wait()
316-
317310
return device
318311
}
319312

@@ -370,8 +363,6 @@ func (device *Device) Close() {
370363
return
371364
}
372365

373-
device.state.starting.Wait()
374-
375366
device.log.Info.Println("Device closing")
376367
device.state.changing.Set(true)
377368
device.state.Lock()
@@ -527,11 +518,9 @@ func (device *Device) BindUpdate() error {
527518

528519
// start receiving routines
529520

530-
device.net.starting.Add(2)
531521
device.net.stopping.Add(2)
532522
go device.RoutineReceiveIncoming(ipv4.Version, netc.bind)
533523
go device.RoutineReceiveIncoming(ipv6.Version, netc.bind)
534-
device.net.starting.Wait()
535524

536525
device.log.Debug.Println("UDP bind has been updated")
537526
}

device/peer.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ type Peer struct {
6666
}
6767

6868
routines struct {
69-
sync.Mutex // held when stopping / starting routines
70-
starting sync.WaitGroup // routines pending start
69+
sync.Mutex // held when stopping routines
7170
stopping sync.WaitGroup // routines pending stop
7271
stop chan struct{} // size 0, stop all go routines in peer
7372
}
@@ -189,10 +188,8 @@ func (peer *Peer) Start() {
189188

190189
// reset routine state
191190

192-
peer.routines.starting.Wait()
193191
peer.routines.stopping.Wait()
194192
peer.routines.stop = make(chan struct{})
195-
peer.routines.starting.Add(PeerRoutineNumber)
196193
peer.routines.stopping.Add(PeerRoutineNumber)
197194

198195
// prepare queues
@@ -213,7 +210,6 @@ func (peer *Peer) Start() {
213210
go peer.RoutineSequentialSender()
214211
go peer.RoutineSequentialReceiver()
215212

216-
peer.routines.starting.Wait()
217213
peer.isRunning.Set(true)
218214
}
219215

@@ -270,8 +266,6 @@ func (peer *Peer) Stop() {
270266
return
271267
}
272268

273-
peer.routines.starting.Wait()
274-
275269
peer.routines.Lock()
276270
defer peer.routines.Unlock()
277271

device/receive.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ func (device *Device) RoutineReceiveIncoming(IP int, bind conn.Bind) {
111111
}()
112112

113113
logDebug.Println("Routine: receive incoming IPv" + strconv.Itoa(IP) + " - started")
114-
device.net.starting.Done()
115114

116115
// receive datagrams until conn is closed
117116

@@ -246,7 +245,6 @@ func (device *Device) RoutineDecryption() {
246245
device.state.stopping.Done()
247246
}()
248247
logDebug.Println("Routine: decryption worker - started")
249-
device.state.starting.Done()
250248

251249
for {
252250
select {
@@ -321,7 +319,6 @@ func (device *Device) RoutineHandshake() {
321319
}()
322320

323321
logDebug.Println("Routine: handshake worker - started")
324-
device.state.starting.Done()
325322

326323
for {
327324
if elem.buffer != nil {
@@ -521,8 +518,6 @@ func (peer *Peer) RoutineSequentialReceiver() {
521518

522519
logDebug.Println(peer, "- Routine: sequential receiver - started")
523520

524-
peer.routines.starting.Done()
525-
526521
for {
527522
if elem != nil {
528523
if !elem.IsDropped() {

device/send.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ func (device *Device) RoutineReadFromTUN() {
262262
}()
263263

264264
logDebug.Println("Routine: TUN reader - started")
265-
device.state.starting.Done()
266265

267266
var elem *QueueOutboundElement
268267

@@ -372,7 +371,6 @@ func (peer *Peer) RoutineNonce() {
372371
peer.routines.stopping.Done()
373372
}()
374373

375-
peer.routines.starting.Done()
376374
logDebug.Println(peer, "- Routine: nonce worker - started")
377375

378376
NextPacket:
@@ -507,7 +505,6 @@ func (device *Device) RoutineEncryption() {
507505
}()
508506

509507
logDebug.Println("Routine: encryption worker - started")
510-
device.state.starting.Done()
511508

512509
for {
513510

@@ -596,8 +593,6 @@ func (peer *Peer) RoutineSequentialSender() {
596593

597594
logDebug.Println(peer, "- Routine: sequential sender - started")
598595

599-
peer.routines.starting.Done()
600-
601596
for {
602597
select {
603598

device/tun.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func (device *Device) RoutineTUNEventReader() {
2020
logError := device.log.Error
2121

2222
logDebug.Println("Routine: event worker - started")
23-
device.state.starting.Done()
2423

2524
for event := range device.tun.device.Events() {
2625
if event&tun.EventMTUUpdate != 0 {

0 commit comments

Comments
 (0)