Skip to content

Commit d788390

Browse files
committed
* Ensure synchronized code in Pointer gets skipped with "org.bytedeco.javacpp.nopointergc" (issue tensorflow/java#313)
1 parent 3968447 commit d788390

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
* Ensure `synchronized` code in `Pointer` gets skipped with "org.bytedeco.javacpp.nopointergc" ([issue tensorflow/java#313](https://github.com/tensorflow/java/issues/313))
23
* Add `protected Pointer.offsetAddress()` and use it for `getPointer()` instead of `position()`
34
* Fix potential infinite loop in `Parser` when processing `class`, `struct`, or `union` declarations
45
* Have `Parser` wrap the `erase()` methods of basic containers with iterators to allow removing from maps

src/main/java/org/bytedeco/javacpp/Pointer.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,10 @@ final void add() {
313313
}
314314

315315
final void remove() {
316+
if (prev == this && next == this) {
317+
return;
318+
}
316319
synchronized (DeallocatorReference.class) {
317-
if (prev == this && next == this) {
318-
return;
319-
}
320320
if (prev == null) {
321321
head = next;
322322
} else {
@@ -678,12 +678,12 @@ protected <P extends Pointer> P deallocator(Deallocator deallocator) {
678678
DeallocatorReference r = deallocator instanceof DeallocatorReference ?
679679
(DeallocatorReference)deallocator : new DeallocatorReference(this, deallocator);
680680
this.deallocator = r;
681-
int count = 0;
682-
long lastPhysicalBytes = maxPhysicalBytes > 0 ? physicalBytes() : 0;
683-
synchronized (DeallocatorThread.class) {
681+
if (referenceQueue != null) synchronized (DeallocatorThread.class) {
682+
int count = 0;
683+
long lastPhysicalBytes = maxPhysicalBytes > 0 ? physicalBytes() : 0;
684684
try {
685685
while (count++ < maxRetries && ((maxBytes > 0 && DeallocatorReference.totalBytes + r.bytes > maxBytes)
686-
|| (maxPhysicalBytes > 0 && lastPhysicalBytes > maxPhysicalBytes)) && referenceQueue != null) {
686+
|| (maxPhysicalBytes > 0 && lastPhysicalBytes > maxPhysicalBytes))) {
687687
if (logger.isDebugEnabled()) {
688688
logger.debug("Calling System.gc() and Pointer.trimMemory() in " + this);
689689
}
@@ -715,18 +715,18 @@ protected <P extends Pointer> P deallocator(Deallocator deallocator) {
715715
logger.debug("Registering " + this);
716716
}
717717
r.add();
718+
}
718719

719-
Iterator<PointerScope> it = PointerScope.getScopeIterator();
720-
if (it != null) {
721-
while (it.hasNext()) {
722-
try {
723-
it.next().attach(this);
724-
} catch (IllegalArgumentException e) {
725-
// try the next scope down the stack
726-
continue;
727-
}
728-
break;
720+
Iterator<PointerScope> it = PointerScope.getScopeIterator();
721+
if (it != null) {
722+
while (it.hasNext()) {
723+
try {
724+
it.next().attach(this);
725+
} catch (IllegalArgumentException e) {
726+
// try the next scope down the stack
727+
continue;
729728
}
729+
break;
730730
}
731731
}
732732
}
@@ -900,7 +900,7 @@ public <P extends Pointer> P getPointer(Class<P> cls) {
900900
return getPointer(cls, 0);
901901
}
902902

903-
/** Returns {@code new P(this).offset(i)}. Throws RuntimeException if constructor is missing. */
903+
/** Returns {@code new P(this).offsetAddress(i)}. Throws RuntimeException if constructor is missing. */
904904
public <P extends Pointer> P getPointer(Class<P> cls, long i) {
905905
try {
906906
return cls.getDeclaredConstructor(Pointer.class).newInstance(this).offsetAddress(i);

0 commit comments

Comments
 (0)