X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmutex.c;h=6360e30e42bf88dcc8a04937184bf289702b0877;hb=68bc4fc5d306d65ce2f9404626b9c627509a3e91;hp=3491f831f84a95c3783a87819292eb0ecdce3c61;hpb=ee6ab2ee3a9ee1a8c65d7272ec7fba1d886f5af0;p=yaz-moved-to-github.git diff --git a/src/mutex.c b/src/mutex.c index 3491f83..6360e30 100644 --- a/src/mutex.c +++ b/src/mutex.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2008 Index Data + * Copyright (C) 1995-2010 Index Data * See the file LICENSE for details. */ @@ -30,74 +30,104 @@ #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; + int log_level; +}; -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; + (*p)->log_level = 0; } } -YAZ_EXPORT void yaz_mutex_enter(YAZ_MUTEX p) +void yaz_mutex_set_name(YAZ_MUTEX p, int log_level, const char *name) +{ + if (p->name) + free(p->name); + p->name = 0; + p->log_level = 0; + if (name) + { + p->name = strdup(name); + p->log_level = log_level; + } +} + +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->log_level) + { /* debugging */ + r = pthread_mutex_trylock(&p->handle); + if (r) + { + yaz_log(p->log_level, + "yaz_mutex_enter: %p name=%s waiting", p, p->name); + } + } + /* r == 0 if already locked */ + if (r && pthread_mutex_lock(&p->handle)) + { + yaz_log(p->log_level ? p->log_level : YLOG_WARN, + "yaz_mutex_enter: %p error", p); + } #endif + if (p->log_level) + { + yaz_log(p->log_level, "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->log_level) + { + yaz_log(p->log_level, "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; } @@ -106,6 +136,7 @@ YAZ_EXPORT void yaz_mutex_destroy(YAZ_MUTEX *p) /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab