Add DynamicPool
parent
6ffce5cc2d
commit
0d2d128b5f
|
@ -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>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue