Add DynamicPool

dev_timestamp
Harald Wolff 2019-09-11 09:28:16 +02:00
parent 6ffce5cc2d
commit 0d2d128b5f
2 changed files with 62 additions and 0 deletions

View File

@ -97,6 +97,7 @@
<Compile Include="odb\ng\index\IndexPath.cs" />
<Compile Include="PathHelper.cs" />
<Compile Include="threads\ThreadHelpers.cs" />
<Compile Include="threads\DynamicPool.cs" />
<Compile Include="threads\PoolThread.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,61 @@
using System;
using System.Threading;
namespace ln.types.threads
{
public class DynamicPool : Pool
{
public int Timeout { get; set; }
public DynamicPool() :this(Environment.ProcessorCount){}
public DynamicPool(int maxPoolSize)
:base(maxPoolSize)
{
Timeout = 15000;
}
public override void Start()
{
if ((State == PoolState.RUN) || (State == PoolState.SHUTDOWN))
throw new NotSupportedException("Pool can only be started if not running");
State = PoolState.RUN;
}
protected override PoolJob WaitForJob(PoolThread poolThread)
{
if (State == PoolState.RUN)
{
lock (waitingThreads)
{
PoolJob poolJob = Dequeue(poolThread);
if (poolJob != null)
return poolJob;
waitingThreads.Add(poolThread);
Monitor.Wait(waitingThreads, Timeout);
waitingThreads.Remove(poolThread);
poolJob = Dequeue(poolThread);
return poolJob;
}
}
return null;
}
protected override void PulseWaitingThread()
{
lock (waitingThreads)
{
if (waitingThreads.Count > 0)
{
Monitor.Pulse(waitingThreads);
}
else if (CurrentPoolSize < PoolSize)
{
CreatePoolThread();
}
}
}
}
}