feat(tree): add rwlock

pull/7/head
Jef Roosens 2023-03-07 12:12:46 +01:00
parent 83b3198a49
commit 1c563d8421
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 32 additions and 0 deletions

View File

@ -67,4 +67,22 @@ void vieter_tree_iterator_free(vieter_tree_iterator **ptp);
vieter_tree_error vieter_tree_iterator_next(void **out, vieter_tree_error vieter_tree_iterator_next(void **out,
vieter_tree_iterator *iter); vieter_tree_iterator *iter);
/*
* Acquire a read lock on the tree. Return value is the result of
* pthread_rwlock_rdlock.
*/
int vieter_tree_rlock(vieter_tree *tree);
/*
* Acquire a write lock on the tree. Return value is the result of
* pthread_rwlock_wrlock.
*/
int vieter_tree_wlock(vieter_tree *tree);
/*
* Unlock the lock after having acquired it. Return value is the result of
* pthread_rwlock_unlock.
*/
int vieter_tree_unlock(vieter_tree *tree);
#endif #endif

View File

@ -114,3 +114,15 @@ bool vieter_tree_validate(vieter_tree *tree) {
return vieter_tree_node_get(tree->root, vieter_tree_node_black) && return vieter_tree_node_get(tree->root, vieter_tree_node_black) &&
vieter_tree_node_validate(tree->root, 0, expected_black_nodes); vieter_tree_node_validate(tree->root, 0, expected_black_nodes);
} }
int vieter_tree_rlock(vieter_tree *tree) {
return pthread_rwlock_rdlock(&tree->lock);
}
int vieter_tree_wlock(vieter_tree *tree) {
return pthread_rwlock_wrlock(&tree->lock);
}
int vieter_tree_unlock(vieter_tree *tree) {
return pthread_rwlock_unlock(&tree->lock);
}

View File

@ -1,11 +1,13 @@
#include "vieter_tree.h" #include "vieter_tree.h"
#include "vieter_tree_node.h" #include "vieter_tree_node.h"
#include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
struct vieter_tree { struct vieter_tree {
uint64_t size; uint64_t size;
vieter_tree_node *root; vieter_tree_node *root;
pthread_rwlock_t lock;
}; };
/* /*