[PATCH 1/6] Add virObject and virAtomic.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



virObject is the base struct that manages reference-counting
for all structs that need the ability of reference-counting.

virAtomic provides atomic operations which are thread-safe.
---
 src/Makefile.am          |    2 +
 src/libvirt_private.syms |    5 ++++
 src/util/viratomic.c     |   46 ++++++++++++++++++++++++++++++++++++++++
 src/util/viratomic.h     |   30 ++++++++++++++++++++++++++
 src/util/virobject.c     |   52 ++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virobject.h     |   39 ++++++++++++++++++++++++++++++++++
 6 files changed, 174 insertions(+), 0 deletions(-)
 create mode 100644 src/util/viratomic.c
 create mode 100644 src/util/viratomic.h
 create mode 100644 src/util/virobject.c
 create mode 100644 src/util/virobject.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 9b54679..9e74060 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -81,6 +81,8 @@ UTIL_SOURCES =							\
 		util/util.c util/util.h				\
 		util/xml.c util/xml.h				\
 		util/virtaudit.c util/virtaudit.h               \
+		util/viratomic.c util/viratomic.h	\
+		util/virobject.c util/virobject.h	\
 		util/virterror.c util/virterror_internal.h
 
 EXTRA_DIST += util/threads-pthread.c util/threads-win32.c
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 65a86d3..98f8d6f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -993,3 +993,8 @@ virXPathUInt;
 virXPathULong;
 virXPathULongHex;
 virXPathULongLong;
+
+# object.h
+virObjectInit;
+virObjectRef;
+virObjectUnref;
diff --git a/src/util/viratomic.c b/src/util/viratomic.c
new file mode 100644
index 0000000..629f189
--- /dev/null
+++ b/src/util/viratomic.c
@@ -0,0 +1,46 @@
+/*
+ * viratomic.c: atomic operations
+ *
+ * Copyright (C) 2011 Hu Tao
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors:
+ *     Hu Tao <hutao@xxxxxxxxxxxxxx>
+ */
+
+#include "viratomic.h"
+
+#ifdef WIN32
+long virAtomicInc(long *value)
+{
+    return InterlockedIncrement(value);
+}
+
+long virAtomicDec(long *value)
+{
+    return InterlockedDecrement(value);
+}
+#else /* WIN32 */
+long virAtomicInc(long *value)
+{
+    return __sync_add_and_fetch(value, 1);
+}
+
+long virAtomicDec(long *value)
+{
+    return __sync_sub_and_fetch(value, 1);
+}
+#endif /* WIN32 */
diff --git a/src/util/viratomic.h b/src/util/viratomic.h
new file mode 100644
index 0000000..a10cb65
--- /dev/null
+++ b/src/util/viratomic.h
@@ -0,0 +1,30 @@
+/*
+ * viratomic.h: atomic operations
+ *
+ * Copyright (C) 2011 Hu Tao
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors:
+ *     Hu Tao <hutao@xxxxxxxxxxxxxx>
+ */
+
+#ifndef __VIR_ATOMIC_H
+#define __VIR_ATOMIC_H
+
+long virAtomicInc(long *value);
+long virAtomicDec(long *value);
+
+#endif /* __VIR_ATOMIC_H */
diff --git a/src/util/virobject.c b/src/util/virobject.c
new file mode 100644
index 0000000..aeedbef
--- /dev/null
+++ b/src/util/virobject.c
@@ -0,0 +1,52 @@
+/*
+ * virobject.c: base object that manages reference-counting
+ *
+ * Copyright (C) 2011 Hu Tao
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors:
+ *     Hu Tao <hutao@xxxxxxxxxxxxxx>
+ */
+
+#include "viratomic.h"
+#include "virobject.h"
+#include "logging.h"
+
+int virObjectInit(virObjectPtr obj, void (*free)(virObjectPtr obj))
+{
+    if (!free) {
+        VIR_ERROR0("method free is required.");
+        return -1;
+    }
+
+    obj->ref = 1;
+    obj->free = free;
+
+    return 0;
+}
+
+void virObjectRef(virObjectPtr obj)
+{
+    sa_assert(obj->ref > 0);
+    virAtomicInc(&obj->ref);
+}
+
+void virObjectUnref(virObjectPtr obj)
+{
+    sa_assert(obj->ref > 0);
+    if (virAtomicDec(&obj->ref) == 0)
+        obj->free(obj);
+}
diff --git a/src/util/virobject.h b/src/util/virobject.h
new file mode 100644
index 0000000..cd7d3e8
--- /dev/null
+++ b/src/util/virobject.h
@@ -0,0 +1,39 @@
+/*
+ * virobject.h: base object that manages reference-counting
+ *
+ * Copyright (C) 2011 Hu Tao
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Authors:
+ *     Hu Tao <hutao@xxxxxxxxxxxxxx>
+ */
+
+#ifndef __VIR_OBJECT_H
+#define __VIR_OBJECT_H
+
+typedef struct _virObject virObject;
+typedef virObject *virObjectPtr;
+
+struct _virObject {
+    long ref;
+    void (*free)(virObjectPtr obj);
+};
+
+int virObjectInit(virObjectPtr obj, void (*free)(virObjectPtr obj));
+void virObjectRef(virObjectPtr obj);
+void virObjectUnref(virObjectPtr obj);
+
+#endif /* __VIR_OBJECT_H */
-- 
1.7.3.1


-- 
Thanks,
Hu Tao

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]