locking fixes
authorBen Maurer <benm@mono-cvs.ximian.com>
Thu, 26 May 2005 23:18:18 +0000 (23:18 -0000)
committerBen Maurer <benm@mono-cvs.ximian.com>
Thu, 26 May 2005 23:18:18 +0000 (23:18 -0000)
svn path=/trunk/mcs/; revision=45088

mcs/class/System/System.ComponentModel/CategoryAttribute.cs
mcs/class/System/System.ComponentModel/LicenseManager.cs
mcs/class/System/System.Configuration/ConfigurationSettings.cs
mcs/class/System/System.IO/DefaultWatcher.cs
mcs/class/System/System.IO/FAMWatcher.cs
mcs/class/System/System.IO/FileSystemWatcher.cs
mcs/class/System/System.IO/KeventWatcher.cs
mcs/class/System/System.IO/WindowsWatcher.cs
mcs/class/System/System.Net/DigestClient.cs
mcs/class/System/System.Net/GlobalProxySelection.cs
mcs/class/System/Test/System.ComponentModel/LicenseManagerTests.cs

index d15b1abbaae6d956bd58f52348e21e59bc31f34c..e603dfd9ef37647df1183c778733908fb25115f0 100755 (executable)
@@ -37,12 +37,13 @@ namespace System.ComponentModel {
        public class CategoryAttribute : Attribute
        {
                private string category;
-               private bool IsLocalized = false;
+               private bool IsLocalized;
 
-               static CategoryAttribute action, appearance, behaviour, data,   def;
-               static CategoryAttribute design, drag_drop,  focus,     format, key;
-               static CategoryAttribute layout, mouse,      window_style;
+               static volatile CategoryAttribute action, appearance, behaviour, data,   def;
+               static volatile CategoryAttribute design, drag_drop,  focus,     format, key;
+               static volatile CategoryAttribute layout, mouse,      window_style;
 
+               static object lockobj = new object ();
 
                public CategoryAttribute ()
                {
@@ -60,7 +61,7 @@ namespace System.ComponentModel {
                                if (action != null)
                                        return action;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (action == null)
                                                action = new CategoryAttribute ("Action");
                                }
@@ -73,7 +74,7 @@ namespace System.ComponentModel {
                                if (appearance != null)
                                        return appearance;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (appearance == null)
                                                appearance = new CategoryAttribute ("Appearance");
                                }
@@ -86,7 +87,7 @@ namespace System.ComponentModel {
                                if (behaviour != null)
                                        return behaviour;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (behaviour == null)
                                                behaviour = new CategoryAttribute ("Behavior");
                                }
@@ -99,7 +100,7 @@ namespace System.ComponentModel {
                                if (data != null)
                                        return data;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (data == null)
                                                data = new CategoryAttribute ("Data");
                                }
@@ -112,7 +113,7 @@ namespace System.ComponentModel {
                                if (def != null)
                                        return def;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (def == null)
                                                def = new CategoryAttribute ();
                                }
@@ -125,7 +126,7 @@ namespace System.ComponentModel {
                                if (design != null)
                                        return design;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (design == null)
                                                design = new CategoryAttribute ("Design");
                                }
@@ -138,7 +139,7 @@ namespace System.ComponentModel {
                                if (drag_drop != null)
                                        return drag_drop;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (drag_drop == null)
                                                drag_drop = new CategoryAttribute ("Drag Drop");
                                }
@@ -151,7 +152,7 @@ namespace System.ComponentModel {
                                if (focus != null)
                                        return focus;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (focus == null)
                                                focus = new CategoryAttribute ("Focus");
                                }
@@ -164,7 +165,7 @@ namespace System.ComponentModel {
                                if (format != null)
                                        return format;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (format == null)
                                                format = new CategoryAttribute ("Format");
                                }
@@ -177,7 +178,7 @@ namespace System.ComponentModel {
                                if (key != null)
                                        return key;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (key == null)
                                                key = new CategoryAttribute ("Key");
                                }
@@ -190,7 +191,7 @@ namespace System.ComponentModel {
                                if (layout != null)
                                        return layout;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (layout == null)
                                                layout = new CategoryAttribute ("Layout");
                                }
@@ -203,7 +204,7 @@ namespace System.ComponentModel {
                                if (mouse != null)
                                        return mouse;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (mouse == null)
                                                mouse = new CategoryAttribute ("Mouse");
                                }
@@ -216,7 +217,7 @@ namespace System.ComponentModel {
                                if (window_style != null)
                                        return window_style;
 
-                               lock (typeof (CategoryAttribute)) {
+                               lock (lockobj) {
                                        if (window_style == null)
                                                window_style = new CategoryAttribute ("Window Style");
                                }
index 77c0661308b4a5806ec848069e9d25a53da87a82..332e78f8b0606af2ee718fb49705395c9e3aff6b 100644 (file)
@@ -36,8 +36,10 @@ namespace System.ComponentModel
 {
        public sealed class LicenseManager
        {
-               private static LicenseContext mycontext = null;
-               private static object contextLockUser = null;
+               static LicenseContext mycontext;
+               static object contextLockUser;
+               
+               static object lockObject = new object ();
 
                private LicenseManager ()
                {
@@ -45,7 +47,7 @@ namespace System.ComponentModel
 
                public static LicenseContext CurrentContext {
                        get {
-                               lock (typeof(LicenseManager)) {
+                               lock (lockObject) {
                                        //Tests indicate a System.ComponentModel.Design.RuntimeLicenseContext should be returned.
                                        if  (mycontext==null)
                                                mycontext = new Design.RuntimeLicenseContext();
@@ -53,7 +55,7 @@ namespace System.ComponentModel
                                }
                        } 
                        set { 
-                               lock (typeof(LicenseManager)) {
+                               lock (lockObject) {
                                        if (contextLockUser==null) {
                                                mycontext = value;
                                        } else {
@@ -80,17 +82,17 @@ namespace System.ComponentModel
                                                        object[] args)
                {
                        object newObject = null;
-                       lock (typeof (LicenseManager)) {
-                               object lockObject = new object ();
+                       lock (lockObject) {
+                               object contextUser = new object ();
                                LicenseContext oldContext = CurrentContext;
                                CurrentContext = creationContext;
-                               LockContext (lockObject);
+                               LockContext (contextUser);
                                try {
                                        newObject = Activator.CreateInstance (type, args);
                                } catch (Reflection.TargetInvocationException exception) {
                                        throw exception.InnerException;
                                } finally {
-                                       UnlockContext (lockObject);
+                                       UnlockContext (contextUser);
                                        CurrentContext = oldContext;
                                }
                        }
@@ -131,14 +133,14 @@ namespace System.ComponentModel
 
                public static void LockContext (object contextUser)
                {
-                       lock (typeof (LicenseManager)) {
+                       lock (lockObject) {
                                contextLockUser = contextUser;
                        }
                }
 
                public static void UnlockContext (object contextUser)
                {
-                       lock (typeof(LicenseManager)) {
+                       lock (lockObject) {
                                //Ignore if we're not locked
                                if (contextLockUser == null)
                                        return;
index b02def2f17c4a5462ce9a282d2f69fb58622289b..fe8070ca4fed26d427495c15c792a8ffbe8d5801 100644 (file)
@@ -46,19 +46,14 @@ namespace System.Configuration
 {
        public sealed class ConfigurationSettings
        {
-               static IConfigurationSystem config;
-                       
+               static IConfigurationSystem config = DefaultConfig.GetInstance ();
+               static object lockobj = new object ();
                private ConfigurationSettings ()
                {
                }
 
                public static object GetConfig (string sectionName)
                {
-                       lock (typeof (ConfigurationSettings)) {
-                               if (config == null)
-                                       config = DefaultConfig.GetInstance ();
-                       }
-
                        return config.GetConfig (sectionName);
                }
 
@@ -98,10 +93,7 @@ namespace System.Configuration
                        if (newSystem == null)
                                throw new ArgumentNullException ("newSystem");
 
-                       lock (typeof (ConfigurationSettings)) {
-                               if (config == null)
-                                       config = DefaultConfig.GetInstance ();
-
+                       lock (lockobj) {
                                IConfigurationSystem old = config;
                                config = newSystem;
                                return old;
@@ -115,20 +107,15 @@ namespace System.Configuration
        //
        class DefaultConfig : IConfigurationSystem
        {
-               static DefaultConfig instance;
+               static readonly DefaultConfig instance = new DefaultConfig ();
                ConfigurationData config;
-
+               
                private DefaultConfig ()
                {
                }
 
                public static DefaultConfig GetInstance ()
                {
-                       lock (typeof (DefaultConfig)) {
-                               if (instance == null)
-                                       instance = new DefaultConfig ();
-                       }
-
                        return instance;
                }
 
index bc4bca41edc747213a208f0d95363f780de937a5..1a02cdb1b62641097b83bcaf82afae2ed45c69e4 100644 (file)
@@ -63,18 +63,17 @@ namespace System.IO {
                {
                }
                
+               // Locked by caller
                public static bool GetInstance (out IFileWatcher watcher)
                {
-                       lock (typeof (DefaultWatcher)) {
-                               if (instance != null) {
-                                       watcher = instance;
-                                       return true;
-                               }
-
-                               instance = new DefaultWatcher ();
+                       if (instance != null) {
                                watcher = instance;
                                return true;
                        }
+
+                       instance = new DefaultWatcher ();
+                       watcher = instance;
+                       return true;
                }
                
                public void StartDispatching (FileSystemWatcher fsw)
index c6d1a61aad8335067f2e386adb0464e25d818b70..47a5595e9b04be9cdfa153d04cb18c8f4618ba43 100644 (file)
@@ -82,31 +82,30 @@ namespace System.IO {
                {
                }
                
+               // Locked by caller
                public static bool GetInstance (out IFileWatcher watcher)
                {
-                       lock (typeof (FAMWatcher)) {
-                               if (failed == true) {
-                                       watcher = null;
-                                       return false;
-                               }
-
-                               if (instance != null) {
-                                       watcher = instance;
-                                       return true;
-                               }
-
-                               watches = Hashtable.Synchronized (new Hashtable ());
-                               requests = Hashtable.Synchronized (new Hashtable ());
-                               if (FAMOpen (out conn) == -1) {
-                                       failed = true;
-                                       watcher = null;
-                                       return false;
-                               }
+                       if (failed == true) {
+                               watcher = null;
+                               return false;
+                       }
 
-                               instance = new FAMWatcher ();
+                       if (instance != null) {
                                watcher = instance;
                                return true;
                        }
+
+                       watches = Hashtable.Synchronized (new Hashtable ());
+                       requests = Hashtable.Synchronized (new Hashtable ());
+                       if (FAMOpen (out conn) == -1) {
+                               failed = true;
+                               watcher = null;
+                               return false;
+                       }
+
+                       instance = new FAMWatcher ();
+                       watcher = instance;
+                       return true;
                }
                
                public void StartDispatching (FileSystemWatcher fsw)
index e4018da121066702c4b881f7e59770a51b775630..d3d421ccc21e40ba9d2e166e9914c0156a2fcc27 100644 (file)
@@ -57,6 +57,7 @@ namespace System.IO {
                bool disposed;
                string mangledFilter;
                static IFileWatcher watcher;
+               static object lockobj = new object ();
 
                #endregion // Fields
 
@@ -104,7 +105,7 @@ namespace System.IO {
 
                void InitWatcher ()
                {
-                       lock (typeof (FileSystemWatcher)) {
+                       lock (lockobj) {
                                if (watcher != null)
                                        return;
 
index 803629effcdc3f7e2f31c405466e92e896687d7c..d26c30e22f7935e391891119397ce1a9449770fb 100644 (file)
@@ -93,32 +93,31 @@ namespace System.IO {
                {
                }
                
+               // Locked by caller
                public static bool GetInstance (out IFileWatcher watcher)
                {
-                       lock (typeof (KeventWatcher)) {
-                               if (failed == true) {
-                                       watcher = null;
-                                       return false;
-                               }
-
-                               if (instance != null) {
-                                       watcher = instance;
-                                       return true;
-                               }
-
-                               watches = Hashtable.Synchronized (new Hashtable ());
-                               requests = Hashtable.Synchronized (new Hashtable ());
-                               conn = kqueue();
-                               if (conn == -1) {
-                                       failed = true;
-                                       watcher = null;
-                                       return false;
-                               }
+                       if (failed == true) {
+                               watcher = null;
+                               return false;
+                       }
 
-                               instance = new KeventWatcher ();
+                       if (instance != null) {
                                watcher = instance;
                                return true;
                        }
+
+                       watches = Hashtable.Synchronized (new Hashtable ());
+                       requests = Hashtable.Synchronized (new Hashtable ());
+                       conn = kqueue();
+                       if (conn == -1) {
+                               failed = true;
+                               watcher = null;
+                               return false;
+                       }
+
+                       instance = new KeventWatcher ();
+                       watcher = instance;
+                       return true;
                }
                
                public void StartDispatching (FileSystemWatcher fsw)
index e7b53592e122e5c2d7075759cbfb592018202ba0..dc5e848f13db29ba6de6a3c38a5eca2be1c14ed5 100644 (file)
@@ -35,6 +35,7 @@ namespace System.IO {
                {
                }
                
+               // Locked by caller
                public static bool GetInstance (out IFileWatcher watcher)
                {
                        throw new NotSupportedException ();
index 208628cba486fe8f1cb2fd81b9cebeea9a1c280b..da240e75db093d897615e12a21fd4072c734179d 100644 (file)
@@ -360,21 +360,15 @@ namespace System.Net
        class DigestClient : IAuthenticationModule
        {
 
-               static Hashtable cache;
-
-               public DigestClient () {}
-
+               static readonly Hashtable cache = Hashtable.Synchronized (new Hashtable ());
+               
                static Hashtable Cache {
                        get {
-                               lock (typeof (DigestClient)) {
-                                       if (cache == null) {
-                                               cache = Hashtable.Synchronized (new Hashtable ());
-                                       } else {
-                                               CheckExpired (cache.Count);
-                                       }
-
-                                       return cache;
+                               lock (cache.SyncRoot) {
+                                       CheckExpired (cache.Count);
                                }
+                               
+                               return cache;
                        }
                }
 
index 54ab7d5627c4792034861f462b457ed4978df4b7..07fb23ff5f3ffc71b9fcbe8e3f20fb3b8f3837ab 100644 (file)
@@ -36,7 +36,8 @@ namespace System.Net
 {\r
        public class GlobalProxySelection\r
        {\r
-               private static IWebProxy proxy;\r
+               volatile static IWebProxy proxy;
+               static readonly object lockobj = new object ();\r
                \r
                // Constructors\r
                public GlobalProxySelection() { }\r
@@ -48,7 +49,7 @@ namespace System.Net
                        if (proxy != null)\r
                                return proxy;\r
 \r
-                       lock (typeof (GlobalProxySelection)) {\r
+                       lock (lockobj) {\r
                                if (proxy != null)\r
                                        return proxy;\r
 \r
@@ -68,9 +69,7 @@ namespace System.Net
                                if (value == null)\r
                                        throw new ArgumentNullException ("GlobalProxySelection.Select",\r
                                                        "null IWebProxy not allowed. Use GetEmptyWebProxy ()");\r
-\r
-                               lock (typeof (GlobalProxySelection))\r
-                                       proxy = value; \r
+                               proxy = value; \r
                        }\r
                }\r
                \r
index f29d2b6af9549c893c7d917a81228a35d713b873..5af2c6cc4c12e8aa52e4550402ccbf7cf5d4c366 100755 (executable)
@@ -131,7 +131,7 @@ namespace MonoTests.System.ComponentModel
 #endif
                public void Test () 
                {
-                       object lockObject = typeof (LicenseManager);
+                       object lockObject = new object ();
                        //**DEFAULT CONTEXT & LicenseUsageMode**
                        //Get CurrentContext, check default type
                        AssertEquals ("LicenseManager #1", "System.ComponentModel.Design.RuntimeLicenseContext", LicenseManager.CurrentContext.GetType().ToString());