rcuja: various fixes
[userspace-rcu.git] / rcuja / rcuja-shadow-nodes.c
index cfc3aacebe311d5eccca6bc950957e25a57aa64a..3a5de33669a9f53db204e6e473c9e928f48803c7 100644 (file)
@@ -168,19 +168,19 @@ unsigned long hash_pointer(const void *_key, unsigned long 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);
 }
 
 __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 *node)
 {
        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;
 
@@ -195,11 +195,12 @@ struct rcu_ja_shadow_node *rcuja_shadow_lookup_lock(struct cds_lfht *ht,
                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;
        }
@@ -209,19 +210,21 @@ rcu_unlock:
 }
 
 __attribute__((visibility("protected")))
-void rcuja_shadow_unlock(struct rcu_ja_shadow_node *shadow_node)
+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_inode *new_node,
+               struct cds_ja_shadow_node *inherit_from)
 {
-       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;
 
@@ -229,15 +232,27 @@ int rcuja_shadow_set(struct cds_lfht *ht,
        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);
+       }
 
        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);
        flavor->read_unlock();
 
@@ -248,27 +263,57 @@ int rcuja_shadow_set(struct cds_lfht *ht,
        return 0;
 }
 
+static
+void free_shadow_node(struct rcu_head *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 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->node);
        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);
+       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 cds_ja_inode *node,
+               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();
+
        cds_lfht_lookup(ht, hash_pointer(node, hash_seed),
                        match_pointer, node, &iter);
        lookup_node = cds_lfht_iter_get_node(&iter);
@@ -276,10 +321,14 @@ int rcuja_shadow_clear_and_free_node(struct cds_lfht *ht,
                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
@@ -288,16 +337,66 @@ int rcuja_shadow_clear_and_free_node(struct cds_lfht *ht,
         */
        ret = cds_lfht_del(ht, lookup_node);
        if (!ret) {
-               flavor->update_call_rcu(&shadow_node->head, free_shadow_node_and_node);
+               if (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);
+                       }
+               } 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);
+                       }
+               }
+       }
+       if (lookup_shadow) {
+               lockret = pthread_mutex_unlock(shadow_node->lock);
+               assert(!lockret);
        }
-       lockret = pthread_mutex_unlock(&shadow_node->lock);
-       assert(!lockret);
 rcu_unlock:
        flavor->read_unlock();
 
        return ret;
 }
 
+/*
+ * Delete all shadow nodes and nodes from hash table, along with their
+ * associated lock.
+ */
+__attribute__((visibility("protected")))
+void rcuja_shadow_prune(struct cds_lfht *ht,
+               unsigned int flags)
+{
+       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);
+       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) {
+                       assert((flags & RCUJA_SHADOW_CLEAR_FREE_NODE)
+                               && (flags & RCUJA_SHADOW_CLEAR_FREE_LOCK));
+                       flavor->update_call_rcu(&shadow_node->head,
+                               free_shadow_node_and_node_and_lock);
+               }
+               lockret = pthread_mutex_unlock(shadow_node->lock);
+               assert(!lockret);
+       }
+       flavor->read_unlock();
+}
+
 __attribute__((visibility("protected")))
 struct cds_lfht *rcuja_create_ht(const struct rcu_flavor_struct *flavor)
 {
@@ -307,12 +406,9 @@ struct cds_lfht *rcuja_create_ht(const struct rcu_flavor_struct *flavor)
 }
 
 __attribute__((visibility("protected")))
-void rcuja_delete_ht(struct cds_lfht *ht)
+int rcuja_delete_ht(struct cds_lfht *ht)
 {
-       int ret;
-
-       ret = cds_lfht_destroy(ht, NULL);
-       assert(!ret);
+       return cds_lfht_destroy(ht, NULL);
 }
 
 __attribute__((constructor))
This page took 0.026401 seconds and 4 git commands to generate.