mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +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{}{},
|
||||
}
|
||||
|
||||
go c.loop()
|
||||
go c.loop(c.initCh)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
@ -224,14 +224,15 @@ func (c *Context) ping() {
|
||||
if c.initCh != nil {
|
||||
close(c.initCh)
|
||||
c.initCh = nil
|
||||
<-c.initedCh
|
||||
}
|
||||
<-c.initedCh
|
||||
|
||||
c.m.Lock()
|
||||
c.pingCount = 5
|
||||
c.m.Unlock()
|
||||
}
|
||||
|
||||
func (c *Context) loop() {
|
||||
func (c *Context) loop(initCh <-chan struct{}) {
|
||||
clock.RegisterPing(c.ping)
|
||||
|
||||
// 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
|
||||
// is unexpectable.
|
||||
// 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())
|
||||
if err != nil {
|
||||
@ -249,7 +250,6 @@ func (c *Context) loop() {
|
||||
defer p.Close()
|
||||
|
||||
close(c.initedCh)
|
||||
c.initedCh = nil
|
||||
|
||||
for {
|
||||
c.m.Lock()
|
||||
|
Loading…
Reference in New Issue
Block a user