Bump copyright year
[yaz-moved-to-github.git] / src / mutex.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file mutex.c
8  * \brief Implements MUTEX functions
9  *
10  */
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <assert.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <stddef.h>
20 #include <yaz/xmalloc.h>
21 #include <yaz/nmem.h>
22 #include <yaz/log.h>
23 #include <yaz/mutex.h>
24
25 #ifdef WIN32
26 #include <windows.h>
27 #endif
28
29 #if YAZ_POSIX_THREADS
30 #include <pthread.h>
31 #endif
32
33 #if YAZ_GNU_THREADS
34 #include <pth.h>
35 #endif
36
37 #ifdef WIN32
38 struct yaz_mutex {
39     CRITICAL_SECTION m_handle;
40 };
41 #elif YAZ_POSIX_THREADS
42 struct yaz_mutex {
43     pthread_mutex_t m_handle;
44 };
45 #elif YAZ_GNU_THREADS
46 struct yaz_mutex {
47     pth_mutex_t m_handle;
48 };
49 #else
50 struct yaz_mutex {
51     int dummy;
52 };
53 #endif
54
55 YAZ_EXPORT void yaz_mutex_create(YAZ_MUTEX *p)
56 {
57     if (!*p)
58     {
59         *p = (YAZ_MUTEX) malloc(sizeof(**p));
60 #ifdef WIN32
61         InitializeCriticalSection(&(*p)->m_handle);
62 #elif YAZ_POSIX_THREADS
63         pthread_mutex_init(&(*p)->m_handle, 0);
64 #elif YAZ_GNU_THREADS
65         pth_mutex_init(&(*p)->m_handle);
66 #endif
67     }
68 }
69
70 YAZ_EXPORT void yaz_mutex_enter(YAZ_MUTEX p)
71 {
72     if (p)
73     {
74 #ifdef WIN32
75         EnterCriticalSection(&p->m_handle);
76 #elif YAZ_POSIX_THREADS
77         pthread_mutex_lock(&p->m_handle);
78 #endif
79     }
80 }
81
82 YAZ_EXPORT void yaz_mutex_leave(YAZ_MUTEX p)
83 {
84     if (p)
85     {
86 #ifdef WIN32
87         LeaveCriticalSection(&p->m_handle);
88 #elif YAZ_POSIX_THREADS
89         pthread_mutex_unlock(&p->m_handle);
90 #endif
91     }
92 }
93
94 YAZ_EXPORT void yaz_mutex_destroy(YAZ_MUTEX *p)
95 {
96     if (*p)
97     {
98 #ifdef WIN32
99         DeleteCriticalSection(&(*p)->m_handle);
100 #endif
101         free(*p);
102         *p = 0;
103     }
104 }
105
106 /*
107  * Local variables:
108  * c-basic-offset: 4
109  * c-file-style: "Stroustrup"
110  * indent-tabs-mode: nil
111  * End:
112  * vim: shiftwidth=4 tabstop=8 expandtab
113  */
114