doc: update examples to API changes
[urcu.git] / doc / examples / hlist / cds_hlist_add_head_rcu.c
CommitLineData
474190bf
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 add into a non-circular 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 */
474190bf
MD
20#include <urcu/rcuhlist.h> /* RCU hlist */
21#include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
22
23/*
24 * Nodes populated into the list.
25 */
26struct mynode {
27 int value; /* Node content */
28 struct cds_hlist_node node; /* Linked-list chaining */
29};
30
31int main(int argc, char **argv)
32{
33 int values[] = { -5, 42, 36, 24, };
34 CDS_HLIST_HEAD(mylist); /* Defines an empty hlist head */
35 unsigned int i;
36 int ret = 0;
37 struct mynode *node;
38
39 /*
40 * Adding nodes to the linked-list. Safe against concurrent
41 * RCU traversals, require mutual exclusion with list updates.
42 */
43 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
44 node = malloc(sizeof(*node));
45 if (!node) {
46 ret = -1;
47 goto end;
48 }
49 node->value = values[i];
50 cds_hlist_add_head_rcu(&node->node, &mylist);
51 }
52
53 /*
54 * Just show the list content. This is _not_ an RCU-safe
55 * iteration on the list.
56 */
57 printf("mylist content:");
58 cds_hlist_for_each_entry_2(node, &mylist, node) {
59 printf(" %d", node->value);
60 }
61 printf("\n");
62end:
63 return ret;
64}
This page took 0.033338 seconds and 4 git commands to generate.