Condition variable wrappers
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 27 Apr 2010 11:03:02 +0000 (13:03 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 27 Apr 2010 11:03:19 +0000 (13:03 +0200)
Add functions yaz_cond_{create,destroy,wait,signal,broadcast}.

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

index 6ef4382..d7f2c6d 100644 (file)
@@ -33,6 +33,7 @@
 #define YAZ_MUTEX_H
 
 #include <stddef.h>
+#include <time.h>
 #include <yaz/yconfig.h>
 
 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
index aff0a13..7ff23ed 100644 (file)
@@ -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