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 void yaz_mutex_create_attr(YAZ_MUTEX *p, int flags) {
44 *p = (YAZ_MUTEX) malloc(sizeof(**p));
46 InitializeCriticalSection(&(*p)->handle);
47 #elif YAZ_POSIX_THREADS
48 (*p)->attr = malloc(sizeof( (*p)->attr));
49 pthread_mutexattr_init((*p)->attr);
50 pthread_mutexattr_settype((*p)->attr, flags);
51 pthread_mutex_init(&(*p)->handle, (*p)->attr);
58 void yaz_mutex_create(YAZ_MUTEX *p) {
59 yaz_mutex_create(YAZ_MUTEX *p, 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_mutexattr_destroy(&(*p)->attr);
153 pthread_mutex_destroy(&(*p)->handle);
165 * c-file-style: "Stroustrup"
166 * indent-tabs-mode: nil
168 * vim: shiftwidth=4 tabstop=8 expandtab