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;
42 void yaz_mutex_create(YAZ_MUTEX *p)
46 *p = (YAZ_MUTEX) malloc(sizeof(**p));
48 InitializeCriticalSection(&(*p)->handle);
49 #elif YAZ_POSIX_THREADS
50 pthread_mutex_init(&(*p)->handle, 0);
56 void yaz_mutex_set_name(YAZ_MUTEX p, const char *name)
62 p->name = strdup(name);
65 void yaz_mutex_enter(YAZ_MUTEX p)
70 EnterCriticalSection(&p->handle);
71 #elif YAZ_POSIX_THREADS
75 r = pthread_mutex_trylock(&p->handle);
78 yaz_log(YLOG_WARN|YLOG_ERRNO,
79 "yaz_mutex_enter: %p name=%s waiting", p, p->name);
82 if (r && pthread_mutex_lock(&p->handle))
84 yaz_log(YLOG_WARN|YLOG_ERRNO, "yaz_mutex_enter: %p error", p);
89 yaz_log(YLOG_LOG, "yaz_mutex_enter: %p name=%s lock", p, p->name);
94 void yaz_mutex_leave(YAZ_MUTEX p)
99 LeaveCriticalSection(&p->handle);
100 #elif YAZ_POSIX_THREADS
101 pthread_mutex_unlock(&p->handle);
105 yaz_log(YLOG_LOG, "yaz_mutex_leave: %p name=%s unlock", p, p->name);
110 void yaz_mutex_destroy(YAZ_MUTEX *p)
115 DeleteCriticalSection(&(*p)->handle);
116 #elif YAZ_POSIX_THREADS
117 pthread_mutex_destroy(&(*p)->handle);
129 * c-file-style: "Stroustrup"
130 * indent-tabs-mode: nil
132 * vim: shiftwidth=4 tabstop=8 expandtab