blocxx
NonRecursiveMutexImpl.cpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
38
39#include "blocxx/BLOCXX_config.h"
41#include <cerrno>
42#include <cassert>
43
44namespace BLOCXX_NAMESPACE
45{
46
48{
49
50#if defined (BLOCXX_USE_PTHREAD)
51
52#if !defined (BLOCXX_NCR)
58int
59createMutex(NonRecursiveMutex_t& handle)
60{
61 pthread_mutexattr_t attr;
62 int res = pthread_mutexattr_init(&attr);
63 assert(res == 0);
64 if (res != 0)
65 {
66 return -1;
67 }
68
69 res = pthread_mutex_init(&handle.mutex, &attr);
70 pthread_mutexattr_destroy(&attr);
71 if (res != 0)
72 {
73 return -1;
74 }
75
76 return 0;
77}
78
79#else //#if !defined (BLOCXX_NCR)
80int
81createMutex(NonRecursiveMutex_t& handle)
82{
83 pthread_mutexattr_t attr;
84 int res = pthread_mutexattr_create(&attr);
85 assert(res == 0);
86 if (res != 0)
87 {
88 return -1;
89 }
90
91 res = pthread_mutex_init(&handle.mutex, attr);
92 pthread_mutexattr_delete(&attr);
93 if (res != 0)
94 {
95 return -1;
96 }
97
98 return 0;
99}
100#endif //#if !defined (BLOCXX_NCR)
101
111int
112destroyMutex(NonRecursiveMutex_t& handle)
113{
114 switch (pthread_mutex_destroy(&handle.mutex))
115 {
116 case 0:
117 break;
118 case EBUSY:
119 return -1;
120 break;
121 default:
122 return -2;
123 }
124 return 0;
125}
134int
135acquireMutex(NonRecursiveMutex_t& handle)
136{
137 int res = pthread_mutex_lock(&handle.mutex);
138 assert(res == 0);
139 return res;
140}
147int
148releaseMutex(NonRecursiveMutex_t& handle)
149{
150 int res = pthread_mutex_unlock(&handle.mutex);
151 assert(res == 0);
152 return res;
153}
154
155int
156conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
157{
158 state.pmutex = &handle.mutex;
159 return 0;
160}
161
162int
163conditionPostWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
164{
165 return 0;
166}
167
168#endif //#if defined (BLOCXX_USE_PTHREAD)
169
170#if defined(BLOCXX_WIN32)
171int
172createMutex(NonRecursiveMutex_t& handle)
173{
174 int cc = -1;
175 if ((handle = CreateMutex(NULL, FALSE, NULL)))
176 {
177 cc = 0;
178 }
179 return cc;
180}
181
182int
183destroyMutex(NonRecursiveMutex_t& handle)
184{
185 ReleaseMutex(handle);
186 return (CloseHandle(handle) == 0) ? -2 : 0;
187}
188
189int
190acquireMutex(NonRecursiveMutex_t& handle)
191{
192 int cc = -1;
193 if (WaitForSingleObject(handle, INFINITE) != WAIT_FAILED)
194 {
195 cc = 0;
196 }
197 return cc;
198}
199
200int
201releaseMutex(NonRecursiveMutex_t& handle)
202{
203 return (ReleaseMutex(handle)) ? 0 : -1;
204}
205
206int
207conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
208{
209 state.pmutex = &handle;
210 return 0;
211}
212
213int
214conditionPostWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
215{
216 return 0;
217}
218
219#endif //#if defined(BLOCXX_WIN32)
220
221} // end namespace NonRecursiveMutexImpl
222} // end namespace BLOCXX_NAMESPACE
223
The NonRecursiveMutexImpl namespace represents the functionality needed by the BloCxx non recursive M...
BLOCXX_COMMON_API int conditionPostWait(NonRecursiveMutex_t &handle, NonRecursiveMutexLockState &state)
BLOCXX_COMMON_API int destroyMutex(NonRecursiveMutex_t &handle)
Destroy a mutex previously created with createMutex.
BLOCXX_COMMON_API int acquireMutex(NonRecursiveMutex_t &handle)
Acquire the mutex specified by a given mutex handle.
BLOCXX_COMMON_API int releaseMutex(NonRecursiveMutex_t &handle)
Release a mutex that was previously acquired with the acquireMutex method.
BLOCXX_COMMON_API int conditionPreWait(NonRecursiveMutex_t &handle, NonRecursiveMutexLockState &state)
BLOCXX_COMMON_API int createMutex(NonRecursiveMutex_t &handle)
Create a platform specific mutext handle.
Taken from RFC 1321.