1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-29 14:52:12 +01:00

Make sure the USB scheduling is thread safe.

This commit is contained in:
James Cotton 2012-08-12 14:12:35 -05:00
parent 69fbefee85
commit 75ce520503

View File

@ -312,7 +312,7 @@ public class HidUAVTalk extends TelemetryTask {
public void run() {
// Enqueue the first read
readData();
queueRead();
while (!shutdown) {
// If there are no request
@ -325,11 +325,10 @@ public class HidUAVTalk extends TelemetryTask {
} else if (returned == writeRequest) {
if (DEBUG) Log.d(TAG, "Received write completed request");
writePending = false;
}
sendData();
}
}
}
}, "HID Read Write");
readWriteThread.start();
@ -351,17 +350,31 @@ public class HidUAVTalk extends TelemetryTask {
* it to the input stream
*/
ByteBuffer readBuffer = ByteBuffer.allocate(MAX_HID_PACKET_SIZE);
/**
* Schedules a USB read
*/
private void queueRead() {
synchronized(readRequest) {
if(!readRequest.queue(readBuffer, MAX_HID_PACKET_SIZE)) {
if (ERROR) Log.e(TAG, "Failed to queue request");
} else
readPending = true;
}
}
/**
* Reads data from the last USB transaction and schedules another read
*/
public void readData() {
synchronized(readRequest) {
if (!readPending) {
queueRead();
} else { // We just received a read
if (ERROR) Log.e(TAG, "Tried to read read while a transaction was not pending");
return;
}
// We just received a read
readPending = false;
// Packet format:
// 0: Report ID (1)
@ -385,6 +398,7 @@ public class HidUAVTalk extends TelemetryTask {
// Queue another read
queueRead();
}
}
@ -392,7 +406,7 @@ public class HidUAVTalk extends TelemetryTask {
* Send a packet if data is available
*/
public void sendData() {
synchronized(writeRequest) {
// Don't try and send data till previous request completes
if (writePending)
return;
@ -410,6 +424,7 @@ public class HidUAVTalk extends TelemetryTask {
Log.e(TAG, "Write queuing failed");
}
}
}
/*********** Helper classes for telemetry streams ************/
@ -446,6 +461,9 @@ public class HidUAVTalk extends TelemetryTask {
data.put((byte) oneByte);
data.notify();
}
// If there is not a write request queued, add one
sendData();
}
@Override
@ -457,6 +475,8 @@ public class HidUAVTalk extends TelemetryTask {
data.put(b);
data.notify();
}
sendData();
}
};