#region * License * /* SimpleHelpers - NamedLock Copyright © 2013 Khalid Salomão Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. License: http://www.opensource.org/licenses/mit-license.php Website: https://github.com/khalidsalomao/SimpleHelpers.Net */ #endregion using System; namespace SimpleHelpers { /// /// Synchronization helper: a static lock collection associated with a key. /// NamedLock manages the lifetime of critical sections that can be accessed by a key (name) throughout the application. /// It also have some helper methods to allow a maximum wait time (timeout) to acquire the lock and safely release it. /// Note: this nuget package contains C# source code and depends on System.Collections.Concurrent introduced in .Net 4.0. /// /// /// // create a lock for this key /// using (var padlock = new NamedLock (key)) /// { /// if (padlock.Enter (TimeSpan.FromMilliseconds (100))) /// { /// // do something /// } /// else /// { /// // do some other thing /// } /// } /// public class NamedLock : IDisposable { #region * Internal static methods * private static readonly System.Collections.Concurrent.ConcurrentDictionary m_waitLock = new System.Collections.Concurrent.ConcurrentDictionary (StringComparer.Ordinal); private static object GetOrAdd (string key) { CountedLock padlock = m_waitLock.GetOrAdd (key, LockFactory); padlock.Increment (); return padlock; } private static void ReleaseOrRemove (string key) { CountedLock padlock; if (m_waitLock.TryGetValue (key, out padlock)) { if (padlock.Decrement () <= 0) m_waitLock.TryRemove (key, out padlock); } } private static CountedLock LockFactory (string key) { return new CountedLock (); } class CountedLock { private int m_counter = 0; public int Increment () { return System.Threading.Interlocked.Increment (ref m_counter); } public int Decrement () { return System.Threading.Interlocked.Decrement (ref m_counter); } } #endregion #region * Internal variables & properties * private string m_key; private object m_padlock; private bool m_locked = false; /// /// Check if a lock was acquired. /// public bool IsLocked { get { return m_locked; } } /// /// Gets the lock key name. /// public string Key { get { return m_key; } } /// /// Gets the internal lock object. /// public object Lock { get { return m_padlock; } } #endregion #region * Constructor & finalizer * /// /// Initializes a new instance of the class. /// /// The named lock key. public NamedLock (string key) { m_key = key; m_padlock = GetOrAdd (m_key); } /// /// Performs application-defined tasks associated with freeing, releasing, /// or resetting unmanaged resources. /// Releases acquired lock and related resources. /// public void Dispose () { Exit (); ReleaseOrRemove (m_key); } #endregion #region * Internal variables & properties * /// /// Tries to acquire a lock. /// public bool Enter () { if (!m_locked) { System.Threading.Monitor.Enter (m_padlock, ref m_locked); } return m_locked; } /// /// Tries to acquire a lock respecting the specified timeout. /// /// The wait timeout milliseconds. /// If the lock was acquired in the specified timeout public bool Enter (int waitTimeoutMilliseconds) { if (!m_locked) { System.Threading.Monitor.TryEnter (m_padlock, waitTimeoutMilliseconds, ref m_locked); } return m_locked; } /// /// Tries to acquire a lock respecting the specified timeout. /// /// The wait timeout. /// If the lock was acquired in the specified timeout public bool Enter (TimeSpan waitTimeout) { return Enter ((int)waitTimeout.TotalMilliseconds); } /// /// Releases the lock if it was already acquired. /// Called also at "Dispose". /// public bool Exit () { if (m_locked) { m_locked = false; System.Threading.Monitor.Exit (m_padlock); } return false; } #endregion #region * Factory methods * /// /// Creates a new instance and tries to acquire a lock. /// /// The named lock key. public static NamedLock CreateAndEnter (string key) { NamedLock item; item = new NamedLock (key); item.Enter (); return item; } /// /// Creates a new instance and tries to acquire a lock. /// /// The named lock key. /// The wait timeout milliseconds. public static NamedLock CreateAndEnter (string key, int waitTimeoutMilliseconds) { NamedLock item; item = new NamedLock (key); item.Enter (waitTimeoutMilliseconds); return item; } /// /// Creates a new instance and tries to acquire a lock. /// /// The named lock key. /// The wait timeout. public static NamedLock CreateAndEnter (string key, TimeSpan waitTimeout) { return CreateAndEnter (key, (int)waitTimeout.TotalMilliseconds); } #endregion } }