Add yaz_mutex_set_name to debug a MUTEX
authorAdam Dickmeiss <adam@indexdata.dk>
Fri, 12 Mar 2010 13:44:49 +0000 (14:44 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Fri, 12 Mar 2010 13:44:49 +0000 (14:44 +0100)
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.

include/yaz/mutex.h
src/mutex.c

index d465f1c..67c6145 100644 (file)
@@ -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
 
index 27058fd..fad796b 100644 (file)
 #include <pthread.h>
 #endif
 
-#if YAZ_GNU_THREADS
-#include <pth.h>
-#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;
     }