summaryrefslogtreecommitdiffstats
path: root/lib/hash.c
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2019-06-21 10:58:02 +0200
committerDavid Lamparter <equinox@diac24.net>2019-09-03 17:15:17 +0200
commit00dffa8cde7661e00245ebe1b1eea248b8dd6802 (patch)
treea1e698d6b613b63407b3ad786565d09c7e7b7382 /lib/hash.c
parentlib: add some macro helpers (diff)
downloadfrr-00dffa8cde7661e00245ebe1b1eea248b8dd6802.tar.xz
frr-00dffa8cde7661e00245ebe1b1eea248b8dd6802.zip
lib: add frr_with_mutex() block-wrapper
frr_with_mutex(...) { ... } locks and automatically unlocks the listed mutex(es) when the block is exited. This adds a bit of safety against forgetting the unlock in error paths & co. and makes the code a slight bit more readable. Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'lib/hash.c')
-rw-r--r--lib/hash.c9
1 files changed, 3 insertions, 6 deletions
diff --git a/lib/hash.c b/lib/hash.c
index 9d9d39702..7f8a23704 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -28,6 +28,7 @@
#include "vty.h"
#include "command.h"
#include "libfrr.h"
+#include "frr_pthread.h"
DEFINE_MTYPE_STATIC(LIB, HASH, "Hash")
DEFINE_MTYPE_STATIC(LIB, HASH_BACKET, "Hash Bucket")
@@ -54,14 +55,12 @@ struct hash *hash_create_size(unsigned int size,
hash->name = name ? XSTRDUP(MTYPE_HASH, name) : NULL;
hash->stats.empty = hash->size;
- pthread_mutex_lock(&_hashes_mtx);
- {
+ frr_with_mutex(&_hashes_mtx) {
if (!_hashes)
_hashes = list_new();
listnode_add(_hashes, hash);
}
- pthread_mutex_unlock(&_hashes_mtx);
return hash;
}
@@ -311,8 +310,7 @@ struct list *hash_to_list(struct hash *hash)
void hash_free(struct hash *hash)
{
- pthread_mutex_lock(&_hashes_mtx);
- {
+ frr_with_mutex(&_hashes_mtx) {
if (_hashes) {
listnode_delete(_hashes, hash);
if (_hashes->count == 0) {
@@ -320,7 +318,6 @@ void hash_free(struct hash *hash)
}
}
}
- pthread_mutex_unlock(&_hashes_mtx);
XFREE(MTYPE_HASH, hash->name);