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. 02f50139aaf7107703cb46fdb8f6e7d67604779d
Date: Mon, 23 Nov 2009 14:26:33 +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  02f50139aaf7107703cb46fdb8f6e7d67604779d (commit)
      from  2dba8321b1d7110e25d57dd465aea9229a1865c1 (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=02f50139aaf7107703cb46fdb8f6e7d67604779d

commit 02f50139aaf7107703cb46fdb8f6e7d67604779d
Author: Klaus Treichel <address@hidden>
Date:   Mon Nov 23 15:26:17 2009 +0100

    Optimize away the lock statements around accesses to the cached toolkit
    and printingSystem members in System.Drawing/Toolkit/ToolkitManager.cs.

diff --git a/ChangeLog b/ChangeLog
index bb5f53f..391b04a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,6 +14,11 @@
        * runtime/System/IO/TextWriter.cs: Lock the underlying writer instead of
        this in the synchronized text writer.
 
+       * System.Drawing/Toolkit/ToolkitManager.cs: Move the static members
+       toolkit and printingSystem to seperate classes to allow initialization
+       on demand and optimize away the locks around accesses to them in the
+       properties.
+
 2009-11-21  Klaus Treichel  <address@hidden>
 
        * runtime/System/Text/Encoding.cs (InvokeI18N): Move the static 
invariant
diff --git a/System.Drawing/Toolkit/ToolkitManager.cs 
b/System.Drawing/Toolkit/ToolkitManager.cs
index eeb40aa..9ac384a 100644
--- a/System.Drawing/Toolkit/ToolkitManager.cs
+++ b/System.Drawing/Toolkit/ToolkitManager.cs
@@ -30,9 +30,16 @@ using DotGNU.Images;
 [NonStandardExtra]
 public sealed class ToolkitManager
 {
-       // Global state.
-       private static IToolkit toolkit;
-       private static IToolkitPrintingSystem printingSystem;
+       // Helper values to allow a user override prior to initialization
+       // with the Toolkit and PrintingSystem property setters.
+       // The initialization happens on first call to the getters of these
+       // properties,
+       // After that point no overriding is possible.
+       // NOTE: Due to possible inlining Setting the value to override with
+       // and accessing the getter in the same function might not give the
+       // desired result.
+       internal static IToolkit toolkitOverride;
+       internal static IToolkitPrintingSystem printingSystemOverride;
 
        public ToolkitManager() {}
 
@@ -41,21 +48,11 @@ public sealed class ToolkitManager
                        {
                                get
                                {
-                                       lock(typeof(ToolkitManager))
-                                       {
-                                               if(toolkit == null)
-                                               {
-                                                       toolkit = 
CreateDefaultToolkit();
-                                               }
-                                               return toolkit;
-                                       }
+                                       return ToolkitHandler.toolkit;
                                }
                                set
                                {
-                                       lock(typeof(ToolkitManager))
-                                       {
-                                               toolkit = value;
-                                       }
+                                       toolkitOverride = value;
                                }
                        }
 
@@ -64,28 +61,14 @@ public sealed class ToolkitManager
                        {
                                get
                                {
-                                       lock(typeof(ToolkitManager))
+                                       try
                                        {
-                                               return (toolkit != null);
+                                               return (ToolkitHandler.toolkit 
!= null);
+                                       }
+                                       catch (TypeInitializationException e)
+                                       {
+                                               return false;
                                        }
-                               }
-                       }
-
-       // Determine if this platform appears to be Unix-ish.
-       private static bool IsUnix()
-                       {
-                       #if !ECMA_COMPAT
-                               if(Environment.OSVersion.Platform != 
PlatformID.Unix)
-                       #else
-                               if(Path.DirectorySeparatorChar == '\\' ||
-                                  Path.AltDirectorySeparatorChar == '\\')
-                       #endif
-                               {
-                                       return false;
-                               }
-                               else
-                               {
-                                       return true;
                                }
                        }
 
@@ -94,29 +77,11 @@ public sealed class ToolkitManager
                        {
                                get
                                {
-                                       lock(typeof(ToolkitManager))
-                                       {
-                                               if(printingSystem == null)
-                                               {
-                                                       if(IsUnix())
-                                                       {
-                                                               printingSystem 
= new UnixPrintingSystem();
-                                                       }
-                                                       else
-                                                       {
-                                                               // TODO: Win32 
printing system.
-                                                               printingSystem 
= new NullPrintingSystem();
-                                                       }
-                                               }
-                                               return printingSystem;
-                                       }
+                                       return 
PrintingSystemHandler.printingSystem;
                                }
                                set
                                {
-                                       lock(typeof(ToolkitManager))
-                                       {
-                                               printingSystem = value;
-                                       }
+                                       printingSystemOverride = value;
                                }
                        }
 
@@ -221,89 +186,172 @@ public sealed class ToolkitManager
                                return new XorBrush(brush);
                        }
 
-       // Get the override toolkit name.
-       private static String GetToolkitOverride()
-                       {
-                               String name;
+       //
+       // This is a helper class for the active toolkit.
+       // It will be initialized on the first access to the
+       // ToolkitManager.Toolkit property getter.
+       //
+       private sealed class ToolkitHandler
+       {
+               public static IToolkit toolkit;
 
-                               // Search for "--toolkit" in the command-line 
options.
-                               String[] args = 
Environment.GetCommandLineArgs();
-                               int index;
-                               name = null;
-                               for(index = 1; index < args.Length; ++index)
+               static ToolkitHandler()
                                {
-                                       if(args[index] == "--toolkit")
+                                       if(ToolkitManager.toolkitOverride != 
null)
                                        {
-                                               if((index + 1) < args.Length)
-                                               {
-                                                       name = args[index + 1];
-                                                       break;
-                                               }
+                                               toolkit = 
ToolkitManager.toolkitOverride;
                                        }
-                                       else 
if(args[index].StartsWith("--toolkit="))
+                                       else
                                        {
-                                               name = 
args[index].Substring(10);
-                                               break;
+                                               toolkit = 
CreateDefaultToolkit();
                                        }
                                }
 
-                               // Check the environment next.
-                               if(name == null)
+               // Determine if this platform appears to be Unix-ish.
+               private static bool IsUnix()
                                {
-                                       name = 
Environment.GetEnvironmentVariable
-                                               ("PNET_WINFORMS_TOOLKIT");
+                               #if !ECMA_COMPAT || CONFIG_FRAMEWORK_2_0
+                                       return (Environment.OSVersion.Platform 
== PlatformID.Unix);
+                               #else
+                                       if(Path.DirectorySeparatorChar == '\\' 
||
+                                          Path.AltDirectorySeparatorChar == 
'\\')
+                                       {
+                                               return false;
+                                       }
+                                       else
+                                       {
+                                               return true;
+                                       }
+                               #endif
                                }
 
-                               // Bail out if no toolkit name specified.
-                               if(name == null || name.Length == 0)
+               // Get the override toolkit name.
+               private static String GetToolkitOverride()
                                {
-                                       return null;
+                                       String name;
+
+                                       // Search for "--toolkit" in the 
command-line options.
+                                       String[] args = 
Environment.GetCommandLineArgs();
+                                       int index;
+                                       name = null;
+                                       for(index = 1; index < args.Length; 
++index)
+                                       {
+                                               if(args[index] == "--toolkit")
+                                               {
+                                                       if((index + 1) < 
args.Length)
+                                                       {
+                                                               name = 
args[index + 1];
+                                                               break;
+                                                       }
+                                               }
+                                               else 
if(args[index].StartsWith("--toolkit="))
+                                               {
+                                                       name = 
args[index].Substring(10);
+                                                       break;
+                                               }
+                                       }
+
+                                       // Check the environment next.
+                                       if(name == null)
+                                       {
+                                               name = 
Environment.GetEnvironmentVariable
+                                                       
("PNET_WINFORMS_TOOLKIT");
+                                       }
+
+                                       // Bail out if no toolkit name 
specified.
+                                       if(name == null || name.Length == 0)
+                                       {
+                                               return null;
+                                       }
+
+                                       // Prepend "System.Drawing." if 
necessary.
+                                       if(name.IndexOf('.') == -1)
+                                       {
+                                               name = "System.Drawing." + name;
+                                       }
+                                       return name;
                                }
 
-                               // Prepend "System.Drawing." if necessary.
-                               if(name.IndexOf('.') == -1)
+               // Create the default toolkit.
+               private static IToolkit CreateDefaultToolkit()
                                {
-                                       name = "System.Drawing." + name;
-                               }
-                               return name;
+                               #if CONFIG_REFLECTION
+                                       // Determine the name of the toolkit we 
wish to use.
+                                       String name = GetToolkitOverride();
+                                       if(name == null)
+                                       {
+                                               if(IsUnix())
+                                               {
+                                                       name = 
"System.Drawing.Xsharp";
+                                               }
+                                               else
+                                               {
+                                                       name = 
"System.Drawing.Win32";
+                                               }
+                                       }
+
+                                       // Load the toolkit's assembly.
+                                       Assembly assembly = Assembly.Load(name);
+
+                                       // Find the 
"System.Drawing.Toolkit.DrawingToolkit" class.
+                                       Type type = assembly.GetType
+                                               
("System.Drawing.Toolkit.DrawingToolkit");
+                                       if(type == null)
+                                       {
+                                               throw new 
NotSupportedException();
+                                       }
+
+                                       // Instantiate "DrawingToolkit" and 
return it.
+                                       ConstructorInfo ctor = 
type.GetConstructor(new Type [0]);
+                                       return (IToolkit)(ctor.Invoke(new 
Object [0]));
+                               #else
+                                       return new NullToolkit();
+                               #endif
                        }
+       }
 
-       // Create the default toolkit.
-       private static IToolkit CreateDefaultToolkit()
-                       {
-                       #if CONFIG_REFLECTION
-                               // Determine the name of the toolkit we wish to 
use.
-                               String name = GetToolkitOverride();
-                               if(name == null)
+       private sealed class PrintingSystemHandler
+       {
+               public static IToolkitPrintingSystem printingSystem;
+
+               static PrintingSystemHandler()
                                {
-                                       if(IsUnix())
+                                       
if(ToolkitManager.printingSystemOverride != null)
                                        {
-                                               name = "System.Drawing.Xsharp";
+                                               printingSystem = 
ToolkitManager.printingSystemOverride;
                                        }
                                        else
                                        {
-                                               name = "System.Drawing.Win32";
+                                               if(IsUnix())
+                                               {
+                                                       printingSystem = new 
UnixPrintingSystem();
+                                               }
+                                               else
+                                               {
+                                                       // TODO: Win32 printing 
system.
+                                                       printingSystem = new 
NullPrintingSystem();
+                                               }
                                        }
                                }
 
-                               // Load the toolkit's assembly.
-                               Assembly assembly = Assembly.Load(name);
-
-                               // Find the 
"System.Drawing.Toolkit.DrawingToolkit" class.
-                               Type type = assembly.GetType
-                                       
("System.Drawing.Toolkit.DrawingToolkit");
-                               if(type == null)
+               // Determine if this platform appears to be Unix-ish.
+               private static bool IsUnix()
                                {
-                                       throw new NotSupportedException();
+                               #if !ECMA_COMPAT || CONFIG_FRAMEWORK_2_0
+                                       return (Environment.OSVersion.Platform 
== PlatformID.Unix);
+                               #else
+                                       if(Path.DirectorySeparatorChar == '\\' 
||
+                                          Path.AltDirectorySeparatorChar == 
'\\')
+                                       {
+                                               return false;
+                                       }
+                                       else
+                                       {
+                                               return true;
+                                       }
+                               #endif
                                }
-
-                               // Instantiate "DrawingToolkit" and return it.
-                               ConstructorInfo ctor = type.GetConstructor(new 
Type [0]);
-                               return (IToolkit)(ctor.Invoke(new Object [0]));
-                       #else
-                               return new NullToolkit();
-                       #endif
-                       }
+       }
 
 }; // class ToolkitManager
 

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

Summary of changes:
 ChangeLog                                |    5 +
 System.Drawing/Toolkit/ToolkitManager.cs |  270 ++++++++++++++++++------------
 2 files changed, 164 insertions(+), 111 deletions(-)


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




reply via email to

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