X-Git-Url: https://git.lttng.org/?p=urcu.git;a=blobdiff_plain;f=doc%2Fexamples%2Frculfhash%2Fcds_lfht_del.c;h=f030ee17c6063491130333e5bcbf07989a49c137;hp=d5a726484a335de620b531a05df029e26643a18d;hb=83e334d03eaba62df373cf44298616458900078a;hpb=f7e7a1b86b06da9d2cef60e43039fa8c8ae6479a diff --git a/doc/examples/rculfhash/cds_lfht_del.c b/doc/examples/rculfhash/cds_lfht_del.c index d5a7264..f030ee1 100644 --- a/doc/examples/rculfhash/cds_lfht_del.c +++ b/doc/examples/rculfhash/cds_lfht_del.c @@ -18,7 +18,7 @@ #include #include -#include /* RCU flavor */ +#include /* RCU flavor */ #include /* RCU Lock-free hash table */ #include /* For CAA_ARRAY_SIZE */ #include "jhash.h" /* Example hash function */ @@ -32,15 +32,24 @@ struct mynode { struct rcu_head rcu_head; /* For call_rcu() */ }; +static int match(struct cds_lfht_node *ht_node, const void *_key) { struct mynode *node = caa_container_of(ht_node, struct mynode, node); - const unsigned int *key = _key; + const int *key = _key; return *key == node->value; } +static +void free_node(struct rcu_head *head) +{ + struct mynode *node = caa_container_of(head, struct mynode, rcu_head); + + free(node); +} + int main(int argc, char **argv) { int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */ @@ -57,7 +66,7 @@ int main(int argc, char **argv) * Each thread need using RCU read-side need to be explicitly * registered. */ - rcu_register_thread(); + urcu_memb_register_thread(); /* Use time as seed for hash table hashing. */ seed = (uint32_t) time(NULL); @@ -65,9 +74,9 @@ int main(int argc, char **argv) /* * Allocate hash table. */ - ht = cds_lfht_new(1, 1, 0, + ht = cds_lfht_new_flavor(1, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, - NULL); + &urcu_memb_flavor, NULL); if (!ht) { printf("Error allocating hash table\n"); ret = -1; @@ -96,9 +105,9 @@ int main(int argc, char **argv) * cds_lfht_add() needs to be called from RCU read-side * critical section. */ - rcu_read_lock(); + urcu_memb_read_lock(); cds_lfht_add(ht, hash, &node->node); - rcu_read_unlock(); + urcu_memb_read_unlock(); } /* @@ -107,11 +116,11 @@ int main(int argc, char **argv) * be performed within RCU read-side critical section. */ printf("hash table content (random order):"); - rcu_read_lock(); + urcu_memb_read_lock(); cds_lfht_for_each_entry(ht, &iter, node, node) { printf(" %d", node->value); } - rcu_read_unlock(); + urcu_memb_read_unlock(); printf("\n"); /* @@ -125,30 +134,35 @@ int main(int argc, char **argv) value = remove_values[i]; hash = jhash(&value, sizeof(value), seed); printf(" %d", value); - rcu_read_lock(); + urcu_memb_read_lock(); cds_lfht_lookup(ht, hash, match, &value, &iter); ht_node = cds_lfht_iter_get_node(&iter); if (ht_node) { ret = cds_lfht_del(ht, ht_node); if (ret) { printf(" (concurrently deleted)"); + } else { + struct mynode *del_node = + caa_container_of(ht_node, + struct mynode, node); + urcu_memb_call_rcu(&del_node->rcu_head, free_node); } } else { printf(" (not found)"); } - rcu_read_unlock(); + urcu_memb_read_unlock(); } printf("\n"); printf("hash table content (random order):"); - rcu_read_lock(); + urcu_memb_read_lock(); cds_lfht_for_each_entry(ht, &iter, node, node) { printf(" %d", node->value); } - rcu_read_unlock(); + urcu_memb_read_unlock(); printf("\n"); end: - rcu_unregister_thread(); + urcu_memb_unregister_thread(); return ret; }