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;
57 pthread_rwlock_t *d_lock;
60 WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
63 if((err = pthread_rwlock_wrlock(d_lock))) {
64 throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
69 if(d_lock) // might have been moved
70 pthread_rwlock_unlock(d_lock);
73 WriteLock(ReadWriteLock& lock): WriteLock(lock.getLock())
77 WriteLock(ReadWriteLock* lock): WriteLock(lock->getLock())
81 WriteLock(WriteLock&& rhs)
86 WriteLock(const WriteLock& rhs) = delete;
87 WriteLock& operator=(const WriteLock& rhs) = delete;
93 pthread_rwlock_t *d_lock;
96 TryWriteLock(const TryWriteLock& rhs) = delete;
97 TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
99 TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
103 if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
104 throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
109 TryWriteLock(TryWriteLock&& rhs)
112 rhs.d_lock = nullptr;
113 d_havelock = rhs.d_havelock;
114 rhs.d_havelock = false;
117 TryWriteLock(ReadWriteLock& lock): TryWriteLock(lock.getLock())
121 TryWriteLock(ReadWriteLock* lock): TryWriteLock(lock->getLock())
127 if(d_havelock && d_lock) // we might be moved
128 pthread_rwlock_unlock(d_lock);
138 pthread_rwlock_t *d_lock;
141 TryReadLock(const TryReadLock& rhs) = delete;
142 TryReadLock& operator=(const TryReadLock& rhs) = delete;
144 TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
147 if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
148 throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err));
153 TryReadLock(ReadWriteLock& lock): TryReadLock(lock.getLock())
157 TryReadLock(ReadWriteLock* lock): TryReadLock(lock->getLock())
161 TryReadLock(TryReadLock&& rhs)
164 rhs.d_lock = nullptr;
165 d_havelock = rhs.d_havelock;
166 rhs.d_havelock = false;
171 if(d_havelock && d_lock)
172 pthread_rwlock_unlock(d_lock);
183 pthread_rwlock_t *d_lock;
186 ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
189 if((err = pthread_rwlock_rdlock(d_lock))) {
190 throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
194 ReadLock(ReadWriteLock& lock): ReadLock(lock.getLock())
198 ReadLock(ReadWriteLock* lock): ReadLock(lock->getLock())
204 if(d_lock) // may have been moved
205 pthread_rwlock_unlock(d_lock);
208 ReadLock(ReadLock&& rhs)
211 rhs.d_lock = nullptr;
213 ReadLock(const ReadLock& rhs) = delete;
214 ReadLock& operator=(const ReadLock& rhs) = delete;