| 1 | // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 2 | // |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | /* |
| 6 | * This example shows how to add unique nodes into a RCU lock-free hash |
| 7 | * table. We use a "seqnum" field to show which node is staying in the |
| 8 | * hash table. This hash table requires using a 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 | int seqnum; /* Our node sequence number */ |
| 26 | struct cds_lfht_node node; /* Chaining in hash table */ |
| 27 | }; |
| 28 | |
| 29 | static |
| 30 | int match(struct cds_lfht_node *ht_node, const void *_key) |
| 31 | { |
| 32 | struct mynode *node = |
| 33 | caa_container_of(ht_node, struct mynode, node); |
| 34 | const int *key = _key; |
| 35 | |
| 36 | return *key == node->value; |
| 37 | } |
| 38 | |
| 39 | int main(void) |
| 40 | { |
| 41 | int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */ |
| 42 | struct cds_lfht *ht; /* Hash table */ |
| 43 | unsigned int i; |
| 44 | int ret = 0, seqnum = 0; |
| 45 | uint32_t seed; |
| 46 | struct cds_lfht_iter iter; /* For iteration on hash table */ |
| 47 | struct cds_lfht_node *ht_node; |
| 48 | struct mynode *node; |
| 49 | |
| 50 | /* |
| 51 | * Each thread need using RCU read-side need to be explicitly |
| 52 | * registered. |
| 53 | */ |
| 54 | urcu_memb_register_thread(); |
| 55 | |
| 56 | /* Use time as seed for hash table hashing. */ |
| 57 | seed = (uint32_t) time(NULL); |
| 58 | |
| 59 | /* |
| 60 | * Allocate hash table. |
| 61 | */ |
| 62 | ht = cds_lfht_new_flavor(1, 1, 0, |
| 63 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, |
| 64 | &urcu_memb_flavor, NULL); |
| 65 | if (!ht) { |
| 66 | printf("Error allocating hash table\n"); |
| 67 | ret = -1; |
| 68 | goto end; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Add nodes to hash table. |
| 73 | */ |
| 74 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { |
| 75 | unsigned long hash; |
| 76 | int value; |
| 77 | |
| 78 | node = malloc(sizeof(*node)); |
| 79 | if (!node) { |
| 80 | ret = -1; |
| 81 | goto end; |
| 82 | } |
| 83 | |
| 84 | cds_lfht_node_init(&node->node); |
| 85 | value = values[i]; |
| 86 | node->value = value; |
| 87 | node->seqnum = seqnum++; |
| 88 | hash = jhash(&value, sizeof(value), seed); |
| 89 | |
| 90 | /* |
| 91 | * cds_lfht_add() needs to be called from RCU read-side |
| 92 | * critical section. |
| 93 | */ |
| 94 | urcu_memb_read_lock(); |
| 95 | ht_node = cds_lfht_add_unique(ht, hash, match, &value, |
| 96 | &node->node); |
| 97 | /* |
| 98 | * cds_lfht_add_unique() returns the added node if not |
| 99 | * already present, else returns the node matching the |
| 100 | * key. |
| 101 | */ |
| 102 | if (ht_node != &node->node) { |
| 103 | /* |
| 104 | * We found a match. Therefore, to ensure we |
| 105 | * don't add duplicates, cds_lfht_add_unique() |
| 106 | * acted as a RCU lookup, returning the found |
| 107 | * match. It did not add the new node to the |
| 108 | * hash table, so we can free it on the spot. |
| 109 | */ |
| 110 | printf("Not adding duplicate (key: %d, seqnum: %d)\n", |
| 111 | node->value, node->seqnum); |
| 112 | free(node); |
| 113 | } else { |
| 114 | printf("Add (key: %d, seqnum: %d)\n", |
| 115 | node->value, node->seqnum); |
| 116 | } |
| 117 | urcu_memb_read_unlock(); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Iterate over each hash table node. Those will appear in |
| 122 | * random order, depending on the hash seed. Iteration needs to |
| 123 | * be performed within RCU read-side critical section. |
| 124 | */ |
| 125 | printf("hash table content (random order):"); |
| 126 | urcu_memb_read_lock(); |
| 127 | cds_lfht_for_each_entry(ht, &iter, node, node) { |
| 128 | printf(" (key: %d, seqnum: %d)", |
| 129 | node->value, node->seqnum); |
| 130 | } |
| 131 | urcu_memb_read_unlock(); |
| 132 | printf("\n"); |
| 133 | |
| 134 | end: |
| 135 | urcu_memb_unregister_thread(); |
| 136 | return ret; |
| 137 | } |