Fix T61765: thread-unsafe logging used
This commit is contained in:
parent
9e4d561a8b
commit
94f83a4ebd
@ -18,6 +18,7 @@
|
||||
|
||||
set(INC
|
||||
.
|
||||
../atomic
|
||||
../guardedalloc
|
||||
)
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/** \} */
|
||||
|
Loading…
Reference in New Issue
Block a user