From 30b94862648cdbf4f537337d2e2f01c369a9dee9 Mon Sep 17 00:00:00 2001 From: Lennart Weller Date: Thu, 21 Apr 2016 14:27:29 +0200 Subject: Imported Upstream version 1.1.0 --- src/avl.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) mode change 100755 => 100644 src/avl.c (limited to 'src/avl.c') diff --git a/src/avl.c b/src/avl.c old mode 100755 new mode 100644 index 4eb0ce0e4..fd4fb1420 --- a/src/avl.c +++ b/src/avl.c @@ -17,6 +17,7 @@ #include #endif #include "avl.h" +#include "log.h" /* Private methods */ int _avl_removeroot(avl_tree* t); @@ -144,19 +145,23 @@ int _avl_insert(avl_tree* t, avl* a) { } } int avl_insert(avl_tree* t, avl* a) { +#ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_lock(&t->mutex); #else pthread_rwlock_wrlock(&t->rwlock); #endif +#endif /* AVL_WITHOUT_PTHREADS */ int ret = _avl_insert(t, a); +#ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_unlock(&t->mutex); #else pthread_rwlock_unlock(&t->rwlock); #endif +#endif /* AVL_WITHOUT_PTHREADS */ return ret; } @@ -242,19 +247,23 @@ int _avl_remove(avl_tree* t, avl* a) { } int avl_remove(avl_tree* t, avl* a) { +#ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_lock(&t->mutex); #else pthread_rwlock_wrlock(&t->rwlock); #endif +#endif /* AVL_WITHOUT_PTHREADS */ int ret = _avl_remove(t, a); +#ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_unlock(&t->mutex); #else pthread_rwlock_unlock(&t->rwlock); #endif +#endif /* AVL_WITHOUT_PTHREADS */ return ret; } @@ -298,19 +307,23 @@ int _avl_removeroot(avl_tree* t) { } int avl_removeroot(avl_tree* t) { +#ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_lock(&t->mutex); #else pthread_rwlock_wrlock(&t->rwlock); #endif +#endif /* AVL_WITHOUT_PTHREADS */ int ret = _avl_removeroot(t); +#ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_unlock(&t->mutex); #else pthread_rwlock_unlock(&t->rwlock); #endif +#endif /* AVL_WITHOUT_PTHREADS */ return ret; } @@ -362,19 +375,23 @@ int _avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { } int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) { +#ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_lock(&t->mutex); #else pthread_rwlock_wrlock(&t->rwlock); #endif +#endif /* AVL_WITHOUT_PTHREADS */ int ret2 = _avl_range(t, a, b, iter, ret); +#ifndef AVL_WITHOUT_PTHREADS #ifdef AVL_LOCK_WITH_MUTEX pthread_mutex_unlock(&t->mutex); #else pthread_rwlock_unlock(&t->rwlock); #endif +#endif /* AVL_WITHOUT_PTHREADS */ return ret2; } @@ -390,9 +407,19 @@ 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; + +#ifndef AVL_WITHOUT_PTHREADS + int lock; + #ifdef AVL_LOCK_WITH_MUTEX - pthread_mutex_init(&t->mutex, NULL); + lock = pthread_mutex_init(&t->mutex, NULL); #else - pthread_rwlock_init(&t->rwlock, NULL); + lock = pthread_rwlock_init(&t->rwlock, NULL); #endif + + if(lock != 0) + fatal("Failed to initialize AVL mutex/rwlock, error: %d", lock); + +#endif /* AVL_WITHOUT_PTHREADS */ + } -- cgit v1.2.3