Move keyframes info into another struct to prevent invalid data race warnings

This commit is contained in:
Zoe Roux 2024-03-24 23:53:25 +01:00
parent 411bbef65c
commit 5374666ac9
No known key found for this signature in database

View File

@ -15,14 +15,17 @@ type Keyframe struct {
Keyframes []float64 Keyframes []float64
CanTransmux bool CanTransmux bool
IsDone bool IsDone bool
mutex sync.RWMutex info *KeyframeInfo
ready sync.WaitGroup }
listeners []func(keyframes []float64) type KeyframeInfo struct {
mutex sync.RWMutex
ready sync.WaitGroup
listeners []func(keyframes []float64)
} }
func (kf *Keyframe) Get(idx int32) float64 { func (kf *Keyframe) Get(idx int32) float64 {
kf.mutex.RLock() kf.info.mutex.RLock()
defer kf.mutex.RUnlock() defer kf.info.mutex.RUnlock()
return kf.Keyframes[idx] return kf.Keyframes[idx]
} }
@ -30,8 +33,8 @@ func (kf *Keyframe) Slice(start int32, end int32) []float64 {
if end <= start { if end <= start {
return []float64{} return []float64{}
} }
kf.mutex.RLock() kf.info.mutex.RLock()
defer kf.mutex.RUnlock() defer kf.info.mutex.RUnlock()
ref := kf.Keyframes[start:end] ref := kf.Keyframes[start:end]
ret := make([]float64, end-start) ret := make([]float64, end-start)
copy(ret, ref) copy(ret, ref)
@ -39,24 +42,24 @@ func (kf *Keyframe) Slice(start int32, end int32) []float64 {
} }
func (kf *Keyframe) Length() (int32, bool) { func (kf *Keyframe) Length() (int32, bool) {
kf.mutex.RLock() kf.info.mutex.RLock()
defer kf.mutex.RUnlock() defer kf.info.mutex.RUnlock()
return int32(len(kf.Keyframes)), kf.IsDone return int32(len(kf.Keyframes)), kf.IsDone
} }
func (kf *Keyframe) add(values []float64) { func (kf *Keyframe) add(values []float64) {
kf.mutex.Lock() kf.info.mutex.Lock()
defer kf.mutex.Unlock() defer kf.info.mutex.Unlock()
kf.Keyframes = append(kf.Keyframes, values...) kf.Keyframes = append(kf.Keyframes, values...)
for _, listener := range kf.listeners { for _, listener := range kf.info.listeners {
listener(kf.Keyframes) listener(kf.Keyframes)
} }
} }
func (kf *Keyframe) AddListener(callback func(keyframes []float64)) { func (kf *Keyframe) AddListener(callback func(keyframes []float64)) {
kf.mutex.Lock() kf.info.mutex.Lock()
defer kf.mutex.Unlock() defer kf.info.mutex.Unlock()
kf.listeners = append(kf.listeners, callback) kf.info.listeners = append(kf.info.listeners, callback)
} }
var keyframes = NewCMap[string, *Keyframe]() var keyframes = NewCMap[string, *Keyframe]()
@ -66,13 +69,14 @@ func GetKeyframes(sha string, path string) *Keyframe {
kf := &Keyframe{ kf := &Keyframe{
Sha: sha, Sha: sha,
IsDone: false, IsDone: false,
info: &KeyframeInfo{},
} }
kf.ready.Add(1) kf.info.ready.Add(1)
go func() { go func() {
save_path := fmt.Sprintf("%s/%s/keyframes.json", Settings.Metadata, sha) save_path := fmt.Sprintf("%s/%s/keyframes.json", Settings.Metadata, sha)
if err := getSavedInfo(save_path, kf); err == nil { if err := getSavedInfo(save_path, kf); err == nil {
log.Printf("Using keyframes cache on filesystem for %s", path) log.Printf("Using keyframes cache on filesystem for %s", path)
kf.ready.Done() kf.info.ready.Done()
return return
} }
@ -83,7 +87,7 @@ func GetKeyframes(sha string, path string) *Keyframe {
}() }()
return kf return kf
}) })
ret.ready.Wait() ret.info.ready.Wait()
return ret return ret
} }
@ -154,7 +158,7 @@ func getKeyframes(path string, kf *Keyframe) error {
if len(ret) == max { if len(ret) == max {
kf.add(ret) kf.add(ret)
if done == 0 { if done == 0 {
kf.ready.Done() kf.info.ready.Done()
} else if done >= 500 { } else if done >= 500 {
max = 500 max = 500
} }
@ -165,7 +169,7 @@ func getKeyframes(path string, kf *Keyframe) error {
} }
kf.add(ret) kf.add(ret)
if done == 0 { if done == 0 {
kf.ready.Done() kf.info.ready.Done()
} }
kf.IsDone = true kf.IsDone = true
return nil return nil