render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
7a3408
From c940f107b75694c3272ca98cb69b9d7df8cd2d0b Mon Sep 17 00:00:00 2001
7a3408
Message-Id: <c940f107b75694c3272ca98cb69b9d7df8cd2d0b@dist-git>
7a3408
From: Jiri Denemark <jdenemar@redhat.com>
7a3408
Date: Thu, 2 Jul 2015 14:21:27 +0200
7a3408
Subject: [PATCH] Introduce virHashAtomic
7a3408
7a3408
This is a self-locking wrapper around virHashTable. Only a limited set
7a3408
of APIs are implemented now (the ones which are used in the following
7a3408
patch) as more can be added on demand.
7a3408
7a3408
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
7a3408
(cherry picked from commit a7c22a1f2949e781aef4aa82afda407efd344f95)
7a3408
7a3408
https://bugzilla.redhat.com/show_bug.cgi?id=1090093
7a3408
7a3408
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
7a3408
---
7a3408
 src/libvirt_private.syms |  3 ++
7a3408
 src/util/virhash.c       | 81 ++++++++++++++++++++++++++++++++++++++++++++++++
7a3408
 src/util/virhash.h       | 10 ++++++
7a3408
 3 files changed, 94 insertions(+)
7a3408
7a3408
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
7a3408
index 720afdf..65168b1 100644
7a3408
--- a/src/libvirt_private.syms
7a3408
+++ b/src/libvirt_private.syms
7a3408
@@ -1476,6 +1476,9 @@ virFirewallStartTransaction;
7a3408
 
7a3408
 # util/virhash.h
7a3408
 virHashAddEntry;
7a3408
+virHashAtomicNew;
7a3408
+virHashAtomicSteal;
7a3408
+virHashAtomicUpdate;
7a3408
 virHashCreate;
7a3408
 virHashEqual;
7a3408
 virHashForEach;
7a3408
diff --git a/src/util/virhash.c b/src/util/virhash.c
7a3408
index e3c1880..3cfcc69 100644
7a3408
--- a/src/util/virhash.c
7a3408
+++ b/src/util/virhash.c
7a3408
@@ -31,6 +31,7 @@
7a3408
 #include "virhashcode.h"
7a3408
 #include "virrandom.h"
7a3408
 #include "virstring.h"
7a3408
+#include "virobject.h"
7a3408
 
7a3408
 #define VIR_FROM_THIS VIR_FROM_NONE
7a3408
 
7a3408
@@ -76,6 +77,28 @@ struct _virHashTable {
7a3408
     virHashKeyFree keyFree;
7a3408
 };
7a3408
 
7a3408
+struct _virHashAtomic {
7a3408
+    virObjectLockable parent;
7a3408
+    virHashTablePtr hash;
7a3408
+};
7a3408
+
7a3408
+static virClassPtr virHashAtomicClass;
7a3408
+static void virHashAtomicDispose(void *obj);
7a3408
+
7a3408
+static int virHashAtomicOnceInit(void)
7a3408
+{
7a3408
+    virHashAtomicClass = virClassNew(virClassForObjectLockable(),
7a3408
+                                     "virHashAtomic",
7a3408
+                                     sizeof(virHashAtomic),
7a3408
+                                     virHashAtomicDispose);
7a3408
+    if (!virHashAtomicClass)
7a3408
+        return -1;
7a3408
+    else
7a3408
+        return 0;
7a3408
+}
7a3408
+VIR_ONCE_GLOBAL_INIT(virHashAtomic)
7a3408
+
7a3408
+
7a3408
 static uint32_t virHashStrCode(const void *name, uint32_t seed)
7a3408
 {
7a3408
     return virHashCodeGen(name, strlen(name), seed);
7a3408
@@ -178,6 +201,36 @@ virHashTablePtr virHashCreate(ssize_t size, virHashDataFree dataFree)
7a3408
                              virHashStrFree);
7a3408
 }
7a3408
 
7a3408
+
7a3408
+virHashAtomicPtr
7a3408
+virHashAtomicNew(ssize_t size,
7a3408
+                 virHashDataFree dataFree)
7a3408
+{
7a3408
+    virHashAtomicPtr hash;
7a3408
+
7a3408
+    if (virHashAtomicInitialize() < 0)
7a3408
+        return NULL;
7a3408
+
7a3408
+    if (!(hash = virObjectLockableNew(virHashAtomicClass)))
7a3408
+        return NULL;
7a3408
+
7a3408
+    if (!(hash->hash = virHashCreate(size, dataFree))) {
7a3408
+        virObjectUnref(hash);
7a3408
+        return NULL;
7a3408
+    }
7a3408
+    return hash;
7a3408
+}
7a3408
+
7a3408
+
7a3408
+static void
7a3408
+virHashAtomicDispose(void *obj)
7a3408
+{
7a3408
+    virHashAtomicPtr hash = obj;
7a3408
+
7a3408
+    virHashFree(hash->hash);
7a3408
+}
7a3408
+
7a3408
+
7a3408
 /**
7a3408
  * virHashGrow:
7a3408
  * @table: the hash table
7a3408
@@ -360,6 +413,21 @@ virHashUpdateEntry(virHashTablePtr table, const void *name,
7a3408
     return virHashAddOrUpdateEntry(table, name, userdata, true);
7a3408
 }
7a3408
 
7a3408
+int
7a3408
+virHashAtomicUpdate(virHashAtomicPtr table,
7a3408
+                    const void *name,
7a3408
+                    void *userdata)
7a3408
+{
7a3408
+    int ret;
7a3408
+
7a3408
+    virObjectLock(table);
7a3408
+    ret = virHashAddOrUpdateEntry(table->hash, name, userdata, true);
7a3408
+    virObjectUnlock(table);
7a3408
+
7a3408
+    return ret;
7a3408
+}
7a3408
+
7a3408
+
7a3408
 /**
7a3408
  * virHashLookup:
7a3408
  * @table: the hash table
7a3408
@@ -409,6 +477,19 @@ void *virHashSteal(virHashTablePtr table, const void *name)
7a3408
     return data;
7a3408
 }
7a3408
 
7a3408
+void *
7a3408
+virHashAtomicSteal(virHashAtomicPtr table,
7a3408
+                   const void *name)
7a3408
+{
7a3408
+    void *data;
7a3408
+
7a3408
+    virObjectLock(table);
7a3408
+    data = virHashSteal(table->hash, name);
7a3408
+    virObjectUnlock(table);
7a3408
+
7a3408
+    return data;
7a3408
+}
7a3408
+
7a3408
 
7a3408
 /**
7a3408
  * virHashSize:
7a3408
diff --git a/src/util/virhash.h b/src/util/virhash.h
7a3408
index a137137..50b9a04 100644
7a3408
--- a/src/util/virhash.h
7a3408
+++ b/src/util/virhash.h
7a3408
@@ -21,6 +21,9 @@
7a3408
 typedef struct _virHashTable virHashTable;
7a3408
 typedef virHashTable *virHashTablePtr;
7a3408
 
7a3408
+typedef struct _virHashAtomic virHashAtomic;
7a3408
+typedef virHashAtomic *virHashAtomicPtr;
7a3408
+
7a3408
 /*
7a3408
  * function types:
7a3408
  */
7a3408
@@ -101,6 +104,8 @@ typedef void (*virHashKeyFree)(void *name);
7a3408
  */
7a3408
 virHashTablePtr virHashCreate(ssize_t size,
7a3408
                               virHashDataFree dataFree);
7a3408
+virHashAtomicPtr virHashAtomicNew(ssize_t size,
7a3408
+                                  virHashDataFree dataFree);
7a3408
 virHashTablePtr virHashCreateFull(ssize_t size,
7a3408
                                   virHashDataFree dataFree,
7a3408
                                   virHashKeyCode keyCode,
7a3408
@@ -119,6 +124,9 @@ int virHashAddEntry(virHashTablePtr table,
7a3408
 int virHashUpdateEntry(virHashTablePtr table,
7a3408
                        const void *name,
7a3408
                        void *userdata);
7a3408
+int virHashAtomicUpdate(virHashAtomicPtr table,
7a3408
+                        const void *name,
7a3408
+                        void *userdata);
7a3408
 
7a3408
 /*
7a3408
  * Remove an entry from the hash table.
7a3408
@@ -140,6 +148,8 @@ void *virHashLookup(const virHashTable *table, const void *name);
7a3408
  * Retrieve & remove the userdata.
7a3408
  */
7a3408
 void *virHashSteal(virHashTablePtr table, const void *name);
7a3408
+void *virHashAtomicSteal(virHashAtomicPtr table,
7a3408
+                         const void *name);
7a3408
 
7a3408
 /*
7a3408
  * Get the hash table's key/value pairs and have them optionally sorted.
7a3408
-- 
7a3408
2.4.5
7a3408