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/common.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/common.c') diff --git a/src/common.c b/src/common.c index bde1bac..f9e46d5 100644 --- a/src/common.c +++ b/src/common.c @@ -6,6 +6,9 @@ #include #endif +#ifdef USE_THREAD +#include +#endif // Initializes a new tinyff context. ff_ctx* ff_init(ff_allocator* allocator) @@ -27,12 +30,25 @@ ff_ctx* ff_init(ff_allocator* allocator) ctx->allocator = *allocator; +#ifdef USE_THREAD + /* initialize mutex used to protect ctx state */ + if (pthread_mutex_init(&ctx->ff_lock, NULL) != 0) { + ctx->allocator.ff_free(ctx); + return NULL; + } +#endif + return ctx; } void ff_cleanup(ff_ctx *ctx) { if (!ctx) return; ff_dprintf(ctx, "goodbye from tinyff ;]\n"); + +#ifdef USE_THREAD + pthread_mutex_destroy(&ctx->ff_lock); +#endif + ctx->allocator.ff_free(ctx); } -- cgit v1.2.3