Code

avltree: Lock the tree during insertion.
authorSebastian Harl <sh@tokkee.org>
Wed, 9 Jul 2014 07:55:23 +0000 (09:55 +0200)
committerSebastian Harl <sh@tokkee.org>
Wed, 9 Jul 2014 07:55:23 +0000 (09:55 +0200)
src/utils/avltree.c

index ce87c52ed726cd5c6048cfdbe2e35aeae6cd2401..8c13233b2eabb6dd7f3fbfd3e408eaccf99f5a0f 100644 (file)
@@ -302,9 +302,12 @@ sdb_avltree_insert(sdb_avltree_t *tree, sdb_object_t *obj)
        if (! n)
                return -1;
 
+       pthread_rwlock_wrlock(&tree->lock);
+
        if (! tree->root) {
                tree->root = n;
                tree->size = 1;
+               pthread_rwlock_unlock(&tree->lock);
                return 0;
        }
 
@@ -315,6 +318,7 @@ sdb_avltree_insert(sdb_avltree_t *tree, sdb_object_t *obj)
                diff = tree->cmp(obj, parent->obj);
                if (! diff) {
                        node_destroy(n);
+                       pthread_rwlock_unlock(&tree->lock);
                        return -1;
                }
 
@@ -338,6 +342,7 @@ sdb_avltree_insert(sdb_avltree_t *tree, sdb_object_t *obj)
        ++tree->size;
 
        rebalance(tree, parent);
+       pthread_rwlock_unlock(&tree->lock);
        return 0;
 } /* sdb_avltree_insert */