forked from eclipse-equinox/equinox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get loader-classname lock for calls to findLoadedClass
This is related to Open J9 issue eclipse-openj9/openj9#20444 There is a concern that the initial call to findLoadedClass by Equinox is not protected by the same loader-classname lock as the later calls to findLoadedClass/defineClass calls. There are concerns that the unprotected findLoadedClass call could catch the JVM in an invalid state if there is another thread in the middle of defining the class. This changes replaces the clunky way of doing loader-classname locks with a more efficient strategy. This way we can use this lock around the initial call to findLoadedClass without too much concern over the performance impact. This change also removes some of the old code that still supproted Java 6. It is always assumed we now can register the class loaders as parallel capable.
- Loading branch information
Showing
3 changed files
with
111 additions
and
89 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...org.eclipse.osgi/container/src/org/eclipse/osgi/internal/container/KeyBasedLockStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022, 2024 IBM Corporation and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.osgi.internal.container; | ||
|
||
import java.lang.ref.ReferenceQueue; | ||
import java.lang.ref.WeakReference; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
|
||
public final class KeyBasedLockStore<Key, Lock> { | ||
|
||
final ReferenceQueue<Lock> refQueue = new ReferenceQueue<>(); | ||
private final ConcurrentHashMap<Key, LockWeakRef> lockMap = new ConcurrentHashMap<>(); | ||
private final Function<Key, Lock> lockCreator; | ||
|
||
public KeyBasedLockStore(Function<Key, Lock> lockCreator) { | ||
this.lockCreator = lockCreator; | ||
} | ||
|
||
private final class LockWeakRef extends WeakReference<Lock> { | ||
final Key key; | ||
|
||
public LockWeakRef(Lock referent, Key keyValue) { | ||
super(referent, refQueue); | ||
key = keyValue; | ||
} | ||
} | ||
|
||
public final Lock getLock(Key key) { | ||
poll(); | ||
LockWeakRef lockRef = lockMap.get(key); | ||
Lock lock = lockRef != null ? lockRef.get() : null; | ||
if (lock != null) { | ||
return lock; | ||
} | ||
|
||
lock = lockCreator.apply(key); | ||
|
||
while (true) { | ||
LockWeakRef retVal = lockMap.putIfAbsent(key, new LockWeakRef(lock, key)); | ||
if (retVal == null) { | ||
return lock; | ||
} | ||
|
||
Lock retLock = retVal.get(); | ||
if (retLock != null) { | ||
return retLock; | ||
} | ||
lockMap.remove(key, retVal); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private final void poll() { | ||
LockWeakRef lockRef; | ||
while ((lockRef = (LockWeakRef) refQueue.poll()) != null) { | ||
lockMap.remove(lockRef.key, lockRef); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters