X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fmutex.c;h=fad796b2779288d5076f50e5cdcba7ae26a307ed;hp=27058fd59079eca842989660ccb533599a77ab4c;hb=0836f7a70cff4b7a8edc2c2a2fd1b3cb2c6588cc;hpb=fec04146c50098096daab3c22095f7174b2aa84c 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; }