Skip to content

Commit

Permalink
use ReentrantLocks instead of synchronized blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Mar 28, 2024
1 parent 47083ea commit 6871e10
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 38 deletions.
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.jaxrs.cbor;

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

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

/*
/**********************************************************
/* Construction
Expand All @@ -32,18 +35,25 @@ 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 @@ -63,8 +73,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.jaxrs.json;

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

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

/*
/**********************************************************
/* Construction
Expand All @@ -31,18 +34,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 @@ -62,8 +72,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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.jaxrs.smile;

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 SmileMapperConfigurator
extends MapperConfiguratorBase<SmileMapperConfigurator, ObjectMapper>
{
private final ReentrantLock _lock = new ReentrantLock();

/*
/**********************************************************
/* Construction
Expand All @@ -33,18 +36,25 @@ 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 ObjectMapper(new SmileFactory());
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_defaultMapper == null) {
_defaultMapper = new ObjectMapper(new SmileFactory());
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _defaultMapper;
}
Expand All @@ -64,8 +74,15 @@ public synchronized ObjectMapper getDefaultMapper() {
protected ObjectMapper mapper()
{
if (_mapper == null) {
_mapper = new ObjectMapper(new SmileFactory());
_setAnnotations(_mapper, _defaultAnnotationsToUse);
_lock.lock();
try {
if (_mapper == null) {
_mapper = new ObjectMapper(new SmileFactory());
_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.jaxrs.xml;

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

import com.fasterxml.jackson.databind.*;

Expand All @@ -20,6 +21,9 @@
public class XMLMapperConfigurator
extends MapperConfiguratorBase<XMLMapperConfigurator, XmlMapper>
{

private final ReentrantLock _lock = new ReentrantLock();

/*
/**********************************************************
/* Construction
Expand All @@ -35,21 +39,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 +85,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 @@ -8,6 +8,7 @@
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;

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

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

private final ReentrantLock _lock = new ReentrantLock();

/*
/**********************************************************
/* Construction
Expand All @@ -30,18 +34,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(); //tarik: maybe there is better default config?
_setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
}
} finally {
_lock.unlock();
}
}
return _defaultMapper;
}
Expand All @@ -60,8 +71,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

0 comments on commit 6871e10

Please sign in to comment.