]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lock.hh
auth: switch circleci mssql image
[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 = nullptr;
122 d_havelock = rhs.d_havelock;
123 rhs.d_havelock = false;
124 }
125
126
127 ~TryWriteLock()
128 {
129 if(g_singleThreaded)
130 return;
131
132 if(d_havelock && d_lock) // we might be moved
133 pthread_rwlock_unlock(d_lock);
134 }
135 bool gotIt()
136 {
137 if(g_singleThreaded)
138 return true;
139
140 return d_havelock;
141 }
142 };
143
144 class TryReadLock
145 {
146 pthread_rwlock_t *d_lock;
147 bool d_havelock;
148 public:
149 TryReadLock(const TryReadLock& rhs) = delete;
150 TryReadLock& operator=(const TryReadLock& rhs) = delete;
151
152 TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
153 {
154 if(g_singleThreaded) {
155 d_havelock=true;
156 return;
157 }
158
159 int err;
160 if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
161 errno = err;
162 throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror());
163 }
164 d_havelock=(err==0);
165 }
166 TryReadLock(TryReadLock&& rhs)
167 {
168 d_lock = rhs.d_lock;
169 rhs.d_lock = nullptr;
170 d_havelock = rhs.d_havelock;
171 rhs.d_havelock = false;
172 }
173
174 ~TryReadLock()
175 {
176 if(g_singleThreaded)
177 return;
178
179 if(d_havelock && d_lock)
180 pthread_rwlock_unlock(d_lock);
181 }
182 bool gotIt()
183 {
184 if(g_singleThreaded)
185 return true;
186
187 return d_havelock;
188 }
189 };
190
191
192 class ReadLock
193 {
194 pthread_rwlock_t *d_lock;
195 public:
196
197 ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
198 {
199 if(g_singleThreaded)
200 return;
201
202 int err;
203 if((err = pthread_rwlock_rdlock(d_lock))) {
204 errno = err;
205 throw PDNSException("error acquiring rwlock readlock: "+stringerror());
206 }
207 }
208 ~ReadLock()
209 {
210 if(g_singleThreaded)
211 return;
212 if(d_lock) // may have been moved
213 pthread_rwlock_unlock(d_lock);
214 }
215
216 ReadLock(ReadLock&& rhs)
217 {
218 d_lock = rhs.d_lock;
219 rhs.d_lock=0;
220 }
221 ReadLock(const ReadLock& rhs) = delete;
222 ReadLock& operator=(const ReadLock& rhs) = delete;
223 };
224 #endif