| 1 | // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 2 | // |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | /* |
| 6 | * This example shows how to use cds_lfht_destroy() to clear memory used |
| 7 | * by a a RCU lock-free hash table. This hash table requires using a |
| 8 | * RCU scheme. |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <time.h> |
| 14 | |
| 15 | #include <urcu/urcu-memb.h> /* RCU flavor */ |
| 16 | #include <urcu/rculfhash.h> /* RCU Lock-free hash table */ |
| 17 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ |
| 18 | #include "jhash.h" /* Example hash function */ |
| 19 | |
| 20 | /* |
| 21 | * Nodes populated into the hash table. |
| 22 | */ |
| 23 | struct mynode { |
| 24 | int value; /* Node content */ |
| 25 | struct cds_lfht_node node; /* Chaining in hash table */ |
| 26 | struct rcu_head rcu_head; /* For call_rcu() */ |
| 27 | }; |
| 28 | |
| 29 | static |
| 30 | void free_node(struct rcu_head *head) |
| 31 | { |
| 32 | struct mynode *node = caa_container_of(head, struct mynode, rcu_head); |
| 33 | |
| 34 | free(node); |
| 35 | } |
| 36 | |
| 37 | int main(void) |
| 38 | { |
| 39 | int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */ |
| 40 | struct cds_lfht *ht; /* Hash table */ |
| 41 | unsigned int i; |
| 42 | int ret = 0; |
| 43 | uint32_t seed; |
| 44 | struct cds_lfht_iter iter; /* For iteration on hash table */ |
| 45 | struct cds_lfht_node *ht_node; |
| 46 | struct mynode *node; |
| 47 | |
| 48 | /* |
| 49 | * Each thread need using RCU read-side need to be explicitly |
| 50 | * registered. |
| 51 | */ |
| 52 | urcu_memb_register_thread(); |
| 53 | |
| 54 | /* Use time as seed for hash table hashing. */ |
| 55 | seed = (uint32_t) time(NULL); |
| 56 | |
| 57 | /* |
| 58 | * Allocate hash table. |
| 59 | */ |
| 60 | ht = cds_lfht_new_flavor(1, 1, 0, |
| 61 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, |
| 62 | &urcu_memb_flavor, NULL); |
| 63 | if (!ht) { |
| 64 | printf("Error allocating hash table\n"); |
| 65 | ret = -1; |
| 66 | goto end; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Add nodes to hash table. |
| 71 | */ |
| 72 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { |
| 73 | unsigned long hash; |
| 74 | int value; |
| 75 | |
| 76 | node = malloc(sizeof(*node)); |
| 77 | if (!node) { |
| 78 | ret = -1; |
| 79 | goto end; |
| 80 | } |
| 81 | |
| 82 | cds_lfht_node_init(&node->node); |
| 83 | value = values[i]; |
| 84 | node->value = value; |
| 85 | hash = jhash(&value, sizeof(value), seed); |
| 86 | |
| 87 | /* |
| 88 | * cds_lfht_add() needs to be called from RCU read-side |
| 89 | * critical section. |
| 90 | */ |
| 91 | urcu_memb_read_lock(); |
| 92 | cds_lfht_add(ht, hash, &node->node); |
| 93 | urcu_memb_read_unlock(); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Iterate over each hash table node. Those will appear in |
| 98 | * random order, depending on the hash seed. Iteration needs to |
| 99 | * be performed within RCU read-side critical section. |
| 100 | */ |
| 101 | printf("hash table content (random order):"); |
| 102 | urcu_memb_read_lock(); |
| 103 | cds_lfht_for_each_entry(ht, &iter, node, node) { |
| 104 | printf(" %d", node->value); |
| 105 | } |
| 106 | urcu_memb_read_unlock(); |
| 107 | printf("\n"); |
| 108 | |
| 109 | |
| 110 | /* |
| 111 | * Make sure all hash table nodes are removed before destroying. |
| 112 | */ |
| 113 | printf("removing all nodes:"); |
| 114 | urcu_memb_read_lock(); |
| 115 | cds_lfht_for_each_entry(ht, &iter, node, node) { |
| 116 | ht_node = cds_lfht_iter_get_node(&iter); |
| 117 | ret = cds_lfht_del(ht, ht_node); |
| 118 | printf(" %d", node->value); |
| 119 | if (ret) { |
| 120 | printf(" (concurrently deleted)"); |
| 121 | } else { |
| 122 | urcu_memb_call_rcu(&node->rcu_head, free_node); |
| 123 | } |
| 124 | } |
| 125 | urcu_memb_read_unlock(); |
| 126 | printf("\n"); |
| 127 | |
| 128 | /* |
| 129 | * cds_lfht_destroy() must be called from a very specific |
| 130 | * context: it needs to be called from a registered RCU reader |
| 131 | * thread. However, this thread should _not_ be within a RCU |
| 132 | * read-side critical section. Also, it should _not_ be called |
| 133 | * from a call_rcu thread. |
| 134 | */ |
| 135 | ret = cds_lfht_destroy(ht, NULL); |
| 136 | if (ret) { |
| 137 | printf("Destroying hash table failed\n"); |
| 138 | } |
| 139 | end: |
| 140 | urcu_memb_unregister_thread(); |
| 141 | return ret; |
| 142 | } |