/// <summary>
        /// Creates a new <see cref="ConcurrencyControl" /> instance using the specified settings.
        /// </summary>
        /// <param name="mode">
        /// The logical concurrency control mode for the resulting control.
        /// </param>
        /// <param name="blockTimeoutThreshold">
        /// The maximum length of time that the resulting control may block a thread before raising an exception, or
        /// <see cref="Timeout.InfiniteTimeSpan" /> if indefinite thread blocking is permitted. The default value is
        /// <see cref="Timeout.InfiniteTimeSpan" />.
        /// </param>
        /// <returns>
        /// A new <see cref="ConcurrencyControl" /> instance.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="blockTimeoutThreshold" /> is less than or equal to <see cref="TimeSpan.Zero" /> and is not equal to
        /// <see cref="Timeout.InfiniteTimeSpan" /> -or- <paramref name="mode" /> is equal to
        /// <see cref="ConcurrencyControlMode.Unspecified" />.
        /// </exception>
        public static ConcurrencyControl New(ConcurrencyControlMode mode, TimeSpan blockTimeoutThreshold)
        {
            switch (mode.RejectIf().IsEqualToValue(ConcurrencyControlMode.Unspecified).TargetArgument)
            {
            case ConcurrencyControlMode.DuplexSemaphore:

                return(new DuplexSemaphoreControl(blockTimeoutThreshold));

            case ConcurrencyControlMode.ProcessorCountSemaphore:

                return(new ProcessorCountSemaphoreControl(blockTimeoutThreshold));

            case ConcurrencyControlMode.SingleThreadLock:

                return(new SingleThreadLockControl(blockTimeoutThreshold));

            case ConcurrencyControlMode.SingleThreadSpinLock:

                return(new SingleThreadSpinLockControl(blockTimeoutThreshold));

            case ConcurrencyControlMode.Unconstrained:

                return(new UnconstrainedControl());

            default:

                throw new InvalidOperationException($"The specified concurrency control mode, {mode}, is not supported.");
            }
        }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Instrument" /> class.
 /// </summary>
 /// <param name="stateControlMode">
 /// The concurrency control mode that is used to manage state. The default value is
 /// <see cref="ConcurrencyControlMode.SingleThreadLock" />.
 /// </param>
 /// <param name="stateControlTimeoutThreshold">
 /// The maximum length of time that the instrument's state control may block a thread before raising an exception, or
 /// <see cref="Timeout.InfiniteTimeSpan" /> if indefinite thread blocking is permitted. The default value is
 /// <see cref="Timeout.InfiniteTimeSpan" />.
 /// </param>
 /// <exception cref="ArgumentOutOfRangeException">
 /// <paramref name="stateControlMode" /> is equal to <see cref="ConcurrencyControlMode.Unspecified" /> -or-
 /// <paramref name="stateControlTimeoutThreshold" /> is less than or equal to <see cref="TimeSpan.Zero" /> and is not equal
 /// to <see cref="Timeout.InfiniteTimeSpan" />.
 /// </exception>
 protected Instrument(ConcurrencyControlMode stateControlMode, TimeSpan stateControlTimeoutThreshold)
 {
     StateControlMode             = stateControlMode.RejectIf().IsEqualToValue(ConcurrencyControlMode.Unspecified, nameof(stateControlMode));
     StateControlTimeoutThreshold = (stateControlTimeoutThreshold == Timeout.InfiniteTimeSpan) ? stateControlTimeoutThreshold : stateControlTimeoutThreshold.RejectIf().IsLessThanOrEqualTo(TimeSpan.Zero, nameof(stateControlTimeoutThreshold));
     LazyStateControl             = new Lazy <IConcurrencyControl>(InitializeStateControl, LazyThreadSafetyMode.ExecutionAndPublication);
 }