From ee5421470a92d66ff79098f68e2ceb99c4cbfd3c Mon Sep 17 00:00:00 2001 From: TazyFoundSoup Date: Thu, 11 Jun 2026 19:07:19 +1000 Subject: fix(thread): Add mutex to ff_ctx and make ff_dprintf thread-safe --- src/dbg.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/dbg.c') diff --git a/src/dbg.c b/src/dbg.c index a12545d..13c3f18 100644 --- a/src/dbg.c +++ b/src/dbg.c @@ -3,6 +3,10 @@ #include #include +#ifdef USE_THREAD +#include +#endif + static size_t ff_internal_itoa(int value, char* buf) { char temp[12]; @@ -52,6 +56,9 @@ static inline void ff_debug_flush(ff_ctx* ctx, char* buffer, size_t* idx) { if (*idx == 0) return; +#ifdef USE_THREAD + pthread_mutex_lock(&ctx->ff_lock); +#endif ctx->ff_debug_stream.write( buffer, *idx, @@ -59,6 +66,9 @@ static inline void ff_debug_flush(ff_ctx* ctx, char* buffer, size_t* idx) ); *idx = 0; +#ifdef USE_THREAD + pthread_mutex_unlock(&ctx->ff_lock); +#endif } ff_result ff_dprintf(ff_ctx* ctx, const char *format, ...) @@ -74,6 +84,10 @@ ff_result ff_dprintf(ff_ctx* ctx, const char *format, ...) size_t buf_idx = 0; const char* p = format; +#ifdef USE_THREAD + pthread_mutex_lock(&ctx->ff_lock); +#endif + while (*p) { if (buf_idx >= sizeof(buffer) - 64) { @@ -152,6 +166,10 @@ ff_result ff_dprintf(ff_ctx* ctx, const char *format, ...) ff_debug_flush(ctx, buffer, &buf_idx); +#ifdef USE_THREAD + pthread_mutex_unlock(&ctx->ff_lock); +#endif + va_end(args); return FF_RESULT_OK; } \ No newline at end of file -- cgit v1.2.3