Skip to content

Commit

Permalink
remove synchronized block
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Mar 28, 2024
1 parent 6224569 commit dcc1f0b
Showing 1 changed file with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.databind.deser;

import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.*;
Expand Down Expand Up @@ -52,6 +53,11 @@ public final class DeserializerCache
protected final HashMap<JavaType, JsonDeserializer<Object>> _incompleteDeserializers
= new HashMap<JavaType, JsonDeserializer<Object>>(8);

/**
* We hold an explicit lock while creating deserializers to avoid creating duplicates.
*/
private final ReentrantLock _incompleteDeserializersLock = new ReentrantLock();

/*
/**********************************************************
/* Life-cycle
Expand Down Expand Up @@ -246,12 +252,26 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
* limitations necessary to ensure that only completely initialized ones
* are visible and used.
*/
synchronized (_incompleteDeserializers) {
// Ok, then: could it be that due to a race condition, deserializer can now be found?
JsonDeserializer<Object> deser = _findCachedDeserializer(type);
if (deser != null) {
return deser;
if (type == null) {
throw new IllegalArgumentException("Null JavaType passed");
}
if (_hasCustomHandlers(type)) {
return null;
}
final boolean isCustom = _hasCustomHandlers(type);
JsonDeserializer<Object> deser = isCustom ? null : _cachedDeserializers.get(type);
if (deser != null) {
return deser;
}
_incompleteDeserializersLock.lock();
try {
if (!isCustom) {
deser = _cachedDeserializers.get(type);
if (deser != null) {
return deser;
}
}
// Ok, then: could it be that due to a race condition, deserializer can now be found?
int count = _incompleteDeserializers.size();
// Or perhaps being resolved right now?
if (count > 0) {
Expand All @@ -269,6 +289,8 @@ protected JsonDeserializer<Object> _createAndCacheValueDeserializer(Deserializat
_incompleteDeserializers.clear();
}
}
} finally {
_incompleteDeserializersLock.unlock();
}
}

Expand Down

0 comments on commit dcc1f0b

Please sign in to comment.