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

Replace synchronized blocks with ReentrantLocks #26

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -81,27 +81,27 @@ public MapperConfiguratorBase(MAPPER mapper, Annotations[] defaultAnnotations)
/**********************************************************************
*/

public synchronized final void setMapper(MAPPER m) {
public final void setMapper(MAPPER m) {
_mapper = m;
}

public synchronized final void setAnnotationsToUse(Annotations[] annotationsToUse) {
public final void setAnnotationsToUse(Annotations[] annotationsToUse) {
_setAnnotations(mapper(), annotationsToUse);
}

public synchronized final void configure(DeserializationFeature f, boolean state) {
public final void configure(DeserializationFeature f, boolean state) {
mapper().configure(f, state);
}

public synchronized final void configure(SerializationFeature f, boolean state) {
public final void configure(SerializationFeature f, boolean state) {
mapper().configure(f, state);
}

public synchronized final void configure(JsonParser.Feature f, boolean state) {
public final void configure(JsonParser.Feature f, boolean state) {
mapper().configure(f, state);
}

public synchronized final void configure(JsonGenerator.Feature f, boolean state) {
public final void configure(JsonGenerator.Feature f, boolean state) {
mapper().configure(f, state);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.jakarta.rs.cbor;

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

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
Expand All @@ -19,6 +20,8 @@
public class CBORMapperConfigurator
extends MapperConfiguratorBase<CBORMapperConfigurator, ObjectMapper>
{
private final ReentrantLock _lock = new ReentrantLock();

public CBORMapperConfigurator(ObjectMapper mapper, Annotations[] defAnnotations)
{
super(mapper, defAnnotations);
Expand All @@ -28,17 +31,24 @@ public CBORMapperConfigurator(ObjectMapper mapper, Annotations[] defAnnotations)
* Method that locates, configures and returns {@link ObjectMapper} to use
*/
@Override
public synchronized ObjectMapper getConfiguredMapper() {
public ObjectMapper getConfiguredMapper() {
// important: should NOT call mapper(); needs to return null
// if no instance has been passed or constructed
return _mapper;
}

@Override
public synchronized ObjectMapper getDefaultMapper() {
public ObjectMapper getDefaultMapper() {
if (_defaultMapper == null) {
_defaultMapper = new ObjectMapper(new CBORFactory());
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_defaultMapper == null) {
_defaultMapper = new ObjectMapper(new CBORFactory());
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _defaultMapper;
}
Expand All @@ -58,8 +68,15 @@ public synchronized ObjectMapper getDefaultMapper() {
protected ObjectMapper mapper()
{
if (_mapper == null) {
_mapper = new ObjectMapper(new CBORFactory());
_setAnnotations(_mapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_mapper == null) {
_mapper = new ObjectMapper(new CBORFactory());
_setAnnotations(_mapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _mapper;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.jakarta.rs.json;

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

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
Expand All @@ -18,6 +19,8 @@
public class JsonMapperConfigurator
extends MapperConfiguratorBase<JsonMapperConfigurator, ObjectMapper>
{
private final ReentrantLock _lock = new ReentrantLock();

public JsonMapperConfigurator(ObjectMapper mapper, Annotations[] defAnnotations)
{
super(mapper, defAnnotations);
Expand All @@ -27,18 +30,25 @@ public JsonMapperConfigurator(ObjectMapper mapper, Annotations[] defAnnotations)
* Method that locates, configures and returns {@link ObjectMapper} to use
*/
@Override
public synchronized ObjectMapper getConfiguredMapper() {
public ObjectMapper getConfiguredMapper() {
/* important: should NOT call mapper(); needs to return null
* if no instance has been passed or constructed
*/
return _mapper;
}

@Override
public synchronized ObjectMapper getDefaultMapper() {
public ObjectMapper getDefaultMapper() {
if (_defaultMapper == null) {
_defaultMapper = new ObjectMapper();
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_defaultMapper == null) {
_defaultMapper = new ObjectMapper();
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _defaultMapper;
}
Expand All @@ -58,8 +68,15 @@ public synchronized ObjectMapper getDefaultMapper() {
protected ObjectMapper mapper()
{
if (_mapper == null) {
_mapper = new ObjectMapper();
_setAnnotations(_mapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_mapper == null) {
_mapper = new ObjectMapper();
_setAnnotations(_mapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _mapper;
}
Expand Down
2 changes: 2 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ PJ Fanning (@pjfanning)
* Contributed #12: Remove unnecessary synchronization from endpoint
reader/writer caches
(2.14.2)
* Contributed #26: Replace synchronized blocks with `ReentrantLock`s
(2.17.1)

Steven Schlansker (@stevenschlansker)

Expand Down
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Sub-modules:
=== Releases ===
------------------------------------------------------------------------

2.17.1 (not yet released)

#26: Replace synchronized blocks with `ReentrantLock`s
(contributed by @pjfanning)

2.17.0 (12-Mar-2024)

* Upgrade Woodstox to 6.6.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.jakarta.rs.smile;

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

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
Expand All @@ -20,6 +21,8 @@
public class SmileMapperConfigurator
extends MapperConfiguratorBase<SmileMapperConfigurator, ObjectMapper>
{
private final ReentrantLock _lock = new ReentrantLock();

public SmileMapperConfigurator(ObjectMapper mapper, Annotations[] defAnnotations) {
super(mapper, defAnnotations);
}
Expand All @@ -28,17 +31,24 @@ public SmileMapperConfigurator(ObjectMapper mapper, Annotations[] defAnnotations
* Method that locates, configures and returns {@link ObjectMapper} to use
*/
@Override
public synchronized ObjectMapper getConfiguredMapper() {
public ObjectMapper getConfiguredMapper() {
// important: should NOT call mapper(); needs to return null
// if no instance has been passed or constructed
return _mapper;
}

@Override
public synchronized ObjectMapper getDefaultMapper() {
public ObjectMapper getDefaultMapper() {
if (_defaultMapper == null) {
_defaultMapper = new SmileMapper();
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_defaultMapper == null) {
_defaultMapper = new SmileMapper();
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _defaultMapper;
}
Expand All @@ -58,8 +68,15 @@ public synchronized ObjectMapper getDefaultMapper() {
protected ObjectMapper mapper()
{
if (_mapper == null) {
_mapper = new SmileMapper();
_setAnnotations(_mapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_mapper == null) {
_mapper = new SmileMapper();
_setAnnotations(_mapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _mapper;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.jakarta.rs.xml;

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

import com.fasterxml.jackson.databind.*;

Expand All @@ -20,6 +21,8 @@
public class XMLMapperConfigurator
extends MapperConfiguratorBase<XMLMapperConfigurator, XmlMapper>
{
private final ReentrantLock _lock = new ReentrantLock();

/*
/**********************************************************
/* Construction
Expand All @@ -35,21 +38,28 @@ public XMLMapperConfigurator(XmlMapper mapper, Annotations[] defAnnotations)
* Method that locates, configures and returns {@link XmlMapper} to use
*/
@Override
public synchronized XmlMapper getConfiguredMapper() {
public XmlMapper getConfiguredMapper() {
/* important: should NOT call mapper(); needs to return null
* if no instance has been passed or constructed
*/
return _mapper;
}

@Override
public synchronized XmlMapper getDefaultMapper()
public XmlMapper getDefaultMapper()
{
if (_defaultMapper == null) {
// 10-Oct-2012, tatu: Better do things explicitly...
JacksonXmlModule module = getConfiguredModule();
_defaultMapper = (module == null) ? new XmlMapper() : new XmlMapper(module);
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_defaultMapper == null) {
// 10-Oct-2012, tatu: Better do things explicitly...
JacksonXmlModule module = getConfiguredModule();
_defaultMapper = (module == null) ? new XmlMapper() : new XmlMapper(module);
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _defaultMapper;
}
Expand All @@ -74,8 +84,15 @@ protected JacksonXmlModule getConfiguredModule()
protected XmlMapper mapper()
{
if (_mapper == null) {
_mapper = new XmlMapper();
_setAnnotations(_mapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_mapper == null) {
_mapper = new XmlMapper();
_setAnnotations(_mapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _mapper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector;

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

/**
* Helper class used to encapsulate details of configuring an
Expand All @@ -18,6 +19,9 @@
*/
public class YAMLMapperConfigurator
extends MapperConfiguratorBase<YAMLMapperConfigurator, YAMLMapper> {

private ReentrantLock _lock = new ReentrantLock();

/*
/**********************************************************
/* Construction
Expand All @@ -32,18 +36,25 @@ public YAMLMapperConfigurator(YAMLMapper mapper, Annotations[] defAnnotations) {
* Method that locates, configures and returns {@link YAMLMapper} to use
*/
@Override
public synchronized YAMLMapper getConfiguredMapper() {
public YAMLMapper getConfiguredMapper() {
/* important: should NOT call mapper(); needs to return null
* if no instance has been passed or constructed
*/
return _mapper;
}

@Override
public synchronized YAMLMapper getDefaultMapper() {
public YAMLMapper getDefaultMapper() {
if (_defaultMapper == null) {
_defaultMapper = new YAMLMapper(); //tarik: maybe there is better default config?
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_defaultMapper == null) {
_defaultMapper = new YAMLMapper();
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _defaultMapper;
}
Expand All @@ -62,8 +73,15 @@ public synchronized YAMLMapper getDefaultMapper() {
@Override
protected YAMLMapper mapper() {
if (_mapper == null) {
_mapper = new YAMLMapper();
_setAnnotations(_mapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_mapper == null) {
_mapper = new YAMLMapper();
_setAnnotations(_mapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _mapper;
}
Expand Down
Loading