/// <summary> /// Releases the lock on account. /// </summary> /// <param name="accountName">Name of the account.</param> private void ExitLock(string accountName) { // Safety check if (accountName == null) { accountName = string.Empty; Log.Warn("(Exit) No account name"); } LockCount lockObj = null; lock (m_locks) { // Get lock object if (!m_locks.TryGetValue(accountName, out lockObj)) { Log.Error("(Exit) No lock object for account: '" + accountName + "'"); } // Remove lock object if no more locks on it if (lockObj != null) { if (--lockObj.count <= 0) { m_locks.Remove(accountName); } } } Monitor.Exit(lockObj); }
// Start is called before the first frame update void Start() { var lck = new LockCount(); lck.onLock = () => { Debug.Log("lock!"); }; lck.onUnlock = () => { Debug.Log("unlock!"); }; uint id; id = lck.Lock(); Debug.Assert(lck.Unlock(id)); Debug.Assert(!lck.Unlock(id)); id = lck.Lock(); Debug.Assert(lck.IsLocked()); Debug.Assert(lck.Count == 1); id = lck.Lock(); Debug.Assert(lck.IsLocked()); Debug.Assert(lck.Count == 2); }
/// <summary> /// Acquires the lock on account. /// </summary> /// <param name="accountName">Name of the account.</param> private void EnterLock(string accountName) { // Safety check if (accountName == null) { accountName = string.Empty; Log.Warn("(Enter) No account name"); } LockCount lockObj = null; lock (m_locks) { // Get/create lock object if (!m_locks.TryGetValue(accountName, out lockObj)) { lockObj = new LockCount(); m_locks.Add(accountName, lockObj); } if (lockObj == null) { Log.Error("(Enter) No lock object for account: '" + accountName + "'"); } else { // Increase count of locks lockObj.count++; } } if (lockObj != null) { Monitor.Enter(lockObj); } }