go-json/decode_compile_race.go

32 lines
584 B
Go
Raw Normal View History

2021-02-04 12:00:08 +03:00
// +build race
package json
import "sync"
var decMu sync.RWMutex
func (d *Decoder) compileToGetDecoder(typeptr uintptr, typ *rtype) (decoder, error) {
if !existsCachedDecoder {
return d.compileToGetDecoderSlowPath(typeptr, typ)
}
index := typeptr - baseTypeAddr
decMu.RLock()
if dec := cachedDecoder[index]; dec != nil {
decMu.RUnlock()
return dec, nil
}
decMu.RUnlock()
d.structTypeToDecoder = map[uintptr]decoder{}
dec, err := d.compileHead(typ)
if err != nil {
return nil, err
}
decMu.Lock()
cachedDecoder[index] = dec
decMu.Unlock()
return dec, nil
}