source: mainline/kernel/generic/include/adt/cht.h@ 78de83de

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 78de83de was 78de83de, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Improve kernel spinlock and AS refcount.

  • Property mode set to 100644
File size: 5.3 KB
Line 
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 <atomic.h>
39#include <stdint.h>
40#include <adt/list.h>
41#include <synch/rcu_types.h>
42#include <macros.h>
43#include <synch/workqueue.h>
44
45typedef uintptr_t cht_ptr_t;
46
47/** Concurrent hash table node link. */
48typedef struct cht_link {
49 /*
50 * Must be placed first.
51 *
52 * The function pointer (rcu_link.func) is used to store the item's
53 * mixed memoized hash. If in use by RCU (ie waiting for deferred
54 * destruction) the hash will contain the value of
55 * cht_t.op->remove_callback.
56 */
57 union {
58 rcu_item_t rcu_link;
59 size_t hash;
60 };
61 /** Link to the next item in the bucket including any marks. */
62 cht_ptr_t link;
63} cht_link_t;
64
65/** Set of operations for a concurrent hash table. */
66typedef struct cht_ops {
67 /** Returns the hash of the item.
68 *
69 * Applicable also to items that were logically deleted from the table
70 * but have yet to be physically removed by means of remove_callback().
71 */
72 size_t (*hash)(const cht_link_t *item);
73 /** Returns the hash value of the key used to search for entries. */
74 size_t (*key_hash)(void *key);
75 /** Returns true if the two items store equal search keys. */
76 bool (*equal)(const cht_link_t *item1, const cht_link_t *item2);
77 /** Returns true if the item contains an equal search key. */
78 bool (*key_equal)(void *key, const cht_link_t *item);
79 /** Invoked to free a removed item once all references to it are dropped. */
80 void (*remove_callback)(cht_link_t *item);
81} cht_ops_t;
82
83/** Groups hash table buckets with their count.
84 *
85 * It allows both the number of buckets as well as the bucket array
86 * to be swapped atomically when resing the table.
87 */
88typedef struct cht_buckets {
89 /** The number of buckets is 2^order. */
90 size_t order;
91 /** Array of single linked list bucket heads along with any marks. */
92 cht_ptr_t head[1];
93} cht_buckets_t;
94
95/** Concurrent hash table structure. */
96typedef struct {
97 /** Item specific operations. */
98 cht_ops_t *op;
99
100 /** Buckets currently in use. */
101 cht_buckets_t *b;
102 /** Resized table buckets that will replace b once resize is complete. */
103 cht_buckets_t *new_b;
104 /** Invalid memoized hash value.
105 *
106 * If cht_link.hash contains this value the item had been logically
107 * removed and is waiting to be freed. Such hashes (and the associated
108 * items) are disregarded and skipped or the actual hash must be
109 * determined via op->hash().
110 */
111 size_t invalid_hash;
112
113 /** Minimum number of buckets is 2^min_order. */
114 size_t min_order;
115 /** Maximum number of items per bucket before the table grows. */
116 size_t max_load;
117 /** Table is resized in the background in a work queue. */
118 work_t resize_work;
119 /** If positive the table should grow or shrink.
120 *
121 * If not 0 resize work had already been posted to the system work queue.
122 */
123 atomic_t resize_reqs;
124
125 /** Number of items in the table that have not been logically deleted. */
126 atomic_t item_cnt;
127} cht_t;
128
129#define cht_get_inst(item, type, member) \
130 member_to_inst((item), type, member)
131
132
133#define cht_read_lock() rcu_read_lock()
134#define cht_read_unlock() rcu_read_unlock()
135
136extern bool cht_create_simple(cht_t *h, cht_ops_t *op);
137extern bool cht_create(cht_t *h, size_t init_size, size_t min_size,
138 size_t max_load, bool can_block, cht_ops_t *op);
139extern void cht_destroy(cht_t *h);
140extern void cht_destroy_unsafe(cht_t *h);
141
142extern cht_link_t *cht_find(cht_t *h, void *key);
143extern cht_link_t *cht_find_lazy(cht_t *h, void *key);
144extern cht_link_t *cht_find_next(cht_t *h, const cht_link_t *item);
145extern cht_link_t *cht_find_next_lazy(cht_t *h, const cht_link_t *item);
146
147extern void cht_insert(cht_t *h, cht_link_t *item);
148extern bool cht_insert_unique(cht_t *h, cht_link_t *item, cht_link_t **dup_item);
149extern size_t cht_remove_key(cht_t *h, void *key);
150extern bool cht_remove_item(cht_t *h, cht_link_t *item);
151
152#endif
153
154/** @}
155 */
Note: See TracBrowser for help on using the repository browser.