Added locking functions to libdebug, to avoid printf race conditions

This commit is contained in:
Andreas Eversberg 2021-12-30 01:40:01 +01:00
parent 2ddf33837e
commit f613bfcc37
7 changed files with 48 additions and 10 deletions

View File

@ -106,6 +106,25 @@ int debug_limit_scroll = 0;
static int lock_initialized = 0;
static pthread_mutex_t debug_mutex;
void lock_debug(void)
{
int rc;
if (!lock_initialized) {
rc = pthread_mutex_init(&debug_mutex, NULL);
if (rc == 0)
lock_initialized = 1;
}
if (lock_initialized)
pthread_mutex_lock(&debug_mutex);
}
void unlock_debug(void)
{
if (lock_initialized)
pthread_mutex_unlock(&debug_mutex);
}
void get_win_size(int *w, int *h)
{
struct winsize win;
@ -129,7 +148,6 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function,
const char *p;
va_list args;
int w, h;
int rc;
if (debuglevel > level)
return;
@ -137,13 +155,7 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function,
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);
lock_debug();
buffer[sizeof(buffer) - 1] = '\0';
@ -182,8 +194,7 @@ void _printdebug(const char *file, const char __attribute__((unused)) *function,
print_console_text();
fflush(stdout);
if (lock_initialized)
pthread_mutex_unlock(&debug_mutex);
unlock_debug();
}
const char *debug_amplitude(double level)

View File

@ -53,6 +53,9 @@
#define DSIP 46
#define DTEL 47
void lock_debug(void);
void unlock_debug(void);
void get_win_size(int *w, int *h);
#define PDEBUG(cat, level, fmt, arg...) _printdebug(__FILE__, __FUNCTION__, __LINE__, cat, level, NULL, fmt, ## arg)

View File

@ -60,12 +60,14 @@ void display_iq_on(int on)
if (iq_on) {
memset(&screen, ' ', sizeof(screen));
memset(&screen_history, 0, sizeof(screen_history));
lock_debug();
printf("\0337\033[H");
for (j = 0; j < SIZE; j++) {
screen[j][w] = '\0';
puts(screen[j]);
}
printf("\0338"); fflush(stdout);
unlock_debug();
}
if (on < 0) {
@ -114,6 +116,8 @@ void display_iq(float *samples, int length)
if (!iq_on)
return;
lock_debug();
get_win_size(&width, &h);
if (width > MAX_DISPLAY_WIDTH - 1)
width = MAX_DISPLAY_WIDTH - 1;
@ -271,6 +275,8 @@ cont:
}
disp.interval_pos = pos;
unlock_debug();
}

View File

@ -110,6 +110,8 @@ static void print_measurements(int on)
if (bar_width < 1)
return;
lock_debug();
lines_total = 0;
color = -1;
printf("\0337\033[H");
@ -252,6 +254,8 @@ static void print_measurements(int on)
printf("\033[0;39m\0338"); fflush(stdout);
debug_limit_scroll = lines_total;
unlock_debug();
}
void display_measurements_on(int on)

View File

@ -101,12 +101,14 @@ void display_spectrum_on(int on)
if (spectrum_on) {
memset(&screen, ' ', sizeof(screen));
memset(&buffer_hold, 0, sizeof(buffer_hold));
lock_debug();
printf("\0337\033[H");
for (j = 0; j < HEIGHT; j++) {
screen[j][w] = '\0';
puts(screen[j]);
}
printf("\0338"); fflush(stdout);
unlock_debug();
}
if (on < 0) {
@ -140,6 +142,8 @@ void display_spectrum(float *samples, int length)
if (!spectrum_on)
return;
lock_debug();
get_win_size(&width, &h);
if (width > MAX_DISPLAY_WIDTH - 1)
width = MAX_DISPLAY_WIDTH - 1;
@ -403,6 +407,8 @@ void display_spectrum(float *samples, int length)
}
disp.interval_pos = pos;
unlock_debug();
}

View File

@ -46,6 +46,7 @@ static void print_status(int on)
if (h > lines_total)
h = lines_total;
lock_debug();
printf("\0337\033[H\033[1;37m");
for (i = 0; i < h; i++) {
j = 0;
@ -59,6 +60,7 @@ static void print_status(int on)
putchar('\n');
}
printf("\0338"); fflush(stdout);
unlock_debug();
}
void display_status_on(int on)

View File

@ -52,6 +52,7 @@ void display_wave_on(int on)
if (wave_on) {
memset(&screen, ' ', sizeof(screen));
lock_debug();
printf("\0337\033[H");
for (i = 0; i < num_sender; i++) {
for (j = 0; j < HEIGHT; j++) {
@ -60,6 +61,7 @@ void display_wave_on(int on)
}
}
printf("\0338"); fflush(stdout);
unlock_debug();
}
if (on < 0)
@ -102,6 +104,8 @@ void display_wave(dispwav_t *disp, sample_t *samples, int length, double range)
if (!wave_on)
return;
lock_debug();
get_win_size(&width, &h);
if (width > MAX_DISPLAY_WIDTH - 1)
width = MAX_DISPLAY_WIDTH - 1;
@ -241,6 +245,8 @@ void display_wave(dispwav_t *disp, sample_t *samples, int length, double range)
}
disp->interval_pos = pos;
unlock_debug();
}