graphics: Make verticesBackend concurrent safe

Fixes #789
This commit is contained in:
Hajime Hoshi 2019-01-19 02:06:48 +09:00
parent e829e650f2
commit 78ed824351

View File

@ -14,6 +14,10 @@
package graphics
import (
"sync"
)
var (
theVerticesBackend = &verticesBackend{}
)
@ -21,6 +25,7 @@ var (
type verticesBackend struct {
backend []float32
head int
m sync.Mutex
}
const (
@ -34,6 +39,8 @@ func (v *verticesBackend) slice(n int) []float32 {
panic("not reached")
}
v.m.Lock()
need := n * VertexFloatNum
if v.head+need > len(v.backend) {
v.backend = nil
@ -46,6 +53,8 @@ func (v *verticesBackend) slice(n int) []float32 {
s := v.backend[v.head : v.head+need]
v.head += need
v.m.Unlock()
return s
}