public void AcquireGlobalLock() { lock (_sync_mutex) { //wait untill global lock is released. if (_globalLockingContext != null && _globalLockingContext.IsCurrentContext) { _globalLockingContext.IncrementRefCount(); return; } while (_globalLock || _waiting4globalLock) { Monitor.Wait(_sync_mutex); } if (_lockTable.Count == 0) { //global lock is acquired. _globalLock = true; } else { _waiting4globalLock = true; _globalLockInfo = new LockInfo(); } } if (_globalLockInfo != null && !_globalLock) { _globalLockInfo.AddWaitingThread(); } _globalLockingContext = new LockingContext(); _globalLockingContext.IncrementRefCount(); }
public void AcquireLock(object key) { LockInfo info = null; lock (_sync_mutex) { while (_globalLock || _waiting4globalLock) { if (_globalLock && _globalLockingContext != null && _globalLockingContext.IsCurrentContext) { break; } Monitor.Wait(_sync_mutex); } if (!_lockTable.Contains(key)) { _lockTable.Add(key, new LockInfo()); } else { info = _lockTable[key] as LockInfo; } } if (info != null) { bool lockAcquired = info.AddWaitingThread(); //retry if (!lockAcquired) { AcquireLock(key); } } }