From a5ab0faee77bf66973c56fc6d1cd1d4b422bc1e4 Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Sat, 13 Jan 2018 19:17:33 +0100 Subject: [PATCH 1/2] Simplify `aio` stuff a little This hopefully gets rid of the uninitialized writes CI and I sometimes see. Fixes #914. --- src/base/system.c | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/base/system.c b/src/base/system.c index 010ead62d..80a7f9bf4 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -448,6 +448,7 @@ int io_flush(IOHANDLE io) #define ASYNC_BUFSIZE 8 * 1024 +#define ASYNC_LOCAL_BUFSIZE 64 * 1024 struct ASYNCIO { @@ -456,8 +457,6 @@ struct ASYNCIO SEMAPHORE sphore; void *thread; - unsigned char *old_buffer; - unsigned char *buffer; unsigned int buffer_size; unsigned int read_pos; @@ -525,6 +524,8 @@ static void aio_thread(void *user) { struct BUFFERS buffers; int result_io_error; + unsigned char local_buffer[ASYNC_LOCAL_BUFSIZE]; + unsigned int local_buffer_len = 0; if(aio->read_pos == aio->write_pos) { @@ -544,24 +545,33 @@ static void aio_thread(void *user) } buffer_ptrs(aio, &buffers); + if(buffers.buf1) + { + if(buffers.len1 > sizeof(local_buffer) - local_buffer_len) + { + buffers.len1 = sizeof(local_buffer) - local_buffer_len; + } + mem_copy(local_buffer + local_buffer_len, buffers.buf1, buffers.len1); + local_buffer_len += buffers.len1; + if(buffers.buf2) + { + if(buffers.len2 > sizeof(local_buffer) - local_buffer_len) + { + buffers.len2 = sizeof(local_buffer) - local_buffer_len; + } + mem_copy(local_buffer + local_buffer_len, buffers.buf2, buffers.len2); + local_buffer_len += buffers.len2; + } + } + aio->read_pos = (aio->read_pos + buffers.len1 + buffers.len2) % aio->buffer_size; lock_unlock(aio->lock); - io_write(aio->io, buffers.buf1, buffers.len1); - if(buffers.buf2) - { - io_write(aio->io, buffers.buf2, buffers.len2); - } + io_write(aio->io, local_buffer, local_buffer_len); io_flush(aio->io); result_io_error = io_error(aio->io); lock_wait(aio->lock); aio->error = result_io_error; - aio->read_pos = (aio->read_pos + buffers.len1 + buffers.len2) % aio->buffer_size; - if(aio->old_buffer) - { - mem_free(aio->old_buffer); - aio->old_buffer = 0; - } } } @@ -577,7 +587,6 @@ ASYNCIO *aio_new(IOHANDLE io) sphore_init(&aio->sphore); aio->thread = 0; - aio->old_buffer = 0; aio->buffer = mem_alloc(ASYNC_BUFSIZE, 1); if(!aio->buffer) { @@ -679,14 +688,7 @@ void aio_write_unlocked(ASYNCIO *aio, const void *buffer, unsigned size) mem_copy(next_buffer + next_len, buffer, size); next_len += size; - if(!aio->old_buffer) - { - aio->old_buffer = aio->buffer; - } - else - { - mem_free(aio->buffer); - } + mem_free(aio->buffer); aio->buffer = next_buffer; aio->buffer_size = next_size; aio->read_pos = 0; From 8f291ce5284d97353252db6ec852236fed1a355e Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Sat, 13 Jan 2018 20:43:41 +0100 Subject: [PATCH 2/2] Don't use aio logging to stdout on Windows This should fix #964. --- src/base/system.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/base/system.c b/src/base/system.c index 80a7f9bf4..01b67e44e 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -170,6 +170,15 @@ static void logger_file(const char *line, void *user) aio_unlock(logfile); } +#if defined(CONF_FAMILY_WINDOWS) +static void logger_stdout_sync(const char *line, void *user) +{ + (void)user; + puts(line); + fflush(stdout); +} +#endif + static void logger_stdout_finish(void *user) { ASYNCIO *logfile = (ASYNCIO *)user; @@ -212,7 +221,11 @@ void dbg_logger(DBG_LOGGER logger, DBG_LOGGER_FINISH finish, void *user) void dbg_logger_stdout() { +#if defined(CONF_FAMILY_WINDOWS) + dbg_logger(logger_stdout_sync, 0, 0); +#else dbg_logger(logger_file, logger_stdout_finish, aio_new(io_stdout())); +#endif } void dbg_logger_debugger()