public LockHandle Lock(ILockable[] toWrite, ILockable[] toRead, LockingMode mode) { // Set up the local constants. int lockCount = toRead.Length + toWrite.Length; LockHandle handle = new LockHandle(lockCount); lock (this) { Lock @lock; LockingQueue queue; // Add Read and Write locks to cache and to the handle. for (int i = toWrite.Length - 1; i >= 0; --i) { var toWriteLock = toWrite[i]; queue = GetQueueFor(toWriteLock); // slightly confusing: this will add Lock to given table queue @lock = new Lock(queue, mode, AccessType.Write); @lock.Acquire(); handle.AddLock(@lock); } for (int i = toRead.Length - 1; i >= 0; --i) { var toReadLock = toRead[i]; queue = GetQueueFor(toReadLock); // slightly confusing: this will add Lock to given table queue @lock = new Lock(queue, mode, AccessType.Read); @lock.Acquire(); handle.AddLock(@lock); } } return handle; }
internal void AddLock(Lock @lock) { locks[lockIndex++] = @lock; }
void ILockable.Released(Lock @lock) { }
void ILockable.Acquired(Lock @lock) { }
protected virtual void OnLockReleased(Lock @lock) { }
protected virtual void OnLockAcquired(Lock @lock) { }
void ILockable.Released(Lock @lock) { try { OnLockReleased(@lock); } finally { IsLocked = true; } }
void ILockable.Acquired(Lock @lock) { try { OnLockAcquired(@lock); } finally { IsLocked = true; } }
private void AddToHandle(LockHandle handle, ILockable[] lockables, AccessType accessType, LockingMode mode) { if (lockables == null) return; for (int i = lockables.Length - 1; i >= 0; --i) { var lockable = lockables[i]; var queue = GetQueueFor(lockable); // slightly confusing: this will add Lock to given table queue var @lock = new Lock(queue, mode, accessType); @lock.Acquire(); handle.AddLock(@lock); } }