X-Git-Url: https://git.lttng.org/?p=urcu.git;a=blobdiff_plain;f=include%2Furcu%2Frculfhash.h;h=8586e1da371f86e2c19518f60a8cef69e6db691e;hp=29dd88f1f16b0ea320ffa82ef2ed0596c7aaba65;hb=HEAD;hpb=6568fdbf5ce243f1ac4d167e8479f84e2ae008b6 diff --git a/include/urcu/rculfhash.h b/include/urcu/rculfhash.h index 29dd88f..e0f4b35 100644 --- a/include/urcu/rculfhash.h +++ b/include/urcu/rculfhash.h @@ -1,28 +1,14 @@ +// SPDX-FileCopyrightText: 2011 Mathieu Desnoyers +// SPDX-FileCopyrightText: 2011 Lai Jiangshan +// +// SPDX-License-Identifier: LGPL-2.1-or-later + #ifndef _URCU_RCULFHASH_H #define _URCU_RCULFHASH_H /* - * urcu/rculfhash.h - * * Userspace RCU library - Lock-Free RCU Hash Table * - * Copyright 2011 - Mathieu Desnoyers - * Copyright 2011 - Lai Jiangshan - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * * For use with URCU_API_MAP (API mapping of liburcu), include this file * _after_ including your URCU flavor. */ @@ -82,6 +68,18 @@ struct cds_lfht_iter { #endif }; +/* + * cds_lfht_alloc: Callbacks if we want to use custom memory allocator. + */ +struct cds_lfht_alloc { + void *(*malloc)(void *state, size_t size); + void *(*calloc)(void *state, size_t nmemb, size_t size); + void *(*realloc)(void *state, void *ptr, size_t size); + void *(*aligned_alloc)(void *state, size_t alignment, size_t size); + void (*free)(void *state, void *ptr); + void *state; +}; + static inline struct cds_lfht_node *cds_lfht_iter_get_node(struct cds_lfht_iter *iter) { @@ -109,6 +107,16 @@ void cds_lfht_node_init(struct cds_lfht_node *node __attribute__((unused))) { } +/* + * cds_lfht_node_init_deleted - initialize a hash table node to "removed" state + * @node: the node to initialize. + * + * Initialize the node such that cds_lfht_is_node_deleted() can be used + * on the node before it is added to a hash table. + */ +extern +void cds_lfht_node_init_deleted(struct cds_lfht_node *node); + /* * Hash table creation flags. */ @@ -119,7 +127,7 @@ enum { struct cds_lfht_mm_type { struct cds_lfht *(*alloc_cds_lfht)(unsigned long min_nr_alloc_buckets, - unsigned long max_nr_buckets); + unsigned long max_nr_buckets, const struct cds_lfht_alloc *alloc); void (*alloc_bucket_table)(struct cds_lfht *ht, unsigned long order); void (*free_bucket_table)(struct cds_lfht *ht, unsigned long order); struct cds_lfht_node *(*bucket_at)(struct cds_lfht *ht, @@ -142,6 +150,19 @@ struct cds_lfht *_cds_lfht_new(unsigned long init_size, const struct rcu_flavor_struct *flavor, pthread_attr_t *attr); +/* + * _cds_lfht_new_with_alloc - API used by cds_lfht_new_with_flavor_alloc. + */ +extern +struct cds_lfht *_cds_lfht_new_with_alloc(unsigned long init_size, + unsigned long min_nr_alloc_buckets, + unsigned long max_nr_buckets, + int flags, + const struct cds_lfht_mm_type *mm, + const struct rcu_flavor_struct *flavor, + const struct cds_lfht_alloc *alloc, + pthread_attr_t *attr); + /* * cds_lfht_new_flavor - allocate a hash table tied to a RCU flavor. * @init_size: number of buckets to allocate initially. Must be power of two. @@ -184,6 +205,52 @@ struct cds_lfht *cds_lfht_new_flavor(unsigned long init_size, flags, NULL, flavor, attr); } +/* + * cds_lfht_new_with_flavor_alloc - allocate a hash table tied to a RCU flavor. + * @init_size: number of buckets to allocate initially. Must be power of two. + * @min_nr_alloc_buckets: the minimum number of allocated buckets. + * (must be power of two) + * @max_nr_buckets: the maximum number of hash table buckets allowed. + * (must be power of two, 0 is accepted, means + * "infinite") + * @flavor: flavor of liburcu to use to synchronize the hash table + * @alloc: Custom memory allocator for hash table memory management. + * NULL for default. If a custom allocator is used, then + * the whole interface of struct cds_lfht_alloc must be implemented. + * @flags: hash table creation flags (can be combined with bitwise or: '|'). + * 0: no flags. + * CDS_LFHT_AUTO_RESIZE: automatically resize hash table. + * CDS_LFHT_ACCOUNTING: count the number of node addition + * and removal in the table + * @attr: optional resize worker thread attributes. NULL for default. + * + * Return NULL on error. + * Note: the RCU flavor must be already included before the hash table header. + * + * The programmer is responsible for ensuring that resize operation has a + * priority equal to hash table updater threads. It should be performed by + * specifying the appropriate priority in the pthread "attr" argument, and, + * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have + * this priority level. Having lower priority for call_rcu and resize threads + * does not pose any correctness issue, but the resize operations could be + * starved by updates, thus leading to long hash table bucket chains. + * Threads calling cds_lfht_new are NOT required to be registered RCU + * read-side threads. It can be called very early. (e.g. before RCU is + * initialized) + */ +static inline +struct cds_lfht *cds_lfht_new_with_flavor_alloc(unsigned long init_size, + unsigned long min_nr_alloc_buckets, + unsigned long max_nr_buckets, + int flags, + const struct rcu_flavor_struct *flavor, + const struct cds_lfht_alloc *alloc, + pthread_attr_t *attr) +{ + return _cds_lfht_new_with_alloc(init_size, min_nr_alloc_buckets, max_nr_buckets, + flags, NULL, flavor, alloc, attr); +} + #ifdef URCU_API_MAP /* @@ -237,16 +304,16 @@ struct cds_lfht *cds_lfht_new(unsigned long init_size, * * Return 0 on success, negative error value on error. + * Threads calling this API need to be registered RCU read-side threads. + * * Prior to liburcu 0.10: - * - Threads calling this API need to be registered RCU read-side - * threads. * - cds_lfht_destroy should *not* be called from a RCU read-side * critical section. It should *not* be called from a call_rcu thread * context neither. * * Starting from liburcu 0.10, rculfhash implements its own worker - * thread to handle resize operations, which removes RCU requirements on - * cds_lfht_destroy. + * thread to handle resize operations, which removes the above RCU + * read-side critical section requirement on cds_lfht_destroy. */ extern int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr); @@ -534,24 +601,27 @@ void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size); cds_lfht_next_duplicate(ht, match, key, iter), \ node = cds_lfht_iter_get_node(iter)) +#define cds_lfht_entry(ptr, type, member) \ + caa_container_of_check_null(ptr, type, member) + #define cds_lfht_for_each_entry(ht, iter, pos, member) \ for (cds_lfht_first(ht, iter), \ - pos = caa_container_of(cds_lfht_iter_get_node(iter), \ - __typeof__(*(pos)), member); \ - cds_lfht_iter_get_node(iter) != NULL; \ + pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ + __typeof__(*(pos)), member); \ + pos != NULL; \ cds_lfht_next(ht, iter), \ - pos = caa_container_of(cds_lfht_iter_get_node(iter), \ - __typeof__(*(pos)), member)) + pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ + __typeof__(*(pos)), member)) #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \ iter, pos, member) \ for (cds_lfht_lookup(ht, hash, match, key, iter), \ - pos = caa_container_of(cds_lfht_iter_get_node(iter), \ - __typeof__(*(pos)), member); \ - cds_lfht_iter_get_node(iter) != NULL; \ + pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ + __typeof__(*(pos)), member); \ + pos != NULL; \ cds_lfht_next_duplicate(ht, match, key, iter), \ - pos = caa_container_of(cds_lfht_iter_get_node(iter), \ - __typeof__(*(pos)), member)) + pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \ + __typeof__(*(pos)), member)) #ifdef __cplusplus }