]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lock.hh
dnsdist: Wrap pthread_ objects
[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 extern bool g_singleThreaded;
29
30 class Lock
31 {
32 pthread_mutex_t *d_lock;
33 public:
34 Lock(const Lock& rhs) = delete;
35 Lock& operator=(const Lock& rhs) = delete;
36
37 Lock(pthread_mutex_t *lock) : d_lock(lock)
38 {
39 if(g_singleThreaded)
40 return;
41
42 int err;
43 if((err = pthread_mutex_lock(d_lock))) {
44 errno = err;
45 throw PDNSException("error acquiring lock: "+stringerror());
46 }
47 }
48 ~Lock()
49 {
50 if(g_singleThreaded)
51 return;
52
53 pthread_mutex_unlock(d_lock);
54 }
55 };
56
57 class ReadWriteLock
58 {
59 public:
60 ReadWriteLock()
61 {
62 if (pthread_rwlock_init(&d_lock, nullptr) != 0) {
63 throw std::runtime_error("Error creating a read-write lock: " + stringerror());
64 }
65 }
66
67 ~ReadWriteLock() {
68 /* might have been moved */
69 pthread_rwlock_destroy(&d_lock);
70 }
71
72 ReadWriteLock(const ReadWriteLock& rhs) = delete;
73 ReadWriteLock& operator=(const ReadWriteLock& rhs) = delete;
74
75 pthread_rwlock_t* getLock()
76 {
77 return &d_lock;
78 }
79
80 private:
81 pthread_rwlock_t d_lock;
82 };
83
84 class WriteLock
85 {
86 pthread_rwlock_t *d_lock;
87 public:
88
89 WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
90 {
91 if(g_singleThreaded)
92 return;
93
94 int err;
95 if((err = pthread_rwlock_wrlock(d_lock))) {
96 throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
97 }
98 }
99 ~WriteLock()
100 {
101 if(g_singleThreaded)
102 return;
103 if(d_lock) // might have been moved
104 pthread_rwlock_unlock(d_lock);
105 }
106
107 WriteLock(ReadWriteLock& lock): WriteLock(lock.getLock())
108 {
109 }
110
111 WriteLock(ReadWriteLock* lock): WriteLock(lock->getLock())
112 {
113 }
114
115 WriteLock(WriteLock&& rhs)
116 {
117 d_lock = rhs.d_lock;
118 rhs.d_lock=0;
119 }
120 WriteLock(const WriteLock& rhs) = delete;
121 WriteLock& operator=(const WriteLock& rhs) = delete;
122
123 };
124
125 class TryWriteLock
126 {
127 pthread_rwlock_t *d_lock;
128 bool d_havelock;
129 public:
130 TryWriteLock(const TryWriteLock& rhs) = delete;
131 TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
132
133 TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
134 {
135 if(g_singleThreaded) {
136 d_havelock=true;
137 return;
138 }
139
140 d_havelock=false;
141 int err;
142 if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
143 throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
144 }
145 d_havelock=(err==0);
146 }
147
148 TryWriteLock(TryWriteLock&& rhs)
149 {
150 d_lock = rhs.d_lock;
151 rhs.d_lock = nullptr;
152 d_havelock = rhs.d_havelock;
153 rhs.d_havelock = false;
154 }
155
156 TryWriteLock(ReadWriteLock& lock): TryWriteLock(lock.getLock())
157 {
158 }
159
160 TryWriteLock(ReadWriteLock* lock): TryWriteLock(lock->getLock())
161 {
162 }
163
164 ~TryWriteLock()
165 {
166 if(g_singleThreaded)
167 return;
168
169 if(d_havelock && d_lock) // we might be moved
170 pthread_rwlock_unlock(d_lock);
171 }
172 bool gotIt()
173 {
174 if(g_singleThreaded)
175 return true;
176
177 return d_havelock;
178 }
179 };
180
181 class TryReadLock
182 {
183 pthread_rwlock_t *d_lock;
184 bool d_havelock;
185 public:
186 TryReadLock(const TryReadLock& rhs) = delete;
187 TryReadLock& operator=(const TryReadLock& rhs) = delete;
188
189 TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
190 {
191 if(g_singleThreaded) {
192 d_havelock=true;
193 return;
194 }
195
196 int err;
197 if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
198 throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err));
199 }
200 d_havelock=(err==0);
201 }
202
203 TryReadLock(ReadWriteLock& lock): TryReadLock(lock.getLock())
204 {
205 }
206
207 TryReadLock(ReadWriteLock* lock): TryReadLock(lock->getLock())
208 {
209 }
210
211 TryReadLock(TryReadLock&& rhs)
212 {
213 d_lock = rhs.d_lock;
214 rhs.d_lock = nullptr;
215 d_havelock = rhs.d_havelock;
216 rhs.d_havelock = false;
217 }
218
219 ~TryReadLock()
220 {
221 if(g_singleThreaded)
222 return;
223
224 if(d_havelock && d_lock)
225 pthread_rwlock_unlock(d_lock);
226 }
227 bool gotIt()
228 {
229 if(g_singleThreaded)
230 return true;
231
232 return d_havelock;
233 }
234 };
235
236
237 class ReadLock
238 {
239 pthread_rwlock_t *d_lock;
240 public:
241
242 ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
243 {
244 if(g_singleThreaded)
245 return;
246
247 int err;
248 if((err = pthread_rwlock_rdlock(d_lock))) {
249 throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
250 }
251 }
252
253 ReadLock(ReadWriteLock& lock): ReadLock(lock.getLock())
254 {
255 }
256
257 ReadLock(ReadWriteLock* lock): ReadLock(lock->getLock())
258 {
259 }
260
261 ~ReadLock()
262 {
263 if(g_singleThreaded)
264 return;
265 if(d_lock) // may have been moved
266 pthread_rwlock_unlock(d_lock);
267 }
268
269 ReadLock(ReadLock&& rhs)
270 {
271 d_lock = rhs.d_lock;
272 rhs.d_lock = nullptr;
273 }
274 ReadLock(const ReadLock& rhs) = delete;
275 ReadLock& operator=(const ReadLock& rhs) = delete;
276 };