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>
39 CRITICAL_SECTION handle;
40 #elif YAZ_POSIX_THREADS
41 pthread_mutex_t handle;
50 #elif YAZ_POSIX_THREADS
55 void yaz_mutex_create(YAZ_MUTEX *p)
59 *p = (YAZ_MUTEX) malloc(sizeof(**p));
61 InitializeCriticalSection(&(*p)->handle);
62 #elif YAZ_POSIX_THREADS
63 pthread_mutex_init(&(*p)->handle, 0);
70 void yaz_mutex_set_name(YAZ_MUTEX p, int log_level, const char *name)
78 p->name = strdup(name);
79 p->log_level = log_level;
83 void yaz_mutex_enter(YAZ_MUTEX p)
88 EnterCriticalSection(&p->handle);
89 #elif YAZ_POSIX_THREADS
90 int r = 1; /* signal : not locked (yet) */
94 r = pthread_mutex_trylock(&p->handle);
99 struct timeval tv1, tv2;
100 gettimeofday(&tv1, 0);
102 yaz_log(p->log_level,
103 "yaz_mutex_enter: %p tid=%p name=%s waiting",
104 p, (void *) pthread_self(), p->name);
106 r = pthread_mutex_lock(&p->handle);
107 gettimeofday(&tv2, 0);
108 d = 1000000LL * ((long long) tv2.tv_sec - tv1.tv_sec) +
109 tv2.tv_usec - tv1.tv_usec;
110 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s "
112 p, (void *) pthread_self(), p->name, d);
117 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
118 p, (void *) pthread_self(), p->name);
123 r = pthread_mutex_lock(&p->handle);
126 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
127 p, (void *) pthread_self(), p->name);
134 void yaz_mutex_leave(YAZ_MUTEX p)
139 LeaveCriticalSection(&p->handle);
140 #elif YAZ_POSIX_THREADS
141 pthread_mutex_unlock(&p->handle);
144 yaz_log(p->log_level,
145 "yaz_mutex_leave: %p tid=%p name=%s unlock",
146 p, (void *) pthread_self(), p->name);
152 void yaz_mutex_destroy(YAZ_MUTEX *p)
157 DeleteCriticalSection(&(*p)->handle);
158 #elif YAZ_POSIX_THREADS
159 pthread_mutex_destroy(&(*p)->handle);
169 void yaz_cond_create(YAZ_COND *p)
171 *p = (YAZ_COND) malloc(sizeof(**p));
173 #elif YAZ_POSIX_THREADS
174 pthread_cond_init(&(*p)->cond, 0);
178 void yaz_cond_destroy(YAZ_COND *p)
183 #elif YAZ_POSIX_THREADS
184 pthread_cond_destroy(&(*p)->cond);
191 int yaz_cond_wait(YAZ_COND p, YAZ_MUTEX m, const struct timespec *abstime)
195 #elif YAZ_POSIX_THREADS
197 return pthread_cond_timedwait(&p->cond, &m->handle, abstime);
199 return pthread_cond_wait(&p->cond, &m->handle);
205 int yaz_cond_signal(YAZ_COND p)
209 #elif YAZ_POSIX_THREADS
210 return pthread_cond_signal(&p->cond);
216 int yaz_cond_broadcast(YAZ_COND p)
220 #elif YAZ_POSIX_THREADS
221 return pthread_cond_broadcast(&p->cond);
230 * c-file-style: "Stroustrup"
231 * indent-tabs-mode: nil
233 * vim: shiftwidth=4 tabstop=8 expandtab