Skip to content

Commit

Permalink
* Ensure synchronized code in Pointer gets skipped with "org.byt…
Browse files Browse the repository at this point in the history
…edeco.javacpp.nopointergc" (issue tensorflow/java#313)
  • Loading branch information
saudet committed May 5, 2021
1 parent 3968447 commit d788390
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Ensure `synchronized` code in `Pointer` gets skipped with "org.bytedeco.javacpp.nopointergc" ([issue tensorflow/java#313](https://github.com/tensorflow/java/issues/313))
* Add `protected Pointer.offsetAddress()` and use it for `getPointer()` instead of `position()`
* Fix potential infinite loop in `Parser` when processing `class`, `struct`, or `union` declarations
* Have `Parser` wrap the `erase()` methods of basic containers with iterators to allow removing from maps
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/org/bytedeco/javacpp/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ final void add() {
}

final void remove() {
if (prev == this && next == this) {
return;
}
synchronized (DeallocatorReference.class) {
if (prev == this && next == this) {
return;
}
if (prev == null) {
head = next;
} else {
Expand Down Expand Up @@ -678,12 +678,12 @@ protected <P extends Pointer> P deallocator(Deallocator deallocator) {
DeallocatorReference r = deallocator instanceof DeallocatorReference ?
(DeallocatorReference)deallocator : new DeallocatorReference(this, deallocator);
this.deallocator = r;
int count = 0;
long lastPhysicalBytes = maxPhysicalBytes > 0 ? physicalBytes() : 0;
synchronized (DeallocatorThread.class) {
if (referenceQueue != null) synchronized (DeallocatorThread.class) {
int count = 0;
long lastPhysicalBytes = maxPhysicalBytes > 0 ? physicalBytes() : 0;
try {
while (count++ < maxRetries && ((maxBytes > 0 && DeallocatorReference.totalBytes + r.bytes > maxBytes)
|| (maxPhysicalBytes > 0 && lastPhysicalBytes > maxPhysicalBytes)) && referenceQueue != null) {
|| (maxPhysicalBytes > 0 && lastPhysicalBytes > maxPhysicalBytes))) {
if (logger.isDebugEnabled()) {
logger.debug("Calling System.gc() and Pointer.trimMemory() in " + this);
}
Expand Down Expand Up @@ -715,18 +715,18 @@ protected <P extends Pointer> P deallocator(Deallocator deallocator) {
logger.debug("Registering " + this);
}
r.add();
}

Iterator<PointerScope> it = PointerScope.getScopeIterator();
if (it != null) {
while (it.hasNext()) {
try {
it.next().attach(this);
} catch (IllegalArgumentException e) {
// try the next scope down the stack
continue;
}
break;
Iterator<PointerScope> it = PointerScope.getScopeIterator();
if (it != null) {
while (it.hasNext()) {
try {
it.next().attach(this);
} catch (IllegalArgumentException e) {
// try the next scope down the stack
continue;
}
break;
}
}
}
Expand Down Expand Up @@ -900,7 +900,7 @@ public <P extends Pointer> P getPointer(Class<P> cls) {
return getPointer(cls, 0);
}

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

0 comments on commit d788390

Please sign in to comment.