From 35c7f2e80937268b4f23fb6755d7da5f13307769 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Wed, 24 Aug 2022 03:08:48 +0800 Subject: [PATCH] util: Remove the need of _MTX_INITIALIZER_NP by using simple_mtx_t/SIMPLE_MTX_INITIALIZER in u_debug_stack.c This removed the thread-unsafe function initialize_backtrace_mutex. And remove the _MTX_INITIALIZER_NP that's doesn't belong to c11 Signed-off-by: Yonggang Luo Reviewed-by: Jesse Natalie Part-of: --- src/util/u_debug_stack.c | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/src/util/u_debug_stack.c b/src/util/u_debug_stack.c index 12fc7dc809a..f213bfbf3e7 100644 --- a/src/util/u_debug_stack.c +++ b/src/util/u_debug_stack.c @@ -32,11 +32,11 @@ * @author Jose Fonseca */ +#include "util/simple_mtx.h" #include "util/u_debug.h" #include "u_debug_symbol.h" #include "u_debug_stack.h" #include "pipe/p_config.h" -#include "c11/threads.h" #if defined(HAVE_LIBUNWIND) @@ -49,7 +49,7 @@ #include "util/hash_table.h" static struct hash_table* symbols_hash; -static mtx_t symbols_mutex = _MTX_INITIALIZER_NP; +static simple_mtx_t symbols_mutex = SIMPLE_MTX_INITIALIZER; /* TODO with some refactoring we might be able to re-use debug_symbol_name_cached() * instead.. otoh if using libunwind I think u_debug_symbol could just be excluded @@ -61,7 +61,7 @@ symbol_name_cached(unw_cursor_t *cursor, unw_proc_info_t *pip) void *addr = (void *)(uintptr_t)pip->start_ip; char *name; - mtx_lock(&symbols_mutex); + simple_mtx_lock(&symbols_mutex); if(!symbols_hash) symbols_hash = _mesa_pointer_hash_table_create(NULL); struct hash_entry *entry = _mesa_hash_table_search(symbols_hash, addr); @@ -80,7 +80,7 @@ symbol_name_cached(unw_cursor_t *cursor, unw_proc_info_t *pip) name = "??"; entry = _mesa_hash_table_insert(symbols_hash, addr, (void*)name); } - mtx_unlock(&symbols_mutex); + simple_mtx_unlock(&symbols_mutex); return entry->data; } @@ -295,33 +295,20 @@ debug_backtrace_capture(struct debug_stack_frame *backtrace, } } - -static mtx_t backtrace_mutex; - -static void -initialize_backtrace_mutex() -{ - static bool initialized = false; - - if (!initialized) { - (void)mtx_init(&backtrace_mutex, mtx_plain); - initialized = true; - } -} +static simple_mtx_t backtrace_mutex = SIMPLE_MTX_INITIALIZER; void debug_backtrace_dump(const struct debug_stack_frame *backtrace, unsigned nr_frames) { unsigned i; - initialize_backtrace_mutex(); - mtx_lock(&backtrace_mutex); + simple_mtx_lock(&backtrace_mutex); for (i = 0; i < nr_frames; ++i) { if (!backtrace[i].function) break; debug_symbol_print(backtrace[i].function); } - mtx_unlock(&backtrace_mutex); + simple_mtx_unlock(&backtrace_mutex); } @@ -332,8 +319,7 @@ debug_backtrace_print(FILE *f, { unsigned i; - initialize_backtrace_mutex(); - mtx_lock(&backtrace_mutex); + simple_mtx_lock(&backtrace_mutex); for (i = 0; i < nr_frames; ++i) { const char *symbol; if (!backtrace[i].function) @@ -343,7 +329,7 @@ debug_backtrace_print(FILE *f, fprintf(f, "%s\n", symbol); } fflush(f); - mtx_unlock(&backtrace_mutex); + simple_mtx_unlock(&backtrace_mutex); } #endif /* HAVE_LIBUNWIND */