]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lock.hh
e3a1e1b57a373b4150f99199dd19b110f4097178
[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 WriteLock
56 {
57 pthread_rwlock_t *d_lock;
58 public:
59
60 WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
61 {
62 int err;
63 if((err = pthread_rwlock_wrlock(d_lock))) {
64 throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
65 }
66 }
67 ~WriteLock()
68 {
69 if(d_lock) // might have been moved
70 pthread_rwlock_unlock(d_lock);
71 }
72
73 WriteLock(ReadWriteLock& lock): WriteLock(lock.getLock())
74 {
75 }
76
77 WriteLock(ReadWriteLock* lock): WriteLock(lock->getLock())
78 {
79 }
80
81 WriteLock(WriteLock&& rhs)
82 {
83 d_lock = rhs.d_lock;
84 rhs.d_lock=0;
85 }
86 WriteLock(const WriteLock& rhs) = delete;
87 WriteLock& operator=(const WriteLock& rhs) = delete;
88
89 };
90
91 class TryWriteLock
92 {
93 pthread_rwlock_t *d_lock;
94 bool d_havelock;
95 public:
96 TryWriteLock(const TryWriteLock& rhs) = delete;
97 TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
98
99 TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
100 {
101 d_havelock=false;
102 int err;
103 if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
104 throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
105 }
106 d_havelock=(err==0);
107 }
108
109 TryWriteLock(TryWriteLock&& rhs)
110 {
111 d_lock = rhs.d_lock;
112 rhs.d_lock = nullptr;
113 d_havelock = rhs.d_havelock;
114 rhs.d_havelock = false;
115 }
116
117 TryWriteLock(ReadWriteLock& lock): TryWriteLock(lock.getLock())
118 {
119 }
120
121 TryWriteLock(ReadWriteLock* lock): TryWriteLock(lock->getLock())
122 {
123 }
124
125 ~TryWriteLock()
126 {
127 if(d_havelock && d_lock) // we might be moved
128 pthread_rwlock_unlock(d_lock);
129 }
130 bool gotIt()
131 {
132 return d_havelock;
133 }
134 };
135
136 class TryReadLock
137 {
138 pthread_rwlock_t *d_lock;
139 bool d_havelock;
140 public:
141 TryReadLock(const TryReadLock& rhs) = delete;
142 TryReadLock& operator=(const TryReadLock& rhs) = delete;
143
144 TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
145 {
146 int err;
147 if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
148 throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err));
149 }
150 d_havelock=(err==0);
151 }
152
153 TryReadLock(ReadWriteLock& lock): TryReadLock(lock.getLock())
154 {
155 }
156
157 TryReadLock(ReadWriteLock* lock): TryReadLock(lock->getLock())
158 {
159 }
160
161 TryReadLock(TryReadLock&& rhs)
162 {
163 d_lock = rhs.d_lock;
164 rhs.d_lock = nullptr;
165 d_havelock = rhs.d_havelock;
166 rhs.d_havelock = false;
167 }
168
169 ~TryReadLock()
170 {
171 if(d_havelock && d_lock)
172 pthread_rwlock_unlock(d_lock);
173 }
174 bool gotIt()
175 {
176 return d_havelock;
177 }
178 };
179
180
181 class ReadLock
182 {
183 pthread_rwlock_t *d_lock;
184 public:
185
186 ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
187 {
188 int err;
189 if((err = pthread_rwlock_rdlock(d_lock))) {
190 throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
191 }
192 }
193
194 ReadLock(ReadWriteLock& lock): ReadLock(lock.getLock())
195 {
196 }
197
198 ReadLock(ReadWriteLock* lock): ReadLock(lock->getLock())
199 {
200 }
201
202 ~ReadLock()
203 {
204 if(d_lock) // may have been moved
205 pthread_rwlock_unlock(d_lock);
206 }
207
208 ReadLock(ReadLock&& rhs)
209 {
210 d_lock = rhs.d_lock;
211 rhs.d_lock = nullptr;
212 }
213 ReadLock(const ReadLock& rhs) = delete;
214 ReadLock& operator=(const ReadLock& rhs) = delete;
215 };