Implement InitInternal functions for Mutex, RWLock and Cond, these functions contain error report using virReportSystemErrorFull, it is controlled by an argument 'quite'. The related macros are Init and InitQuite, they call InitInternal function by passing 'false' or 'true' to quite argument. Signed-off-by: Jincheng Miao <jmiao@xxxxxxxxxx> --- src/libvirt_private.syms | 7 ++-- src/util/virthread.c | 95 ++++++++++++++++++++++++------------------------ src/util/virthread.h | 57 ++++++++++++++++++++++------- 3 files changed, 94 insertions(+), 65 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 7e841d1..ceed1bf 100644 --- a/src/util/virthread.c +++ b/src/util/virthread.c @@ -31,6 +31,16 @@ #include "viralloc.h" +#define VIR_THREAD_ERR_EXIT(str) do { \ + errno = ret; \ + if (!quite) { \ + virReportSystemErrorFull(VIR_FROM_NONE, errno, \ + filename, funcname, linenr, \ + "%s", _(str)); \ + } \ + return -1; \ + } while (0) + /* Nothing special required for pthreads */ int virThreadInitialize(void) @@ -48,92 +58,81 @@ int virOnce(virOnceControlPtr once, virOnceFunc init) } -int virMutexInit(virMutexPtr m) +int virMutexInitInternal(virMutexPtr mutex, bool recursive, bool quite, + 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); - ret = pthread_mutex_init(&m->lock, &attr); + 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; -} -int virMutexInitRecursive(virMutexPtr m) -{ - int ret; - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - ret = pthread_mutex_init(&m->lock, &attr); - pthread_mutexattr_destroy(&attr); - if (ret != 0) { - errno = ret; - return -1; - } + if (ret != 0) + VIR_THREAD_ERR_EXIT("unable to init Mutex"); + return 0; } -void virMutexDestroy(virMutexPtr m) +void virMutexDestroy(virMutexPtr mutex) { - pthread_mutex_destroy(&m->lock); + pthread_mutex_destroy(&mutex->lock); } -void virMutexLock(virMutexPtr m) +void virMutexLock(virMutexPtr mutex) { - pthread_mutex_lock(&m->lock); + pthread_mutex_lock(&mutex->lock); } -void virMutexUnlock(virMutexPtr m) +void virMutexUnlock(virMutexPtr mutex) { - pthread_mutex_unlock(&m->lock); + pthread_mutex_unlock(&mutex->lock); } -int virRWLockInit(virRWLockPtr m) +int virRWLockInitInternal(virRWLockPtr rwlock, bool quite, + const char *filename, const char *funcname, + size_t linenr) { - int ret; - ret = pthread_rwlock_init(&m->lock, NULL); - if (ret != 0) { - errno = ret; - return -1; - } + int ret = pthread_rwlock_init(&rwlock->lock, NULL); + if (ret != 0) + VIR_THREAD_ERR_EXIT("unable to init RWLock"); + return 0; } -void virRWLockDestroy(virRWLockPtr m) +void virRWLockDestroy(virRWLockPtr rwlock) { - pthread_rwlock_destroy(&m->lock); + pthread_rwlock_destroy(&rwlock->lock); } -void virRWLockRead(virRWLockPtr m) +void virRWLockRead(virRWLockPtr rwlock) { - pthread_rwlock_rdlock(&m->lock); + pthread_rwlock_rdlock(&rwlock->lock); } -void virRWLockWrite(virRWLockPtr m) +void virRWLockWrite(virRWLockPtr rwlock) { - pthread_rwlock_wrlock(&m->lock); + pthread_rwlock_wrlock(&rwlock->lock); } -void virRWLockUnlock(virRWLockPtr m) +void virRWLockUnlock(virRWLockPtr rwlock) { - pthread_rwlock_unlock(&m->lock); + pthread_rwlock_unlock(&rwlock->lock); } -int virCondInit(virCondPtr c) +int virCondInitInternal(virCondPtr cond, bool quite, + const char *filename, const char *funcname, + size_t linenr) { - int ret; - if ((ret = pthread_cond_init(&c->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 Cond"); + return 0; } diff --git a/src/util/virthread.h b/src/util/virthread.h index 4b92a43..99c16f3 100644 --- a/src/util/virthread.h +++ b/src/util/virthread.h @@ -122,24 +122,54 @@ unsigned long long virThreadID(virThreadPtr thread); int virOnce(virOnceControlPtr once, virOnceFunc init) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; -int virMutexInit(virMutexPtr m) ATTRIBUTE_RETURN_CHECK; -int virMutexInitRecursive(virMutexPtr m) ATTRIBUTE_RETURN_CHECK; -void virMutexDestroy(virMutexPtr m); +int virMutexInitInternal(virMutexPtr mutex, bool recursive, bool quite, + const char *filename, const char *funcname, + size_t linenr) + ATTRIBUTE_RETURN_CHECK; -void virMutexLock(virMutexPtr m); -void virMutexUnlock(virMutexPtr m); +# define virMutexInit(mutex) \ + virMutexInitInternal(mutex, false, false, __FILE__, __FUNCTION__, __LINE__) +# define virMutexInitQuite(mutex) \ + virMutexInitInternal(mutex, false, true, __FILE__, __FUNCTION__, __LINE__) +# define virMutexInitRecursive(mutex) \ + virMutexInitInternal(mutex, true, false, __FILE__, __FUNCTION__, __LINE__) +# define virMutexInitRecursiveQuite(mutex) \ + virMutexInitInternal(mutex, true, true, __FILE__, __FUNCTION__, __LINE__) +void virMutexDestroy(virMutexPtr mutex); -int virRWLockInit(virRWLockPtr m) ATTRIBUTE_RETURN_CHECK; -void virRWLockDestroy(virRWLockPtr m); +void virMutexLock(virMutexPtr mutex); +void virMutexUnlock(virMutexPtr mutex); -void virRWLockRead(virRWLockPtr m); -void virRWLockWrite(virRWLockPtr m); -void virRWLockUnlock(virRWLockPtr m); +int virRWLockInitInternal(virRWLockPtr rwlock, bool quite, + const char *filename, const char *funcname, + size_t linenr) + ATTRIBUTE_RETURN_CHECK; -int virCondInit(virCondPtr c) ATTRIBUTE_RETURN_CHECK; -int virCondDestroy(virCondPtr c); +# define virRWLockInit(rwlock) \ + virRWLockInitInternal(rwlock, false, __FILE__, __FUNCTION__, __LINE__) +# define virRWLockInitQuite(rwlock) \ + virRWLockInitInternal(rwlock, true, __FILE__, __FUNCTION__, __LINE__) + +void virRWLockDestroy(virRWLockPtr rwlock); + +void virRWLockRead(virRWLockPtr rwlock); +void virRWLockWrite(virRWLockPtr rwlock); +void virRWLockUnlock(virRWLockPtr rwlock); + + +int virCondInitInternal(virCondPtr cond, bool quite, + const char *filename, const char *funcname, + size_t linenr) + ATTRIBUTE_RETURN_CHECK; + +# define virCondInit(cond) \ + virCondInitInternal(cond, false, __FILE__, __FUNCTION__, __LINE__) +# define virCondInitQuite(cond) \ + virCondInitInternal(cond, true, __FILE__, __FUNCTION__, __LINE__) + +int virCondDestroy(virCondPtr cond); /* virCondWait, virCondWaitUntil: * These functions can return without the associated predicate @@ -147,7 +177,8 @@ int virCondDestroy(virCondPtr c); * should be enclosed in a while loop that checks the predicate. */ int virCondWait(virCondPtr c, virMutexPtr m) ATTRIBUTE_RETURN_CHECK; -int virCondWaitUntil(virCondPtr c, virMutexPtr m, unsigned long long whenms) ATTRIBUTE_RETURN_CHECK; +int virCondWaitUntil(virCondPtr c, virMutexPtr m, unsigned long long whenms) + ATTRIBUTE_RETURN_CHECK; void virCondSignal(virCondPtr c); void virCondBroadcast(virCondPtr c); -- 1.8.3.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list