1 #ifndef NETDATA_LOCKS_H
2 #define NETDATA_LOCKS_H
4 // ----------------------------------------------------------------------------
7 typedef pthread_mutex_t netdata_mutex_t;
9 #define NETDATA_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
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);
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);
25 static inline int __netdata_mutex_trylock(netdata_mutex_t *mutex) {
26 int ret = pthread_mutex_trylock(mutex);
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);
37 #ifdef NETDATA_INTERNAL_CHECKS
39 static inline int netdata_mutex_init_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex) {
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);
47 int ret = __netdata_mutex_init(mutex);
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);
54 static inline int netdata_mutex_lock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex) {
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);
62 int ret = __netdata_mutex_lock(mutex);
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);
69 static inline int netdata_mutex_trylock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex) {
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);
77 int ret = __netdata_mutex_trylock(mutex);
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);
84 static inline int netdata_mutex_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_mutex_t *mutex) {
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);
92 int ret = __netdata_mutex_unlock(mutex);
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);
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)
104 #else // !NETDATA_INTERNAL_CHECKS
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)
111 #endif // NETDATA_INTERNAL_CHECKS
114 // ----------------------------------------------------------------------------
117 typedef pthread_rwlock_t netdata_rwlock_t;
119 #define NETDATA_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
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);
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);
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);
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);
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);
156 static inline int __netdata_rwlock_tryrdlock(netdata_rwlock_t *rwlock) {
157 int ret = pthread_rwlock_tryrdlock(rwlock);
161 static inline int __netdata_rwlock_trywrlock(netdata_rwlock_t *rwlock) {
162 int ret = pthread_rwlock_trywrlock(rwlock);
167 #ifdef NETDATA_INTERNAL_CHECKS
169 static inline int netdata_rwlock_destroy_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
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);
177 int ret = __netdata_rwlock_destroy(rwlock);
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);
184 static inline int netdata_rwlock_init_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
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);
192 int ret = __netdata_rwlock_init(rwlock);
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);
199 static inline int netdata_rwlock_rdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
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);
207 int ret = __netdata_rwlock_rdlock(rwlock);
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);
214 static inline int netdata_rwlock_wrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
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);
222 int ret = __netdata_rwlock_wrlock(rwlock);
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);
229 static inline int netdata_rwlock_unlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
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);
237 int ret = __netdata_rwlock_unlock(rwlock);
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);
244 static inline int netdata_rwlock_tryrdlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
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);
252 int ret = __netdata_rwlock_tryrdlock(rwlock);
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);
259 static inline int netdata_rwlock_trywrlock_debug( const char *file, const char *function, const unsigned long line, netdata_rwlock_t *rwlock) {
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);
267 int ret = __netdata_rwlock_trywrlock(rwlock);
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);
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)
282 #else // !NETDATA_INTERNAL_CHECKS
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)
292 #endif // NETDATA_INTERNAL_CHECKS
294 #endif //NETDATA_LOCKS_H