internal ConcurrentExclusiveTaskScheduler(ConcurrentExclusiveSchedulerPair pair, int maxConcurrencyLevel, ConcurrentExclusiveSchedulerPair.ProcessingMode processingMode)
 {
     this.m_pair = pair;
     this.m_maxConcurrencyLevel = maxConcurrencyLevel;
     this.m_processingMode      = processingMode;
     this.m_tasks = processingMode == ConcurrentExclusiveSchedulerPair.ProcessingMode.ProcessingExclusiveTask ? (IProducerConsumerQueue <Task>) new SingleProducerSingleConsumerQueue <Task>() : (IProducerConsumerQueue <Task>) new MultiProducerMultiConsumerQueue <Task>();
 }
예제 #2
0
        /// <summary>Initializes the target core.</summary>
        /// <param name="owningTarget">The target using this helper.</param>
        /// <param name="callAction">An action to invoke for all accepted items.</param>
        /// <param name="reorderingBuffer">The reordering buffer used by the owner; may be null.</param>
        /// <param name="dataflowBlockOptions">The options to use to configure this block. The target core assumes these options are immutable.</param>
        /// <param name="targetCoreOptions">Options for how the target core should behave.</param>
        internal TargetCore(
            ITargetBlock <TInput> owningTarget,
            Action <KeyValuePair <TInput, long> > callAction,
            IReorderingBuffer reorderingBuffer,
            ExecutionDataflowBlockOptions dataflowBlockOptions,
            TargetCoreOptions targetCoreOptions)
        {
            // Validate internal arguments
            Debug.Assert(owningTarget != null, "Core must be associated with a target block.");
            Debug.Assert(dataflowBlockOptions != null, "Options must be provided to configure the core.");
            Debug.Assert(callAction != null, "Action to invoke for each item is required.");

            // Store arguments and do additional initialization
            _owningTarget         = owningTarget;
            _callAction           = callAction;
            _reorderingBuffer     = reorderingBuffer;
            _dataflowBlockOptions = dataflowBlockOptions;
            _targetCoreOptions    = targetCoreOptions;
            _messages             = (dataflowBlockOptions.MaxDegreeOfParallelism == 1) ?
                                    (IProducerConsumerQueue <KeyValuePair <TInput, long> >) new SingleProducerSingleConsumerQueue <KeyValuePair <TInput, long> >() :
                                    (IProducerConsumerQueue <KeyValuePair <TInput, long> >) new MultiProducerMultiConsumerQueue <KeyValuePair <TInput, long> >();
            if (_dataflowBlockOptions.BoundedCapacity != System.Threading.Tasks.Dataflow.DataflowBlockOptions.Unbounded)
            {
                Debug.Assert(_dataflowBlockOptions.BoundedCapacity > 0, "Positive bounding count expected; should have been verified by options ctor");
                _boundingState = new BoundingStateWithPostponed <TInput>(_dataflowBlockOptions.BoundedCapacity);
            }
        }
예제 #3
0
        public ConcurrentBlockDecompressor(
            ICompressor compressor,
            string pathToFile,
            string saveToFile,
            int batchSize = 1024
            )
        {
            var workerCount = Environment.ProcessorCount * 5;

            _chunkSize = 100;

            _compressor = compressor;

            _saveToFile = saveToFile;
            _fileStreamByBatchWriter = new FileStreamByBatchWriter(_saveToFile);

            _producerConsumerDecompressTask = new ProducerConsumerQueue(workerCount);
            _producerConsumerWriteTask      = new ProducerConsumerQueue();
            _gzipActionPerformer            = new GzipActionPerformer(
                _producerConsumerDecompressTask,
                _producerConsumerWriteTask,
                _fileStreamByBatchWriter
                );


            _pathToFile = pathToFile;
            _fileStreamBatchEnumerator = new FileStreamBatchEnumerator(_pathToFile, batchSize);
        }
예제 #4
0
        /// <summary>
        /// Completes the block.  This must only be called once, and only once all of the completion conditions are met.
        /// As such, it must only be called from CompleteBlockIfPossible.
        /// </summary>
        private void CompleteBlockOncePossible()
        {
            // Since the lock is needed only for the Assert, we do this only in DEBUG mode
#if DEBUG
            lock (IncomingLock) Debug.Assert(_numberOfOutstandingOperations == 0, "Everything must be done by now.");
#endif

            // Release any postponed messages
            if (_boundingState != null)
            {
                // Note: No locks should be held at this point.
                Common.ReleaseAllPostponedMessages(_owningTarget, _boundingState.PostponedMessages, ref _exceptions);
            }

            // For good measure and help in preventing leaks, clear out the incoming message queue,
            // which may still contain orphaned data if we were canceled or faulted.  However,
            // we don't reset the bounding count here, as the block as a whole may still be active.
            KeyValuePair <TInput, long> ignored;
            IProducerConsumerQueue <KeyValuePair <TInput, long> > messages = _messages;
            while (messages.TryDequeue(out ignored))
            {
                ;
            }

            // If we completed with any unhandled exception, finish in an error state
            if (Volatile.Read(ref _exceptions) != null)
            {
                // It's ok to read _exceptions' content here, because
                // at this point no more exceptions can be generated and thus no one will
                // be writing to it.
                _completionSource.TrySetException(Volatile.Read(ref _exceptions));
            }
            // If we completed with cancellation, finish in a canceled state
            else if (_dataflowBlockOptions.CancellationToken.IsCancellationRequested)
            {
                _completionSource.TrySetCanceled();
            }
            // Otherwise, finish in a successful state.
            else
            {
                _completionSource.TrySetResult(default(VoidResult));
            }
#if FEATURE_TRACING
            // We only want to do tracing for block completion if this target core represents the whole block.
            // If it only represents a part of the block (i.e. there's a source associated with it as well),
            // then we shouldn't log just for the first half of the block; the source half will handle logging.
            DataflowEtwProvider etwLog;
            if ((_targetCoreOptions & TargetCoreOptions.RepresentsBlockCompletion) != 0 &&
                (etwLog = DataflowEtwProvider.Log).IsEnabled())
            {
                etwLog.DataflowBlockCompleted(_owningTarget);
            }
#endif
        }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QueueProcessManager"/> class.
        /// </summary>
        /// <param name="asyncHandler">if set to <c>true</c> [async handler].</param>
        protected QueueProcessManagerEx(QueueProcessMode mode, IExecutorService executorService, int capacity, int queueTimeout, bool isLogging)
            : base(executorService)
        {
            _mode = mode;

            if (mode == QueueProcessMode.Thread)
            {
                _queue = ProducerConsumerQueueFactory.Create<T>(executorService, capacity, queueTimeout, isLogging);
                _queue.Dequeue += OnQueue_Dequeue;
            }
        }
예제 #6
0
        public GzipActionPerformer(
            IProducerConsumerQueue producerConsumerGzipTask,
            IProducerConsumerQueue producerConsumerWriteTask,
            IFileStreamByBatchWriter fileStreamByBatchWriter
            )
        {
            _producerConsumerGzipTask  = producerConsumerGzipTask;
            _producerConsumerWriteTask = producerConsumerWriteTask;
            _priorityLock = new PriorityLock();

            _fileStreamByBatchWriter = fileStreamByBatchWriter;
        }
예제 #7
0
        internal TcpSocketTransceiverImpl(IExecutorService executorService, UdpSocketTransceiverParameter parameter)
            : base(executorService)
        {
            _parameter   = parameter;
            _readBuffer  = new byte[Math.Max(256, parameter.ReadBufferSize)];
            _writeBuffer = new byte[Math.Max(256, parameter.WriteBufferSize)];

            _useInternalQueue = parameter.UseInternalQueue;
            if (_useInternalQueue)
            {
                _queueReceived = ProducerConsumerQueueFactory.Create <UdpSocketReceiveData>(executorService, -1,
                                                                                            parameter.ListenWaitTime, false);
                _queueReceived.Dequeue += this.OnDataReceived;
            }
            _remoteEndPoints = new SortedDictionary <IPAddress, EndPoint>(new IPAddressComparer());
        }
            /// <summary>Initializes the scheduler.</summary>
            /// <param name="pair">The parent pair.</param>
            /// <param name="maxConcurrencyLevel">The maximum degree of concurrency this scheduler may use.</param>
            /// <param name="processingMode">The processing mode of this scheduler.</param>
            internal ConcurrentExclusiveTaskScheduler(ConcurrentExclusiveSchedulerPair pair, int maxConcurrencyLevel, ProcessingMode processingMode)
            {
                Contract.Requires(pair != null, "Scheduler must be associated with a valid pair.");
                Contract.Requires(processingMode == ProcessingMode.ProcessingConcurrentTasks || processingMode == ProcessingMode.ProcessingExclusiveTask,
                                  "Scheduler must be for concurrent or exclusive processing.");
                Contract.Requires(
                    (processingMode == ProcessingMode.ProcessingConcurrentTasks && (maxConcurrencyLevel >= 1 || maxConcurrencyLevel == UNLIMITED_PROCESSING)) ||
                    (processingMode == ProcessingMode.ProcessingExclusiveTask && maxConcurrencyLevel == 1),
                    "If we're in concurrent mode, our concurrency level should be positive or unlimited.  If exclusive, it should be 1.");

                m_pair = pair;
                m_maxConcurrencyLevel = maxConcurrencyLevel;
                m_processingMode      = processingMode;
                m_tasks = (processingMode == ProcessingMode.ProcessingExclusiveTask) ?
                          (IProducerConsumerQueue <Task>) new SingleProducerSingleConsumerQueue <Task>() :
                          (IProducerConsumerQueue <Task>) new MultiProducerMultiConsumerQueue <Task>();
            }
        public ServiceMainForm(IExecutorService executorService, IServiceHost serviceHost)
        {
            InitializeComponent();
            SetTagProperty();
            this.ResolveResources();

            _normalFont = this.Font;
            _boldFont = new Font(this.Font, FontStyle.Bold);

            _queue = ProducerConsumerQueueFactory.Create<LogItem>(executorService, -1);
            _queue.Dequeue += OnQueue_Dequeue;

            Log.GlobalWriteToExternalLog += new WriteToExternalLogHandler(Log_WriteToExternalLog);
            _setStatus = new SetStatusHandler(this.SetStatus);

            _serviceHost = serviceHost;
        }
        public ServiceMainForm(IExecutorService executorService, IServiceHost serviceHost)
        {
            InitializeComponent();
            SetTagProperty();
            this.ResolveResources();

            _normalFont = this.Font;
            _boldFont   = new Font(this.Font, FontStyle.Bold);

            _queue          = ProducerConsumerQueueFactory.Create <LogItem>(executorService, -1);
            _queue.Dequeue += OnQueue_Dequeue;

            Log.GlobalWriteToExternalLog += new WriteToExternalLogHandler(Log_WriteToExternalLog);
            _setStatus = new SetStatusHandler(this.SetStatus);

            _serviceHost = serviceHost;
        }
예제 #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProducerConsumer" /> class.
        /// </summary>
        /// <param name="queue">An instance of <see cref="IProducerConsumerQueue" />.</param>
        /// <param name="consumerAction">The <see cref="Action{T}" /> that will be executed when the consumer thread processes a data item.</param>
        /// <param name="consumerThreads">Number of consumer threads.</param>
        /// <param name="startNow">Whether to start the consumer thread immediately.</param>
        public ProducerConsumer(IProducerConsumerQueue queue, Action <object> consumerAction, int consumerThreads = 1, bool startNow = true)
        {
            if (consumerAction == null)
            {
                throw new ArgumentNullException("consumerAction");
            }

            if (consumerThreads < 1)
            {
                throw new ArgumentOutOfRangeException("consumerThreads", consumerThreads, "Must be greater than 0");
            }

            this._queue = queue;

            Interlocked.Exchange(ref this._produceAccumulation, this._queue.Count());

            this._consumerAction = consumerAction;

            this._consumerThreads = consumerThreads;

            this.IsRunning = startNow;

            this._consumerThreadList = new List <Thread>(consumerThreads);

            for (int i = 0; i < this._consumerThreads; i++)
            {
                Thread worker = new Thread(this.ConsumerThread);
                worker.IsBackground = true;
                this._consumerThreadList.Add(worker);
            }

            foreach (Thread item in this._consumerThreadList)
            {
                item.Start();
            }
        }
 public FFTransReceiver_InMemory(FFTransceiverArgs arg, IExecutorService executorService)
     : base(arg, executorService)
 {
     // create a listener thread and do the receive processing
     _queue = ProducerConsumerQueueFactory.Create<UdpFreeformEntity>(executorService, -1);
 }
예제 #13
0
 public FFTransReceiver_InMemory(FFTransceiverArgs arg, IExecutorService executorService)
     : base(arg, executorService)
 {
     // create a listener thread and do the receive processing
     _queue = ProducerConsumerQueueFactory.Create <UdpFreeformEntity>(executorService, -1);
 }
 public ProducerConsumerRunner([NotNull] IProducerConsumerQueue <string> queue) => Queue = queue;
        private void RunProducer(IProducerConsumerQueue<int> producerConsumerCollection, int id, Barrier barrier)
        {
            barrier.SignalAndWait();
            for (var i = 0; i < repetitions; i++)
            {
                while (!producerConsumerCollection.TryAdd(id))
                {

                    Thread.Yield();
                }
            }
        }