altoslib: synchronize access to serial debug output list
authorKeith Packard <keithp@keithp.com>
Sat, 25 Oct 2014 04:21:19 +0000 (21:21 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 25 Oct 2014 04:24:31 +0000 (21:24 -0700)
This list is access by both the receiver and the monitor task, so it
needs to be locked to prevent collisions.

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosLink.java

index 62bd82b93c2ae617c4d67a305696e99a32bd3ba7..c0031cadbf059ce3ea38c6e102901dac1be79e9d 100644 (file)
@@ -55,8 +55,11 @@ public abstract class AltosLink implements Runnable {
 
        public void printf(String format, Object ... arguments) {
                String  line = String.format(format, arguments);
-               if (debug)
-                       pending_output.add(line);
+               if (debug) {
+                       synchronized (pending_output) {
+                               pending_output.add(line);
+                       }
+               }
                try {
                        print(line);
                } catch (InterruptedException ie) {
@@ -286,12 +289,14 @@ public abstract class AltosLink implements Runnable {
                binary_queue.put(dup);
        }
 
-       public void flush_output() {
+       public synchronized void flush_output() {
                if (pending_output == null)
                        return;
-               for (String s : pending_output)
-                       System.out.print(s);
-               pending_output.clear();
+               synchronized (pending_output) {
+                       for (String s : pending_output)
+                               System.out.print(s);
+                       pending_output.clear();
+               }
        }
 
        public void flush_input(int timeout) throws InterruptedException {