Remove all native OS calls from Critical Section

This commit is contained in:
Will Hedgecock 2022-02-16 14:10:03 -06:00
parent bc4eae88f9
commit ea7a505a26
33 changed files with 17 additions and 14 deletions

View File

@ -249,6 +249,7 @@ char getPortLocation(const char* portDirectory, char* portLocation)
{ {
isUSB = 0; isUSB = 0;
portLocation[portLocationLength++] = '0'; portLocation[portLocationLength++] = '0';
portLocation[portLocationLength] = '\0';
} }
// Clean up the dynamic memory // Clean up the dynamic memory

View File

@ -2,7 +2,7 @@
* SerialPort_Posix.c * SerialPort_Posix.c
* *
* Created on: Feb 25, 2012 * Created on: Feb 25, 2012
* Last Updated on: Feb 15, 2022 * Last Updated on: Feb 16, 2022
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2022 Fazecast, Inc. * Copyright (C) 2012-2022 Fazecast, Inc.
@ -459,12 +459,16 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
verifyAndSetUserPortGroup(portName); verifyAndSetUserPortGroup(portName);
// Try to open the serial port with read/write access // Try to open the serial port with read/write access
pthread_mutex_lock(&criticalSection);
port->errorLineNumber = lastErrorLineNumber = __LINE__ + 1; port->errorLineNumber = lastErrorLineNumber = __LINE__ + 1;
if ((port->handle = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC)) > 0) int portHandle = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC);
if (portHandle > 0)
{ {
// Ensure that multiple root users cannot access the device simultaneously // Set the newly opened port handle in the serial port structure
pthread_mutex_lock(&criticalSection);
port->handle = portHandle;
pthread_mutex_unlock(&criticalSection); pthread_mutex_unlock(&criticalSection);
// Ensure that multiple root users cannot access the device simultaneously
if (!disableExclusiveLock && flock(port->handle, LOCK_EX | LOCK_NB)) if (!disableExclusiveLock && flock(port->handle, LOCK_EX | LOCK_NB))
{ {
port->errorLineNumber = lastErrorLineNumber = __LINE__ - 2; port->errorLineNumber = lastErrorLineNumber = __LINE__ - 2;
@ -494,10 +498,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
} }
} }
else else
{
port->errorNumber = lastErrorNumber = errno; port->errorNumber = lastErrorNumber = errno;
pthread_mutex_unlock(&criticalSection);
}
// Return a pointer to the serial port data structure // Return a pointer to the serial port data structure
(*env)->ReleaseStringUTFChars(env, portNameJString, portName); (*env)->ReleaseStringUTFChars(env, portNameJString, portName);

View File

@ -2,7 +2,7 @@
* SerialPort_Windows.c * SerialPort_Windows.c
* *
* Created on: Feb 25, 2012 * Created on: Feb 25, 2012
* Last Updated on: Feb 15, 2022 * Last Updated on: Feb 16, 2022
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2022 Fazecast, Inc. * Copyright (C) 2012-2022 Fazecast, Inc.
@ -515,12 +515,16 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
reduceLatencyToMinimum(portName + 4, requestElevatedPermissions); reduceLatencyToMinimum(portName + 4, requestElevatedPermissions);
// Try to open the serial port with read/write access // Try to open the serial port with read/write access
EnterCriticalSection(&criticalSection);
port->errorLineNumber = lastErrorLineNumber = __LINE__ + 1; port->errorLineNumber = lastErrorLineNumber = __LINE__ + 1;
if ((port->handle = CreateFileW(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_OVERLAPPED, NULL)) != INVALID_HANDLE_VALUE) void *portHandle = CreateFileW(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_OVERLAPPED, NULL);
if (portHandle != INVALID_HANDLE_VALUE)
{ {
// Configure the port parameters and timeouts // Set the newly opened port handle in the serial port structure
EnterCriticalSection(&criticalSection);
port->handle = portHandle;
LeaveCriticalSection(&criticalSection); LeaveCriticalSection(&criticalSection);
// Configure the port parameters and timeouts
if (!disableAutoConfig && !Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, (jlong)(intptr_t)port)) if (!disableAutoConfig && !Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, (jlong)(intptr_t)port))
{ {
// Close the port if there was a problem setting the parameters // Close the port if there was a problem setting the parameters
@ -536,10 +540,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
Java_com_fazecast_jSerialComm_SerialPort_flushRxTxBuffers(env, obj, (jlong)(intptr_t)port); Java_com_fazecast_jSerialComm_SerialPort_flushRxTxBuffers(env, obj, (jlong)(intptr_t)port);
} }
else else
{
port->errorNumber = lastErrorNumber = GetLastError(); port->errorNumber = lastErrorNumber = GetLastError();
LeaveCriticalSection(&criticalSection);
}
// Return a pointer to the serial port data structure // Return a pointer to the serial port data structure
(*env)->ReleaseStringChars(env, portNameJString, (const jchar*)portName); (*env)->ReleaseStringChars(env, portNameJString, (const jchar*)portName);