1
0
mirror of https://github.com/mik3y/usb-serial-for-android.git synced 2026-08-14 01:43:08 +00:00

iomanager with configurable threadpriority and higher default to prevent data loss

This commit is contained in:
kai-morich
2020-08-31 22:31:20 +02:00
parent f443d1f012
commit 80e8eb8a60
3 changed files with 24 additions and 9 deletions
@@ -6,6 +6,7 @@
package com.hoho.android.usbserial.util;
import android.os.Process;
import android.util.Log;
import com.hoho.android.usbserial.driver.UsbSerialPort;
@@ -42,6 +43,7 @@ public class SerialInputOutputManager implements Runnable {
STOPPING
}
private int mThreadPriority = Process.THREAD_PRIORITY_URGENT_AUDIO;
private State mState = State.STOPPED; // Synchronized by 'this'
private Listener mListener; // Synchronized by 'this'
private final UsbSerialPort mSerialPort;
@@ -75,6 +77,17 @@ public class SerialInputOutputManager implements Runnable {
return mListener;
}
/**
* setThreadPriority. By default use higher priority than UI thread to prevent data loss
*
* @param threadPriority see {@link Process#setThreadPriority(int)}
* */
public void setThreadPriority(int threadPriority) {
if (mState != State.STOPPED)
throw new IllegalStateException("threadPriority only configurable before SerialInputOutputManager is started");
mThreadPriority = threadPriority;
}
/**
* read/write timeout
*/
@@ -154,6 +167,9 @@ public class SerialInputOutputManager implements Runnable {
*/
@Override
public void run() {
if(mThreadPriority != Process.THREAD_PRIORITY_DEFAULT)
setThreadPriority(mThreadPriority);
synchronized (this) {
if (getState() != State.STOPPED) {
throw new IllegalStateException("Already running");