using Microsoft.AspNet.Server.Kestrel.Networking;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Server.Kestrel
{
///
/// Summary description for KestrelThread
///
public class KestrelThread
{
KestrelEngine _engine;
Thread _thread;
UvLoopHandle _loop;
UvAsyncHandle _post;
Queue _workAdding = new Queue();
Queue _workRunning = new Queue();
object _workSync = new Object();
bool _stopImmediate = false;
public KestrelThread(KestrelEngine engine)
{
_engine = engine;
_loop = new UvLoopHandle();
_post = new UvAsyncHandle();
_thread = new Thread(ThreadStart);
}
public UvLoopHandle Loop { get { return _loop; } }
public Task StartAsync()
{
var tcs = new TaskCompletionSource();
_thread.Start(tcs);
return tcs.Task;
}
public void Stop(TimeSpan timeout)
{
Post(OnStop, null);
if (!_thread.Join(timeout))
{
Post(OnStopImmediate, null);
if (!_thread.Join(timeout))
{
_thread.Abort();
}
}
}
private void OnStop(object obj)
{
_post.Unreference();
}
private void OnStopImmediate(object obj)
{
_stopImmediate = true;
_loop.Stop();
}
public void Post(Action