From 88cf92175a4524143349491a817c9037dd8c39ef Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 25 Feb 2020 11:57:21 -0800 Subject: [PATCH] libaltos: Delay freeing serial device until not busy Just spins for a while waiting for reading thread to wake up and return. Signed-off-by: Keith Packard --- libaltos/libaltos_common.c | 16 ++++++++++++++-- libaltos/libaltos_private.h | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/libaltos/libaltos_common.c b/libaltos/libaltos_common.c index 713a775c..6f0cbe61 100644 --- a/libaltos/libaltos_common.c +++ b/libaltos/libaltos_common.c @@ -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 + 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); } diff --git a/libaltos/libaltos_private.h b/libaltos/libaltos_private.h index ee3dd708..45e141f9 100644 --- a/libaltos/libaltos_private.h +++ b/libaltos/libaltos_private.h @@ -35,6 +35,7 @@ struct altos_file { unsigned char in_data[USB_BUF_SIZE]; int in_used; int in_read; + int busy; }; #ifdef LINUX -- 2.30.2