Exemplo n.º 1
0
        /// <summary>
        /// Creates a <see cref="Channel{T}"/> instance accordingly to the provided capacity.
        /// If greater than 0, the channel will be bounded; otherwise unbounded.
        /// </summary>
        public static Channel <T> CreateForCapacity <T>(
            int capacity,
            bool singleReader = false,
            bool singleWriter = false,
            bool allowSynchronousContinuations = false,
            BoundedChannelFullMode fullMode    = BoundedChannelFullMode.Wait)
        {
            if (capacity > 0)
            {
                return(Channel.CreateBounded <T>(new BoundedChannelOptions(capacity)
                {
                    SingleReader = singleReader,
                    SingleWriter = singleWriter,
                    AllowSynchronousContinuations = allowSynchronousContinuations,
                    FullMode = fullMode
                }));
            }

            return(Channel.CreateUnbounded <T>(new UnboundedChannelOptions()
            {
                SingleReader = singleReader,
                SingleWriter = singleWriter,
                AllowSynchronousContinuations = allowSynchronousContinuations
            }));
        }
Exemplo n.º 2
0
 public ChannelReaderSink(int bufferSize, bool singleReader = false, BoundedChannelFullMode fullMode = BoundedChannelFullMode.Wait)
 {
     _bufferSize   = bufferSize;
     _singleReader = singleReader;
     _fullMode     = fullMode;
     Inlet         = new Inlet <T>("channelReader.in");
     Shape         = new SinkShape <T>(Inlet);
 }
Exemplo n.º 3
0
 /// <summary>Initializes the <see cref="BoundedChannel{T}"/>.</summary>
 /// <param name="bufferedCapacity">The positive bounded capacity for the channel.</param>
 /// <param name="mode">The mode used when writing to a full channel.</param>
 /// <param name="runContinuationsAsynchronously">Whether to force continuations to be executed asynchronously.</param>
 internal BoundedChannel(int bufferedCapacity, BoundedChannelFullMode mode, bool runContinuationsAsynchronously)
 {
     Debug.Assert(bufferedCapacity > 0);
     _bufferedCapacity = bufferedCapacity;
     _mode             = mode;
     _runContinuationsAsynchronously = runContinuationsAsynchronously;
     _completion = new TaskCompletionSource <VoidResult>(runContinuationsAsynchronously ? TaskCreationOptions.RunContinuationsAsynchronously : TaskCreationOptions.None);
     Reader      = new BoundedChannelReader(this);
     Writer      = new BoundedChannelWriter(this);
 }
Exemplo n.º 4
0
        /// <summary>Creates a channel with the specified bounded size.</summary>
        /// <typeparam name="T">Specifies the type of data in the channel.</typeparam>
        /// <param name="bufferedCapacity">The maximum number of elements the channel can store.</param>
        /// <param name="mode">The behavior to use when writing to a full channel.</param>
        /// <param name="optimizations">Controls optimizations that may be applied to the channel.</param>
        /// <returns>The created channel.</returns>
        public static Channel <T> CreateBounded <T>(int bufferedCapacity, BoundedChannelFullMode mode = BoundedChannelFullMode.Wait, ChannelOptimizations optimizations = null)
        {
            if (bufferedCapacity < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferedCapacity));
            }
            if (mode != BoundedChannelFullMode.DropNewest &&
                mode != BoundedChannelFullMode.DropOldest &&
                mode != BoundedChannelFullMode.Wait)
            {
                throw new ArgumentOutOfRangeException(nameof(mode));
            }

            bool runContinuationsAsynchronously = optimizations == null || !optimizations.AllowSynchronousContinuations;

            return(new BoundedChannel <T>(bufferedCapacity, mode, runContinuationsAsynchronously));
        }
Exemplo n.º 5
0
        protected BaseChannelWorkTask(Channel <TDataModel> channel, Action <TDataModel> workAction, int workTaskTotalCount, int taskSleepMilliseconds, int channelCapacityCount, BoundedChannelFullMode channelFullMode)
        {
            if (workAction.IfIsNull())
            {
                throw new ArgumentNullException(nameof(workAction));
            }

            if (workTaskTotalCount < 1)
            {
                workTaskTotalCount = 1;
            }

            if (taskSleepMilliseconds <= 0)
            {
                taskSleepMilliseconds = 3 * 1000;
            }

            if (channelCapacityCount < 0)
            {
                channelCapacityCount = 0;
            }

            WorkTaskTotalCount = workTaskTotalCount;
            _CurrentWorkAction = workAction;

            ChannelCapacityCount  = channelCapacityCount;
            ChannelFullMode       = channelFullMode;
            TaskSleepMilliseconds = taskSleepMilliseconds;


            if (!channel.IfIsNull())
            {
                _IsInternalChannel = true;
                _CurrentChannel    = channel;
            }
        }
Exemplo n.º 6
0
 public void BoundedChannelOptions_InvalidModes_ThrowArgumentExceptions(BoundedChannelFullMode mode) =>
 AssertExtensions.Throws <ArgumentOutOfRangeException>("value", () => new BoundedChannelOptions(1)
 {
     FullMode = mode
 });
Exemplo n.º 7
0
 public void CreateBounded_InvalidModes_ThrowArgumentExceptions(BoundedChannelFullMode mode)
 {
     Assert.Throws <ArgumentOutOfRangeException>("mode", () => Channel.CreateBounded <int>(1, mode));
 }
Exemplo n.º 8
0
        public WorkTaskQueue(Action<TDataModel> workAction, int workTaskTotalCount = 1, int taskSleepMilliseconds = 3 * 1000, int channelCapacityCount = 0, BoundedChannelFullMode channelFullMode = BoundedChannelFullMode.Wait)
            : this(null, workAction, workTaskTotalCount, taskSleepMilliseconds, channelCapacityCount, channelFullMode)
        {


        }
Exemplo n.º 9
0
        internal WorkTaskQueue(Channel<TDataModel> channel, Action<TDataModel> workAction, int workTaskTotalCount, int taskSleepMilliseconds, int channelCapacityCount, BoundedChannelFullMode channelFullMode)
            : base(channel, workAction, workTaskTotalCount, taskSleepMilliseconds, channelCapacityCount, channelFullMode)
        {



        }
Exemplo n.º 10
0
 internal WorkTaskTriggerQueue(Channel <TDataModel> channel, Action <List <TDataModel> > workTriggerAction, ushort actionTriggerCount, TimeSpan actionTriggerTimeSpan, int taskSleepMilliseconds = 3000, int channelCapacityCount = 0, BoundedChannelFullMode channelFullMode = BoundedChannelFullMode.Wait)
     : base(channel, workTriggerAction, actionTriggerCount, actionTriggerTimeSpan, taskSleepMilliseconds, channelCapacityCount, channelFullMode)
 {
 }
Exemplo n.º 11
0
 /// <summary>
 /// Creates a sink that upon materialization, returns a <see cref="ChannelReader{T}"/> connected with
 /// this materialized graph. It can then be used to consume events incoming from the graph. It will
 /// also be completed once upstream completes.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="bufferSize"></param>
 /// <param name="singleReader"></param>
 /// <param name="fullMode"></param>
 /// <returns></returns>
 public static Sink <T, ChannelReader <T> > AsReader <T>(int bufferSize, bool singleReader = false, BoundedChannelFullMode fullMode = BoundedChannelFullMode.Wait) =>
 Sink.FromGraph(new ChannelReaderSink <T>(bufferSize, singleReader));