go-json/internal/decoder/compile_race.go

37 lines
681 B
Go
Raw Normal View History

2021-02-04 12:00:08 +03:00
// +build race
2021-06-03 12:49:01 +03:00
package decoder
2021-02-04 12:00:08 +03:00
2021-02-09 15:37:18 +03:00
import (
"sync"
2021-02-10 19:15:31 +03:00
"unsafe"
2021-06-03 12:49:01 +03:00
"github.com/goccy/go-json/internal/runtime"
2021-02-09 15:37:18 +03:00
)
2021-02-04 12:00:08 +03:00
var decMu sync.RWMutex
2021-06-03 12:49:01 +03:00
func CompileToGetDecoder(typ *runtime.Type) (decoder, error) {
2021-02-10 19:15:31 +03:00
typeptr := uintptr(unsafe.Pointer(typ))
2021-06-03 12:57:41 +03:00
if typeptr > typeAddr.MaxTypeAddr {
return compileToGetDecoderSlowPath(typeptr, typ)
2021-02-04 12:00:08 +03:00
}
2021-06-03 12:57:41 +03:00
index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
2021-02-04 12:00:08 +03:00
decMu.RLock()
if dec := cachedDecoder[index]; dec != nil {
decMu.RUnlock()
return dec, nil
}
decMu.RUnlock()
2021-06-03 12:57:41 +03:00
dec, err := compileHead(typ, map[uintptr]decoder{})
2021-02-04 12:00:08 +03:00
if err != nil {
return nil, err
}
decMu.Lock()
cachedDecoder[index] = dec
decMu.Unlock()
return dec, nil
}