changeset 16008:76f40e11c820

refactored HotSpotGraalRuntime.LogFileOption to PrintStreamOption to workaround a javac bug as well as to clarify its design
author Doug Simon <doug.simon@oracle.com>
date Tue, 03 Jun 2014 17:13:52 +0200
parents 23ae0cbcb2ae
children a831a16e52b4
files graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompilationQueue.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotGraalRuntime.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/PrintStreamOption.java
diffstat 3 files changed, 85 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompilationQueue.java	Tue Jun 03 17:11:40 2014 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompilationQueue.java	Tue Jun 03 17:13:52 2014 +0200
@@ -119,7 +119,7 @@
     private CompilationQueue() {
         CompilerThreadFactory factory = new CompilerThreadFactory("GraalCompilerThread", new DebugConfigAccess() {
             public GraalDebugConfig getDebugConfig() {
-                return Debug.isEnabled() ? DebugEnvironment.initialize(HotSpotGraalRuntime.Options.LogFile.log()) : null;
+                return Debug.isEnabled() ? DebugEnvironment.initialize(HotSpotGraalRuntime.Options.LogFile.getStream()) : null;
             }
         });
 
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotGraalRuntime.java	Tue Jun 03 17:11:40 2014 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotGraalRuntime.java	Tue Jun 03 17:13:52 2014 +0200
@@ -25,13 +25,11 @@
 import static com.oracle.graal.compiler.GraalDebugConfig.*;
 import static com.oracle.graal.compiler.common.GraalOptions.*;
 import static com.oracle.graal.compiler.common.UnsafeAccess.*;
-//import static com.oracle.graal.hotspot.CompilationQueue.*;
 import static com.oracle.graal.hotspot.CompileTheWorld.Options.*;
 import static com.oracle.graal.hotspot.HotSpotGraalRuntime.Options.*;
 import static com.oracle.graal.hotspot.InitTimer.*;
 import static sun.reflect.Reflection.*;
 
-import java.io.*;
 import java.lang.reflect.*;
 import java.util.*;
 
@@ -124,7 +122,7 @@
 
         this.compilerToVm = toVM;
 
-        TTY.initialize(Options.LogFile.log());
+        TTY.initialize(Options.LogFile.getStream());
 
         if (Log.getValue() == null && Meter.getValue() == null && Time.getValue() == null && Dump.getValue() == null) {
             if (MethodFilter.getValue() != null) {
@@ -133,7 +131,7 @@
         }
 
         if (Debug.isEnabled()) {
-            DebugEnvironment.initialize(LogFile.log());
+            DebugEnvironment.initialize(LogFile.getStream());
 
             String summary = DebugValueSummary.getValue();
             if (summary != null) {
@@ -176,38 +174,10 @@
         static final OptionValue<String> GraalRuntime = new OptionValue<>("");
 
         @Option(help = "File to which logging is sent")
-        public static final LogFileOption LogFile = new LogFileOption();
+        public static final PrintStreamOption LogFile = new PrintStreamOption();
         // @formatter:on
     }
 
-    public static class LogFileOption extends OptionValue<String> {
-        public LogFileOption() {
-            super(null);
-        }
-
-        private volatile PrintStream log;
-
-        public PrintStream log() {
-            if (log == null) {
-                if (getValue() != null) {
-                    synchronized (this) {
-                        if (log == null) {
-                            try {
-                                final boolean enableAutoflush = true;
-                                log = new PrintStream(new FileOutputStream(LogFile.getValue()), enableAutoflush);
-                            } catch (FileNotFoundException e) {
-                                throw new RuntimeException("couldn't open log file: " + LogFile.getValue(), e);
-                            }
-                        }
-                    }
-                } else {
-                    log = System.out;
-                }
-            }
-            return log;
-        }
-    }
-
     private static HotSpotBackendFactory findFactory(String architecture) {
         HotSpotBackendFactory basic = null;
         HotSpotBackendFactory selected = null;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/PrintStreamOption.java	Tue Jun 03 17:13:52 2014 +0200
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.hotspot;
+
+import java.io.*;
+
+import com.oracle.graal.options.*;
+
+/**
+ * An option that encapsulates and configures a print stream.
+ */
+public class PrintStreamOption extends OptionValue<String> {
+
+    public PrintStreamOption() {
+        super(null);
+    }
+
+    /**
+     * The print stream to which output will be written.
+     *
+     * Declared {@code volatile} to enable safe use of double-checked locking in
+     * {@link #getStream()} and {@link #setValue(Object)}.
+     */
+    private volatile PrintStream ps;
+
+    /**
+     * Gets the print stream configured by this option.
+     */
+    public PrintStream getStream() {
+        if (ps == null) {
+            if (getValue() != null) {
+                synchronized (this) {
+                    if (ps == null) {
+                        try {
+                            final boolean enableAutoflush = true;
+                            ps = new PrintStream(new FileOutputStream(getValue()), enableAutoflush);
+                        } catch (FileNotFoundException e) {
+                            throw new RuntimeException("couldn't open file: " + getValue(), e);
+                        }
+                    }
+                }
+            } else {
+                ps = System.out;
+            }
+        }
+        return ps;
+    }
+
+    @Override
+    public void setValue(Object v) {
+        if (ps != null) {
+            synchronized (this) {
+                if (ps != null) {
+                    ps.close();
+                    ps = null;
+                }
+            }
+        }
+        super.setValue(v);
+    }
+}
\ No newline at end of file