mirror of
https://git.zx2c4.com/wireguard-go
synced 2025-10-05 16:47:02 +08:00
all: use Go 1.19 and its atomic types
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:

committed by
Jason A. Donenfeld

parent
d1d08426b2
commit
b51010ba13
@@ -30,7 +30,7 @@ type Device struct {
|
||||
// will become the actual state; Up can fail.
|
||||
// The device can also change state multiple times between time of check and time of use.
|
||||
// Unsynchronized uses of state must therefore be advisory/best-effort only.
|
||||
state uint32 // actually a deviceState, but typed uint32 for convenience
|
||||
state atomic.Uint32 // actually a deviceState, but typed uint32 for convenience
|
||||
// stopping blocks until all inputs to Device have been closed.
|
||||
stopping sync.WaitGroup
|
||||
// mu protects state changes.
|
||||
@@ -58,9 +58,8 @@ type Device struct {
|
||||
keyMap map[NoisePublicKey]*Peer
|
||||
}
|
||||
|
||||
// Keep this 8-byte aligned
|
||||
rate struct {
|
||||
underLoadUntil int64
|
||||
underLoadUntil atomic.Int64
|
||||
limiter ratelimiter.Ratelimiter
|
||||
}
|
||||
|
||||
@@ -82,7 +81,7 @@ type Device struct {
|
||||
|
||||
tun struct {
|
||||
device tun.Device
|
||||
mtu int32
|
||||
mtu atomic.Int32
|
||||
}
|
||||
|
||||
ipcMutex sync.RWMutex
|
||||
@@ -94,10 +93,9 @@ type Device struct {
|
||||
// There are three states: down, up, closed.
|
||||
// Transitions:
|
||||
//
|
||||
// down -----+
|
||||
// ↑↓ ↓
|
||||
// up -> closed
|
||||
//
|
||||
// down -----+
|
||||
// ↑↓ ↓
|
||||
// up -> closed
|
||||
type deviceState uint32
|
||||
|
||||
//go:generate go run golang.org/x/tools/cmd/stringer -type deviceState -trimprefix=deviceState
|
||||
@@ -110,7 +108,7 @@ const (
|
||||
// deviceState returns device.state.state as a deviceState
|
||||
// See those docs for how to interpret this value.
|
||||
func (device *Device) deviceState() deviceState {
|
||||
return deviceState(atomic.LoadUint32(&device.state.state))
|
||||
return deviceState(device.state.state.Load())
|
||||
}
|
||||
|
||||
// isClosed reports whether the device is closed (or is closing).
|
||||
@@ -149,14 +147,14 @@ func (device *Device) changeState(want deviceState) (err error) {
|
||||
case old:
|
||||
return nil
|
||||
case deviceStateUp:
|
||||
atomic.StoreUint32(&device.state.state, uint32(deviceStateUp))
|
||||
device.state.state.Store(uint32(deviceStateUp))
|
||||
err = device.upLocked()
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
fallthrough // up failed; bring the device all the way back down
|
||||
case deviceStateDown:
|
||||
atomic.StoreUint32(&device.state.state, uint32(deviceStateDown))
|
||||
device.state.state.Store(uint32(deviceStateDown))
|
||||
errDown := device.downLocked()
|
||||
if err == nil {
|
||||
err = errDown
|
||||
@@ -182,7 +180,7 @@ func (device *Device) upLocked() error {
|
||||
device.peers.RLock()
|
||||
for _, peer := range device.peers.keyMap {
|
||||
peer.Start()
|
||||
if atomic.LoadUint32(&peer.persistentKeepaliveInterval) > 0 {
|
||||
if peer.persistentKeepaliveInterval.Load() > 0 {
|
||||
peer.SendKeepalive()
|
||||
}
|
||||
}
|
||||
@@ -219,11 +217,11 @@ func (device *Device) IsUnderLoad() bool {
|
||||
now := time.Now()
|
||||
underLoad := len(device.queue.handshake.c) >= QueueHandshakeSize/8
|
||||
if underLoad {
|
||||
atomic.StoreInt64(&device.rate.underLoadUntil, now.Add(UnderLoadAfterTime).UnixNano())
|
||||
device.rate.underLoadUntil.Store(now.Add(UnderLoadAfterTime).UnixNano())
|
||||
return true
|
||||
}
|
||||
// check if recently under load
|
||||
return atomic.LoadInt64(&device.rate.underLoadUntil) > now.UnixNano()
|
||||
return device.rate.underLoadUntil.Load() > now.UnixNano()
|
||||
}
|
||||
|
||||
func (device *Device) SetPrivateKey(sk NoisePrivateKey) error {
|
||||
@@ -283,7 +281,7 @@ func (device *Device) SetPrivateKey(sk NoisePrivateKey) error {
|
||||
|
||||
func NewDevice(tunDevice tun.Device, bind conn.Bind, logger *Logger) *Device {
|
||||
device := new(Device)
|
||||
device.state.state = uint32(deviceStateDown)
|
||||
device.state.state.Store(uint32(deviceStateDown))
|
||||
device.closed = make(chan struct{})
|
||||
device.log = logger
|
||||
device.net.bind = bind
|
||||
@@ -293,7 +291,7 @@ func NewDevice(tunDevice tun.Device, bind conn.Bind, logger *Logger) *Device {
|
||||
device.log.Errorf("Trouble determining MTU, assuming default: %v", err)
|
||||
mtu = DefaultMTU
|
||||
}
|
||||
device.tun.mtu = int32(mtu)
|
||||
device.tun.mtu.Store(int32(mtu))
|
||||
device.peers.keyMap = make(map[NoisePublicKey]*Peer)
|
||||
device.rate.limiter.Init()
|
||||
device.indexTable.Init()
|
||||
@@ -359,7 +357,7 @@ func (device *Device) Close() {
|
||||
if device.isClosed() {
|
||||
return
|
||||
}
|
||||
atomic.StoreUint32(&device.state.state, uint32(deviceStateClosed))
|
||||
device.state.state.Store(uint32(deviceStateClosed))
|
||||
device.log.Verbosef("Device closing")
|
||||
|
||||
device.tun.device.Close()
|
||||
|
Reference in New Issue
Block a user