Remove busywait
pull/615/head
Dmitry Kaukov 2025-01-27 13:10:45 +11:00
rodzic 7d4892b737
commit 46ab1e9b00
Nie znaleziono w bazie danych klucza dla tego podpisu
1 zmienionych plików z 18 dodań i 3 usunięć

Wyświetl plik

@ -152,7 +152,16 @@ public class SerialInputOutputManager {
*/ */
public void writeAsync(byte[] data) { public void writeAsync(byte[] data) {
synchronized (mWriteBufferLock) { synchronized (mWriteBufferLock) {
while (mWriteBuffer.remaining() < data.length) {
try {
wait(); // Block until space is available in the buffer
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Restore the interrupt flag
return; // Exit gracefully
}
}
mWriteBuffer.put(data); mWriteBuffer.put(data);
notifyAll(); // Notify waiting threads
} }
} }
@ -184,6 +193,9 @@ 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) {
notifyAll(); // Wake up any waiting thread to check the stop condition
}
Log.i(TAG, "Stop requested"); Log.i(TAG, "Stop requested");
} }
} }
@ -231,7 +243,7 @@ public class SerialInputOutputManager {
* Continuously services the read buffers until {@link #stop()} is called, or until a driver exception is * Continuously services the read buffers until {@link #stop()} is called, or until a driver exception is
* raised. * raised.
*/ */
private void runRead() { void runRead() {
Log.i(TAG, "runRead running ..."); Log.i(TAG, "runRead running ...");
try { try {
setThreadPriority(); setThreadPriority();
@ -263,7 +275,7 @@ public class SerialInputOutputManager {
* Continuously services the write buffers until {@link #stop()} is called, or until a driver exception is * Continuously services the write buffers until {@link #stop()} is called, or until a driver exception is
* raised. * raised.
*/ */
private void runWrite() { void runWrite() {
Log.i(TAG, "runWrite running ..."); Log.i(TAG, "runWrite running ...");
try { try {
setThreadPriority(); setThreadPriority();
@ -311,7 +323,7 @@ public class SerialInputOutputManager {
} }
} }
private void stepWrite() throws IOException { private void stepWrite() throws IOException, InterruptedException {
// Handle outgoing data. // Handle outgoing data.
byte[] buffer = null; byte[] buffer = null;
synchronized (mWriteBufferLock) { synchronized (mWriteBufferLock) {
@ -321,12 +333,15 @@ public class SerialInputOutputManager {
mWriteBuffer.rewind(); mWriteBuffer.rewind();
mWriteBuffer.get(buffer, 0, len); mWriteBuffer.get(buffer, 0, len);
mWriteBuffer.clear(); mWriteBuffer.clear();
} else {
wait();
} }
} }
if (buffer != null) { if (buffer != null) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "Writing data len=" + buffer.length); Log.d(TAG, "Writing data len=" + buffer.length);
} }
notifyAll(); // Notify writeAsync that there is space in the buffer
mSerialPort.write(buffer, mWriteTimeout); mSerialPort.write(buffer, mWriteTimeout);
} }
} }