From 6eff8199912c4ef7dc2c4a10246058434c26c444 Mon Sep 17 00:00:00 2001 From: Thilo Graf Date: Tue, 9 May 2023 21:04:24 +0200 Subject: [PATCH] system/debug.h: try for more thread save implementation Replacing printf() with fprintf() in this code section aims to prevent unpredictable behavior in multi-threaded environments that could be caused by printf()'s non-atomic nature. The change is intended to enhance output consistency and prevent potential race conditions. Additionally, it is important to note that many other plain calls to printf() in the Neutrino code could also cause issues and require further investigation. Whether this change will have a positive impact remains to be seen. Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/5d44bd1e0f80218a15e48ddaab450c9938125154 Author: Thilo Graf Date: 2023-05-09 (Tue, 09 May 2023) ------------------ This commit was generated by Migit --- src/system/debug.h | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/system/debug.h b/src/system/debug.h index a05ff1895..ba26705b7 100644 --- a/src/system/debug.h +++ b/src/system/debug.h @@ -39,12 +39,29 @@ enum void setDebugLevel( int level ); +#if 0 +#define dprintf(debuglevel, fmt, args...) \ + do + { \ + if (debug >= debuglevel) \ + printf( "[neutrino] " fmt, ## args); \ + } + while(0) +#define dperror(str) {perror("[neutrino] " str);} +#else +// more thread save implementation #define dprintf(debuglevel, fmt, args...) \ do { \ if (debug >= debuglevel) \ - printf( "[neutrino] " fmt, ## args); \ + fprintf(stderr, "[neutrino] " fmt, ## args); \ } while(0) -#define dperror(str) {perror("[neutrino] " str);} +#define dperror(str) \ + do { \ + char errbuf[256]; \ + strerror_r(errno, errbuf, sizeof(errbuf)); \ + fprintf(stderr, "[neutrino] %s: %s\n", str, errbuf); \ + } while(0) +#endif #endif