Skip to content

Commit

Permalink
bug fix: compile error with Go 1.16 and older
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Mar 7, 2022
1 parent 86ba771 commit e4fba8b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
24 changes: 21 additions & 3 deletions audio/internal/cbackend/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"io"
"runtime"
"sync"
"sync/atomic"
)

type playerState int
Expand Down Expand Up @@ -129,7 +128,7 @@ type playerImpl struct {
context *Context
src io.Reader
volume float64
err atomic.Value
err atomicError
state playerState
tmpbuf []byte
buf []byte
Expand Down Expand Up @@ -395,6 +394,25 @@ func (p *playerImpl) readSourceToBuffer() {
}

func (p *playerImpl) setErrorImpl(err error) {
p.err.CompareAndSwap(nil, err)
p.err.TryStore(err)
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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hajimehoshi/bitmapfont/v2 v2.2.0
github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41
github.com/hajimehoshi/go-mp3 v0.3.2
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.7.0.20220307144353-ebc14f63b1c1
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.7.0.20220307150401-d142925f4f40
github.com/jakecoffman/cp v1.1.0
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240
github.com/jfreymuth/oggvorbis v1.0.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/hajimehoshi/go-mp3 v0.3.2 h1:xSYNE2F3lxtOu9BRjCWHHceg7S91IHfXfXp5+LYQ
github.com/hajimehoshi/go-mp3 v0.3.2/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1 h1:7cJz/zRQV4aJvMSSRqzN2TImoVVMpE0BCY4nrNJaDOM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.7.0.20220307144353-ebc14f63b1c1 h1:i3nUxJnazLDnglzCtwoNmqX88Nf5JmRbKzJeQ4gCsr4=
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.7.0.20220307144353-ebc14f63b1c1/go.mod h1:rUKQmwMkqmRxe+IAof9+tuYA2ofm8cAWXFmSfzDN8vQ=
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.7.0.20220307150401-d142925f4f40 h1:i1wPmufpfePzHxDJPiTX1tR2nvSVZ9JbUvKbTeVIzQ0=
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.7.0.20220307150401-d142925f4f40/go.mod h1:rUKQmwMkqmRxe+IAof9+tuYA2ofm8cAWXFmSfzDN8vQ=
github.com/jakecoffman/cp v1.1.0 h1:bhKvCNbAddYegYHSV5abG3G23vZdsISgqXa4X/lK8Oo=
github.com/jakecoffman/cp v1.1.0/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 h1:dy+DS31tGEGCsZzB45HmJJNHjur8GDgtRNX9U7HnSX4=
Expand Down
18 changes: 11 additions & 7 deletions internal/ui/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,27 @@ var theGlobalState = globalState{
// globalState represents a global state in this package.
// This is available even before the game loop starts.
type globalState struct {
err_ atomic.Value
err_ error
errM sync.Mutex

fpsMode_ int32
maxTPS_ int32
isScreenClearedEveryFrame_ int32
screenFilterEnabled_ int32
}

func (g *globalState) error() error {
err, ok := g.err_.Load().(error)
if !ok {
return nil
}
return err
g.errM.Lock()
defer g.errM.Unlock()
return g.err_
}

func (g *globalState) setError(err error) {
g.err_.CompareAndSwap(nil, err)
g.errM.Lock()
defer g.errM.Unlock()
if g.err_ == nil {
g.err_ = err
}
}

func (g *globalState) fpsMode() FPSModeType {
Expand Down

0 comments on commit e4fba8b

Please sign in to comment.