]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lock.hh
Merge pull request #9134 from omoerbeek/secpoll-cleanup
[thirdparty/pdns.git] / pdns / lock.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
12 *
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.
17 *
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.
21 */
22 #pragma once
23 #include <pthread.h>
24 #include <errno.h>
25 #include "misc.hh"
26 #include "pdnsexception.hh"
27
28 class ReadWriteLock
29 {
30 public:
31 ReadWriteLock()
32 {
33 if (pthread_rwlock_init(&d_lock, nullptr) != 0) {
34 throw std::runtime_error("Error creating a read-write lock: " + stringerror());
35 }
36 }
37
38 ~ReadWriteLock() {
39 /* might have been moved */
40 pthread_rwlock_destroy(&d_lock);
41 }
42
43 ReadWriteLock(const ReadWriteLock& rhs) = delete;
44 ReadWriteLock& operator=(const ReadWriteLock& rhs) = delete;
45
46 pthread_rwlock_t* getLock()
47 {
48 return &d_lock;
49 }
50
51 private:
52 pthread_rwlock_t d_lock;
53 };
54
55 class ReadLock
56 {
57 public:
58 ReadLock(ReadWriteLock& lock): ReadLock(lock.getLock())
59 {
60 }
61
62 ReadLock(ReadWriteLock* lock): ReadLock(lock->getLock())
63 {
64 }
65
66 ~ReadLock()
67 {
68 if(d_lock) // may have been moved
69 pthread_rwlock_unlock(d_lock);
70 }
71
72 ReadLock(ReadLock&& rhs)
73 {
74 d_lock = rhs.d_lock;
75 rhs.d_lock = nullptr;
76 }
77 ReadLock(const ReadLock& rhs) = delete;
78 ReadLock& operator=(const ReadLock& rhs) = delete;
79
80 private:
81 ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
82 {
83 int err;
84 if((err = pthread_rwlock_rdlock(d_lock))) {
85 throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
86 }
87 }
88
89 pthread_rwlock_t *d_lock;
90 };
91
92 class WriteLock
93 {
94 public:
95 WriteLock(ReadWriteLock& lock): WriteLock(lock.getLock())
96 {
97 }
98
99 WriteLock(ReadWriteLock* lock): WriteLock(lock->getLock())
100 {
101 }
102
103 WriteLock(WriteLock&& rhs)
104 {
105 d_lock = rhs.d_lock;
106 rhs.d_lock=0;
107 }
108
109 ~WriteLock()
110 {
111 if(d_lock) // might have been moved
112 pthread_rwlock_unlock(d_lock);
113 }
114
115 WriteLock(const WriteLock& rhs) = delete;
116 WriteLock& operator=(const WriteLock& rhs) = delete;
117
118 private:
119 WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
120 {
121 int err;
122 if((err = pthread_rwlock_wrlock(d_lock))) {
123 throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
124 }
125 }
126
127 pthread_rwlock_t *d_lock;
128 };
129
130 class TryReadLock
131 {
132 public:
133 TryReadLock(ReadWriteLock& lock): TryReadLock(lock.getLock())
134 {
135 }
136
137 TryReadLock(ReadWriteLock* lock): TryReadLock(lock->getLock())
138 {
139 }
140
141 TryReadLock(TryReadLock&& rhs)
142 {
143 d_lock = rhs.d_lock;
144 rhs.d_lock = nullptr;
145 d_havelock = rhs.d_havelock;
146 rhs.d_havelock = false;
147 }
148
149 ~TryReadLock()
150 {
151 if(d_havelock && d_lock)
152 pthread_rwlock_unlock(d_lock);
153 }
154
155 TryReadLock(const TryReadLock& rhs) = delete;
156 TryReadLock& operator=(const TryReadLock& rhs) = delete;
157
158 bool gotIt()
159 {
160 return d_havelock;
161 }
162
163 private:
164 TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
165 {
166 int err;
167 if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
168 throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err));
169 }
170 d_havelock=(err==0);
171 }
172
173 pthread_rwlock_t *d_lock;
174 bool d_havelock;
175 };
176
177 class TryWriteLock
178 {
179 public:
180 TryWriteLock(ReadWriteLock& lock): TryWriteLock(lock.getLock())
181 {
182 }
183
184 TryWriteLock(ReadWriteLock* lock): TryWriteLock(lock->getLock())
185 {
186 }
187
188 TryWriteLock(TryWriteLock&& rhs)
189 {
190 d_lock = rhs.d_lock;
191 rhs.d_lock = nullptr;
192 d_havelock = rhs.d_havelock;
193 rhs.d_havelock = false;
194 }
195
196 ~TryWriteLock()
197 {
198 if(d_havelock && d_lock) // we might be moved
199 pthread_rwlock_unlock(d_lock);
200 }
201
202 TryWriteLock(const TryWriteLock& rhs) = delete;
203 TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
204
205 bool gotIt()
206 {
207 return d_havelock;
208 }
209
210 private:
211 TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
212 {
213 d_havelock=false;
214 int err;
215 if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
216 throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
217 }
218 d_havelock=(err==0);
219 }
220
221 pthread_rwlock_t *d_lock;
222 bool d_havelock;
223 };
224