提交 02fbfd9e 编写于 作者: K kohsuke

Fixed a possible race condition during Hudson start up.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@34294 71c3de6d-444a-0410-be80-ed276b4c234a
上级 4abc357d
......@@ -209,7 +209,7 @@ public class ExtensionList<T> extends AbstractList<T> {
if(Hudson.getInstance().getInitLevel().compareTo(InitMilestone.PLUGINS_PREPARED)<0)
return legacyInstances; // can't perform the auto discovery until all plugins are loaded, so just make the legacy instances visible
synchronized (this) {
synchronized (hudson.lookup.setIfNull(Lock.class,new Lock())) {
if(extensions==null) {
List<ExtensionComponent<T>> r = load();
r.addAll(legacyInstances);
......@@ -219,6 +219,13 @@ public class ExtensionList<T> extends AbstractList<T> {
}
}
/**
* Loading an {@link ExtensionList} can result in a nested loading of another {@link ExtensionList}.
* What that means is that we need a single lock that spans across all the {@link ExtensionList}s,
* or else we can end up in a dead lock.
*/
private static final class Lock {}
/**
* Loads all the extensions.
*/
......
......@@ -24,7 +24,6 @@
package hudson;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
......@@ -33,7 +32,7 @@ import java.util.concurrent.ConcurrentHashMap;
* @author Kohsuke Kawaguchi
*/
public class Lookup {
private final Map<Class,Object> data = new ConcurrentHashMap<Class,Object>();
private final ConcurrentHashMap<Class,Object> data = new ConcurrentHashMap<Class,Object>();
public <T> T get(Class<T> type) {
return type.cast(data.get(type));
......@@ -42,4 +41,17 @@ public class Lookup {
public <T> T set(Class<T> type, T instance) {
return type.cast(data.put(type,instance));
}
/**
* Overwrites the value only if the current value is null.
*
* @return
* If the value was null, return the {@code instance} value.
* Otherwise return the current value, which is non-null.
*/
public <T> T setIfNull(Class<T> type, T instance) {
Object o = data.putIfAbsent(type, instance);
if (o!=null) return type.cast(o);
return instance;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册