From: Keith Packard Date: Tue, 25 Feb 2020 19:57:21 +0000 (-0800) Subject: libaltos: Delay freeing serial device until not busy X-Git-Tag: 1.9.2~2^2~11 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=88cf92175a4524143349491a817c9037dd8c39ef;hp=017e7ed4d2eea2bbc01972a8dfe11babd42d5993 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 --- 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