Skip to content

Commit

Permalink
use ReentrantLock in ThreadLocalBufferManager
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Mar 27, 2024
1 parent b8a132e commit 6884072
Showing 1 changed file with 8 additions and 4 deletions.
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

0 comments on commit 6884072

Please sign in to comment.