mirror of
https://github.com/farcasclaudiu/SmartThreadPool.git
synced 2026-06-23 21:00:41 +03:00
Added support for Silverlight and Mono (And more)
Also added: * Join, Choice, and Pipe to SmartThreadPool. * Local performance counters (for Mono, Silverlight, and WindowsCE)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
||||
<PropertyGroup>
|
||||
<ProjectType>Local</ProjectType>
|
||||
<ProductVersion>9.0.30729</ProductVersion>
|
||||
<ProductVersion>9.0.21022</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{6A3E4DBF-12AD-4636-ACB3-24B5172FAE03}</ProjectGuid>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
@@ -65,6 +65,7 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DebugType>full</DebugType>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<UseVSHostingProcess>false</UseVSHostingProcess>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
@@ -89,16 +90,6 @@
|
||||
<DebugType>full</DebugType>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseCE|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\ReleaseCE\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<BaseAddress>285212672</BaseAddress>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="nunit.framework, Version=2.4.8.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
|
||||
<Reference Include="System">
|
||||
@@ -120,6 +111,7 @@
|
||||
<Compile Include="AssemblyInfo.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="TestParallelMethods.cs" />
|
||||
<Compile Include="TestFalseFillStateWithParams.cs" />
|
||||
<Compile Include="TestFillStateWithParams.cs" />
|
||||
<Compile Include="TestWIGFillStateWithParams.cs" />
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Amib.Threading;
|
||||
|
||||
namespace SmartThreadPoolTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for TestParallelMethods.
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
[Category("TestParallelMethods")]
|
||||
public class TestParallelMethods
|
||||
{
|
||||
[Test]
|
||||
public void TestJoin()
|
||||
{
|
||||
SmartThreadPool stp = new SmartThreadPool();
|
||||
|
||||
SafeCounter sc = new SafeCounter();
|
||||
|
||||
stp.Join(
|
||||
sc.Increment,
|
||||
sc.Increment,
|
||||
sc.Increment);
|
||||
|
||||
Assert.AreEqual(3, sc.Counter);
|
||||
|
||||
for (int j = 0; j < 10; j++)
|
||||
{
|
||||
sc.Reset();
|
||||
|
||||
Action[] actions = new Action[1000];
|
||||
for (int i = 0; i < actions.Length; i++)
|
||||
{
|
||||
actions[i] = sc.Increment;
|
||||
}
|
||||
|
||||
stp.Join(actions);
|
||||
|
||||
Assert.AreEqual(actions.Length, sc.Counter);
|
||||
}
|
||||
|
||||
stp.Shutdown();
|
||||
}
|
||||
|
||||
private class SafeCounter
|
||||
{
|
||||
private int _counter;
|
||||
|
||||
public void Increment()
|
||||
{
|
||||
Interlocked.Increment(ref _counter);
|
||||
}
|
||||
|
||||
public int Counter
|
||||
{
|
||||
get { return _counter; }
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestChoice()
|
||||
{
|
||||
SmartThreadPool stp = new SmartThreadPool();
|
||||
|
||||
int index = stp.Choice(
|
||||
() => Thread.Sleep(1000),
|
||||
() => Thread.Sleep(1500),
|
||||
() => Thread.Sleep(500));
|
||||
|
||||
Assert.AreEqual(2, index);
|
||||
|
||||
index = stp.Choice(
|
||||
() => Thread.Sleep(300),
|
||||
() => Thread.Sleep(100),
|
||||
() => Thread.Sleep(200));
|
||||
|
||||
Assert.AreEqual(1, index);
|
||||
|
||||
stp.Shutdown();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPipe()
|
||||
{
|
||||
SafeCounter sc = new SafeCounter();
|
||||
SmartThreadPool stp = new SmartThreadPool();
|
||||
|
||||
stp.Pipe(
|
||||
sc,
|
||||
sc1 => { if (sc.Counter == 0) { sc1.Increment(); }},
|
||||
sc1 => { if (sc.Counter == 1) { sc1.Increment(); }},
|
||||
sc1 => { if (sc.Counter == 2) { sc1.Increment(); }}
|
||||
);
|
||||
|
||||
Assert.AreEqual(3, sc.Counter);
|
||||
|
||||
stp.Shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
using NUnit.Framework;
|
||||
@@ -73,11 +72,10 @@ namespace SmartThreadPoolTests
|
||||
|
||||
stp.Start();
|
||||
|
||||
Assert.IsTrue(wig.WaitForIdle(200));
|
||||
Assert.IsTrue(stp.WaitForIdle(0));
|
||||
Assert.IsTrue(wig.WaitForIdle(5000), "WIG is not idle");
|
||||
Assert.IsTrue(stp.WaitForIdle(5000), "STP is not idle");
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void TwoWIGsStartSuspended()
|
||||
{
|
||||
|
||||
@@ -18,30 +18,37 @@ namespace SmartThreadPoolTests
|
||||
[Test]
|
||||
public void TestDefaultPriority()
|
||||
{
|
||||
SmartThreadPool stp = new SmartThreadPool();
|
||||
|
||||
IWorkItemResult wir = stp.QueueWorkItem(new WorkItemCallback(DoSomeWork));
|
||||
ThreadPriority currentThreadPriority = (ThreadPriority)wir.GetResult();
|
||||
|
||||
Assert.AreEqual(currentThreadPriority, SmartThreadPool.DefaultThreadPriority);
|
||||
CheckSinglePriority(SmartThreadPool.DefaultThreadPriority);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPriorities()
|
||||
public void TestLowestPriority()
|
||||
{
|
||||
ThreadPriority [] priorities =
|
||||
{
|
||||
ThreadPriority.Lowest,
|
||||
ThreadPriority.BelowNormal,
|
||||
ThreadPriority.Normal,
|
||||
ThreadPriority.AboveNormal,
|
||||
ThreadPriority.Highest,
|
||||
};
|
||||
CheckSinglePriority(ThreadPriority.Lowest);
|
||||
}
|
||||
|
||||
foreach(ThreadPriority priority in priorities)
|
||||
{
|
||||
CheckSinglePriority(priority);
|
||||
}
|
||||
[Test]
|
||||
public void TestBelowNormalPriority()
|
||||
{
|
||||
CheckSinglePriority(ThreadPriority.BelowNormal);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestNormalPriority()
|
||||
{
|
||||
CheckSinglePriority(ThreadPriority.BelowNormal);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestAboveNormalPriority()
|
||||
{
|
||||
CheckSinglePriority(ThreadPriority.AboveNormal);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHighestPriority()
|
||||
{
|
||||
CheckSinglePriority(ThreadPriority.Highest);
|
||||
}
|
||||
|
||||
private void CheckSinglePriority(ThreadPriority threadPriority)
|
||||
@@ -51,13 +58,13 @@ namespace SmartThreadPoolTests
|
||||
|
||||
SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
|
||||
|
||||
IWorkItemResult wir = stp.QueueWorkItem(new WorkItemCallback(DoSomeWork));
|
||||
IWorkItemResult wir = stp.QueueWorkItem(new WorkItemCallback(GetThreadPriority));
|
||||
ThreadPriority currentThreadPriority = (ThreadPriority)wir.GetResult();
|
||||
|
||||
Assert.AreEqual(currentThreadPriority, threadPriority);
|
||||
Assert.AreEqual(threadPriority, currentThreadPriority);
|
||||
}
|
||||
|
||||
private object DoSomeWork(object state)
|
||||
private object GetThreadPriority(object state)
|
||||
{
|
||||
return Thread.CurrentThread.Priority;
|
||||
}
|
||||
|
||||
@@ -152,5 +152,23 @@ namespace WorkItemsGroupTests
|
||||
|
||||
smartThreadPool.Shutdown();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WaitForIdleEvent()
|
||||
{
|
||||
SmartThreadPool smartThreadPool = new SmartThreadPool();
|
||||
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(1);
|
||||
ManualResetEvent wigIsIdle = new ManualResetEvent(false);
|
||||
|
||||
workItemsGroup.OnIdle += wig => wigIsIdle.Set();
|
||||
|
||||
workItemsGroup.QueueWorkItem(() => { });
|
||||
|
||||
bool eventFired = wigIsIdle.WaitOne(100, true);
|
||||
|
||||
smartThreadPool.Shutdown();
|
||||
|
||||
Assert.IsTrue(eventFired);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user