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