Esempio n. 1
0
 /// <summary>
 /// Try to get the resource until earliest of following happens
 ///     1- get it successfully,
 ///     2- cancellation requested via <paramref name="token"/>
 ///     3- time specified by <paramref name="timeout"/> exceeded
 /// </summary>
 /// <param name="timeout">how long should we wait?  Null indicates potential infinite wait .</param>
 /// <param name="token">token by which another thread can cancel the attempt to obtain resource</param>
 /// <param name="mode">acquisition mode</param>
 /// <returns>the resource</returns>
 /// <exception cref="ArgumentOutOfRangeException">non-null, non-positive <paramref name="timeout"/> argument; OR mode not
 /// a defined value of the <see cref="AcquisitionMode"/> <see langword="enum"/></exception>
 /// <exception cref="TimeoutException">didn't obtain it within time specified by <paramref name="timeout"/></exception>
 /// <exception cref="OperationCanceledException">operation was cancelled</exception>
 /// <exception cref="ObjectDisposedException">the object was disposed</exception>
 /// <exception cref="LockAlreadyHeldThreadException">the thread attempting to obtain the lock already holds the lock.</exception>
 /// <remarks>After method returns value, you are responsible for disposal until passing to ultimate user behind a method whose return
 /// value is annotated by the <see cref="UsingMandatoryAttribute"/>.  This means you must dispose of it yourself in all failure/exceptional
 /// cases after this method returns a value but before ultimately passed to user.</remarks>
 protected RwVaultInternalLockedResource ExecuteGetInternalLockedResource(TimeSpan?timeout, CancellationToken token, AcquisitionMode mode)
 {
     ThrowIfDisposingOrDisposed();
     if (timeout.HasValue && timeout.Value <= TimeSpan.Zero)
     {
         throw new ArgumentOutOfRangeException(nameof(timeout), timeout, @"Must be positive.");
     }
     return(RwVaultInternalLockedResource.CreateInternalLockedResource(this, timeout, token, mode.ValueOrThrowIfNDef()));
 }
Esempio n. 2
0
 /// <summary>
 /// Get the protected resource in writable mode during execution of the dispose routine
 /// </summary>
 /// <param name="timeOut">the timeout</param>
 /// <returns>the resource</returns>
 /// <exception cref="TimeoutException">unable to secure resource within period specified by the
 /// <paramref name="timeOut"/> parameter.</exception>
 /// <exception cref="LockAlreadyHeldThreadException">the lock is already held by the calling thread</exception>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="timeOut"/> was not positive.</exception>
 protected RwVaultInternalLockedResource ExecuteGetInternalLockedResourceDuringDispose(TimeSpan timeOut)
 {
     ThrowIfNotDisposing();
     if (timeOut <= TimeSpan.Zero)
     {
         throw new ArgumentOutOfRangeException(nameof(timeOut), timeOut, @"Must be positive.");
     }
     return(RwVaultInternalLockedResource.CreateInternalLockedResource(this, timeOut, CancellationToken.None,
                                                                       AcquisitionMode.ReadWrite, true));
 }
            //internal static InternalCustomRoLockedResource<TVault> CreateRoLockedResource([NotNull] TVault v,
            //    [NotNull] Box b)
            //{
            //    Func<ReadWriteVault<TResource>, Box, AcquisitionMode, Box> releaseMethod =
            //        ReadWriteVault<TResource>.ReleaseResourceMethod;
            //    return new InternalCustomRoLockedResource<TVault>(v, b, releaseMethod);
            //}

            internal static InternalCustomRoLockedResource <TVault> CreateRoLockedResource(ref RwVaultInternalLockedResource internalLock, [NotNull] TVault v)
            {
                if (!internalLock.IsGood)
                {
                    throw new ArgumentException(@"Locked resource received is not good.", nameof(internalLock));
                }
                Func <ReadWriteVault <TResource>, Box, AcquisitionMode, Box> releaseMethod =
                    ReadWriteVault <TResource> .ReleaseResourceMethod;

                return(new InternalCustomRoLockedResource <TVault>(v, internalLock.Release(), releaseMethod));
            }
            internal static InternalCustomRwLockedResource <TVault> CreateWritableLockedResource(ref RwVaultInternalLockedResource ilr,
                                                                                                 [NotNull] TVault v)
            {
                if (v == null)
                {
                    throw new ArgumentNullException(nameof(v));
                }
                Func <ReadWriteVault <TResource>, Box, AcquisitionMode, Box> releaseMethod =
                    ReadWriteVault <TResource> .ReleaseResourceMethod;

                return(new InternalCustomRwLockedResource <TVault>(v, ilr.Release(), releaseMethod));
            }
Esempio n. 5
0
 /// <summary>
 /// Try to get the locked resource.  This thread will block (potentially forever)
 /// until the resource is obtained or an exception is thrown.
 /// </summary>
 /// <returns>The locked resource</returns>
 /// <exception cref="ObjectDisposedException">object was disposed</exception>
 /// <exception cref="LockAlreadyHeldThreadException">the thread attempting to obtain the lock,
 /// already holds the lock.</exception>
 protected RwVaultInternalLockedResource ExecuteGetInternalLockedResourceBlockForever(AcquisitionMode mode)
 {
     ThrowIfDisposingOrDisposed();
     return(RwVaultInternalLockedResource.CreateInternalLockedResourceBlockForever(this, mode.ValueOrThrowIfNDef()));
 }