Added refactorings suggested by Simon Cropp

This commit is contained in:
Ami Bar
2009-12-19 17:33:30 +02:00
parent 1bd7e4d104
commit d2c14da5ad
62 changed files with 785 additions and 1131 deletions
-44
View File
@@ -1,11 +1,5 @@
using System.Reflection;
using System.Runtime.CompilerServices;
//
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
//
[assembly: AssemblyTitle("")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
@@ -14,45 +8,7 @@ using System.Runtime.CompilerServices;
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
//
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")]
//
// In order to sign your assembly you must specify a key to use. Refer to the
// Microsoft .NET Framework documentation for more information on assembly signing.
//
// Use the attributes below to control which key is used for signing.
//
// Notes:
// (*) If no key is specified, the assembly is not signed.
// (*) KeyName refers to a key that has been installed in the Crypto Service
// Provider (CSP) on your machine. KeyFile refers to a file which contains
// a key.
// (*) If the KeyFile and the KeyName values are both specified, the
// following processing occurs:
// (1) If the KeyName can be found in the CSP, that key is used.
// (2) If the KeyName does not exist and the KeyFile does exist, the key
// in the KeyFile is installed into the CSP and used.
// (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
// When specifying the KeyFile, the location of the KeyFile should be
// relative to the project output directory which is
// %Project Directory%\obj\<configuration>. For example, if your KeyFile is
// located in the project directory, you would specify the AssemblyKeyFile
// attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
// documentation for more information on this.
//
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]
-3
View File
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using NUnit.Framework;
using Amib.Threading;
/*
* The code below generates permutations.
*
+6 -18
View File
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using Amib.Threading.Internal;
using NUnit.Framework;
using Amib.Threading;
@@ -628,25 +629,12 @@ namespace SmartThreadPoolTests
Assert.IsTrue(success);
}
private static WorkItemInfo GetCurrentWorkItemInfo()
{
object threadEntry = typeof(SmartThreadPool).GetField("_threadEntry", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
object workitem = threadEntry.GetType().GetField("_currentWorkItem", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(threadEntry);
WorkItemInfo wii = (WorkItemInfo)workitem.GetType().GetField("_workItemInfo", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(workitem);
return wii;
}
private class WorkItemInfoComparer
{
private WorkItemInfo _neededWorkItemInfo;
private object _state;
private int _sleepTime = 0;
public int SleepTime
{
get { return _sleepTime; }
set { _sleepTime = value; }
}
public int SleepTime { get; set; }
public WorkItemInfoComparer(WorkItemInfo workItemInfo)
{
@@ -665,12 +653,12 @@ namespace SmartThreadPoolTests
bool equals = object.Equals(_state, state);
if (equals)
{
WorkItemInfo currentWorkItemInfo = GetCurrentWorkItemInfo();
WorkItemInfo currentWorkItemInfo = SmartThreadPool.CurrentThreadEntry.CurrentWorkItem.WorkItemInfo;
equals = CompareWorkItemInfo(currentWorkItemInfo, _neededWorkItemInfo);
}
if (_sleepTime > 0)
if (SleepTime > 0)
{
Thread.Sleep(_sleepTime);
Thread.Sleep(SleepTime);
}
return equals;
+10 -11
View File
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using NUnit.Framework;
@@ -28,7 +27,7 @@ namespace SmartThreadPoolTests
stpStartInfo.StartSuspended = true;
SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
IWorkItemResult wir = stp.QueueWorkItem(delegate(object state) { return null; });
IWorkItemResult wir = stp.QueueWorkItem(arg => null);
wir.Cancel();
@@ -59,7 +58,7 @@ namespace SmartThreadPoolTests
SmartThreadPool stp = new SmartThreadPool();
IWorkItemResult wir = stp.QueueWorkItem(
delegate(object state) { waitToStart.Set(); Thread.Sleep(100); return null; }
state => { waitToStart.Set(); Thread.Sleep(100); return null; }
);
waitToStart.WaitOne();
@@ -98,7 +97,7 @@ namespace SmartThreadPoolTests
SmartThreadPool stp = new SmartThreadPool();
IWorkItemResult wir = stp.QueueWorkItem(
delegate(object state) { waitToStart.Set() ; Thread.Sleep(100); ++counter; return null; }
state => { waitToStart.Set() ; Thread.Sleep(100); ++counter; return null; }
);
waitToStart.WaitOne();
@@ -137,7 +136,7 @@ namespace SmartThreadPoolTests
SmartThreadPool stp = new SmartThreadPool();
IWorkItemResult wir = stp.QueueWorkItem(
delegate(object state) {
state => {
waitToStart.Set();
Thread.Sleep(100);
cancelled = SmartThreadPool.IsWorkItemCanceled;
@@ -174,7 +173,7 @@ namespace SmartThreadPoolTests
stpStartInfo.StartSuspended = true;
SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
IWorkItemResult wir = stp.QueueWorkItem(delegate(object state) { return null; });
IWorkItemResult wir = stp.QueueWorkItem(state => { return null; });
int counter = 0;
@@ -217,7 +216,7 @@ namespace SmartThreadPoolTests
{
SmartThreadPool stp = new SmartThreadPool();
IWorkItemResult wir = stp.QueueWorkItem(
delegate(object state) { return 1; }
state => 1
);
stp.WaitForIdle();
@@ -250,7 +249,7 @@ namespace SmartThreadPoolTests
for (int i = 0; i < 10; i++)
{
stp.QueueWorkItem(
delegate(object state) { Thread.Sleep(500); ++counter; return null; }
state => { Thread.Sleep(500); ++counter; return null; }
);
}
@@ -285,7 +284,7 @@ namespace SmartThreadPoolTests
for (int i = 0; i < 10; i++)
{
wig.QueueWorkItem(
delegate(object state) { Thread.Sleep(500); ++counter; return null; }
state => { Thread.Sleep(500); ++counter; return null; }
);
}
@@ -324,14 +323,14 @@ namespace SmartThreadPoolTests
for (int i = 0; i < 3; i++)
{
wig1.QueueWorkItem(
delegate(object state) { Interlocked.Increment(ref counter1); Thread.Sleep(500); Interlocked.Increment(ref counter1); return null; }
state => { Interlocked.Increment(ref counter1); Thread.Sleep(500); Interlocked.Increment(ref counter1); return null; }
);
}
for (int i = 0; i < 3; i++)
{
wig2.QueueWorkItem(
delegate(object state) { Thread.Sleep(500); Interlocked.Increment(ref counter2); return null; }
state => { Thread.Sleep(500); Interlocked.Increment(ref counter2); return null; }
);
}
+3 -11
View File
@@ -1,6 +1,4 @@
using System;
using System.Threading;
using NUnit.Framework;
using Amib.Threading;
@@ -14,11 +12,7 @@ namespace SmartThreadPoolTests
[Category("TestChainedDelegates")]
public class TestChainedDelegates
{
public TestChainedDelegates()
{
}
[Test]
[Test]
public void GoodCallback()
{
SmartThreadPool stp = new SmartThreadPool();
@@ -67,10 +61,8 @@ namespace SmartThreadPoolTests
{
SmartThreadPool stp = new SmartThreadPool();
PostExecuteWorkItemCallback postExecuteWorkItemCallback =
new PostExecuteWorkItemCallback(DoPostExecute);
postExecuteWorkItemCallback +=
new PostExecuteWorkItemCallback(DoPostExecute);
PostExecuteWorkItemCallback postExecuteWorkItemCallback = DoPostExecute;
postExecuteWorkItemCallback += DoPostExecute;
stp.QueueWorkItem(
new WorkItemCallback(DoWork),
+4 -10
View File
@@ -15,11 +15,7 @@ namespace SmartThreadPoolTests
[Category("TestConcurrencyChanges")]
public class TestConcurrencyChanges
{
public TestConcurrencyChanges()
{
}
/// <summary>
/// <summary>
/// Example of waiting for idle
/// </summary>
[Test]
@@ -27,8 +23,6 @@ namespace SmartThreadPoolTests
{
SmartThreadPool smartThreadPool = new SmartThreadPool(1 * 1000, 1, 0);
bool success = false;
for (int i = 0; i < 100; ++i)
{
smartThreadPool.QueueWorkItem(
@@ -36,7 +30,7 @@ namespace SmartThreadPoolTests
null);
}
success = WaitForMaxThreadsValue(smartThreadPool, 1, 1 * 1000);
bool success = WaitForMaxThreadsValue(smartThreadPool, 1, 1 * 1000);
Assert.IsTrue(success);
smartThreadPool.MaxThreads = 5;
@@ -59,9 +53,9 @@ namespace SmartThreadPoolTests
{
SmartThreadPool smartThreadPool = new SmartThreadPool(1 * 1000, 25, 0);
bool success = false;
success = WaitForMinThreadsValue(smartThreadPool, 0, 1 * 1000);
bool success = WaitForMinThreadsValue(smartThreadPool, 0, 1 * 1000);
Assert.IsTrue(success);
smartThreadPool.MinThreads = 5;
-3
View File
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Amib.Threading;
using NUnit.Framework;
+5 -10
View File
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
@@ -111,9 +110,7 @@ namespace SmartThreadPoolTests
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
bool success = true;
IWorkItemResult [] wirs = new IWorkItemResult[5];
IWorkItemResult [] wirs = new IWorkItemResult[5];
for(int i = 0; i < wirs.Length; ++i)
{
@@ -122,7 +119,7 @@ namespace SmartThreadPoolTests
}
bool timeout = !SmartThreadPool.WaitAll(wirs, 1500, true);
success = !timeout;
bool success = !timeout;
smartThreadPool.Shutdown();
@@ -138,9 +135,7 @@ namespace SmartThreadPoolTests
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
bool success = true;
IWorkItemResult [] wirs = new IWorkItemResult[5];
IWorkItemResult [] wirs = new IWorkItemResult[5];
for(int i = 0; i < wirs.Length; ++i)
{
@@ -149,7 +144,7 @@ namespace SmartThreadPoolTests
}
bool timeout = !SmartThreadPool.WaitAll(wirs, 10, true);
success = timeout;
bool success = timeout;
smartThreadPool.Shutdown();
@@ -193,7 +188,7 @@ namespace SmartThreadPoolTests
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
bool success = true;
bool success;
IWorkItemResult [] wirs = new IWorkItemResult[5];
+6 -18
View File
@@ -15,11 +15,7 @@ namespace PriorityQueueTests
[Category("TestPriorityQueue")]
public class TestPriorityQueue
{
public TestPriorityQueue()
{
}
[Test]
[Test]
public void Init()
{
PriorityQueue pq = new PriorityQueue();
@@ -149,20 +145,12 @@ namespace PriorityQueueTests
private class PriorityItem : IHasWorkItemPriority
{
private WorkItemPriority _workItemPriority;
public PriorityItem(WorkItemPriority workItemPriority)
{
WorkItemPriority = workItemPriority;
}
public PriorityItem(WorkItemPriority workItemPriority)
{
_workItemPriority = workItemPriority;
}
public WorkItemPriority WorkItemPriority
{
get
{
return _workItemPriority;
}
}
public WorkItemPriority WorkItemPriority { get; private set; }
}
}
+1 -5
View File
@@ -14,11 +14,7 @@ namespace SmartThreadPoolTests
[Category("TestStartSuspended")]
public class TestStartSuspended
{
public TestStartSuspended()
{
}
[Test]
[Test]
public void StartSuspended()
{
STPStartInfo stpStartInfo = new STPStartInfo();
+4 -15
View File
@@ -9,16 +9,11 @@ namespace SmartThreadPoolTests
{
public class CallerState
{
private int _val = 0;
public int Value
{
get { return _val; }
}
public int Value { get; private set; }
protected void IncValue()
{
++_val;
++Value;
}
}
@@ -55,11 +50,7 @@ namespace SmartThreadPoolTests
[Category("TestStateDispose")]
public class TestStateDispose
{
public TestStateDispose()
{
}
/// <summary>
/// <summary>
/// Example of non disposable caller state
/// </summary>
[Test]
@@ -108,8 +99,6 @@ namespace SmartThreadPoolTests
SmartThreadPool smartThreadPool = new SmartThreadPool(stpStartInfo);
bool success = false;
CallerState nonDisposableCallerState = new NonDisposableCallerState();
CallerState disposableCallerState = new DisposableCallerState();
@@ -124,7 +113,7 @@ namespace SmartThreadPoolTests
disposableCallerState);
wir1.GetResult();
success = (1 == nonDisposableCallerState.Value);
bool success = (1 == nonDisposableCallerState.Value);
wir2.GetResult();
+3 -10
View File
@@ -1,5 +1,4 @@
using System;
using System.Threading;
using NUnit.Framework;
@@ -31,8 +30,8 @@ namespace SmartThreadPoolTests
SmartThreadPool stp = new SmartThreadPool();
stp.OnThreadInitialization += new ThreadInitializationHandler(OnInitialization);
stp.OnThreadTermination += new ThreadTerminationHandler(OnTermination);
stp.OnThreadInitialization += OnInitialization;
stp.OnThreadTermination += OnTermination;
stp.QueueWorkItem(new WorkItemCallback(DoSomeWork), null);
@@ -72,13 +71,7 @@ namespace SmartThreadPoolTests
[ThreadStatic]
private static ThreadContextState _threadContextState;
private int _counter = 0;
public int Counter
{
get { return _counter; }
set { _counter = value; }
}
public int Counter { get; set; }
// Static member so it can be used anywhere in code of the work item method
public static ThreadContextState Current
+1 -7
View File
@@ -1,6 +1,4 @@
using System;
using System.Threading;
using NUnit.Framework;
using Amib.Threading;
@@ -14,11 +12,7 @@ namespace WorkItemsGroupTests
[Category("Test WorkItemsGroup ChainedDelegates")]
public class TestChainedDelegates
{
public TestChainedDelegates()
{
}
[Test]
[Test]
public void GoodCallback()
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
+1 -5
View File
@@ -19,11 +19,7 @@ namespace WorkItemsGroupTests
private int _concurrencyPerWig;
private bool _success;
public TestWIGConcurrency()
{
}
[Test]
[Test]
public void TestConcurrencies()
{
Concurrency(1, 1, 10);
+1 -7
View File
@@ -1,7 +1,5 @@
using System;
using System.Threading;
using System.Diagnostics;
using NUnit.Framework;
using Amib.Threading;
@@ -15,11 +13,7 @@ namespace WorkItemsGroupTests
[Category("TestWIGConcurrencyChanges")]
public class TestWIGConcurrencyChanges
{
public TestWIGConcurrencyChanges()
{
}
/// <summary>
/// <summary>
/// Example of waiting for idle
/// </summary>
[Test]
+1
View File
@@ -1,3 +1,4 @@
using System;
using Amib.Threading;
using NUnit.Framework;
+7 -14
View File
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
@@ -114,9 +113,7 @@ namespace WorkItemsGroupTests
SmartThreadPool smartThreadPool = new SmartThreadPool();
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
bool success = true;
IWorkItemResult [] wirs = new IWorkItemResult[5];
IWorkItemResult [] wirs = new IWorkItemResult[5];
for(int i = 0; i < wirs.Length; ++i)
{
@@ -125,7 +122,7 @@ namespace WorkItemsGroupTests
}
bool timeout = !SmartThreadPool.WaitAll(wirs, 1500, true);
success = !timeout;
bool success = !timeout;
smartThreadPool.Shutdown();
@@ -142,9 +139,7 @@ namespace WorkItemsGroupTests
SmartThreadPool smartThreadPool = new SmartThreadPool();
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
bool success = true;
IWorkItemResult [] wirs = new IWorkItemResult[5];
IWorkItemResult [] wirs = new IWorkItemResult[5];
for(int i = 0; i < wirs.Length; ++i)
{
@@ -153,7 +148,7 @@ namespace WorkItemsGroupTests
}
bool timeout = !SmartThreadPool.WaitAll(wirs, 10, true);
success = timeout;
bool success = timeout;
smartThreadPool.Shutdown();
@@ -170,7 +165,7 @@ namespace WorkItemsGroupTests
SmartThreadPool smartThreadPool = new SmartThreadPool();
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
bool success = true;
bool success;
IWorkItemResult [] wirs = new IWorkItemResult[5];
@@ -199,9 +194,7 @@ namespace WorkItemsGroupTests
SmartThreadPool smartThreadPool = new SmartThreadPool();
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
bool success = true;
IWorkItemResult [] wirs = new IWorkItemResult[5];
IWorkItemResult [] wirs = new IWorkItemResult[5];
for(int i = 0; i < wirs.Length; ++i)
{
@@ -211,7 +204,7 @@ namespace WorkItemsGroupTests
int index = SmartThreadPool.WaitAny(wirs, 10, true);
success = (index == WaitHandle.WaitTimeout);
bool success = (index == WaitHandle.WaitTimeout);
smartThreadPool.Shutdown();
-3
View File
@@ -1,6 +1,3 @@
using System;
using System.Threading;
using NUnit.Framework;
using Amib.Threading;
+4 -15
View File
@@ -9,16 +9,11 @@ namespace WorkItemsGroupTests
{
public class CallerState
{
private int _val = 0;
public int Value
{
get { return _val; }
}
public int Value { get; private set; }
protected void IncValue()
{
++_val;
++Value;
}
}
@@ -55,11 +50,7 @@ namespace WorkItemsGroupTests
[Category("WorkItemsGroup")]
public class TestStateDispose
{
public TestStateDispose()
{
}
/// <summary>
/// <summary>
/// Example of non disposable caller state
/// </summary>
[Test]
@@ -112,8 +103,6 @@ namespace WorkItemsGroupTests
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue, wigStartInfo);
bool success = false;
CallerState nonDisposableCallerState = new NonDisposableCallerState();
CallerState disposableCallerState = new DisposableCallerState();
@@ -128,7 +117,7 @@ namespace WorkItemsGroupTests
disposableCallerState);
wir1.GetResult();
success = (1 == nonDisposableCallerState.Value);
bool success = (1 == nonDisposableCallerState.Value);
wir2.GetResult();
+2 -9
View File
@@ -1,7 +1,5 @@
using System;
using System.Threading;
using System.Diagnostics;
using NUnit.Framework;
using Amib.Threading;
@@ -15,11 +13,7 @@ namespace WorkItemsGroupTests
[Category("WorkItemsGroup")]
public class TestWaitForIdle
{
public TestWaitForIdle()
{
}
/// <summary>
/// <summary>
/// Example of waiting for idle
/// </summary>
[Test]
@@ -28,7 +22,6 @@ namespace WorkItemsGroupTests
SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 25, 0);
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
bool success = false;
ManualResetEvent isRunning = new ManualResetEvent(false);
for (int i = 0; i < 4; ++i)
@@ -36,7 +29,7 @@ namespace WorkItemsGroupTests
workItemsGroup.QueueWorkItem(delegate { isRunning.WaitOne(); });
}
success = !workItemsGroup.WaitForIdle(1000);
bool success = !workItemsGroup.WaitForIdle(1000);
isRunning.Set();
+2 -7
View File
@@ -15,11 +15,7 @@ namespace SmartThreadPoolTests
[Category("TestWaitForIdle")]
public class TestWaitForIdle
{
public TestWaitForIdle()
{
}
/// <summary>
/// <summary>
/// Example of waiting for idle
/// </summary>
[Test]
@@ -27,7 +23,6 @@ namespace SmartThreadPoolTests
{
SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 25, 0);
bool success = false;
ManualResetEvent isRunning = new ManualResetEvent(false);
for(int i = 0; i < 4; ++i)
@@ -35,7 +30,7 @@ namespace SmartThreadPoolTests
smartThreadPool.QueueWorkItem(delegate { isRunning.WaitOne(); });
}
success = !smartThreadPool.WaitForIdle(1000);
bool success = !smartThreadPool.WaitForIdle(1000);
isRunning.Set();
+1 -7
View File
@@ -1,7 +1,5 @@
using System;
using System.Threading;
using System.Diagnostics;
using NUnit.Framework;
using Amib.Threading;
@@ -15,11 +13,7 @@ namespace WorkItemsGroupTests
[Category("TestWorkItemsGroups")]
public class TestWorkItemsGroups
{
public TestWorkItemsGroups()
{
}
[Test]
[Test]
public void BlockingCall()
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
+3 -13
View File
@@ -1,12 +1,6 @@
using System;
using NUnit.Framework;
using Amib.Threading;
using Amib.Threading.Internal;
using SmartThreadPoolTests;
namespace PriorityQueueTests
{
/// <summary>
@@ -16,11 +10,7 @@ namespace PriorityQueueTests
[Category("TestWorkItemsQueue")]
public class TestWorkItemsQueue
{
public TestWorkItemsQueue()
{
}
[Test]
[Test]
public void Init()
{
}
@@ -32,7 +22,7 @@ namespace PriorityQueueTests
Assert.AreEqual(0, q.WaitersCount);
WorkItemsQueue.WaiterEntry we1 = new Amib.Threading.Internal.WorkItemsQueue.WaiterEntry();
WorkItemsQueue.WaiterEntry we1 = new WorkItemsQueue.WaiterEntry();
q.PushWaiter(we1);
Assert.AreEqual(1, q.WaitersCount);
@@ -41,7 +31,7 @@ namespace PriorityQueueTests
Assert.AreEqual(1, q.WaitersCount);
WorkItemsQueue.WaiterEntry we2 = new Amib.Threading.Internal.WorkItemsQueue.WaiterEntry();
WorkItemsQueue.WaiterEntry we2 = new WorkItemsQueue.WaiterEntry();
q.PushWaiter(we2);
Assert.AreEqual(2, q.WaitersCount);