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 add nodes into a RCU lock-free hash table
14 * with cds_lfht_add_replace(), which replaces existing nodes with the
16 * We use a "seqnum" field to show which node is staying in the hash
17 * table. This hash table requires using a RCU scheme.
24 #include <urcu/urcu-memb.h> /* RCU flavor */
25 #include <urcu/rculfhash.h> /* RCU Lock-free hash table */
26 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
27 #include "jhash.h" /* Example hash function */
30 * Nodes populated into the hash table.
33 int value
; /* Node content */
34 int seqnum
; /* Our node sequence number */
35 struct cds_lfht_node node
; /* Chaining in hash table */
36 struct rcu_head rcu_head
; /* For call_rcu() */
40 int match(struct cds_lfht_node
*ht_node
, const void *_key
)
43 caa_container_of(ht_node
, struct mynode
, node
);
44 const int *key
= _key
;
46 return *key
== node
->value
;
50 void free_node(struct rcu_head
*head
)
52 struct mynode
*node
= caa_container_of(head
, struct mynode
, rcu_head
);
59 int values
[] = { -5, 42, 42, 36, 24, }; /* 42 is duplicated */
60 struct cds_lfht
*ht
; /* Hash table */
62 int ret
= 0, seqnum
= 0;
64 struct cds_lfht_iter iter
; /* For iteration on hash table */
65 struct cds_lfht_node
*ht_node
;
69 * Each thread need using RCU read-side need to be explicitly
72 urcu_memb_register_thread();
74 /* Use time as seed for hash table hashing. */
75 seed
= (uint32_t) time(NULL
);
78 * Allocate hash table.
80 ht
= cds_lfht_new_flavor(1, 1, 0,
81 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
82 &urcu_memb_flavor
, NULL
);
84 printf("Error allocating hash table\n");
90 * Add nodes to hash table.
92 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
96 node
= malloc(sizeof(*node
));
102 cds_lfht_node_init(&node
->node
);
105 node
->seqnum
= seqnum
++;
106 hash
= jhash(&value
, sizeof(value
), seed
);
109 * cds_lfht_add() needs to be called from RCU read-side
112 urcu_memb_read_lock();
113 ht_node
= cds_lfht_add_replace(ht
, hash
, match
, &value
,
116 struct mynode
*ret_node
=
117 caa_container_of(ht_node
, struct mynode
, node
);
119 printf("Replaced node (key: %d, seqnum: %d) by (key: %d, seqnum: %d)\n",
120 ret_node
->value
, ret_node
->seqnum
,
121 node
->value
, node
->seqnum
);
122 urcu_memb_call_rcu(&ret_node
->rcu_head
, free_node
);
124 printf("Add (key: %d, seqnum: %d)\n",
125 node
->value
, node
->seqnum
);
127 urcu_memb_read_unlock();
131 * Iterate over each hash table node. Those will appear in
132 * random order, depending on the hash seed. Iteration needs to
133 * be performed within RCU read-side critical section.
135 printf("hash table content (random order):");
136 urcu_memb_read_lock();
137 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
138 printf(" (key: %d, seqnum: %d)",
139 node
->value
, node
->seqnum
);
141 urcu_memb_read_unlock();
145 urcu_memb_unregister_thread();
This page took 0.032981 seconds and 5 git commands to generate.