public void DisposeCallerState() { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.DisposeOfStateObjects = true; SmartThreadPool smartThreadPool = new SmartThreadPool(stpStartInfo); CallerState nonDisposableCallerState = new NonDisposableCallerState(); CallerState disposableCallerState = new DisposableCallerState(); IWorkItemResult wir1 = smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), nonDisposableCallerState); IWorkItemResult wir2 = smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), disposableCallerState); wir1.GetResult(); Assert.AreEqual(1, nonDisposableCallerState.Value); wir2.GetResult(); // Wait a little bit for the working thread to call dispose on the // work item's state. smartThreadPool.WaitForIdle(); Assert.AreEqual(2, disposableCallerState.Value); smartThreadPool.Shutdown(); }
public void CancelCanceledWorkItem() { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem(state => null); int counter = 0; wir.Cancel(); try { wir.GetResult(); } catch (WorkItemCancelException ce) { ce.GetHashCode(); ++counter; } Assert.AreEqual(counter, 1); wir.Cancel(); try { wir.GetResult(); } finally { stp.Shutdown(); } }
public void STPAndWIGStartSuspended() { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); WIGStartInfo wigStartInfo = new WIGStartInfo(); wigStartInfo.StartSuspended = true; IWorkItemsGroup wig = stp.CreateWorkItemsGroup(10, wigStartInfo); wig.QueueWorkItem(new WorkItemCallback(this.DoWork)); Assert.IsFalse(wig.WaitForIdle(200)); wig.Start(); Assert.IsFalse(wig.WaitForIdle(200)); stp.Start(); Assert.IsTrue(wig.WaitForIdle(5000), "WIG is not idle"); Assert.IsTrue(stp.WaitForIdle(5000), "STP is not idle"); }
public STPStartInfo(STPStartInfo stpStartInfo) : base(stpStartInfo) { _idleTimeout = stpStartInfo._idleTimeout; _minWorkerThreads = stpStartInfo._minWorkerThreads; _maxWorkerThreads = stpStartInfo._maxWorkerThreads; _threadPriority = stpStartInfo._threadPriority; _pcInstanceName = stpStartInfo._pcInstanceName; }
/// <summary> /// Constructor /// </summary> /// <param name="idleTimeout">Idle timeout in milliseconds</param> public SmartThreadPool(int idleTimeout) { _stpStartInfo = new STPStartInfo { IdleTimeout = idleTimeout, }; Initialize(); }
public STPStartInfo(STPStartInfo stpStartInfo) : base(stpStartInfo) { _idleTimeout = stpStartInfo._idleTimeout; _minWorkerThreads = stpStartInfo._minWorkerThreads; _maxWorkerThreads = stpStartInfo._maxWorkerThreads; _threadPriority = stpStartInfo._threadPriority; _pcInstanceName = stpStartInfo._pcInstanceName; _stackSize = stpStartInfo._stackSize; }
public STPStartInfo(STPStartInfo stpStartInfo) : base(stpStartInfo) { _idleTimeout = stpStartInfo.IdleTimeout; _minWorkerThreads = stpStartInfo.MinWorkerThreads; _maxWorkerThreads = stpStartInfo.MaxWorkerThreads; _threadPriority = stpStartInfo.ThreadPriority; _performanceCounterInstanceName = stpStartInfo.PerformanceCounterInstanceName; }
public STPStartInfo(STPStartInfo stpStartInfo) : base(stpStartInfo) { _idleTimeout = stpStartInfo.IdleTimeout; _minWorkerThreads = stpStartInfo.MinWorkerThreads; _maxWorkerThreads = stpStartInfo.MaxWorkerThreads; _threadPriority = stpStartInfo.ThreadPriority; _performanceCounterInstanceName = stpStartInfo.PerformanceCounterInstanceName; _enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters; }
/// <summary> /// Constructor /// </summary> /// <param name="idleTimeout">Idle timeout in milliseconds</param> /// <param name="maxWorkerThreads">Upper limit of threads in the pool</param> public SmartThreadPool( int idleTimeout, int maxWorkerThreads) { _stpStartInfo = new STPStartInfo { IdleTimeout = idleTimeout, MaxWorkerThreads = maxWorkerThreads, }; Initialize(); }
private void CheckSinglePriority(ThreadPriority threadPriority) { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.ThreadPriority = threadPriority; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem(new WorkItemCallback(GetThreadPriority)); ThreadPriority currentThreadPriority = (ThreadPriority)wir.GetResult(); Assert.AreEqual(threadPriority, currentThreadPriority); }
public STPStartInfo(STPStartInfo stpStartInfo) : base(stpStartInfo) { _idleTimeout = stpStartInfo.IdleTimeout; _minWorkerThreads = stpStartInfo.MinWorkerThreads; _maxWorkerThreads = stpStartInfo.MaxWorkerThreads; _threadPriority = stpStartInfo.ThreadPriority; _performanceCounterInstanceName = stpStartInfo.PerformanceCounterInstanceName; _enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters; _threadPoolName = stpStartInfo._threadPoolName; _areThreadsBackground = stpStartInfo.AreThreadsBackground; ThreadApartmentState = stpStartInfo.ThreadApartmentState; }
// Token: 0x06001842 RID: 6210 // RVA: 0x00074F58 File Offset: 0x00073158 public STPStartInfo(STPStartInfo stpstartInfo_0) : base(stpstartInfo_0) { this._idleTimeout = stpstartInfo_0.IdleTimeout; this._minWorkerThreads = stpstartInfo_0.MinWorkerThreads; this._maxWorkerThreads = stpstartInfo_0.MaxWorkerThreads; this._threadPriority = stpstartInfo_0.ThreadPriority; this._performanceCounterInstanceName = stpstartInfo_0.PerformanceCounterInstanceName; this._enableLocalPerformanceCounters = stpstartInfo_0._enableLocalPerformanceCounters; this._threadPoolName = stpstartInfo_0._threadPoolName; this._areThreadsBackground = stpstartInfo_0.AreThreadsBackground; this._apartmentState = stpstartInfo_0._apartmentState; }
public void QueueWorkItem_WhenMaxIsNull_Queues() { var info = new STPStartInfo { MaxQueueLength = null, }; var pool = new SmartThreadPool(info); pool.Start(); var workItem = pool.QueueWorkItem<object>(ReturnNull); // If rejected, an exception would have been thrown instead. Assert.IsTrue(workItem.GetResult() == null); }
public STPStartInfo(STPStartInfo stpStartInfo) : base(stpStartInfo) { _idleTimeout = stpStartInfo.IdleTimeout; _minWorkerThreads = stpStartInfo.MinWorkerThreads; _maxWorkerThreads = stpStartInfo.MaxWorkerThreads; _threadPriority = stpStartInfo.ThreadPriority; _performanceCounterInstanceName = stpStartInfo.PerformanceCounterInstanceName; _enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters; _threadPoolName = stpStartInfo._threadPoolName; _areThreadsBackground = stpStartInfo.AreThreadsBackground; _apartmentState = stpStartInfo._apartmentState; _supressflow = stpStartInfo._supressflow; }
public static bool Init(bool useSmartThredPool) { if (Pool == null) { STPStartInfo param = new STPStartInfo(); param.MinWorkerThreads = 2; param.MaxWorkerThreads = 50; param.ThreadPoolName = "LibOpenMetaverse Main ThreadPool"; param.AreThreadsBackground = true; Pool = new SmartThreadPool(param); } return true; }
private void InitSTP() { STPStartInfo stpStartInfo = new STPStartInfo { StartSuspended = true, MaxWorkerThreads = ((int)spinCon6.Value), IdleTimeout = int.Parse(spinIdleTimeout.Text) * 1000, }; if (_useWindowsPerformanceCounters) { stpStartInfo.PerformanceCounterInstanceName = "WIG Test SmartThreadPool"; } else { stpStartInfo.EnableLocalPerformanceCounters = true; } _smartThreadPool = new SmartThreadPool(stpStartInfo); WIGStartInfo wigStartInfo = new WIGStartInfo() { FillStateWithArgs = true, }; _wig1 = _smartThreadPool.CreateWorkItemsGroup((int)spinCon1.Value, wigStartInfo); _wig2 = _smartThreadPool.CreateWorkItemsGroup((int)spinCon2.Value, wigStartInfo); _wig3 = _smartThreadPool.CreateWorkItemsGroup((int)spinCon3.Value, wigStartInfo); spinCon1.Tag = _wig1; spinCon2.Tag = _wig2; spinCon3.Tag = _wig3; spinCon6.Tag = _smartThreadPool; comboWIPriority1.SelectedIndex = 1; comboWIPriority2.SelectedIndex = 1; comboWIPriority3.SelectedIndex = 1; comboWIPriority6.SelectedIndex = 1; _wigEntries = new WigEntry[] { new WigEntry(_wig1, queueUsageControl1, lblStatus1), new WigEntry(_wig2, queueUsageControl2, lblStatus2), new WigEntry(_wig3, queueUsageControl3, lblStatus3), }; for (int i = 0; i < _lastIndex.Length; i++) { _lastIndex[i] = 1; } }
private static void CheckApartmentState(ApartmentState requestApartmentState) { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.ApartmentState = requestApartmentState; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult<ApartmentState> wir = stp.QueueWorkItem(() => GetCurrentThreadApartmentState()); ApartmentState resultApartmentState = wir.GetResult(); stp.WaitForIdle(); Assert.AreEqual(requestApartmentState, resultApartmentState); }
private static void CheckIsBackground(bool isBackground) { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.AreThreadsBackground = isBackground; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult<bool> wir = stp.QueueWorkItem(() => GetCurrentThreadIsBackground()); bool resultIsBackground = wir.GetResult(); stp.WaitForIdle(); Assert.AreEqual(isBackground, resultIsBackground); }
public void StartSuspended() { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); stp.QueueWorkItem(new WorkItemCallback(this.DoWork)); Assert.IsFalse(stp.WaitForIdle(200)); stp.Start(); Assert.IsTrue(stp.WaitForIdle(200)); }
static Worker() { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.IdleTimeout = Worker.IdleTimeoutConfig * 1000; stpStartInfo.PerformanceCounterInstanceName = Worker.instanceName; stpStartInfo.MaxWorkerThreads = 300; Worker.smartThreadPool = new SmartThreadPool(stpStartInfo); _getActiveThreads = () => (long)smartThreadPool.ActiveThreads; _getInUseThreads = () => (long)smartThreadPool.InUseThreads; _getQueuedWorkItems = () => (long)smartThreadPool.CurrentWorkItemsCount; _getCompletedWorkItems = () => (long)smartThreadPool.WorkItemsProcessed; }
public STPStartInfo(STPStartInfo stpStartInfo) : base(stpStartInfo) { _idleTimeout = stpStartInfo.IdleTimeout; _minWorkerThreads = stpStartInfo.MinWorkerThreads; _maxWorkerThreads = stpStartInfo.MaxWorkerThreads; #if !(WINDOWS_PHONE) _threadPriority = stpStartInfo.ThreadPriority; #endif _performanceCounterInstanceName = stpStartInfo.PerformanceCounterInstanceName; _enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters; _threadPoolName = stpStartInfo._threadPoolName; _areThreadsBackground = stpStartInfo.AreThreadsBackground; #if !(_SILVERLIGHT) && !(WINDOWS_PHONE) _apartmentState = stpStartInfo._apartmentState; #endif }
public void QueueWorkItem_WhenBiggerMaxIsSet_ThrowsExceptionWhenHit() { Assert.Throws<QueueRejectedException>(() => { var info = new STPStartInfo { MaxQueueLength = 5, MinWorkerThreads = 5, MaxWorkerThreads = 10, }; var pool = new SmartThreadPool(info); pool.Start(); try { // Pool starts with 5 available waiters. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // Taken by waiter immediately. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // New thread created, takes work item. Not queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. pool.QueueWorkItem(SleepForOneSecond); // No waiters available. Queued. } catch (QueueRejectedException e) { throw new Exception("Caught QueueRejectedException too early: ", e); } // All threads are busy, and queue is at its max. Throws. pool.QueueWorkItem(SleepForOneSecond); }); }
private void Concurrency( int concurrencyPerWig, int wigsCount, int workItemsCount) { Console.WriteLine( "Testing : concurrencyPerWig = {0}, wigsCount = {1}, workItemsCount = {2}", concurrencyPerWig, wigsCount, workItemsCount); _success = true; _concurrencyPerWig = concurrencyPerWig; _randGen = new Random(0); STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); _concurrentOps = new int[wigsCount]; IWorkItemsGroup [] wigs = new IWorkItemsGroup[wigsCount]; for(int i = 0; i < wigs.Length; ++i) { wigs[i] = stp.CreateWorkItemsGroup(_concurrencyPerWig); for(int j = 0; j < workItemsCount; ++j) { wigs[i].QueueWorkItem(new WorkItemCallback(this.DoWork), i); } wigs[i].Start(); } stp.Start(); stp.WaitForIdle(); Assert.IsTrue(_success); stp.Shutdown(); }
// Can't run this test, StackOverflowException crashes the application and can't be catched and ignored //[Test] public void NotTestThreadsMaxStackSize() { STPStartInfo stpStartInfo = new STPStartInfo() { MaxStackSize = 64 * 1024, }; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); stp.Start(); IWorkItemResult<bool> wir = stp.QueueWorkItem(() => AllocateBufferOnStack(10 * 1024)); bool result = wir.GetResult(); Assert.IsTrue(result); wir = stp.QueueWorkItem(() => AllocateBufferOnStack(1000 * 1024)); result = wir.GetResult(); Assert.IsFalse(result); }
private void Start_Click(object sender, RoutedEventArgs e) { btnStart.IsEnabled = false; btnStop.IsEnabled = true; UpdateControls(true); workItemsCompleted = 0; workItemsGenerated = 0; STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.IdleTimeout = _spinIdleTimeout.Value * 1000; stpStartInfo.MaxWorkerThreads = _spinMaxThreads.Value; stpStartInfo.MinWorkerThreads = _spinMinThreads.Value; stpStartInfo.EnableLocalPerformanceCounters = true; _stp = new SmartThreadPool(stpStartInfo); workItemsProducerThread = new Thread(WorkItemsProducer) {IsBackground = true}; workItemsProducerThread.Start(); }
public void CancelInQueueWorkItem() { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem(arg => null); wir.Cancel(); Assert.IsTrue(wir.IsCanceled); try { wir.GetResult(); } finally { stp.Shutdown(); } }
public PollServiceRequestManager( BaseHttpServer pSrv, bool performResponsesAsync, uint pWorkerThreadCount, int pTimeout) { m_server = pSrv; m_WorkerThreadCount = pWorkerThreadCount; m_workerThreads = new Thread[m_WorkerThreadCount]; PollServiceHttpRequestComparer preqCp = new PollServiceHttpRequestComparer(); m_bycontext = new Dictionary<PollServiceHttpRequest, Queue<PollServiceHttpRequest>>(preqCp); STPStartInfo startInfo = new STPStartInfo(); startInfo.IdleTimeout = 30000; startInfo.MaxWorkerThreads = 20; startInfo.MinWorkerThreads = 1; startInfo.ThreadPriority = ThreadPriority.Normal; startInfo.StartSuspended = true; startInfo.ThreadPoolName = "PoolService"; m_threadPool = new SmartThreadPool(startInfo); }
public void DoWork(object [] states) { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool smartThreadPool = new SmartThreadPool(stpStartInfo); foreach(object state in states) { smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), state); } // Start working on the work items in the queue smartThreadPool.Start(); // Wait for the completion of all work items smartThreadPool.WaitForIdle(); smartThreadPool.Shutdown(); }
public void CancelInQueueWorkItem() { Assert.Throws<WorkItemCancelException>(() => { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; bool hasRun = false; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem( new WorkItemInfo() {Timeout = 500}, state => { hasRun = true; return null; }); Assert.IsFalse(wir.IsCanceled); Thread.Sleep(2000); Assert.IsTrue(wir.IsCanceled); stp.Start(); stp.WaitForIdle(); Assert.IsFalse(hasRun); try { wir.GetResult(); } finally { stp.Shutdown(); } }); }
public void DoWork() { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool smartThreadPool = new SmartThreadPool(); smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), "Queued first", WorkItemPriority.BelowNormal); smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), "Queued second", WorkItemPriority.AboveNormal); smartThreadPool.Start(); smartThreadPool.WaitForIdle(); smartThreadPool.Shutdown(); }
public void Start() { STPStartInfo START_INFO = new STPStartInfo { WorkItemPriority = Amib.Threading.WorkItemPriority.Normal, MinWorkerThreads = 0, MaxWorkerThreads = Config.Settings.Instance.CFWorkerThreads, IdleTimeout = THREADPOOL_IDLE_TIMEOUT, }; _threadPool = new SmartThreadPool(START_INFO); _threadPool.Name = "Cloudfiles"; Func<CloudFilesAssetWorker> ctorFunc = () => { return new CloudFilesAssetWorker(_readTimeout, _writeTimeout); }; _asyncAssetWorkers = new ObjectPool<CloudFilesAssetWorker>(Config.Settings.Instance.CFWorkerThreads * 2, ctorFunc); _assetCache = new Cache.Cache(MAX_IDLE_CACHE_ENTRY_AGE); if (!Config.Settings.Instance.DisableWritebackCache) { _diskWriteBack = new Cache.DiskWriteBackCache(); _diskWriteBack.Start(); } //start maintaining the cache _maintTimer = new Timer(this.Maintain, null, MAINTENANCE_INTERVAL, MAINTENANCE_INTERVAL); }
/// <summary> /// Constructor /// </summary> public SmartThreadPool() { _stpStartInfo = new STPStartInfo(); Initialize(); }
public STPStartInfoRO(STPStartInfo stpStartInfo) { _stpStartInfo = stpStartInfo; }
//private int CacheRegionsDistance = 256; #region INonSharedRegionModule Members public virtual void Initialise (IConfigSource config) { IConfig startupConfig = config.Configs["Startup"]; if (startupConfig.GetString("WorldMapModule", "WorldMap") == "WorldMap") m_Enabled = true; STPStartInfo reqPoolStartInfo = new STPStartInfo(); reqPoolStartInfo.MaxWorkerThreads = 2; reqPoolStartInfo.IdleTimeout = 5 * 60 * 1000; reqPoolStartInfo.ThreadPriority = ThreadPriority.Lowest; STPStartInfo infoReqPoolStartInfo = new STPStartInfo(); reqPoolStartInfo.MaxWorkerThreads = 4; reqPoolStartInfo.IdleTimeout = 5 * 60 * 1000; reqPoolStartInfo.ThreadPriority = ThreadPriority.Lowest; _blockRequestPool = new SmartThreadPool(reqPoolStartInfo); _blockRequestPool.Name = "Map Block Requests"; _infoRequestPool = new SmartThreadPool(infoReqPoolStartInfo); _infoRequestPool.Name = "Map Info Requests"; }
/// <summary> /// Constructor /// </summary> /// <param name="stpStartInfo">A SmartThreadPool configuration that overrides the default behavior</param> public SmartThreadPool(STPStartInfo stpStartInfo) { _stpStartInfo = new STPStartInfo(stpStartInfo); Initialize(); }
/// <summary> /// Constructor /// </summary> /// <param name="idleTimeout">Idle timeout in milliseconds</param> /// <param name="maxWorkerThreads">Upper limit of threads in the pool</param> /// <param name="minWorkerThreads">Lower limit of threads in the pool</param> public SmartThreadPool( int idleTimeout, int maxWorkerThreads, int minWorkerThreads) { _stpStartInfo = new STPStartInfo { IdleTimeout = idleTimeout, MaxWorkerThreads = maxWorkerThreads, MinWorkerThreads = minWorkerThreads, }; Initialize(); }