pki/vendor/github.com/goccy/go-json/internal/encoder/compiler_race.go

46 lines
998 B
Go
Raw Normal View History

2022-11-02 17:49:03 +01:00
//go:build race
2021-08-30 20:04:15 +02:00
// +build race
package encoder
import (
"sync"
)
var setsMu sync.RWMutex
2022-11-02 17:49:03 +01:00
func CompileToGetCodeSet(ctx *RuntimeContext, typeptr uintptr) (*OpcodeSet, error) {
if typeptr > typeAddr.MaxTypeAddr || typeptr < typeAddr.BaseTypeAddr {
codeSet, err := compileToGetCodeSetSlowPath(typeptr)
if err != nil {
return nil, err
}
return getFilteredCodeSetIfNeeded(ctx, codeSet)
2021-08-30 20:04:15 +02:00
}
index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
setsMu.RLock()
if codeSet := cachedOpcodeSets[index]; codeSet != nil {
2022-11-02 17:49:03 +01:00
filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
if err != nil {
setsMu.RUnlock()
return nil, err
}
2021-08-30 20:04:15 +02:00
setsMu.RUnlock()
2022-11-02 17:49:03 +01:00
return filtered, nil
2021-08-30 20:04:15 +02:00
}
setsMu.RUnlock()
2022-11-02 17:49:03 +01:00
codeSet, err := newCompiler().compile(typeptr)
2021-08-30 20:04:15 +02:00
if err != nil {
return nil, err
}
2022-11-02 17:49:03 +01:00
filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
2021-08-30 20:04:15 +02:00
if err != nil {
return nil, err
}
setsMu.Lock()
cachedOpcodeSets[index] = codeSet
setsMu.Unlock()
2022-11-02 17:49:03 +01:00
return filtered, nil
2021-08-30 20:04:15 +02:00
}