From: Adam Dickmeiss Date: Fri, 12 Mar 2010 13:44:49 +0000 (+0100) Subject: Add yaz_mutex_set_name to debug a MUTEX X-Git-Tag: v4.0.3~34 X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=commitdiff_plain;h=0836f7a70cff4b7a8edc2c2a2fd1b3cb2c6588cc Add yaz_mutex_set_name to debug a MUTEX When yaz_mutex_set_name is used, each lock/unlock operation will be yaz_log'ged. Remove YAZ_GNU_THREADS portions, since they are no not in use. The yaz_mutex_destroy now calls pthread_mutex_destroy if YAZ_POSIX_THREADS is on. --- diff --git a/include/yaz/mutex.h b/include/yaz/mutex.h index d465f1c..67c6145 100644 --- a/include/yaz/mutex.h +++ b/include/yaz/mutex.h @@ -47,6 +47,8 @@ YAZ_EXPORT void yaz_mutex_enter(YAZ_MUTEX); YAZ_EXPORT void yaz_mutex_leave(YAZ_MUTEX); /** \brief destroy MUTEX */ YAZ_EXPORT void yaz_mutex_destroy(YAZ_MUTEX *); +/** \brief sets name of MUTEX for debugging purposes */ +void yaz_mutex_set_name(YAZ_MUTEX p, const char *name); YAZ_END_CDECL diff --git a/src/mutex.c b/src/mutex.c index 27058fd..fad796b 100644 --- a/src/mutex.c +++ b/src/mutex.c @@ -30,74 +30,94 @@ #include #endif -#if YAZ_GNU_THREADS -#include -#endif - -#ifdef WIN32 struct yaz_mutex { - CRITICAL_SECTION m_handle; -}; +#ifdef WIN32 + CRITICAL_SECTION handle; #elif YAZ_POSIX_THREADS -struct yaz_mutex { - pthread_mutex_t m_handle; -}; -#elif YAZ_GNU_THREADS -struct yaz_mutex { - pth_mutex_t m_handle; -}; -#else -struct yaz_mutex { - int dummy; -}; + pthread_mutex_t handle; #endif + char *name; +}; -YAZ_EXPORT void yaz_mutex_create(YAZ_MUTEX *p) +void yaz_mutex_create(YAZ_MUTEX *p) { if (!*p) { *p = (YAZ_MUTEX) malloc(sizeof(**p)); #ifdef WIN32 - InitializeCriticalSection(&(*p)->m_handle); + InitializeCriticalSection(&(*p)->handle); #elif YAZ_POSIX_THREADS - pthread_mutex_init(&(*p)->m_handle, 0); -#elif YAZ_GNU_THREADS - pth_mutex_init(&(*p)->m_handle); + pthread_mutex_init(&(*p)->handle, 0); #endif + (*p)->name = 0; } } -YAZ_EXPORT void yaz_mutex_enter(YAZ_MUTEX p) +void yaz_mutex_set_name(YAZ_MUTEX p, const char *name) +{ + if (p->name) + free(p->name); + p->name = 0; + if (name) + p->name = strdup(name); +} + +void yaz_mutex_enter(YAZ_MUTEX p) { if (p) { #ifdef WIN32 - EnterCriticalSection(&p->m_handle); + EnterCriticalSection(&p->handle); #elif YAZ_POSIX_THREADS - pthread_mutex_lock(&p->m_handle); + int r = 1; + if (p->name) + { /* debugging */ + r = pthread_mutex_trylock(&p->handle); + if (r) + { + yaz_log(YLOG_WARN|YLOG_ERRNO, + "yaz_mutex_enter: %p name=%s waiting", p, p->name); + } + } + if (r && pthread_mutex_lock(&p->handle)) + { + yaz_log(YLOG_WARN|YLOG_ERRNO, "yaz_mutex_enter: %p error", p); + } #endif + if (p->name) + { + yaz_log(YLOG_LOG, "yaz_mutex_enter: %p name=%s lock", p, p->name); + } } } -YAZ_EXPORT void yaz_mutex_leave(YAZ_MUTEX p) +void yaz_mutex_leave(YAZ_MUTEX p) { if (p) { #ifdef WIN32 - LeaveCriticalSection(&p->m_handle); + LeaveCriticalSection(&p->handle); #elif YAZ_POSIX_THREADS - pthread_mutex_unlock(&p->m_handle); + pthread_mutex_unlock(&p->handle); #endif + if (p->name) + { + yaz_log(YLOG_LOG, "yaz_mutex_leave: %p name=%s unlock", p, p->name); + } } } -YAZ_EXPORT void yaz_mutex_destroy(YAZ_MUTEX *p) +void yaz_mutex_destroy(YAZ_MUTEX *p) { if (*p) { #ifdef WIN32 - DeleteCriticalSection(&(*p)->m_handle); + DeleteCriticalSection(&(*p)->handle); +#elif YAZ_POSIX_THREADS + pthread_mutex_destroy(&(*p)->handle); #endif + if ((*p)->name) + free((*p)->name); free(*p); *p = 0; }