Mercurial > hg > truffle
diff graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java @ 13163:1baa169508f5
made DebugScope be AutoCloseable so that try-with-resource can be used to more cleanly manage scopes
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Mon, 25 Nov 2013 18:42:51 +0100 |
parents | 401830ff96f4 |
children | 8569b9e047cd |
line wrap: on
line diff
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java Mon Nov 25 17:32:15 2013 +0100 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java Mon Nov 25 18:42:51 2013 +0100 @@ -28,7 +28,7 @@ import com.oracle.graal.debug.*; -public final class DebugScope { +public final class DebugScope implements AutoCloseable { private final class IndentImpl implements Indent { @@ -99,6 +99,8 @@ private static ThreadLocal<Throwable> lastExceptionThrownTL = new ThreadLocal<>(); private final DebugScope parent; + private final DebugConfig parentConfig; + private final boolean sandbox; private IndentImpl lastUsedIndent; private boolean logScopeName; @@ -118,7 +120,7 @@ public static DebugScope getInstance() { DebugScope result = instanceTL.get(); if (result == null) { - DebugScope topLevelDebugScope = new DebugScope(Thread.currentThread().getName(), "", null); + DebugScope topLevelDebugScope = new DebugScope(Thread.currentThread().getName(), "", null, false); instanceTL.set(topLevelDebugScope); DebugValueMap.registerTopLevel(topLevelDebugScope.getValueMap()); return topLevelDebugScope; @@ -131,8 +133,10 @@ return configTL.get(); } - private DebugScope(String name, String qualifiedName, DebugScope parent, Object... context) { + private DebugScope(String name, String qualifiedName, DebugScope parent, boolean sandbox, Object... context) { this.parent = parent; + this.sandbox = sandbox; + this.parentConfig = getConfig(); this.context = context; this.qualifiedName = qualifiedName; if (parent != null) { @@ -162,6 +166,12 @@ } } + public void close() { + context = null; + instanceTL.set(parent); + setConfig(parentConfig); + } + public boolean isDumpEnabled() { return dumpEnabled; } @@ -227,32 +237,28 @@ * @param newName the name of the new scope * @param runnable the task to run (must be null iff {@code callable} is not null) * @param callable the task to run (must be null iff {@code runnable} is not null) - * @param sandbox specifies if the scope is a child of the current scope or a top level scope - * @param sandboxConfig the config to use of a new top level scope (ignored if - * {@code sandbox == false}) + * @param sandboxConfig if non-null, a new top level scope is entered with this configuration * @param newContext context objects of the new scope * @return the value returned by the task */ - public <T> T scope(String newName, Runnable runnable, Callable<T> callable, boolean sandbox, DebugConfig sandboxConfig, Object[] newContext) { - DebugScope oldContext = getInstance(); - DebugConfig oldConfig = getConfig(); - DebugScope newChild = null; - if (sandbox) { - newChild = new DebugScope(newName, newName, null, newContext); + public <T> T scope(String newName, Runnable runnable, Callable<T> callable, DebugConfig sandboxConfig, Object[] newContext) { + try (DebugScope s = openScope(newName, sandboxConfig, newContext)) { + return executeScope(runnable, callable); + } + } + + public DebugScope openScope(String newName, DebugConfig sandboxConfig, Object... newContext) { + DebugScope newScope = null; + if (sandboxConfig != null) { + newScope = new DebugScope(newName, newName, this, true, newContext); setConfig(sandboxConfig); } else { - newChild = oldContext.createChild(newName, newContext); + newScope = this.createChild(newName, newContext); } - instanceTL.set(newChild); - newChild.setLogEnabled(oldContext.isLogEnabled()); - newChild.updateFlags(); - try { - return executeScope(runnable, callable); - } finally { - newChild.context = null; - instanceTL.set(oldContext); - setConfig(oldConfig); - } + instanceTL.set(newScope); + newScope.setLogEnabled(this.isLogEnabled()); + newScope.updateFlags(); + return newScope; } private <T> T executeScope(Runnable runnable, Callable<T> callable) { @@ -315,7 +321,7 @@ return new RuntimeException("Exception while intercepting exception", t); } } - }, false, null, new Object[]{e}); + }, null, new Object[]{e}); } return null; } @@ -337,7 +343,7 @@ if (this.qualifiedName.length() > 0) { newQualifiedName = this.qualifiedName + SCOPE_SEP + newName; } - DebugScope result = new DebugScope(newName, newQualifiedName, this, newContext); + DebugScope result = new DebugScope(newName, newQualifiedName, this, false, newContext); return result; } @@ -360,7 +366,7 @@ private void selectScope() { while (currentScope != null && currentScope.context.length <= objectIndex) { - currentScope = currentScope.parent; + currentScope = currentScope.sandbox ? null : currentScope.parent; objectIndex = 0; } }