github.com/3JoB/go-json@v0.10.4/internal/decoder/compile_race.go (about)

     1  //go:build race
     2  // +build race
     3  
     4  package decoder
     5  
     6  import (
     7  	"sync"
     8  	"unsafe"
     9  
    10  	"github.com/3JoB/go-json/internal/runtime"
    11  )
    12  
    13  var decMu sync.RWMutex
    14  
    15  func CompileToGetDecoder(typ *runtime.Type) (Decoder, error) {
    16  	typeptr := uintptr(unsafe.Pointer(typ))
    17  	if typeptr > typeAddr.MaxTypeAddr {
    18  		return compileToGetDecoderSlowPath(typeptr, typ)
    19  	}
    20  
    21  	index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
    22  	decMu.RLock()
    23  	if dec := cachedDecoder[index]; dec != nil {
    24  		decMu.RUnlock()
    25  		return dec, nil
    26  	}
    27  	decMu.RUnlock()
    28  
    29  	dec, err := compileHead(typ, map[uintptr]Decoder{})
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	decMu.Lock()
    34  	cachedDecoder[index] = dec
    35  	decMu.Unlock()
    36  	return dec, nil
    37  }