changeset 38887:bb8ffdf4e7aa

8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs Summary: Making Preferences.systemRoot/userRoot lazy on Windows, to avoid warnings for system root when only user root was requested; reducing synchronization while creating the Preferences. Reviewed-by: alanb
author jlahoda
date Mon, 13 Jun 2016 11:46:07 +0200
parents bf687d4281eb
children dd584fbea6a2
files jdk/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferences.java jdk/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java jdk/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferences.java jdk/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferencesFactory.java
diffstat 4 files changed, 78 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferences.java	Mon Jun 13 00:47:32 2016 -0700
+++ b/jdk/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferences.java	Mon Jun 13 11:46:07 2016 +0200
@@ -44,27 +44,39 @@
     private final String path;
 
     // User root and system root nodes
-    private static MacOSXPreferences userRoot = null;
-    private static MacOSXPreferences systemRoot = null;
+    private static volatile MacOSXPreferences userRoot;
+    private static volatile MacOSXPreferences systemRoot;
 
 
     // Returns user root node, creating it if necessary.
     // Called by MacOSXPreferencesFactory
-    static synchronized Preferences getUserRoot() {
-        if (userRoot == null) {
-            userRoot = new MacOSXPreferences(true);
+    static Preferences getUserRoot() {
+        MacOSXPreferences root = userRoot;
+        if (root == null) {
+            synchronized (MacOSXPreferences.class) {
+                root = userRoot;
+                if (root == null) {
+                    userRoot = root = new MacOSXPreferences(true);
+                }
+            }
         }
-        return userRoot;
+        return root;
     }
 
 
     // Returns system root node, creating it if necessary.
     // Called by MacOSXPreferencesFactory
-    static synchronized Preferences getSystemRoot() {
-        if (systemRoot == null) {
-            systemRoot = new MacOSXPreferences(false);
+    static Preferences getSystemRoot() {
+        MacOSXPreferences root = systemRoot;
+        if (root == null) {
+            synchronized (MacOSXPreferences.class) {
+                root = systemRoot;
+                if (root == null) {
+                    systemRoot = root = new MacOSXPreferences(false);
+                }
+            }
         }
-        return systemRoot;
+        return root;
     }
 
 
--- a/jdk/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java	Mon Jun 13 00:47:32 2016 -0700
+++ b/jdk/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java	Mon Jun 13 11:46:07 2016 +0200
@@ -94,14 +94,20 @@
    /**
      * The user root.
      */
-    static Preferences userRoot = null;
+    private static volatile Preferences userRoot;
 
-    static synchronized Preferences getUserRoot() {
-        if (userRoot == null) {
-            setupUserRoot();
-            userRoot = new FileSystemPreferences(true);
+    static Preferences getUserRoot() {
+        Preferences root = userRoot;
+        if (root == null) {
+            synchronized (FileSystemPreferences.class) {
+                root = userRoot;
+                if (root == null) {
+                    setupUserRoot();
+                    userRoot = root = new FileSystemPreferences(true);
+                }
+            }
         }
-        return userRoot;
+        return root;
     }
 
     private static void setupUserRoot() {
@@ -155,14 +161,20 @@
     /**
      * The system root.
      */
-    static Preferences systemRoot;
+    private static volatile Preferences systemRoot;
 
-    static synchronized Preferences getSystemRoot() {
-        if (systemRoot == null) {
-            setupSystemRoot();
-            systemRoot = new FileSystemPreferences(false);
+    static Preferences getSystemRoot() {
+        Preferences root = systemRoot;
+        if (root == null) {
+            synchronized (FileSystemPreferences.class) {
+                root = systemRoot;
+                if (root == null) {
+                    setupSystemRoot();
+                    systemRoot = root = new FileSystemPreferences(false);
+                }
+            }
         }
-        return systemRoot;
+        return root;
     }
 
     private static void setupSystemRoot() {
--- a/jdk/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferences.java	Mon Jun 13 00:47:32 2016 -0700
+++ b/jdk/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferences.java	Mon Jun 13 11:46:07 2016 +0200
@@ -91,14 +91,40 @@
     /**
      * User root node.
      */
-    static final Preferences userRoot =
-         new WindowsPreferences(USER_ROOT_NATIVE_HANDLE, WINDOWS_ROOT_PATH);
+    private static volatile Preferences userRoot;
+
+    static Preferences getUserRoot() {
+        Preferences root = userRoot;
+        if (root == null) {
+            synchronized (WindowsPreferences.class) {
+                root = userRoot;
+                if (root == null) {
+                    root = new WindowsPreferences(USER_ROOT_NATIVE_HANDLE, WINDOWS_ROOT_PATH);
+                    userRoot = root;
+                }
+            }
+        }
+        return root;
+    }
 
     /**
      * System root node.
      */
-    static final Preferences systemRoot =
-        new WindowsPreferences(SYSTEM_ROOT_NATIVE_HANDLE, WINDOWS_ROOT_PATH);
+    private static volatile Preferences systemRoot;
+
+    static Preferences getSystemRoot() {
+        Preferences root = systemRoot;
+        if (root == null) {
+            synchronized (WindowsPreferences.class) {
+                root = systemRoot;
+                if (root == null) {
+                    root = new WindowsPreferences(SYSTEM_ROOT_NATIVE_HANDLE, WINDOWS_ROOT_PATH);
+                    systemRoot = root;
+                }
+            }
+        }
+        return root;
+    }
 
     /*  Windows error codes. */
     private static final int ERROR_SUCCESS = 0;
--- a/jdk/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferencesFactory.java	Mon Jun 13 00:47:32 2016 -0700
+++ b/jdk/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferencesFactory.java	Mon Jun 13 11:46:07 2016 +0200
@@ -39,13 +39,13 @@
      * Returns WindowsPreferences.userRoot
      */
     public Preferences userRoot() {
-        return WindowsPreferences.userRoot;
+        return WindowsPreferences.getUserRoot();
     }
 
     /**
      * Returns WindowsPreferences.systemRoot
      */
     public Preferences systemRoot() {
-        return WindowsPreferences.systemRoot;
+        return WindowsPreferences.getSystemRoot();
     }
 }