dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dotgnu-pnet-commits] [SCM] DotGNU Portable.NET Class Library (pnetlib)


From: Klaus Treichel
Subject: [dotgnu-pnet-commits] [SCM] DotGNU Portable.NET Class Library (pnetlib) branch, master, updated. 34bc87b6dc6c3f9979bf34747d8dd4894418ea84
Date: Sat, 21 Nov 2009 18:09:24 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "DotGNU Portable.NET Class Library (pnetlib)".

The branch, master has been updated
       via  34bc87b6dc6c3f9979bf34747d8dd4894418ea84 (commit)
      from  31b2206e5dffe7f12c036fd06806ee459fb0a552 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.savannah.gnu.org/cgit/pnetlib.git/commit/?id=34bc87b6dc6c3f9979bf34747d8dd4894418ea84

commit 34bc87b6dc6c3f9979bf34747d8dd4894418ea84
Author: Klaus Treichel <address@hidden>
Date:   Sat Nov 21 19:09:04 2009 +0100

    Optimize the class locks away in the System.Text.Encoding and
    I18N.Common.Manager classes.

diff --git a/ChangeLog b/ChangeLog
index fccb556..b217ee8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2009-11-21  Klaus Treichel  <address@hidden>
+
+       * runtime/System/Text/Encoding.cs (InvokeI18N): Move the static 
invariant
+       stuff to a new static constructor and remove the class lock.
+       Add new private nested classes for the different encoding properties and
+       create the corresponding encoding in the static constructor ot the new
+       class with the code of the encoding property.
+       Return the static encoding member of the new corresponding nested class
+       in the encoding properties.
+
+       * I18N/Common/Manager.cs: Create the singleton manager in a static
+       constructor now instead of the PrimaryManager property.
+
 2009-06-08  Heiko Weiss  <address@hidden>
 
        * runtime/System/Private/NumberFormat/CustomFormatter.cs: fixed 
exception 
diff --git a/I18N/Common/Manager.cs b/I18N/Common/Manager.cs
index 8e3a77d..b3f2a94 100644
--- a/I18N/Common/Manager.cs
+++ b/I18N/Common/Manager.cs
@@ -44,6 +44,12 @@ public class Manager
        private Hashtable active;               // Currently active handlers.
        private Hashtable assemblies;   // Currently loaded region assemblies.
 
+       // static constructor
+       static Manager()
+                       {
+                               manager = new Manager();
+                       }
+
        // Constructor.
        private Manager()
                        {
@@ -58,14 +64,7 @@ public class Manager
                        {
                                get
                                {
-                                       lock(typeof(Manager))
-                                       {
-                                               if(manager == null)
-                                               {
-                                                       manager = new Manager();
-                                               }
-                                               return manager;
-                                       }
+                                       return manager;
                                }
                        }
 
diff --git a/runtime/System/Text/Encoding.cs b/runtime/System/Text/Encoding.cs
index 5838672..2529c1c 100644
--- a/runtime/System/Text/Encoding.cs
+++ b/runtime/System/Text/Encoding.cs
@@ -30,9 +30,97 @@ using System.Security;
 [Serializable]
 public abstract class Encoding
 {
+#if CONFIG_REFLECTION
+       // Loaded copy of the "I18N" assembly.  We need to move
+       // this into a class in "System.Private" eventually.
+       private static Type managerClass;
+       private static Object manager;
+#endif
+
        // Code page used by this encoding.
        internal int codePage;
 
+#if CONFIG_REFLECTION
+       // static constructor
+       static Encoding()
+                       {
+                               Assembly i18nAssembly;
+
+                               // Find or load the "I18N" assembly.
+                               try
+                               {
+                                       try
+                                       {
+                                               i18nAssembly = 
Assembly.Load("I18N");
+                                       }
+                                       catch(NotImplementedException)
+                                       {
+                                               // Assembly loading unsupported 
by the engine.
+                                               return;
+                                       }
+                                       catch(FileNotFoundException)
+                                       {
+                                               // Could not locate the I18N 
assembly.
+                                               return;
+                                       }
+                                       catch(BadImageFormatException)
+                                       {
+                                               // Something was wrong with the 
I18N assembly.
+                                               return;
+                                       }
+                                       catch(SecurityException)
+                                       {
+                                               // The engine refused to load 
I18N.
+                                               return;
+                                       }
+                               }
+                               catch(SystemException)
+                               {
+                                       return;
+                               }
+
+                               // Find the "I18N.Common.Manager" class.
+                               try
+                               {
+                                       managerClass = 
i18nAssembly.GetType("I18N.Common.Manager");
+                               }
+                               catch(NotImplementedException)
+                               {
+                                       // "GetType" is not supported by the 
engine.
+                                       managerClass = null;
+                                       return;
+                               }
+                               if(managerClass == null)
+                               {
+                                       return;
+                               }
+
+                               // Get the value of the "PrimaryManager" 
property.
+                               try
+                               {
+                                       manager = managerClass.InvokeMember
+                                                               
("PrimaryManager",
+                                                                
BindingFlags.GetProperty |
+                                                                       
BindingFlags.Static |
+                                                                       
BindingFlags.Public,
+                                                                null, null, 
null, null, null, null);
+                               }
+                               catch(MissingMethodException)
+                               {
+                                       return;
+                               }
+                               catch(SecurityException)
+                               {
+                                       return;
+                               }
+                               catch(NotImplementedException)
+                               {
+                                       // "InvokeMember" is not supported by 
the engine.
+                                       return;
+                               }
+                       }
+#endif
+
        // Constructor.
        protected Encoding()
                        {
@@ -229,137 +317,35 @@ public abstract class Encoding
                                return new ForwardingEncoder(this);
                        }
 
-#if CONFIG_REFLECTION
-       // Loaded copy of the "I18N" assembly.  We need to move
-       // this into a class in "System.Private" eventually.
-       private static Assembly i18nAssembly;
-       private static bool i18nDisabled;
-#endif
-
        // Invoke a specific method on the "I18N" manager object.
        // Returns NULL if the method failed.
        internal static Object InvokeI18N(String name, params Object[] args)
                        {
                        #if CONFIG_REFLECTION
-                               lock(typeof(Encoding))
+                               // Bail out if the static constructor detected 
that there
+                               // is insufficent engine support for I18N 
handling.
+                               if(manager == null)
                                {
-                                       // Bail out if we previously detected 
that there
-                                       // is insufficent engine support for 
I18N handling.
-                                       if(i18nDisabled)
-                                       {
-                                               return null;
-                                       }
-
-                                       // Find or load the "I18N" assembly.
-                                       if(i18nAssembly == null)
-                                       {
-                                               try
-                                               {
-                                                       try
-                                                       {
-                                                               i18nAssembly = 
Assembly.Load("I18N");
-                                                       }
-                                                       
catch(NotImplementedException)
-                                                       {
-                                                               // Assembly 
loading unsupported by the engine.
-                                                               i18nDisabled = 
true;
-                                                               return null;
-                                                       }
-                                                       
catch(FileNotFoundException)
-                                                       {
-                                                               // Could not 
locate the I18N assembly.
-                                                               i18nDisabled = 
true;
-                                                               return null;
-                                                       }
-                                                       
catch(BadImageFormatException)
-                                                       {
-                                                               // Something 
was wrong with the I18N assembly.
-                                                               i18nDisabled = 
true;
-                                                               return null;
-                                                       }
-                                                       catch(SecurityException)
-                                                       {
-                                                               // The engine 
refused to load I18N.
-                                                               i18nDisabled = 
true;
-                                                               return null;
-                                                       }
-                                                       if(i18nAssembly == null)
-                                                       {
-                                                               return null;
-                                                       }
-                                               }
-                                               catch(SystemException)
-                                               {
-                                                       return null;
-                                               }
-                                       }
-
-                                       // Find the "I18N.Common.Manager" class.
-                                       Type managerClass;
-                                       try
-                                       {
-                                               managerClass =
-                                                       
i18nAssembly.GetType("I18N.Common.Manager");
-                                       }
-                                       catch(NotImplementedException)
-                                       {
-                                               // "GetType" is not supported 
by the engine.
-                                               i18nDisabled = true;
-                                               return null;
-                                       }
-                                       if(managerClass == null)
-                                       {
-                                               return null;
-                                       }
-
-                                       // Get the value of the 
"PrimaryManager" property.
-                                       Object manager;
-                                       try
-                                       {
-                                               manager = 
managerClass.InvokeMember
-                                                               
("PrimaryManager",
-                                                                
BindingFlags.GetProperty |
-                                                                       
BindingFlags.Static |
-                                                                       
BindingFlags.Public,
-                                                                null, null, 
null, null, null, null);
-                                               if(manager == null)
-                                               {
-                                                       return null;
-                                               }
-                                       }
-                                       catch(MissingMethodException)
-                                       {
-                                               return null;
-                                       }
-                                       catch(SecurityException)
-                                       {
-                                               return null;
-                                       }
-                                       catch(NotImplementedException)
-                                       {
-                                               // "InvokeMember" is not 
supported by the engine.
-                                               i18nDisabled = true;
-                                               return null;
-                                       }
+                                       return null;
+                               }
 
-                                       // Invoke the requested method on the 
manager.
-                                       try
-                                       {
-                                               return managerClass.InvokeMember
-                                                               (name,
-                                                                
BindingFlags.InvokeMethod |
-                                                                       
BindingFlags.Instance |
-                                                                       
BindingFlags.Public,
-                                                                null, manager, 
args, null, null, null);
-                                       }
-                                       catch(MissingMethodException)
-                                       {
-                                               return null;
-                                       }
-                                       catch(SecurityException)
-                                       {
-                                               return null;
-                                       }
+                               // Invoke the requested method on the manager.
+                               try
+                               {
+                                       return managerClass.InvokeMember
+                                                       (name,
+                                                        
BindingFlags.InvokeMethod |
+                                                               
BindingFlags.Instance |
+                                                               
BindingFlags.Public,
+                                                        null, manager, args, 
null, null, null);
+                               }
+                               catch(MissingMethodException)
+                               {
+                                       return null;
+                               }
+                               catch(SecurityException)
+                               {
+                                       return null;
                                }
                        #else
                                return null;
@@ -729,30 +715,12 @@ public abstract class Encoding
 
 #endif // !ECMA_COMPAT
 
-       // Storage for standard encoding objects.
-       private static Encoding asciiEncoding = null;
-       private static Encoding bigEndianEncoding = null;
-       private static Encoding defaultEncoding = null;
-       private static Encoding utf7Encoding = null;
-       private static Encoding utf8Encoding = null;
-       private static Encoding unicodeEncoding = null;
-       private static Encoding isoLatin1Encoding = null;
-       private static Encoding utf32Encoding = null;
-       private static Encoding bigEndianUtf32Encoding = null;
-
        // Get the standard ASCII encoding object.
        public static Encoding ASCII
                        {
                                get
                                {
-                                       lock(typeof(Encoding))
-                                       {
-                                               if(asciiEncoding == null)
-                                               {
-                                                       asciiEncoding = new 
ASCIIEncoding();
-                                               }
-                                               return asciiEncoding;
-                                       }
+                                       return PrivateAsciiEncoding.encoding;
                                }
                        }
 
@@ -761,14 +729,7 @@ public abstract class Encoding
                        {
                                get
                                {
-                                       lock(typeof(Encoding))
-                                       {
-                                               if(bigEndianEncoding == null)
-                                               {
-                                                       bigEndianEncoding = new 
UnicodeEncoding(true, true);
-                                               }
-                                               return bigEndianEncoding;
-                                       }
+                                       return 
PrivateBigEndianUnicodeEncoding.encoding;
                                }
                        }
 
@@ -777,31 +738,7 @@ public abstract class Encoding
                        {
                                get
                                {
-                                       lock(typeof(Encoding))
-                                       {
-                                               if(defaultEncoding == null)
-                                               {
-                                                       // See if the 
underlying system knows what
-                                                       // code page handler we 
should be using.
-                                                       int codePage = 
DefaultEncoding.InternalCodePage();
-                                                       if(codePage != 0)
-                                                       {
-                                                               try
-                                                               {
-                                                                       
defaultEncoding = GetEncoding(codePage);
-                                                               }
-                                                               
catch(NotSupportedException)
-                                                               {
-                                                                       
defaultEncoding = new DefaultEncoding();
-                                                               }
-                                                       }
-                                                       else
-                                                       {
-                                                               defaultEncoding 
= new DefaultEncoding();
-                                                       }
-                                               }
-                                               return defaultEncoding;
-                                       }
+                                       return PrivateDefaultEncoding.encoding;
                                }
                        }
 
@@ -810,14 +747,7 @@ public abstract class Encoding
                        {
                                get
                                {
-                                       lock(typeof(Encoding))
-                                       {
-                                               if(isoLatin1Encoding == null)
-                                               {
-                                                       isoLatin1Encoding = new 
Latin1Encoding();
-                                               }
-                                               return isoLatin1Encoding;
-                                       }
+                                       return 
PrivateISOLatin1Encoding.encoding;
                                }
                        }
 
@@ -831,14 +761,7 @@ public abstract class Encoding
                        {
                                get
                                {
-                                       lock(typeof(Encoding))
-                                       {
-                                               if(utf7Encoding == null)
-                                               {
-                                                       utf7Encoding = new 
UTF7Encoding();
-                                               }
-                                               return utf7Encoding;
-                                       }
+                                       return PrivateUTF7Encoding.encoding;
                                }
                        }
 
@@ -847,14 +770,7 @@ public abstract class Encoding
                        {
                                get
                                {
-                                       lock(typeof(Encoding))
-                                       {
-                                               if(utf8Encoding == null)
-                                               {
-                                                       utf8Encoding = new 
UTF8Encoding(true);
-                                               }
-                                               return utf8Encoding;
-                                       }
+                                       return PrivateUTF8Encoding.encoding;
                                }
                        }
 
@@ -863,19 +779,12 @@ public abstract class Encoding
                        {
                                get
                                {
-                                       lock(typeof(Encoding))
-                                       {
-                                               if(unicodeEncoding == null)
-                                               {
-                                                       unicodeEncoding = new 
UnicodeEncoding();
-                                               }
-                                               return unicodeEncoding;
-                                       }
+                                       return PrivateUnicodeEncoding.encoding;
                                }
                        }
 
        // Get the standard UTF-32 encoding object.
-#if !ECMA_COMPAT && CONFIG_FRAMEWORK_1_2
+#if !ECMA_COMPAT && CONFIG_FRAMEWORK_2_0
        public
 #else
        internal
@@ -884,14 +793,7 @@ public abstract class Encoding
                        {
                                get
                                {
-                                       lock(typeof(Encoding))
-                                       {
-                                               if(utf32Encoding == null)
-                                               {
-                                                       utf32Encoding = new 
UTF32Encoding();
-                                               }
-                                               return utf32Encoding;
-                                       }
+                                       return PrivateUTF32Encoding.encoding;
                                }
                        }
 
@@ -900,17 +802,120 @@ public abstract class Encoding
                        {
                                get
                                {
-                                       lock(typeof(Encoding))
+                                       return 
PrivateBigEndianUTF32Encoding.encoding;
+                               }
+                       }
+
+       // Helper classes for initializing the static encoding instances on
+       // demand without having to lock something because static constructors
+       // are handled by the engine.
+       private sealed class PrivateAsciiEncoding
+       {
+               public static Encoding encoding;
+
+               static PrivateAsciiEncoding()
+                               {
+                                       encoding = new ASCIIEncoding();
+                               }
+       }
+
+       private sealed class PrivateBigEndianUnicodeEncoding
+       {
+               public static Encoding encoding;
+
+               static PrivateBigEndianUnicodeEncoding()
+                               {
+                                       encoding = new UnicodeEncoding(true, 
true);
+                               }
+       }
+
+       private sealed class PrivateDefaultEncoding
+       {
+               public static Encoding encoding;
+
+               static PrivateDefaultEncoding()
+                               {
+                                       // See if the underlying system knows 
what
+                                       // code page handler we should be using.
+                                       int codePage = 
DefaultEncoding.InternalCodePage();
+
+                                       if(codePage != 0)
                                        {
-                                               if(bigEndianUtf32Encoding == 
null)
+                                               try
+                                               {
+                                                       encoding = 
GetEncoding(codePage);
+                                               }
+                                               catch(NotSupportedException)
                                                {
-                                                       bigEndianUtf32Encoding 
= new UTF32Encoding
-                                                               (true, true);
+                                                       encoding = new 
DefaultEncoding();
                                                }
-                                               return bigEndianUtf32Encoding;
+                                       }
+                                       else
+                                       {
+                                               encoding = new 
DefaultEncoding();
                                        }
                                }
-                       }
+       }
+
+       private sealed class PrivateISOLatin1Encoding
+       {
+               public static Encoding encoding;
+
+               static PrivateISOLatin1Encoding()
+                               {
+                                       encoding = new Latin1Encoding();
+                               }
+       }
+
+       private sealed class PrivateUTF7Encoding
+       {
+               public static Encoding encoding;
+
+               static PrivateUTF7Encoding()
+                               {
+                                       encoding = new UTF7Encoding();
+                               }
+       }
+
+       private sealed class PrivateUTF8Encoding
+       {
+               public static Encoding encoding;
+
+               static PrivateUTF8Encoding()
+                               {
+                                       encoding = new UTF8Encoding(true);
+                               }
+       }
+
+       private sealed class PrivateUnicodeEncoding
+       {
+               public static Encoding encoding;
+
+               static PrivateUnicodeEncoding()
+                               {
+                                       encoding = new UnicodeEncoding();
+                               }
+       }
+
+       private sealed class PrivateUTF32Encoding
+       {
+               public static Encoding encoding;
+
+               static PrivateUTF32Encoding()
+                               {
+                                       encoding = new UTF32Encoding();
+                               }
+       }
+
+       private sealed class PrivateBigEndianUTF32Encoding
+       {
+               public static Encoding encoding;
+
+               static PrivateBigEndianUTF32Encoding()
+                               {
+                                       encoding = new UTF32Encoding(true, 
true);
+                               }
+       }
 
        // Forwarding decoder implementation.
        private sealed class ForwardingDecoder : Decoder

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog                       |   13 ++
 I18N/Common/Manager.cs          |   15 +-
 runtime/System/Text/Encoding.cs |  451 ++++++++++++++++++++-------------------
 3 files changed, 248 insertions(+), 231 deletions(-)


hooks/post-receive
-- 
DotGNU Portable.NET Class Library (pnetlib)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]