Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework synchronization in ProtobufMapper #484

Merged
merged 3 commits into from
Apr 7, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.locks.ReentrantLock;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.type.TypeReference;
Expand All @@ -21,6 +22,8 @@ public class ProtobufMapper extends ObjectMapper
{
private static final long serialVersionUID = 1L;

private final ReentrantLock _lock = new ReentrantLock();

/**
* Base implementation for "Vanilla" {@link ObjectMapper}, used with
* Protobuf backend.
Expand All @@ -42,7 +45,7 @@ public Builder(ProtobufMapper m) {
*
* @since 2.9
*/
protected DescriptorLoader _descriptorLoader;
protected volatile DescriptorLoader _descriptorLoader;

/*
/**********************************************************
Expand Down Expand Up @@ -192,11 +195,19 @@ public FileDescriptorSet loadDescriptorSet(InputStream src) throws IOException {
*
* @since 2.9
*/
public synchronized DescriptorLoader descriptorLoader() throws IOException
public DescriptorLoader descriptorLoader() throws IOException
{
DescriptorLoader l = _descriptorLoader;
if (l == null) {
_descriptorLoader = l = DescriptorLoader.construct(this);
_lock.lock();
try {
l = _descriptorLoader;
if (l == null) {
_descriptorLoader = l = DescriptorLoader.construct(this);
}
} finally {
_lock.unlock();
}
}
return l;
}
Expand Down