libaltos: use PurgeComm in Windows altos_close to abort in-progress ops
authorKeith Packard <keithp@keithp.com>
Fri, 17 May 2013 10:34:50 +0000 (03:34 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 17 May 2013 10:49:42 +0000 (03:49 -0700)
Instead of manually signalling the related events, use PurgeComm which
can then abort the operations itself. Also make sure all of the
relevant handles are set to INVALID before closing them to avoid race conditions.

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

index fc949c7020a2e8143f5c5f83238ebda3cb4b1003..4a6363edc7a069c4e0f83d930d24d9d7bdea4b2c 100644 (file)
@@ -994,6 +994,11 @@ log_message(char *fmt, ...)
        if (!log)
                log = fopen("\\temp\\altos.txt", "w");
        if (log) {
+               SYSTEMTIME time;
+               GetLocalTime(&time);
+               fprintf (log, "%4d-%02d-%02d %2d:%02d:%02d. ", 
+                        time.wYear, time.wMonth, time.wDay,
+                        time.wHour, time.wMinute, time.wSecond);
                va_start(a, fmt);
                vfprintf(log, fmt, a);
                va_end(a);
@@ -1339,6 +1344,7 @@ altos_open(struct altos_device *device)
                file->handle = open_serial(full_name);
                if (file->handle != INVALID_HANDLE_VALUE)
                        break;
+               altos_set_last_windows_error();
                Sleep(100);
        }
        
@@ -1373,13 +1379,19 @@ altos_open(struct altos_device *device)
 PUBLIC void
 altos_close(struct altos_file *file)
 {
-       if (file->handle != INVALID_HANDLE_VALUE) {
-               CloseHandle(file->handle);
+       HANDLE  handle = file->handle;
+       if (handle != INVALID_HANDLE_VALUE) {
+               HANDLE  ov_read = file->ov_read.hEvent;
+               HANDLE  ov_write = file->ov_write.hEvent;
+               file->handle = INVALID_HANDLE_VALUE;
+               file->ov_read.hEvent = INVALID_HANDLE_VALUE;
+               file->ov_write.hEvent = INVALID_HANDLE_VALUE;
+               PurgeComm(handle, PURGE_RXABORT|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_TXCLEAR);
+               Sleep(100);
+               CloseHandle(handle);
                file->handle = INVALID_HANDLE_VALUE;
-               SetEvent(file->ov_read.hEvent);
-               SetEvent(file->ov_write.hEvent);
-               CloseHandle(file->ov_read.hEvent);
-               CloseHandle(file->ov_write.hEvent);
+               CloseHandle(ov_read);
+               CloseHandle(ov_write);
        }
 }