uatomic/x86: Remove redundant memory barriers
[urcu.git] / doc / examples / list / cds_list_for_each_entry_rcu.c
1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 //
3 // SPDX-License-Identifier: MIT
4
5 /*
6 * This example shows how to do a RCU linked list traversal, safely
7 * against concurrent RCU updates.
8 */
9
10 #include <stdio.h>
11
12 #include <urcu/urcu-memb.h> /* Userspace RCU flavor */
13 #include <urcu/rculist.h> /* RCU list */
14 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
15
16 /*
17 * Nodes populated into the list.
18 */
19 struct mynode {
20 int value; /* Node content */
21 struct cds_list_head node; /* Linked-list chaining */
22 };
23
24 int main(void)
25 {
26 int values[] = { -5, 42, 36, 24, };
27 CDS_LIST_HEAD(mylist); /* Defines an empty list head */
28 unsigned int i;
29 int ret = 0;
30 struct mynode *node;
31
32 /*
33 * Each thread need using RCU read-side need to be explicitly
34 * registered.
35 */
36 urcu_memb_register_thread();
37
38 /*
39 * Adding nodes to the linked-list. Safe against concurrent
40 * RCU traversals, require mutual exclusion with list updates.
41 */
42 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
43 node = malloc(sizeof(*node));
44 if (!node) {
45 ret = -1;
46 goto end;
47 }
48 node->value = values[i];
49 cds_list_add_tail_rcu(&node->node, &mylist);
50 }
51
52 /*
53 * RCU-safe iteration on the list.
54 */
55 printf("mylist content:");
56
57 /*
58 * Surround the RCU read-side critical section with urcu_memb_read_lock()
59 * and urcu_memb_read_unlock().
60 */
61 urcu_memb_read_lock();
62
63 /*
64 * This traversal can be performed concurrently with RCU
65 * updates.
66 */
67 cds_list_for_each_entry_rcu(node, &mylist, node) {
68 printf(" %d", node->value);
69 }
70
71 urcu_memb_read_unlock();
72
73 printf("\n");
74 end:
75 urcu_memb_unregister_thread();
76 return ret;
77 }
This page took 0.031806 seconds and 5 git commands to generate.