1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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(YAZ_MUTEX *p)
45 *p = (YAZ_MUTEX) malloc(sizeof(**p));
47 InitializeCriticalSection(&(*p)->handle);
48 #elif YAZ_POSIX_THREADS
49 pthread_mutex_init(&(*p)->handle, 0);
56 void yaz_mutex_set_name(YAZ_MUTEX p, int log_level, const char *name)
64 p->name = strdup(name);
65 p->log_level = log_level;
69 void yaz_mutex_enter(YAZ_MUTEX p)
74 EnterCriticalSection(&p->handle);
75 #elif YAZ_POSIX_THREADS
76 int r = 1; /* signal : not locked (yet) */
80 r = pthread_mutex_trylock(&p->handle);
85 struct timeval tv1, tv2;
86 gettimeofday(&tv1, 0);
89 "yaz_mutex_enter: %p tid=%p name=%s waiting",
90 p, (void *) pthread_self(), p->name);
92 r = pthread_mutex_lock(&p->handle);
93 gettimeofday(&tv2, 0);
94 d = 1000000LL * ((long long) tv2.tv_sec - tv1.tv_sec) +
95 tv2.tv_usec - tv1.tv_usec;
96 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s "
98 p, (void *) pthread_self(), p->name, d);
103 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
104 p, (void *) pthread_self(), p->name);
109 r = pthread_mutex_lock(&p->handle);
112 yaz_log(p->log_level, "yaz_mutex_enter: %p tid=%p name=%s lock",
113 p, (void *) pthread_self(), p->name);
120 void yaz_mutex_leave(YAZ_MUTEX p)
125 LeaveCriticalSection(&p->handle);
126 #elif YAZ_POSIX_THREADS
127 pthread_mutex_unlock(&p->handle);
130 yaz_log(p->log_level,
131 "yaz_mutex_leave: %p tid=%p name=%s unlock",
132 p, (void *) pthread_self(), p->name);
138 void yaz_mutex_destroy(YAZ_MUTEX *p)
143 DeleteCriticalSection(&(*p)->handle);
144 #elif YAZ_POSIX_THREADS
145 pthread_mutex_destroy(&(*p)->handle);
157 * c-file-style: "Stroustrup"
158 * indent-tabs-mode: nil
160 * vim: shiftwidth=4 tabstop=8 expandtab