libsysprof: add rwlock for symbol cache

This is necessary so we can start looking at threading symbolizers.
This commit is contained in:
Christian Hergert
2023-08-15 19:21:31 -07:00
parent 10807758aa
commit d1b4308af7

View File

@ -42,6 +42,7 @@ struct _SysprofSymbolCacheNode
struct _SysprofSymbolCache struct _SysprofSymbolCache
{ {
GObject parent_instance; GObject parent_instance;
GRWLock rwlock;
RB_HEAD(sysprof_symbol_cache, _SysprofSymbolCacheNode) head; RB_HEAD(sysprof_symbol_cache, _SysprofSymbolCacheNode) head;
}; };
@ -104,6 +105,8 @@ sysprof_symbol_cache_finalize (GObject *object)
if (node != NULL) if (node != NULL)
sysprof_symbol_cache_node_free (node); sysprof_symbol_cache_node_free (node);
g_rw_lock_clear (&self->rwlock);
G_OBJECT_CLASS (sysprof_symbol_cache_parent_class)->finalize (object); G_OBJECT_CLASS (sysprof_symbol_cache_parent_class)->finalize (object);
} }
@ -118,6 +121,7 @@ sysprof_symbol_cache_class_init (SysprofSymbolCacheClass *klass)
static void static void
sysprof_symbol_cache_init (SysprofSymbolCache *self) sysprof_symbol_cache_init (SysprofSymbolCache *self)
{ {
g_rw_lock_init (&self->rwlock);
RB_INIT (&self->head); RB_INIT (&self->head);
} }
@ -154,11 +158,14 @@ sysprof_symbol_cache_take (SysprofSymbolCache *self,
node->high = symbol->end_address-1; node->high = symbol->end_address-1;
node->max = node->high; node->max = node->high;
g_rw_lock_writer_lock (&self->rwlock);
/* If there is a collision, then the node is returned. Otherwise /* If there is a collision, then the node is returned. Otherwise
* if the node was inserted, NULL is returned. * if the node was inserted, NULL is returned.
*/ */
if ((ret = RB_INSERT(sysprof_symbol_cache, &self->head, node))) if ((ret = RB_INSERT(sysprof_symbol_cache, &self->head, node)))
{ {
g_rw_lock_writer_unlock (&self->rwlock);
sysprof_symbol_cache_node_free (node); sysprof_symbol_cache_node_free (node);
return; return;
} }
@ -172,6 +179,8 @@ sysprof_symbol_cache_take (SysprofSymbolCache *self,
node = parent; node = parent;
parent = RB_PARENT(parent, link); parent = RB_PARENT(parent, link);
} }
g_rw_lock_writer_unlock (&self->rwlock);
} }
SysprofSymbol * SysprofSymbol *
@ -185,6 +194,8 @@ sysprof_symbol_cache_lookup (SysprofSymbolCache *self,
if (address == 0) if (address == 0)
return NULL; return NULL;
g_rw_lock_reader_lock (&self->rwlock);
node = RB_ROOT(&self->head); node = RB_ROOT(&self->head);
/* The root node contains our calculated max as augmented in RBTree. /* The root node contains our calculated max as augmented in RBTree.
@ -192,7 +203,10 @@ sysprof_symbol_cache_lookup (SysprofSymbolCache *self,
* in O(1) without having to add a branch to the while loop below. * in O(1) without having to add a branch to the while loop below.
*/ */
if (node == NULL || node->max < address) if (node == NULL || node->max < address)
return NULL; {
g_rw_lock_reader_unlock (&self->rwlock);
return NULL;
}
while (node != NULL) while (node != NULL)
{ {
@ -202,7 +216,10 @@ sysprof_symbol_cache_lookup (SysprofSymbolCache *self,
node->max >= RB_RIGHT(node, link)->max); node->max >= RB_RIGHT(node, link)->max);
if (address >= node->low && address <= node->high) if (address >= node->low && address <= node->high)
return node->symbol; {
g_rw_lock_reader_unlock (&self->rwlock);
return node->symbol;
}
if (RB_LEFT(node, link) && RB_LEFT(node, link)->max >= address) if (RB_LEFT(node, link) && RB_LEFT(node, link)->max >= address)
node = RB_LEFT(node, link); node = RB_LEFT(node, link);
@ -210,6 +227,8 @@ sysprof_symbol_cache_lookup (SysprofSymbolCache *self,
node = RB_RIGHT(node, link); node = RB_RIGHT(node, link);
} }
g_rw_lock_reader_unlock (&self->rwlock);
return NULL; return NULL;
} }