audio/internal/readerdriver: Reduce the mutex usages (Windows)

Closes #1652
This commit is contained in:
Hajime Hoshi 2021-05-28 21:00:12 +09:00
parent c2148dcc01
commit 100b42513e

View File

@ -18,6 +18,7 @@ import (
"io" "io"
"runtime" "runtime"
"sync" "sync"
"sync/atomic"
"unsafe" "unsafe"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
@ -97,32 +98,32 @@ type players struct {
context *context context *context
players map[*playerImpl]struct{} players map[*playerImpl]struct{}
buf []byte buf []byte
err error err atomic.Value
waveOut uintptr waveOut uintptr
headers []*header headers []*header
// cond protects only the member 'players'.
// The other members don't have to be protected.
cond *sync.Cond cond *sync.Cond
} }
func (p *players) setContext(context *context) { func (p *players) setContext(context *context) {
p.cond.L.Lock()
defer p.cond.L.Unlock()
p.context = context p.context = context
} }
func (p *players) add(player *playerImpl) error { func (p *players) add(player *playerImpl) error {
p.cond.L.Lock() if err := p.err.Load(); err != nil {
defer p.cond.L.Unlock() return err.(error)
if p.err != nil {
return p.err
} }
p.cond.L.Lock()
if p.players == nil { if p.players == nil {
p.players = map[*playerImpl]struct{}{} p.players = map[*playerImpl]struct{}{}
} }
p.players[player] = struct{}{} p.players[player] = struct{}{}
p.cond.L.Unlock()
p.cond.Signal() p.cond.Signal()
if p.waveOut != 0 { if p.waveOut != 0 {
@ -160,7 +161,8 @@ func (p *players) add(player *playerImpl) error {
p.headers = append(p.headers, h) p.headers = append(p.headers, h)
} }
if err := p.readAndWriteBuffers(); err != nil { // There should be only one players in total.
if err := p.readAndWriteBuffers([]*playerImpl{player}); err != nil {
return err return err
} }
@ -170,16 +172,14 @@ func (p *players) add(player *playerImpl) error {
} }
func (p *players) remove(player *playerImpl) error { func (p *players) remove(player *playerImpl) error {
p.cond.L.Lock() if err := p.err.Load(); err != nil {
defer p.cond.L.Unlock() return err.(error)
return p.removeImpl(player)
}
func (p *players) removeImpl(player *playerImpl) error {
if p.err != nil {
return p.err
} }
p.cond.L.Lock()
delete(p.players, player) delete(p.players, player)
p.cond.L.Unlock()
return nil return nil
} }
@ -206,28 +206,30 @@ func (p *players) shouldWait() bool {
} }
func (p *players) loop() { func (p *players) loop() {
var players []*playerImpl
for { for {
p.cond.L.Lock() p.cond.L.Lock()
for p.shouldWait() { for p.shouldWait() {
p.cond.Wait() p.cond.Wait()
} }
if p.waveOut == 0 {
p.cond.L.Unlock() players = players[:0]
return for pl := range p.players {
} players = append(players, pl)
if err := p.readAndWriteBuffers(); err != nil {
p.err = err
p.cond.L.Unlock()
break
} }
p.cond.L.Unlock() p.cond.L.Unlock()
if p.waveOut == 0 {
return
}
if err := p.readAndWriteBuffers(players); err != nil {
p.err.Store(err)
break
}
} }
} }
func (p *players) suspend() error { func (p *players) suspend() error {
p.cond.L.Lock()
defer p.cond.L.Unlock()
if p.waveOut == 0 { if p.waveOut == 0 {
return nil return nil
} }
@ -238,9 +240,6 @@ func (p *players) suspend() error {
} }
func (p *players) resume() error { func (p *players) resume() error {
p.cond.L.Lock()
defer p.cond.L.Unlock()
if p.waveOut == 0 { if p.waveOut == 0 {
return nil return nil
} }
@ -260,8 +259,8 @@ var waveOutOpenCallback = windows.NewCallbackCDecl(func(hwo, uMsg, dwInstance, d
return 0 return 0
}) })
func (p *players) readAndWriteBuffers() error { func (p *players) readAndWriteBuffers(players []*playerImpl) error {
if len(p.players) == 0 { if len(players) == 0 {
return nil return nil
} }
@ -283,7 +282,7 @@ func (p *players) readAndWriteBuffers() error {
// waveOutSetVolume is not used since it doesn't work correctly in some environments. // waveOutSetVolume is not used since it doesn't work correctly in some environments.
var volumes []float64 var volumes []float64
var bufs [][]byte var bufs [][]byte
for pl := range p.players { for _, pl := range players {
buf := make([]byte, n) buf := make([]byte, n)
n := pl.read(buf) n := pl.read(buf)
bufs = append(bufs, buf[:n]) bufs = append(bufs, buf[:n])