]> xenbits.xensource.com Git - libvirt.git/commitdiff
virthread: Introduce virRWLockInitPreferWriter
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 19 Jul 2017 10:54:23 +0000 (12:54 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 24 Jul 2017 13:54:06 +0000 (15:54 +0200)
We already have virRWLockInit. But this uses pthread defaults
which prefer reader to initialize the RW lock. This may lead to
writer starvation. Therefore we need to have the counterpart that
prefers writers. Now, according to the
pthread_rwlockattr_setkind_np() man page setting
PTHREAD_RWLOCK_PREFER_WRITER_NP attribute is no-op. Therefore we
need to use PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
attribute. So much for good enum value names.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/libvirt_private.syms
src/util/virthread.c
src/util/virthread.h

index b6569e2430241138b54225796e0590dd333e6acd..e58b5ebe32e91e775dc4b4f1ff7f51e58607ffa4 100644 (file)
@@ -2730,6 +2730,7 @@ virMutexUnlock;
 virOnce;
 virRWLockDestroy;
 virRWLockInit;
+virRWLockInitPreferWriter;
 virRWLockRead;
 virRWLockUnlock;
 virRWLockWrite;
index 6c495158f5665804585715abba3c414a87dc329f..a8dd72f8ba164ac08e91f4fa48297cc686c47300 100644 (file)
@@ -95,6 +95,15 @@ void virMutexUnlock(virMutexPtr m)
 }
 
 
+/**
+ * virRWLockInit:
+ * @m: rwlock to init
+ *
+ * Initializes RW lock using pthread default attributes (which
+ * is PTHREAD_RWLOCK_PREFER_READER_NP).
+ *
+ * Returns 0 on success, -1 otherwise.
+ */
 int virRWLockInit(virRWLockPtr m)
 {
     int ret;
@@ -106,6 +115,32 @@ int virRWLockInit(virRWLockPtr m)
     return 0;
 }
 
+
+/**
+ * virRWLockInitPreferWriter:
+ * @m: rwlock to init
+ *
+ * Initializes RW lock which prefers writers over readers.
+ *
+ * Returns 0 on success, -1 otherwise.
+ */
+int virRWLockInitPreferWriter(virRWLockPtr m)
+{
+    int ret;
+    pthread_rwlockattr_t attr;
+
+    pthread_rwlockattr_init(&attr);
+    pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
+    ret = pthread_rwlock_init(&m->lock, &attr);
+    pthread_rwlockattr_destroy(&attr);
+    if (ret != 0) {
+        errno = ret;
+        return -1;
+    }
+    return 0;
+}
+
+
 void virRWLockDestroy(virRWLockPtr m)
 {
     pthread_rwlock_destroy(&m->lock);
index e466d9bf01843a4f4563c05b5d82f4f7663f47d0..18b785af23e21fe2c2b58a851e77a248107556f8 100644 (file)
@@ -136,6 +136,7 @@ void virMutexUnlock(virMutexPtr m);
 
 
 int virRWLockInit(virRWLockPtr m) ATTRIBUTE_RETURN_CHECK;
+int virRWLockInitPreferWriter(virRWLockPtr m) ATTRIBUTE_RETURN_CHECK;
 void virRWLockDestroy(virRWLockPtr m);
 
 void virRWLockRead(virRWLockPtr m);