Beispiel #1
0
        void IDispatchHost.IncreaseThreadCount(string reason)
        {
            // check if thread pool is already awaiting another thread
            if (_threadVelocity > 0)
            {
                return;
            }
            lock (_syncRoot) {
                _threadVelocity = 1;

                // check if thread pool has enough threads
                if (_threadCount >= _maxParallelThreads)
                {
                    _threadVelocity = 0;
                    return;
                }
#if EXTRA_DEBUG
                _log.DebugFormat("IncreaseThreadCount: {1} - {0}", this, reason);
#endif
            }

            // check if there are threads in the reserve
            KeyValuePair <DispatchThread, Result <DispatchWorkItem> > reservedThread;
            if (_reservedThreads.TryPop(out reservedThread))
            {
                AddThread(reservedThread);
            }
            else
            {
                DispatchThreadScheduler.RequestThread(0, AddThread);
            }
        }
Beispiel #2
0
        //--- Constructors ---

        /// <summary>
        /// Creates a new ElasticThreadPool instance.
        /// </summary>
        /// <param name="minReservedThreads">Minium number of threads to reserve for the thread pool.</param>
        /// <param name="maxParallelThreads">Maximum number of parallel threads used by the thread pool.</param>
        /// <exception cref="InsufficientResourcesException">The ElasticThreadPool instance was unable to obtain the minimum reserved threads.</exception>
        public ElasticThreadPool(int minReservedThreads, int maxParallelThreads)
        {
            _minReservedThreads = Math.Max(0, Math.Min(minReservedThreads, MAX_RESERVED_THREADS));
            _maxParallelThreads = Math.Max(Math.Max(1, minReservedThreads), Math.Min(maxParallelThreads, int.MaxValue));

            // initialize reserved threads
            _activeThreads = new DispatchThread[Math.Min(_maxParallelThreads, Math.Max(_minReservedThreads, Math.Min(16, _maxParallelThreads)))];
            if (_minReservedThreads > 0)
            {
                DispatchThreadScheduler.RequestThread(_minReservedThreads, AddThread);
            }
            DispatchThreadScheduler.RegisterHost(this);
            _log.DebugFormat("Create @{0}", this);
        }
        //--- Constructors ---

        /// <summary>
        /// Creates a new ElasticPriorityThreadPool instance.
        /// </summary>
        /// <param name="minReservedThreads">Minium number of threads to reserve for the thread pool.</param>
        /// <param name="maxParallelThreads">Maximum number of parallel threads used by the thread pool.</param>
        /// <param name="maxPriority">Maximum priority number (inclusive upper bound).</param>
        /// <exception cref="InsufficientResourcesException">The ElasticPriorityThreadPool instance was unable to obtain the minimum reserved threads.</exception>
        public ElasticPriorityThreadPool(int minReservedThreads, int maxParallelThreads, int maxPriority)
        {
            _minReservedThreads = Math.Max(0, Math.Min(minReservedThreads, MAX_RESERVED_THREADS));
            _maxParallelThreads = Math.Max(Math.Max(1, minReservedThreads), Math.Min(maxParallelThreads, int.MaxValue));
            _inbox            = new LockFreePriorityQueue <Action>(maxPriority);
            _prioritizedInbox = new PrioritizedThreadPool[maxPriority];
            for (int i = 0; i < maxPriority; ++i)
            {
                _prioritizedInbox[i] = new PrioritizedThreadPool(i, this);
            }

            // initialize reserved threads
            _activeThreads = new DispatchThread[Math.Min(_maxParallelThreads, Math.Max(_minReservedThreads, Math.Min(16, _maxParallelThreads)))];
            if (_minReservedThreads > 0)
            {
                DispatchThreadScheduler.RequestThread(_minReservedThreads, AddThread);
            }
            DispatchThreadScheduler.RegisterHost(this);
            _log.DebugFormat("Create @{0}", this);
        }