summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrediano Ziglio <fziglio@redhat.com>2017-01-24 21:50:04 +0000
committerFrediano Ziglio <freddy77@gmail.com>2023-11-22 07:39:15 +0000
commit80033bd671c99d396362ea57d1326938f2ec4cf4 (patch)
tree708d5e48b98ed54ac7e907f1a615e0cc6bc5dc21
parentc0612f8b8c31aa6c5e718bb446531ed8ab6e6735 (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.c8
-rw-r--r--server/tests/test-stat-file.c1
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;
}