Skip to content

Commit

Permalink
testing atomic INITIALIZATION_STATE
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed Mar 15, 2016
1 parent 79698d6 commit 4230a07
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Expand Up @@ -33,6 +33,7 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.event.SubstituteLoggingEvent;
import org.slf4j.helpers.NOPLoggerFactory;
Expand Down Expand Up @@ -82,7 +83,7 @@ public final class LoggerFactory {
static final int SUCCESSFUL_INITIALIZATION = 3;
static final int NOP_FALLBACK_INITIALIZATION = 4;

static volatile int INITIALIZATION_STATE = UNINITIALIZED;
static AtomicLong INITIALIZATION_STATE = new AtomicLong(UNINITIALIZED);
static SubstituteLoggerFactory SUBST_FACTORY = new SubstituteLoggerFactory();
static NOPLoggerFactory NOP_FALLBACK_FACTORY = new NOPLoggerFactory();

Expand Down Expand Up @@ -117,12 +118,12 @@ private LoggerFactory() {
* You are strongly discouraged from calling this method in production code.
*/
static void reset() {
INITIALIZATION_STATE = UNINITIALIZED;
INITIALIZATION_STATE.set(UNINITIALIZED);
}

private final static void performInitialization() {
bind();
if (INITIALIZATION_STATE == SUCCESSFUL_INITIALIZATION) {
if (INITIALIZATION_STATE.get() == SUCCESSFUL_INITIALIZATION) {
versionSanityCheck();
}
}
Expand All @@ -147,13 +148,13 @@ private final static void bind() {
}
// the next line does the binding
StaticLoggerBinder.getSingleton();
INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
INITIALIZATION_STATE.set(SUCCESSFUL_INITIALIZATION);
reportActualBinding(staticLoggerBinderPathSet);
replayEvents();
} catch (NoClassDefFoundError ncde) {
String msg = ncde.getMessage();
if (messageContainsOrgSlf4jImplStaticLoggerBinder(msg)) {
INITIALIZATION_STATE = NOP_FALLBACK_INITIALIZATION;
INITIALIZATION_STATE.set(NOP_FALLBACK_INITIALIZATION);
Util.report("Failed to load class \"org.slf4j.impl.StaticLoggerBinder\".");
Util.report("Defaulting to no-operation (NOP) logger implementation");
Util.report("See " + NO_STATICLOGGERBINDER_URL + " for further details.");
Expand All @@ -164,7 +165,7 @@ private final static void bind() {
} catch (java.lang.NoSuchMethodError nsme) {
String msg = nsme.getMessage();
if (msg != null && msg.contains("org.slf4j.impl.StaticLoggerBinder.getSingleton()")) {
INITIALIZATION_STATE = FAILED_INITIALIZATION;
INITIALIZATION_STATE.set(FAILED_INITIALIZATION);
Util.report("slf4j-api 1.6.x (or later) is incompatible with this binding.");
Util.report("Your binding is version 1.5.5 or earlier.");
Util.report("Upgrade your binding to version 1.6.x.");
Expand All @@ -177,7 +178,7 @@ private final static void bind() {
}

static void failedBinding(Throwable t) {
INITIALIZATION_STATE = FAILED_INITIALIZATION;
INITIALIZATION_STATE.set(FAILED_INITIALIZATION);
Util.report("Failed to instantiate SLF4J LoggerFactory", t);
}

Expand Down Expand Up @@ -383,15 +384,20 @@ private static boolean nonMatchingClasses(Class<?> clazz, Class<?> autoComputedC
* @return the ILoggerFactory instance in use
*/
public static ILoggerFactory getILoggerFactory() {
if (INITIALIZATION_STATE == UNINITIALIZED) {
synchronized (LoggerFactory.class) {
if (INITIALIZATION_STATE == UNINITIALIZED) {
INITIALIZATION_STATE = ONGOING_INITIALIZATION;
performInitialization();
}
}
// if (INITIALIZATION_STATE == UNINITIALIZED) {
// synchronized (LoggerFactory.class) {
// if (INITIALIZATION_STATE == UNINITIALIZED) {
// INITIALIZATION_STATE = ONGOING_INITIALIZATION;
// performInitialization();
// }
// }
// }

if(INITIALIZATION_STATE.compareAndSet(UNINITIALIZED, ONGOING_INITIALIZATION)) {
performInitialization();
}
switch (INITIALIZATION_STATE) {

switch ((int) INITIALIZATION_STATE.get()) {
case SUCCESSFUL_INITIALIZATION:
return StaticLoggerBinder.getSingleton().getLoggerFactory();
case NOP_FALLBACK_INITIALIZATION:
Expand Down

0 comments on commit 4230a07

Please sign in to comment.