mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-03-14 10:21:28 +00:00
types/dynamicpb: switch atomicExtFiles to atomic.Uint64 type
(Go Protobuf currently requires Go 1.21.) This guarantees correct alignment on stack-allocated values, too. Change-Id: If713532c20326a7ee3f1dd5c77f49c1d52f6eb9a Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/641017 Reviewed-by: Chressie Himpel <chressie@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
ad89419853
commit
9acc8f2c65
@ -28,11 +28,7 @@ type extField struct {
|
||||
type Types struct {
|
||||
// atomicExtFiles is used with sync/atomic and hence must be the first word
|
||||
// of the struct to guarantee 64-bit alignment.
|
||||
//
|
||||
// TODO(stapelberg): once we only support Go 1.19 and newer, switch this
|
||||
// field to be of type atomic.Uint64 to guarantee alignment on
|
||||
// stack-allocated values, too.
|
||||
atomicExtFiles uint64
|
||||
atomicExtFiles atomic.Uint64
|
||||
extMu sync.Mutex
|
||||
|
||||
files *protoregistry.Files
|
||||
@ -90,7 +86,7 @@ func (t *Types) FindExtensionByName(name protoreflect.FullName) (protoreflect.Ex
|
||||
func (t *Types) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
|
||||
// Construct the extension number map lazily, since not every user will need it.
|
||||
// Update the map if new files are added to the registry.
|
||||
if atomic.LoadUint64(&t.atomicExtFiles) != uint64(t.files.NumFiles()) {
|
||||
if t.atomicExtFiles.Load() != uint64(t.files.NumFiles()) {
|
||||
t.updateExtensions()
|
||||
}
|
||||
xd := t.extensionsByMessage[extField{message, field}]
|
||||
@ -133,10 +129,10 @@ func (t *Types) FindMessageByURL(url string) (protoreflect.MessageType, error) {
|
||||
func (t *Types) updateExtensions() {
|
||||
t.extMu.Lock()
|
||||
defer t.extMu.Unlock()
|
||||
if atomic.LoadUint64(&t.atomicExtFiles) == uint64(t.files.NumFiles()) {
|
||||
if t.atomicExtFiles.Load() == uint64(t.files.NumFiles()) {
|
||||
return
|
||||
}
|
||||
defer atomic.StoreUint64(&t.atomicExtFiles, uint64(t.files.NumFiles()))
|
||||
defer t.atomicExtFiles.Store(uint64(t.files.NumFiles()))
|
||||
t.files.RangeFiles(func(fd protoreflect.FileDescriptor) bool {
|
||||
t.registerExtensions(fd.Extensions())
|
||||
t.registerExtensionsInMessages(fd.Messages())
|
||||
|
Loading…
x
Reference in New Issue
Block a user