Made QueueEntry.ReadSamples atomic
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2022-07-31 22:42:18 +02:00
parent 80330e5b0d
commit a7d06f5eea
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
2 changed files with 8 additions and 6 deletions

View file

@ -11,9 +11,10 @@ import (
type QueueIdentifier int type QueueIdentifier int
type QueueEntry struct { type QueueEntry struct {
Identifier QueueIdentifier Identifier QueueIdentifier
Source audio.Source Source audio.Source
ReadSamples int //TODO: convert ReadSamples to atomic.Uint64 once 1.19 releases
ReadSamples uint64
cancel chan bool cancel chan bool
StartCallback func(q *Queue, entry *QueueEntry) StartCallback func(q *Queue, entry *QueueEntry)
EndCallback func(q *Queue, entry *QueueEntry) EndCallback func(q *Queue, entry *QueueEntry)
@ -146,10 +147,10 @@ func queueLoopStart[T audio.AllowedSourceTypes](q *Queue) {
q.Remove(current.Identifier) q.Remove(current.Identifier)
break F1 break F1
} else { } else {
if current.StartCallback != nil && current.ReadSamples == 0 && len(block) > 0 { if current.StartCallback != nil && atomic.LoadUint64(&current.ReadSamples) == 0 && len(block) > 0 {
current.StartCallback(q, current) current.StartCallback(q, current)
} }
current.ReadSamples += len(block) / current.Source.GetChannels() atomic.AddUint64(&current.ReadSamples, uint64(len(block)/current.Source.GetChannels()))
q.output.(audio.TypedSource[T]).IngestNative(block, current.Source.GetBitDepth()) q.output.(audio.TypedSource[T]).IngestNative(block, current.Source.GetBitDepth())
} }
} }

View file

@ -6,6 +6,7 @@ import (
"git.gammaspectra.live/S.O.N.G/Kirika/test" "git.gammaspectra.live/S.O.N.G/Kirika/test"
"os" "os"
"path" "path"
"sync/atomic"
"testing" "testing"
) )
@ -42,7 +43,7 @@ func TestQueue(t *testing.T) {
q.AddTail(source, func(q *Queue, entry *QueueEntry) { q.AddTail(source, func(q *Queue, entry *QueueEntry) {
t.Logf("Started playback of %d %s\n", entry.Identifier, fullPath) t.Logf("Started playback of %d %s\n", entry.Identifier, fullPath)
}, func(q *Queue, entry *QueueEntry) { }, func(q *Queue, entry *QueueEntry) {
t.Logf("Finished playback of %d %s: output %d samples\n", entry.Identifier, fullPath, entry.ReadSamples) t.Logf("Finished playback of %d %s: output %d samples\n", entry.Identifier, fullPath, atomic.LoadUint64(&entry.ReadSamples))
}, func(q *Queue, entry *QueueEntry) { }, func(q *Queue, entry *QueueEntry) {
fp.Close() fp.Close()
if q.GetQueueSize() == 0 { if q.GetQueueSize() == 0 {