80 lines
1.9 KiB
C#
80 lines
1.9 KiB
C#
using System;
|
|
using System.Threading;
|
|
|
|
namespace ln.types.threads
|
|
{
|
|
public class PoolThreadMustExitException : Exception
|
|
{}
|
|
|
|
public class PoolThread
|
|
{
|
|
public static ThreadLocal<PoolThread> CurrentPoolThread { get; } = new ThreadLocal<PoolThread>();
|
|
|
|
public Pool Pool { get; private set; }
|
|
public Thread Thread { get; }
|
|
|
|
public PoolJob CurrentJob { get; private set; }
|
|
|
|
private Action ThreadLoop;
|
|
|
|
public PoolThread(Action threadLoop)
|
|
{
|
|
ThreadLoop = threadLoop;
|
|
|
|
Thread = new Thread(thread);
|
|
Thread.Start();
|
|
}
|
|
|
|
private void thread()
|
|
{
|
|
lock (this)
|
|
{
|
|
CurrentPoolThread.Value = this;
|
|
}
|
|
|
|
ThreadLoop();
|
|
|
|
lock (this)
|
|
{
|
|
CurrentPoolThread.Value = null;
|
|
}
|
|
}
|
|
//while (Pool.State != PoolState.SHUTDOWN)
|
|
//{
|
|
// PoolJob poolJob = null;
|
|
// try
|
|
// {
|
|
// poolJob = Pool.Dequeue();
|
|
// }
|
|
// catch (TimeoutException)
|
|
// {
|
|
// break;
|
|
// }
|
|
|
|
// if (poolJob != null)
|
|
// {
|
|
// State = PoolThreadState.WORKING;
|
|
// CurrentJob = poolJob;
|
|
// CurrentJob.Run(Pool);
|
|
|
|
// if (Pool.PoolJobFinished != null)
|
|
// Pool.PoolJobFinished.Invoke(Pool, CurrentJob);
|
|
|
|
// CurrentJob = null;
|
|
// State = PoolThreadState.READY;
|
|
// }
|
|
//}
|
|
|
|
//CurrentPoolThread.Value = null;
|
|
|
|
//State = PoolThreadState.EXITED;
|
|
|
|
//lock (Pool.poolThreads)
|
|
//{
|
|
// Pool.poolThreads.Remove(this);
|
|
// Monitor.Pulse(Pool.poolThreads);
|
|
//}
|
|
|
|
}
|
|
}
|