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

[libvirt] [PATCH V9 2/6] Support for atomic operations on integers



For threading support, add atomic add and sub operations working on
integers. Base this on locking support provided by virMutex.

---
 src/util/viratomic.h |   91 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)

Index: libvirt-acl/src/util/viratomic.h
===================================================================
--- /dev/null
+++ libvirt-acl/src/util/viratomic.h
@@ -0,0 +1,91 @@
+/*
+ * viratomic.h: atomic integer operations
+ *
+ * Copyright (C) 2012 IBM Corporation
+ *
+ * Authors:
+ *     Stefan Berger <stefanb linux vnet ibm com>
+ *
+ * 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
+ *
+ */
+
+#ifndef __VIR_ATOMIC_H__
+# define __VIR_ATOMIC_H__
+
+# include "threads.h"
+
+typedef struct _virAtomicInt virAtomicInt;
+typedef virAtomicInt *virAtomicIntPtr;
+
+struct _virAtomicInt {
+    virMutex lock;
+    int value;
+};
+
+static inline int
+virAtomicIntInit(virAtomicIntPtr vaip)
+{
+    vaip->value = 0;
+    return virMutexInit(&vaip->lock);
+}
+
+static inline void
+virAtomicIntSet(virAtomicIntPtr vaip, int value)
+{
+     virMutexLock(&vaip->lock);
+
+     vaip->value = value;
+
+     virMutexUnlock(&vaip->lock);
+}
+
+static inline int
+virAtomicIntRead(virAtomicIntPtr vaip)
+{
+     return vaip->value;
+}
+
+static inline int
+virAtomicIntAdd(virAtomicIntPtr vaip, int add)
+{
+    int ret;
+
+    virMutexLock(&vaip->lock);
+
+    vaip->value += add;
+    ret = vaip->value;
+
+    virMutexUnlock(&vaip->lock);
+
+    return ret;
+}
+
+static inline int
+virAtomicIntSub(virAtomicIntPtr vaip, int sub)
+{
+    int ret;
+
+    virMutexLock(&vaip->lock);
+
+    vaip->value -= sub;
+    ret = vaip->value;
+
+    virMutexUnlock(&vaip->lock);
+
+    return ret;
+}
+
+#endif /* __VIR_ATOMIC_H */


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