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