LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Mutex.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
33 #ifndef LSST_AP_MUTEX_H
34 #define LSST_AP_MUTEX_H
35 
36 #include <pthread.h>
37 #include <errno.h>
38 
39 #include <cassert>
40 
41 #include "boost/format.hpp"
42 #include "boost/noncopyable.hpp"
43 
44 #include "lsst/pex/exceptions.h"
45 
46 #include "Common.h"
47 
48 
49 namespace lsst { namespace ap {
50 
51 template <typename MutexT> class ScopedLock;
52 template <typename MutexT> class Condition;
53 
54 
56 class Mutex : private boost::noncopyable {
57 
58 public :
59 
60  Mutex() {
61  int err = ::pthread_mutex_init(&_mutex, 0);
62  if (err != 0) {
63  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
64  (boost::format("pthread_mutex_init() failed, return code: %1%") % err).str());
65  }
66  }
67 
68  ~Mutex() {
69  int result = ::pthread_mutex_destroy(&_mutex);
70  assert(result == 0);
71  }
72 
73 private :
74 
75  ::pthread_mutex_t _mutex;
76 
77  void acquire() {
78  int result = ::pthread_mutex_lock(&_mutex);
79  assert(result == 0);
80  }
81 
82  bool tryAcquire() {
83  return ::pthread_mutex_trylock(&_mutex) == 0;
84  }
85 
86  void release() {
87  int result = ::pthread_mutex_unlock(&_mutex);
88  assert(result == 0);
89  }
90 
91  friend class ScopedLock<Mutex>;
92  friend class Condition<Mutex>;
93 };
94 
95 
97 class SharedMutex : private boost::noncopyable {
98 
99 public :
100 
101  SharedMutex();
102 
104  int result = ::pthread_mutex_destroy(&_mutex);
105  assert(result == 0);
106  }
107 
108 private :
109 
110  ::pthread_mutex_t _mutex;
111 
112  void acquire() {
113  int result = ::pthread_mutex_lock(&_mutex);
114  assert(result == 0);
115  }
116 
117  bool tryAcquire() {
118  return ::pthread_mutex_trylock(&_mutex) == 0;
119  }
120 
121  void release() {
122  int result = ::pthread_mutex_unlock(&_mutex);
123  assert(result == 0);
124  }
125 
126  friend class ScopedLock<SharedMutex>;
127  friend class Condition<SharedMutex>;
128 };
129 
130 
132 template <typename MutexT>
133 class ScopedLock : private boost::noncopyable {
134 
135 public :
136 
137  ScopedLock() : _mutex(0) {}
138 
139  explicit ScopedLock(MutexT & m) : _mutex(&m) {
140  m.acquire();
141  }
142 
144  if (_mutex != 0) {
145  _mutex->release();
146  _mutex = 0;
147  }
148  }
149 
151  void acquire(MutexT & m) {
152  assert(_mutex == 0);
153  _mutex = &m;
154  m.acquire();
155  }
156 
163  bool tryAcquire(MutexT & m) {
164  assert(_mutex == 0);
165  if (m.tryAcquire()) {
166  _mutex = &m;
167  return true;
168  }
169  return false;
170  }
171 
177  void release() {
178  assert(_mutex != 0);
179  _mutex->release();
180  _mutex = 0;
181  }
182 
187  bool isAcquired() const {
188  return _mutex != 0;
189  }
190 
191  // implicit conversion to "bool"
192  typedef MutexT * ScopedLock::* UnspecifiedBool;
193 
194  operator UnspecifiedBool() const {
195  return _mutex == 0 ? 0 : &ScopedLock::_mutex;
196  }
197 
198  bool operator!() const {
199  return _mutex == 0;
200  }
201 
202 private :
203 
204  MutexT * _mutex;
205 
206  ::pthread_mutex_t * getPosixMutex() {
207  return &(_mutex->_mutex);
208  }
209 
210  friend class Condition<MutexT>;
211 };
212 
213 
214 }} // end of namespace lsst::ap
215 
216 #endif // LSST_AP_MUTEX_H
bool isAcquired() const
Definition: Mutex.h:187
Encapsulates a POSIX condition variable.
Definition: Condition.h:50
::pthread_mutex_t * getPosixMutex()
Definition: Mutex.h:206
::pthread_mutex_t _mutex
Definition: Mutex.h:110
::pthread_mutex_t _mutex
Definition: Mutex.h:75
bool tryAcquire(MutexT &m)
Definition: Mutex.h:163
A wrapper for a POSIX process shared mutual exclusion lock.
Definition: Mutex.h:97
ScopedLock(MutexT &m)
Definition: Mutex.h:139
MutexT * _mutex
Definition: Mutex.h:204
MutexT *ScopedLock::* UnspecifiedBool
Definition: Mutex.h:192
Master header file for the association pipeline.
bool operator!() const
Definition: Mutex.h:198
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A wrapper for a process private POSIX mutual exclusion lock.
Definition: Mutex.h:56
void release()
Definition: Mutex.h:86
Grants access to a mutex, enforcing the RAII principle.
Definition: Mutex.h:51
bool tryAcquire()
Definition: Mutex.h:82
Include files required for standard LSST Exception handling.
void acquire()
Definition: Mutex.h:77
void acquire(MutexT &m)
Acquires the given Mutex.
Definition: Mutex.h:151