diff options
author | Campbell Barton <ideasman42@gmail.com> | 2019-02-21 03:31:00 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2019-02-21 03:33:50 +0300 |
commit | 94f83a4ebd929e7c4f405b1c78d9db842dfe1689 (patch) | |
tree | d56bbe15952baa67e3ba96354b32a4b32993852c | |
parent | 9e4d561a8b7db26bf8ba70e0198c3a465609c96d (diff) |
Fix T61765: thread-unsafe logging used
-rw-r--r-- | intern/clog/CMakeLists.txt | 1 | ||||
-rw-r--r-- | intern/clog/clog.c | 19 |
2 files changed, 17 insertions, 3 deletions
diff --git a/intern/clog/CMakeLists.txt b/intern/clog/CMakeLists.txt index 8bf7b66f7ec..e752ffdb623 100644 --- a/intern/clog/CMakeLists.txt +++ b/intern/clog/CMakeLists.txt @@ -18,6 +18,7 @@ set(INC . + ../atomic ../guardedalloc ) diff --git a/intern/clog/clog.c b/intern/clog/clog.c index aa6c94caaff..586cb6963c9 100644 --- a/intern/clog/clog.c +++ b/intern/clog/clog.c @@ -23,6 +23,7 @@ #include <string.h> #include <stdint.h> #include <assert.h> +#include <pthread.h> /* For 'isatty' to check for color. */ #if defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__) @@ -39,6 +40,7 @@ #define __STDC_FORMAT_MACROS #include <inttypes.h> +#include "atomic_ops.h" /* Only other dependency (could use regular malloc too). */ #include "MEM_guardedalloc.h" @@ -69,6 +71,8 @@ typedef struct CLG_IDFilter { typedef struct CLogContext { /** Single linked list of types. */ CLG_LogType *types; + pthread_mutex_t types_lock; + /* exclude, include filters. */ CLG_IDFilter *filters[2]; bool use_color; @@ -574,6 +578,7 @@ static void CLG_ctx_level_set(CLogContext *ctx, int level) static CLogContext *CLG_ctx_init(void) { CLogContext *ctx = MEM_callocN(sizeof(*ctx), __func__); + pthread_mutex_init(&ctx->types_lock, NULL); ctx->use_color = true; ctx->default_type.level = 1; CLG_ctx_output_set(ctx, stdout); @@ -596,6 +601,7 @@ static void CLG_ctx_free(CLogContext *ctx) MEM_freeN(item); } } + pthread_mutex_destroy(&ctx->types_lock); MEM_freeN(ctx); } @@ -672,9 +678,16 @@ void CLG_level_set(int level) void CLG_logref_init(CLG_LogRef *clg_ref) { - assert(clg_ref->type == NULL); - CLG_LogType *clg_ty = clg_ctx_type_find_by_name(g_ctx, clg_ref->identifier); - clg_ref->type = clg_ty ? clg_ty : clg_ctx_type_register(g_ctx, clg_ref->identifier); + /* Only runs once when initializing a static type in most cases. */ + pthread_mutex_lock(&g_ctx->types_lock); + if (clg_ref->type == NULL) { + CLG_LogType *clg_ty = clg_ctx_type_find_by_name(g_ctx, clg_ref->identifier); + if (clg_ty == NULL) { + clg_ty = clg_ctx_type_register(g_ctx, clg_ref->identifier); + } + atomic_cas_ptr((void **)&clg_ref->type, clg_ref->type, clg_ty); + } + pthread_mutex_unlock(&g_ctx->types_lock); } /** \} */ |