From 54c4a833f9f46462e117e0fe3b48baf1e395138a Mon Sep 17 00:00:00 2001 From: "S. Sivarajah" Date: Thu, 19 Dec 2019 10:58:53 +0100 Subject: [PATCH 1/2] Add __io_lock() and __io_unlock() functions to lock io functions such as printf in order to avoid collision between multiple threads/cores. --- printf.c | 2 ++ printf.h | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/printf.c b/printf.c index 8a700add..4481911d 100644 --- a/printf.c +++ b/printf.c @@ -861,11 +861,13 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const int printf_(const char* format, ...) { + __io_lock(); va_list va; va_start(va, format); char buffer[1]; const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va); va_end(va); + __io_unlock(); return ret; } diff --git a/printf.h b/printf.h index 6104ccfb..c8f19241 100644 --- a/printf.h +++ b/printf.h @@ -40,6 +40,17 @@ extern "C" { #endif +/** + * Lock the printf to avoid collision when multiple threads/cores try to output strings/characters. + * This function can implement a mutex, semaphore or whatever lock. + */ +void __io_lock(); + +/** + * Unlock the printf, release the lock, semaphore or mutex used to lock printf. + * This function releases the mutex, semaphore or whatever lock used previously. + */ +void __io_unlock(); /** * Output a character to a custom device like UART, used by the printf() function From 3455e53aea1a46a7b56fa5ddf6394a37e68f63a5 Mon Sep 17 00:00:00 2001 From: "S. Sivarajah" Date: Thu, 19 Dec 2019 16:21:13 +0100 Subject: [PATCH 2/2] Add flags to enable lock/unlock in printf --- printf.c | 10 ++++++++++ printf.h | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/printf.c b/printf.c index 4481911d..fa0f06e9 100644 --- a/printf.c +++ b/printf.c @@ -95,6 +95,12 @@ #define PRINTF_SUPPORT_PTRDIFF_T #endif +// add lock and unlock functions to protect io operations +// default: deactivated +#ifdef PRINTF_ENABLE_LOCK +#define PRINTF_IO_LOCK +#endif + /////////////////////////////////////////////////////////////////////////////// // internal flag definitions @@ -861,13 +867,17 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const int printf_(const char* format, ...) { +#if defined(PRINTF_IO_LOCK) __io_lock(); +#endif va_list va; va_start(va, format); char buffer[1]; const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va); va_end(va); +#if defined(PRINTF_IO_LOCK) __io_unlock(); +#endif return ret; } diff --git a/printf.h b/printf.h index c8f19241..6b2cf5c9 100644 --- a/printf.h +++ b/printf.h @@ -43,12 +43,16 @@ extern "C" { /** * Lock the printf to avoid collision when multiple threads/cores try to output strings/characters. * This function can implement a mutex, semaphore or whatever lock. + * + * \note PRINTF_ENABLE_LOCK should be defined in order to activate lock function. */ void __io_lock(); /** * Unlock the printf, release the lock, semaphore or mutex used to lock printf. * This function releases the mutex, semaphore or whatever lock used previously. + * + * \note PRINTF_ENABLE_LOCK should be defined in order to activate unlock function. */ void __io_unlock();