[7ef2249] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Adam Hraska
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup genericadt
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #ifndef KERN_CONC_HASH_TABLE_H_
|
---|
| 36 | #define KERN_CONC_HASH_TABLE_H_
|
---|
| 37 |
|
---|
| 38 | #include <stdint.h>
|
---|
| 39 | #include <adt/list.h>
|
---|
[8e3ed06] | 40 | #include <synch/rcu_types.h>
|
---|
[7ef2249] | 41 | #include <macros.h>
|
---|
[14c9aa6] | 42 | #include <synch/workqueue.h>
|
---|
[7ef2249] | 43 |
|
---|
| 44 | typedef uintptr_t cht_ptr_t;
|
---|
| 45 |
|
---|
| 46 | /** Concurrent hash table node link. */
|
---|
| 47 | typedef struct cht_link {
|
---|
[7c3fb9b] | 48 | /*
|
---|
| 49 | * Must be placed first.
|
---|
[1b20da0] | 50 | *
|
---|
| 51 | * The function pointer (rcu_link.func) is used to store the item's
|
---|
| 52 | * mixed memoized hash. If in use by RCU (ie waiting for deferred
|
---|
| 53 | * destruction) the hash will contain the value of
|
---|
[b9cb911] | 54 | * cht_t.op->remove_callback.
|
---|
[6eaed07] | 55 | */
|
---|
| 56 | union {
|
---|
| 57 | rcu_item_t rcu_link;
|
---|
| 58 | size_t hash;
|
---|
| 59 | };
|
---|
[7ef2249] | 60 | /** Link to the next item in the bucket including any marks. */
|
---|
| 61 | cht_ptr_t link;
|
---|
| 62 | } cht_link_t;
|
---|
| 63 |
|
---|
| 64 | /** Set of operations for a concurrent hash table. */
|
---|
| 65 | typedef struct cht_ops {
|
---|
[b9cb911] | 66 | /** Returns the hash of the item.
|
---|
[1b20da0] | 67 | *
|
---|
[b9cb911] | 68 | * Applicable also to items that were logically deleted from the table
|
---|
| 69 | * but have yet to be physically removed by means of remove_callback().
|
---|
| 70 | */
|
---|
[14c9aa6] | 71 | size_t (*hash)(const cht_link_t *item);
|
---|
[b9cb911] | 72 | /** Returns the hash value of the key used to search for entries. */
|
---|
[7ef2249] | 73 | size_t (*key_hash)(void *key);
|
---|
[b9cb911] | 74 | /** Returns true if the two items store equal search keys. */
|
---|
[7ef2249] | 75 | bool (*equal)(const cht_link_t *item1, const cht_link_t *item2);
|
---|
[b9cb911] | 76 | /** Returns true if the item contains an equal search key. */
|
---|
[7ef2249] | 77 | bool (*key_equal)(void *key, const cht_link_t *item);
|
---|
[b9cb911] | 78 | /** Invoked to free a removed item once all references to it are dropped. */
|
---|
[7ef2249] | 79 | void (*remove_callback)(cht_link_t *item);
|
---|
| 80 | } cht_ops_t;
|
---|
| 81 |
|
---|
[b9cb911] | 82 | /** Groups hash table buckets with their count.
|
---|
[1b20da0] | 83 | *
|
---|
[b9cb911] | 84 | * It allows both the number of buckets as well as the bucket array
|
---|
| 85 | * to be swapped atomically when resing the table.
|
---|
| 86 | */
|
---|
[7ef2249] | 87 | typedef struct cht_buckets {
|
---|
[b9cb911] | 88 | /** The number of buckets is 2^order. */
|
---|
[7ef2249] | 89 | size_t order;
|
---|
[b9cb911] | 90 | /** Array of single linked list bucket heads along with any marks. */
|
---|
[7ef2249] | 91 | cht_ptr_t head[1];
|
---|
| 92 | } cht_buckets_t;
|
---|
| 93 |
|
---|
| 94 | /** Concurrent hash table structure. */
|
---|
| 95 | typedef struct {
|
---|
[b9cb911] | 96 | /** Item specific operations. */
|
---|
[7ef2249] | 97 | cht_ops_t *op;
|
---|
[a35b458] | 98 |
|
---|
[b9cb911] | 99 | /** Buckets currently in use. */
|
---|
[7ef2249] | 100 | cht_buckets_t *b;
|
---|
[b9cb911] | 101 | /** Resized table buckets that will replace b once resize is complete. */
|
---|
[7ef2249] | 102 | cht_buckets_t *new_b;
|
---|
[1b20da0] | 103 | /** Invalid memoized hash value.
|
---|
| 104 | *
|
---|
[b9cb911] | 105 | * If cht_link.hash contains this value the item had been logically
|
---|
| 106 | * removed and is waiting to be freed. Such hashes (and the associated
|
---|
[1b20da0] | 107 | * items) are disregarded and skipped or the actual hash must be
|
---|
[b9cb911] | 108 | * determined via op->hash().
|
---|
| 109 | */
|
---|
[6eaed07] | 110 | size_t invalid_hash;
|
---|
[14c9aa6] | 111 |
|
---|
[b9cb911] | 112 | /** Minimum number of buckets is 2^min_order. */
|
---|
[6eaed07] | 113 | size_t min_order;
|
---|
[b9cb911] | 114 | /** Maximum number of items per bucket before the table grows. */
|
---|
[0b7bcb8] | 115 | size_t max_load;
|
---|
[b9cb911] | 116 | /** Table is resized in the background in a work queue. */
|
---|
[14c9aa6] | 117 | work_t resize_work;
|
---|
[b9cb911] | 118 | /** If positive the table should grow or shrink.
|
---|
[1b20da0] | 119 | *
|
---|
[b9cb911] | 120 | * If not 0 resize work had already been posted to the system work queue.
|
---|
| 121 | */
|
---|
[7ef2249] | 122 | atomic_t resize_reqs;
|
---|
[a35b458] | 123 |
|
---|
[b9cb911] | 124 | /** Number of items in the table that have not been logically deleted. */
|
---|
[7ef2249] | 125 | atomic_t item_cnt;
|
---|
| 126 | } cht_t;
|
---|
| 127 |
|
---|
| 128 | #define cht_get_inst(item, type, member) \
|
---|
| 129 | member_to_inst((item), type, member)
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | #define cht_read_lock() rcu_read_lock()
|
---|
| 133 | #define cht_read_unlock() rcu_read_unlock()
|
---|
| 134 |
|
---|
[c28413a9] | 135 | extern bool cht_create_simple(cht_t *h, cht_ops_t *op);
|
---|
[1b20da0] | 136 | extern bool cht_create(cht_t *h, size_t init_size, size_t min_size,
|
---|
[1433ecda] | 137 | size_t max_load, bool can_block, cht_ops_t *op);
|
---|
[7ef2249] | 138 | extern void cht_destroy(cht_t *h);
|
---|
[3ac5086] | 139 | extern void cht_destroy_unsafe(cht_t *h);
|
---|
[7ef2249] | 140 |
|
---|
| 141 | extern cht_link_t *cht_find(cht_t *h, void *key);
|
---|
| 142 | extern cht_link_t *cht_find_lazy(cht_t *h, void *key);
|
---|
[14c9aa6] | 143 | extern cht_link_t *cht_find_next(cht_t *h, const cht_link_t *item);
|
---|
| 144 | extern cht_link_t *cht_find_next_lazy(cht_t *h, const cht_link_t *item);
|
---|
| 145 |
|
---|
[7ef2249] | 146 | extern void cht_insert(cht_t *h, cht_link_t *item);
|
---|
[04d66804] | 147 | extern bool cht_insert_unique(cht_t *h, cht_link_t *item, cht_link_t **dup_item);
|
---|
[7ef2249] | 148 | extern size_t cht_remove_key(cht_t *h, void *key);
|
---|
| 149 | extern bool cht_remove_item(cht_t *h, cht_link_t *item);
|
---|
| 150 |
|
---|
| 151 | #endif
|
---|
| 152 |
|
---|
| 153 | /** @}
|
---|
| 154 | */
|
---|