From: Martin Ling Date: Mon, 3 Jul 2017 19:55:13 +0000 (+0100) Subject: windows: Break out helper function for awaiting previous write completion. X-Git-Tag: libserialport-0.1.2~95 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=62ed9f801abe409d3f7c1cfa91287896187f2ac0;p=libserialport.git windows: Break out helper function for awaiting previous write completion. --- diff --git a/serialport.c b/serialport.c index db2aa43..1b71cdb 100644 --- a/serialport.c +++ b/serialport.c @@ -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) {