Make ds4 threads background threads. Still trying to make output thread exit gracefully.

The switch to using background threads reduced input lag.
This commit is contained in:
Travis Nickles 2017-03-24 08:06:01 -07:00
parent 9d190fcaf7
commit 156b7103e6

View File

@ -143,6 +143,8 @@ namespace DS4Windows
private bool charging; private bool charging;
private bool outputRumble = false; private bool outputRumble = false;
private int warnInterval = WARN_INTERVAL_USB; private int warnInterval = WARN_INTERVAL_USB;
private bool exitOutputThread = false;
private object exitLocker = new object();
public event EventHandler<EventArgs> Report = null; public event EventHandler<EventArgs> Report = null;
public event EventHandler<EventArgs> Removal = null; public event EventHandler<EventArgs> Removal = null;
@ -280,6 +282,7 @@ namespace DS4Windows
ds4Output = new Thread(performDs4Output); ds4Output = new Thread(performDs4Output);
ds4Output.Priority = ThreadPriority.AboveNormal; ds4Output.Priority = ThreadPriority.AboveNormal;
ds4Output.Name = "DS4 Output thread: " + Mac; ds4Output.Name = "DS4 Output thread: " + Mac;
ds4Output.IsBackground = true;
if (conType == ConnectionType.BT) if (conType == ConnectionType.BT)
{ {
// Only use the output thread for Bluetooth connections. // Only use the output thread for Bluetooth connections.
@ -290,6 +293,7 @@ namespace DS4Windows
ds4Input = new Thread(performDs4Input); ds4Input = new Thread(performDs4Input);
ds4Input.Priority = ThreadPriority.AboveNormal; ds4Input.Priority = ThreadPriority.AboveNormal;
ds4Input.Name = "DS4 Input thread: " + Mac; ds4Input.Name = "DS4 Input thread: " + Mac;
ds4Input.IsBackground = true;
ds4Input.Start(); ds4Input.Start();
} }
else else
@ -298,8 +302,8 @@ namespace DS4Windows
public void StopUpdate() public void StopUpdate()
{ {
if (ds4Input.IsAlive && ds4Input.ThreadState != System.Threading.ThreadState.Stopped && if (ds4Input.IsAlive && !ds4Input.ThreadState.HasFlag(System.Threading.ThreadState.Stopped) &&
ds4Input.ThreadState != System.Threading.ThreadState.AbortRequested) !ds4Input.ThreadState.HasFlag(System.Threading.ThreadState.AbortRequested))
{ {
try try
{ {
@ -316,17 +320,26 @@ namespace DS4Windows
private void StopOutputUpdate() private void StopOutputUpdate()
{ {
if (ds4Output.IsAlive && ds4Output.ThreadState != System.Threading.ThreadState.Stopped && lock (exitLocker)
ds4Output.ThreadState != System.Threading.ThreadState.AbortRequested)
{ {
try if (ds4Output.IsAlive && !ds4Output.ThreadState.HasFlag(System.Threading.ThreadState.Stopped) &&
!ds4Output.ThreadState.HasFlag(System.Threading.ThreadState.AbortRequested))
{ {
ds4Output.Abort(); try
ds4Output.Join(); {
} exitOutputThread = true;
catch (Exception e) lock (outputReport)
{ {
Console.WriteLine(e.Message); Monitor.PulseAll(outputReport);
}
//ds4Output.Interrupt();
ds4Output.Join();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
} }
} }
} }
@ -348,7 +361,7 @@ namespace DS4Windows
lock (outputReport) lock (outputReport)
{ {
int lastError = 0; int lastError = 0;
while (true) while (!exitOutputThread)
{ {
bool result = false; bool result = false;
if (outputRumble) if (outputRumble)
@ -635,13 +648,10 @@ namespace DS4Windows
outputReportBuffer.CopyTo(outputReport, 0); outputReportBuffer.CopyTo(outputReport, 0);
try try
{ {
if (!writeOutput() && ds4Output.IsAlive && if (!writeOutput())
ds4Output.ThreadState != System.Threading.ThreadState.Stopped &&
ds4Output.ThreadState != System.Threading.ThreadState.AbortRequested)
{ {
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> encountered synchronous write failure: " + Marshal.GetLastWin32Error()); Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> encountered synchronous write failure: " + Marshal.GetLastWin32Error());
ds4Output.Abort(); StopOutputUpdate();
ds4Output.Join();
} }
} }
catch catch