state make more threadsafe + close started chanel#39
state make more threadsafe + close started chanel#39tantra35 wants to merge 8 commits intojfreymuth:masterfrom
Conversation
|
Another PR #36 partialy make the same, it only maek state as sync.Atomic, but not close started chanell |
This reverts commit 58648fd.
|
I've merged a different PR for the data race on |
|
@jfreymuth Ok I wil try to explain:
func (p *PlaybackStream) Start() {
if p.state.CompareAndSwap(int32(idle), int32(running)) { // we go here only when succesfuly chnage state from idle to running, and run rest of code only in this case
p.c.c.Request(&proto.FlushPlaybackStream{StreamIndex: p.index}, nil)
p.err = nil
p.request <- int(p.createReply.BufferTargetLength)
p.underflow = false
p.c.c.Request(&proto.CorkPlaybackStream{StreamIndex: p.index, Corked: false}, nil)
<-p.started
}
}but start now looks like this func (p *PlaybackStream) Start() {
if p.state.is(idle) { // <------
p.c.c.Request(&proto.FlushPlaybackStream{StreamIndex: p.index}, nil)
p.state.set(running) // <--- between marked calls we have undefined behaviour in other goroutines, because state doesn't changes atomically
p.err = nil
p.request <- int(p.createReply.BufferTargetLength)
p.underflow = false
p.c.c.Request(&proto.CorkPlaybackStream{StreamIndex: p.index, Corked: false}, nil)
<-p.started
}
} |
|
I made a similar PR a while ago, but didn't make it ready yet. It has a similar approach to this PR. |
When i begin use this library i found 2 issues: