From e8be0a844a4301118fad33bd953b4a1d4044c575 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 27 Apr 2010 13:03:02 +0200 Subject: [PATCH] Condition variable wrappers Add functions yaz_cond_{create,destroy,wait,signal,broadcast}. --- include/yaz/mutex.h | 38 +++++++++++++++++++++++++++++ src/mutex.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/include/yaz/mutex.h b/include/yaz/mutex.h index 6ef4382..d7f2c6d 100644 --- a/include/yaz/mutex.h +++ b/include/yaz/mutex.h @@ -33,6 +33,7 @@ #define YAZ_MUTEX_H #include +#include #include YAZ_BEGIN_CDECL @@ -40,6 +41,9 @@ YAZ_BEGIN_CDECL /** \brief YAZ MUTEX opaque pointer */ typedef struct yaz_mutex *YAZ_MUTEX; +/** \brief YAZ condition opaque pointer */ +typedef struct yaz_cond *YAZ_COND; + /** \brief create MUTEX \param mutexp is pointer to MUTEX handle (*mutexp must be NULL) @@ -82,6 +86,40 @@ YAZ_EXPORT void yaz_mutex_destroy(YAZ_MUTEX *mutexp); */ void yaz_mutex_set_name(YAZ_MUTEX mutex, int log_level, const char *name); +/** \brief creates condition variable + \param p reference to condition handle + + Upon successful completion *p holds the condition handle; *p = 0 + on error. +*/ +void yaz_cond_create(YAZ_COND *p); + +/** \brief destroys condition variable + \param p reference to condition handle + + Upon completion *p holds 0. +*/ +void yaz_cond_destroy(YAZ_COND *p); + +/** \brief waits for condition + \param p condition variable handle + \param m mutex + \param abstime wait until this time; 0 for indefinite wait + + Semantics like pthread_cond_wait. +*/ +int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timespec *abstime); + +/** \brief unblock one thread waiting for block + \param p condition variable handle +*/ +int yaz_cond_signal(YAZ_COND p); + +/** \brief unblock all threads waiting for block + \param p condition variable handle +*/ +int yaz_cond_broadcast(YAZ_COND p); + YAZ_END_CDECL #endif diff --git a/src/mutex.c b/src/mutex.c index aff0a13..7ff23ed 100644 --- a/src/mutex.c +++ b/src/mutex.c @@ -44,6 +44,14 @@ struct yaz_mutex { int log_level; }; +struct yaz_cond { +#ifdef WIN32 + +#elif YAZ_POSIX_THREADS + pthread_cond_t cond; +#endif +}; + void yaz_mutex_create(YAZ_MUTEX *p) { if (!*p) @@ -157,6 +165,65 @@ void yaz_mutex_destroy(YAZ_MUTEX *p) } } + +void yaz_cond_create(YAZ_COND *p) +{ + *p = (YAZ_COND) malloc(sizeof(**p)); +#ifdef WIN32 +#elif YAZ_POSIX_THREADS + pthread_cond_init(&(*p)->cond, 0); +#endif +} + +void yaz_cond_destroy(YAZ_COND *p) +{ + if (*p) + { +#ifdef WIN32 +#elif YAZ_POSIX_THREADS + pthread_cond_destroy(&(*p)->cond); +#endif + free(*p); + *p = 0; + } +} + +int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timespec *abstime) +{ +#ifdef WIN32 + return -1; +#elif YAZ_POSIX_THREADS + if (abstime) + return pthread_cond_timedwait(&p->cond, &m->handle, abstime); + else + return pthread_cond_wait(&p->cond, &m->handle); +#else + return -1; +#endif +} + +int yaz_cond_signal(YAZ_COND p) +{ +#ifdef WIN32 + return -1; +#elif YAZ_POSIX_THREADS + return pthread_cond_signal(&p->cond); +#else + return -1; +#endif +} + +int yaz_cond_broadcast(YAZ_COND p) +{ +#ifdef WIN32 + return -1; +#elif YAZ_POSIX_THREADS + return pthread_cond_broadcast(&p->cond); +#else + return -1; +#endif +} + /* * Local variables: * c-basic-offset: 4 -- 1.7.10.4