YAZ 5.35.1
condvar.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
5
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
41struct yaz_cond {
42#ifdef WIN32
43 CONDITION_VARIABLE cond;
44#elif YAZ_POSIX_THREADS
45 pthread_cond_t cond;
46#endif
47};
48
50{
51#ifdef WIN32
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);
57#else
58 *p = 0;
59#endif
60}
61
63{
64 if (*p)
65 {
66#ifdef WIN32
67#elif YAZ_POSIX_THREADS
68 pthread_cond_destroy(&(*p)->cond);
69#endif
70 free(*p);
71 *p = 0;
72 }
73}
74
75int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timeval *abstime)
76{
77#ifdef WIN32
78 BOOL v;
79 if (abstime)
80 {
81 struct timeval tval_now;
82 int sec, msec;
83
84 yaz_gettimeofday(&tval_now);
85
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);
89 }
90 else
91 v = SleepConditionVariableCS(&p->cond, &m->handle, INFINITE);
92 return v ? 0 : -1;
93#elif YAZ_POSIX_THREADS
94 if (abstime)
95 {
96 struct timespec s;
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);
100 }
101 else
102 return pthread_cond_wait(&p->cond, &m->handle);
103#else
104 return -1;
105#endif
106}
107
109{
110#ifdef WIN32
111 WakeConditionVariable(&p->cond);
112 return 0;
113#elif YAZ_POSIX_THREADS
114 return pthread_cond_signal(&p->cond);
115#else
116 return -1;
117#endif
118}
119
121{
122#ifdef WIN32
123 WakeAllConditionVariable(&p->cond);
124 return 0;
125#elif YAZ_POSIX_THREADS
126 return pthread_cond_broadcast(&p->cond);
127#else
128 return -1;
129#endif
130}
131
132/*
133 * Local variables:
134 * c-basic-offset: 4
135 * c-file-style: "Stroustrup"
136 * indent-tabs-mode: nil
137 * End:
138 * vim: shiftwidth=4 tabstop=8 expandtab
139 */
140
void yaz_cond_destroy(YAZ_COND *p)
destroys condition variable
Definition condvar.c:62
void yaz_cond_create(YAZ_COND *p)
creates condition variable
Definition condvar.c:49
int yaz_cond_signal(YAZ_COND p)
unblock one thread waiting for block
Definition condvar.c:108
int yaz_cond_broadcast(YAZ_COND p)
unblock all threads waiting for block
Definition condvar.c:120
int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timeval *abstime)
waits for condition
Definition condvar.c:75
void * malloc(YYSIZE_T)
void free(void *)
Header for errno utilities.
int yaz_gettimeofday(struct timeval *tval)
gettimeofday
Header for gettimeofday wrapper.
Logging utility.
Declares internal definitions of for Mutex functions.
Header for Mutex functions.
struct yaz_cond * YAZ_COND
YAZ condition opaque pointer.
Definition mutex.h:45
Header for Nibble Memory functions.
Header for memory handling functions.