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

[libvirt] [PATCH 3/5] Implement a RWLock primitive for drivers to use



This implements a thin wrapper around the pthread_rwlock
primitives. No impl is provided for Win32 at this time
since it is rather hard, and none of our code yet requires
it on Win32

* src/util/threads.h: Add virRWLockInit, virRWLockDestroy,
  virRWLockRead, virRWLockWrite, virRWLockUnlock APIs
* src/util/threads-pthread.h: define virRWLock  struct
* src/util/threads-pthread.c: Implement RWLock APIs
---
 src/libvirt_private.syms   |    6 ++++++
 src/util/threads-pthread.c |   30 ++++++++++++++++++++++++++++++
 src/util/threads-pthread.h |    4 ++++
 src/util/threads.h         |   10 ++++++++++
 4 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f17d260..7b1e3c8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -420,6 +420,12 @@ virCondWait;
 virCondSignal;
 virCondBroadcast;
 
+virRWLockInit;
+virRWLockDestroy;
+virRWLockRead;
+virRWLockWrite;
+virRWLockUnlock;
+
 # util.h
 virFileReadAll;
 virFileWriteStr;
diff --git a/src/util/threads-pthread.c b/src/util/threads-pthread.c
index 4e00bc5..2052c0a 100644
--- a/src/util/threads-pthread.c
+++ b/src/util/threads-pthread.c
@@ -57,6 +57,36 @@ 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)
 {
diff --git a/src/util/threads-pthread.h b/src/util/threads-pthread.h
index 6404d1d..f2b546e 100644
--- a/src/util/threads-pthread.h
+++ b/src/util/threads-pthread.h
@@ -27,6 +27,10 @@ struct virMutex {
     pthread_mutex_t lock;
 };
 
+struct virRWLock {
+    pthread_rwlock_t lock;
+};
+
 struct virCond {
     pthread_cond_t cond;
 };
diff --git a/src/util/threads.h b/src/util/threads.h
index 62239b7..be65031 100644
--- a/src/util/threads.h
+++ b/src/util/threads.h
@@ -27,6 +27,9 @@
 typedef struct virMutex virMutex;
 typedef virMutex *virMutexPtr;
 
+typedef struct virRWLock virRWLock;
+typedef virRWLock *virRWLockPtr;
+
 typedef struct virCond virCond;
 typedef virCond *virCondPtr;
 
@@ -44,6 +47,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) ATTRIBUTE_RETURN_CHECK;
-- 
1.6.2.5


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