Exemplo n.º 1
0
        public bool SetMaxThreads(int workerThreads, int ioCompletionThreads)
        {
            if (workerThreads <= 0 || ioCompletionThreads <= 0)
            {
                return(false);
            }

            _threadAdjustmentLock.Acquire();
            try
            {
                if (workerThreads < _minThreads || !ThreadPool.CanSetMaxIOCompletionThreads(ioCompletionThreads))
                {
                    return(false);
                }

                ThreadPool.SetMaxIOCompletionThreads(ioCompletionThreads);

                if (ForcedMaxWorkerThreads != 0)
                {
                    return(true);
                }

                short newMaxThreads = (short)Math.Min(workerThreads, MaxPossibleThreadCount);
                _maxThreads = newMaxThreads;
                if (_separated.counts.NumThreadsGoal > newMaxThreads)
                {
                    _separated.counts.InterlockedSetNumThreadsGoal(newMaxThreads);
                }
                return(true);
            }
            finally
            {
                _threadAdjustmentLock.Release();
            }
        }
Exemplo n.º 2
0
        public bool SetMaxThreads(int workerThreads, int ioCompletionThreads)
        {
            if (workerThreads <= 0 || ioCompletionThreads <= 0)
            {
                return(false);
            }

            _threadAdjustmentLock.Acquire();
            try
            {
                if (workerThreads < _minThreads)
                {
                    return(false);
                }

                if (ThreadPool.UsePortableThreadPoolForIO
                        ? ioCompletionThreads < _legacy_minIOCompletionThreads
                        : !ThreadPool.CanSetMaxIOCompletionThreads(ioCompletionThreads))
                {
                    return(false);
                }

                if (HasForcedMaxThreads && workerThreads != ForcedMaxWorkerThreads)
                {
                    return(false);
                }

                if (ThreadPool.UsePortableThreadPoolForIO)
                {
                    _legacy_maxIOCompletionThreads = (short)Math.Min(ioCompletionThreads, MaxPossibleThreadCount);
                }
                else
                {
                    ThreadPool.SetMaxIOCompletionThreads(ioCompletionThreads);
                }

                short newMaxThreads = (short)Math.Min(workerThreads, MaxPossibleThreadCount);
                if (newMaxThreads == _maxThreads)
                {
                    return(true);
                }

                _maxThreads = newMaxThreads;
                if (_separated.counts.NumThreadsGoal > newMaxThreads)
                {
                    _separated.counts.InterlockedSetNumThreadsGoal(newMaxThreads);
                }
                return(true);
            }
            finally
            {
                _threadAdjustmentLock.Release();
            }
        }
Exemplo n.º 3
0
        public bool SetMaxThreads(int workerThreads, int ioCompletionThreads)
        {
            if (workerThreads <= 0 || ioCompletionThreads <= 0)
            {
                return(false);
            }

            _maxMinThreadLock.Acquire();
            try
            {
                if (workerThreads < _minThreads || !ThreadPool.CanSetMaxIOCompletionThreads(ioCompletionThreads))
                {
                    return(false);
                }

                ThreadPool.SetMaxIOCompletionThreads(ioCompletionThreads);

                if (s_forcedMaxWorkerThreads != 0)
                {
                    return(true);
                }

                short newMaxThreads = (short)Math.Min(workerThreads, MaxPossibleThreadCount);
                _maxThreads = newMaxThreads;

                ThreadCounts counts = _separated.counts.VolatileRead();
                while (counts.NumThreadsGoal > newMaxThreads)
                {
                    ThreadCounts newCounts = counts;
                    newCounts.NumThreadsGoal = newMaxThreads;

                    ThreadCounts oldCounts = _separated.counts.InterlockedCompareExchange(newCounts, counts);
                    if (oldCounts == counts)
                    {
                        break;
                    }

                    counts = oldCounts;
                }

                return(true);
            }
            finally
            {
                _maxMinThreadLock.Release();
            }
        }