예제 #1
0
        /// <summary>
        /// Tries to acquire a shared read lock
        /// for the resource, and specifies a timeout for the lock. If this operation
        /// succeeds, the lock must be released as soon
        /// as possible through the <see cref="ReleaseReadLock"/>
        /// method.
        /// </summary>
        /// <returns>A lock that corresponds to the request. If the lock was denied, the
        /// returned item's <see cref="LockItem.LockType"/>
        /// is <see cref="ResourceLockType.Denied"/>.</returns>
        /// <param name="timeout">The timeout of the lock.
        /// A null value requests a lock that doesn't time out.</param>
        /// <returns>True if the lock was granted.</returns>
        /// <exception cref="ArgumentOutOfRangeException">In case of a negative
        /// timeout.</exception>
        public LockItem TryGetReadLock(TimeSpan?timeout)
        {
            lock (this)
            {
                if (HasWriteLock)
                {
                    return(LockItem.CreateDenied(ResourceId));
                }

                //clean up inactive locks from time to time
                Cleanup();

                var item = LockItem.CreateRead(ResourceId, timeout);
                readLocks.Add(item);
                return(item);
            }
        }
 /// <summary>
 /// Tries to acquire an exclusive write lock for the resource
 /// which expires after a given time span in order not to block
 /// the resource indefinitely.
 /// If this operation succeeds, the lock must be released as soon
 /// as possible through the <see cref="IResourceLockRepository.ReleaseReadLock"/>
 /// method.
 /// </summary>
 /// <param name="resourceId">Identifies the locked resource.</param>
 /// <param name="timeout">The specified expiration timeout from now.
 /// Allowed values are null for indefinite locking, or any positive value.</param>
 /// <returns>True if the lock was granted.</returns>
 /// <exception cref="ArgumentOutOfRangeException">In case of a negative
 /// timeout.</exception>
 public LockItem TryGetReadLock(string resourceId, TimeSpan?timeout)
 {
     return(LockItem.CreateRead(resourceId, timeout));
 }
 /// <summary>
 /// Tries to acquire a shared read lock for a given resource
 /// which never expires.
 /// If this operation succeeds, the lock must be released as soon
 /// as possible through the <see cref="IResourceLockRepository.ReleaseReadLock"/>
 /// method.
 /// </summary>
 /// <returns>A <see cref="LockItem"/> instance which represents
 /// the acquired lock, if any. If no lock was granted, the
 /// returned item's <see cref="LockItem.LockType"/> property
 /// returns <see cref="ResourceLockType.Denied"/>.</returns>
 public LockItem TryGetReadLock(string resourceId)
 {
     return(LockItem.CreateRead(resourceId, null));
 }
 /// <summary>
 /// Gets a guard that can be used with a <c>using</c>
 /// statement, which tries to get a read lock and
 /// ensures the lock is being released as soon as
 /// the using block is being exited. Check the
 /// guard's <see cref="ResourceLockGuard.IsLockEnabled"/>
 /// property to verify whether the lock was granted
 /// or not.
 /// </summary>
 /// <returns>A guard that handles management of the
 /// acquired read lock.</returns>
 public ResourceLockGuard GetReadGuard(string resourceId)
 {
     return(new ResourceLockGuard(LockItem.CreateRead(resourceId, null), this));
 }