CARMA C++
ScopedSharedLockManager.h
1 #ifndef CARMA_UTIL_SCOPEDSHAREDLOCKMANAGER_H
2 #define CARMA_UTIL_SCOPEDSHAREDLOCKMANAGER_H
3 
4 #include "carma/util/checking.h"
7 
8 namespace carma {
9 namespace util {
10 
24 template < typename L >
26 public:
27 
28  explicit ScopedSharedLockManager( L & l );
29 
30  /* virtual */ ~ScopedSharedLockManager( );
31 
32  void lock( );
33 
34  bool tryLock( );
35 
36  void unlock( );
37 
38 private:
39 
40  // prevent copying and assignment
42  ScopedSharedLockManager & operator=( const ScopedSharedLockManager & );
43 
44  bool locked_;
45  L & lock_;
46 };
47 
48 } // namespace util
49 } // namespace carma
50 
51 template< typename L >
52 inline
54  locked_( false ),
55  lock_( l )
56 {
57  // Nothing
58 }
59 
60 template< typename L >
61 inline
63 {
64  try {
65  if ( locked_ ) {
66  sharedUnlock( lock_ );
67  locked_ = false;
68  }
69  } catch ( const carma::util::ErrorException & err ) {
70  programLogErrorIfPossible( err.getLogString( ) ); // No throw
71  } catch (...) {
72  programLogErrorIfPossible( "~ScopedSharedLockManger() - Unknown" );
73  }
74 }
75 
76 template< typename L >
77 inline void
79 {
80  CARMA_CHECK( locked_ == false );
81 
82  sharedLock( lock_ );
83 
84  locked_ = true;
85 }
86 
87 template< typename L >
88 inline bool
90 {
91  CARMA_CHECK( locked_ == false );
92 
93  const bool result = trySharedLock( lock_ );
94 
95  if ( result )
96  locked_ = true;
97 
98  return result;
99 }
100 
101 template< typename L >
102 inline void
104 {
105  CARMA_CHECK( locked_ == true );
106 
107  sharedUnlock( lock_ );
108 
109  locked_ = false;
110 }
111 
112 #endif
Scoped manager class for shared PthreadRWLock locks.
Exception class for errors.
Exception class for errors The exception comes with a text string that can be printed or logged...
Header file for the CARMA checked build diagnostic macros.
#define CARMA_CHECK(assertion)
Diagnostic macro for checking an assertion in checked builds.
Definition: checking.h:52
This is the interface file for extra APIs for program logging.