aboutsummaryrefslogtreecommitdiff
path: root/device/peer.go
diff options
context:
space:
mode:
Diffstat (limited to 'device/peer.go')
-rw-r--r--device/peer.go25
1 files changed, 16 insertions, 9 deletions
diff --git a/device/peer.go b/device/peer.go
index 8a8224c..65581d5 100644
--- a/device/peer.go
+++ b/device/peer.go
@@ -19,20 +19,27 @@ const (
)
type Peer struct {
- isRunning AtomicBool
- sync.RWMutex // Mostly protects endpoint, but is generally taken whenever we modify peer
- keypairs Keypairs
- handshake Handshake
- device *Device
- endpoint Endpoint
- persistentKeepaliveInterval uint16
-
- // This must be 64-bit aligned, so make sure the above members come out to even alignment and pad accordingly
+ // These fields are accessed with atomic operations, which must be
+ // 64-bit aligned even on 32-bit platforms. Go guarantees that an
+ // allocated struct will be 64-bit aligned. So we place
+ // atomically-accessed fields up front, so that they can share in
+ // this alignment before smaller fields throw it off.
stats struct {
txBytes uint64 // bytes send to peer (endpoint)
rxBytes uint64 // bytes received from peer
lastHandshakeNano int64 // nano seconds since epoch
}
+ // This field is only 32 bits wide, but is still aligned to 64
+ // bits. Don't place other atomic fields after this one.
+ isRunning AtomicBool
+
+ // Mostly protects endpoint, but is generally taken whenever we modify peer
+ sync.RWMutex
+ keypairs Keypairs
+ handshake Handshake
+ device *Device
+ endpoint Endpoint
+ persistentKeepaliveInterval uint16
timers struct {
retransmitHandshake *Timer