LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
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
Include files required for standard LSST Exception handling.
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
void acquire()
Definition: Mutex.h:77
void acquire(MutexT &m)
Acquires the given Mutex.
Definition: Mutex.h:151