]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lock.hh
Merge pull request #4305 from rgacogne/dnsdist-lua-anon
[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
37 Lock(pthread_mutex_t *lock) : d_lock(lock)
38 {
39 if(g_singleThreaded)
40 return;
41 if((errno=pthread_mutex_lock(d_lock)))
42 throw PDNSException("error acquiring lock: "+stringerror());
43 }
44 ~Lock()
45 {
46 if(g_singleThreaded)
47 return;
48
49 pthread_mutex_unlock(d_lock);
50 }
51 };
52
53 class WriteLock
54 {
55 pthread_rwlock_t *d_lock;
56 public:
57
58 WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
59 {
60 if(g_singleThreaded)
61 return;
62
63 if((errno=pthread_rwlock_wrlock(d_lock))) {
64 throw PDNSException("error acquiring rwlock wrlock: "+stringerror());
65 }
66 }
67 ~WriteLock()
68 {
69 if(g_singleThreaded)
70 return;
71
72 pthread_rwlock_unlock(d_lock);
73 }
74 };
75
76 class TryWriteLock
77 {
78 pthread_rwlock_t *d_lock;
79 bool d_havelock;
80 public:
81
82 TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
83 {
84 if(g_singleThreaded) {
85 d_havelock=true;
86 return;
87 }
88
89 d_havelock=false;
90 if((errno=pthread_rwlock_trywrlock(d_lock)) && errno!=EBUSY)
91 throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror());
92 d_havelock=(errno==0);
93 }
94 ~TryWriteLock()
95 {
96 if(g_singleThreaded)
97 return;
98
99 if(d_havelock)
100 pthread_rwlock_unlock(d_lock);
101 }
102 bool gotIt()
103 {
104 if(g_singleThreaded)
105 return true;
106
107 return d_havelock;
108 }
109 };
110
111 class TryReadLock
112 {
113 pthread_rwlock_t *d_lock;
114 bool d_havelock;
115 public:
116
117 TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
118 {
119 if(g_singleThreaded) {
120 d_havelock=true;
121 return;
122 }
123
124 if((errno=pthread_rwlock_tryrdlock(d_lock)) && errno!=EBUSY)
125 throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror());
126 d_havelock=(errno==0);
127 }
128 ~TryReadLock()
129 {
130 if(g_singleThreaded)
131 return;
132
133 if(d_havelock)
134 pthread_rwlock_unlock(d_lock);
135 }
136 bool gotIt()
137 {
138 if(g_singleThreaded)
139 return true;
140
141 return d_havelock;
142 }
143 };
144
145
146 class ReadLock
147 {
148 pthread_rwlock_t *d_lock;
149 public:
150
151 ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
152 {
153 if(g_singleThreaded)
154 return;
155
156 if((errno=pthread_rwlock_rdlock(d_lock)))
157 throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror());
158 }
159 ~ReadLock()
160 {
161 if(g_singleThreaded)
162 return;
163
164 pthread_rwlock_unlock(d_lock);
165 }
166
167 void upgrade()
168 {
169 if(g_singleThreaded)
170 return;
171
172 pthread_rwlock_unlock(d_lock);
173 pthread_rwlock_wrlock(d_lock);
174 }
175 };
176 #endif