Skip to content

Commit

Permalink
Fix possible race in schedulePlayerSettingChange
Browse files Browse the repository at this point in the history
  • Loading branch information
yktoo committed Aug 13, 2020
1 parent 18cdc31 commit 8a5863f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/player/prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/yktoo/ymuse/internal/config"
"github.com/yktoo/ymuse/internal/generated"
"github.com/yktoo/ymuse/internal/util"
"sync"
"time"
)

Expand Down Expand Up @@ -65,6 +66,7 @@ type PrefsDialog struct {
queueColumns []queueCol
// Timer for delayed player setting change callback invocation
playerSettingChangeTimer *time.Timer
playerSettingChangeMutex sync.Mutex
// Callbacks
onQueueColumnsChanged func()
onPlayerSettingChanged func()
Expand Down Expand Up @@ -337,15 +339,18 @@ func (d *PrefsDialog) populateColumns() {

func (d *PrefsDialog) schedulePlayerSettingChange() {
// Cancel the currently scheduled callback, if any
d.playerSettingChangeMutex.Lock()
defer d.playerSettingChangeMutex.Unlock()
if d.playerSettingChangeTimer != nil {
d.playerSettingChangeTimer.Stop()
}

// Schedule a new callback
d.playerSettingChangeTimer = time.AfterFunc(time.Second, func() {
util.WhenIdle("onPlayerSettingChanged()", func() {
d.playerSettingChangeTimer = nil
d.onPlayerSettingChanged()
})
d.playerSettingChangeMutex.Lock()
d.playerSettingChangeTimer = nil
d.playerSettingChangeMutex.Unlock()
util.WhenIdle("onPlayerSettingChanged()", d.onPlayerSettingChanged)
})
}

Expand Down

0 comments on commit 8a5863f

Please sign in to comment.