doc/examples: lfstack
[urcu.git] / doc / examples / lfstack / cds_lfs_push.c
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 push nodes into a lfstack.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <urcu/lfstack.h> /* Lock-free stack */
20 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
21
22 /*
23 * Nodes populated into the stack.
24 */
25 struct mynode {
26 int value; /* Node content */
27 struct cds_lfs_node node; /* Chaining in stack */
28 };
29
30 int main(int argc, char **argv)
31 {
32 int values[] = { -5, 42, 36, 24, };
33 struct cds_lfs_stack mystack; /* Stack */
34 unsigned int i;
35 int ret = 0;
36 struct cds_lfs_node *snode;
37 struct cds_lfs_head *shead;
38
39 cds_lfs_init(&mystack);
40
41 /*
42 * Push nodes.
43 */
44 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
45 struct mynode *node;
46
47 node = malloc(sizeof(*node));
48 if (!node) {
49 ret = -1;
50 goto end;
51 }
52
53 cds_lfs_node_init(&node->node);
54 node->value = values[i];
55 cds_lfs_push(&mystack, &node->node);
56 }
57
58 /*
59 * Show the stack content, iterate in reverse order of push,
60 * from newest to oldest.
61 */
62 printf("mystack content:");
63 shead = cds_lfs_pop_all_blocking(&mystack);
64 cds_lfs_for_each(shead, snode) {
65 struct mynode *node =
66 caa_container_of(snode, struct mynode, node);
67 printf(" %d", node->value);
68 }
69 printf("\n");
70 end:
71 return ret;
72 }
This page took 0.03019 seconds and 4 git commands to generate.