mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-07 16:06:10 +00:00
terminate SerialInputOutputManager write thread if read thread terminates (e.g. port closed)
This commit is contained in:
parent
150174573c
commit
026355f61e
@ -1485,6 +1485,17 @@ public class DeviceTest {
|
|||||||
assertFalse("iomanager threads", usb.hasIoManagerThreads());
|
assertFalse("iomanager threads", usb.hasIoManagerThreads());
|
||||||
assertNull(usb.ioManager);
|
assertNull(usb.ioManager);
|
||||||
assertEquals(SerialInputOutputManager.State.STOPPED, ioManager.getState());
|
assertEquals(SerialInputOutputManager.State.STOPPED, ioManager.getState());
|
||||||
|
|
||||||
|
usb.open();
|
||||||
|
ioManager = usb.ioManager;
|
||||||
|
assertEquals(SerialInputOutputManager.State.RUNNING, usb.ioManager.getState());
|
||||||
|
usb.serialPort.close(); // stop before ioManager
|
||||||
|
for (int i = 0; i < 100 && usb.hasIoManagerThreads(); i++) {
|
||||||
|
Thread.sleep(1);
|
||||||
|
}
|
||||||
|
assertFalse("iomanager threads", usb.hasIoManagerThreads());
|
||||||
|
assertEquals(SerialInputOutputManager.State.STOPPED, usb.ioManager.getState());
|
||||||
|
|
||||||
SerialInputOutputManager.DEBUG = false;
|
SerialInputOutputManager.DEBUG = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,7 +183,7 @@ public class SerialInputOutputManager {
|
|||||||
public void stop() {
|
public void stop() {
|
||||||
if(mState.compareAndSet(State.RUNNING, State.STOPPING)) {
|
if(mState.compareAndSet(State.RUNNING, State.STOPPING)) {
|
||||||
synchronized (mWriteBufferLock) {
|
synchronized (mWriteBufferLock) {
|
||||||
mWriteBufferLock.notifyAll(); // Wake up any waiting thread to check the stop condition
|
mWriteBufferLock.notifyAll(); // wake up write thread to check the stop condition
|
||||||
}
|
}
|
||||||
Log.i(TAG, "Stop requested");
|
Log.i(TAG, "Stop requested");
|
||||||
}
|
}
|
||||||
@ -250,10 +250,12 @@ public class SerialInputOutputManager {
|
|||||||
}
|
}
|
||||||
notifyErrorListener(e);
|
notifyErrorListener(e);
|
||||||
} finally {
|
} finally {
|
||||||
if (!mState.compareAndSet(State.RUNNING, State.STOPPING)) {
|
if (mState.compareAndSet(State.RUNNING, State.STOPPING)) {
|
||||||
if (mState.compareAndSet(State.STOPPING, State.STOPPED)) {
|
synchronized (mWriteBufferLock) {
|
||||||
Log.i(TAG, "runRead: Stopped mState=" + getState());
|
mWriteBufferLock.notifyAll(); // wake up write thread to check the stop condition
|
||||||
}
|
}
|
||||||
|
} else if (mState.compareAndSet(State.STOPPING, State.STOPPED)) {
|
||||||
|
Log.i(TAG, "runRead: Stopped mState=" + getState());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user