56b9df65359f903759df99ad87e77c9aa5bc93e0
2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
13 * This example shows how to use cds_lfht_destroy() to clear memory used
14 * by a a RCU lock-free hash table. This hash table requires using a
22 #include <urcu/urcu-memb.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 */
28 * Nodes populated into the hash table.
31 int value
; /* Node content */
32 struct cds_lfht_node node
; /* Chaining in hash table */
33 struct rcu_head rcu_head
; /* For call_rcu() */
37 void free_node(struct rcu_head
*head
)
39 struct mynode
*node
= caa_container_of(head
, struct mynode
, rcu_head
);
46 int values
[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
47 struct cds_lfht
*ht
; /* Hash table */
51 struct cds_lfht_iter iter
; /* For iteration on hash table */
52 struct cds_lfht_node
*ht_node
;
56 * Each thread need using RCU read-side need to be explicitly
59 urcu_memb_register_thread();
61 /* Use time as seed for hash table hashing. */
62 seed
= (uint32_t) time(NULL
);
65 * Allocate hash table.
67 ht
= cds_lfht_new_flavor(1, 1, 0,
68 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
69 &urcu_memb_flavor
, NULL
);
71 printf("Error allocating hash table\n");
77 * Add nodes to hash table.
79 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
83 node
= malloc(sizeof(*node
));
89 cds_lfht_node_init(&node
->node
);
92 hash
= jhash(&value
, sizeof(value
), seed
);
95 * cds_lfht_add() needs to be called from RCU read-side
98 urcu_memb_read_lock();
99 cds_lfht_add(ht
, hash
, &node
->node
);
100 urcu_memb_read_unlock();
104 * Iterate over each hash table node. Those will appear in
105 * random order, depending on the hash seed. Iteration needs to
106 * be performed within RCU read-side critical section.
108 printf("hash table content (random order):");
109 urcu_memb_read_lock();
110 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
111 printf(" %d", node
->value
);
113 urcu_memb_read_unlock();
118 * Make sure all hash table nodes are removed before destroying.
120 printf("removing all nodes:");
121 urcu_memb_read_lock();
122 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
123 ht_node
= cds_lfht_iter_get_node(&iter
);
124 ret
= cds_lfht_del(ht
, ht_node
);
125 printf(" %d", node
->value
);
127 printf(" (concurrently deleted)");
129 urcu_memb_call_rcu(&node
->rcu_head
, free_node
);
132 urcu_memb_read_unlock();
136 * cds_lfht_destroy() must be called from a very specific
137 * context: it needs to be called from a registered RCU reader
138 * thread. However, this thread should _not_ be within a RCU
139 * read-side critical section. Also, it should _not_ be called
140 * from a call_rcu thread.
142 ret
= cds_lfht_destroy(ht
, NULL
);
144 printf("Destroying hash table failed\n");
147 urcu_memb_unregister_thread();
This page took 0.031022 seconds and 3 git commands to generate.