#define YAZ_MUTEX_H
#include <stddef.h>
+#include <time.h>
#include <yaz/yconfig.h>
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)
*/
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
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)
}
}
+
+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