[Bf-blender-cvs] [94f83a4ebd9] master: Fix T61765: thread-unsafe logging used

Campbell Barton noreply at git.blender.org
Thu Feb 21 01:34:42 CET 2019


Commit: 94f83a4ebd929e7c4f405b1c78d9db842dfe1689
Author: Campbell Barton
Date:   Thu Feb 21 11:31:00 2019 +1100
Branches: master
https://developer.blender.org/rB94f83a4ebd929e7c4f405b1c78d9db842dfe1689

Fix T61765: thread-unsafe logging used

===================================================================

M	intern/clog/CMakeLists.txt
M	intern/clog/clog.c

===================================================================

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);
 }
 
 /** \} */



More information about the Bf-blender-cvs mailing list