From: Jeff Layton <jlayton(a)redhat.com>
Add a function to walk every entry in the hashtable and issue a
function on each. The callback takes an rbt_node and an opaque
void * argument to allow for maintaining state across callbacks.
Change-Id: Ide04ff2402b11c3af91d8444f5dabe69eafe56c7
Signed-off-by: Jeff Layton <jlayton(a)redhat.com>
---
src/hashtable/hashtable.c | 17 +++++++++++++++++
src/include/hashtable.h | 3 +++
2 files changed, 20 insertions(+)
diff --git a/src/hashtable/hashtable.c b/src/hashtable/hashtable.c
index a0ba858453f9..200a20fc5f4f 100644
--- a/src/hashtable/hashtable.c
+++ b/src/hashtable/hashtable.c
@@ -1051,4 +1051,21 @@ hashtable_getref(hash_table_t *ht, struct gsh_buffdesc *key,
return rc;
}
+void hashtable_for_each(hash_table_t *ht, ht_for_each_cb_t callback, void *arg)
+{
+ uint32_t i;
+ struct rbt_head *head_rbt;
+ struct rbt_node *pn;
+
+ /* For each bucket of the requested hashtable */
+ for (i = 0; i < ht->parameter.index_size; i++) {
+ head_rbt = &ht->partitions[i].rbt;
+ PTHREAD_RWLOCK_rdlock(&ht->partitions[i].lock);
+ RBT_LOOP(head_rbt, pn) {
+ callback(pn, arg);
+ RBT_INCREMENT(pn);
+ }
+ PTHREAD_RWLOCK_unlock(&ht->partitions[i].lock);
+ }
+}
/** @} */
diff --git a/src/include/hashtable.h b/src/include/hashtable.h
index ba366268f5fd..438e6e72247c 100644
--- a/src/include/hashtable.h
+++ b/src/include/hashtable.h
@@ -344,6 +344,9 @@ hash_error_t hashtable_getref(struct hash_table *, struct gsh_buffdesc
*,
struct gsh_buffdesc *,
void (*)(struct gsh_buffdesc *));
+typedef void (*ht_for_each_cb_t)(struct rbt_node *pn, void *arg);
+void hashtable_for_each(struct hash_table *ht, ht_for_each_cb_t callback,
+ void *arg);
/** @} */
#endif /* HASHTABLE_H */
--
2.17.0