From 8dfc4f8a6088c88327a21803e96883344087bead Mon Sep 17 00:00:00 2001 From: Martijn Vels Date: Wed, 22 Oct 2025 12:48:57 -0700 Subject: [PATCH] Internal cleanup PiperOrigin-RevId: 822702398 --- src/google/protobuf/descriptor.cc | 28 ++++++++++++++-------------- src/google/protobuf/descriptor.h | 7 ++++++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index 94c00f9513ef2..cf07dc46e0b49 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -1979,7 +1979,7 @@ Symbol DescriptorPool::Tables::FindByNameHelper(const DescriptorPool* pool, absl::string_view name) { if (pool->mutex_ != nullptr) { // Fast path: the Symbol is already cached. This is just a hash lookup. - absl::ReaderMutexLock lock(pool->mutex_); + ReaderMutexLock lock(pool->mutex_); if (known_bad_symbols_.empty() && known_bad_files_.empty()) { Symbol result = FindSymbol(name); if (!result.IsNull()) return result; @@ -1988,7 +1988,7 @@ Symbol DescriptorPool::Tables::FindByNameHelper(const DescriptorPool* pool, DescriptorPool::DeferredValidation deferred_validation(pool); Symbol result; { - absl::MutexLockMaybe lock(pool->mutex_); + MutexLockMaybe lock(pool->mutex_); if (pool->fallback_database_ != nullptr) { known_bad_symbols_.clear(); known_bad_files_.clear(); @@ -2165,7 +2165,7 @@ FileDescriptorTables::FindEnumValueByNumberCreatingIfUnknown( { // Must lock the pool because we will do allocations in the shared arena. - absl::MutexLockMaybe l2(pool->mutex_); + DescriptorPool::MutexLockMaybe l2(pool->mutex_); alloc.FinalizePlanning(tables); } EnumValueDescriptor* result = alloc.AllocateArray(1); @@ -2387,7 +2387,7 @@ DescriptorPool::DescriptorPool() DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database, ErrorCollector* error_collector) - : mutex_(new absl::Mutex), + : mutex_(new Mutex), fallback_database_(fallback_database), default_error_collector_(error_collector), underlay_(nullptr), @@ -2452,7 +2452,7 @@ bool DescriptorPool::IsReadyForCheckingDescriptorExtDecl( void DescriptorPool::ClearDirectInputFiles() { direct_input_files_.clear(); } bool DescriptorPool::InternalIsFileLoaded(absl::string_view filename) const { - absl::MutexLockMaybe lock(mutex_); + MutexLockMaybe lock(mutex_); return tables_->FindFile(filename) != nullptr; } @@ -2522,7 +2522,7 @@ void DescriptorPool::InternalAddGeneratedFile( // Therefore, when we parse one, we have to be very careful to avoid using // any descriptor-based operations, since this might cause infinite recursion // or deadlock. - absl::MutexLockMaybe lock(internal_generated_pool()->mutex_); + MutexLockMaybe lock(internal_generated_pool()->mutex_); ABSL_CHECK(GeneratedDatabase()->Add(encoded_file_descriptor, size)); } @@ -2538,7 +2538,7 @@ const FileDescriptor* DescriptorPool::FindFileByName( DeferredValidation deferred_validation(this); const FileDescriptor* result = nullptr; { - absl::MutexLockMaybe lock(mutex_); + MutexLockMaybe lock(mutex_); if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); @@ -2564,7 +2564,7 @@ const FileDescriptor* DescriptorPool::FindFileContainingSymbol( const FileDescriptor* file_result = nullptr; DeferredValidation deferred_validation(this); { - absl::MutexLockMaybe lock(mutex_); + MutexLockMaybe lock(mutex_); if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); @@ -2644,7 +2644,7 @@ const FieldDescriptor* DescriptorPool::FindExtensionByNumber( // A faster path to reduce lock contention in finding extensions, assuming // most extensions will be cache hit. if (mutex_ != nullptr) { - absl::ReaderMutexLock lock(mutex_); + ReaderMutexLock lock(mutex_); const FieldDescriptor* result = tables_->FindExtension(extendee, number); if (result != nullptr) { return result; @@ -2653,7 +2653,7 @@ const FieldDescriptor* DescriptorPool::FindExtensionByNumber( const FieldDescriptor* result = nullptr; DeferredValidation deferred_validation(this); { - absl::MutexLockMaybe lock(mutex_); + MutexLockMaybe lock(mutex_); if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); @@ -2728,7 +2728,7 @@ void DescriptorPool::FindAllExtensions( DeferredValidation deferred_validation(this); std::vector extensions; { - absl::MutexLockMaybe lock(mutex_); + MutexLockMaybe lock(mutex_); if (fallback_database_ != nullptr) { tables_->known_bad_symbols_.clear(); tables_->known_bad_files_.clear(); @@ -5309,7 +5309,7 @@ Symbol DescriptorBuilder::FindSymbolNotEnforcingDepsHelper( const DescriptorPool* pool, const absl::string_view name, bool build_it) { // If we are looking at an underlay, we must lock its mutex_, since we are // accessing the underlay's tables_ directly. - absl::MutexLockMaybe lock((pool == pool_) ? nullptr : pool->mutex_); + DescriptorPool::MutexLockMaybe lock((pool == pool_) ? nullptr : pool->mutex_); Symbol result = pool->tables_->FindSymbol(name); if (result.IsNull() && pool->underlay_ != nullptr) { @@ -5502,7 +5502,7 @@ static bool ValidateQualifiedName(absl::string_view name) { Symbol DescriptorPool::NewPlaceholder(absl::string_view name, PlaceholderType placeholder_type) const { - absl::MutexLockMaybe lock(mutex_); + MutexLockMaybe lock(mutex_); return NewPlaceholderWithMutexHeld(name, placeholder_type); } @@ -5634,7 +5634,7 @@ Symbol DescriptorPool::NewPlaceholderWithMutexHeld( FileDescriptor* DescriptorPool::NewPlaceholderFile( const absl::string_view name) const { - absl::MutexLockMaybe lock(mutex_); + MutexLockMaybe lock(mutex_); internal::FlatAllocator alloc; alloc.PlanArray(1); alloc.PlanArray(1); diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index f664951307900..0faca7960c070 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -2661,9 +2661,14 @@ class PROTOBUF_EXPORT DescriptorPool { std::unique_ptr>>(); #endif // SWIG + using Mutex = absl::Mutex; + using MutexLock = absl::MutexLock; + using MutexLockMaybe = absl::MutexLockMaybe; + using ReaderMutexLock = absl::ReaderMutexLock; + // If fallback_database_ is nullptr, this is nullptr. Otherwise, this is a // mutex which must be locked while accessing tables_. - absl::Mutex* mutex_; + Mutex* mutex_; // See constructor. DescriptorDatabase* fallback_database_;