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 Implements MUTEX functions
20 #include <yaz/xmalloc.h>
23 #include <yaz/mutex.h>
24 #include <yaz/gettimeofday.h>
27 #include <sys/timeb.h>
41 CRITICAL_SECTION handle;
42 #elif YAZ_POSIX_THREADS
43 pthread_mutex_t handle;
51 CONDITION_VARIABLE cond;
52 #elif YAZ_POSIX_THREADS
57 void yaz_mutex_create(YAZ_MUTEX *p)
61 *p = (YAZ_MUTEX) malloc(sizeof(**p));
63 InitializeCriticalSection(&(*p)->handle);
64 #elif YAZ_POSIX_THREADS
65 pthread_mutex_init(&(*p)->handle, 0);
72 void yaz_mutex_set_name(YAZ_MUTEX p, int log_level, const char *name)
80 p->name = strdup(name);
81 p->log_level = log_level;
85 void yaz_mutex_enter(YAZ_MUTEX p)
90 EnterCriticalSection(&p->handle);
91 #elif YAZ_POSIX_THREADS
92 int r = 1; /* signal : not locked (yet) */
96 r = pthread_mutex_trylock(&p->handle);
101 struct timeval tv1, tv2;
102 gettimeofday(&tv1, 0);
104 yaz_log(p->log_level,
105 "yaz_mutex_enter: %p tid=%p name=%s waiting",
106 p, (void *) pthread_self(), p->name);
108 r = pthread_mutex_lock(&p->handle);
109 gettimeofday(&tv2, 0);
110 d = 1000000LL * ((long long) tv2.tv_sec - tv1.tv_sec) +
111 tv2.tv_usec - tv1.tv_usec;
112 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s "
114 p, (void *) pthread_self(), p->name, d);
119 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
120 p, (void *) pthread_self(), p->name);
125 r = pthread_mutex_lock(&p->handle);
128 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
129 p, (void *) pthread_self(), p->name);
136 void yaz_mutex_leave(YAZ_MUTEX p)
141 LeaveCriticalSection(&p->handle);
142 #elif YAZ_POSIX_THREADS
143 pthread_mutex_unlock(&p->handle);
146 yaz_log(p->log_level,
147 "yaz_mutex_leave: %p tid=%p name=%s unlock",
148 p, (void *) pthread_self(), p->name);
154 void yaz_mutex_destroy(YAZ_MUTEX *p)
159 DeleteCriticalSection(&(*p)->handle);
160 #elif YAZ_POSIX_THREADS
161 pthread_mutex_destroy(&(*p)->handle);
171 void yaz_cond_create(YAZ_COND *p)
173 *p = (YAZ_COND) malloc(sizeof(**p));
175 InitializeConditionVariable(&(*p)->cond);
176 #elif YAZ_POSIX_THREADS
177 pthread_cond_init(&(*p)->cond, 0);
181 void yaz_cond_destroy(YAZ_COND *p)
186 #elif YAZ_POSIX_THREADS
187 pthread_cond_destroy(&(*p)->cond);
194 int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timeval *abstime)
199 struct timeval tval_now;
202 yaz_gettimeofday(&tval_now);
204 sec = abstime->tv_sec - tval_now.tv_sec;
205 msec = (abstime->tv_usec - tval_now.tv_usec) / 1000;
206 return SleepConditionVariableCS(&p->cond, &m->handle, sec*1000 + msec);
209 return SleepConditionVariableCS(&p->cond, &m->handle, INFINITE);
210 #elif YAZ_POSIX_THREADS
214 s.tv_sec = abstime->tv_sec;
215 s.tv_nsec = abstime->tv_usec * 1000;
216 return pthread_cond_timedwait(&p->cond, &m->handle, &s);
219 return pthread_cond_wait(&p->cond, &m->handle);
225 int yaz_cond_signal(YAZ_COND p)
228 WakeConditionVariable(&p->cond);
230 #elif YAZ_POSIX_THREADS
231 return pthread_cond_signal(&p->cond);
237 int yaz_cond_broadcast(YAZ_COND p)
240 WakeAllConditionVariable(&p->cond);
242 #elif YAZ_POSIX_THREADS
243 return pthread_cond_broadcast(&p->cond);
252 * c-file-style: "Stroustrup"
253 * indent-tabs-mode: nil
255 * vim: shiftwidth=4 tabstop=8 expandtab