Esempio n. 1
0
 private AtomicLockedResource ExecuteGetInternalLockedResourceDuringDispose(TimeSpan timeOut, bool spin)
 {
     ThrowIfNotDisposing();
     if (timeOut <= TimeSpan.Zero)
     {
         throw new ArgumentOutOfRangeException(nameof(timeOut), timeOut, @"Must be positive.");
     }
     return(AtomicLockedResource.CreateInternalLockedResource(this, timeOut, spin, true));
 }
Esempio n. 2
0
        /// <summary>
        /// Creates the LockedResource.  Until you actually return it to the ultimate consumer in one of the <see cref="Lock()"/> or <see cref="SpinLock()"/>
        /// methods (which are annotated with <see cref="UsingMandatoryAttribute"/>, guaranteeing disposal) it is YOUR responsiblity to make sure
        /// that the item is disposed IN ANY PATH THAT DOES NOT RESULT IN THE USER GETTING THE LOCKED RESOURCE REQUESTED.  This include exceptions,
        /// short-circuited returns, etc.
        /// </summary>
        /// <param name="timeout">How long should be wait.  If null, wait forever</param>
        /// <param name="token">A cancellation token that can be used to cancel attempting to obtain the resource.</param>
        /// <param name="spin">true for busy wait, false for yielding wait</param>
        /// <returns>A locked vault mutable resource</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="timeout"/> was not-null but was not positive</exception>
        /// <exception cref="TimeoutException">operation not completed in time (should be handled by user not by me or you ... except, see above, we need
        /// to dispose the resource ourselves if we've obtained it ... before rethrowing)</exception>
        /// <exception cref="OperationCanceledException">the operation was cancelled and the cancellation was received via <paramref name="token"/>.
        /// The user should handle this, not you or me (except, if we obtained the resource we need to dispose it before rethrowing).</exception>
        protected LockedVaultMutableResource <MutableResourceVault <T>, T> PerformLock(TimeSpan?timeout, CancellationToken token, bool spin)
        {
            if (timeout == null)
            {
                using (AtomicLockedResource ilr = GetInternalLockedResource(token, spin))
                {
                    return(LockedVaultMutableResource <MutableResourceVault <T>, T> .CreateLockedResource(this, ilr.Release()));
                }
            }

            using (AtomicLockedResource ilr = GetInternalLockedResource(timeout.Value, token, spin))
            {
                return(LockedVaultMutableResource <MutableResourceVault <T>, T> .CreateLockedResource(this, ilr.Release()));
            }
        }
Esempio n. 3
0
        private AtomicLockedResource ExecuteGetInternalLockedResource(TimeSpan?timeout, bool spin,
                                                                      CancellationToken token)
        {
            ThrowIfDisposingOrDisposed();
            //todo we are currently going to allow the infinite wait if desired ... not sure if this is a good idea but for now i'll leave it.
            //if (timeout == null && token == CancellationToken.None)
            //{
            //    throw new ArgumentException("If no timeout is specified, the cancellation token must not be none.");
            //}
            if (timeout.HasValue && timeout.Value <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(timeout), timeout, @"Must be positive.");
            }

            return(timeout == null
                ? AtomicLockedResource.CreateInternalLockedResource(this, token, spin)
                : AtomicLockedResource.CreateInternalLockedResource(this, timeout.Value, token, spin));
        }
Esempio n. 4
0
            internal static bool CreateInternalLockedResourceNowOrGiveUp([NotNull] AtomicVault <T> owner, out AtomicLockedResource res)
            {
                bool ret;

                res = default;
                var boxRes = AcquireBoxPointer(owner, null, true, true, CancellationToken.None);

                ret = boxRes.acquiredBox != null;
                return(ret);
            }