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.
26 #include "pdnsexception.hh"
28 extern bool g_singleThreaded;
32 pthread_mutex_t *d_lock;
34 Lock(const Lock& rhs) = delete;
35 Lock& operator=(const Lock& rhs) = delete;
37 Lock(pthread_mutex_t *lock) : d_lock(lock)
43 if((err = pthread_mutex_lock(d_lock))) {
45 throw PDNSException("error acquiring lock: "+stringerror());
53 pthread_mutex_unlock(d_lock);
62 if (pthread_rwlock_init(&d_lock, nullptr) != 0) {
63 throw std::runtime_error("Error creating a read-write lock: " + stringerror());
68 /* might have been moved */
69 pthread_rwlock_destroy(&d_lock);
72 ReadWriteLock(const ReadWriteLock& rhs) = delete;
73 ReadWriteLock& operator=(const ReadWriteLock& rhs) = delete;
75 pthread_rwlock_t* getLock()
81 pthread_rwlock_t d_lock;
86 pthread_rwlock_t *d_lock;
89 WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
95 if((err = pthread_rwlock_wrlock(d_lock))) {
96 throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
103 if(d_lock) // might have been moved
104 pthread_rwlock_unlock(d_lock);
107 WriteLock(ReadWriteLock& lock): WriteLock(lock.getLock())
111 WriteLock(ReadWriteLock* lock): WriteLock(lock->getLock())
115 WriteLock(WriteLock&& rhs)
120 WriteLock(const WriteLock& rhs) = delete;
121 WriteLock& operator=(const WriteLock& rhs) = delete;
127 pthread_rwlock_t *d_lock;
130 TryWriteLock(const TryWriteLock& rhs) = delete;
131 TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
133 TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
135 if(g_singleThreaded) {
142 if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
143 throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
148 TryWriteLock(TryWriteLock&& rhs)
151 rhs.d_lock = nullptr;
152 d_havelock = rhs.d_havelock;
153 rhs.d_havelock = false;
156 TryWriteLock(ReadWriteLock& lock): TryWriteLock(lock.getLock())
160 TryWriteLock(ReadWriteLock* lock): TryWriteLock(lock->getLock())
169 if(d_havelock && d_lock) // we might be moved
170 pthread_rwlock_unlock(d_lock);
183 pthread_rwlock_t *d_lock;
186 TryReadLock(const TryReadLock& rhs) = delete;
187 TryReadLock& operator=(const TryReadLock& rhs) = delete;
189 TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
191 if(g_singleThreaded) {
197 if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
198 throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err));
203 TryReadLock(ReadWriteLock& lock): TryReadLock(lock.getLock())
207 TryReadLock(ReadWriteLock* lock): TryReadLock(lock->getLock())
211 TryReadLock(TryReadLock&& rhs)
214 rhs.d_lock = nullptr;
215 d_havelock = rhs.d_havelock;
216 rhs.d_havelock = false;
224 if(d_havelock && d_lock)
225 pthread_rwlock_unlock(d_lock);
239 pthread_rwlock_t *d_lock;
242 ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
248 if((err = pthread_rwlock_rdlock(d_lock))) {
249 throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
253 ReadLock(ReadWriteLock& lock): ReadLock(lock.getLock())
257 ReadLock(ReadWriteLock* lock): ReadLock(lock->getLock())
265 if(d_lock) // may have been moved
266 pthread_rwlock_unlock(d_lock);
269 ReadLock(ReadLock&& rhs)
272 rhs.d_lock = nullptr;
274 ReadLock(const ReadLock& rhs) = delete;
275 ReadLock& operator=(const ReadLock& rhs) = delete;