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