Skip to content

Commit

Permalink
Use ReentrantReadWriteLock
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg committed Oct 23, 2023
1 parent 153ed45 commit 8926439
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.StampedLock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -47,7 +49,9 @@ public final class DefaultSynchronousMetricStorage<T extends PointData, U extend
private final MetricDescriptor metricDescriptor;
private final AggregationTemporality aggregationTemporality;
private final Aggregator<T, U> aggregator;
private final StampedLock sl = new StampedLock();
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private final Lock readLock = readWriteLock.readLock();
private final Lock writeLock = readWriteLock.writeLock();
private ConcurrentHashMap<Attributes, AggregatorHandle<T, U>> aggregatorHandles =
new ConcurrentHashMap<>();
private final AttributesProcessor attributesProcessor;
Expand Down Expand Up @@ -85,12 +89,12 @@ Queue<AggregatorHandle<T, U>> getAggregatorHandlePool() {

@Override
public void recordLong(long value, Attributes attributes, Context context) {
long stamp = sl.readLock();
readLock.lock();
try {
AggregatorHandle<T, U> handle = getAggregatorHandle(attributes, context);
handle.recordLong(value, attributes, context);
} finally {
sl.unlockRead(stamp);
readLock.unlock();
}
}

Expand All @@ -106,12 +110,12 @@ public void recordDouble(double value, Attributes attributes, Context context) {
+ ". Dropping measurement.");
return;
}
long stamp = sl.readLock();
readLock.lock();
try {
AggregatorHandle<T, U> handle = getAggregatorHandle(attributes, context);
handle.recordDouble(value, attributes, context);
} finally {
sl.unlockRead(stamp);
readLock.unlock();
}
}

Expand Down Expand Up @@ -160,12 +164,12 @@ public MetricData collect(

ConcurrentHashMap<Attributes, AggregatorHandle<T, U>> aggregatorHandles;
if (reset) {
long stamp = sl.writeLock();
writeLock.lock();
try {
aggregatorHandles = this.aggregatorHandles;
this.aggregatorHandles = new ConcurrentHashMap<>();
} finally {
sl.unlockWrite(stamp);
writeLock.unlock();
}
} else {
aggregatorHandles = this.aggregatorHandles;
Expand Down

0 comments on commit 8926439

Please sign in to comment.