Skip to content

Commit d512683

Browse files
Revert "Replace most usage of synchronized with ReentrantLock"
This reverts commit 2668e5f.
1 parent 43a2e8a commit d512683

File tree

7 files changed

+16
-86
lines changed

7 files changed

+16
-86
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.lang.reflect.Constructor;
55
import java.lang.reflect.InvocationTargetException;
66
import java.util.*;
7-
import java.util.concurrent.locks.ReentrantLock;
87

98
import com.fasterxml.jackson.annotation.*;
109

@@ -181,8 +180,6 @@ public abstract class BeanDeserializerBase
181180
*/
182181
protected transient HashMap<ClassKey, JsonDeserializer<Object>> _subDeserializers;
183182

184-
private final ReentrantLock _subDeserializersLock = new ReentrantLock();
185-
186183
/**
187184
* If one of properties has "unwrapped" value, we need separate
188185
* helper object
@@ -1888,11 +1885,8 @@ protected JsonDeserializer<Object> _findSubclassDeserializer(DeserializationCont
18881885
JsonDeserializer<Object> subDeser;
18891886

18901887
// First: maybe we have already created sub-type deserializer?
1891-
try {
1892-
_subDeserializersLock.lock();
1888+
synchronized (this) {
18931889
subDeser = (_subDeserializers == null) ? null : _subDeserializers.get(new ClassKey(bean.getClass()));
1894-
} finally {
1895-
_subDeserializersLock.unlock();
18961890
}
18971891
if (subDeser != null) {
18981892
return subDeser;
@@ -1908,14 +1902,11 @@ protected JsonDeserializer<Object> _findSubclassDeserializer(DeserializationCont
19081902
subDeser = ctxt.findRootValueDeserializer(type);
19091903
// Also, need to cache it
19101904
if (subDeser != null) {
1911-
try {
1912-
_subDeserializersLock.lock();
1905+
synchronized (this) {
19131906
if (_subDeserializers == null) {
19141907
_subDeserializers = new HashMap<ClassKey,JsonDeserializer<Object>>();;
19151908
}
19161909
_subDeserializers.put(new ClassKey(bean.getClass()), subDeser);
1917-
} finally {
1918-
_subDeserializersLock.unlock();
19191910
}
19201911
}
19211912
return subDeser;

src/main/java/com/fasterxml/jackson/databind/deser/std/DateDeserializers.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.lang.reflect.Constructor;
55
import java.text.*;
66
import java.util.*;
7-
import java.util.concurrent.locks.ReentrantLock;
87

98
import com.fasterxml.jackson.annotation.JsonFormat;
109

@@ -70,8 +69,6 @@ protected abstract static class DateBasedDeserializer<T>
7069
*/
7170
protected final DateFormat _customFormat;
7271

73-
private final ReentrantLock _customFormatLock = new ReentrantLock();
74-
7572
/**
7673
* Let's also keep format String for reference, to use for error messages
7774
*/
@@ -191,16 +188,13 @@ protected java.util.Date _parseDate(JsonParser p, DeserializationContext ctxt)
191188
}
192189
return null;
193190
}
194-
try {
195-
_customFormatLock.lock();
191+
synchronized (_customFormat) {
196192
try {
197193
return _customFormat.parse(str);
198194
} catch (ParseException e) {
199195
return (java.util.Date) ctxt.handleWeirdStringValue(handledType(), str,
200196
"expected format \"%s\"", _formatString);
201197
}
202-
} finally {
203-
_customFormatLock.unlock();
204198
}
205199
}
206200
}

src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.fasterxml.jackson.databind.util.CompactStringObjectMap;
2121
import com.fasterxml.jackson.databind.util.EnumResolver;
2222
import java.util.Optional;
23-
import java.util.concurrent.locks.ReentrantLock;
2423

2524
/**
2625
* Deserializer class that can deserialize instances of
@@ -55,8 +54,6 @@ public class EnumDeserializer
5554
*/
5655
protected volatile CompactStringObjectMap _lookupByToString;
5756

58-
private final ReentrantLock _lookupLock = new ReentrantLock();
59-
6057
protected final Boolean _caseInsensitive;
6158

6259
private Boolean _useDefaultValueForUnknownEnum;
@@ -475,16 +472,13 @@ protected Class<?> _enumClass() {
475472
protected CompactStringObjectMap _getToStringLookup(DeserializationContext ctxt) {
476473
CompactStringObjectMap lookup = _lookupByToString;
477474
if (lookup == null) {
478-
try {
479-
_lookupLock.lock();
475+
synchronized (this) {
480476
lookup = _lookupByToString;
481477
if (lookup == null) {
482478
lookup = EnumResolver.constructUsingToString(ctxt.getConfig(), _enumClass())
483479
.constructLookup();
484480
_lookupByToString = lookup;
485481
}
486-
} finally {
487-
_lookupLock.unlock();
488482
}
489483
}
490484
return lookup;

src/main/java/com/fasterxml/jackson/databind/deser/std/StdKeyDeserializer.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.net.URI;
99
import java.net.URL;
1010
import java.util.*;
11-
import java.util.concurrent.locks.ReentrantLock;
1211

1312
import com.fasterxml.jackson.core.JsonParser;
1413
import com.fasterxml.jackson.core.io.NumberInput;
@@ -391,8 +390,6 @@ final static class EnumKD extends StdKeyDeserializer
391390
*/
392391
protected final EnumResolver _byEnumNamingResolver;
393392

394-
private final ReentrantLock _resolverLock = new ReentrantLock();
395-
396393
protected final Enum<?> _enumDefaultValue;
397394

398395
protected EnumKD(EnumResolver er, AnnotatedMethod factory) {
@@ -475,16 +472,13 @@ private EnumResolver _getToStringResolver(DeserializationContext ctxt)
475472
{
476473
EnumResolver res = _byToStringResolver;
477474
if (res == null) {
478-
try {
479-
_resolverLock.lock();
475+
synchronized (this) {
480476
res = _byToStringResolver;
481477
if (res == null) {
482478
res = EnumResolver.constructUsingToString(ctxt.getConfig(),
483479
_byNameResolver.getEnumClass());
484480
_byToStringResolver = res;
485481
}
486-
} finally {
487-
_resolverLock.unlock();
488482
}
489483
}
490484
return res;
@@ -501,16 +495,13 @@ private EnumResolver _getToStringResolver(DeserializationContext ctxt)
501495
private EnumResolver _getIndexResolver(DeserializationContext ctxt) {
502496
EnumResolver res = _byIndexResolver;
503497
if (res == null) {
504-
try {
505-
_resolverLock.lock();
498+
synchronized (this) {
506499
res = _byIndexResolver;
507500
if (res == null) {
508501
res = EnumResolver.constructUsingIndex(ctxt.getConfig(),
509502
_byNameResolver.getEnumClass());
510503
_byIndexResolver = res;
511504
}
512-
} finally {
513-
_resolverLock.unlock();
514505
}
515506
}
516507
return res;

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeDeserializerBase.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.util.Map;
55
import java.util.concurrent.ConcurrentHashMap;
6-
import java.util.concurrent.locks.ReentrantLock;
76

87
import com.fasterxml.jackson.annotation.JsonTypeInfo;
98

@@ -43,8 +42,6 @@ public abstract class TypeDeserializerBase
4342
*/
4443
protected final JavaType _defaultImpl;
4544

46-
protected final ReentrantLock _defaultImplLock = new ReentrantLock();
47-
4845
/**
4946
* Name of type property used; needed for non-property versions too,
5047
* in cases where type id is to be exposed as part of JSON.
@@ -226,15 +223,12 @@ protected final JsonDeserializer<Object> _findDefaultImplDeserializer(Deserializ
226223
return NullifyingDeserializer.instance;
227224
}
228225

229-
try {
230-
_defaultImplLock.lock();
226+
synchronized (_defaultImpl) {
231227
if (_defaultImplDeserializer == null) {
232228
_defaultImplDeserializer = ctxt.findContextualValueDeserializer(
233229
_defaultImpl, _property);
234230
}
235231
return _defaultImplDeserializer;
236-
} finally {
237-
_defaultImplLock.unlock();
238232
}
239233
}
240234

src/main/java/com/fasterxml/jackson/databind/ser/SerializerCache.java

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fasterxml.jackson.databind.ser;
22

33
import java.util.concurrent.atomic.AtomicReference;
4-
import java.util.concurrent.locks.ReentrantLock;
54

65
import com.fasterxml.jackson.databind.*;
76
import com.fasterxml.jackson.databind.ser.impl.ReadOnlyClassToSerializerMap;
@@ -46,8 +45,6 @@ public final class SerializerCache
4645
*/
4746
private final LookupCache<TypeKey, JsonSerializer<Object>> _sharedMap;
4847

49-
private final ReentrantLock _sharedMapLock = new ReentrantLock();
50-
5148
/**
5249
* Most recent read-only instance, created from _sharedMap, if any.
5350
*/
@@ -110,41 +107,29 @@ public synchronized int size() {
110107
*/
111108
public JsonSerializer<Object> untypedValueSerializer(Class<?> type)
112109
{
113-
try {
114-
_sharedMapLock.lock();
110+
synchronized (this) {
115111
return _sharedMap.get(new TypeKey(type, false));
116-
} finally {
117-
_sharedMapLock.unlock();
118112
}
119113
}
120114

121115
public JsonSerializer<Object> untypedValueSerializer(JavaType type)
122116
{
123-
try {
124-
_sharedMapLock.lock();
117+
synchronized (this) {
125118
return _sharedMap.get(new TypeKey(type, false));
126-
} finally {
127-
_sharedMapLock.unlock();
128119
}
129120
}
130121

131122
public JsonSerializer<Object> typedValueSerializer(JavaType type)
132123
{
133-
try {
134-
_sharedMapLock.lock();
124+
synchronized (this) {
135125
return _sharedMap.get(new TypeKey(type, true));
136-
} finally {
137-
_sharedMapLock.unlock();
138126
}
139127
}
140128

141129
public JsonSerializer<Object> typedValueSerializer(Class<?> cls)
142130
{
143-
try {
144-
_sharedMapLock.lock();
131+
synchronized (this) {
145132
return _sharedMap.get(new TypeKey(cls, true));
146-
} finally {
147-
_sharedMapLock.unlock();
148133
}
149134
}
150135

@@ -161,36 +146,29 @@ public JsonSerializer<Object> typedValueSerializer(Class<?> cls)
161146
*/
162147
public void addTypedSerializer(JavaType type, JsonSerializer<Object> ser)
163148
{
164-
try {
165-
_sharedMapLock.lock();
149+
synchronized (this) {
166150
if (_sharedMap.put(new TypeKey(type, true), ser) == null) {
167151
// let's invalidate the read-only copy, too, to get it updated
168152
_readOnlyMap.set(null);
169153
}
170-
} finally {
171-
_sharedMapLock.unlock();
172154
}
173155
}
174156

175157
public void addTypedSerializer(Class<?> cls, JsonSerializer<Object> ser)
176158
{
177-
try {
178-
_sharedMapLock.lock();
159+
synchronized (this) {
179160
if (_sharedMap.put(new TypeKey(cls, true), ser) == null) {
180161
// let's invalidate the read-only copy, too, to get it updated
181162
_readOnlyMap.set(null);
182163
}
183-
} finally {
184-
_sharedMapLock.unlock();
185164
}
186165
}
187166

188167
public void addAndResolveNonTypedSerializer(Class<?> type, JsonSerializer<Object> ser,
189168
SerializerProvider provider)
190169
throws JsonMappingException
191170
{
192-
try {
193-
_sharedMapLock.lock();
171+
synchronized (this) {
194172
if (_sharedMap.put(new TypeKey(type, false), ser) == null) {
195173
_readOnlyMap.set(null);
196174
}
@@ -202,17 +180,14 @@ public void addAndResolveNonTypedSerializer(Class<?> type, JsonSerializer<Object
202180
if (ser instanceof ResolvableSerializer) {
203181
((ResolvableSerializer) ser).resolve(provider);
204182
}
205-
} finally {
206-
_sharedMapLock.unlock();
207183
}
208184
}
209185

210186
public void addAndResolveNonTypedSerializer(JavaType type, JsonSerializer<Object> ser,
211187
SerializerProvider provider)
212188
throws JsonMappingException
213189
{
214-
try {
215-
_sharedMapLock.lock();
190+
synchronized (this) {
216191
if (_sharedMap.put(new TypeKey(type, false), ser) == null) {
217192
_readOnlyMap.set(null);
218193
}
@@ -224,8 +199,6 @@ public void addAndResolveNonTypedSerializer(JavaType type, JsonSerializer<Object
224199
if (ser instanceof ResolvableSerializer) {
225200
((ResolvableSerializer) ser).resolve(provider);
226201
}
227-
} finally {
228-
_sharedMapLock.unlock();
229202
}
230203
}
231204

@@ -240,8 +213,7 @@ public void addAndResolveNonTypedSerializer(Class<?> rawType, JavaType fullType,
240213
SerializerProvider provider)
241214
throws JsonMappingException
242215
{
243-
try {
244-
_sharedMapLock.lock();
216+
synchronized (this) {
245217
Object ob1 = _sharedMap.put(new TypeKey(rawType, false), ser);
246218
Object ob2 = _sharedMap.put(new TypeKey(fullType, false), ser);
247219
if ((ob1 == null) || (ob2 == null)) {
@@ -250,8 +222,6 @@ public void addAndResolveNonTypedSerializer(Class<?> rawType, JavaType fullType,
250222
if (ser instanceof ResolvableSerializer) {
251223
((ResolvableSerializer) ser).resolve(provider);
252224
}
253-
} finally {
254-
_sharedMapLock.unlock();
255225
}
256226
}
257227

src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,10 +1758,6 @@ protected JavaType _fromVariable(ClassStack context, TypeVariable<?> var, TypeBi
17581758
//
17591759
// No good way to reproduce but since this should not be on critical path, let's add
17601760
// syncing as it seems potentially necessary.
1761-
//
1762-
// 15-Mar-2024, oddbjornkvalsund: Not replacing this usage of 'synchronized' with an explicit lock as elsewhere.
1763-
// Assuming java.lang.reflect.TypeVariable.getBounds() does not do IO or other calls causing the thread to
1764-
// park and potentially pin to the platform thread.
17651761
synchronized (var) {
17661762
bounds = var.getBounds();
17671763
}

0 commit comments

Comments
 (0)