From dd19011aa66c00977c4e18b959d8c317860cbb63 Mon Sep 17 00:00:00 2001 From: Ben Maurer Date: Thu, 26 May 2005 23:18:18 +0000 Subject: locking fixes svn path=/trunk/mcs/; revision=45088 --- .../System.ComponentModel/CategoryAttribute.cs | 35 +++++++++---------- .../System/System.ComponentModel/LicenseManager.cs | 22 ++++++------ .../System.Configuration/ConfigurationSettings.cs | 23 +++---------- mcs/class/System/System.IO/DefaultWatcher.cs | 13 ++++---- mcs/class/System/System.IO/FAMWatcher.cs | 37 ++++++++++---------- mcs/class/System/System.IO/FileSystemWatcher.cs | 3 +- mcs/class/System/System.IO/KeventWatcher.cs | 39 +++++++++++----------- mcs/class/System/System.IO/WindowsWatcher.cs | 1 + mcs/class/System/System.Net/DigestClient.cs | 18 ++++------ .../System/System.Net/GlobalProxySelection.cs | 9 +++-- .../System.ComponentModel/LicenseManagerTests.cs | 2 +- 11 files changed, 92 insertions(+), 110 deletions(-) (limited to 'mcs') diff --git a/mcs/class/System/System.ComponentModel/CategoryAttribute.cs b/mcs/class/System/System.ComponentModel/CategoryAttribute.cs index d15b1abbaae..e603dfd9ef3 100755 --- a/mcs/class/System/System.ComponentModel/CategoryAttribute.cs +++ b/mcs/class/System/System.ComponentModel/CategoryAttribute.cs @@ -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"); } diff --git a/mcs/class/System/System.ComponentModel/LicenseManager.cs b/mcs/class/System/System.ComponentModel/LicenseManager.cs index 77c0661308b..332e78f8b06 100644 --- a/mcs/class/System/System.ComponentModel/LicenseManager.cs +++ b/mcs/class/System/System.ComponentModel/LicenseManager.cs @@ -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; diff --git a/mcs/class/System/System.Configuration/ConfigurationSettings.cs b/mcs/class/System/System.Configuration/ConfigurationSettings.cs index b02def2f17c..fe8070ca4fe 100644 --- a/mcs/class/System/System.Configuration/ConfigurationSettings.cs +++ b/mcs/class/System/System.Configuration/ConfigurationSettings.cs @@ -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; } diff --git a/mcs/class/System/System.IO/DefaultWatcher.cs b/mcs/class/System/System.IO/DefaultWatcher.cs index bc4bca41edc..1a02cdb1b62 100644 --- a/mcs/class/System/System.IO/DefaultWatcher.cs +++ b/mcs/class/System/System.IO/DefaultWatcher.cs @@ -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) diff --git a/mcs/class/System/System.IO/FAMWatcher.cs b/mcs/class/System/System.IO/FAMWatcher.cs index c6d1a61aad8..47a5595e9b0 100644 --- a/mcs/class/System/System.IO/FAMWatcher.cs +++ b/mcs/class/System/System.IO/FAMWatcher.cs @@ -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) diff --git a/mcs/class/System/System.IO/FileSystemWatcher.cs b/mcs/class/System/System.IO/FileSystemWatcher.cs index e4018da1210..d3d421ccc21 100644 --- a/mcs/class/System/System.IO/FileSystemWatcher.cs +++ b/mcs/class/System/System.IO/FileSystemWatcher.cs @@ -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; diff --git a/mcs/class/System/System.IO/KeventWatcher.cs b/mcs/class/System/System.IO/KeventWatcher.cs index 803629effcd..d26c30e22f7 100644 --- a/mcs/class/System/System.IO/KeventWatcher.cs +++ b/mcs/class/System/System.IO/KeventWatcher.cs @@ -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) diff --git a/mcs/class/System/System.IO/WindowsWatcher.cs b/mcs/class/System/System.IO/WindowsWatcher.cs index e7b53592e12..dc5e848f13d 100644 --- a/mcs/class/System/System.IO/WindowsWatcher.cs +++ b/mcs/class/System/System.IO/WindowsWatcher.cs @@ -35,6 +35,7 @@ namespace System.IO { { } + // Locked by caller public static bool GetInstance (out IFileWatcher watcher) { throw new NotSupportedException (); diff --git a/mcs/class/System/System.Net/DigestClient.cs b/mcs/class/System/System.Net/DigestClient.cs index 208628cba48..da240e75db0 100644 --- a/mcs/class/System/System.Net/DigestClient.cs +++ b/mcs/class/System/System.Net/DigestClient.cs @@ -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; } } diff --git a/mcs/class/System/System.Net/GlobalProxySelection.cs b/mcs/class/System/System.Net/GlobalProxySelection.cs index 54ab7d5627c..07fb23ff5f3 100644 --- a/mcs/class/System/System.Net/GlobalProxySelection.cs +++ b/mcs/class/System/System.Net/GlobalProxySelection.cs @@ -36,7 +36,8 @@ namespace System.Net { public class GlobalProxySelection { - private static IWebProxy proxy; + volatile static IWebProxy proxy; + static readonly object lockobj = new object (); // Constructors public GlobalProxySelection() { } @@ -48,7 +49,7 @@ namespace System.Net if (proxy != null) return proxy; - lock (typeof (GlobalProxySelection)) { + lock (lockobj) { if (proxy != null) return proxy; @@ -68,9 +69,7 @@ namespace System.Net if (value == null) throw new ArgumentNullException ("GlobalProxySelection.Select", "null IWebProxy not allowed. Use GetEmptyWebProxy ()"); - - lock (typeof (GlobalProxySelection)) - proxy = value; + proxy = value; } } diff --git a/mcs/class/System/Test/System.ComponentModel/LicenseManagerTests.cs b/mcs/class/System/Test/System.ComponentModel/LicenseManagerTests.cs index f29d2b6af95..5af2c6cc4c1 100755 --- a/mcs/class/System/Test/System.ComponentModel/LicenseManagerTests.cs +++ b/mcs/class/System/Test/System.ComponentModel/LicenseManagerTests.cs @@ -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()); -- cgit v1.2.3