]> arthur.barton.de Git - netdata.git/blobdiff - src/avl.c
cleanup locking; ability to compile AVL with MUTEX instead of RWLOCK; disks and inter...
[netdata.git] / src / avl.c
index c41d5f6dd7ce37cc033b6af9e3105fc09593d1e0..f5d67dc20f1c9a08e4b764ac522eab20f5a74d3f 100755 (executable)
--- a/src/avl.c
+++ b/src/avl.c
@@ -19,6 +19,7 @@
 #include "avl.h"
 
 /* Private methods */
+int _avl_removeroot(avl_tree* t);
 
 /* Swing to the left
  * Warning: no balance maintainance
@@ -143,9 +144,19 @@ int _avl_insert(avl_tree* t, avl* a) {
        }
 }
 int avl_insert(avl_tree* t, avl* a) {
+#ifdef AVL_LOCK_WITH_MUTEX
+       pthread_mutex_lock(&t->mutex);
+#else
        pthread_rwlock_wrlock(&t->rwlock);
+#endif
+
        int ret = _avl_insert(t, a);
+
+#ifdef AVL_LOCK_WITH_MUTEX
+       pthread_mutex_unlock(&t->mutex);
+#else
        pthread_rwlock_unlock(&t->rwlock);
+#endif
        return ret;
 }
 
@@ -231,9 +242,19 @@ int _avl_remove(avl_tree* t, avl* a) {
 }
 
 int avl_remove(avl_tree* t, avl* a) {
+#ifdef AVL_LOCK_WITH_MUTEX
+       pthread_mutex_lock(&t->mutex);
+#else
        pthread_rwlock_wrlock(&t->rwlock);
+#endif
+
        int ret = _avl_remove(t, a);
+
+#ifdef AVL_LOCK_WITH_MUTEX
+       pthread_mutex_unlock(&t->mutex);
+#else
        pthread_rwlock_unlock(&t->rwlock);
+#endif
        return ret;
 }
 
@@ -277,9 +298,19 @@ int _avl_removeroot(avl_tree* t) {
 }
 
 int avl_removeroot(avl_tree* t) {
+#ifdef AVL_LOCK_WITH_MUTEX
+       pthread_mutex_lock(&t->mutex);
+#else
        pthread_rwlock_wrlock(&t->rwlock);
+#endif
+
        int ret = _avl_removeroot(t);
+
+#ifdef AVL_LOCK_WITH_MUTEX
+       pthread_mutex_unlock(&t->mutex);
+#else
        pthread_rwlock_unlock(&t->rwlock);
+#endif
        return ret;
 }
 
@@ -331,9 +362,20 @@ int _avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl* a), avl** ret) {
 }
 
 int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl* a), avl** ret) {
-       pthread_rwlock_rdlock(&t->rwlock);
+#ifdef AVL_LOCK_WITH_MUTEX
+       pthread_mutex_lock(&t->mutex);
+#else
+       pthread_rwlock_wrlock(&t->rwlock);
+#endif
+
        int ret2 = _avl_range(t, a, b, iter, ret);
+
+#ifdef AVL_LOCK_WITH_MUTEX
+       pthread_mutex_unlock(&t->mutex);
+#else
        pthread_rwlock_unlock(&t->rwlock);
+#endif
+
        return ret2;
 }
 
@@ -348,5 +390,9 @@ int avl_search(avl_tree* t, avl* a, int (*iter)(avl* a), avl** ret) {
 void avl_init(avl_tree* t, int (*compar)(void* a, void* b)) {
        t->root = NULL;
        t->compar = compar;
+#ifdef AVL_LOCK_WITH_MUTEX
+       pthread_mutex_init(&t->mutex, NULL);
+#else
        pthread_rwlock_init(&t->rwlock, NULL);
+#endif
 }