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 
39 #include "blocxx/BLOCXX_config.h"
41 #include <cerrno>
42 #include <cassert>
43 
44 namespace BLOCXX_NAMESPACE
45 {
46 
47 namespace NonRecursiveMutexImpl
48 {
49 
50 #if defined (BLOCXX_USE_PTHREAD)
51 
52 #if !defined (BLOCXX_NCR)
53 
58 int
59 createMutex(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)
80 int
81 createMutex(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 
111 int
112 destroyMutex(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 }
134 int
135 acquireMutex(NonRecursiveMutex_t& handle)
136 {
137  int res = pthread_mutex_lock(&handle.mutex);
138  assert(res == 0);
139  return res;
140 }
147 int
148 releaseMutex(NonRecursiveMutex_t& handle)
149 {
150  int res = pthread_mutex_unlock(&handle.mutex);
151  assert(res == 0);
152  return res;
153 }
154 
155 int
156 conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
157 {
158  state.pmutex = &handle.mutex;
159  return 0;
160 }
161 
162 int
163 conditionPostWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
164 {
165  return 0;
166 }
167 
168 #endif //#if defined (BLOCXX_USE_PTHREAD)
169 
170 #if defined(BLOCXX_WIN32)
171 int
172 createMutex(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 
182 int
183 destroyMutex(NonRecursiveMutex_t& handle)
184 {
185  ReleaseMutex(handle);
186  return (CloseHandle(handle) == 0) ? -2 : 0;
187 }
188 
189 int
190 acquireMutex(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 
200 int
201 releaseMutex(NonRecursiveMutex_t& handle)
202 {
203  return (ReleaseMutex(handle)) ? 0 : -1;
204 }
205 
206 int
207 conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
208 {
209  state.pmutex = &handle;
210  return 0;
211 }
212 
213 int
214 conditionPostWait(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