From fe4995cf1fb5772e46af28abafa18c8c4fe8a892 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Sun, 6 Aug 2023 02:18:43 -0700 Subject: [PATCH] libsysprof: synchronize access to tid symbols hashtable This can get mutated after the document is loaded, so we need to synchronize access to it. --- src/libsysprof/sysprof-document.c | 40 ++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/libsysprof/sysprof-document.c b/src/libsysprof/sysprof-document.c index 23937052..478606a5 100644 --- a/src/libsysprof/sysprof-document.c +++ b/src/libsysprof/sysprof-document.c @@ -2102,29 +2102,41 @@ _sysprof_document_thread_symbol (SysprofDocument *self, int pid, int tid) { + static GRWLock rwlock; SysprofSymbol *ret; g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL); - if (!(ret = g_hash_table_lookup (self->tid_to_symbol, GINT_TO_POINTER (tid)))) + g_rw_lock_reader_lock (&rwlock); + ret = g_hash_table_lookup (self->tid_to_symbol, GINT_TO_POINTER (tid)); + g_rw_lock_reader_unlock (&rwlock); + + if (ret == NULL) { - char pidstr[32]; - char tidstr[32]; + g_rw_lock_writer_lock (&rwlock); - g_snprintf (pidstr, sizeof pidstr, "(%d)", pid); + if (!(ret = g_hash_table_lookup (self->tid_to_symbol, GINT_TO_POINTER (tid)))) + { + char pidstr[32]; + char tidstr[32]; - if (tid == pid) - g_snprintf (tidstr, sizeof tidstr, "Thread-%d (Main)", tid); - else - g_snprintf (tidstr, sizeof tidstr, "Thread-%d", tid); + g_snprintf (pidstr, sizeof pidstr, "(%d)", pid); - ret = _sysprof_symbol_new (g_ref_string_new (tidstr), - NULL, - g_ref_string_new (pidstr), - 0, 0, - SYSPROF_SYMBOL_KIND_THREAD); + if (tid == pid) + g_snprintf (tidstr, sizeof tidstr, "Thread-%d (Main)", tid); + else + g_snprintf (tidstr, sizeof tidstr, "Thread-%d", tid); - g_hash_table_insert (self->tid_to_symbol, GINT_TO_POINTER (tid), ret); + ret = _sysprof_symbol_new (g_ref_string_new (tidstr), + NULL, + g_ref_string_new (pidstr), + 0, 0, + SYSPROF_SYMBOL_KIND_THREAD); + + g_hash_table_insert (self->tid_to_symbol, GINT_TO_POINTER (tid), ret); + } + + g_rw_lock_writer_unlock (&rwlock); } return ret;