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>
try {
for (;;) {
- if (altos == null)
- break;
c = libaltos.altos_getchar(altos, 0);
if (Thread.interrupted())
break;
}
public void close() {
- if (altos != null) {
+ if (altos != null)
libaltos.altos_close(altos);
- altos = null;
- }
if (input_thread != null) {
try {
input_thread.interrupt();
}
input_thread = null;
}
+ if (altos != null) {
+ libaltos.altos_free(altos);
+ altos = null;
+ }
}
public void putc(char c) {
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);
free(file);
}
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;
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;
void altos_close(struct altos_file *file);
+void altos_free(struct altos_file *file);
+
int
altos_putchar(struct altos_file *file, char c);