in src/main/c/Posix/SerialPort_Posix.c [1087:1124]
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setEventListeningStatus(JNIEnv *env, jobject obj, jlong serialPortPointer, jboolean eventListenerRunning)
{
// Create or cancel a separate event listening thread if required
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
port->eventListenerRunning = eventListenerRunning;
#if defined(__linux__) && !defined(__ANDROID__)
if (eventListenerRunning && ((port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_CARRIER_DETECT) || (port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_CTS) ||
(port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DSR) || (port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_RING_INDICATOR)))
{
port->event = 0;
if (!port->eventsThread1)
{
if (!pthread_create(&port->eventsThread1, NULL, eventReadingThread1, port))
pthread_detach(port->eventsThread1);
else
port->eventsThread1 = 0;
}
if (!port->eventsThread2)
{
if (!pthread_create(&port->eventsThread2, NULL, eventReadingThread2, port))
pthread_detach(port->eventsThread2);
else
port->eventsThread2 = 0;
}
port->eventListenerUsesThreads = 1;
}
else if (port->eventListenerUsesThreads)
{
port->eventListenerUsesThreads = 0;
if (port->eventsThread1)
pthread_cancel(port->eventsThread1);
port->eventsThread1 = 0;
if (port->eventsThread2)
pthread_cancel(port->eventsThread2);
port->eventsThread2 = 0;
}
#endif // #if defined(__linux__)
}