CARMA C++
PthreadRWLock.h
1 #ifndef CARMA_UTIL_PTHREAD_RW_LOCK_H
2 #define CARMA_UTIL_PTHREAD_RW_LOCK_H
3 
4 #include <pthread.h>
5 #include <cerrno>
6 
7 #include "carma/util/posixErrors.h"
8 
9 
10 namespace carma {
11 namespace util {
12 
13 
14 class PthreadRWLockAttr;
15 class PthreadRWLock;
16 
17 
18 void exclusiveLock( ::pthread_rwlock_t & );
19 void exclusiveLock( PthreadRWLock & );
20 
21 bool tryExclusiveLock( ::pthread_rwlock_t & );
22 bool tryExclusiveLock( PthreadRWLock & );
23 
24 void exclusiveUnlock( ::pthread_rwlock_t & );
25 void exclusiveUnlock( PthreadRWLock & );
26 
27 void sharedLock( ::pthread_rwlock_t & );
28 void sharedLock( PthreadRWLock & );
29 
30 bool trySharedLock( ::pthread_rwlock_t & );
31 bool trySharedLock( PthreadRWLock & );
32 
33 void sharedUnlock( ::pthread_rwlock_t & );
34 void sharedUnlock( PthreadRWLock & );
35 
36 
45 
47  public:
48 
54 
55  explicit PthreadRWLock( );
56 
57 
63 
64  explicit PthreadRWLock( const PthreadRWLockAttr & attr );
65 
66 
72 
73  explicit PthreadRWLock( const ::pthread_rwlockattr_t & attr );
74 
75 
83 
84  virtual ~PthreadRWLock( );
85 
86 
89 
90  void ExclusiveLock( );
91 
92 
95 
96  bool TryExclusiveLock( );
97 
98 
101 
102  void ExclusiveUnlock( );
103 
104 
107 
108  void SharedLock( );
109 
110 
113 
114  bool TrySharedLock( );
115 
116 
119 
120  void SharedUnlock( );
121 
122 
131 
132  const ::pthread_rwlock_t & InternalPthreadRWLock( ) const;
133 
134 
143 
144  ::pthread_rwlock_t & InternalPthreadRWLock( );
145 
146 
147  private:
148  // no copying
149  PthreadRWLock( const PthreadRWLock & rhs );
150  PthreadRWLock & operator=( const PthreadRWLock & rhs );
151 
152  ::pthread_rwlock_t rwlock_;
153 };
154 
155 
156 } // namespace carma::util
157 } // namespace carma
158 
159 
160 inline void
161 carma::util::exclusiveLock( ::pthread_rwlock_t & l )
162 {
163  failIfPosixError( ::pthread_rwlock_wrlock( &l ),
164  "::pthread_rwlock_wrlock error" );
165 }
166 
167 
168 inline bool
169 carma::util::tryExclusiveLock( ::pthread_rwlock_t & l )
170 {
171  bool result = false;
172 
173  const int posixRetVal = ::pthread_rwlock_trywrlock( &l );
174 
175  switch ( posixRetVal ) {
176  case 0:
177  result = true;
178  break;
179 
180  case EBUSY:
181  result = false;
182  break;
183 
184  default:
185  throwPosixError( posixRetVal, "::pthread_rwlock_trywrlock error" );
186  }
187 
188  return result;
189 }
190 
191 
192 inline void
193 carma::util::exclusiveUnlock( ::pthread_rwlock_t & l )
194 {
195  failIfPosixError( ::pthread_rwlock_unlock( &l ),
196  "::pthread_rwlock_unlock error" );
197 }
198 
199 
200 inline void
201 carma::util::sharedLock( ::pthread_rwlock_t & l )
202 {
203  failIfPosixError( ::pthread_rwlock_rdlock( &l ),
204  "::pthread_rwlock_rdlock error" );
205 }
206 
207 
208 inline bool
209 carma::util::trySharedLock( ::pthread_rwlock_t & l )
210 {
211  bool result = false;
212 
213  const int posixRetVal = ::pthread_rwlock_tryrdlock( &l );
214 
215  switch ( posixRetVal ) {
216  case 0:
217  result = true;
218  break;
219 
220  case EBUSY:
221  result = false;
222  break;
223 
224  default:
225  throwPosixError( posixRetVal, "::pthread_rwlock_tryrdlock error" );
226  }
227 
228  return result;
229 }
230 
231 
232 inline void
233 carma::util::sharedUnlock( ::pthread_rwlock_t & l )
234 {
235  failIfPosixError( ::pthread_rwlock_unlock( &l ),
236  "::pthread_rwlock_unlock error" );
237 }
238 
239 
240 inline void
242 {
243  exclusiveLock( rwlock_ );
244 }
245 
246 
247 inline bool
249 {
250  return tryExclusiveLock( rwlock_ );
251 }
252 
253 
254 inline void
256 {
257  exclusiveUnlock( rwlock_ );
258 }
259 
260 
261 inline void
263 {
264  sharedLock( rwlock_ );
265 }
266 
267 
268 inline bool
270 {
271  return trySharedLock( rwlock_ );
272 }
273 
274 
275 inline void
277 {
278  sharedUnlock( rwlock_ );
279 }
280 
281 
282 inline const ::pthread_rwlock_t &
284 {
285  return rwlock_;
286 }
287 
288 
289 inline ::pthread_rwlock_t &
291 {
292  return rwlock_;
293 }
294 
295 
296 inline void
297 carma::util::exclusiveLock( PthreadRWLock & l )
298 {
299  l.ExclusiveLock();
300 }
301 
302 
303 inline bool
304 carma::util::tryExclusiveLock( PthreadRWLock & l )
305 {
306  return l.TryExclusiveLock();
307 }
308 
309 
310 inline void
311 carma::util::exclusiveUnlock( PthreadRWLock & l )
312 {
313  l.ExclusiveUnlock();
314 }
315 
316 
317 inline void
318 carma::util::sharedLock( PthreadRWLock & l )
319 {
320  l.SharedLock();
321 }
322 
323 
324 inline bool
325 carma::util::trySharedLock( PthreadRWLock & l )
326 {
327  return l.TrySharedLock();
328 }
329 
330 
331 inline void
332 carma::util::sharedUnlock( PthreadRWLock & l )
333 {
334  l.SharedUnlock();
335 }
336 
337 
338 #endif
A simple wrapper class that makes use of ::pthread_rwlock_t easier in a C++ world.
Definition: PthreadRWLock.h:46
void SharedLock()
Obtain an shared lock on the instance for the caller&#39;s thread.
void ExclusiveLock()
Obtain an exclusive lock on the instance for the caller&#39;s thread.
virtual ~PthreadRWLock()
Destruct a read/write lock.
void SharedUnlock()
Release a shared lock on the instance for the caller&#39;s thread.
bool TryExclusiveLock()
Try to obtain an exclusive lock on the instance for the caller&#39;s thread.
const ::pthread_rwlock_t & InternalPthreadRWLock() const
Obtain a reference to the internal ::pthread_rwlock_t.
bool TrySharedLock()
Try to obtain a shared lock on the instance for the caller&#39;s thread.
PthreadRWLock()
Construct a read/write lock with the CARMA defaults.
A simple wrapper class that makes use of ::pthread_rwlockattr_t easier in a C++ world.
void ExclusiveUnlock()
Release an exclusive lock on the instance for the caller&#39;s thread.