Skip to content

Commit

Permalink
Use reader/writer locks to reduce contention for key parsing and seri…
Browse files Browse the repository at this point in the history
…alization.

PiperOrigin-RevId: 556014241
Change-Id: Ief34885c2901932eb06c94b535b91c774c6bcc49
  • Loading branch information
ise-crypto authored and copybara-github committed Aug 11, 2023
1 parent 6c659c4 commit cee0aba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 6 additions & 6 deletions tink/internal/mutable_serialization_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ MutableSerializationRegistry& MutableSerializationRegistry::GlobalInstance() {

util::Status MutableSerializationRegistry::RegisterParametersParser(
ParametersParser* parser) {
absl::MutexLock lock(&registry_mutex_);
absl::WriterMutexLock lock(&registry_mutex_);
SerializationRegistry::Builder builder(registry_);
util::Status status = builder.RegisterParametersParser(parser);
if (!status.ok()) return status;
Expand All @@ -58,7 +58,7 @@ util::Status MutableSerializationRegistry::RegisterParametersParser(

util::Status MutableSerializationRegistry::RegisterParametersSerializer(
ParametersSerializer* serializer) {
absl::MutexLock lock(&registry_mutex_);
absl::WriterMutexLock lock(&registry_mutex_);
SerializationRegistry::Builder builder(registry_);
util::Status status = builder.RegisterParametersSerializer(serializer);
if (!status.ok()) return status;
Expand All @@ -68,7 +68,7 @@ util::Status MutableSerializationRegistry::RegisterParametersSerializer(

util::Status MutableSerializationRegistry::RegisterKeyParser(
KeyParser* parser) {
absl::MutexLock lock(&registry_mutex_);
absl::WriterMutexLock lock(&registry_mutex_);
SerializationRegistry::Builder builder(registry_);
util::Status status = builder.RegisterKeyParser(parser);
if (!status.ok()) return status;
Expand All @@ -78,7 +78,7 @@ util::Status MutableSerializationRegistry::RegisterKeyParser(

util::Status MutableSerializationRegistry::RegisterKeySerializer(
KeySerializer* serializer) {
absl::MutexLock lock(&registry_mutex_);
absl::WriterMutexLock lock(&registry_mutex_);
SerializationRegistry::Builder builder(registry_);
util::Status status = builder.RegisterKeySerializer(serializer);
if (!status.ok()) return status;
Expand All @@ -89,14 +89,14 @@ util::Status MutableSerializationRegistry::RegisterKeySerializer(
util::StatusOr<std::unique_ptr<Parameters>>
MutableSerializationRegistry::ParseParameters(
const Serialization& serialization) {
absl::MutexLock lock(&registry_mutex_);
absl::ReaderMutexLock lock(&registry_mutex_);
return registry_.ParseParameters(serialization);
}

util::StatusOr<std::unique_ptr<Key>> MutableSerializationRegistry::ParseKey(
const Serialization& serialization,
absl::optional<SecretKeyAccessToken> token) {
absl::MutexLock lock(&registry_mutex_);
absl::ReaderMutexLock lock(&registry_mutex_);
return registry_.ParseKey(serialization, token);
}

Expand Down
8 changes: 5 additions & 3 deletions tink/internal/mutable_serialization_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MutableSerializationRegistry {
template <typename SerializationT>
util::StatusOr<std::unique_ptr<Serialization>> SerializeParameters(
const Parameters& parameters) ABSL_LOCKS_EXCLUDED(registry_mutex_) {
absl::MutexLock lock(&registry_mutex_);
absl::ReaderMutexLock lock(&registry_mutex_);
return registry_.SerializeParameters<SerializationT>(parameters);
}

Expand All @@ -95,18 +95,20 @@ class MutableSerializationRegistry {
util::StatusOr<std::unique_ptr<Serialization>> SerializeKey(
const Key& key, absl::optional<SecretKeyAccessToken> token)
ABSL_LOCKS_EXCLUDED(registry_mutex_) {
absl::MutexLock lock(&registry_mutex_);
absl::ReaderMutexLock lock(&registry_mutex_);
return registry_.SerializeKey<SerializationT>(key, token);
}

// Resets to a new empty registry.
void Reset() ABSL_LOCKS_EXCLUDED(registry_mutex_) {
absl::MutexLock lock(&registry_mutex_);
absl::WriterMutexLock lock(&registry_mutex_);
registry_ = SerializationRegistry();
}

private:
mutable absl::Mutex registry_mutex_;
// Simple wrappers around const methods of `registry_` may safely acquire a
// shared (reader) lock. Other calls require an exclusive (writer) lock.
SerializationRegistry registry_ ABSL_GUARDED_BY(registry_mutex_);
};

Expand Down

0 comments on commit cee0aba

Please sign in to comment.