KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
l_mt_mutexlock.h
Go to the documentation of this file.
1 
6 /*
7  * $Id: l_mt_mutexlock.h 17461 2014-09-05 21:08:33Z predoehl $
8  */
9 
10 #ifndef MUTEXLOCK_H_INCLUDED_IVILAB_2013AUG09
11 #define MUTEXLOCK_H_INCLUDED_IVILAB_2013AUG09
12 
13 #include <l_mt/l_mt_pthread.h>
14 #include <l_cpp/l_exception.h>
15 
16 namespace kjb
17 {
18 
30 class Mutex_lock;
31 
57 {
58  kjb_c::kjb_pthread_mutex_t mutex_;
59 
60  Pthread_mutex(const Pthread_mutex&); // uncopyable
61  Pthread_mutex& operator=(const Pthread_mutex&); // unassignable
62 
63  friend class Mutex_lock;
64 
65  void fp_abort(const char*);
66 
67 public:
69  Pthread_mutex(void (Pthread_mutex::* pmf)() = 0)
70  {
71 #ifndef KJB_HAVE_PTHREAD
72  KJB_THROW_2(Missing_dependency, "built without pthread");
73 #endif
74  ETX(kjb_c::kjb_pthread_mutex_init(&mutex_, 0));
75 
76  if (pmf)
77  {
78  (this ->* pmf)();
79  }
80  }
81 
83  virtual ~Pthread_mutex()
84  {
85  if (kjb_c::ERROR == kjb_c::kjb_pthread_mutex_destroy(&mutex_))
86  {
87  fp_abort(kjb::kjb_get_error().c_str());
88  }
89  }
90 
92  void lock()
93  {
94  ETX(kjb_c::kjb_pthread_mutex_lock(&mutex_));
95  }
96 
98  void unlock()
99  {
100  ETX(kjb_c::kjb_pthread_mutex_unlock(&mutex_));
101  }
102 
104  int try_lock()
105  {
106  return kjb_c::kjb_pthread_mutex_trylock(&mutex_);
107  }
108 };
109 
110 
113 {
115 };
116 
117 
134 {
135  kjb_c::kjb_pthread_mutex_t* mutex_;
136  Mutex_lock(const Mutex_lock&); // uncopyable
137  Mutex_lock& operator=(const Mutex_lock&); // unassignable
138 
139  void lock()
140  {
141  NTX(mutex_);
142  ETX(kjb_c::kjb_pthread_mutex_lock(mutex_));
143  }
144 
145  void fp_abort(const char*);
146 
147 public:
149  Mutex_lock(kjb_c::kjb_pthread_mutex_t* m)
150  : mutex_(m)
151  {
152  lock();
153  }
154 
157  : mutex_(& m.mutex_)
158  {
159  lock();
160  }
161 
164  : mutex_(m ? & m -> mutex_ : 0)
165  {
166  lock();
167  }
168 
170  void release()
171  {
172  if (mutex_)
173  {
174  if (kjb_c::ERROR == kjb_c::kjb_pthread_mutex_unlock(mutex_))
175  {
176  fp_abort("unlock");
177  }
178  mutex_ = 0;
179  }
180  }
181 
184  {
185  release();
186  }
187 };
188 
189 
191 
192 }
193 
194 
195 #endif
#define ETX(a)
Definition: l_exception.h:67
void lock()
lock this mutex
Definition: l_mt_mutexlock.h:92
Mutex_lock(Pthread_mutex *m)
establish a lock on the mutex object pointer
Definition: l_mt_mutexlock.h:163
Mutex_lock(kjb_c::kjb_pthread_mutex_t *m)
establish a lock on the mutex (as a C-style struct)
Definition: l_mt_mutexlock.h:149
Mutex_lock(Pthread_mutex &m)
establish a lock on the mutex object (as a reference)
Definition: l_mt_mutexlock.h:156
Pthread_locked_mutex()
Definition: l_mt_mutexlock.h:114
#define NTX(a)
Definition: l_exception.h:89
~Mutex_lock()
dtor releases the mutex
Definition: l_mt_mutexlock.h:183
simple RAII class to grab and release a mutex
Definition: l_mt_mutexlock.h:133
int try_lock()
try to lock; return WOULD_BLOCK if locked, else NO_ERROR.
Definition: l_mt_mutexlock.h:104
void release()
release the mutex for others (safe to call twice)
Definition: l_mt_mutexlock.h:170
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
virtual ~Pthread_mutex()
dtor cleans up the mutex, which MUST be unlocked beforehand.
Definition: l_mt_mutexlock.h:83
Pthread_mutex(void(Pthread_mutex::*pmf)()=0)
dynamic mutex: unlock before you destroy it!
Definition: l_mt_mutexlock.h:69
same as Pthread_mutex, but starts off in "locked" state.
Definition: l_mt_mutexlock.h:112
for m
Definition: APPgetLargeConnectedEdges.m:64
Support for error handling exception classes in libKJB.
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
dynamically allocated mutex: unlock before you destroy it!
Definition: l_mt_mutexlock.h:56
void unlock()
unlock this mutex
Definition: l_mt_mutexlock.h:98
std::string kjb_get_error()
similar to kjb_c::kjb_get_error(), but this returns std::string.
Definition: l_exception.cpp:723