libaltos: Delay freeing serial device until not busy
authorKeith Packard <keithp@keithp.com>
Tue, 25 Feb 2020 19:57:21 +0000 (11:57 -0800)
committerKeith Packard <keithp@keithp.com>
Tue, 25 Feb 2020 23:39:08 +0000 (15:39 -0800)
Just spins for a while waiting for reading thread to wake up and
return.

Signed-off-by: Keith Packard <keithp@keithp.com>
libaltos/libaltos_common.c
libaltos/libaltos_private.h

index 713a775c6f70c9acee6011705b5aa6256cd06bc2..6f0cbe6137d898ce2998de3628eb46213cd78575 100644 (file)
@@ -49,12 +49,17 @@ PUBLIC int
 altos_getchar(struct altos_file *file, int timeout)
 {
        int     ret;
+
+       file->busy = 1;
        while (file->in_read == file->in_used) {
                ret = altos_fill(file, timeout);
                if (ret)
-                       return ret;
+                       goto done;
        }
-       return file->in_data[file->in_read++];
+       ret = file->in_data[file->in_read++];
+done:
+       file->busy = 0;
+       return ret;
 }
 
 PUBLIC int
@@ -112,9 +117,16 @@ int altos_bt_port(struct altos_bt_device *device) {
        return BT_PORT_DEFAULT;
 }
 
+#include <time.h>
+
 PUBLIC void
 altos_free(struct altos_file *file)
 {
+       int i;
        altos_close(file);
+       for (i = 0; i < 10 && file->busy; i++) {
+               struct timespec delay = { .tv_sec = 1, .tv_nsec = 0 };
+               nanosleep(&delay, NULL);
+       }
        free(file);
 }
index ee3dd708873d39a1e05fbb51c488e0748a0a8b94..45e141f9a0126b559c37ceac89817c9a967d9eff 100644 (file)
@@ -35,6 +35,7 @@ struct altos_file {
        unsigned char                   in_data[USB_BUF_SIZE];
        int                             in_used;
        int                             in_read;
+       int                             busy;
 };
 
 #ifdef LINUX