Sometimes we need the lock in virObjectLockable to be recursive.
Because of the nature of pthreads we don't need a special class
for that - the pthread_* APIs don't distinguish between normal
and recursive locks.
Based-on-work-of: John Ferlan <jferlan(a)redhat.com>
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virobject.c | 22 +++++++++++++++++++---
src/util/virobject.h | 4 ++++
3 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 3b14d7d15..fcf378105 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2417,6 +2417,7 @@ virObjectListFreeCount;
virObjectLock;
virObjectLockableNew;
virObjectNew;
+virObjectRecursiveLockableNew;
virObjectRef;
virObjectRWLockableNew;
virObjectRWLockRead;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index b2fc63aec..1d82e826b 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -257,8 +257,9 @@ virObjectNew(virClassPtr klass)
}
-void *
-virObjectLockableNew(virClassPtr klass)
+static void *
+virObjectLockableNewInternal(virClassPtr klass,
+ bool recursive)
{
virObjectLockablePtr obj;
@@ -272,7 +273,8 @@ virObjectLockableNew(virClassPtr klass)
if (!(obj = virObjectNew(klass)))
return NULL;
- if (virMutexInit(&obj->lock) < 0) {
+ if ((!recursive && virMutexInit(&obj->lock) < 0) ||
+ (recursive && virMutexInitRecursive(&obj->lock) < 0)) {
virReportSystemError(errno, "%s",
_("Unable to initialize mutex"));
virObjectUnref(obj);
@@ -283,6 +285,20 @@ virObjectLockableNew(virClassPtr klass)
}
+void *
+virObjectLockableNew(virClassPtr klass)
+{
+ return virObjectLockableNewInternal(klass, false);
+}
+
+
+void *
+virObjectRecursiveLockableNew(virClassPtr klass)
+{
+ return virObjectLockableNewInternal(klass, true);
+}
+
+
void *
virObjectRWLockableNew(virClassPtr klass)
{
diff --git a/src/util/virobject.h b/src/util/virobject.h
index ac6cf22f9..367d505ae 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -116,6 +116,10 @@ void *
virObjectLockableNew(virClassPtr klass)
ATTRIBUTE_NONNULL(1);
+void *
+virObjectRecursiveLockableNew(virClassPtr klass)
+ ATTRIBUTE_NONNULL(1);
+
void *
virObjectRWLockableNew(virClassPtr klass)
ATTRIBUTE_NONNULL(1);
--
2.13.6