mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
audio: Bug fix: Race condition (#412)
This commit is contained in:
parent
eaadf68f32
commit
ba75592807
@ -208,7 +208,7 @@ func NewContext(sampleRate int) (*Context, error) {
|
|||||||
players: map[*Player]struct{}{},
|
players: map[*Player]struct{}{},
|
||||||
}
|
}
|
||||||
|
|
||||||
go c.loop()
|
go c.loop(c.initCh)
|
||||||
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
@ -224,14 +224,15 @@ func (c *Context) ping() {
|
|||||||
if c.initCh != nil {
|
if c.initCh != nil {
|
||||||
close(c.initCh)
|
close(c.initCh)
|
||||||
c.initCh = nil
|
c.initCh = nil
|
||||||
<-c.initedCh
|
|
||||||
}
|
}
|
||||||
|
<-c.initedCh
|
||||||
|
|
||||||
c.m.Lock()
|
c.m.Lock()
|
||||||
c.pingCount = 5
|
c.pingCount = 5
|
||||||
c.m.Unlock()
|
c.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) loop() {
|
func (c *Context) loop(initCh <-chan struct{}) {
|
||||||
clock.RegisterPing(c.ping)
|
clock.RegisterPing(c.ping)
|
||||||
|
|
||||||
// Initialize oto.Player lazily to enable calling NewContext in an 'init' function.
|
// Initialize oto.Player lazily to enable calling NewContext in an 'init' function.
|
||||||
@ -239,7 +240,7 @@ func (c *Context) loop() {
|
|||||||
// but if Ebiten is used for a shared library, the timing when init functions are called
|
// but if Ebiten is used for a shared library, the timing when init functions are called
|
||||||
// is unexpectable.
|
// is unexpectable.
|
||||||
// e.g. a variable for JVM on Android might not be set.
|
// e.g. a variable for JVM on Android might not be set.
|
||||||
<-c.initCh
|
<-initCh
|
||||||
|
|
||||||
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, c.bufferSize())
|
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, c.bufferSize())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -249,7 +250,6 @@ func (c *Context) loop() {
|
|||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
close(c.initedCh)
|
close(c.initedCh)
|
||||||
c.initedCh = nil
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
c.m.Lock()
|
c.m.Lock()
|
||||||
|
Loading…
Reference in New Issue
Block a user