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;
47 void yaz_mutex_create(YAZ_MUTEX *p)
51 *p = (YAZ_MUTEX) malloc(sizeof(**p));
53 InitializeCriticalSection(&(*p)->handle);
54 #elif YAZ_POSIX_THREADS
55 pthread_mutex_init(&(*p)->handle, 0);
62 void yaz_mutex_set_name(YAZ_MUTEX p, int log_level, const char *name)
70 p->name = strdup(name);
71 p->log_level = log_level;
75 void yaz_mutex_enter(YAZ_MUTEX p)
80 EnterCriticalSection(&p->handle);
81 #elif YAZ_POSIX_THREADS
82 int r = 1; /* signal : not locked (yet) */
86 r = pthread_mutex_trylock(&p->handle);
91 struct timeval tv1, tv2;
92 gettimeofday(&tv1, 0);
95 "yaz_mutex_enter: %p tid=%p name=%s waiting",
96 p, (void *) pthread_self(), p->name);
98 r = pthread_mutex_lock(&p->handle);
99 gettimeofday(&tv2, 0);
100 d = 1000000LL * ((long long) tv2.tv_sec - tv1.tv_sec) +
101 tv2.tv_usec - tv1.tv_usec;
102 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s "
104 p, (void *) pthread_self(), p->name, d);
109 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
110 p, (void *) pthread_self(), p->name);
115 r = pthread_mutex_lock(&p->handle);
118 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
119 p, (void *) pthread_self(), p->name);
126 void yaz_mutex_leave(YAZ_MUTEX p)
131 LeaveCriticalSection(&p->handle);
132 #elif YAZ_POSIX_THREADS
133 pthread_mutex_unlock(&p->handle);
136 yaz_log(p->log_level,
137 "yaz_mutex_leave: %p tid=%p name=%s unlock",
138 p, (void *) pthread_self(), p->name);
144 void yaz_mutex_destroy(YAZ_MUTEX *p)
149 DeleteCriticalSection(&(*p)->handle);
150 #elif YAZ_POSIX_THREADS
151 pthread_mutex_destroy(&(*p)->handle);
163 * c-file-style: "Stroustrup"
164 * indent-tabs-mode: nil
166 * vim: shiftwidth=4 tabstop=8 expandtab