private void ReleaseLockIfUnused(ISynchronizableLibraryRepository repository, CountableReaderWriterLockSlim repositoryLock)
		{
			if (repositoryLock.ReadLockCount == 0 && repositoryLock.WriteLockCount == 0)
			{
				_repositoryLocks.Remove(repository.Id);
				repositoryLock.Dispose();
			}
		}
 private void ReleaseLockIfUnused(ISynchronizableLibraryRepository repository, CountableReaderWriterLockSlim repositoryLock)
 {
     if (repositoryLock.ReadLockCount == 0 && repositoryLock.WriteLockCount == 0)
     {
         _repositoryLocks.Remove(repository.Id);
         repositoryLock.Dispose();
     }
 }
		public void BeginRead(ISynchronizableLibraryRepository repository)
		{
			CountableReaderWriterLockSlim repositoryLock;
			lock (_repositoryLocks)
			{
				repositoryLock = _repositoryLocks.GetOrAdd(repository.Id, repositoryDescriptor => new CountableReaderWriterLockSlim());
				repositoryLock.ReadLockCount++;
			}
			repositoryLock.EnterReadLock();
		}
        public void BeginRead(ISynchronizableLibraryRepository repository)
        {
            CountableReaderWriterLockSlim repositoryLock;

            lock (_repositoryLocks)
            {
                repositoryLock = _repositoryLocks.GetOrAdd(repository.Id, repositoryDescriptor => new CountableReaderWriterLockSlim());
                repositoryLock.ReadLockCount++;
            }
            repositoryLock.EnterReadLock();
        }
		public void EndWrite(ISynchronizableLibraryRepository repository)
		{
			lock (_repositoryLocks)
			{
				if (_repositoryLocks.ContainsKey(repository.Id) && _repositoryLocks[repository.Id].WriteLockCount > 0)
				{
					var repositoryLock = _repositoryLocks[repository.Id];
					repositoryLock.WriteLockCount--;
					repositoryLock.ExitWriteLock();
					ReleaseLockIfUnused(repository, repositoryLock);
				}
			}
		}
 public void EndWrite(ISynchronizableLibraryRepository repository)
 {
     lock (_repositoryLocks)
     {
         if (_repositoryLocks.ContainsKey(repository.Id) && _repositoryLocks[repository.Id].WriteLockCount > 0)
         {
             var repositoryLock = _repositoryLocks[repository.Id];
             repositoryLock.WriteLockCount--;
             repositoryLock.ExitWriteLock();
             ReleaseLockIfUnused(repository, repositoryLock);
         }
     }
 }
		public bool TryBeginWrite(ISynchronizableLibraryRepository repository)
		{
			CountableReaderWriterLockSlim repositoryLock;
			lock (_repositoryLocks)
			{
				if (_repositoryLocks.ContainsKey(repository.Id))
				{
					repositoryLock = _repositoryLocks[repository.Id];
					if (repositoryLock.WriteLockCount > 0)
					{
						return false;
					}
				}
				else
				{
					repositoryLock = new CountableReaderWriterLockSlim();
					_repositoryLocks.Add(repository.Id, repositoryLock);
				}
				repositoryLock.WriteLockCount++;
			}
			repositoryLock.EnterWriteLock();
			return true;
		}
        public bool TryBeginWrite(ISynchronizableLibraryRepository repository)
        {
            CountableReaderWriterLockSlim repositoryLock;

            lock (_repositoryLocks)
            {
                if (_repositoryLocks.ContainsKey(repository.Id))
                {
                    repositoryLock = _repositoryLocks[repository.Id];
                    if (repositoryLock.WriteLockCount > 0)
                    {
                        return(false);
                    }
                }
                else
                {
                    repositoryLock = new CountableReaderWriterLockSlim();
                    _repositoryLocks.Add(repository.Id, repositoryLock);
                }
                repositoryLock.WriteLockCount++;
            }
            repositoryLock.EnterWriteLock();
            return(true);
        }