]> arthur.barton.de Git - netdata.git/blob - src/locks.h
Merge branch 'master' into ab-debian
[netdata.git] / src / locks.h
1 #ifndef NETDATA_LOCKS_H
2 #define NETDATA_LOCKS_H
3
4 // ----------------------------------------------------------------------------
5 // mutex
6
7 typedef pthread_mutex_t netdata_mutex_t;
8
9 #define NETDATA_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
10
11 static inline int __netdata_mutex_init(netdata_mutex_t *mutex) {
12     int ret = pthread_mutex_init(mutex, NULL);
13     if(unlikely(ret != 0))
14         error("MUTEX_LOCK: failed to initialize (code %d).", ret);
15     return ret;
16 }
17
18 static inline int __netdata_mutex_lock(netdata_mutex_t *mutex) {
19     int ret = pthread_mutex_lock(mutex);
20     if(unlikely(ret != 0))
21         error("MUTEX_LOCK: failed to get lock (code %d)", ret);
22     return ret;
23 }
24
25 static inline int __netdata_mutex_trylock(netdata_mutex_t *mutex) {
26     int ret = pthread_mutex_trylock(mutex);
27     return ret;
28 }
29
30 static inline int __netdata_mutex_unlock(netdata_mutex_t *mutex) {
31     int ret = pthread_mutex_unlock(mutex);
32     if(unlikely(ret != 0))
33         error("MUTEX_LOCK: failed to unlock (code %d).", ret);
34     return ret;
35 }
36
37 #ifdef NETDATA_INTERNAL_CHECKS
38
39 static inline int netdata_mutex_init_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex) {
40     usec_t start = 0;
41
42     if(unlikely(debug_flags & D_LOCKS)) {
43         start = now_boottime_usec();
44         debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_init(0x%p) from %lu@%s, %s()", mutex, line, file, function);
45     }
46
47     int ret = __netdata_mutex_init(mutex);
48
49     debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_init(0x%p) = %d in %llu usec, from %lu@%s, %s()", mutex, ret, now_boottime_usec() - start, line, file, function);
50
51     return ret;
52 }
53
54 static inline int netdata_mutex_lock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex) {
55     usec_t start = 0;
56
57     if(unlikely(debug_flags & D_LOCKS)) {
58         start = now_boottime_usec();
59         debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_lock(0x%p) from %lu@%s, %s()", mutex, line, file, function);
60     }
61
62     int ret = __netdata_mutex_lock(mutex);
63
64     debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_lock(0x%p) = %d in %llu usec, from %lu@%s, %s()", mutex, ret, now_boottime_usec() - start, line, file, function);
65
66     return ret;
67 }
68
69 static inline int netdata_mutex_trylock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex) {
70     usec_t start = 0;
71
72     if(unlikely(debug_flags & D_LOCKS)) {
73         start = now_boottime_usec();
74         debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_trylock(0x%p) from %lu@%s, %s()", mutex, line, file, function);
75     }
76
77     int ret = __netdata_mutex_trylock(mutex);
78
79     debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_trylock(0x%p) = %d in %llu usec, from %lu@%s, %s()", mutex, ret, now_boottime_usec() - start, line, file, function);
80
81     return ret;
82 }
83
84 static inline int netdata_mutex_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex) {
85     usec_t start = 0;
86
87     if(unlikely(debug_flags & D_LOCKS)) {
88         start = now_boottime_usec();
89         debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_unlock(0x%p) from %lu@%s, %s()", mutex, line, file, function);
90     }
91
92     int ret = __netdata_mutex_unlock(mutex);
93
94     debug(D_LOCKS, "MUTEX_LOCK: netdata_mutex_unlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", mutex, ret, now_boottime_usec() - start, line, file, function);
95
96     return ret;
97 }
98
99 #define netdata_mutex_init(mutex)    netdata_mutex_init_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
100 #define netdata_mutex_lock(mutex)    netdata_mutex_lock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
101 #define netdata_mutex_trylock(mutex) netdata_mutex_trylock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
102 #define netdata_mutex_unlock(mutex)  netdata_mutex_unlock_debug(__FILE__, __FUNCTION__, __LINE__, mutex)
103
104 #else // !NETDATA_INTERNAL_CHECKS
105
106 #define netdata_mutex_init(mutex)    __netdata_mutex_init(mutex)
107 #define netdata_mutex_lock(mutex)    __netdata_mutex_lock(mutex)
108 #define netdata_mutex_trylock(mutex) __netdata_mutex_trylock(mutex)
109 #define netdata_mutex_unlock(mutex)  __netdata_mutex_unlock(mutex)
110
111 #endif // NETDATA_INTERNAL_CHECKS
112
113
114 // ----------------------------------------------------------------------------
115 // r/w lock
116
117 typedef pthread_rwlock_t netdata_rwlock_t;
118
119 #define NETDATA_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
120
121 static inline int __netdata_rwlock_destroy(netdata_rwlock_t *rwlock) {
122     int ret = pthread_rwlock_destroy(rwlock);
123     if(unlikely(ret != 0))
124         error("RW_LOCK: failed to destroy lock (code %d)", ret);
125     return ret;
126 }
127
128 static inline int __netdata_rwlock_init(netdata_rwlock_t *rwlock) {
129     int ret = pthread_rwlock_init(rwlock, NULL);
130     if(unlikely(ret != 0))
131         error("RW_LOCK: failed to initialize lock (code %d)", ret);
132     return ret;
133 }
134
135 static inline int __netdata_rwlock_rdlock(netdata_rwlock_t *rwlock) {
136     int ret = pthread_rwlock_rdlock(rwlock);
137     if(unlikely(ret != 0))
138         error("RW_LOCK: failed to obtain read lock (code %d)", ret);
139     return ret;
140 }
141
142 static inline int __netdata_rwlock_wrlock(netdata_rwlock_t *rwlock) {
143     int ret = pthread_rwlock_wrlock(rwlock);
144     if(unlikely(ret != 0))
145         error("RW_LOCK: failed to obtain write lock (code %d)", ret);
146     return ret;
147 }
148
149 static inline int __netdata_rwlock_unlock(netdata_rwlock_t *rwlock) {
150     int ret = pthread_rwlock_unlock(rwlock);
151     if(unlikely(ret != 0))
152         error("RW_LOCK: failed to release lock (code %d)", ret);
153     return ret;
154 }
155
156 static inline int __netdata_rwlock_tryrdlock(netdata_rwlock_t *rwlock) {
157     int ret = pthread_rwlock_tryrdlock(rwlock);
158     return ret;
159 }
160
161 static inline int __netdata_rwlock_trywrlock(netdata_rwlock_t *rwlock) {
162     int ret = pthread_rwlock_trywrlock(rwlock);
163     return ret;
164 }
165
166
167 #ifdef NETDATA_INTERNAL_CHECKS
168
169 static inline int netdata_rwlock_destroy_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
170     usec_t start = 0;
171
172     if(unlikely(debug_flags & D_LOCKS)) {
173         start = now_boottime_usec();
174         debug(D_LOCKS, "RW_LOCK: netdata_rwlock_destroy(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
175     }
176
177     int ret = __netdata_rwlock_destroy(rwlock);
178
179     debug(D_LOCKS, "RW_LOCK: netdata_rwlock_destroy(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
180
181     return ret;
182 }
183
184 static inline int netdata_rwlock_init_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
185     usec_t start = 0;
186
187     if(unlikely(debug_flags & D_LOCKS)) {
188         start = now_boottime_usec();
189         debug(D_LOCKS, "RW_LOCK: netdata_rwlock_init(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
190     }
191
192     int ret = __netdata_rwlock_init(rwlock);
193
194     debug(D_LOCKS, "RW_LOCK: netdata_rwlock_init(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
195
196     return ret;
197 }
198
199 static inline int netdata_rwlock_rdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
200     usec_t start = 0;
201
202     if(unlikely(debug_flags & D_LOCKS)) {
203         start = now_boottime_usec();
204         debug(D_LOCKS, "RW_LOCK: netdata_rwlock_rdlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
205     }
206
207     int ret = __netdata_rwlock_rdlock(rwlock);
208
209     debug(D_LOCKS, "RW_LOCK: netdata_rwlock_rdlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
210
211     return ret;
212 }
213
214 static inline int netdata_rwlock_wrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
215     usec_t start = 0;
216
217     if(unlikely(debug_flags & D_LOCKS)) {
218         start = now_boottime_usec();
219         debug(D_LOCKS, "RW_LOCK: netdata_rwlock_wrlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
220     }
221
222     int ret = __netdata_rwlock_wrlock(rwlock);
223
224     debug(D_LOCKS, "RW_LOCK: netdata_rwlock_wrlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
225
226     return ret;
227 }
228
229 static inline int netdata_rwlock_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
230     usec_t start = 0;
231
232     if(unlikely(debug_flags & D_LOCKS)) {
233         start = now_boottime_usec();
234         debug(D_LOCKS, "RW_LOCK: netdata_rwlock_unlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
235     }
236
237     int ret = __netdata_rwlock_unlock(rwlock);
238
239     debug(D_LOCKS, "RW_LOCK: netdata_rwlock_unlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
240
241     return ret;
242 }
243
244 static inline int netdata_rwlock_tryrdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
245     usec_t start = 0;
246
247     if(unlikely(debug_flags & D_LOCKS)) {
248         start = now_boottime_usec();
249         debug(D_LOCKS, "RW_LOCK: netdata_rwlock_tryrdlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
250     }
251
252     int ret = __netdata_rwlock_tryrdlock(rwlock);
253
254     debug(D_LOCKS, "RW_LOCK: netdata_rwlock_tryrdlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
255
256     return ret;
257 }
258
259 static inline int netdata_rwlock_trywrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
260     usec_t start = 0;
261
262     if(unlikely(debug_flags & D_LOCKS)) {
263         start = now_boottime_usec();
264         debug(D_LOCKS, "RW_LOCK: netdata_rwlock_trywrlock(0x%p) from %lu@%s, %s()", rwlock, line, file, function);
265     }
266
267     int ret = __netdata_rwlock_trywrlock(rwlock);
268
269     debug(D_LOCKS, "RW_LOCK: netdata_rwlock_trywrlock(0x%p) = %d in %llu usec, from %lu@%s, %s()", rwlock, ret, now_boottime_usec() - start, line, file, function);
270
271     return ret;
272 }
273
274 #define netdata_rwlock_destroy(rwlock)   netdata_rwlock_destroy_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
275 #define netdata_rwlock_init(rwlock)      netdata_rwlock_init_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
276 #define netdata_rwlock_rdlock(rwlock)    netdata_rwlock_rdlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
277 #define netdata_rwlock_wrlock(rwlock)    netdata_rwlock_wrlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
278 #define netdata_rwlock_unlock(rwlock)    netdata_rwlock_unlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
279 #define netdata_rwlock_tryrdlock(rwlock) netdata_rwlock_tryrdlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
280 #define netdata_rwlock_trywrlock(rwlock) netdata_rwlock_trywrlock_debug(__FILE__, __FUNCTION__, __LINE__, rwlock)
281
282 #else // !NETDATA_INTERNAL_CHECKS
283
284 #define netdata_rwlock_destroy(rwlock)    __netdata_rwlock_destroy(rwlock)
285 #define netdata_rwlock_init(rwlock)       __netdata_rwlock_init(rwlock)
286 #define netdata_rwlock_rdlock(rwlock)     __netdata_rwlock_rdlock(rwlock)
287 #define netdata_rwlock_wrlock(rwlock)     __netdata_rwlock_wrlock(rwlock)
288 #define netdata_rwlock_unlock(rwlock)     __netdata_rwlock_unlock(rwlock)
289 #define netdata_rwlock_tryrdlock(rwlock)  __netdata_rwlock_tryrdlock(rwlock)
290 #define netdata_rwlock_trywrlock(rwlock)  __netdata_rwlock_trywrlock(rwlock)
291
292 #endif // NETDATA_INTERNAL_CHECKS
293
294 #endif //NETDATA_LOCKS_H