Synchronize open/close on class as well as instance

This commit is contained in:
Will Hedgecock 2021-11-19 12:44:28 -06:00
parent bcc9e1e19f
commit b52271c48c
2 changed files with 62 additions and 54 deletions

View File

@ -7,7 +7,7 @@ buildscript {
plugins {
id 'maven-publish'
id 'biz.aQute.bnd.builder' version '5.0.0'
id 'biz.aQute.bnd.builder' version '6.0.0'
}
group = 'com.fazecast'

View File

@ -2,7 +2,7 @@
* SerialPort.java
*
* Created on: Feb 25, 2012
* Last Updated on: Nov 12, 2021
* Last Updated on: Nov 19, 2021
* Author: Will Hedgecock
*
* Copyright (C) 2012-2021 Fazecast, Inc.
@ -418,56 +418,60 @@ public final class SerialPort
*/
public final synchronized boolean openPort(int safetySleepTime, int deviceSendQueueSize, int deviceReceiveQueueSize)
{
// Set the send/receive internal buffer sizes, and return true if already opened
safetySleepTimeMS = safetySleepTime;
sendDeviceQueueSize = deviceSendQueueSize;
receiveDeviceQueueSize = deviceReceiveQueueSize;
if (portHandle > 0)
return configPort(portHandle);
// Force a sleep to ensure that the port does not become unusable due to rapid closing/opening on the part of the user
if (safetySleepTimeMS > 0)
try { Thread.sleep(safetySleepTimeMS); } catch (Exception e) { Thread.currentThread().interrupt(); }
// If this is an Android root application, we must explicitly allow serial port access to the library
File portFile = isAndroid ? new File(comPort) : null;
if (portFile != null && (!portFile.canRead() || !portFile.canWrite()))
{
Process process = null;
try
// Synchronize this method to the class scope as well
synchronized (SerialPort.class)
{
// Set the send/receive internal buffer sizes, and return true if already opened
safetySleepTimeMS = safetySleepTime;
sendDeviceQueueSize = deviceSendQueueSize;
receiveDeviceQueueSize = deviceReceiveQueueSize;
if (portHandle > 0)
return configPort(portHandle);
// Force a sleep to ensure that the port does not become unusable due to rapid closing/opening on the part of the user
if (safetySleepTimeMS > 0)
try { Thread.sleep(safetySleepTimeMS); } catch (Exception e) { Thread.currentThread().interrupt(); }
// If this is an Android root application, we must explicitly allow serial port access to the library
File portFile = isAndroid ? new File(comPort) : null;
if (portFile != null && (!portFile.canRead() || !portFile.canWrite()))
{
process = Runtime.getRuntime().exec("su");
DataOutputStream writer = new DataOutputStream(process.getOutputStream());
writer.writeBytes("chmod 666 " + comPort + "\n");
writer.writeBytes("exit\n");
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (reader.readLine() != null);
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
finally
{
if (process == null)
Process process = null;
try
{
process = Runtime.getRuntime().exec("su");
DataOutputStream writer = new DataOutputStream(process.getOutputStream());
writer.writeBytes("chmod 666 " + comPort + "\n");
writer.writeBytes("exit\n");
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (reader.readLine() != null);
}
catch (Exception e)
{
e.printStackTrace();
return false;
try { process.waitFor(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; }
try { process.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { process.getOutputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { process.getErrorStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; }
}
finally
{
if (process == null)
return false;
try { process.waitFor(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; }
try { process.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { process.getOutputStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { process.getErrorStream().close(); } catch (IOException e) { e.printStackTrace(); return false; }
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; }
}
}
}
// Open the serial port and start an event-based listener if registered
if ((portHandle = openPortNative()) > 0)
{
if (serialEventListener != null)
serialEventListener.startListening();
}
return (portHandle > 0);
// Open the serial port and start an event-based listener if registered
if ((portHandle = openPortNative()) > 0)
{
if (serialEventListener != null)
serialEventListener.startListening();
}
return (portHandle > 0);
}
}
/**
@ -504,11 +508,15 @@ public final class SerialPort
*/
public final synchronized boolean closePort()
{
if (serialEventListener != null)
serialEventListener.stopListening();
if (portHandle > 0)
portHandle = closePortNative(portHandle);
return (portHandle < 0);
// Synchronize this method to the class scope as well
synchronized (SerialPort.class)
{
if (serialEventListener != null)
serialEventListener.stopListening();
if (portHandle > 0)
portHandle = closePortNative(portHandle);
return (portHandle < 0);
}
}
/**