Separating out the close and free actions ensures that the reader thread will not
access freed memory or dereference a null pointer while shutting down the
connection to the serial device. Otherwise, a race condition exists between the
serial close and the thread join.
Signed-off-by: Keith Packard <keithp@keithp.com>
- if (altos == null)
- break;
c = libaltos.altos_getchar(altos, 0);
if (Thread.interrupted())
break;
c = libaltos.altos_getchar(altos, 0);
if (Thread.interrupted())
break;
libaltos.altos_close(altos);
libaltos.altos_close(altos);
if (input_thread != null) {
try {
input_thread.interrupt();
if (input_thread != null) {
try {
input_thread.interrupt();
+ if (altos != null) {
+ libaltos.altos_free(altos);
+ altos = null;
+ }
}
public void putc(char c) {
}
public void putc(char c) {
altos_close(struct altos_file *file)
{
close(file->fd);
altos_close(struct altos_file *file)
{
close(file->fd);
+ file->fd = -1;
+}
+
+void
+altos_free(struct altos_file *file)
+{
+ if (file->fd != -1)
+ close(file->fd);
while (file->out_used) {
int ret;
while (file->out_used) {
int ret;
+ if (file->fd < 0)
+ return -EBADF;
ret = write (file->fd, file->out_data, file->out_used);
if (ret < 0)
return -errno;
ret = write (file->fd, file->out_data, file->out_used);
if (ret < 0)
return -errno;
int ret;
altos_flush(file);
int ret;
altos_flush(file);
+ if (file->fd < 0)
+ return -EBADF;
ret = read(file->fd, file->in_data, USB_BUF_SIZE);
if (ret < 0)
return -errno;
ret = read(file->fd, file->in_data, USB_BUF_SIZE);
if (ret < 0)
return -errno;
void altos_close(struct altos_file *file);
void altos_close(struct altos_file *file);
+void altos_free(struct altos_file *file);
+
int
altos_putchar(struct altos_file *file, char c);
int
altos_putchar(struct altos_file *file, char c);