From 411208e5ef75db4905463d2de8e3c9ec64a5eb2d Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" <114750+alfredh@users.noreply.github.com> Date: Tue, 30 Apr 2024 14:13:41 +0200 Subject: [PATCH] dbg: mutex should be unlocked while calling print handler (#1113) * dbg: mutex should be unlocked while calling print handler * dbg_handler_set: add mutex --- src/dbg/dbg.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/dbg/dbg.c b/src/dbg/dbg.c index df3e74847..0e84cf3d2 100644 --- a/src/dbg/dbg.c +++ b/src/dbg/dbg.c @@ -91,8 +91,10 @@ void dbg_close(void) */ void dbg_handler_set(dbg_print_h *ph, void *arg) { + dbg_lock(); dbg.ph = ph; dbg.arg = arg; + dbg_unlock(); } @@ -151,27 +153,24 @@ static void dbg_vprintf(int level, const char *fmt, va_list ap) static void dbg_fmt_vprintf(int level, const char *fmt, va_list ap) { char buf[256]; - int len; dbg_lock(); + int dbg_level = dbg.level; + dbg_print_h *ph = dbg.ph; + void *arg = dbg.arg; + dbg_unlock(); - if (level > dbg.level) - goto out; - - if (!dbg.ph) - goto out; - - len = re_vsnprintf(buf, sizeof(buf), fmt, ap); - if (len <= 0) - goto out; + if (level > dbg_level) + return; /* Print handler? */ - if (dbg.ph) { - dbg.ph(level, buf, len, dbg.arg); - } + if (ph) { + int len = re_vsnprintf(buf, sizeof(buf), fmt, ap); + if (len <= 0) + return; - out: - dbg_unlock(); + ph(level, buf, len, arg); + } }