1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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)
52 *p = (YAZ_COND) malloc(sizeof(**p));
53 InitializeConditionVariable(&(*p)->cond);
54 #elif YAZ_POSIX_THREADS
55 *p = (YAZ_COND) malloc(sizeof(**p));
56 pthread_cond_init(&(*p)->cond, 0);
62 void yaz_cond_destroy(YAZ_COND *p)
67 #elif YAZ_POSIX_THREADS
68 pthread_cond_destroy(&(*p)->cond);
75 int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timeval *abstime)
81 struct timeval tval_now;
84 yaz_gettimeofday(&tval_now);
86 sec = abstime->tv_sec - tval_now.tv_sec;
87 msec = (abstime->tv_usec - tval_now.tv_usec) / 1000;
88 v = SleepConditionVariableCS(&p->cond, &m->handle, sec*1000 + msec);
91 v = SleepConditionVariableCS(&p->cond, &m->handle, INFINITE);
93 #elif YAZ_POSIX_THREADS
97 s.tv_sec = abstime->tv_sec;
98 s.tv_nsec = abstime->tv_usec * 1000;
99 return pthread_cond_timedwait(&p->cond, &m->handle, &s);
102 return pthread_cond_wait(&p->cond, &m->handle);
108 int yaz_cond_signal(YAZ_COND p)
111 WakeConditionVariable(&p->cond);
113 #elif YAZ_POSIX_THREADS
114 return pthread_cond_signal(&p->cond);
120 int yaz_cond_broadcast(YAZ_COND p)
123 WakeAllConditionVariable(&p->cond);
125 #elif YAZ_POSIX_THREADS
126 return pthread_cond_broadcast(&p->cond);
135 * c-file-style: "Stroustrup"
136 * indent-tabs-mode: nil
138 * vim: shiftwidth=4 tabstop=8 expandtab