Commit | Line | Data |
---|---|---|
7eba9f88 MD |
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 replace a node within a linked-list safely | |
14 | * against concurrent RCU traversals. | |
15 | */ | |
16 | ||
17 | #include <stdio.h> | |
18 | ||
b9050d91 | 19 | #include <urcu/urcu-memb.h> /* Userspace RCU flavor */ |
7eba9f88 MD |
20 | #include <urcu/rculist.h> /* RCU list */ |
21 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ | |
22 | ||
23 | /* | |
24 | * Nodes populated into the list. | |
25 | */ | |
26 | struct mynode { | |
27 | int value; /* Node content */ | |
28 | struct cds_list_head node; /* Linked-list chaining */ | |
29 | struct rcu_head rcu_head; /* For call_rcu() */ | |
30 | }; | |
31 | ||
32 | static | |
33 | void free_node_rcu(struct rcu_head *head) | |
34 | { | |
35 | struct mynode *node = caa_container_of(head, struct mynode, rcu_head); | |
36 | ||
37 | free(node); | |
38 | } | |
39 | ||
70469b43 | 40 | int main(void) |
7eba9f88 MD |
41 | { |
42 | int values[] = { -5, 42, 36, 24, }; | |
43 | CDS_LIST_HEAD(mylist); /* Defines an empty list head */ | |
44 | unsigned int i; | |
45 | int ret = 0; | |
46 | struct mynode *node, *n; | |
47 | ||
48 | /* | |
49 | * Adding nodes to the linked-list. Safe against concurrent | |
50 | * RCU traversals, require mutual exclusion with list updates. | |
51 | */ | |
52 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { | |
53 | node = malloc(sizeof(*node)); | |
54 | if (!node) { | |
55 | ret = -1; | |
56 | goto end; | |
57 | } | |
58 | node->value = values[i]; | |
59 | cds_list_add_tail_rcu(&node->node, &mylist); | |
60 | } | |
61 | ||
62 | /* | |
63 | * Replacing all values by their negated value. Safe against | |
64 | * concurrent RCU traversals, require mutual exclusion with list | |
65 | * updates. Notice the "safe" iteration: it is safe against | |
66 | * removal (and thus replacement) of nodes as we iterate on the | |
67 | * list. | |
68 | */ | |
69 | cds_list_for_each_entry_safe(node, n, &mylist, node) { | |
70 | struct mynode *new_node; | |
71 | ||
72 | new_node = malloc(sizeof(*node)); | |
73 | if (!new_node) { | |
74 | ret = -1; | |
75 | goto end; | |
76 | } | |
77 | /* Replacement node value is negated original value. */ | |
78 | new_node->value = -node->value; | |
79 | cds_list_replace_rcu(&node->node, &new_node->node); | |
b9050d91 | 80 | urcu_memb_call_rcu(&node->rcu_head, free_node_rcu); |
7eba9f88 MD |
81 | } |
82 | ||
83 | /* | |
84 | * Just show the list content. This is _not_ an RCU-safe | |
85 | * iteration on the list. | |
86 | */ | |
87 | printf("mylist content:"); | |
88 | cds_list_for_each_entry(node, &mylist, node) { | |
89 | printf(" %d", node->value); | |
90 | } | |
91 | printf("\n"); | |
92 | end: | |
93 | return ret; | |
94 | } |