/// <summary> /// Tries to acquire an exclusive write /// 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="ReleaseWriteLock"/> /// method. /// </summary> /// <param name="timeout">The timeout of the lock. /// A value of null requests a lock that doesn't time out.</param> /// <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> /// <exception cref="ArgumentOutOfRangeException">In case of a negative /// timeout.</exception> public LockItem TryGetWriteLock(TimeSpan?timeout) { lock (this) { if (HasWriteLock || HasReadLocks) { return(LockItem.CreateDenied(ResourceId)); } activeWriteLock = LockItem.CreateWrite(ResourceId, timeout); return(activeWriteLock); } }
/// <summary> /// Tries to acquire an exclusive write lock for the resource /// which expires after a given while 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.ReleaseWriteLock"/> /// 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>sitive value.</param> /// <returns>True if the lock was granted.</returns> public LockItem TryGetWriteLock(string resourceId, TimeSpan?timeout) { return(LockItem.CreateWrite(resourceId, timeout)); }
/// <summary> /// Tries to acquire an exclusive write 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.ReleaseWriteLock"/> /// 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 TryGetWriteLock(string resourceId) { return(LockItem.CreateWrite(resourceId, null)); }
/// <summary> /// Gets a guard that can be used with a <c>using</c> /// statement, which tries to get a write 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 write lock.</returns> public ResourceLockGuard GetWriteGuard(string resourceId) { return(new ResourceLockGuard(LockItem.CreateWrite(resourceId, null), this)); }