Condition variable functions in separate DLL
[yaz-moved-to-github.git] / src / condvar.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file condvar.c
8  * \brief Wraps condition variables
9  *
10  */
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <assert.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <stddef.h>
20 #include <yaz/xmalloc.h>
21 #include <yaz/nmem.h>
22 #include <yaz/log.h>
23 #include <yaz/mutex.h>
24 #include <yaz/gettimeofday.h>
25 #ifdef WIN32
26 #include <windows.h>
27 #include <sys/timeb.h>
28 #endif
29 #include <time.h>
30
31 #if HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34
35 #if YAZ_POSIX_THREADS
36 #include <pthread.h>
37 #endif
38
39 #include "mutex-p.h"
40
41 struct yaz_cond {
42 #ifdef WIN32
43     CONDITION_VARIABLE cond;
44 #elif YAZ_POSIX_THREADS
45     pthread_cond_t cond;
46 #endif
47 };
48
49 void yaz_cond_create(YAZ_COND *p)
50 {
51     *p = (YAZ_COND) malloc(sizeof(**p));
52 #ifdef WIN32
53     InitializeConditionVariable(&(*p)->cond);
54 #elif YAZ_POSIX_THREADS
55     pthread_cond_init(&(*p)->cond, 0);
56 #endif
57 }
58
59 void yaz_cond_destroy(YAZ_COND *p)
60 {
61     if (*p)
62     {
63 #ifdef WIN32
64 #elif YAZ_POSIX_THREADS
65         pthread_cond_destroy(&(*p)->cond);
66 #endif
67         free(*p);
68         *p = 0;
69     }
70 }
71
72 int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timeval *abstime)
73 {
74 #ifdef WIN32
75     if (abstime)
76     {
77         struct timeval tval_now;
78         int sec, msec;
79
80         yaz_gettimeofday(&tval_now);
81
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);
85     }
86     else
87         return SleepConditionVariableCS(&p->cond, &m->handle, INFINITE);
88 #elif YAZ_POSIX_THREADS
89     if (abstime)
90     {
91         struct timespec s;
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);
95     }
96     else
97         return pthread_cond_wait(&p->cond, &m->handle);
98 #else
99     return -1;
100 #endif
101 }
102
103 int yaz_cond_signal(YAZ_COND p)
104 {
105 #ifdef WIN32
106     WakeConditionVariable(&p->cond);
107     return 0;
108 #elif YAZ_POSIX_THREADS
109     return pthread_cond_signal(&p->cond);
110 #else
111     return -1;
112 #endif
113 }
114
115 int yaz_cond_broadcast(YAZ_COND p)
116 {
117 #ifdef WIN32
118     WakeAllConditionVariable(&p->cond);
119     return 0;
120 #elif YAZ_POSIX_THREADS
121     return pthread_cond_broadcast(&p->cond);
122 #else
123     return -1;
124 #endif
125 }
126
127 /*
128  * Local variables:
129  * c-basic-offset: 4
130  * c-file-style: "Stroustrup"
131  * indent-tabs-mode: nil
132  * End:
133  * vim: shiftwidth=4 tabstop=8 expandtab
134  */
135