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>
35 CRITICAL_SECTION handle;
36 #elif YAZ_POSIX_THREADS
37 pthread_mutex_t handle;
43 void yaz_mutex_create(YAZ_MUTEX *p)
47 *p = (YAZ_MUTEX) malloc(sizeof(**p));
49 InitializeCriticalSection(&(*p)->handle);
50 #elif YAZ_POSIX_THREADS
51 pthread_mutex_init(&(*p)->handle, 0);
58 void yaz_mutex_set_name(YAZ_MUTEX p, int log_level, const char *name)
66 p->name = strdup(name);
67 p->log_level = log_level;
71 void yaz_mutex_enter(YAZ_MUTEX p)
76 EnterCriticalSection(&p->handle);
77 #elif YAZ_POSIX_THREADS
81 r = pthread_mutex_trylock(&p->handle);
85 "yaz_mutex_enter: %p name=%s waiting", p, p->name);
88 /* r == 0 if already locked */
89 if (r && pthread_mutex_lock(&p->handle))
91 yaz_log(p->log_level ? p->log_level : YLOG_WARN,
92 "yaz_mutex_enter: %p error", p);
97 yaz_log(p->log_level, "yaz_mutex_enter: %p name=%s lock", p,
103 void yaz_mutex_leave(YAZ_MUTEX p)
108 LeaveCriticalSection(&p->handle);
109 #elif YAZ_POSIX_THREADS
110 pthread_mutex_unlock(&p->handle);
114 yaz_log(p->log_level, "yaz_mutex_leave: %p name=%s unlock", p,
120 void yaz_mutex_destroy(YAZ_MUTEX *p)
125 DeleteCriticalSection(&(*p)->handle);
126 #elif YAZ_POSIX_THREADS
127 pthread_mutex_destroy(&(*p)->handle);
139 * c-file-style: "Stroustrup"
140 * indent-tabs-mode: nil
142 * vim: shiftwidth=4 tabstop=8 expandtab