Added locking to debug output, to prevent race condition between threads

This commit is contained in:
Andreas Eversberg 2021-09-15 12:28:29 +02:00
parent 1b16029ac6
commit 35ed2d5138
2 changed files with 21 additions and 5 deletions

View File

@ -24,8 +24,8 @@ dnl Checks for typedefs, structures and compiler characteristics
AC_CANONICAL_HOST AC_CANONICAL_HOST
AC_CHECK_LIB([m], [main]) AC_CHECK_LIB([m], [main], [], [echo "Failed to find lib!" ; exit -1])
AC_CHECK_LIB([pthread], [main]) AC_CHECK_LIB([pthread], [main], [], [echo "Failed to find lib!" ; exit -1])
with_sdr=no with_sdr=no
soapy_0_7_1_or_higher= soapy_0_7_1_or_higher=

View File

@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <math.h> #include <math.h>
#include <time.h> #include <time.h>
#include <pthread.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/time.h> #include <sys/time.h>
#include "debug.h" #include "debug.h"
@ -100,6 +101,9 @@ void (*print_console_text)(void) = NULL;
int debug_limit_scroll = 0; int debug_limit_scroll = 0;
static int lock_initialized = 0;
static pthread_mutex_t debug_mutex;
void get_win_size(int *w, int *h) void get_win_size(int *w, int *h)
{ {
struct winsize win; struct winsize win;
@ -123,10 +127,22 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function,
const char *p; const char *p;
va_list args; va_list args;
int w, h; int w, h;
int rc;
if (debuglevel > level) if (debuglevel > level)
return; return;
if (!(debug_mask & ((uint64_t)1 << cat)))
return;
if (!lock_initialized) {
rc = pthread_mutex_init(&debug_mutex, NULL);
if (rc == 0)
lock_initialized = 1;
}
if (lock_initialized)
pthread_mutex_lock(&debug_mutex);
buffer[sizeof(buffer) - 1] = '\0'; buffer[sizeof(buffer) - 1] = '\0';
/* if kanal is used, prefix the channel number */ /* if kanal is used, prefix the channel number */
@ -136,9 +152,6 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function,
s -= strlen(buffer); s -= strlen(buffer);
} }
if (!(debug_mask & ((uint64_t)1 << cat)))
return;
va_start(args, fmt); va_start(args, fmt);
vsnprintf(b, s, fmt, args); vsnprintf(b, s, fmt, args);
va_end(args); va_end(args);
@ -166,6 +179,9 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function,
if (print_console_text) if (print_console_text)
print_console_text(); print_console_text();
fflush(stdout); fflush(stdout);
if (lock_initialized)
pthread_mutex_unlock(&debug_mutex);
} }
const char *debug_amplitude(double level) const char *debug_amplitude(double level)