c401cc
From ed3327eb5d7f1df9b2c5a8f3628e37c45c4f9fba Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <ed3327eb5d7f1df9b2c5a8f3628e37c45c4f9fba.1391615407.git.jdenemar@redhat.com>
c401cc
From: "Daniel P. Berrange" <berrange@redhat.com>
c401cc
Date: Tue, 4 Feb 2014 06:23:06 -0700
c401cc
Subject: [PATCH] Add a read/write lock implementation
c401cc
c401cc
https://bugzilla.redhat.com/show_bug.cgi?id=1034807
c401cc
c401cc
Add virRWLock backed up by a POSIX rwlock primitive
c401cc
c401cc
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
c401cc
(cherry picked from commit c065984b58000a44c90588198d222a314ac532fd)
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
---
c401cc
 src/libvirt_private.syms    |  5 +++++
c401cc
 src/util/virthread.h        | 10 ++++++++++
c401cc
 src/util/virthreadpthread.c | 33 +++++++++++++++++++++++++++++++++
c401cc
 src/util/virthreadpthread.h |  4 ++++
c401cc
 src/util/virthreadwin32.c   | 19 +++++++++++++++++++
c401cc
 src/util/virthreadwin32.h   |  4 ++++
c401cc
 6 files changed, 75 insertions(+)
c401cc
c401cc
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
c401cc
index 22df5a9..fa56bfd 100644
c401cc
--- a/src/libvirt_private.syms
c401cc
+++ b/src/libvirt_private.syms
c401cc
@@ -1975,6 +1975,11 @@ virMutexInitRecursive;
c401cc
 virMutexLock;
c401cc
 virMutexUnlock;
c401cc
 virOnce;
c401cc
+virRWLockDestroy;
c401cc
+virRWLockInit;
c401cc
+virRWLockRead;
c401cc
+virRWLockUnlock;
c401cc
+virRWLockWrite;
c401cc
 virThreadCancel;
c401cc
 virThreadCreate;
c401cc
 virThreadID;
c401cc
diff --git a/src/util/virthread.h b/src/util/virthread.h
c401cc
index 84d3bdc..7015d60 100644
c401cc
--- a/src/util/virthread.h
c401cc
+++ b/src/util/virthread.h
c401cc
@@ -28,6 +28,9 @@
c401cc
 typedef struct virMutex virMutex;
c401cc
 typedef virMutex *virMutexPtr;
c401cc
 
c401cc
+typedef struct virRWLock virRWLock;
c401cc
+typedef virRWLock *virRWLockPtr;
c401cc
+
c401cc
 typedef struct virCond virCond;
c401cc
 typedef virCond *virCondPtr;
c401cc
 
c401cc
@@ -89,6 +92,13 @@ void virMutexLock(virMutexPtr m);
c401cc
 void virMutexUnlock(virMutexPtr m);
c401cc
 
c401cc
 
c401cc
+int virRWLockInit(virRWLockPtr m) ATTRIBUTE_RETURN_CHECK;
c401cc
+void virRWLockDestroy(virRWLockPtr m);
c401cc
+
c401cc
+void virRWLockRead(virRWLockPtr m);
c401cc
+void virRWLockWrite(virRWLockPtr m);
c401cc
+void virRWLockUnlock(virRWLockPtr m);
c401cc
+
c401cc
 
c401cc
 int virCondInit(virCondPtr c) ATTRIBUTE_RETURN_CHECK;
c401cc
 int virCondDestroy(virCondPtr c);
c401cc
diff --git a/src/util/virthreadpthread.c b/src/util/virthreadpthread.c
c401cc
index ca841e4..38b6454 100644
c401cc
--- a/src/util/virthreadpthread.c
c401cc
+++ b/src/util/virthreadpthread.c
c401cc
@@ -91,6 +91,39 @@ void virMutexUnlock(virMutexPtr m)
c401cc
 }
c401cc
 
c401cc
 
c401cc
+int virRWLockInit(virRWLockPtr m)
c401cc
+{
c401cc
+    int ret;
c401cc
+    ret = pthread_rwlock_init(&m->lock, NULL);
c401cc
+    if (ret != 0) {
c401cc
+        errno = ret;
c401cc
+        return -1;
c401cc
+    }
c401cc
+    return 0;
c401cc
+}
c401cc
+
c401cc
+void virRWLockDestroy(virRWLockPtr m)
c401cc
+{
c401cc
+    pthread_rwlock_destroy(&m->lock);
c401cc
+}
c401cc
+
c401cc
+
c401cc
+void virRWLockRead(virRWLockPtr m)
c401cc
+{
c401cc
+    pthread_rwlock_rdlock(&m->lock);
c401cc
+}
c401cc
+
c401cc
+void virRWLockWrite(virRWLockPtr m)
c401cc
+{
c401cc
+    pthread_rwlock_wrlock(&m->lock);
c401cc
+}
c401cc
+
c401cc
+
c401cc
+void virRWLockUnlock(virRWLockPtr m)
c401cc
+{
c401cc
+    pthread_rwlock_unlock(&m->lock);
c401cc
+}
c401cc
+
c401cc
 int virCondInit(virCondPtr c)
c401cc
 {
c401cc
     int ret;
c401cc
diff --git a/src/util/virthreadpthread.h b/src/util/virthreadpthread.h
c401cc
index b9f1319..cb607d0 100644
c401cc
--- a/src/util/virthreadpthread.h
c401cc
+++ b/src/util/virthreadpthread.h
c401cc
@@ -27,6 +27,10 @@ struct virMutex {
c401cc
     pthread_mutex_t lock;
c401cc
 };
c401cc
 
c401cc
+struct virRWLock {
c401cc
+    pthread_rwlock_t lock;
c401cc
+};
c401cc
+
c401cc
 struct virCond {
c401cc
     pthread_cond_t cond;
c401cc
 };
c401cc
diff --git a/src/util/virthreadwin32.c b/src/util/virthreadwin32.c
c401cc
index 5d6277f..a9e2353 100644
c401cc
--- a/src/util/virthreadwin32.c
c401cc
+++ b/src/util/virthreadwin32.c
c401cc
@@ -123,6 +123,25 @@ void virMutexUnlock(virMutexPtr m)
c401cc
 }
c401cc
 
c401cc
 
c401cc
+int virRWLockInit(virRWLockPtr m ATTRIBUTE_UNUSED)
c401cc
+{
c401cc
+    errno = ENOSYS;
c401cc
+    return -1;
c401cc
+}
c401cc
+
c401cc
+void virRWLockDestroy(virRWLockPtr m ATTRIBUTE_UNUSED)
c401cc
+{}
c401cc
+
c401cc
+
c401cc
+void virRWLockRead(virRWLockPtr m ATTRIBUTE_UNUSED)
c401cc
+{}
c401cc
+
c401cc
+void virRWLockWrite(virRWLockPtr m ATTRIBUTE_UNUSED)
c401cc
+{}
c401cc
+
c401cc
+
c401cc
+void virRWLockUnlock(virRWLockPtr m ATTRIBUTE_UNUSED)
c401cc
+{}
c401cc
 
c401cc
 int virCondInit(virCondPtr c)
c401cc
 {
c401cc
diff --git a/src/util/virthreadwin32.h b/src/util/virthreadwin32.h
c401cc
index 645031b..31d9444 100644
c401cc
--- a/src/util/virthreadwin32.h
c401cc
+++ b/src/util/virthreadwin32.h
c401cc
@@ -30,6 +30,10 @@ struct virMutex {
c401cc
     HANDLE lock;
c401cc
 };
c401cc
 
c401cc
+struct virRWLock {
c401cc
+    bool ignored;
c401cc
+};
c401cc
+
c401cc
 struct virCond {
c401cc
     virMutex lock;
c401cc
     size_t nwaiters;
c401cc
-- 
c401cc
1.8.5.3
c401cc