From 5b3948f68cccdb13957cfd95baa985aa640311e7 Mon Sep 17 00:00:00 2001 From: WeebDataHoarder <57538841+WeebDataHoarder@users.noreply.github.com> Date: Mon, 5 Sep 2022 16:57:17 +0200 Subject: [PATCH] Made Source Locked atomic.Bool --- audio/source_float32.go | 13 ++++++++----- audio/source_int16.go | 13 ++++++++----- audio/source_int32.go | 13 ++++++++----- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/audio/source_float32.go b/audio/source_float32.go index 482bf1f..30b2e53 100644 --- a/audio/source_float32.go +++ b/audio/source_float32.go @@ -2,6 +2,7 @@ package audio import ( "git.gammaspectra.live/S.O.N.G/Kirika/vector" + "sync/atomic" ) type float32Source struct { @@ -9,7 +10,7 @@ type float32Source struct { SampleRate int Channels int Blocks chan []float32 - locked bool + locked atomic.Bool } func newFloat32Source(bitDepth, sampleRate, channels int) TypedSource[float32] { @@ -33,8 +34,10 @@ func (s *float32Source) GetBlocks() chan []float32 { if s.Locked() { return nil } - s.locked = true - return s.Blocks + if !s.locked.Swap(true) { + return s.Blocks + } + return nil } func (s *float32Source) Close() { @@ -54,11 +57,11 @@ func (s *float32Source) GetBitDepth() int { } func (s *float32Source) Locked() bool { - return s.locked + return s.locked.Load() } func (s *float32Source) Unlock() { - s.locked = false + s.locked.Store(false) } func (s *float32Source) SwapBlocks(blocks chan []float32) chan []float32 { diff --git a/audio/source_int16.go b/audio/source_int16.go index 85f8a0c..0cf4dd1 100644 --- a/audio/source_int16.go +++ b/audio/source_int16.go @@ -2,6 +2,7 @@ package audio import ( "git.gammaspectra.live/S.O.N.G/Kirika/vector" + "sync/atomic" ) type int16Source struct { @@ -9,7 +10,7 @@ type int16Source struct { SampleRate int Channels int Blocks chan []int16 - locked bool + locked atomic.Bool } func newInt16Source(bitDepth, sampleRate, channels int) TypedSource[int16] { @@ -36,8 +37,10 @@ func (s *int16Source) GetBlocks() chan []int16 { if s.Locked() { return nil } - s.locked = true - return s.Blocks + if !s.locked.Swap(true) { + return s.Blocks + } + return nil } func (s *int16Source) Close() { @@ -57,11 +60,11 @@ func (s *int16Source) GetBitDepth() int { } func (s *int16Source) Locked() bool { - return s.locked + return s.locked.Load() } func (s *int16Source) Unlock() { - s.locked = false + s.locked.Store(false) } func (s *int16Source) SwapBlocks(blocks chan []int16) chan []int16 { diff --git a/audio/source_int32.go b/audio/source_int32.go index 69f418c..d135df4 100644 --- a/audio/source_int32.go +++ b/audio/source_int32.go @@ -2,6 +2,7 @@ package audio import ( "git.gammaspectra.live/S.O.N.G/Kirika/vector" + "sync/atomic" ) type int32Source struct { @@ -9,7 +10,7 @@ type int32Source struct { SampleRate int Channels int Blocks chan []int32 - locked bool + locked atomic.Bool } func newInt32Source(bitDepth, sampleRate, channels int) TypedSource[int32] { @@ -36,8 +37,10 @@ func (s *int32Source) GetBlocks() chan []int32 { if s.Locked() { return nil } - s.locked = true - return s.Blocks + if !s.locked.Swap(true) { + return s.Blocks + } + return nil } func (s *int32Source) Close() { @@ -57,11 +60,11 @@ func (s *int32Source) GetBitDepth() int { } func (s *int32Source) Locked() bool { - return s.locked + return s.locked.Load() } func (s *int32Source) Unlock() { - s.locked = false + s.locked.Store(false) } func (s *int32Source) SwapBlocks(blocks chan []int32) chan []int32 {