Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove synchronization in Reference and unused methods in ReferenceQueue #20694

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions jcl/src/java.base/share/classes/java/lang/ref/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
import java.security.AccessController;
import java.security.PrivilegedAction;

import com.ibm.oti.vm.VM;

/*[IF JAVA_SPEC_VERSION >= 12]*/
import jdk.internal.access.JavaLangRefAccess;
import jdk.internal.access.SharedSecrets;
/*[ELSEIF JAVA_SPEC_VERSION >= 9] JAVA_SPEC_VERSION >= 12 */
import jdk.internal.misc.JavaLangRefAccess;
import jdk.internal.misc.SharedSecrets;
/*[ELSEIF Sidecar18-SE-OpenJ9] JAVA_SPEC_VERSION >= 129 */
/*[ELSEIF Sidecar18-SE-OpenJ9] JAVA_SPEC_VERSION >= 12 */
import sun.misc.JavaLangRefAccess;
import sun.misc.SharedSecrets;
/*[ENDIF] JAVA_SPEC_VERSION >= 12 */
Expand All @@ -60,7 +58,7 @@ public abstract sealed class Reference<T> extends Object permits PhantomReferenc

private T referent;
private ReferenceQueue queue;
private int state;
private volatile int state;

/* jdk.lang.ref.disableClearBeforeEnqueue property allow reverting to the old behavior(non clear before enqueue)
* defer initializing the immutable variable to avoid bootstrap error
Expand Down Expand Up @@ -181,9 +179,7 @@ public T get() {
@Deprecated(since="16")
/*[ENDIF] JAVA_SPEC_VERSION >= 16 */
public boolean isEnqueued () {
synchronized(this) {
return state == STATE_ENQUEUED;
}
return state == STATE_ENQUEUED;
}

/**
Expand All @@ -196,24 +192,19 @@ public boolean isEnqueued () {
@NotCheckpointSafe
/*[ENDIF] CRIU_SUPPORT */
boolean enqueueImpl() {
final ReferenceQueue tempQueue;
boolean result;
T tempReferent = referent;
synchronized(this) {
/* Static order for the following code (DO NOT CHANGE) */
tempQueue = queue;
final ReferenceQueue tempQueue = queue;
queue = null;
if (state == STATE_ENQUEUED || tempQueue == null) {
if ((null == tempQueue) || (STATE_ENQUEUED == state)) {
return false;
}
result = tempQueue.enqueue(this);
keithc-ca marked this conversation as resolved.
Show resolved Hide resolved
if (result) {
state = STATE_ENQUEUED;
if (null != tempReferent) {
reprocess();
}
tempQueue.enqueue(this);
if (null != tempReferent) {
keithc-ca marked this conversation as resolved.
Show resolved Hide resolved
reprocess();
}
return result;
return true;
}
}

Expand Down Expand Up @@ -252,13 +243,16 @@ void initReference (T r, ReferenceQueue q) {

/**
* Called when a Reference has been removed from its ReferenceQueue.
* Set the enqueued field to false.
*/
void dequeue() {
/*[PR 112508] not synchronized, so isEnqueued() could return wrong result */
synchronized(this) {
state = STATE_CLEARED;
}
state = STATE_CLEARED;
}

/**
* Called when a Reference has been added to its ReferenceQueue.
*/
void setEnqueued() {
state = STATE_ENQUEUED;
}

/*[IF JAVA_SPEC_VERSION >= 9]*/
Expand Down
43 changes: 8 additions & 35 deletions jcl/src/java.base/share/classes/java/lang/ref/ReferenceQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,14 @@ public Reference<? extends T> remove(long timeout) throws IllegalArgumentExcepti
*
* @param reference
* reference object to be enqueued.
* @return boolean
* true if reference is enqueued.
* false if reference failed to enqueue.
*/
boolean enqueue(Reference<? extends T> reference) {
void enqueue(Reference<? extends T> reference) {
keithc-ca marked this conversation as resolved.
Show resolved Hide resolved
/*[PR CMVC 96472] deadlock loading sun.misc.Cleaner */
/*[PR 102259] call Cleaner.clean(), do not enqueue */
if (reference instanceof Cleaner) {
reference.dequeue();
((Cleaner)reference).clean();
return true;
keithc-ca marked this conversation as resolved.
Show resolved Hide resolved
return;
}
/*[PR 125873] Improve reflection cache */
Class refClass = reference.getClass();
Expand All @@ -210,62 +207,38 @@ boolean enqueue(Reference<? extends T> reference) {
) {
reference.dequeue();
((Runnable)reference).run();
return true;
return;
}
synchronized(this) {
/*[PR CMVC 181985] Perf: zWAS ftprint regressed 6% Java7 vs 626FP1 -ReferenceQueue */
if ( references == null) {
if (references == null) {
references = new Reference[DEFAULT_QUEUE_SIZE];
} else if(!empty && head == tail) {
} else if (!empty && (head == tail)) {
/* Queue is full - grow */
int newQueueSize = (int)(references.length * 1.10);
Reference newQueue[] = new Reference[newQueueSize];
System.arraycopy(references, head, newQueue, 0, references.length - head);
if(tail > 0) {
if (tail > 0) {
System.arraycopy(references, 0, newQueue, references.length - head, tail);
}
head = 0;
tail = references.length;
references = newQueue;
}
references[tail++] = reference;
if(tail == references.length) {
if (tail == references.length) {
tail = 0;
}
empty = false;
reference.setEnqueued();
notifyAll();
}
return true;
}

void forEach(java.util.function.Consumer<? super Reference<? extends T>> consumer) {
}

/*[IF JAVA_SPEC_VERSION >= 19]*/
final boolean headIsNull() {
return empty;
}

final Reference<? extends T> poll0() {
return poll();
}

final Reference<? extends T> remove0(long timeout) throws IllegalArgumentException, InterruptedException {
return remove(timeout);
}

final Reference<? extends T> remove0() throws IllegalArgumentException, InterruptedException {
return remove(0L);
}

final boolean enqueue0(Reference reference) {
return enqueue(reference);
}

void signal() {}
void await() throws InterruptedException {}
void await(long timeout) throws InterruptedException {}

ReferenceQueue(int value) {
this();
}
babsingh marked this conversation as resolved.
Show resolved Hide resolved
Expand Down