Skip to content

Commit

Permalink
Use faster reference type
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd committed Feb 24, 2015
1 parent 9f2b507 commit 0e1cac4
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions src/main/java/org/jboss/modules/ModuleClassLoader.java
Expand Up @@ -39,7 +39,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;

/**
* A module classloader. Instances of this class implement the complete view of classes and resources available in a
Expand Down Expand Up @@ -69,7 +69,7 @@ public class ModuleClassLoader extends ConcurrentClassLoader {
private final Module module;
private final ClassFileTransformer transformer;

private volatile Paths<ResourceLoader, ResourceLoaderSpec> paths;
private final AtomicReference<Paths<ResourceLoader, ResourceLoaderSpec>> paths = new AtomicReference<>(Paths.<ResourceLoader, ResourceLoaderSpec>none());

private final LocalLoader localLoader = new IterableLocalLoader() {
public Class<?> loadClassLocal(final String name, final boolean resolve) {
Expand Down Expand Up @@ -104,22 +104,14 @@ public String toString() {
}
};

private static final AtomicReferenceFieldUpdater<ModuleClassLoader, Paths<ResourceLoader, ResourceLoaderSpec>> pathsUpdater
= unsafeCast(AtomicReferenceFieldUpdater.newUpdater(ModuleClassLoader.class, Paths.class, "paths"));

@SuppressWarnings({ "unchecked" })
private static <A, B> AtomicReferenceFieldUpdater<A, B> unsafeCast(AtomicReferenceFieldUpdater<?, ?> updater) {
return (AtomicReferenceFieldUpdater<A, B>) updater;
}

/**
* Construct a new instance.
*
* @param configuration the module class loader configuration to use
*/
protected ModuleClassLoader(final Configuration configuration) {
module = configuration.getModule();
paths = new Paths<ResourceLoader, ResourceLoaderSpec>(configuration.getResourceLoaders(), Collections.<String, List<ResourceLoader>>emptyMap());
paths.lazySet(new Paths<>(configuration.getResourceLoaders(), Collections.<String, List<ResourceLoader>>emptyMap()));
final AssertionSetting setting = configuration.getAssertionSetting();
if (setting != AssertionSetting.INHERIT) {
setDefaultAssertionStatus(setting == AssertionSetting.ENABLED);
Expand All @@ -134,7 +126,7 @@ protected ModuleClassLoader(final Configuration configuration) {
* before the calling thread
*/
boolean recalculate() {
final Paths<ResourceLoader, ResourceLoaderSpec> paths = this.paths;
final Paths<ResourceLoader, ResourceLoaderSpec> paths = this.paths.get();
return setResourceLoaders(paths, paths.getSourceList(NO_RESOURCE_LOADERS));
}

Expand All @@ -146,7 +138,7 @@ boolean recalculate() {
* before the calling thread
*/
boolean setResourceLoaders(final ResourceLoaderSpec[] resourceLoaders) {
return setResourceLoaders(paths, resourceLoaders);
return setResourceLoaders(paths.get(), resourceLoaders);
}

private boolean setResourceLoaders(final Paths<ResourceLoader, ResourceLoaderSpec> paths, final ResourceLoaderSpec[] resourceLoaders) {
Expand All @@ -167,7 +159,7 @@ private boolean setResourceLoaders(final Paths<ResourceLoader, ResourceLoaderSpe
}
}
}
return pathsUpdater.compareAndSet(this, paths, new Paths<ResourceLoader, ResourceLoaderSpec>(resourceLoaders, allPaths));
return this.paths.compareAndSet(paths, new Paths<>(resourceLoaders, allPaths));
}

/**
Expand Down Expand Up @@ -239,7 +231,7 @@ public Class<?> loadClassLocal(final String className, final boolean resolve) th
return loadedClass;
}

final Map<String, List<ResourceLoader>> paths = this.paths.getAllPaths();
final Map<String, List<ResourceLoader>> paths = this.paths.get().getAllPaths();

log.trace("Loading class %s locally from %s", className, module);

Expand Down Expand Up @@ -302,7 +294,7 @@ public Class<?> loadClassLocal(final String className, final boolean resolve) th
*/
Resource loadResourceLocal(final String root, final String name) {

final Map<String, List<ResourceLoader>> paths = this.paths.getAllPaths();
final Map<String, List<ResourceLoader>> paths = this.paths.get().getAllPaths();

final String path = Module.pathOf(name);

Expand All @@ -328,7 +320,7 @@ Resource loadResourceLocal(final String root, final String name) {
* @return the list of resources
*/
public List<Resource> loadResourceLocal(final String name) {
final Map<String, List<ResourceLoader>> paths = this.paths.getAllPaths();
final Map<String, List<ResourceLoader>> paths = this.paths.get().getAllPaths();

final String path = Module.pathOf(name);

Expand Down Expand Up @@ -509,7 +501,7 @@ protected final String findLibrary(final String libname) {
final ModuleLogger log = Module.log;
log.trace("Attempting to load native library %s from %s", libname, module);

for (ResourceLoaderSpec loader : paths.getSourceList(NO_RESOURCE_LOADERS)) {
for (ResourceLoaderSpec loader : paths.get().getSourceList(NO_RESOURCE_LOADERS)) {
final String library = loader.getResourceLoader().getLibrary(libname);
if (library != null) {
return library;
Expand Down Expand Up @@ -560,7 +552,7 @@ public final String toString() {
}

Set<String> getPaths() {
return paths.getAllPaths().keySet();
return paths.get().getAllPaths().keySet();
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -634,7 +626,7 @@ protected final void finalize() throws Throwable {
}

ResourceLoader[] getResourceLoaders() {
final ResourceLoaderSpec[] specs = paths.getSourceList(NO_RESOURCE_LOADERS);
final ResourceLoaderSpec[] specs = paths.get().getSourceList(NO_RESOURCE_LOADERS);
final int length = specs.length;
final ResourceLoader[] loaders = new ResourceLoader[length];
for (int i = 0; i < length; i++) {
Expand Down Expand Up @@ -664,7 +656,7 @@ public final Iterator<Resource> iterateResources(final String startName, final b
} else {
filter = PathFilters.is(realStartName);
}
final Map<String, List<ResourceLoader>> paths = this.paths.getAllPaths();
final Map<String, List<ResourceLoader>> paths = this.paths.get().getAllPaths();
final Iterator<Map.Entry<String, List<ResourceLoader>>> iterator = paths.entrySet().iterator();
return new Iterator<Resource>() {

Expand Down Expand Up @@ -730,7 +722,7 @@ public void remove() {
* @return the set of local paths
*/
public final Set<String> getLocalPaths() {
return Collections.unmodifiableSet(paths.getAllPaths().keySet());
return Collections.unmodifiableSet(paths.get().getAllPaths().keySet());
}

/**
Expand Down

0 comments on commit 0e1cac4

Please sign in to comment.