-
/*
- * TODO: keys are currently assumed <= sizeof(void *). Key target never freed.
+ * rculfhash.c
+ *
+ * Userspace RCU library - Lock-Free Expandable RCU Hash Table
+ *
+ * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * 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
*/
#define _LGPL_SOURCE
#include <errno.h>
#include <assert.h>
#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
#include <urcu.h>
-#include <urcu-defer.h>
+#include <urcu-call-rcu.h>
#include <urcu/arch.h>
#include <urcu/uatomic.h>
#include <urcu/jhash.h>
#include <urcu/compiler.h>
+#include <urcu/rculfhash.h>
#include <stdio.h>
#include <pthread.h>
-#include <urcu/rculfhash.h>
-/*
- * Maximum number of hash table buckets: 256M on 64-bit.
- * Should take about 512MB max if we assume 1 node per 4 buckets.
- */
-#define MAX_HT_BUCKETS ((256 << 10) / sizeof(void *))
+#define DEBUG /* Test */
-/* node flags */
-#define NODE_STOLEN (1 << 0)
+#ifdef DEBUG
+#define dbg_printf(args...) printf(args)
+#else
+#define dbg_printf(args...)
+#endif
-struct rcu_ht_node;
+#define CHAIN_LEN_TARGET 1
+#define CHAIN_LEN_RESIZE_THRESHOLD 2
-struct rcu_ht_node {
- struct rcu_ht_node *next;
- void *key;
- void *data;
- unsigned int flags;
-};
+#ifndef max
+#define max(a, b) ((a) > (b) ? (a) : (b))
+#endif
struct rcu_table {
- unsigned long size;
+ unsigned long size; /* always a power of 2 */
+ unsigned long resize_target;
+ int resize_initiated;
+ struct rcu_head head;
struct rcu_ht_node *tbl[0];
};
struct rcu_ht {
struct rcu_table *t; /* shared */
ht_hash_fct hash_fct;
- void (*free_fct)(void *data); /* fct to free data */
- uint32_t keylen;
- uint32_t hashseed;
+ ht_compare_fct compare_fct;
+ unsigned long hash_seed;
pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
- int resize_ongoing; /* fast-path resize check */
+ void (*ht_call_rcu)(struct rcu_head *head,
+ void (*func)(struct rcu_head *head));
};
-struct rcu_ht *ht_new(ht_hash_fct hash_fct, void (*free_fct)(void *data),
- unsigned long init_size, uint32_t keylen,
- uint32_t hashseed)
-{
+struct rcu_resize_work {
+ struct rcu_head head;
struct rcu_ht *ht;
+};
- ht = calloc(1, sizeof(struct rcu_ht));
- ht->hash_fct = hash_fct;
- ht->free_fct = free_fct;
- ht->keylen = keylen;
- ht->hashseed = hashseed;
- /* this mutex should not nest in read-side C.S. */
- pthread_mutex_init(&ht->resize_mutex, NULL);
- ht->resize_ongoing = 0; /* shared */
- ht->t = calloc(1, sizeof(struct rcu_table)
- + (init_size * sizeof(struct rcu_ht_node *)));
- ht->t->size = init_size;
- return ht;
+/*
+ * Algorithm to reverse bits in a word by lookup table, extended to
+ * 64-bit words.
+ * Source:
+ * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
+ * Originally from Public Domain.
+ */
+
+static const uint8_t BitReverseTable256[256] =
+{
+#define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
+#define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
+#define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
+ R6(0), R6(2), R6(1), R6(3)
+};
+#undef R2
+#undef R4
+#undef R6
+
+static
+uint8_t bit_reverse_u8(uint8_t v)
+{
+ return BitReverseTable256[v];
}
-void *ht_lookup(struct rcu_ht *ht, void *key)
+static __attribute__((unused))
+uint32_t bit_reverse_u32(uint32_t v)
{
- struct rcu_table *t;
- unsigned long hash;
- struct rcu_ht_node *node;
- void *ret;
+ return ((uint32_t) bit_reverse_u8(v) << 24) |
+ ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
+ ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
+ ((uint32_t) bit_reverse_u8(v >> 24));
+}
- rcu_read_lock();
- t = rcu_dereference(ht->t);
- smp_read_barrier_depends(); /* read t before size and table */
- hash = ht->hash_fct(key, ht->keylen, ht->hashseed) % t->size;
- smp_read_barrier_depends(); /* read size before links */
- node = rcu_dereference(t->tbl[hash]);
- for (;;) {
- if (likely(!node)) {
- ret = NULL;
- break;
- }
- if (node->key == key) {
- ret = node->data;
- break;
- }
- node = rcu_dereference(node->next);
- }
- rcu_read_unlock();
+static __attribute__((unused))
+uint64_t bit_reverse_u64(uint64_t v)
+{
+ return ((uint64_t) bit_reverse_u8(v) << 56) |
+ ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
+ ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
+ ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
+ ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
+ ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
+ ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
+ ((uint64_t) bit_reverse_u8(v >> 56));
+}
- return ret;
+static
+unsigned long bit_reverse_ulong(unsigned long v)
+{
+#if (CAA_BITS_PER_LONG == 32)
+ return bit_reverse_u32(v);
+#else
+ return bit_reverse_u64(v);
+#endif
}
/*
- * Will re-try until either:
- * - The key is already there (-EEXIST)
- * - We successfully add the key at the head of a table bucket.
+ * Algorithm to find the log2 of a 32-bit unsigned integer.
+ * source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
+ * Originally from Public Domain.
*/
-int ht_add(struct rcu_ht *ht, void *key, void *data)
+static const char LogTable256[256] =
{
- struct rcu_ht_node *node, *old_head, *new_head;
- struct rcu_table *t;
- unsigned long hash;
- int ret = 0;
-
- new_head = calloc(1, sizeof(struct rcu_ht_node));
- new_head->key = key;
- new_head->data = data;
- new_head->flags = 0;
- /* here comes the fun and tricky part.
- * Add at the beginning with a cmpxchg.
- * Hold a read lock between the moment the first element is read
- * and the nodes traversal (to find duplicates). This ensures
- * the head pointer has not been reclaimed when cmpxchg is done.
- * Always adding at the head ensures that we would have to
- * re-try if a new item has been added concurrently. So we ensure that
- * we never add duplicates. */
-retry:
- rcu_read_lock();
-
- if (unlikely(LOAD_SHARED(ht->resize_ongoing))) {
- rcu_read_unlock();
- /*
- * Wait for resize to complete before continuing.
- */
- ret = pthread_mutex_lock(&ht->resize_mutex);
- assert(!ret);
- ret = pthread_mutex_unlock(&ht->resize_mutex);
- assert(!ret);
- goto retry;
- }
+#define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
+ -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
+ LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
+ LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
+};
- t = rcu_dereference(ht->t);
- /* no read barrier needed, because no concurrency with resize */
- hash = ht->hash_fct(key, ht->keylen, ht->hashseed) % t->size;
+uint32_t log2_u32(uint32_t v)
+{
+ uint32_t t, tt;
- old_head = node = rcu_dereference(t->tbl[hash]);
- for (;;) {
- if (likely(!node)) {
- break;
- }
- if (node->key == key) {
- ret = -EEXIST;
- goto end;
- }
- node = rcu_dereference(node->next);
- }
- new_head->next = old_head;
- if (rcu_cmpxchg_pointer(&t->tbl[hash], old_head, new_head) != old_head)
- goto restart;
-end:
- rcu_read_unlock();
- return ret;
+ if ((tt = (v >> 16)))
+ return (t = (tt >> 8))
+ ? 24 + LogTable256[t]
+ : 16 + LogTable256[tt];
+ else
+ return (t = (v >> 8))
+ ? 8 + LogTable256[t]
+ : LogTable256[v];
+}
- /* restart loop, release and re-take the read lock to be kind to GP */
-restart:
- rcu_read_unlock();
- goto retry;
+static
+void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth);
+
+static
+void check_resize(struct rcu_ht *ht, struct rcu_table *t,
+ uint32_t chain_len)
+{
+ if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
+ ht_resize_lazy(ht, t,
+ log2_u32(chain_len - CHAIN_LEN_TARGET - 1));
}
-/*
- * Restart until we successfully remove the entry, or no entry is left
- * ((void *)(unsigned long)-ENOENT).
- * Deal with concurrent stealers by doing an extra verification pass to check
- * that no element in the list are still pointing to the element stolen.
- * This could happen if two concurrent steal for consecutive objects are
- * executed. A pointer to an object being stolen could be saved by the
- * concurrent stealer for the previous object.
- * Also, given that in this precise scenario, another stealer can also want to
- * delete the doubly-referenced object; use a "stolen" flag to let only one
- * stealer delete the object.
- */
-void *ht_steal(struct rcu_ht *ht, void *key)
+static
+struct rcu_ht_node *clear_flag(struct rcu_ht_node *node)
{
- struct rcu_ht_node **prev, *node, *del_node = NULL;
- struct rcu_table *t;
- unsigned long hash;
- void *data;
- int ret;
+ return (struct rcu_ht_node *) (((unsigned long) node) & ~0x1);
+}
+
+static
+int is_removed(struct rcu_ht_node *node)
+{
+ return ((unsigned long) node) & 0x1;
+}
-retry:
- rcu_read_lock();
+static
+struct rcu_ht_node *flag_removed(struct rcu_ht_node *node)
+{
+ return (struct rcu_ht_node *) (((unsigned long) node) | 0x1);
+}
- if (unlikely(LOAD_SHARED(ht->resize_ongoing))) {
- rcu_read_unlock();
- /*
- * Wait for resize to complete before continuing.
- */
- ret = pthread_mutex_lock(&ht->resize_mutex);
- assert(!ret);
- ret = pthread_mutex_unlock(&ht->resize_mutex);
- assert(!ret);
- goto retry;
- }
+static
+unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
+{
+ unsigned long old1, old2;
+
+ old1 = uatomic_read(ptr);
+ do {
+ old2 = old1;
+ if (old2 >= v)
+ return old2;
+ } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
+ return v;
+}
- t = rcu_dereference(ht->t);
- /* no read barrier needed, because no concurrency with resize */
- hash = ht->hash_fct(key, ht->keylen, ht->hashseed) % t->size;
+/*
+ * Remove all logically deleted nodes from a bucket up to a certain node key.
+ */
+static
+void _ht_gc_bucket(struct rcu_ht_node *dummy, struct rcu_ht_node *node)
+{
+ struct rcu_ht_node *iter_prev, *iter, *next;
- prev = &t->tbl[hash];
- node = rcu_dereference(*prev);
for (;;) {
- if (likely(!node)) {
- if (del_node) {
- goto end;
- } else {
- goto error;
- }
- }
- if (node->key == key) {
- break;
+ iter_prev = dummy;
+ /* We can always skip the dummy node initially */
+ iter = rcu_dereference(iter_prev->next);
+ assert(iter_prev->reverse_hash <= node->reverse_hash);
+ if (unlikely(!iter))
+ return;
+ for (;;) {
+ if (clear_flag(iter)->reverse_hash > node->reverse_hash)
+ return;
+ next = rcu_dereference(clear_flag(iter)->next);
+ if (is_removed(next))
+ break;
+ if (unlikely(!next))
+ return;
+ iter_prev = iter;
+ iter = next;
}
- prev = &node->next;
- node = rcu_dereference(*prev);
+ assert(!is_removed(iter));
+ (void) uatomic_cmpxchg(&iter_prev->next, iter, clear_flag(next));
}
+}
+
+static
+int _ht_add(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node,
+ int unique)
+{
+ struct rcu_ht_node *iter_prev, *dummy, *iter, *next;
+
+ if (!t->size)
+ return 0;
+ for (;;) {
+ uint32_t chain_len = 0;
- if (!del_node) {
/*
- * Another concurrent thread stole it ? If so, let it deal with
- * this. Assume NODE_STOLEN is the only flag. If this changes,
- * read flags before cmpxchg.
+ * iter_prev points to the non-removed node prior to the
+ * insert location.
*/
- if (cmpxchg(&node->flags, 0, NODE_STOLEN) != 0)
- goto error;
+ iter_prev = rcu_dereference(t->tbl[node->hash & (t->size - 1)]);
+ /* We can always skip the dummy node initially */
+ iter = rcu_dereference(iter_prev->next);
+ assert(iter_prev->reverse_hash <= node->reverse_hash);
+ for (;;) {
+ if (unlikely(!iter))
+ goto insert;
+ if (clear_flag(iter)->reverse_hash > node->reverse_hash)
+ goto insert;
+ next = rcu_dereference(clear_flag(iter)->next);
+ if (is_removed(next))
+ goto gc;
+ /* Only account for identical reverse hash once */
+ if (iter_prev->reverse_hash != clear_flag(iter)->reverse_hash)
+ check_resize(ht, t, ++chain_len);
+ iter_prev = clear_flag(iter);
+ iter = next;
+ }
+ insert:
+ assert(node != clear_flag(iter));
+ assert(!is_removed(iter_prev));
+ assert(iter_prev != node);
+ node->next = iter;
+ if (uatomic_cmpxchg(&iter_prev->next, iter,
+ node) != iter)
+ continue; /* retry */
+ else
+ goto gc_end;
+ gc:
+ /* Garbage collect logically removed nodes in the bucket */
+ dummy = rcu_dereference(t->tbl[node->hash & (t->size - 1)]);
+ _ht_gc_bucket(dummy, node);
+ /* retry */
}
+gc_end:
+ /* Garbage collect logically removed nodes in the bucket */
+ dummy = rcu_dereference(t->tbl[node->hash & (t->size - 1)]);
+ _ht_gc_bucket(dummy, node);
+ return 0;
+}
+
+static
+int _ht_remove(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node)
+{
+ struct rcu_ht_node *dummy, *next, *old;
+ int flagged = 0;
+
+ /* logically delete the node */
+ old = rcu_dereference(node->next);
+ do {
+ next = old;
+ if (is_removed(next))
+ goto end;
+ assert(!node->dummy);
+ old = uatomic_cmpxchg(&node->next, next,
+ flag_removed(next));
+ } while (old != next);
- /* Found it ! pointer to object is in "prev" */
- if (rcu_cmpxchg_pointer(prev, node, node->next) == node)
- del_node = node;
- goto restart;
+ /* We performed the (logical) deletion. */
+ flagged = 1;
+ /*
+ * Ensure that the node is not visible to readers anymore: lookup for
+ * the node, and remove it (along with any other logically removed node)
+ * if found.
+ */
+ dummy = rcu_dereference(t->tbl[node->hash & (t->size - 1)]);
+ _ht_gc_bucket(dummy, node);
end:
/*
- * From that point, we own node. Note that there can still be concurrent
- * RCU readers using it. We can free it outside of read lock after a GP.
+ * Only the flagging action indicated that we (and no other)
+ * removed the node from the hash.
*/
- rcu_read_unlock();
-
- data = del_node->data;
- call_rcu(free, del_node);
- return data;
-
-error:
- data = (void *)(unsigned long)-ENOENT;
- rcu_read_unlock();
- return data;
+ if (flagged) {
+ assert(is_removed(rcu_dereference(node->next)));
+ return 0;
+ } else
+ return -ENOENT;
+}
- /* restart loop, release and re-take the read lock to be kind to GP */
-restart:
- rcu_read_unlock();
- goto retry;
+static
+void init_table(struct rcu_ht *ht, struct rcu_table *t,
+ unsigned long first, unsigned long len)
+{
+ unsigned long i, end;
+
+ end = first + len;
+ for (i = first; i < end; i++) {
+ /* Update table size when power of two */
+ if (i != 0 && !(i & (i - 1)))
+ t->size = i;
+ t->tbl[i] = calloc(1, sizeof(struct rcu_ht_node));
+ t->tbl[i]->dummy = 1;
+ t->tbl[i]->hash = i;
+ t->tbl[i]->reverse_hash = bit_reverse_ulong(i);
+ (void) _ht_add(ht, t, t->tbl[i], 0);
+ }
+ t->resize_target = t->size = end;
+ t->resize_initiated = 0;
}
-int ht_delete(struct rcu_ht *ht, void *key)
+struct rcu_ht *ht_new(ht_hash_fct hash_fct,
+ ht_compare_fct compare_fct,
+ unsigned long hash_seed,
+ unsigned long init_size,
+ void (*ht_call_rcu)(struct rcu_head *head,
+ void (*func)(struct rcu_head *head)))
{
- void *data;
+ struct rcu_ht *ht;
- data = ht_steal(ht, key);
- if (data && data != (void *)(unsigned long)-ENOENT) {
- if (ht->free_fct)
- call_rcu(ht->free_fct, data);
- return 0;
- } else {
- return -ENOENT;
- }
+ ht = calloc(1, sizeof(struct rcu_ht));
+ ht->hash_fct = hash_fct;
+ ht->compare_fct = compare_fct;
+ ht->hash_seed = hash_seed;
+ ht->ht_call_rcu = ht_call_rcu;
+ /* this mutex should not nest in read-side C.S. */
+ pthread_mutex_init(&ht->resize_mutex, NULL);
+ ht->t = calloc(1, sizeof(struct rcu_table)
+ + (max(init_size, 1) * sizeof(struct rcu_ht_node *)));
+ ht->t->size = 0;
+ pthread_mutex_lock(&ht->resize_mutex);
+ init_table(ht, ht->t, 0, max(init_size, 1));
+ pthread_mutex_unlock(&ht->resize_mutex);
+ return ht;
}
-/* Delete all old elements. Allow concurrent writer accesses. */
-int ht_delete_all(struct rcu_ht *ht)
+struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key, size_t key_len)
{
- unsigned long i;
- struct rcu_ht_node **prev, *node, *inext;
struct rcu_table *t;
- int cnt = 0;
- int ret;
+ struct rcu_ht_node *node;
+ unsigned long hash, reverse_hash;
- /*
- * Mutual exclusion with resize operations, but leave add/steal execute
- * concurrently. This is OK because we operate only on the heads.
- */
- ret = pthread_mutex_lock(&ht->resize_mutex);
- assert(!ret);
+ hash = ht->hash_fct(key, key_len, ht->hash_seed);
+ reverse_hash = bit_reverse_ulong(hash);
t = rcu_dereference(ht->t);
- /* no read barrier needed, because no concurrency with resize */
- for (i = 0; i < t->size; i++) {
- rcu_read_lock();
- prev = &t->tbl[i];
- /*
- * Cut the head. After that, we own the first element.
- */
- node = rcu_xchg_pointer(prev, NULL);
- if (!node) {
- rcu_read_unlock();
- continue;
+ node = rcu_dereference(t->tbl[hash & (t->size - 1)]);
+ for (;;) {
+ if (unlikely(!node))
+ break;
+ if (unlikely(node->reverse_hash > reverse_hash)) {
+ node = NULL;
+ break;
}
- /*
- * We manage a list shared with concurrent writers and readers.
- * Note that a concurrent add may or may not be deleted by us,
- * depending if it arrives before or after the head is cut.
- * "node" points to our first node. Remove first elements
- * iteratively.
- */
- for (;;) {
- inext = NULL;
- prev = &node->next;
- if (prev)
- inext = rcu_xchg_pointer(prev, NULL);
- /*
- * "node" is the first element of the list we have cut.
- * We therefore own it, no concurrent writer may delete
- * it. There can only be concurrent lookups. Concurrent
- * add can only be done on a bucket head, but we've cut
- * it already. inext is also owned by us, because we
- * have exchanged it for "NULL". It will therefore be
- * safe to use it after a G.P.
- */
- rcu_read_unlock();
- if (node->data)
- call_rcu(ht->free_fct, node->data);
- call_rcu(free, node);
- cnt++;
- if (likely(!inext))
+ if (!ht->compare_fct(node->key, node->key_len, key, key_len)) {
+ if (likely(!is_removed(rcu_dereference(node->next)))
+ && likely(!node->dummy))
break;
- rcu_read_lock();
- node = inext;
}
+ node = clear_flag(rcu_dereference(node->next));
}
+ assert(!node || !node->dummy);
+ return node;
+}
+
+void ht_add(struct rcu_ht *ht, struct rcu_ht_node *node)
+{
+ struct rcu_table *t;
+
+ node->hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
+ node->reverse_hash = bit_reverse_ulong((unsigned long) node->hash);
+
+ t = rcu_dereference(ht->t);
+ (void) _ht_add(ht, t, node, 0);
+}
+
+int ht_add_unique(struct rcu_ht *ht, struct rcu_ht_node *node)
+{
+ struct rcu_table *t;
+
+ node->hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
+ node->reverse_hash = bit_reverse_ulong((unsigned long) node->hash);
+
+ t = rcu_dereference(ht->t);
+ return _ht_add(ht, t, node, 1);
+}
+
+int ht_remove(struct rcu_ht *ht, struct rcu_ht_node *node)
+{
+ struct rcu_table *t;
+
+ t = rcu_dereference(ht->t);
+ return _ht_remove(ht, t, node);
+}
+
+static
+int ht_delete_dummy(struct rcu_ht *ht)
+{
+ struct rcu_table *t;
+ struct rcu_ht_node *node;
+ unsigned long i;
- ret = pthread_mutex_unlock(&ht->resize_mutex);
- assert(!ret);
- return cnt;
+ t = ht->t;
+ /* Check that the table is empty */
+ node = t->tbl[0];
+ do {
+ if (!node->dummy)
+ return -EPERM;
+ node = node->next;
+ assert(!is_removed(node));
+ } while (node);
+ /* Internal sanity check: all nodes left should be dummy */
+ for (i = 0; i < t->size; i++) {
+ assert(t->tbl[i]->dummy);
+ free(t->tbl[i]);
+ }
+ return 0;
}
/*
{
int ret;
- ret = ht_delete_all(ht);
+ ret = ht_delete_dummy(ht);
+ if (ret)
+ return ret;
free(ht->t);
free(ht);
return ret;
}
-static void ht_resize_grow(struct rcu_ht *ht)
+void ht_count_nodes(struct rcu_ht *ht,
+ unsigned long *count,
+ unsigned long *removed)
{
- unsigned long i, new_size, old_size;
- struct rcu_table *new_t, *old_t;
- struct rcu_ht_node *node, *new_node, *tmp;
- unsigned long hash;
-
- old_t = ht->t;
- old_size = old_t->size;
-
- if (old_size == MAX_HT_BUCKETS)
- return;
-
- new_size = old_size << 1;
- new_t = calloc(1, sizeof(struct rcu_table)
- + (new_size * sizeof(struct rcu_ht_node *)));
- new_t->size = new_size;
-
- for (i = 0; i < old_size; i++) {
- /*
- * Re-hash each entry, insert in new table.
- * It's important that a reader looking for a key _will_ find it
- * if it's in the table.
- * Copy each node. (just the node, not ->data)
- */
- node = old_t->tbl[i];
- while (node) {
- hash = ht->hash_fct(node->key, ht->keylen, ht->hashseed)
- % new_size;
- new_node = malloc(sizeof(struct rcu_ht_node));
- new_node->key = node->key;
- new_node->data = node->data;
- new_node->flags = node->flags;
- new_node->next = new_t->tbl[hash]; /* link to first */
- new_t->tbl[hash] = new_node; /* add to head */
- node = node->next;
- }
- }
+ struct rcu_table *t;
+ struct rcu_ht_node *node, *next;
- /* Changing table and size atomically wrt lookups */
- rcu_assign_pointer(ht->t, new_t);
+ *count = 0;
+ *removed = 0;
- /* Ensure all concurrent lookups use new size and table */
- synchronize_rcu();
+ t = rcu_dereference(ht->t);
+ /* Check that the table is empty */
+ node = rcu_dereference(t->tbl[0]);
+ do {
+ next = rcu_dereference(node->next);
+ if (is_removed(next)) {
+ assert(!node->dummy);
+ (*removed)++;
+ } else if (!node->dummy)
+ (*count)++;
+ node = clear_flag(next);
+ } while (node);
+}
- for (i = 0; i < old_size; i++) {
- node = old_t->tbl[i];
- while (node) {
- tmp = node->next;
- free(node);
- node = tmp;
- }
- }
- free(old_t);
+static
+void ht_free_table_cb(struct rcu_head *head)
+{
+ struct rcu_table *t =
+ caa_container_of(head, struct rcu_table, head);
+ free(t);
}
-static void ht_resize_shrink(struct rcu_ht *ht)
+/* called with resize mutex held */
+static
+void _do_ht_resize(struct rcu_ht *ht)
{
- unsigned long i, new_size;
+ unsigned long new_size, old_size;
struct rcu_table *new_t, *old_t;
- struct rcu_ht_node **prev, *node;
old_t = ht->t;
- if (old_t->size == 1)
- return;
-
- new_size = old_t->size >> 1;
-
- for (i = 0; i < new_size; i++) {
- /* Link end with first entry of i + new_size */
- prev = &old_t->tbl[i];
- node = *prev;
- while (node) {
- prev = &node->next;
- node = *prev;
- }
- *prev = old_t->tbl[i + new_size];
- }
- smp_wmb(); /* write links before changing size */
- STORE_SHARED(old_t->size, new_size);
+ old_size = old_t->size;
- /* Ensure all concurrent lookups use new size */
- synchronize_rcu();
+ new_size = CMM_LOAD_SHARED(old_t->resize_target);
+ dbg_printf("rculfhash: resize from %lu to %lu buckets\n",
+ old_size, new_size);
+ if (old_size == new_size)
+ return;
+ new_t = malloc(sizeof(struct rcu_table)
+ + (new_size * sizeof(struct rcu_ht_node *)));
+ assert(new_size > old_size);
+ memcpy(&new_t->tbl, &old_t->tbl,
+ old_size * sizeof(struct rcu_ht_node *));
+ init_table(ht, new_t, old_size, new_size - old_size);
+ /* Changing table and size atomically wrt lookups */
+ rcu_assign_pointer(ht->t, new_t);
+ ht->ht_call_rcu(&old_t->head, ht_free_table_cb);
+}
- new_t = realloc(old_t, sizeof(struct rcu_table)
- + (new_size * sizeof(struct rcu_ht_node *)));
- /* shrinking, pointers should not move */
- assert(new_t == old_t);
+static
+unsigned long resize_target_update(struct rcu_table *t,
+ int growth_order)
+{
+ return _uatomic_max(&t->resize_target,
+ t->size << growth_order);
}
-/*
- * growth: >0: *2, <0: /2
- */
void ht_resize(struct rcu_ht *ht, int growth)
{
- int ret;
-
- ret = pthread_mutex_lock(&ht->resize_mutex);
- assert(!ret);
- STORE_SHARED(ht->resize_ongoing, 1);
- synchronize_rcu();
- /* All add/remove are waiting on the mutex. */
- if (growth > 0)
- ht_resize_grow(ht);
- else if (growth < 0)
- ht_resize_shrink(ht);
- smp_mb();
- STORE_SHARED(ht->resize_ongoing, 0);
- ret = pthread_mutex_unlock(&ht->resize_mutex);
- assert(!ret);
+ struct rcu_table *t = rcu_dereference(ht->t);
+ unsigned long target_size;
+
+ target_size = resize_target_update(t, growth);
+ if (t->size < target_size) {
+ CMM_STORE_SHARED(t->resize_initiated, 1);
+ pthread_mutex_lock(&ht->resize_mutex);
+ _do_ht_resize(ht);
+ pthread_mutex_unlock(&ht->resize_mutex);
+ }
}
-/*
- * Expects keys <= than pointer size to be encoded in the pointer itself.
- */
-uint32_t ht_jhash(void *key, uint32_t length, uint32_t initval)
+static
+void do_resize_cb(struct rcu_head *head)
{
- uint32_t ret;
- void *vkey;
+ struct rcu_resize_work *work =
+ caa_container_of(head, struct rcu_resize_work, head);
+ struct rcu_ht *ht = work->ht;
+
+ pthread_mutex_lock(&ht->resize_mutex);
+ _do_ht_resize(ht);
+ pthread_mutex_unlock(&ht->resize_mutex);
+ free(work);
+}
- if (length <= sizeof(void *))
- vkey = &key;
- else
- vkey = key;
- ret = jhash(vkey, length, initval);
- return ret;
+static
+void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth)
+{
+ struct rcu_resize_work *work;
+ unsigned long target_size;
+
+ target_size = resize_target_update(t, growth);
+ if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
+ work = malloc(sizeof(*work));
+ work->ht = ht;
+ ht->ht_call_rcu(&work->head, do_resize_cb);
+ CMM_STORE_SHARED(t->resize_initiated, 1);
+ }
}