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 <luoyonggang@gmail.com>
Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19154>
This commit is contained in:
Yonggang Luo 2022-08-24 03:08:48 +08:00 committed by Marge Bot
parent 574b4c547f
commit 35c7f2e809

View file

@ -32,11 +32,11 @@
* @author Jose Fonseca <jfonseca@vmware.com>
*/
#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 */