[Spice-devel] [PATCH spice-server 2/3] record: Use reference counting for recording

Frediano Ziglio fziglio at redhat.com
Thu Mar 2 10:38:13 UTC 2017


Allows to share the recording object.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/red-record-qxl.c | 19 ++++++++++++++-----
 server/red-record-qxl.h |  3 ++-
 server/red-worker.c     |  2 +-
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/server/red-record-qxl.c b/server/red-record-qxl.c
index be6ac5a..e687c68 100644
--- a/server/red-record-qxl.c
+++ b/server/red-record-qxl.c
@@ -32,6 +32,7 @@ struct RedRecord {
     FILE *fd;
     pthread_mutex_t lock;
     unsigned int counter;
+    gint refs;
 };
 
 #if 0
@@ -910,17 +911,25 @@ RedRecord *red_record_new(const char *filename)
     }
 
     record = g_new(RedRecord, 1);
+    record->refs = 1;
     record->fd = f;
     record->counter = 0;
     pthread_mutex_init(&record->lock, NULL);
     return record;
 }
 
-void red_record_free(RedRecord *record)
+RedRecord *red_record_ref(RedRecord *record)
 {
-    if (record) {
-        fclose(record->fd);
-        pthread_mutex_destroy(&record->lock);
-        g_free(record);
+    g_atomic_int_inc(&record->refs);
+    return record;
+}
+
+void red_record_unref(RedRecord *record)
+{
+    if (!record || !g_atomic_int_dec_and_test(&record->refs)) {
+        return;
     }
+    fclose(record->fd);
+    pthread_mutex_destroy(&record->lock);
+    g_free(record);
 }
diff --git a/server/red-record-qxl.h b/server/red-record-qxl.h
index 0685393..293e24a 100644
--- a/server/red-record-qxl.h
+++ b/server/red-record-qxl.h
@@ -33,7 +33,8 @@ typedef struct RedRecord RedRecord;
  */
 RedRecord* red_record_new(const char *filename);
 
-void red_record_free(RedRecord *record);
+RedRecord *red_record_ref(RedRecord *record);
+void red_record_unref(RedRecord *record);
 
 void red_record_primary_surface_create(RedRecord *record,
                                        QXLDevSurfaceCreate *surface,
diff --git a/server/red-worker.c b/server/red-worker.c
index 8735cd1..93cb615 100644
--- a/server/red-worker.c
+++ b/server/red-worker.c
@@ -1461,7 +1461,7 @@ void red_worker_free(RedWorker *worker)
     g_main_context_unref(worker->core.main_context);
 
     if (worker->record) {
-        red_record_free(worker->record);
+        red_record_unref(worker->record);
     }
     memslot_info_destroy(&worker->mem_slots);
     free(worker);
-- 
2.9.3



More information about the Spice-devel mailing list