2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "pdnsexception.hh"
30 extern bool g_singleThreaded;
34 pthread_mutex_t *d_lock;
36 Lock(const Lock& rhs) = delete;
37 Lock& operator=(const Lock& rhs) = delete;
39 Lock(pthread_mutex_t *lock) : d_lock(lock)
45 if((err = pthread_mutex_lock(d_lock))) {
47 throw PDNSException("error acquiring lock: "+stringerror());
55 pthread_mutex_unlock(d_lock);
61 pthread_rwlock_t *d_lock;
64 WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
70 if((err = pthread_rwlock_wrlock(d_lock))) {
72 throw PDNSException("error acquiring rwlock wrlock: "+stringerror());
79 if(d_lock) // might have been moved
80 pthread_rwlock_unlock(d_lock);
83 WriteLock(WriteLock&& rhs)
88 WriteLock(const WriteLock& rhs) = delete;
89 WriteLock& operator=(const WriteLock& rhs) = delete;
96 pthread_rwlock_t *d_lock;
99 TryWriteLock(const TryWriteLock& rhs) = delete;
100 TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
102 TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
104 if(g_singleThreaded) {
111 if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
113 throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror());
118 TryWriteLock(TryWriteLock&& rhs)
121 rhs.d_lock = nullptr;
122 d_havelock = rhs.d_havelock;
123 rhs.d_havelock = false;
132 if(d_havelock && d_lock) // we might be moved
133 pthread_rwlock_unlock(d_lock);
146 pthread_rwlock_t *d_lock;
149 TryReadLock(const TryReadLock& rhs) = delete;
150 TryReadLock& operator=(const TryReadLock& rhs) = delete;
152 TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
154 if(g_singleThreaded) {
160 if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
162 throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror());
166 TryReadLock(TryReadLock&& rhs)
169 rhs.d_lock = nullptr;
170 d_havelock = rhs.d_havelock;
171 rhs.d_havelock = false;
179 if(d_havelock && d_lock)
180 pthread_rwlock_unlock(d_lock);
194 pthread_rwlock_t *d_lock;
197 ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
203 if((err = pthread_rwlock_rdlock(d_lock))) {
205 throw PDNSException("error acquiring rwlock readlock: "+stringerror());
212 if(d_lock) // may have been moved
213 pthread_rwlock_unlock(d_lock);
216 ReadLock(ReadLock&& rhs)
221 ReadLock(const ReadLock& rhs) = delete;
222 ReadLock& operator=(const ReadLock& rhs) = delete;