Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/main/c/Posix/SerialPort_Posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ static void enumeratePorts(void)
// Event listening threads
void* eventReadingThread1(void *serialPortPointer)
{
// Block signals that could be triggered by serial port operations in this thread only,
// avoiding process-wide signal handler changes that would interfere with the JVM
sigset_t signalMask;
sigemptyset(&signalMask);
sigaddset(&signalMask, SIGHUP);
sigaddset(&signalMask, SIGIO);
sigaddset(&signalMask, SIGTTOU);
sigaddset(&signalMask, SIGTTIN);
pthread_sigmask(SIG_BLOCK, &signalMask, NULL);

// Make this thread immediately and asynchronously cancellable
int oldValue;
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
Expand Down Expand Up @@ -185,6 +195,16 @@ void* eventReadingThread1(void *serialPortPointer)

void* eventReadingThread2(void *serialPortPointer)
{
// Block signals that could be triggered by serial port operations in this thread only,
// avoiding process-wide signal handler changes that would interfere with the JVM
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGIO);
sigaddset(&mask, SIGTTOU);
sigaddset(&mask, SIGTTIN);
pthread_sigmask(SIG_BLOCK, &mask, NULL);

// Make this thread immediately and asynchronously cancellable
int oldValue;
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
Expand Down Expand Up @@ -333,20 +353,6 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
eventFlagsField = (*env)->GetFieldID(env, serialCommClass, "eventFlags", "I");
if (checkJniError(env, __LINE__ - 1)) return JNI_ERR;

// Disable handling of various POSIX signals
sigset_t blockMask;
memset(&blockMask, 0, sizeof(blockMask));
struct sigaction ignoreAction = { 0 };
ignoreAction.sa_handler = SIG_IGN;
ignoreAction.sa_mask = blockMask;
sigaction(SIGIO, &ignoreAction, NULL);
sigaction(SIGHUP, &ignoreAction, NULL);
sigaction(SIGCONT, &ignoreAction, NULL);
sigaction(SIGUSR1, &ignoreAction, NULL);
sigaction(SIGUSR2, &ignoreAction, NULL);
sigaction(SIGTTOU, &ignoreAction, NULL);
sigaction(SIGTTIN, &ignoreAction, NULL);

// Initialize the critical section lock
pthread_mutex_init(&criticalSection, NULL);
classInitialized = 1;
Expand Down