Only globally synchronize open/close port methods on the native function call

This commit is contained in:
Will Hedgecock 2022-02-14 11:41:00 -06:00
parent 60b90199bf
commit a9444ffbad
3 changed files with 67 additions and 61 deletions

View File

@ -5,8 +5,8 @@ include $(CLEAR_VARS)
LOCAL_MODULE := jSerialComm LOCAL_MODULE := jSerialComm
TARGET_OUT := ../../resources/Android/$(TARGET_ARCH_ABI) TARGET_OUT := ../../resources/Android/$(TARGET_ARCH_ABI)
LOCAL_SRC_FILES := ../SerialPort_Posix.c ../PosixHelperFunctions.c LOCAL_SRC_FILES := ../SerialPort_Posix.c ../PosixHelperFunctions.c
LOCAL_CFLAGS := -fsigned-char -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
LOCAL_LDLIBS := -llog LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -fsigned-char
include $(BUILD_SHARED_LIBRARY) include $(BUILD_SHARED_LIBRARY)

View File

@ -1,3 +1,4 @@
APP_ABI := all APP_ABI := all
APP_PLATFORM := android-21 APP_PLATFORM := android-21
APP_MODULES := jSerialComm APP_MODULES := jSerialComm
APP_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0

View File

@ -501,7 +501,9 @@ public final class SerialPort
serialPort.friendlyName = "User-Specified Port"; serialPort.friendlyName = "User-Specified Port";
serialPort.portDescription = "User-Specified Port"; serialPort.portDescription = "User-Specified Port";
serialPort.portLocation = "0-0"; serialPort.portLocation = "0-0";
synchronized (SerialPort.class) {
serialPort.retrievePortDetails(); serialPort.retrievePortDetails();
}
return serialPort; return serialPort;
} }
@ -583,9 +585,6 @@ public final class SerialPort
* @return Whether the port was successfully opened with a valid configuration. * @return Whether the port was successfully opened with a valid configuration.
*/ */
public final synchronized boolean openPort(int safetySleepTime, int deviceSendQueueSize, int deviceReceiveQueueSize) public final synchronized boolean openPort(int safetySleepTime, int deviceSendQueueSize, int deviceReceiveQueueSize)
{
// 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 // Set the send/receive internal buffer sizes, and return true if already opened
safetySleepTimeMS = safetySleepTime; safetySleepTimeMS = safetySleepTime;
@ -630,15 +629,17 @@ public final class SerialPort
} }
} }
// Open the serial port and start an event-based listener if registered // Natively open the serial port, and synchronize to the class scope since port enumeration methods are class-based,
if ((portHandle = openPortNative()) != 0) // and this method may alter or read a global class structure in native code
{ synchronized (SerialPort.class) {
if (serialEventListener != null) portHandle = openPortNative();
}
// Start an event-based listener if registered and the port is open
if ((portHandle != 0) && (serialEventListener != null))
serialEventListener.startListening(); serialEventListener.startListening();
}
return (portHandle != 0); return (portHandle != 0);
} }
}
/** /**
* Opens this serial port for reading and writing with an optional delay time. * Opens this serial port for reading and writing with an optional delay time.
@ -684,16 +685,20 @@ public final class SerialPort
*/ */
public final synchronized boolean closePort() public final synchronized boolean closePort()
{ {
// Synchronize this method to the class scope as well // Stop a registered event listener
synchronized (SerialPort.class)
{
if (serialEventListener != null) if (serialEventListener != null)
serialEventListener.stopListening(); serialEventListener.stopListening();
// Natively close the port, and synchronize to the class scope since port enumeration methods are class-based,
// and this method may alter or read a global class structure in native code
if (portHandle != 0) if (portHandle != 0)
{
synchronized (SerialPort.class) {
portHandle = closePortNative(portHandle); portHandle = closePortNative(portHandle);
return (portHandle == 0);
} }
} }
return (portHandle == 0);
}
/** /**
* Returns whether the port is currently open and available for communication. * Returns whether the port is currently open and available for communication.