| 1 | /* |
| 2 | * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 3 | * |
| 4 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 5 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 6 | * |
| 7 | * Permission is hereby granted to use or copy this program for any |
| 8 | * purpose, provided the above notices are retained on all copies. |
| 9 | * Permission to modify the code and to distribute modified code is |
| 10 | * granted, provided the above notices are retained, and a notice that |
| 11 | * the code was modified is included with the above copyright notice. |
| 12 | * |
| 13 | * This example shows how to add unique nodes into a RCU lock-free hash |
| 14 | * table. We use a "seqnum" field to show which node is staying in the |
| 15 | * hash table. This hash table requires using a RCU scheme. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <time.h> |
| 21 | |
| 22 | #include <urcu.h> /* RCU flavor */ |
| 23 | #include <urcu/rculfhash.h> /* RCU Lock-free hash table */ |
| 24 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ |
| 25 | #include "jhash.h" /* Example hash function */ |
| 26 | |
| 27 | /* |
| 28 | * Nodes populated into the hash table. |
| 29 | */ |
| 30 | struct mynode { |
| 31 | int value; /* Node content */ |
| 32 | int seqnum; /* Our node sequence number */ |
| 33 | struct cds_lfht_node node; /* Chaining in hash table */ |
| 34 | }; |
| 35 | |
| 36 | int match(struct cds_lfht_node *ht_node, const void *_key) |
| 37 | { |
| 38 | struct mynode *node = |
| 39 | caa_container_of(ht_node, struct mynode, node); |
| 40 | const unsigned int *key = _key; |
| 41 | |
| 42 | return *key == node->value; |
| 43 | } |
| 44 | |
| 45 | int main(int argc, char **argv) |
| 46 | { |
| 47 | int values[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */ |
| 48 | struct cds_lfht *ht; /* Hash table */ |
| 49 | unsigned int i; |
| 50 | int ret = 0, seqnum = 0; |
| 51 | uint32_t seed; |
| 52 | struct cds_lfht_iter iter; /* For iteration on hash table */ |
| 53 | struct cds_lfht_node *ht_node; |
| 54 | struct mynode *node; |
| 55 | |
| 56 | /* |
| 57 | * Each thread need using RCU read-side need to be explicitly |
| 58 | * registered. |
| 59 | */ |
| 60 | rcu_register_thread(); |
| 61 | |
| 62 | /* Use time as seed for hash table hashing. */ |
| 63 | seed = (uint32_t) time(NULL); |
| 64 | |
| 65 | /* |
| 66 | * Allocate hash table. |
| 67 | */ |
| 68 | ht = cds_lfht_new(1, 1, 0, |
| 69 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, |
| 70 | NULL); |
| 71 | if (!ht) { |
| 72 | printf("Error allocating hash table\n"); |
| 73 | ret = -1; |
| 74 | goto end; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Add nodes to hash table. |
| 79 | */ |
| 80 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { |
| 81 | unsigned long hash; |
| 82 | int value; |
| 83 | |
| 84 | node = malloc(sizeof(*node)); |
| 85 | if (!node) { |
| 86 | ret = -1; |
| 87 | goto end; |
| 88 | } |
| 89 | |
| 90 | cds_lfht_node_init(&node->node); |
| 91 | value = values[i]; |
| 92 | node->value = value; |
| 93 | node->seqnum = seqnum++; |
| 94 | hash = jhash(&value, sizeof(value), seed); |
| 95 | |
| 96 | /* |
| 97 | * cds_lfht_add() needs to be called from RCU read-side |
| 98 | * critical section. |
| 99 | */ |
| 100 | rcu_read_lock(); |
| 101 | ht_node = cds_lfht_add_unique(ht, hash, match, &value, |
| 102 | &node->node); |
| 103 | /* |
| 104 | * cds_lfht_add_unique() returns the added node if not |
| 105 | * already present, else returns the node matching the |
| 106 | * key. |
| 107 | */ |
| 108 | if (ht_node != &node->node) { |
| 109 | /* |
| 110 | * We found a match. Therefore, to ensure we |
| 111 | * don't add duplicates, cds_lfht_add_unique() |
| 112 | * acted as a RCU lookup, returning the found |
| 113 | * match. It did not add the new node to the |
| 114 | * hash table, so we can free it on the spot. |
| 115 | */ |
| 116 | printf("Not adding duplicate key: %d, seqnum: %d\n", |
| 117 | node->value, node->seqnum); |
| 118 | free(node); |
| 119 | } else { |
| 120 | printf("Add key: %d, seqnum: %d\n", |
| 121 | node->value, node->seqnum); |
| 122 | } |
| 123 | rcu_read_unlock(); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Iterate over each hash table node. Those will appear in |
| 128 | * random order, depending on the hash seed. Iteration needs to |
| 129 | * be performed within RCU read-side critical section. |
| 130 | */ |
| 131 | printf("hash table content (random order):"); |
| 132 | rcu_read_lock(); |
| 133 | cds_lfht_for_each_entry(ht, &iter, node, node) { |
| 134 | printf(" (key: %d, seqnum: %d)", |
| 135 | node->value, node->seqnum); |
| 136 | } |
| 137 | rcu_read_unlock(); |
| 138 | printf("\n"); |
| 139 | |
| 140 | end: |
| 141 | rcu_unregister_thread(); |
| 142 | return ret; |
| 143 | } |