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

ThreadLocalBufferManager replace synchronized with ReentrantLock #1252

Merged
Merged
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 @@ -5,6 +5,7 @@

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

/**
* For issue [jackson-core#400] We keep a separate Set of all SoftReferences to BufferRecyclers
Expand All @@ -23,7 +24,7 @@ class ThreadLocalBufferManager
* A lock to make sure releaseBuffers is only executed by one thread at a time
* since it iterates over and modifies the allSoftBufRecyclers.
*/
private final Object RELEASE_LOCK = new Object();
private final ReentrantLock RELEASE_LOCK = new ReentrantLock();

/**
* A set of all SoftReferences to all BufferRecyclers to be able to release them on shutdown.
Expand Down Expand Up @@ -64,17 +65,20 @@ public static ThreadLocalBufferManager instance() {
* It will clear all bufRecyclers from the SoftRefs and release all SoftRefs itself from our set.
*/
public int releaseBuffers() {
synchronized (RELEASE_LOCK) {
int count = 0;
int count = 0;
RELEASE_LOCK.lock();
try {
// does this need to be in sync block too? Looping over Map definitely has to but...
removeSoftRefsClearedByGc(); // make sure the refQueue is empty
for (SoftReference<BufferRecycler> ref : _trackedRecyclers.keySet()) {
ref.clear(); // possibly already cleared by gc, nothing happens in that case
++count;
}
_trackedRecyclers.clear(); //release cleared SoftRefs
return count;
} finally {
RELEASE_LOCK.unlock();
}
return count;
}

public SoftReference<BufferRecycler> wrapAndTrack(BufferRecycler br) {
Expand Down