Skip to content

Commit

Permalink
fix: use atomic for global counter used for parallel testing (gnolang…
Browse files Browse the repository at this point in the history
…#1932)

This allows `go test -race` to pass on`gnovm/pkg/gnolang` when run in
parallel mode with CPU > 1.
As we use lockless atomic instructions there should be no overhead.
  • Loading branch information
mvertes authored Apr 16, 2024
1 parent 54bd960 commit b63e5ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
12 changes: 5 additions & 7 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/big"
"reflect"
"sync/atomic"

"github.com/gnolang/gno/tm2/pkg/errors"
)
Expand Down Expand Up @@ -95,7 +96,8 @@ func PredefineFileSet(store Store, pn *PackageNode, fset *FileSet) {
// (like ConvertUntypedTo() for bigints and strings)
// are only called during the preprocessing stage.
// It is a counter because Preprocess() is recursive.
var preprocessing int
// As a global counter, use lockless atomic to support concurrency.
var preprocessing atomic.Int32

// Preprocess n whose parent block node is ctx. If any names
// are defined in another file, generally you must call
Expand All @@ -116,12 +118,8 @@ var preprocessing int
// - TODO document what it does.
func Preprocess(store Store, ctx BlockNode, n Node) Node {
// Increment preprocessing counter while preprocessing.
{
preprocessing += 1
defer func() {
preprocessing -= 1
}()
}
preprocessing.Add(1)
defer preprocessing.Add(-1)

if ctx == nil {
// Generally a ctx is required, but if not, it's ok to pass in nil.
Expand Down
6 changes: 3 additions & 3 deletions gnovm/pkg/gnolang/values_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,17 +939,17 @@ func ConvertUntypedTo(tv *TypedValue, t Type) {
case UntypedRuneType:
ConvertUntypedRuneTo(tv, t)
case UntypedBigintType:
if preprocessing == 0 {
if preprocessing.Load() == 0 {
panic("untyped Bigint conversion should not happen during interpretation")
}
ConvertUntypedBigintTo(tv, tv.V.(BigintValue), t)
case UntypedBigdecType:
if preprocessing == 0 {
if preprocessing.Load() == 0 {
panic("untyped Bigdec conversion should not happen during interpretation")
}
ConvertUntypedBigdecTo(tv, tv.V.(BigdecValue), t)
case UntypedStringType:
if preprocessing == 0 {
if preprocessing.Load() == 0 {
panic("untyped String conversion should not happen during interpretation")
}
if t.Kind() == StringKind {
Expand Down

0 comments on commit b63e5ed

Please sign in to comment.