1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief Wraps condition variables
20 #include <yaz/xmalloc.h>
23 #include <yaz/mutex.h>
24 #include <yaz/gettimeofday.h>
27 #include <sys/timeb.h>
43 CONDITION_VARIABLE cond;
44 #elif YAZ_POSIX_THREADS
49 void yaz_cond_create(YAZ_COND *p)
51 *p = (YAZ_COND) malloc(sizeof(**p));
53 InitializeConditionVariable(&(*p)->cond);
54 #elif YAZ_POSIX_THREADS
55 pthread_cond_init(&(*p)->cond, 0);
59 void yaz_cond_destroy(YAZ_COND *p)
64 #elif YAZ_POSIX_THREADS
65 pthread_cond_destroy(&(*p)->cond);
72 int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timeval *abstime)
77 struct timeval tval_now;
80 yaz_gettimeofday(&tval_now);
82 sec = abstime->tv_sec - tval_now.tv_sec;
83 msec = (abstime->tv_usec - tval_now.tv_usec) / 1000;
84 return SleepConditionVariableCS(&p->cond, &m->handle, sec*1000 + msec);
87 return SleepConditionVariableCS(&p->cond, &m->handle, INFINITE);
88 #elif YAZ_POSIX_THREADS
92 s.tv_sec = abstime->tv_sec;
93 s.tv_nsec = abstime->tv_usec * 1000;
94 return pthread_cond_timedwait(&p->cond, &m->handle, &s);
97 return pthread_cond_wait(&p->cond, &m->handle);
103 int yaz_cond_signal(YAZ_COND p)
106 WakeConditionVariable(&p->cond);
108 #elif YAZ_POSIX_THREADS
109 return pthread_cond_signal(&p->cond);
115 int yaz_cond_broadcast(YAZ_COND p)
118 WakeAllConditionVariable(&p->cond);
120 #elif YAZ_POSIX_THREADS
121 return pthread_cond_broadcast(&p->cond);
130 * c-file-style: "Stroustrup"
131 * indent-tabs-mode: nil
133 * vim: shiftwidth=4 tabstop=8 expandtab