From 682f3c8c659884adbd6d1de8f97f4bf7be78637b Mon Sep 17 00:00:00 2001 From: Felix Palmen Date: Fri, 15 Apr 2016 10:35:33 +0200 Subject: [PATCH] make console output thread-safe --- simavr/sim/sim_avr.c | 28 +++++++++++++++++----------- simavr/sim/sim_avr.h | 7 +++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/simavr/sim/sim_avr.c b/simavr/sim/sim_avr.c index 6744242..88c6019 100644 --- a/simavr/sim/sim_avr.c +++ b/simavr/sim/sim_avr.c @@ -113,6 +113,12 @@ void avr_terminate(avr_t * avr) if (avr->flash) free(avr->flash); if (avr->data) free(avr->data); + if (avr->io_console_buffer.buf) { + avr->io_console_buffer.len = 0; + avr->io_console_buffer.size = 0; + free(avr->io_console_buffer.buf); + avr->io_console_buffer.buf = NULL; + } avr->flash = avr->data = NULL; } @@ -186,21 +192,21 @@ void avr_set_command_register(avr_t * avr, avr_io_addr_t addr) static void _avr_io_console_write(struct avr_t * avr, avr_io_addr_t addr, uint8_t v, void * param) { - static char * buf = NULL; - static int size = 0, len = 0; - - if (v == '\r' && buf) { - buf[len] = 0; - AVR_LOG(avr, LOG_OUTPUT, "O:" "%s" "" "\n", buf); - len = 0; + if (v == '\r' && avr->io_console_buffer.buf) { + avr->io_console_buffer.buf[avr->io_console_buffer.len] = 0; + AVR_LOG(avr, LOG_OUTPUT, "O:" "%s" "" "\n", + avr->io_console_buffer.buf); + avr->io_console_buffer.len = 0; return; } - if (len + 1 >= size) { - size += 128; - buf = (char*)realloc(buf, size); + if (avr->io_console_buffer.len + 1 >= avr->io_console_buffer.size) { + avr->io_console_buffer.size += 128; + avr->io_console_buffer.buf = (char*)realloc( + avr->io_console_buffer.buf, + avr->io_console_buffer.size); } if (v >= ' ') - buf[len++] = v; + avr->io_console_buffer.buf[avr->io_console_buffer.len++] = v; } void avr_set_console_register(avr_t * avr, avr_io_addr_t addr) diff --git a/simavr/sim/sim_avr.h b/simavr/sim/sim_avr.h index 038b893..4c433f6 100644 --- a/simavr/sim/sim_avr.h +++ b/simavr/sim/sim_avr.h @@ -311,6 +311,13 @@ typedef struct avr_t { // crashed even if not activated at startup // if zero, the simulator will just exit() in case of a crash int gdb_port; + + // buffer for console debugging output from register + struct { + char * buf; + uint32_t size; + uint32_t len; + } io_console_buffer; } avr_t; -- 2.39.5