audio/internal/cbackend: refactoring: remove unnecessary mutex locks

This commit is contained in:
Hajime Hoshi 2022-04-12 11:58:49 +09:00
parent 8f524b5c23
commit 3f0b3f9686

View File

@ -128,7 +128,7 @@ type playerImpl struct {
context *Context context *Context
src io.Reader src io.Reader
volume float64 volume float64
err atomicError err error
state playerState state playerState
tmpbuf []byte tmpbuf []byte
buf []byte buf []byte
@ -156,10 +156,10 @@ func (p *Player) Err() error {
} }
func (p *playerImpl) Err() error { func (p *playerImpl) Err() error {
if err := p.err.Load(); err != nil { p.m.Lock()
return err.(error) defer p.m.Unlock()
}
return nil return p.err
} }
func (p *Player) Play() { func (p *Player) Play() {
@ -204,7 +204,7 @@ func (p *playerImpl) ensureTmpBuf() []byte {
} }
func (p *playerImpl) playImpl() { func (p *playerImpl) playImpl() {
if p.err.Load() != nil { if p.err != nil {
return return
} }
if p.state != playerPaused { if p.state != playerPaused {
@ -328,14 +328,11 @@ func (p *playerImpl) closeImpl() error {
p.m.Lock() p.m.Lock()
if p.state == playerClosed { if p.state == playerClosed {
return nil return p.err
} }
p.state = playerClosed p.state = playerClosed
p.buf = nil p.buf = nil
if err := p.err.Load(); err != nil { return p.err
return err.(error)
}
return nil
} }
func (p *playerImpl) readBufferAndAdd(buf []float32) int { func (p *playerImpl) readBufferAndAdd(buf []float32) int {
@ -387,7 +384,7 @@ func (p *playerImpl) readSourceToBuffer() {
p.m.Lock() p.m.Lock()
defer p.m.Unlock() defer p.m.Unlock()
if p.err.Load() != nil { if p.err != nil {
return return
} }
if p.state == playerClosed { if p.state == playerClosed {
@ -414,25 +411,6 @@ func (p *playerImpl) readSourceToBuffer() {
} }
func (p *playerImpl) setErrorImpl(err error) { func (p *playerImpl) setErrorImpl(err error) {
p.err.TryStore(err) p.err = err
p.closeImpl() p.closeImpl()
} }
type atomicError struct {
err error
m sync.Mutex
}
func (a *atomicError) TryStore(err error) {
a.m.Lock()
defer a.m.Unlock()
if a.err == nil {
a.err = err
}
}
func (a *atomicError) Load() error {
a.m.Lock()
defer a.m.Unlock()
return a.err
}