Replacing hand rolled ion-sfu atomic with uber/atomic (#465)

* Replacing hand rolled ion-sfu atomic with uber/atomic

* Remove another hand rolled atomic
This commit is contained in:
Raja Subramanian
2022-02-25 11:57:09 +05:30
committed by GitHub
parent 2959eebca8
commit 5a4181b581
11 changed files with 90 additions and 150 deletions
+17 -16
View File
@@ -2,9 +2,10 @@ package sfu
import (
"sync"
"sync/atomic"
"time"
"go.uber.org/atomic"
"github.com/livekit/protocol/utils"
)
@@ -37,9 +38,9 @@ type StreamTracker struct {
onStatusChanged func(status StreamStatus)
paused atomicBool
countSinceLast uint32 // number of packets received since last check
generation atomicUint32
paused atomic.Bool
countSinceLast atomic.Uint32 // number of packets received since last check
generation atomic.Uint32
initMu sync.Mutex
initialized bool
@@ -102,7 +103,7 @@ func (s *StreamTracker) maybeSetStopped() {
func (s *StreamTracker) init() {
s.maybeSetActive()
go s.detectWorker(s.generation.get())
go s.detectWorker(s.generation.Load())
}
func (s *StreamTracker) Start() {
@@ -114,7 +115,7 @@ func (s *StreamTracker) Stop() {
}
// bump generation to trigger exit of worker
s.generation.add(1)
s.generation.Inc()
}
func (s *StreamTracker) Reset() {
@@ -123,9 +124,9 @@ func (s *StreamTracker) Reset() {
}
// bump generation to trigger exit of current worker
s.generation.add(1)
s.generation.Inc()
atomic.StoreUint32(&s.countSinceLast, 0)
s.countSinceLast.Store(0)
s.cycleCount = 0
s.initMu.Lock()
@@ -138,12 +139,12 @@ func (s *StreamTracker) Reset() {
}
func (s *StreamTracker) SetPaused(paused bool) {
s.paused.set(paused)
s.paused.Store(paused)
}
// Observe a packet that's received
func (s *StreamTracker) Observe(sn uint16) {
if s.paused.get() {
if s.paused.Load() {
return
}
@@ -154,7 +155,7 @@ func (s *StreamTracker) Observe(sn uint16) {
s.initMu.Unlock()
s.lastSN = sn
atomic.AddUint32(&s.countSinceLast, 1)
s.countSinceLast.Inc()
// declare stream active and start the detection worker
go s.init()
@@ -168,7 +169,7 @@ func (s *StreamTracker) Observe(sn uint16) {
return
}
s.lastSN = sn
atomic.AddUint32(&s.countSinceLast, 1)
s.countSinceLast.Inc()
}
func (s *StreamTracker) detectWorker(generation uint32) {
@@ -176,7 +177,7 @@ func (s *StreamTracker) detectWorker(generation uint32) {
for {
<-ticker.C
if generation != s.generation.get() {
if generation != s.generation.Load() {
return
}
@@ -185,11 +186,11 @@ func (s *StreamTracker) detectWorker(generation uint32) {
}
func (s *StreamTracker) detectChanges() {
if s.paused.get() {
if s.paused.Load() {
return
}
if atomic.LoadUint32(&s.countSinceLast) >= s.samplesRequired {
if s.countSinceLast.Load() >= s.samplesRequired {
s.cycleCount += 1
} else {
s.cycleCount = 0
@@ -203,5 +204,5 @@ func (s *StreamTracker) detectChanges() {
s.maybeSetActive()
}
atomic.StoreUint32(&s.countSinceLast, 0)
s.countSinceLast.Store(0)
}