]> sigrok.org Git - libserialport.git/commitdiff
windows: Break out helper function for awaiting previous write completion.
authorMartin Ling <redacted>
Mon, 3 Jul 2017 19:55:13 +0000 (20:55 +0100)
committerUwe Hermann <redacted>
Wed, 13 Sep 2017 17:27:50 +0000 (19:27 +0200)
serialport.c

index db2aa43ca0ba98442b042b80ce99e85eb034064c..1b71cdbe59eb47ea84520157e0a9ec6998310979 100644 (file)
@@ -731,6 +731,27 @@ SP_API enum sp_return sp_drain(struct sp_port *port)
 #endif
 }
 
+#ifdef _WIN32
+static enum sp_return await_write_completion(struct sp_port *port)
+{
+       TRACE("%p", port);
+       DWORD bytes_written;
+       BOOL result;
+
+       /* Wait for previous non-blocking write to complete, if any. */
+       if (port->writing) {
+               DEBUG("Waiting for previous write to complete");
+               result = GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
+               port->writing = 0;
+               if (!result)
+                       RETURN_FAIL("Previous write failed to complete");
+               DEBUG("Previous write completed");
+       }
+
+       RETURN_OK();
+}
+#endif
+
 SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
                                         size_t count, unsigned int timeout_ms)
 {
@@ -753,17 +774,8 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
 
 #ifdef _WIN32
        DWORD bytes_written = 0;
-       BOOL result;
 
-       /* Wait for previous non-blocking write to complete, if any. */
-       if (port->writing) {
-               DEBUG("Waiting for previous write to complete");
-               result = GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
-               port->writing = 0;
-               if (!result)
-                       RETURN_FAIL("Previous write failed to complete");
-               DEBUG("Previous write completed");
-       }
+       TRY(await_write_completion(port));
 
        /* Set timeout. */
        if (port->timeouts.WriteTotalTimeoutConstant != timeout_ms) {