From ede53b6b891578a63deaa64716dcd1f5525970df Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Tue, 3 Mar 2026 20:08:18 +0100 Subject: [PATCH] Fix pcl-0c1: make pdbg_runtime_level atomic (#343) Co-authored-by: Levi Neely --- pclsync/pdbg.c | 20 +++++++++++--------- pclsync/pdbg.h | 10 +++++----- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/pclsync/pdbg.c b/pclsync/pdbg.c index 0a4cc9d..3d493de 100644 --- a/pclsync/pdbg.c +++ b/pclsync/pdbg.c @@ -35,29 +35,31 @@ static void pdbg_init_level(void) { const char *level_str = getenv("PCLOUD_LOG_LEVEL"); if (!level_str || level_str[0] == '\0') { - pdbg_runtime_level = D_INFO; /* default to INFO */ + __atomic_store_n(&pdbg_runtime_level, D_INFO, __ATOMIC_RELAXED); return; } + unsigned int level; /* Parse log level string (case-insensitive) */ if (strcasecmp(level_str, "NONE") == 0) { - pdbg_runtime_level = D_NONE; + level = D_NONE; } else if (strcasecmp(level_str, "BUG") == 0 || strcasecmp(level_str, "DEBUG") == 0) { - pdbg_runtime_level = D_BUG; + level = D_BUG; } else if (strcasecmp(level_str, "CRITICAL") == 0) { - pdbg_runtime_level = D_CRITICAL; + level = D_CRITICAL; } else if (strcasecmp(level_str, "ERROR") == 0) { - pdbg_runtime_level = D_ERROR; + level = D_ERROR; } else if (strcasecmp(level_str, "WARNING") == 0 || strcasecmp(level_str, "WARN") == 0) { - pdbg_runtime_level = D_WARNING; + level = D_WARNING; } else if (strcasecmp(level_str, "INFO") == 0) { - pdbg_runtime_level = D_INFO; + level = D_INFO; } else if (strcasecmp(level_str, "NOTICE") == 0) { - pdbg_runtime_level = D_NOTICE; + level = D_NOTICE; } else { /* Invalid level, default to INFO */ - pdbg_runtime_level = D_INFO; + level = D_INFO; } + __atomic_store_n(&pdbg_runtime_level, level, __ATOMIC_RELAXED); } char *psync_debug_path() { diff --git a/pclsync/pdbg.h b/pclsync/pdbg.h index fb4efed..def19ae 100644 --- a/pclsync/pdbg.h +++ b/pclsync/pdbg.h @@ -1,14 +1,14 @@ #ifndef __PDBG_H #define __PDBG_H -#ifdef __cplusplus -extern "C" { -#endif - #include "pcompiler.h" #include "putil.h" // need for macros... #include "psynclib.h" // need for macros... +#ifdef __cplusplus +extern "C" { +#endif + #define D_NONE 0 #define D_CRITICAL 10 @@ -36,7 +36,7 @@ extern unsigned int pdbg_runtime_level; #define pdbg_logf(level, ...) \ do { \ - if (level <= pdbg_runtime_level) \ + if (level <= __atomic_load_n(&pdbg_runtime_level, __ATOMIC_RELAXED)) \ pdbg_printf(__FILE__, __FUNCTION__, __LINE__, level, __VA_ARGS__); \ } while (0) #define pdbg_assert(cond) \