[PATCH V2 2/3] add error report for virMutexInit virRWLockInit and virCondInit

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

 



Implement vir{Mutex,RWLock,Cond}InitInternal functions which have
related error report when failure, and vir{Mutex,RWLock,Cond}Init
as macros. So that the caller no longer to print error message
explicitly.

Signed-off-by: Jincheng Miao <jmiao@xxxxxxxxxx>
---
 src/libvirt_private.syms |  7 +++---
 src/util/virthread.c     | 56 +++++++++++++++++++++---------------------------
 src/util/virthread.h     | 25 +++++++++++++++++----
 3 files changed, 49 insertions(+), 39 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 8d3671c..b1c05d3 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2015,18 +2015,17 @@ virSystemdTerminateMachine;
 # util/virthread.h
 virCondBroadcast;
 virCondDestroy;
-virCondInit;
+virCondInitInternal;
 virCondSignal;
 virCondWait;
 virCondWaitUntil;
 virMutexDestroy;
-virMutexInit;
-virMutexInitRecursive;
+virMutexInitInternal;
 virMutexLock;
 virMutexUnlock;
 virOnce;
 virRWLockDestroy;
-virRWLockInit;
+virRWLockInitInternal;
 virRWLockRead;
 virRWLockUnlock;
 virRWLockWrite;
diff --git a/src/util/virthread.c b/src/util/virthread.c
index e22cadd..36044eb 100644
--- a/src/util/virthread.c
+++ b/src/util/virthread.c
@@ -31,6 +31,13 @@
 
 #include "viralloc.h"
 
+#define VIR_THREAD_ERR_EXIT(str) do {                                   \
+        errno = ret;                                                    \
+        virReportSystemErrorFull(VIR_FROM_NONE, errno,                  \
+                                 filename, funcname, linenr,            \
+                                 "%s", _(str));                         \
+        return -1;                                                      \
+    } while (0)
 
 /* Nothing special required for pthreads */
 int virThreadInitialize(void)
@@ -48,33 +55,20 @@ int virOnce(virOnceControlPtr once, virOnceFunc init)
 }
 
 
-int virMutexInit(virMutexPtr mutex)
+int virMutexInitInternal(virMutexPtr mutex, bool recursive,
+                         const char *filename, const char *funcname,
+                         size_t linenr)
 {
     int ret;
     pthread_mutexattr_t attr;
+    int type = recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL;
     pthread_mutexattr_init(&attr);
-    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
+    pthread_mutexattr_settype(&attr, type);
     ret = pthread_mutex_init(&mutex->lock, &attr);
     pthread_mutexattr_destroy(&attr);
-    if (ret != 0) {
-        errno = ret;
-        return -1;
-    }
-    return 0;
-}
+    if (ret != 0)
+        VIR_THREAD_ERR_EXIT("unable to init Mutex");
 
-int virMutexInitRecursive(virMutexPtr mutex)
-{
-    int ret;
-    pthread_mutexattr_t attr;
-    pthread_mutexattr_init(&attr);
-    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
-    ret = pthread_mutex_init(&mutex->lock, &attr);
-    pthread_mutexattr_destroy(&attr);
-    if (ret != 0) {
-        errno = ret;
-        return -1;
-    }
     return 0;
 }
 
@@ -94,14 +88,14 @@ void virMutexUnlock(virMutexPtr mutex)
 }
 
 
-int virRWLockInit(virRWLockPtr rwlock)
+int virRWLockInitInternal(virRWLockPtr rwlock, const char *filename,
+                          const char *funcname, size_t linenr)
 {
     int ret;
     ret = pthread_rwlock_init(&rwlock->lock, NULL);
-    if (ret != 0) {
-        errno = ret;
-        return -1;
-    }
+    if (ret != 0)
+        VIR_THREAD_ERR_EXIT("unable to init RWLock");
+
     return 0;
 }
 
@@ -127,13 +121,13 @@ void virRWLockUnlock(virRWLockPtr rwlock)
     pthread_rwlock_unlock(&rwlock->lock);
 }
 
-int virCondInit(virCondPtr cond)
+int virCondInitInternal(virCondPtr cond, const char *filename,
+                        const char *funcname, size_t linenr)
 {
-    int ret;
-    if ((ret = pthread_cond_init(&cond->cond, NULL)) != 0) {
-        errno = ret;
-        return -1;
-    }
+    int ret = pthread_cond_init(&cond->cond, NULL);
+    if (ret != 0)
+        VIR_THREAD_ERR_EXIT("unable to init Condition");
+
     return 0;
 }
 
diff --git a/src/util/virthread.h b/src/util/virthread.h
index 7c71c6b..280294b 100644
--- a/src/util/virthread.h
+++ b/src/util/virthread.h
@@ -122,15 +122,27 @@ unsigned long long virThreadID(virThreadPtr thread);
 int virOnce(virOnceControlPtr once, virOnceFunc init)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 
-int virMutexInit(virMutexPtr mutex) ATTRIBUTE_RETURN_CHECK;
-int virMutexInitRecursive(virMutexPtr mutex) ATTRIBUTE_RETURN_CHECK;
+int virMutexInitInternal(virMutexPtr mutex, bool recursive,
+                         const char *filename, const char *funcname,
+                         size_t linenr)
+    ATTRIBUTE_RETURN_CHECK;
+# define virMutexInit(mutex)                                            \
+    virMutexInitInternal(mutex, false, __FILE__, __FUNCTION__, __LINE__)
+# define virMutexInitRecursive(mutex)                                   \
+    virMutexInitInternal(mutex, true, __FILE__, __FUNCTION__, __LINE__)
+
 void virMutexDestroy(virMutexPtr mutex);
 
 void virMutexLock(virMutexPtr mutex);
 void virMutexUnlock(virMutexPtr mutex);
 
 
-int virRWLockInit(virRWLockPtr rwlock) ATTRIBUTE_RETURN_CHECK;
+int virRWLockInitInternal(virRWLockPtr rwlock, const char *filename,
+                          const char *funcname, size_t linenr)
+    ATTRIBUTE_RETURN_CHECK;
+# define virRWLockInit(rwlock)                                          \
+    virRWLockInitInternal(rwlock, __FILE__, __FUNCTION__, __LINE__)
+
 void virRWLockDestroy(virRWLockPtr rwlock);
 
 void virRWLockRead(virRWLockPtr rwlock);
@@ -138,7 +150,12 @@ void virRWLockWrite(virRWLockPtr rwlock);
 void virRWLockUnlock(virRWLockPtr rwlock);
 
 
-int virCondInit(virCondPtr cond) ATTRIBUTE_RETURN_CHECK;
+int virCondInitInternal(virCondPtr cond, const char *filename,
+                        const char *funcname, size_t linenr)
+    ATTRIBUTE_RETURN_CHECK;
+# define virCondInit(cond)                                      \
+    virCondInitInternal(cond, __FILE__, __FUNCTION__, __LINE__)
+
 int virCondDestroy(virCondPtr cond);
 
 /* virCondWait, virCondWaitUntil:
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]