audio/internal/readerdriver: Reduce mutext on Windows

The granularity of context switches on Windows is pretty big and
mutexes might not be efficent.
This commit is contained in:
Hajime Hoshi 2021-05-28 19:25:56 +09:00
parent 646f48ef09
commit dc2f5e296e

View File

@ -160,7 +160,7 @@ func (p *players) add(player *playerImpl) error {
p.headers = append(p.headers, h) p.headers = append(p.headers, h)
} }
if err := p.readAndWriteBuffersImpl(); err != nil { if err := p.readAndWriteBuffers(); err != nil {
return err return err
} }
@ -205,34 +205,25 @@ func (p *players) shouldWait() bool {
return true return true
} }
func (p *players) wait() bool {
p.cond.L.Lock()
defer p.cond.L.Unlock()
for p.shouldWait() {
p.cond.Wait()
}
return p.waveOut != 0
}
func (p *players) loop() { func (p *players) loop() {
for { for {
if !p.wait() { p.cond.L.Lock()
for p.shouldWait() {
p.cond.Wait()
}
if p.waveOut == 0 {
p.cond.L.Unlock()
return return
} }
if err := p.readAndWriteBuffers(); err != nil { if err := p.readAndWriteBuffers(); err != nil {
p.setError(err) p.err = err
p.cond.L.Unlock()
break break
} }
p.cond.L.Unlock()
} }
} }
func (p *players) setError(err error) {
p.cond.L.Lock()
defer p.cond.L.Unlock()
p.err = err
}
func (p *players) suspend() error { func (p *players) suspend() error {
p.cond.L.Lock() p.cond.L.Lock()
defer p.cond.L.Unlock() defer p.cond.L.Unlock()
@ -270,12 +261,6 @@ var waveOutOpenCallback = windows.NewCallbackCDecl(func(hwo, uMsg, dwInstance, d
}) })
func (p *players) readAndWriteBuffers() error { func (p *players) readAndWriteBuffers() error {
p.cond.L.Lock()
defer p.cond.L.Unlock()
return p.readAndWriteBuffersImpl()
}
func (p *players) readAndWriteBuffersImpl() error {
if len(p.players) == 0 { if len(p.players) == 0 {
return nil return nil
} }