Add virRWLock backed up by a POSIX rwlock primitive
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/libvirt_private.syms | 5 +++++
src/util/virthread.h | 10 ++++++++++
src/util/virthreadpthread.c | 31 +++++++++++++++++++++++++++++++
src/util/virthreadpthread.h | 4 ++++
4 files changed, 50 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index d1a58f9..eb91693 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1816,6 +1816,11 @@ virMutexInitRecursive;
virMutexLock;
virMutexUnlock;
virOnce;
+virRWLockInit;
+virRWLockDestroy;
+virRWLockRead;
+virRWLockWrite;
+virRWLockUnlock;
virThreadCancel;
virThreadCreate;
virThreadID;
diff --git a/src/util/virthread.h b/src/util/virthread.h
index 84d3bdc..7015d60 100644
--- a/src/util/virthread.h
+++ b/src/util/virthread.h
@@ -28,6 +28,9 @@
typedef struct virMutex virMutex;
typedef virMutex *virMutexPtr;
+typedef struct virRWLock virRWLock;
+typedef virRWLock *virRWLockPtr;
+
typedef struct virCond virCond;
typedef virCond *virCondPtr;
@@ -89,6 +92,13 @@ void virMutexLock(virMutexPtr m);
void virMutexUnlock(virMutexPtr m);
+int virRWLockInit(virRWLockPtr m) ATTRIBUTE_RETURN_CHECK;
+void virRWLockDestroy(virRWLockPtr m);
+
+void virRWLockRead(virRWLockPtr m);
+void virRWLockWrite(virRWLockPtr m);
+void virRWLockUnlock(virRWLockPtr m);
+
int virCondInit(virCondPtr c) ATTRIBUTE_RETURN_CHECK;
int virCondDestroy(virCondPtr c);
diff --git a/src/util/virthreadpthread.c b/src/util/virthreadpthread.c
index ca841e4..2efb4c1 100644
--- a/src/util/virthreadpthread.c
+++ b/src/util/virthreadpthread.c
@@ -91,6 +91,37 @@ void virMutexUnlock(virMutexPtr m)
}
+int virRWLockInit(virRWLockPtr m)
+{
+ if (pthread_rwlock_init(&m->lock, NULL) != 0) {
+ errno = EINVAL;
+ return -1;
+ }
+ return 0;
+}
+
+void virRWLockDestroy(virRWLockPtr m)
+{
+ pthread_rwlock_destroy(&m->lock);
+}
+
+
+void virRWLockRead(virRWLockPtr m)
+{
+ pthread_rwlock_rdlock(&m->lock);
+}
+
+void virRWLockWrite(virRWLockPtr m)
+{
+ pthread_rwlock_wrlock(&m->lock);
+}
+
+
+void virRWLockUnlock(virRWLockPtr m)
+{
+ pthread_rwlock_unlock(&m->lock);
+}
+
int virCondInit(virCondPtr c)
{
int ret;
diff --git a/src/util/virthreadpthread.h b/src/util/virthreadpthread.h
index b9f1319..cb607d0 100644
--- a/src/util/virthreadpthread.h
+++ b/src/util/virthreadpthread.h
@@ -27,6 +27,10 @@ struct virMutex {
pthread_mutex_t lock;
};
+struct virRWLock {
+ pthread_rwlock_t lock;
+};
+
struct virCond {
pthread_cond_t cond;
};
--
1.8.4.2