Skip to content

Commit

Permalink
Now totalCount is volatile as totalBytes. Important: even though thei…
Browse files Browse the repository at this point in the history
…r values are changed together in a synchronized block, when reading their values might not be in sync. This has been done to favor speed over correctness.
  • Loading branch information
digiovinazzo committed Aug 1, 2020
1 parent d4149e0 commit 83a76d2
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/main/java/org/bytedeco/javacpp/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.nio.ByteOrder;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.bytedeco.javacpp.annotation.Name;
import org.bytedeco.javacpp.tools.Generator;
import org.bytedeco.javacpp.tools.PointerBufferPoolMXBean;
Expand Down Expand Up @@ -286,7 +285,7 @@ static class DeallocatorReference extends PhantomReference<Pointer> implements D
Deallocator deallocator;

static volatile long totalBytes = 0;
static AtomicLong totalCount = new AtomicLong();
static volatile long totalCount = 0;
long bytes;

AtomicInteger count;
Expand All @@ -300,7 +299,7 @@ final void add() {
next.prev = head = this;
}
totalBytes += bytes;
totalCount.incrementAndGet();
totalCount++;
}
}

Expand All @@ -319,7 +318,7 @@ final void remove() {
}
prev = next = this;
totalBytes -= bytes;
totalCount.decrementAndGet();
totalCount--;
}
}

Expand Down Expand Up @@ -538,7 +537,7 @@ public static long totalBytes() {

/** Returns {@link DeallocatorReference#totalCount}, current number of pointers tracked by deallocators. */
public static long totalCount() {
return DeallocatorReference.totalCount.get();
return DeallocatorReference.totalCount;
}

/** Returns {@link #maxPhysicalBytes}, the maximum amount of physical memory that should be used. */
Expand Down

0 comments on commit 83a76d2

Please sign in to comment.