From 7658e1b60db047a1cae6b05cd297acca174b48b5 Mon Sep 17 00:00:00 2001 From: akarl10 Date: Sat, 17 Feb 2024 20:44:55 +0100 Subject: [PATCH] make 64bit atomics also align on 32bit (#2386) do this by simply putting them at the front of each struct see https://pkg.go.dev/sync/atomic#pkg-note-BUG fixes #2383 Co-authored-by: akarl10 --- pkg/rtc/participant.go | 5 ++++- pkg/rtc/room.go | 14 +++++++++----- pkg/rtc/uptrackmanager.go | 5 ++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index 962b90b1f..acf326311 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -137,6 +137,10 @@ type ParticipantParams struct { } type ParticipantImpl struct { + // utils.TimedVersion is a atomic. To be correctly aligned also on 32bit archs + // 64it atomics need to be at the front of a struct + timedVersion utils.TimedVersion + params ParticipantParams isClosed atomic.Bool @@ -200,7 +204,6 @@ type ParticipantImpl struct { dirty atomic.Bool version atomic.Uint32 - timedVersion utils.TimedVersion // callbacks & handlers onTrackPublished func(types.LocalParticipant, types.MediaTrack) diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index 622e5e62b..3b94a0cf9 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -80,6 +80,15 @@ type disconnectSignalOnResumeNoMessages struct { } type Room struct { + // atomics always need to be 64bit/8byte aligned + // on 32bit arch only the beginning of the struct + // starts at such a boundary. + // time the first participant joined the room + joinedAt atomic.Int64 + // time that the last participant left the room + leftAt atomic.Int64 + holds atomic.Int32 + lock sync.RWMutex protoRoom *livekit.Room @@ -109,11 +118,6 @@ type Room struct { batchedUpdates map[livekit.ParticipantIdentity]*participantUpdate batchedUpdatesMu sync.Mutex - // time the first participant joined the room - joinedAt atomic.Int64 - holds atomic.Int32 - // time that the last participant left the room - leftAt atomic.Int64 closed chan struct{} trailer []byte diff --git a/pkg/rtc/uptrackmanager.go b/pkg/rtc/uptrackmanager.go index 5cadfc267..2e81324ab 100644 --- a/pkg/rtc/uptrackmanager.go +++ b/pkg/rtc/uptrackmanager.go @@ -37,6 +37,10 @@ type UpTrackManagerParams struct { // UpTrackManager manages all uptracks from a participant type UpTrackManager struct { + // utils.TimedVersion is a atomic. To be correctly aligned also on 32bit archs + // 64it atomics need to be at the front of a struct + subscriptionPermissionVersion utils.TimedVersion + params UpTrackManagerParams closed bool @@ -44,7 +48,6 @@ type UpTrackManager struct { // publishedTracks that participant is publishing publishedTracks map[livekit.TrackID]types.MediaTrack subscriptionPermission *livekit.SubscriptionPermission - subscriptionPermissionVersion utils.TimedVersion // subscriber permission for published tracks subscriberPermissions map[livekit.ParticipantIdentity]*livekit.TrackPermission // subscriberIdentity => *livekit.TrackPermission