qht: add qht_iter_remove

This currently has no users, but the use case is so common that I
think we must support it.

Note that without the appended we cannot safely remove a set of
elements; a 2-step approach (i.e. qht_iter first, keep track of
the to-be-deleted elements, and then a bunch of qht_remove calls)
would be racy, since between the iteration and the removals other
threads might insert additional elements.

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Emilio G. Cota <cota@braap.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Emilio G. Cota 2018-08-15 15:00:48 -04:00 committed by Richard Henderson
parent e2f07efadd
commit 69d55e9cc2
2 changed files with 85 additions and 8 deletions

View file

@ -44,6 +44,8 @@ struct qht_stats {
typedef bool (*qht_lookup_func_t)(const void *obj, const void *userp);
typedef void (*qht_iter_func_t)(struct qht *ht, void *p, uint32_t h, void *up);
typedef bool (*qht_iter_bool_func_t)(struct qht *ht, void *p, uint32_t h,
void *up);
#define QHT_MODE_AUTO_RESIZE 0x1 /* auto-resize when heavily loaded */
#define QHT_MODE_RAW_MUTEXES 0x2 /* bypass the profiler (QSP) */
@ -179,9 +181,26 @@ bool qht_resize(struct qht *ht, size_t n_elems);
*
* Each time it is called, user-provided @func is passed a pointer-hash pair,
* plus @userp.
*
* Note: @ht cannot be accessed from @func
* See also: qht_iter_remove()
*/
void qht_iter(struct qht *ht, qht_iter_func_t func, void *userp);
/**
* qht_iter_remove - Iterate over a QHT, optionally removing entries
* @ht: QHT to be iterated over
* @func: function to be called for each entry in QHT
* @userp: additional pointer to be passed to @func
*
* Each time it is called, user-provided @func is passed a pointer-hash pair,
* plus @userp. If @func returns true, the pointer-hash pair is removed.
*
* Note: @ht cannot be accessed from @func
* See also: qht_iter()
*/
void qht_iter_remove(struct qht *ht, qht_iter_bool_func_t func, void *userp);
/**
* qht_statistics_init - Gather statistics from a QHT
* @ht: QHT to gather statistics from