use atomic.Load/Store to avoid data race
This commit is contained in:
parent
04bb0f5e62
commit
88dd0224d5
1 changed files with 5 additions and 4 deletions
|
@ -3,6 +3,7 @@ package runtime
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/d5/tengo/compiler"
|
"github.com/d5/tengo/compiler"
|
||||||
"github.com/d5/tengo/compiler/token"
|
"github.com/d5/tengo/compiler/token"
|
||||||
|
@ -38,7 +39,7 @@ type VM struct {
|
||||||
curFrame *Frame
|
curFrame *Frame
|
||||||
curInsts []byte
|
curInsts []byte
|
||||||
curIPLimit int
|
curIPLimit int
|
||||||
aborting bool
|
aborting int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewVM creates a VM.
|
// NewVM creates a VM.
|
||||||
|
@ -72,14 +73,14 @@ func NewVM(bytecode *compiler.Bytecode, globals []*objects.Object) *VM {
|
||||||
|
|
||||||
// Abort aborts the execution.
|
// Abort aborts the execution.
|
||||||
func (v *VM) Abort() {
|
func (v *VM) Abort() {
|
||||||
v.aborting = true
|
atomic.StoreInt64(&v.aborting, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run starts the execution.
|
// Run starts the execution.
|
||||||
func (v *VM) Run() error {
|
func (v *VM) Run() error {
|
||||||
var ip int
|
var ip int
|
||||||
|
|
||||||
for v.curFrame.ip < v.curIPLimit && !v.aborting {
|
for v.curFrame.ip < v.curIPLimit && (atomic.LoadInt64(&v.aborting) == 0) {
|
||||||
v.curFrame.ip++
|
v.curFrame.ip++
|
||||||
|
|
||||||
ip = v.curFrame.ip
|
ip = v.curFrame.ip
|
||||||
|
@ -1120,7 +1121,7 @@ func (v *VM) Run() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if stack still has some objects left
|
// check if stack still has some objects left
|
||||||
if v.sp > 0 && !v.aborting {
|
if v.sp > 0 && atomic.LoadInt64(&v.aborting) == 0 {
|
||||||
return fmt.Errorf("non empty stack after execution")
|
return fmt.Errorf("non empty stack after execution")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue