Exemplo n.º 1
0
        /// <summary>
        /// Gets the lock information.
        /// </summary>
        /// <param name="nameKey">The name key.</param>
        /// <returns></returns>
        private static ResourceLockInfo GetLockInfo(string nameKey)
        {
            _resourcesLock.EnterReadLock();
            try
            {
                if (_resources.ContainsKey(nameKey))
                {
                    var lockInfo = _resources[nameKey];
                    lockInfo.IncrementReferenceCount();

                    return(lockInfo);
                }
            }
            finally
            {
                _resourcesLock.ExitReadLock();
            }

            _resourcesLock.EnterWriteLock();
            try
            {
                ResourceLockInfo lockInfo;

                if (_resources.ContainsKey(nameKey))
                {
                    lockInfo = _resources[nameKey];
                    lockInfo.IncrementReferenceCount();

                    return(lockInfo);
                }

                lockInfo = new ResourceLockInfo(nameKey)
                {
                    Lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion)
                };
                lockInfo.IncrementReferenceCount();

                _resources.Add(nameKey, lockInfo);

                return(lockInfo);
            }
            finally
            {
                _resourcesLock.ExitWriteLock();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the lock information.
        /// </summary>
        /// <param name="nameKey">The name key.</param>
        /// <returns></returns>
        private static ResourceLockInfo GetLockInfo(string nameKey)
        {
            using (_resourcesLock.ReaderLock())
            {
                if (_resources.ContainsKey(nameKey))
                {
                    var lockInfo = _resources[nameKey];
                    lockInfo.IncrementReferenceCount();

                    return(lockInfo);
                }
            }

            using (_resourcesLock.WriterLock())
            {
                ResourceLockInfo lockInfo;

                if (_resources.ContainsKey(nameKey))
                {
                    lockInfo = _resources[nameKey];
                    lockInfo.IncrementReferenceCount();

                    return(lockInfo);
                }

                lockInfo = new ResourceLockInfo(nameKey)
                {
                    Lock = new AsyncReaderWriterLock()
                };
                lockInfo.IncrementReferenceCount();

                _resources.Add(nameKey, lockInfo);

                return(lockInfo);
            }
        }