*
* Userspace RCU library - RCU Judy Array Shadow Node Hash Table
*
- * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2012-2013 - 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
#include <stdint.h>
#include <errno.h>
#include <limits.h>
-
-/*
- * The hash table used by judy array updates only for the shadow node
- * mapping rely on standard urcu_mb flavor. It does not put any
- * requirement on the RCU flavor used by applications using the judy
- * array.
- */
-#include <urcu.h>
-
#include <urcu/rcuja.h>
#include <urcu/compiler.h>
#include <urcu/arch.h>
#include <time.h>
#include "rcuja-internal.h"
-#include "bitfield.h"
static unsigned long hash_seed;
static
int match_pointer(struct cds_lfht_node *node, const void *key)
{
- struct rcu_ja_shadow_node *shadow =
- caa_container_of(node, struct rcu_ja_shadow_node, ht_node);
+ struct cds_ja_shadow_node *shadow =
+ caa_container_of(node, struct cds_ja_shadow_node, ht_node);
- return (key == shadow->node);
+ return (key == shadow->node_flag);
}
__attribute__((visibility("protected")))
-struct rcu_ja_shadow_node *rcuja_shadow_lookup_lock(struct cds_lfht *ht,
- struct rcu_ja_node *node)
+struct cds_ja_shadow_node *rcuja_shadow_lookup_lock(struct cds_lfht *ht,
+ struct cds_ja_inode_flag *node_flag)
{
struct cds_lfht_iter iter;
struct cds_lfht_node *lookup_node;
- struct rcu_ja_shadow_node *shadow_node;
+ struct cds_ja_shadow_node *shadow_node;
+ const struct rcu_flavor_struct *flavor;
int ret;
- rcu_read_lock();
- cds_lfht_lookup(ht, hash_pointer(node, hash_seed),
- match_pointer, node, &iter);
+ flavor = cds_lfht_rcu_flavor(ht);
+ flavor->read_lock();
+ cds_lfht_lookup(ht, hash_pointer(node_flag, hash_seed),
+ match_pointer, node_flag, &iter);
lookup_node = cds_lfht_iter_get_node(&iter);
if (!lookup_node) {
goto rcu_unlock;
}
shadow_node = caa_container_of(lookup_node,
- struct rcu_ja_shadow_node, ht_node);
- ret = pthread_mutex_lock(&shadow_node->lock);
+ struct cds_ja_shadow_node, ht_node);
+ dbg_printf("Lock %p\n", shadow_node->lock);
+ ret = pthread_mutex_lock(shadow_node->lock);
assert(!ret);
if (cds_lfht_is_node_deleted(lookup_node)) {
- ret = pthread_mutex_unlock(&shadow_node->lock);
+ ret = pthread_mutex_unlock(shadow_node->lock);
assert(!ret);
shadow_node = NULL;
}
rcu_unlock:
- rcu_read_unlock();
+ flavor->read_unlock();
return shadow_node;
}
-void rcuja_shadow_unlock(struct rcu_ja_shadow_node *shadow_node)
+__attribute__((visibility("protected")))
+void rcuja_shadow_unlock(struct cds_ja_shadow_node *shadow_node)
{
int ret;
- ret = pthread_mutex_unlock(&shadow_node->lock);
+ dbg_printf("Unlock %p\n", shadow_node->lock);
+ ret = pthread_mutex_unlock(shadow_node->lock);
assert(!ret);
}
__attribute__((visibility("protected")))
-int rcuja_shadow_set(struct cds_lfht *ht,
- struct rcu_ja_node *node)
+struct cds_ja_shadow_node *rcuja_shadow_set(struct cds_lfht *ht,
+ struct cds_ja_inode_flag *new_node_flag,
+ struct cds_ja_shadow_node *inherit_from,
+ struct cds_ja *ja, int level)
{
- struct rcu_ja_shadow_node *shadow_node;
+ struct cds_ja_shadow_node *shadow_node;
struct cds_lfht_node *ret_node;
+ const struct rcu_flavor_struct *flavor;
shadow_node = calloc(sizeof(*shadow_node), 1);
if (!shadow_node)
- return -ENOMEM;
+ return NULL;
- shadow_node->node = node;
- pthread_mutex_init(&shadow_node->lock, NULL);
+ shadow_node->node_flag = new_node_flag;
+ shadow_node->ja = ja;
+ /*
+ * Lock can be inherited from previous node at this position.
+ */
+ if (inherit_from) {
+ shadow_node->lock = inherit_from->lock;
+ shadow_node->level = inherit_from->level;
+ } else {
+ shadow_node->lock = calloc(sizeof(*shadow_node->lock), 1);
+ if (!shadow_node->lock) {
+ free(shadow_node);
+ return NULL;
+ }
+ pthread_mutex_init(shadow_node->lock, NULL);
+ shadow_node->level = level;
+ }
- rcu_read_lock();
+ flavor = cds_lfht_rcu_flavor(ht);
+ flavor->read_lock();
ret_node = cds_lfht_add_unique(ht,
- hash_pointer(node, hash_seed),
+ hash_pointer(new_node_flag, hash_seed),
match_pointer,
- node,
+ new_node_flag,
&shadow_node->ht_node);
- rcu_read_unlock();
+ flavor->read_unlock();
if (ret_node != &shadow_node->ht_node) {
free(shadow_node);
- return -EEXIST;
+ return NULL;
}
- return 0;
+ return shadow_node;
}
static
void free_shadow_node(struct rcu_head *head)
{
- struct rcu_ja_shadow_node *shadow_node =
- caa_container_of(head, struct rcu_ja_shadow_node, head);
+ struct cds_ja_shadow_node *shadow_node =
+ caa_container_of(head, struct cds_ja_shadow_node, head);
+ free(shadow_node);
+}
+
+static
+void free_shadow_node_and_node(struct rcu_head *head)
+{
+ struct cds_ja_shadow_node *shadow_node =
+ caa_container_of(head, struct cds_ja_shadow_node, head);
+ free_cds_ja_node(shadow_node->ja, ja_node_ptr(shadow_node->node_flag));
+ free(shadow_node);
+}
+
+static
+void free_shadow_node_and_lock(struct rcu_head *head)
+{
+ struct cds_ja_shadow_node *shadow_node =
+ caa_container_of(head, struct cds_ja_shadow_node, head);
+ free(shadow_node->lock);
+ free(shadow_node);
+}
+
+static
+void free_shadow_node_and_node_and_lock(struct rcu_head *head)
+{
+ struct cds_ja_shadow_node *shadow_node =
+ caa_container_of(head, struct cds_ja_shadow_node, head);
+ assert(shadow_node->level);
+ free_cds_ja_node(shadow_node->ja, ja_node_ptr(shadow_node->node_flag));
+ free(shadow_node->lock);
free(shadow_node);
}
__attribute__((visibility("protected")))
int rcuja_shadow_clear(struct cds_lfht *ht,
- struct rcu_ja_node *node)
+ struct cds_ja_inode_flag *node_flag,
+ struct cds_ja_shadow_node *shadow_node,
+ unsigned int flags)
{
struct cds_lfht_iter iter;
struct cds_lfht_node *lookup_node;
- struct rcu_ja_shadow_node *shadow_node;
+ const struct rcu_flavor_struct *flavor;
int ret, lockret;
+ int lookup_shadow = 0;
+
+ flavor = cds_lfht_rcu_flavor(ht);
+ flavor->read_lock();
- rcu_read_lock();
- cds_lfht_lookup(ht, hash_pointer(node, hash_seed),
- match_pointer, node, &iter);
+ cds_lfht_lookup(ht, hash_pointer(node_flag, hash_seed),
+ match_pointer, node_flag, &iter);
lookup_node = cds_lfht_iter_get_node(&iter);
if (!lookup_node) {
ret = -ENOENT;
goto rcu_unlock;
}
- shadow_node = caa_container_of(lookup_node,
- struct rcu_ja_shadow_node, ht_node);
- lockret = pthread_mutex_lock(&shadow_node->lock);
- assert(!lockret);
+
+ if (!shadow_node) {
+ shadow_node = caa_container_of(lookup_node,
+ struct cds_ja_shadow_node, ht_node);
+ lockret = pthread_mutex_lock(shadow_node->lock);
+ assert(!lockret);
+ lookup_shadow = 1;
+ }
/*
* Holding the mutex across deletion, and by also re-checking if
* don't let RCU JA use a node being removed.
*/
ret = cds_lfht_del(ht, lookup_node);
- if (!ret) {
- call_rcu(&shadow_node->head, free_shadow_node);
+ if (ret)
+ goto unlock;
+ if ((flags & RCUJA_SHADOW_CLEAR_FREE_NODE)
+ && shadow_node->level) {
+ if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node_and_node_and_lock);
+ } else {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node_and_node);
+ }
+ } else {
+ if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node_and_lock);
+ } else {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node);
+ }
+ }
+unlock:
+ if (lookup_shadow) {
+ lockret = pthread_mutex_unlock(shadow_node->lock);
+ assert(!lockret);
}
- lockret = pthread_mutex_unlock(&shadow_node->lock);
- assert(!lockret);
rcu_unlock:
- rcu_read_unlock();
+ flavor->read_unlock();
return ret;
}
+/*
+ * Delete all shadow nodes and nodes from hash table, along with their
+ * associated lock.
+ */
__attribute__((visibility("protected")))
-struct cds_lfht *rcuja_create_ht(void)
+void rcuja_shadow_prune(struct cds_lfht *ht,
+ unsigned int flags)
{
- return cds_lfht_new(1, 1, 0,
- CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
- NULL);
+ const struct rcu_flavor_struct *flavor;
+ struct cds_ja_shadow_node *shadow_node;
+ struct cds_lfht_iter iter;
+ int ret, lockret;
+
+ flavor = cds_lfht_rcu_flavor(ht);
+ /*
+ * Read-side lock is needed to ensure hash table node existence
+ * vs concurrent resize.
+ */
+ flavor->read_lock();
+ cds_lfht_for_each_entry(ht, &iter, shadow_node, ht_node) {
+ lockret = pthread_mutex_lock(shadow_node->lock);
+ assert(!lockret);
+
+ ret = cds_lfht_del(ht, &shadow_node->ht_node);
+ if (ret)
+ goto unlock;
+ if ((flags & RCUJA_SHADOW_CLEAR_FREE_NODE)
+ && shadow_node->level) {
+ if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node_and_node_and_lock);
+ } else {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node_and_node);
+ }
+ } else {
+ if (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK) {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node_and_lock);
+ } else {
+ flavor->update_call_rcu(&shadow_node->head,
+ free_shadow_node);
+ }
+ }
+ unlock:
+ lockret = pthread_mutex_unlock(shadow_node->lock);
+ assert(!lockret);
+ }
+ flavor->read_unlock();
}
__attribute__((visibility("protected")))
-void rcuja_delete_ht(struct cds_lfht *ht)
+struct cds_lfht *rcuja_create_ht(const struct rcu_flavor_struct *flavor)
{
- int ret;
+ return _cds_lfht_new(1, 1, 0,
+ CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
+ NULL, flavor, NULL);
+}
- ret = cds_lfht_destroy(ht, NULL);
- assert(!ret);
+__attribute__((visibility("protected")))
+int rcuja_delete_ht(struct cds_lfht *ht)
+{
+ return cds_lfht_destroy(ht, NULL);
}
__attribute__((constructor))