rcuja: share lock across all nodes with same key
[userspace-rcu.git] / rcuja / rcuja-shadow-nodes.c
index b737e26711504d9cba41cd8ef478574c491a5377..5ef68d58631bd5d7a673496f78e755ce78b97a90 100644 (file)
 #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>
@@ -190,9 +181,11 @@ struct rcu_ja_shadow_node *rcuja_shadow_lookup_lock(struct cds_lfht *ht,
        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;
 
-       rcu_read_lock();
+       flavor = cds_lfht_rcu_flavor(ht);
+       flavor->read_lock();
        cds_lfht_lookup(ht, hash_pointer(node, hash_seed),
                        match_pointer, node, &iter);
 
@@ -203,15 +196,15 @@ struct rcu_ja_shadow_node *rcuja_shadow_lookup_lock(struct cds_lfht *ht,
        }
        shadow_node = caa_container_of(lookup_node,
                        struct rcu_ja_shadow_node, ht_node);
-       ret = pthread_mutex_lock(&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;
 }
 
@@ -220,31 +213,46 @@ void rcuja_shadow_unlock(struct rcu_ja_shadow_node *shadow_node)
 {
        int ret;
 
-       ret = pthread_mutex_unlock(&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 rcu_ja_node *new_node,
+               struct rcu_ja_shadow_node *inherit_from)
 {
        struct rcu_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;
 
-       shadow_node->node = node;
-       pthread_mutex_init(&shadow_node->lock, NULL);
+       shadow_node->node = new_node;
+       /*
+        * Lock can be inherited from previous node at this position.
+        */
+       if (inherit_from) {
+               shadow_node->lock = inherit_from->lock;
+       } else {
+               shadow_node->lock = calloc(sizeof(*shadow_node->lock), 1);
+               if (!shadow_node->lock) {
+                       free(shadow_node);
+                       return -ENOMEM;
+               }
+               pthread_mutex_init(shadow_node->lock, NULL);
+       }
 
-       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, hash_seed),
                        match_pointer,
-                       node,
+                       new_node,
                        &shadow_node->ht_node);
-       rcu_read_unlock();
+       flavor->read_unlock();
 
        if (ret_node != &shadow_node->ht_node) {
                free(shadow_node);
@@ -262,16 +270,29 @@ void free_shadow_node_and_node(struct rcu_head *head)
        free(shadow_node);
 }
 
+static
+void free_shadow_node_and_node_and_lock(struct rcu_head *head)
+{
+       struct rcu_ja_shadow_node *shadow_node =
+               caa_container_of(head, struct rcu_ja_shadow_node, head);
+       free(shadow_node->node);
+       free(shadow_node->lock);
+       free(shadow_node);
+}
+
 __attribute__((visibility("protected")))
-int rcuja_shadow_clear_and_free_node(struct cds_lfht *ht,
-               struct rcu_ja_node *node)
+int rcuja_shadow_clear(struct cds_lfht *ht,
+               struct rcu_ja_node *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;
 
-       rcu_read_lock();
+       flavor = cds_lfht_rcu_flavor(ht);
+       flavor->read_lock();
        cds_lfht_lookup(ht, hash_pointer(node, hash_seed),
                        match_pointer, node, &iter);
        lookup_node = cds_lfht_iter_get_node(&iter);
@@ -281,7 +302,7 @@ int rcuja_shadow_clear_and_free_node(struct cds_lfht *ht,
        }
        shadow_node = caa_container_of(lookup_node,
                        struct rcu_ja_shadow_node, ht_node);
-       lockret = pthread_mutex_lock(&shadow_node->lock);
+       lockret = pthread_mutex_lock(shadow_node->lock);
        assert(!lockret);
 
        /*
@@ -291,22 +312,29 @@ int rcuja_shadow_clear_and_free_node(struct cds_lfht *ht,
         */
        ret = cds_lfht_del(ht, lookup_node);
        if (!ret) {
-               call_rcu(&shadow_node->head, free_shadow_node_and_node);
+               assert(flags & RCUJA_SHADOW_CLEAR_FREE_NODE);
+               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);
+               }
        }
-       lockret = pthread_mutex_unlock(&shadow_node->lock);
+       lockret = pthread_mutex_unlock(shadow_node->lock);
        assert(!lockret);
 rcu_unlock:
-       rcu_read_unlock();
+       flavor->read_unlock();
 
        return ret;
 }
 
 __attribute__((visibility("protected")))
-struct cds_lfht *rcuja_create_ht(void)
+struct cds_lfht *rcuja_create_ht(const struct rcu_flavor_struct *flavor)
 {
-       return cds_lfht_new(1, 1, 0,
+       return _cds_lfht_new(1, 1, 0,
                CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
-               NULL);
+               NULL, flavor, NULL);
 }
 
 __attribute__((visibility("protected")))
This page took 0.026944 seconds and 4 git commands to generate.