From: Dennis Schafroth Date: Sat, 7 Aug 2010 09:53:07 +0000 (+0200) Subject: Add new mutex create where mutex attribute can be set X-Git-Tag: v4.0.12~10 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=9f11f349958f122419856006d9295eb0ce41274d;p=yaz-moved-to-github.git Add new mutex create where mutex attribute can be set --- diff --git a/include/yaz/mutex.h b/include/yaz/mutex.h index 414e09f..ff8babb 100644 --- a/include/yaz/mutex.h +++ b/include/yaz/mutex.h @@ -52,6 +52,17 @@ typedef struct yaz_cond *YAZ_COND; */ YAZ_EXPORT void yaz_mutex_create(YAZ_MUTEX *mutexp); +/** \brief create MUTEX with custom MUTEX flags + \param mutexp is pointer to MUTEX handle (*mutexp must be NULL) + \param attr is flags defined by PTHREAD_MUTEX_xxx + + It is important that *mutexp is NULL. If not, yaz_mutex_create will + not modify the handle (assumes it is already created!) + + This calls yax_mutex_create_attr(mutexp, PTHREAD_MUTEX_NORMAL) + */ +YAZ_EXPORT void yaz_mutex_create_attr(YAZ_MUTEX *mutexp, int flags); + /** \brief enter critical section / AKA lock \param mutex MUTEX handle */ diff --git a/src/mutex-p.h b/src/mutex-p.h index 783f119..fedc459 100644 --- a/src/mutex-p.h +++ b/src/mutex-p.h @@ -8,6 +8,7 @@ struct yaz_mutex { CRITICAL_SECTION handle; #elif YAZ_POSIX_THREADS pthread_mutex_t handle; + pthread_mutexattr_t *attr; #endif char *name; int log_level; diff --git a/src/mutex.c b/src/mutex.c index c4f6d5b..d1cd50e 100644 --- a/src/mutex.c +++ b/src/mutex.c @@ -38,21 +38,27 @@ #include "mutex-p.h" -void yaz_mutex_create(YAZ_MUTEX *p) -{ +void yaz_mutex_create_attr(YAZ_MUTEX *p, int flags) { if (!*p) { *p = (YAZ_MUTEX) malloc(sizeof(**p)); #ifdef WIN32 InitializeCriticalSection(&(*p)->handle); #elif YAZ_POSIX_THREADS - pthread_mutex_init(&(*p)->handle, 0); + (*p)->attr = malloc(sizeof( (*p)->attr)); + pthread_mutexattr_init((*p)->attr); + pthread_mutexattr_settype((*p)->attr, flags); + pthread_mutex_init(&(*p)->handle, (*p)->attr); #endif (*p)->name = 0; (*p)->log_level = 0; } } +void yaz_mutex_create(YAZ_MUTEX *p) { + yaz_mutex_create(YAZ_MUTEX *p, 0); +} + void yaz_mutex_set_name(YAZ_MUTEX p, int log_level, const char *name) { if (p->name) @@ -142,6 +148,8 @@ void yaz_mutex_destroy(YAZ_MUTEX *p) #ifdef WIN32 DeleteCriticalSection(&(*p)->handle); #elif YAZ_POSIX_THREADS + pthread_mutexattr_destroy(&(*p)->attr); + free((*p)->attr); pthread_mutex_destroy(&(*p)->handle); #endif if ((*p)->name)