From 88dd0224d5b9b1155b55635a7204846413ac876a Mon Sep 17 00:00:00 2001 From: Daniel Kang Date: Sun, 20 Jan 2019 06:33:22 -0800 Subject: [PATCH] use atomic.Load/Store to avoid data race --- runtime/vm.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/runtime/vm.go b/runtime/vm.go index f4254e8..1c35c6f 100644 --- a/runtime/vm.go +++ b/runtime/vm.go @@ -3,6 +3,7 @@ package runtime import ( "errors" "fmt" + "sync/atomic" "github.com/d5/tengo/compiler" "github.com/d5/tengo/compiler/token" @@ -38,7 +39,7 @@ type VM struct { curFrame *Frame curInsts []byte curIPLimit int - aborting bool + aborting int64 } // NewVM creates a VM. @@ -72,14 +73,14 @@ func NewVM(bytecode *compiler.Bytecode, globals []*objects.Object) *VM { // Abort aborts the execution. func (v *VM) Abort() { - v.aborting = true + atomic.StoreInt64(&v.aborting, 1) } // Run starts the execution. func (v *VM) Run() error { 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++ ip = v.curFrame.ip @@ -1120,7 +1121,7 @@ func (v *VM) Run() error { } // 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") }