diff options
author | Frediano Ziglio <fziglio@redhat.com> | 2017-01-24 21:50:04 +0000 |
---|---|---|
committer | Frediano Ziglio <freddy77@gmail.com> | 2024-06-12 21:35:38 +0100 |
commit | 12d306085216652985c6d50b489be0a998c5ac81 (patch) | |
tree | f15c5e5f7ccdf0ccc4c911cc965bdb196eede4be | |
parent | 322f4d59169b3f411fffde40cbd69f9fa8bd7211 (diff) |
FIX REMOVEstat
use global reference counting instead of one each?
use destructors to automatically free references?
keep a list of all stat files created to be able to release also
from counters
-rw-r--r-- | server/stat-file.c | 8 | ||||
-rw-r--r-- | server/tests/test-stat-file.c | 1 |
2 files changed, 9 insertions, 0 deletions
diff --git a/server/stat-file.c b/server/stat-file.c index 38c39338..68d095d7 100644 --- a/server/stat-file.c +++ b/server/stat-file.c @@ -39,6 +39,7 @@ struct RedStatFile { SpiceStat *stat; pthread_mutex_t lock; unsigned int max_nodes; + uint16_t *refs; }; RedStatFile *stat_file_new(unsigned int max_nodes) @@ -48,6 +49,7 @@ RedStatFile *stat_file_new(unsigned int max_nodes) RedStatFile *stat_file = g_new0(RedStatFile, 1); stat_file->max_nodes = max_nodes; + stat_file->refs = g_new0(uint16_t, max_nodes); stat_file->shm_name = g_strdup_printf(SPICE_STAT_SHM_NAME, getpid()); shm_unlink(stat_file->shm_name); if ((fd = shm_open(stat_file->shm_name, O_CREAT | O_RDWR, 0444)) == -1) { @@ -154,6 +156,7 @@ stat_file_add_node(RedStatFile *stat_file, StatNodeRef parent, const char *name, while (ref != INVALID_STAT_REF) { node = &stat_file->stat->nodes[ref]; if (strcmp(name, node->name) == 0) { + stat_file->refs[ref]++; pthread_mutex_unlock(&stat_file->lock); return ref; } @@ -166,6 +169,7 @@ stat_file_add_node(RedStatFile *stat_file, StatNodeRef parent, const char *name, } stat_file->stat->generation++; stat_file->stat->num_of_nodes++; + stat_file->refs[ref] = 1; node->value = 0; node->flags = SPICE_STAT_NODE_FLAG_ENABLED | (visible ? SPICE_STAT_NODE_FLAG_VISIBLE : 0); @@ -199,6 +203,10 @@ static void stat_file_remove(RedStatFile *stat_file, SpiceStatNode *node) StatNodeRef ref; pthread_mutex_lock(&stat_file->lock); + if (--stat_file->refs[node_ref]) { + pthread_mutex_unlock(&stat_file->lock); + return; + } node->flags &= ~SPICE_STAT_NODE_FLAG_ENABLED; stat_file->stat->generation++; stat_file->stat->num_of_nodes--; diff --git a/server/tests/test-stat-file.c b/server/tests/test-stat-file.c index 00eb66be..a7df8bb3 100644 --- a/server/tests/test-stat-file.c +++ b/server/tests/test-stat-file.c @@ -70,6 +70,7 @@ static void stat_file(void) /* see above why the formula is used */ int n = (i * 23 + 3) % 10; stat_file_remove_node(stat_file, refs[n]); + stat_file_remove_node(stat_file, refs[n]); refs[n] = INVALID_STAT_REF; } |