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"
33 if (pthread_rwlock_init(&d_lock, nullptr) != 0) {
34 throw std::runtime_error("Error creating a read-write lock: " + stringerror());
39 /* might have been moved */
40 pthread_rwlock_destroy(&d_lock);
43 ReadWriteLock(const ReadWriteLock& rhs) = delete;
44 ReadWriteLock& operator=(const ReadWriteLock& rhs) = delete;
46 pthread_rwlock_t* getLock()
52 pthread_rwlock_t d_lock;
58 ReadLock(ReadWriteLock& lock): ReadLock(lock.getLock())
62 ReadLock(ReadWriteLock* lock): ReadLock(lock->getLock())
68 if(d_lock) // may have been moved
69 pthread_rwlock_unlock(d_lock);
72 ReadLock(ReadLock&& rhs)
77 ReadLock(const ReadLock& rhs) = delete;
78 ReadLock& operator=(const ReadLock& rhs) = delete;
81 ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
84 if((err = pthread_rwlock_rdlock(d_lock))) {
85 throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
89 pthread_rwlock_t *d_lock;
95 WriteLock(ReadWriteLock& lock): WriteLock(lock.getLock())
99 WriteLock(ReadWriteLock* lock): WriteLock(lock->getLock())
103 WriteLock(WriteLock&& rhs)
111 if(d_lock) // might have been moved
112 pthread_rwlock_unlock(d_lock);
115 WriteLock(const WriteLock& rhs) = delete;
116 WriteLock& operator=(const WriteLock& rhs) = delete;
119 WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
122 if((err = pthread_rwlock_wrlock(d_lock))) {
123 throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
127 pthread_rwlock_t *d_lock;
133 TryReadLock(ReadWriteLock& lock): TryReadLock(lock.getLock())
137 TryReadLock(ReadWriteLock* lock): TryReadLock(lock->getLock())
141 TryReadLock(TryReadLock&& rhs)
144 rhs.d_lock = nullptr;
145 d_havelock = rhs.d_havelock;
146 rhs.d_havelock = false;
151 if(d_havelock && d_lock)
152 pthread_rwlock_unlock(d_lock);
155 TryReadLock(const TryReadLock& rhs) = delete;
156 TryReadLock& operator=(const TryReadLock& rhs) = delete;
164 TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
167 if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
168 throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err));
173 pthread_rwlock_t *d_lock;
180 TryWriteLock(ReadWriteLock& lock): TryWriteLock(lock.getLock())
184 TryWriteLock(ReadWriteLock* lock): TryWriteLock(lock->getLock())
188 TryWriteLock(TryWriteLock&& rhs)
191 rhs.d_lock = nullptr;
192 d_havelock = rhs.d_havelock;
193 rhs.d_havelock = false;
198 if(d_havelock && d_lock) // we might be moved
199 pthread_rwlock_unlock(d_lock);
202 TryWriteLock(const TryWriteLock& rhs) = delete;
203 TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
211 TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
215 if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
216 throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
221 pthread_rwlock_t *d_lock;