Fix retval check of SleepConditionVariableCS
[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     BOOL v;
76     if (abstime)
77     {
78         struct timeval tval_now;
79         int sec, msec;
80
81         yaz_gettimeofday(&tval_now);
82
83         sec = abstime->tv_sec - tval_now.tv_sec;
84         msec = (abstime->tv_usec - tval_now.tv_usec) / 1000;
85         v = SleepConditionVariableCS(&p->cond, &m->handle, sec*1000 + msec);
86     }
87     else
88         v = SleepConditionVariableCS(&p->cond, &m->handle, INFINITE);
89     return v ? 0 : -1;
90 #elif YAZ_POSIX_THREADS
91     if (abstime)
92     {
93         struct timespec s;
94         s.tv_sec = abstime->tv_sec;
95         s.tv_nsec = abstime->tv_usec * 1000;
96         return pthread_cond_timedwait(&p->cond, &m->handle, &s);
97     }
98     else
99         return pthread_cond_wait(&p->cond, &m->handle);
100 #else
101     return -1;
102 #endif
103 }
104
105 int yaz_cond_signal(YAZ_COND p)
106 {
107 #ifdef WIN32
108     WakeConditionVariable(&p->cond);
109     return 0;
110 #elif YAZ_POSIX_THREADS
111     return pthread_cond_signal(&p->cond);
112 #else
113     return -1;
114 #endif
115 }
116
117 int yaz_cond_broadcast(YAZ_COND p)
118 {
119 #ifdef WIN32
120     WakeAllConditionVariable(&p->cond);
121     return 0;
122 #elif YAZ_POSIX_THREADS
123     return pthread_cond_broadcast(&p->cond);
124 #else
125     return -1;
126 #endif
127 }
128
129 /*
130  * Local variables:
131  * c-basic-offset: 4
132  * c-file-style: "Stroustrup"
133  * indent-tabs-mode: nil
134  * End:
135  * vim: shiftwidth=4 tabstop=8 expandtab
136  */
137