Skip to content

Commit

Permalink
allow concurrent jukebox reads
Browse files Browse the repository at this point in the history
fixes #411
  • Loading branch information
sentriz committed Nov 18, 2023
1 parent 2edb1b8 commit 3d73a9f
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions jukebox/jukebox.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Jukebox struct {
conn *mpvipc.Connection
events <-chan *mpvipc.Event

mu sync.Mutex
mu sync.RWMutex
}

func New() *Jukebox {
Expand Down Expand Up @@ -101,7 +101,7 @@ func (j *Jukebox) Wait() error {
}

func (j *Jukebox) GetPlaylist() ([]string, error) {
defer lock(&j.mu)()
defer lockr(&j.mu)()

var playlist mpvPlaylist
if err := j.getDecode(&playlist, "playlist"); err != nil {
Expand Down Expand Up @@ -257,7 +257,7 @@ func (j *Jukebox) SetVolumePct(v int) error {
}

func (j *Jukebox) GetVolumePct() (float64, error) {
defer lock(&j.mu)()
defer lockr(&j.mu)()

var volume float64
if err := j.getDecode(&volume, "volume"); err != nil {
Expand All @@ -276,7 +276,7 @@ type Status struct {
}

func (j *Jukebox) GetStatus() (*Status, error) {
defer lock(&j.mu)()
defer lockr(&j.mu)()

var status Status
_ = j.getDecode(&status.Position, "time-pos") // property may not always be there
Expand Down Expand Up @@ -416,11 +416,16 @@ func filter[T comparable](items []T, f func(T) bool) ([]T, bool) {
return ret, found
}

func lock(mu *sync.Mutex) func() {
func lock(mu *sync.RWMutex) func() {
mu.Lock()
return mu.Unlock
}

func lockr(mu *sync.RWMutex) func() {
mu.RLock()
return mu.RUnlock
}

var mpvVersionExpr = regexp.MustCompile(`mpv\s(\d+)\.(\d+)\.(\d+)`)

func parseMPVVersion(version string) (major, minor, patch int) {
Expand Down

0 comments on commit 3d73a9f

Please sign in to comment.